InventoryCompareService.php 8.1 KB

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