InventoryCompareService.php 9.0 KB

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