InventoryCompareService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\QueryService;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\DB;
  12. use Overtrue\Pinyin\Pinyin;
  13. use Ramsey\Uuid\Uuid;
  14. class InventoryCompareService
  15. {
  16. static private $missionCode;
  17. // private function conditionQuery($SKU,$LotAtt05,$descr_c){
  18. // $sql='select * from (select result.*,rownum rn from (';
  19. // $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  20. // $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  21. // $sql.=' lot.LotAtt04 批号 ';
  22. // $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  23. // $sql.=' INV_LOT_LOC_ID storeStatus';
  24. // $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  25. // $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  26. // $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  27. // $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  28. // $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  29. // $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  30. // $sql.=' )result where 1=1 ';
  31. // if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  32. // if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  33. // if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
  34. // $sql.=' ) ';
  35. // return DB::connection('oracle')->select($sql);
  36. // }
  37. public function getCreatingMissionCode($ownerName=''){
  38. if(self::$missionCode)return self::$missionCode;
  39. $sequence=Cache::get('InventoryCompareMissionSequence');
  40. if(!$sequence||$sequence>998)$sequence=0;
  41. $sequence++;
  42. Cache::put('InventoryCompareMissionSequence',$sequence,300);
  43. $pinyin=new Pinyin();
  44. $ownerFirstLetter=strtoupper($pinyin->abbr($ownerName));
  45. self::$missionCode= $ownerFirstLetter.date ("ymd").'KCBD'.str_pad($sequence,3,"0",STR_PAD_LEFT);
  46. return self::$missionCode;
  47. }
  48. public function createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name){
  49. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  50. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  51. $inventoryCompare=new InventoryCompare();
  52. $inventoryCompare->owner_id=$owner_id;
  53. $inventoryCompare->commodity_id=$commodityId;
  54. $inventoryCompare->mission_code=Uuid::uuid1();
  55. $inventoryCompare->custom_location=$custom_location;
  56. $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
  57. $inventoryCompare->quality='正品';
  58. $inventoryCompare->amount_in_sys=0;
  59. $inventoryCompare->amount_in_compare=$amount;
  60. $inventoryCompare->differ=0-$amount;
  61. $inventoryCompare->mission_code= $creatingMissionCode;
  62. $inventoryCompare->save();
  63. Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
  64. return $inventoryCompare;
  65. }
  66. public function getInventoryCompare(array $param){
  67. return $this->conditionQueryInventoryCompare($param)->paginate($param['paginate'] ?? 50);
  68. }
  69. public function createInventoryCompares($inventoryCompares){
  70. $custom_locations = array_column($inventoryCompares,'custom_location');
  71. $custom_locations = array_unique($custom_locations);
  72. $skus = array_column($inventoryCompares,'sku');
  73. $skus = array_unique($skus);
  74. $query = OracleInvLotLocId::query()
  75. ->leftJoin('INV_LOT_ATT','INV_LOT_LOC_ID.LOTNUM','=','INV_LOT_ATT.LOTNUM')
  76. ->whereIn('INV_LOT_LOC_ID.sku',$skus)
  77. ->whereIn('INV_LOT_ATT.LOTATT05',$custom_locations)
  78. ->selectRaw('INV_LOT_LOC_ID.customerid 货主编码,INV_LOT_ATT.LOTATT05 属性仓,INV_LOT_LOC_ID.sku 产品编码,
  79. INV_LOT_ATT.LOTATT08 质量状态,sum(INV_LOT_LOC_ID.QTY) 在库数量')
  80. ->groupBy(['INV_LOT_ATT.LOTATT05','INV_LOT_LOC_ID.customerid','INV_LOT_LOC_ID.sku','INV_LOT_ATT.LOTATT08'])
  81. ->get();
  82. $wasInventoryCompares=[];
  83. foreach ($inventoryCompares as $inventoryCompare){
  84. $owner_id=$inventoryCompare['owner_id'];
  85. $owner_name=$inventoryCompare['owner_name'];
  86. $owner_code=$inventoryCompare['owner_code'];
  87. $sku=$inventoryCompare['sku'];
  88. $custom_location=$inventoryCompare['custom_location'];
  89. $amount=$inventoryCompare['amount'];
  90. $wmsInventoryCompareZp=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','ZP')->first();
  91. $wmsInventoryCompareCc=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','CC')->first();
  92. $wmsInventoryCompareDj=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->where('质量状态','DJ')->first();
  93. $unknownQualityStatus=$query->where('属性仓',$custom_location)->where('产品编码',$sku)->whereNotIn('质量状态',['DJ','CC','ZP']);
  94. if (!$wmsInventoryCompareZp&&!$wmsInventoryCompareCc&&!$wmsInventoryCompareDj&&$unknownQualityStatus->isEmpty()){
  95. $inventoryCompare=$this->createInventoryCompare_underImport($sku, $custom_location, $amount, $owner_id,$owner_name);
  96. }
  97. $creatingMissionCode = $this->getCreatingMissionCode($owner_name);
  98. $commodityId=Commodity::where('sku',$sku)->where('owner_id',$owner_id)->value('id');
  99. if ($wmsInventoryCompareZp||$wmsInventoryCompareDj) {
  100. $wasInventoryCompareZP = [
  101. 'owner_id' => $owner_id,
  102. 'commodity_id' => $commodityId,
  103. 'mission_code' => $creatingMissionCode,
  104. 'custom_location' => $custom_location,
  105. 'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
  106. 'quality' => '正品',
  107. 'amount_in_sys' => ($wmsInventoryCompareZp['在库数量'] ?? 0) + ($wmsInventoryCompareDj['在库数量'] ?? 0),
  108. 'amount_in_compare' => $amount,
  109. 'differ' => $amount - ($wmsInventoryCompareZp['在库数量'] ?? 0) - ($wmsInventoryCompareDj['在库数量'] ?? 0),
  110. ];
  111. array_push($wasInventoryCompares,$wasInventoryCompareZP);
  112. }
  113. if ($wmsInventoryCompareCc){
  114. $wasInventoryCompareCC=[
  115. 'owner_id'=>$owner_id,
  116. 'commodity_id'=>$commodityId,
  117. 'mission_code'=>$creatingMissionCode,
  118. 'custom_location'=>$custom_location,
  119. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  120. 'quality'=>'次品',
  121. 'amount_in_sys'=>$wmsInventoryCompareCc['在库数量'],
  122. 'amount_in_compare' =>'',
  123. 'differ' =>'',
  124. ];
  125. array_push($wasInventoryCompares,$wasInventoryCompareCC);
  126. }
  127. if($unknownQualityStatus->isNotEmpty())return null;
  128. }
  129. $inventoryCompares=DB::table('inventory_compares')->insert($wasInventoryCompares);
  130. if (!$inventoryCompares)return null;
  131. return $inventoryCompares;
  132. }
  133. private function conditionQueryInventoryCompare(array $param){
  134. $differ=$param['differ']??'';
  135. if ($differ=='有'){
  136. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  137. $query->with('barcodes');
  138. }])->where('differ','>',0)->orderByDesc('id');
  139. }elseif ($differ=='无'){
  140. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  141. $query->with('barcodes');
  142. }])->where('differ','<',0)->orderByDesc('id');
  143. }else{
  144. $inventoryCompares = InventoryCompare::query()->with(['owner','commodity'=>function($query){
  145. $query->with('barcodes');
  146. }])->orderByDesc('id');
  147. }
  148. unset($param['differ']);
  149. $columnQueryRules=[
  150. 'owner_id' => ['multi' => ','],
  151. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  152. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  153. 'mission_code' => ['timeLimit' => 20],
  154. ];
  155. $inventoryCompares = app(QueryService::class)->query($param,$inventoryCompares,$columnQueryRules);
  156. return $inventoryCompares;
  157. }
  158. // public function createInventoryCompare($SKU,$LotAtt05,$amount,$ownerId){
  159. // $ownerName=Owner::where('id',$ownerId)->value('name');
  160. // $request=[
  161. // '产品编码'=>$SKU,
  162. // '属性仓'=>$LotAtt05,
  163. // '数量'=>$amount,
  164. // '货主'=>$ownerId,
  165. // ];
  166. // $wmsInventories=$this->conditionQuery($SKU,$LotAtt05,$ownerName);
  167. // if (count($wmsInventories)==0) return null;
  168. // $zpAmount=0;
  169. // $ccAmount=0;
  170. // $amountTotal=0;
  171. // foreach ($wmsInventories as $wmsInventory){
  172. // $commodity=Commodity::query()->firstOrCreate([
  173. // 'owner_id'=>$ownerId,
  174. // 'sku'=>$wmsInventory->产品编码,
  175. // 'name'=>$wmsInventory->商品名称,
  176. // ]);
  177. // Controller::logS(__METHOD__,"根据wms产品编码和货主查询或创建商品信息__".__FUNCTION__,json_encode($wmsInventory));
  178. // $commodity->newBarcode($wmsInventory->产品条码);
  179. // Controller::logS(__METHOD__,"根据wms产品条码和商品id查询或创建商品条码信息__".__FUNCTION__,json_encode($wmsInventory));
  180. // $amountTotal=$amountTotal+$wmsInventory->在库数量;
  181. // if ($wmsInventory->质量状态=='DJ'||$wmsInventory->质量状态=='ZP'){
  182. // $zpAmount=$zpAmount+$wmsInventory->在库数量;
  183. // }
  184. // if ($wmsInventory->质量状态=='CC'){
  185. // $ccAmount=$ccAmount+$wmsInventory->在库数量;
  186. // }
  187. // }
  188. // $unknownStatusAmount=$amountTotal-$ccAmount-$zpAmount;
  189. // $creatingMissionCode = $this->getCreatingMissionCode($ownerName);
  190. // $commodityId=Commodity::where('sku',$SKU)->where('owner_id',$ownerId)->value('id');
  191. // $inventoryCompare=new InventoryCompare();
  192. // $inventoryCompare->owner_id=$ownerId;
  193. // $inventoryCompare->commodity_id=$commodityId;
  194. // $inventoryCompare->mission_code=Uuid::uuid1();
  195. // $inventoryCompare->custom_location=$LotAtt05;
  196. // $inventoryCompare->created_at=Carbon::now()->format('Y-m-d H:i:s');
  197. // $inventoryCompare->mission_code=$creatingMissionCode;
  198. // if ($zpAmount!=0){
  199. // $inventoryCompare->quality='正品';
  200. // $inventoryCompare->amount_in_sys=$zpAmount;
  201. // $inventoryCompare->amount_in_compare=$amount;
  202. // $inventoryCompare->differ=$zpAmount-$amount;
  203. // }
  204. // if ($ccAmount!=0){
  205. // $inventoryCompare->quality='次品';
  206. // $inventoryCompare['amount_in_sys']=$ccAmount;
  207. // }
  208. // if ($unknownStatusAmount!=0){
  209. // $inventoryCompare->quality='未知';
  210. // $inventoryCompare->amount_in_sys=$unknownStatusAmount;
  211. // }
  212. // $inventoryCompare->save();
  213. // Controller::logS(__METHOD__,"创建库存对比__".__FUNCTION__,json_encode($request));
  214. // Controller::logS(__METHOD__,"修改库存对比任务号__".__FUNCTION__,json_encode($creatingMissionCode));
  215. // return $inventoryCompare;
  216. // }
  217. //
  218. }