InventoryCompareService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. public function getCreatingMissionCode($ownerName=''){
  19. if(self::$missionCode)return self::$missionCode;
  20. $sequence=Cache::get('InventoryCompareMissionSequence');
  21. if(!$sequence||$sequence>998)$sequence=0;
  22. $sequence++;
  23. Cache::put('InventoryCompareMissionSequence',$sequence,300);
  24. $pinyin=new Pinyin();
  25. $ownerFirstLetter=strtoupper($pinyin->abbr($ownerName));
  26. self::$missionCode= $ownerFirstLetter.date ("ymd").'KCBD'.str_pad($sequence,3,"0",STR_PAD_LEFT);
  27. return self::$missionCode;
  28. }
  29. public function createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name){
  30. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  31. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  32. $inventoryCompare=new InventoryCompare();
  33. $inventoryCompare->owner_id=$owner_id;
  34. $inventoryCompare->commodity_id=$commodityId;
  35. $inventoryCompare->mission_code=Uuid::uuid1();
  36. $inventoryCompare->custom_location=$custom_location;
  37. $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
  38. $inventoryCompare->quality='正品';
  39. $inventoryCompare->amount_in_sys=0;
  40. $inventoryCompare->amount_in_compare=$amount;
  41. $inventoryCompare->differ=$amount-0;
  42. $inventoryCompare->mission_code= $creatingMissionCode;
  43. $inventoryCompare->save();
  44. Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
  45. return $inventoryCompare;
  46. }
  47. public function getInventoryCompare(array $param){
  48. return $this->conditionQueryInventoryCompare($param)->paginate($param['paginate'] ?? 50);
  49. }
  50. public function createInventoryCompares($inventoryCompares){
  51. $custom_locations = array_column($inventoryCompares,'custom_location');
  52. $custom_locations = array_unique($custom_locations);
  53. $skus = array_column($inventoryCompares,'sku');
  54. $skus = array_unique($skus);
  55. $query = OracleInvLotLocId::query()
  56. ->leftJoin('INV_LOT_ATT','INV_LOT_LOC_ID.LOTNUM','=','INV_LOT_ATT.LOTNUM')
  57. ->whereIn('INV_LOT_LOC_ID.sku',$skus)
  58. ->whereIn('INV_LOT_ATT.LOTATT05',$custom_locations)
  59. ->selectRaw('INV_LOT_LOC_ID.customerid 货主编码,INV_LOT_ATT.LOTATT05 属性仓,INV_LOT_LOC_ID.sku 产品编码,
  60. INV_LOT_ATT.LOTATT08 质量状态,sum(INV_LOT_LOC_ID.QTY) 在库数量')
  61. ->groupBy(['INV_LOT_ATT.LOTATT05','INV_LOT_LOC_ID.customerid','INV_LOT_LOC_ID.sku','INV_LOT_ATT.LOTATT08'])
  62. ->get();
  63. $wasInventoryCompares=[];
  64. foreach ($inventoryCompares as $inventoryCompare){
  65. $owner_id=$inventoryCompare['owner_id'];
  66. $owner_name=$inventoryCompare['owner_name'];
  67. $owner_code=$inventoryCompare['owner_code'];
  68. $sku=$inventoryCompare['sku'];
  69. $custom_location=$inventoryCompare['custom_location'];
  70. $amount=$inventoryCompare['amount'];
  71. $wmsInventoryCompareZp=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','ZP')->first();
  72. $wmsInventoryCompareCc=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','CC')->first();
  73. $wmsInventoryCompareDj=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','DJ')->first();
  74. $wmsInventoryCompareYjz=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','YJZ')->first();
  75. $unknownQualityStatus=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->whereNotIn('质量状态',['DJ','CC','ZP','YJZ']);
  76. if (!$wmsInventoryCompareZp&&!$wmsInventoryCompareCc&&!$wmsInventoryCompareDj&&$unknownQualityStatus->isEmpty()){
  77. $this->createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name);
  78. continue;
  79. }
  80. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  81. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  82. $params = [
  83. 'owner_id' => $owner_id,
  84. 'commodity_id' => $commodityId,
  85. 'mission_code' => $creatingMissionCode,
  86. 'custom_location' => $custom_location,
  87. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  88. 'amount_in_compare'=>$amount,
  89. ];
  90. if ($wmsInventoryCompareZp||$wmsInventoryCompareDj) {
  91. $params['quality'] = '正品';
  92. $params['amount_in_sys'] = ($wmsInventoryCompareZp['在库数量'] ?? 0)+($wmsInventoryCompareDj['在库数量'] ?? 0);
  93. $params['differ'] = $amount-(($wmsInventoryCompareZp['在库数量'] ?? 0) + ($wmsInventoryCompareDj['在库数量'] ?? 0));
  94. array_push($wasInventoryCompares,$params);
  95. }
  96. if ($wmsInventoryCompareCc){
  97. $params['quality'] = '次品';
  98. $params['amount_in_sys'] = $wmsInventoryCompareCc['在库数量'];
  99. $params['differ'] = $amount-($wmsInventoryCompareCc['在库数量']??0);
  100. array_push($wasInventoryCompares,$params);
  101. }
  102. if ($wmsInventoryCompareYjz){
  103. $params['quality'] = '有价值';
  104. $params['amount_in_sys'] = $wmsInventoryCompareYjz['在库数量'];
  105. $params['differ'] = $amount-($wmsInventoryCompareYjz['在库数量']??0);
  106. array_push($wasInventoryCompares,$params);
  107. }
  108. if($unknownQualityStatus->isNotEmpty())continue;
  109. }
  110. $inventoryCompares=DB::table('inventory_compares')->insert($wasInventoryCompares);
  111. if (!$inventoryCompares)return null;
  112. return $inventoryCompares;
  113. }
  114. private function conditionQueryInventoryCompare(array $param){
  115. $ownerIds=app('OwnerService')->getSelectionId();
  116. $differ=$param['differ']??'';
  117. if ($differ=='有'){
  118. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  119. $query->with('barcodes');
  120. }])->where('differ','>',0)->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  121. }elseif ($differ=='无'){
  122. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  123. $query->with('barcodes');
  124. }])->where('differ','<',0)->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  125. }else{
  126. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  127. $query->with('barcodes');
  128. }])->orderByDesc('id')->whereIn('inventory_compares.owner_id',$ownerIds);
  129. }
  130. unset($param['differ']);
  131. $columnQueryRules=[
  132. 'owner_id' => ['multi' => ','],
  133. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  134. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  135. 'mission_code' => ['timeLimit' => 20],
  136. 'id' => ['multi' => ','],
  137. ];
  138. $inventoryCompares = app(QueryService::class)->query($param,$inventoryCompares,$columnQueryRules,'inventory_compares');
  139. return $inventoryCompares;
  140. }
  141. /**
  142. * @param array $params
  143. * @return string $sql
  144. */
  145. public function getSql(array $params){
  146. return $this->conditionQueryInventoryCompare($params)
  147. ->selectRaw("inventory_compares.*")
  148. ->leftJoin('owners','inventory_compares.owner_id','owners.id')
  149. ->selectRaw('owners.name owner_name')
  150. ->leftJoin('commodities','inventory_compares.commodity_id','commodities.id')
  151. ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku')
  152. ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
  153. ->selectRaw('commodity_barcodes.code commodity_barcode_code')
  154. ->groupBy('owner_name','commodity_sku','inventory_compares.custom_location','inventory_compares.quality')
  155. ->sql();
  156. }
  157. public function batchUpdateItself($column, array $params)
  158. {
  159. return app(BatchUpdateService::class)->batchUpdateItself('inventory_compares', $column, $params);
  160. }
  161. }