InventoryCompareService.php 7.3 KB

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