InventoryCompareService.php 9.1 KB

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