InventoryCompareService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\Http\Controllers\Controller;
  5. use App\InventoryCompare;
  6. use App\OracleInvLotLocId;
  7. use App\Owner;
  8. use App\Services\common\BatchUpdateService;
  9. use App\Services\common\QueryService;
  10. use Carbon\Carbon;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. use Overtrue\Pinyin\Pinyin;
  14. use Ramsey\Uuid\Uuid;
  15. class InventoryCompareService
  16. {
  17. static private $missionCode;
  18. // private function conditionQuery($SKU,$LotAtt05,$descr_c){
  19. // $sql='select * from (select result.*,rownum rn from (';
  20. // $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  21. // $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  22. // $sql.=' lot.LotAtt04 批号 ';
  23. // $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  24. // $sql.=' INV_LOT_LOC_ID storeStatus';
  25. // $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  26. // $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  27. // $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  28. // $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  29. // $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  30. // $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  31. // $sql.=' )result where 1=1 ';
  32. // if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  33. // if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  34. // if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
  35. // $sql.=' ) ';
  36. // return DB::connection('oracle')->select($sql);
  37. // }
  38. public function getCreatingMissionCode($ownerName=''){
  39. if(self::$missionCode)return self::$missionCode;
  40. $sequence=Cache::get('InventoryCompareMissionSequence');
  41. if(!$sequence||$sequence>998)$sequence=0;
  42. $sequence++;
  43. Cache::put('InventoryCompareMissionSequence',$sequence,300);
  44. $pinyin=new Pinyin();
  45. $ownerFirstLetter=strtoupper($pinyin->abbr($ownerName));
  46. self::$missionCode= $ownerFirstLetter.date ("ymd").'KCBD'.str_pad($sequence,3,"0",STR_PAD_LEFT);
  47. return self::$missionCode;
  48. }
  49. public function createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name){
  50. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  51. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  52. $inventoryCompare=new InventoryCompare();
  53. $inventoryCompare->owner_id=$owner_id;
  54. $inventoryCompare->commodity_id=$commodityId;
  55. $inventoryCompare->mission_code=Uuid::uuid1();
  56. $inventoryCompare->custom_location=$custom_location;
  57. $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
  58. $inventoryCompare->quality='正品';
  59. $inventoryCompare->amount_in_sys=0;
  60. $inventoryCompare->amount_in_compare=$amount;
  61. $inventoryCompare->differ=$amount-0;
  62. $inventoryCompare->mission_code= $creatingMissionCode;
  63. $inventoryCompare->save();
  64. Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
  65. return $inventoryCompare;
  66. }
  67. public function getInventoryCompare(array $param){
  68. return $this->conditionQueryInventoryCompare($param)->paginate($param['paginate'] ?? 50);
  69. }
  70. public function createInventoryCompares($inventoryCompares){
  71. $custom_locations = array_column($inventoryCompares,'custom_location');
  72. $custom_locations = array_unique($custom_locations);
  73. $skus = array_column($inventoryCompares,'sku');
  74. $skus = array_unique($skus);
  75. $query = OracleInvLotLocId::query()
  76. ->leftJoin('INV_LOT_ATT','INV_LOT_LOC_ID.LOTNUM','=','INV_LOT_ATT.LOTNUM')
  77. ->whereIn('INV_LOT_LOC_ID.sku',$skus)
  78. ->whereIn('INV_LOT_ATT.LOTATT05',$custom_locations)
  79. ->selectRaw('INV_LOT_LOC_ID.customerid 货主编码,INV_LOT_ATT.LOTATT05 属性仓,INV_LOT_LOC_ID.sku 产品编码,
  80. INV_LOT_ATT.LOTATT08 质量状态,sum(INV_LOT_LOC_ID.QTY) 在库数量')
  81. ->groupBy(['INV_LOT_ATT.LOTATT05','INV_LOT_LOC_ID.customerid','INV_LOT_LOC_ID.sku','INV_LOT_ATT.LOTATT08'])
  82. ->get();
  83. $wasInventoryCompares=[];
  84. foreach ($inventoryCompares as $inventoryCompare){
  85. $owner_id=$inventoryCompare['owner_id'];
  86. $owner_name=$inventoryCompare['owner_name'];
  87. $owner_code=$inventoryCompare['owner_code'];
  88. $sku=$inventoryCompare['sku'];
  89. $custom_location=$inventoryCompare['custom_location'];
  90. $amount=$inventoryCompare['amount'];
  91. $wmsInventoryCompareZp=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','ZP')->first();
  92. $wmsInventoryCompareCc=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','CC')->first();
  93. $wmsInventoryCompareDj=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','DJ')->first();
  94. $unknownQualityStatus=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->whereNotIn('质量状态',['DJ','CC','ZP']);
  95. if (!$wmsInventoryCompareZp&&!$wmsInventoryCompareCc&&!$wmsInventoryCompareDj&&$unknownQualityStatus->isEmpty()){
  96. $this->createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name);
  97. }
  98. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  99. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  100. if ($wmsInventoryCompareZp||$wmsInventoryCompareDj) {
  101. $wasInventoryCompareZP = [
  102. 'owner_id' => $owner_id,
  103. 'commodity_id' => $commodityId,
  104. 'mission_code' => $creatingMissionCode,
  105. 'custom_location' => $custom_location,
  106. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  107. 'quality' => '正品',
  108. 'amount_in_sys' => ($wmsInventoryCompareZp['在库数量'] ?? 0)+($wmsInventoryCompareDj['在库数量'] ?? 0),
  109. 'amount_in_compare' => $amount,
  110. 'differ' => $amount-(($wmsInventoryCompareZp['在库数量'] ?? 0) + ($wmsInventoryCompareDj['在库数量'] ?? 0)),
  111. ];
  112. array_push($wasInventoryCompares,$wasInventoryCompareZP);
  113. }
  114. if ($wmsInventoryCompareCc){
  115. $wasInventoryCompareCC=[
  116. 'owner_id'=>$owner_id,
  117. 'commodity_id'=>$commodityId,
  118. 'mission_code'=>$creatingMissionCode,
  119. 'custom_location'=>$custom_location,
  120. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  121. 'quality'=>'次品',
  122. 'amount_in_sys'=>$wmsInventoryCompareCc['在库数量'],
  123. 'amount_in_compare' =>$amount,
  124. 'differ' =>$amount-($wmsInventoryCompareCc['在库数量']??0),
  125. ];
  126. array_push($wasInventoryCompares,$wasInventoryCompareCC);
  127. }
  128. if($unknownQualityStatus->isNotEmpty())return null;
  129. }
  130. $inventoryCompares=DB::table('inventory_compares')->insert($wasInventoryCompares);
  131. if (!$inventoryCompares)return null;
  132. return $inventoryCompares;
  133. }
  134. private function conditionQueryInventoryCompare(array $param){
  135. $ownerIds=app('OwnerService')->getSelectionId();
  136. $differ=$param['differ']??'';
  137. if ($differ=='有'){
  138. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  139. $query->with('barcodes');
  140. }])->where('differ','>',0)->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  141. }elseif ($differ=='无'){
  142. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  143. $query->with('barcodes');
  144. }])->where('differ','<',0)->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  145. }else{
  146. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  147. $query->with('barcodes');
  148. }])->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  149. }
  150. unset($param['differ']);
  151. $columnQueryRules=[
  152. 'owner_id' => ['multi' => ','],
  153. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  154. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  155. 'mission_code' => ['timeLimit' => 20],
  156. 'id' => ['multi' => ','],
  157. ];
  158. $inventoryCompares = app(QueryService::class)->query($param,$inventoryCompares,$columnQueryRules,'inventory_compares');
  159. return $inventoryCompares;
  160. }
  161. /**
  162. * @param array $params
  163. * @return string $sql
  164. */
  165. public function getSql(array $params){
  166. return $this->conditionQueryInventoryCompare($params)
  167. ->selectRaw("inventory_compares.*")
  168. ->leftJoin('owners','inventory_compares.owner_id','owners.id')
  169. ->selectRaw('owners.name owner_name')
  170. ->leftJoin('commodities','inventory_compares.commodity_id','commodities.id')
  171. ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku')
  172. ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
  173. ->selectRaw('commodity_barcodes.code commodity_barcode_code')
  174. ->groupBy('owner_name','commodity_sku','inventory_compares.custom_location','inventory_compares.quality')
  175. ->sql();
  176. }
  177. public function batchUpdateItself($column, array $params)
  178. {
  179. return app(BatchUpdateService::class)->batchUpdateItself('inventory_compares', $column, $params);
  180. }
  181. }