InventoryAccountService.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\Http\Controllers\Controller;
  5. use App\InventoryAccount;
  6. use App\InventoryAccountMission;
  7. use App\OracleBasSKU;
  8. use App\OracleInvLotAtt;
  9. use App\OracleInvLotLocId;
  10. use App\Owner;
  11. use App\Services\common\BatchUpdateService;
  12. use App\Services\common\DataHandlerService;
  13. use App\Services\common\QueryService;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\DB;
  17. use App\Traits\ServiceAppAop;
  18. class InventoryAccountService
  19. {
  20. use ServiceAppAop;
  21. protected $modelClass=InventoryAccount::class;
  22. private function conditionQueryInventoryAccountMission(array $params){
  23. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->orderBy('difference_amount','desc');
  24. $columnQueryRules=[];
  25. $inventoryAccountMissions = app(QueryService::class)->query($params,$inventoryAccountMissions,$columnQueryRules,'inventory_account_missions');
  26. return $inventoryAccountMissions;
  27. }
  28. public function getSql(array $params){
  29. return $this->conditionQueryInventoryAccountMission($params)->selectRaw("inventory_account_missions.*")
  30. ->leftJoin('signs','inventory_account_missions.id','signs.signable_id')
  31. ->selectRaw('signs.mark stockInventoryPerson')
  32. ->leftJoin('commodities','inventory_account_missions.commodity_id','commodities.id')
  33. ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku')
  34. ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
  35. ->selectRaw('commodity_barcodes.code commodity_barcode_code')
  36. ->sql();
  37. }
  38. private function conditionQuery($queryParam){
  39. $ownerIds=app('OwnerService')->getSelection();
  40. $inventories=InventoryAccount::query()->with(['owner','creator'])->orderBy('id','desc')->whereIn('owner_id',$ownerIds);
  41. $columnQueryRules=[
  42. 'owner' => ['alias'=>'owner_id','multi' => ','],
  43. 'type' => ['multi' => ','],
  44. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  45. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  46. ];
  47. $inventories = app(QueryService::class)->query($queryParam,$inventories,$columnQueryRules);
  48. return $inventories;
  49. }
  50. public function paginate($queryParam){
  51. $inventories = $this->conditionQuery($queryParam);
  52. return $inventories->paginate($queryParam['paginate'] ?? 50);
  53. }
  54. public function get($queryParam){
  55. $inventories = $this->conditionQuery($queryParam);
  56. return $inventories->get();
  57. }
  58. public function some($queryParam){
  59. return InventoryAccount::query()->with(['owner'])->orderBy('id','DESC')
  60. ->whereIn('id',explode(',',$queryParam['data']))->get();
  61. }
  62. //动盘查询
  63. public function conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode){
  64. if (!$ownerId) return null;
  65. $code=Owner::where('id',$ownerId)->value('code');
  66. $sql='select * from (select result.*,rownum rn from (';
  67. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  68. $sql.=' sku.Descr_C 商品名称, MAX(lot.LotAtt05) 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  69. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期';
  70. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  71. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  72. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  73. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  74. if ($code)$sql.=" and TOCustomerID = '".$code."' ";
  75. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  76. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  77. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  78. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  79. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  80. if ($code)$sql.=" and FMCUSTOMERID = '".$code."' ";
  81. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  82. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  83. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  84. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  85. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  86. if ($code)$sql.=" and FMCUSTOMERID = '".$code."' ";
  87. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  88. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  89. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  90. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  91. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  92. if ($code)$sql.=" and TOCustomerID = '".$code."' ";
  93. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  94. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  95. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  96. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  97. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  98. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  99. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  100. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  101. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  102. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt01,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  103. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  104. $sql.=' )result where 1=1 ';
  105. if ($code)$sql.=" and 客户 = '".$code."' ";
  106. // if ($location)$sql.=" and 库位 like '".$location."%' ";
  107. if ($location){
  108. $arr=array_filter(preg_split('/[,, ]+/is', $location));
  109. if (count($arr)==1){
  110. $sql.=" and 库位 like '".$location."%'";
  111. }
  112. if (count($arr)>1){
  113. $sql.=" and (库位 like ";
  114. foreach ($arr as $index=>$str){
  115. if ($index==0){
  116. $sql.="'".$str."%'";
  117. continue;
  118. }
  119. $sql.= "or 库位 like'".$str."%'";
  120. }
  121. $sql.=")";
  122. }
  123. }
  124. if ($barcode)$sql=$this->按条码查询($sql,$barcode);
  125. $sql.=' ) ';
  126. return DB::connection('oracle')->select($sql);
  127. }
  128. //全盘查询
  129. public function conditionTotalStock($ownerId,$location,$barcode){
  130. $code=Owner::where('id',$ownerId)->value('code');
  131. $sql='select * from (select result.* from (';
  132. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  133. $sql.=' sku.Descr_C 商品名称, MAX(lot.LotAtt05) 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  134. $sql.=' lot.LotAtt04 批号,lot.LotAtt01 生产日期 ';
  135. $sql.=' , SUM(storeStatus.QTY) 在库数量, SUM(storeStatus.QtyAllocated) 占用数量 from ';
  136. $sql.=' INV_LOT_LOC_ID storeStatus';
  137. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  138. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  139. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  140. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  141. $sql.=' ,sku.Descr_C,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  142. $sql.=' ,storeStatus.CUSTOMERID,lot.LotAtt01 ';
  143. $sql.=' )result where 1=1 ';
  144. if ($code)$sql.=" and 客户 = '".$code."' ";
  145. if ($location){
  146. $arr=array_filter(preg_split('/[,, ]+/is', $location));
  147. if (count($arr)==1){
  148. $sql.=" and 库位 like '".$location."%'";
  149. }
  150. if (count($arr)>1){
  151. $sql.=" and (库位 like ";
  152. foreach ($arr as $index=>$str){
  153. if ($index==0){
  154. $sql.="'".$str."%'";
  155. continue;
  156. }
  157. $sql.= "or 库位 like'".$str."%'";
  158. }
  159. $sql.=")";
  160. }
  161. }
  162. if ($barcode)$sql=$this->按条码查询($sql,$barcode);
  163. $sql.=' ) ';
  164. return DB::connection('oracle')->select($sql);
  165. }
  166. private function 按条码查询($sql,$barcode){
  167. $arr=array_filter(preg_split('/[,, ]+/is', $barcode));
  168. if (count($arr)==1){
  169. $sql.=" and (产品条码1 like '".$barcode."%' or 产品条码2 like '".$barcode."%' or 产品条码3 like '".$barcode."%')";
  170. }
  171. if (count($arr)>1){
  172. $sql.=" and (产品条码1 in (";
  173. foreach ($arr as $index=>$str){
  174. if ($index==0){
  175. $sql.="'".$str."'";
  176. continue;
  177. }
  178. $sql.=",'".$str."'";
  179. }
  180. $sql.=")";
  181. $sql.=" or 产品条码2 in (";
  182. foreach ($arr as $index=>$str){
  183. if ($index==0){
  184. $sql.="'".$str."'";
  185. continue;
  186. }
  187. $sql.=",'".$str."'";
  188. }
  189. $sql.=")";
  190. $sql.=" or 产品条码3 in (";
  191. foreach ($arr as $index=>$str){
  192. if ($index==0){
  193. $sql.="'".$str."'";
  194. continue;
  195. }
  196. $sql.=",'".$str."'";
  197. }
  198. $sql.=")";
  199. $sql.=")";
  200. }
  201. return $sql;
  202. }
  203. //创建盘点任务
  204. public function createMission($date_start,$date_end,$ownerId,$location,$barcode){
  205. if (!$ownerId) return null;
  206. if ($date_start&&$date_end){
  207. $date_end_time=$date_end.' 23:59:59';
  208. if ($location||$barcode)$type='局部盘点';
  209. if (!$location&&!$barcode)$type='动盘';
  210. $wmsInventories=$this->conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode);
  211. //动盘 判断库存为0的ide料想是否放有其他商品
  212. foreach ($wmsInventories as $key=>$wmsInventory){
  213. if (!$wmsInventory->在库数量 && strpos($wmsInventory->库位,'IDE') !== false){
  214. /** @var HandInStorageService $handInStorageService */
  215. $handInStorageService=app('HandInStorageService');
  216. $invs=$handInStorageService->getInventoryInfos($wmsInventory->库位);
  217. if (count($invs)>0) unset($wmsInventories[$key]);
  218. }
  219. }
  220. }
  221. if (!$date_start&&!$date_end){
  222. $ownerName=Owner::where('id',$ownerId)->value('code');
  223. //$ownerName=OracleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  224. $date_start=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','ASC')->value('addtime');
  225. $date_end_time=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','DESC')->value('addtime');
  226. if ($location||$barcode)$type='局部盘点';
  227. if (!$location&&!$barcode)$type='全盘';
  228. $wmsInventories=$this->conditionTotalStock($ownerId,$location,$barcode);
  229. }
  230. $inventory=new InventoryAccount([
  231. 'owner_id'=>$ownerId,
  232. 'remark'=>$location,
  233. 'type'=>$type,
  234. 'start_at'=>$date_start,
  235. 'end_at'=>$date_end_time,
  236. 'total'=>count($wmsInventories),
  237. ]);
  238. $inventory->save();
  239. $inventory->createSignCreator();
  240. $this->createInventoryAccountMissionRecord($ownerId,$inventory['id'],$wmsInventories);
  241. $request=[
  242. 'date_start'=>$date_start,
  243. 'date_end'=>$date_end,
  244. 'ownerId'=>$ownerId,
  245. 'location'=>$location,
  246. 'barcode'=>$barcode,
  247. 'inventoryId'=>$inventory['id'],
  248. ];
  249. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  250. return $inventory;
  251. }
  252. //更新盘点记录任务
  253. public function updateInventoryAccountMissionRecord($ownerId,$inventoryAccountId,array $locations,$wmsInventories){
  254. ini_set('memory_limit','1526M');
  255. $ownerCode=Owner::query()->where('id',$ownerId)->value('code');
  256. $commodities=Commodity::query()
  257. ->select('id','owner_id','name','sku')
  258. ->where('owner_id',$ownerId)
  259. ->get();
  260. $commoditiesArr=[];
  261. foreach ($commodities as $commodity){
  262. $commoditiesArr[$ownerCode.'|'.$commodity['sku']]=$commodity;
  263. }
  264. $inventoryAccountMissions = InventoryAccountMission::query()
  265. ->where('inventory_account_id', $inventoryAccountId)
  266. ->whereIn('location', array_unique($locations))
  267. ->get();
  268. /** @var DataHandlerService $dataHandlerService */
  269. $dataHandlerService = app(DataHandlerService::class);
  270. $inventoryAccountMissions_map = $dataHandlerService->dataHeader(['inventory_account_id','commodity_id','location'], $inventoryAccountMissions);
  271. $updateParams = [[
  272. 'id', 'commodity_id', 'inventory_account_id', 'location', 'produced_at', 'valid_at', 'batch_number',
  273. 'erp_type_position', 'quality', 'stored_amount', 'occupied_amount', 'valid_amount','created_at','updated_at'
  274. ]];
  275. foreach ($wmsInventories as $wmsInventory){
  276. $commodity=$commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]??null;
  277. if (!$commodity)continue;
  278. $inventoryAccountMission = $dataHandlerService
  279. ->getKeyValue(['inventory_account_id'=>$inventoryAccountId,'commodity_id' => $commodity['id'],'location' => $wmsInventory->库位], $inventoryAccountMissions_map);
  280. if (!$inventoryAccountMission)continue;
  281. if($wmsInventory->质量状态=='ZP') $quality='正品';
  282. if ($wmsInventory->质量状态=='CC') $quality='残次';
  283. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  284. if ($wmsInventory->质量状态=='JS') $quality='机损';
  285. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  286. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  287. if (
  288. $inventoryAccountMission->produced_at != $wmsInventory->生产日期 ||
  289. $inventoryAccountMission->valid_at != $wmsInventory->失效日期 ||
  290. $inventoryAccountMission->batch_number != $wmsInventory->批号 ||
  291. $inventoryAccountMission->erp_type_position != $wmsInventory->属性仓 ||
  292. $inventoryAccountMission->quality != $quality ||
  293. $inventoryAccountMission->stored_amount != $wmsInventory->在库数量 ||
  294. $inventoryAccountMission->occupied_amount != $wmsInventory->占用数量 ||
  295. $inventoryAccountMission->valid_amount != ($wmsInventory->在库数量-$wmsInventory->占用数量)
  296. ){
  297. $updateParams[] = [
  298. 'id' => $inventoryAccountMission->id,
  299. 'commodity_id'=>$commodity->id,
  300. 'inventory_account_id'=>$inventoryAccountId,
  301. 'location'=>$wmsInventory->库位,
  302. 'produced_at'=>$wmsInventory->生产日期,
  303. 'valid_at'=>$wmsInventory->失效日期,
  304. 'batch_number'=>$wmsInventory->批号,
  305. 'erp_type_position'=>$wmsInventory->属性仓,
  306. 'quality'=>$quality,
  307. 'stored_amount'=>$wmsInventory->在库数量,
  308. 'occupied_amount'=>$wmsInventory->占用数量,
  309. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  310. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  311. 'updated_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  312. ];
  313. }
  314. }
  315. app(BatchUpdateService::class)->batchUpdate('inventory_account_missions', $updateParams);
  316. Controller::logS(__METHOD__,"批量更新盘点记录__".__FUNCTION__,json_encode($updateParams));
  317. }
  318. //创建盘点记录任务
  319. public function createInventoryAccountMissionRecord($ownerId,$inventoryAccountId,$wmsInventories){
  320. ini_set('memory_limit','1526M');
  321. $ownerCode=Owner::query()->where('id',$ownerId)->value('code');
  322. $commodities=Commodity::query()
  323. ->select('id','owner_id','name','sku')
  324. ->where('owner_id',$ownerId)
  325. ->get();
  326. $commoditiesArr=[];
  327. foreach ($commodities as $commodity){
  328. $commoditiesArr[$ownerCode.'|'.$commodity['sku']]=$commodity;
  329. }
  330. $commodityArrBarcode=[];
  331. $commodityArr=[];
  332. $inventoryAccountMissions=[];
  333. foreach ($wmsInventories as $wmsInventory){
  334. $commodity=$commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]??null;
  335. if (!$commodity){
  336. $commodity=new Commodity();
  337. $commodity->owner_id=$ownerId;
  338. $commodity->sku=$wmsInventory->产品编码;
  339. $commodity->name=$wmsInventory->商品名称;
  340. $commodity->save();
  341. $commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]=$commodity;
  342. array_push($commodityArr,$commodity);
  343. }
  344. if ($wmsInventory->产品条码1){
  345. $commodity->newBarcode($wmsInventory->产品条码1);
  346. $arr=[
  347. 'owner_id'=>$ownerId,
  348. 'sku'=>$wmsInventory->产品编码,
  349. 'name'=>$wmsInventory->商品名称,
  350. 'commodity_id'=>$commodity->id,
  351. 'barcode'=>$wmsInventory->产品条码1,
  352. ];
  353. }
  354. if($wmsInventory->产品条码2){
  355. $commodity->newBarcode($wmsInventory->产品条码2);
  356. $arr=[
  357. 'owner_id'=>$ownerId,
  358. 'sku'=>$wmsInventory->产品编码,
  359. 'name'=>$wmsInventory->商品名称,
  360. 'commodity_id'=>$commodity->id,
  361. 'barcode'=>$wmsInventory->产品条码2,
  362. ];
  363. }
  364. if($wmsInventory->产品条码3){
  365. $commodity->newBarcode($wmsInventory->产品条码3);
  366. $arr=[
  367. 'owner_id'=>$ownerId,
  368. 'sku'=>$wmsInventory->产品编码,
  369. 'name'=>$wmsInventory->商品名称,
  370. 'commodity_id'=>$commodity->id,
  371. 'barcode'=>$wmsInventory->产品条码3,
  372. ];
  373. }
  374. array_push($commodityArrBarcode,$arr);
  375. if($wmsInventory->质量状态=='ZP') $quality='正品';
  376. if ($wmsInventory->质量状态=='CC') $quality='残次';
  377. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  378. if ($wmsInventory->质量状态=='JS') $quality='机损';
  379. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  380. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  381. $inventoryAccountMission=[
  382. 'commodity_id'=>$commodity->id,
  383. 'inventory_account_id'=>$inventoryAccountId,
  384. 'location'=>$wmsInventory->库位,
  385. 'produced_at'=>$wmsInventory->生产日期,
  386. 'valid_at'=>$wmsInventory->失效日期,
  387. 'batch_number'=>$wmsInventory->批号,
  388. 'erp_type_position'=>$wmsInventory->属性仓,
  389. 'quality'=>$quality,
  390. 'stored_amount'=>$wmsInventory->在库数量,
  391. 'occupied_amount'=>$wmsInventory->占用数量,
  392. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  393. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  394. ];
  395. array_push($inventoryAccountMissions,$inventoryAccountMission);
  396. }
  397. $inventoryAccountMissions=InventoryAccountMission::query()->insert($inventoryAccountMissions);
  398. if ($commodityArr){
  399. Controller::logS(__METHOD__,"插入was中商品信息__".__FUNCTION__,json_encode($commodityArr));
  400. Controller::logS(__METHOD__,"插入was中商品信息的条码信息__".__FUNCTION__,json_encode($commodityArrBarcode));
  401. }
  402. Controller::logS(__METHOD__,"批量插入盘点记录__".__FUNCTION__,json_encode($inventoryAccountMissions));
  403. }
  404. //盘点库存
  405. public function stockInventory($id,$location,$barcode,$count,$inventoryAccountId){
  406. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  407. if (!$inventoryAccountMission)return null;
  408. $this->盘点($inventoryAccountId,$count,$inventoryAccountMission);
  409. $request=[
  410. 'location'=>$location,
  411. 'barcode'=>$barcode,
  412. 'count'=>$count,
  413. 'inventoryId'=>$inventoryAccountId,
  414. ];
  415. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  416. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  417. return $inventoryAccountMission;
  418. }
  419. //盘点修改盘点任务数据
  420. public function updateInventory($inventoryAccountId){
  421. $inventoryAccount=InventoryAccount::find($inventoryAccountId);
  422. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  423. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  424. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  425. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount(); //跳过数
  426. if($inventoryAccount->status=='待盘点')
  427. $inventoryAccount->status='盘点中';
  428. $inventoryAccount->update();
  429. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($inventoryAccountId));
  430. return $inventoryAccount;
  431. }
  432. //根据该库存和产品条码查询该条盘点记录
  433. public function searchStockInventoryRecord($location,$barcode,$inventoryAccountId){
  434. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  435. $query->whereHas('barcodes',function($sql)use($barcode){
  436. $sql->where('code',$barcode);
  437. });
  438. })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->get();
  439. return $inventoryAccountMissions;
  440. }
  441. public function 修改质量状态($id,$location,$sku,$quality,$ownerCode){
  442. $lotnum=OracleInvLotLocId::query()->where('locationid',$location)->where('customerid',$ownerCode)->where('sku',$sku)->value('lotnum');
  443. if (!$lotnum) return null;
  444. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->first();
  445. if(!isset($oracleInvLotAtt))return null;
  446. if ($quality=='正品')$status='ZP';
  447. if ($quality=='残次')$status='CC';
  448. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->update([
  449. 'lotatt08'=>$status,
  450. ]);
  451. if ($oracleInvLotAtt>0){
  452. $inventoryAccountMission=InventoryAccountMission::query()->find($id);
  453. $inventoryAccountMission->quality=$quality;
  454. $inventoryAccountMission=$inventoryAccountMission->update();
  455. return $inventoryAccountMission;
  456. }else{
  457. return null;
  458. }
  459. }
  460. public function 完结盘点任务($id){
  461. $inventoryAccount=InventoryAccount::query()->find($id);
  462. if ($inventoryAccount->status=='复盘中'){
  463. $inventoryAccount->status='已完成';
  464. $inventoryAccount->update();
  465. Controller::logS(__METHOD__,"盘点修改盘点任务状态__".__FUNCTION__,json_encode($id));
  466. return $inventoryAccount;
  467. }else{
  468. return null;
  469. }
  470. }
  471. public function 增加系统之外的盘点记录($location,$barcode,$inventoryId,$count,$owner_code,$param){
  472. if($param=='商品新增'||$param=='库位和商品新增'){
  473. $oracleBasSku=$this->searchCommodityByBarcode($barcode,$owner_code);
  474. if ($oracleBasSku){
  475. $ownerId=Owner::query()->where('code',$owner_code)->value('id');
  476. $commodity=Commodity::query()->where('sku',$oracleBasSku->sku)->where('owner_id',$ownerId)->first();
  477. if (!$commodity){
  478. $commodity=Commodity::query()->create([
  479. 'sku'=>$oracleBasSku->sku,
  480. 'owner_id'=>$ownerId,
  481. 'name'=>$oracleBasSku->descr_c
  482. ]);
  483. $commodity->newBarcode($barcode);
  484. }
  485. }
  486. }
  487. $commodity=Commodity::query()->whereHas('barcodes',function ($query)use($barcode){
  488. $query->where('code',$barcode);
  489. })->orderBy('id','DESC')->first();
  490. $inventoryAccountMission=new InventoryAccountMission();
  491. $inventoryAccountMission->location=$location;
  492. $inventoryAccountMission->inventory_account_id=$inventoryId;
  493. $inventoryAccountMission->commodity_id=$commodity->id;
  494. $inventoryAccountMission->quality='正品';
  495. $inventoryAccountMission->verified_amount=$count;//盘点数量
  496. $inventoryAccountMission->difference_amount=$count;//盘点差异
  497. $inventoryAccountMission->checked='是';
  498. $inventoryAccountMission->save();
  499. Controller::logS(__METHOD__,"增加系统之外的盘点记录".__FUNCTION__,json_encode($inventoryAccountMission));
  500. if ($inventoryAccountMission){
  501. $inventoryAccountMission->createSignStockInventoryPersons();
  502. Controller::logS(__METHOD__,"增加盘点人".__FUNCTION__,json_encode($inventoryId));
  503. $inventoryAccount=InventoryAccount::query()->find($inventoryId);
  504. $inventoryAccount->total=$inventoryAccount->total+1;
  505. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  506. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  507. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  508. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  509. $inventoryAccount->update();
  510. Controller::logS(__METHOD__,"修改盘点任务记录".__FUNCTION__,json_encode($inventoryId));
  511. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('id',$inventoryAccountMission->id)->first();
  512. return $inventoryAccountMission;
  513. }else{
  514. return null;
  515. }
  516. }
  517. public function 盘点选中任务($id,$count,$inventoryId){
  518. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  519. if (!$inventoryAccountMission) return null;
  520. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  521. $request=[
  522. 'id'=>$id,
  523. 'count'=>$count,
  524. 'inventoryId'=>$inventoryId,
  525. ];
  526. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  527. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  528. return $inventoryAccountMission;
  529. }
  530. public function 盘点生产日期_失效日期_批号有改动任务($id,$count,$inventoryId,$produced_at,$valid_at,$batch_number){
  531. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  532. $inventoryMissionId=null;
  533. if (!$inventoryAccountMission) return null;
  534. $inventory=InventoryAccount::find($inventoryId);
  535. if ($produced_at!=$inventoryAccountMission->produced_at||$valid_at!=$inventoryAccountMission->valid_at ||$batch_number!=$inventoryAccountMission->batch_number){
  536. if($inventory->status=='复盘中'){//盘点原记录
  537. $inventoryAccountMission->re_checked_amount=$count;
  538. $inventoryAccountMission->difference_amount=0-$count;
  539. $inventoryAccountMission->checked='已复核';
  540. if ($inventoryAccountMission->difference_amount==0){
  541. $inventoryAccountMission->returned='是';
  542. }else{
  543. $inventoryAccountMission->returned='否';
  544. }
  545. }else{//初盘
  546. $inventoryAccountMission->verified_amount=$count;
  547. $inventoryAccountMission->difference_amount=0-$count;
  548. $inventoryAccountMission->checked='是';
  549. }
  550. $inventoryAccountMission->update();
  551. $inventoryAccountMission->createSignStockInventoryPersons();
  552. //新增新记录
  553. $inventoryMission=new InventoryAccountMission();
  554. $inventoryMission->location=$inventoryAccountMission->location;
  555. $inventoryMission->inventory_account_id=$inventoryId;
  556. $inventoryMission->commodity_id=$inventoryAccountMission->commodity_id;
  557. $inventoryMission->produced_at=$produced_at;
  558. $inventoryMission->valid_at=$valid_at;
  559. $inventoryMission->batch_number=$batch_number;
  560. $inventoryMission->erp_type_position=$inventoryAccountMission->erp_type_position;
  561. $inventoryMission->quality=$inventoryAccountMission->quality;
  562. $inventoryMission->stored_amount=0;
  563. $inventoryMission->verified_amount=$count;
  564. $inventoryMission->difference_amount=$count;
  565. $inventoryMission->checked='是';
  566. if($inventory->status=='复盘中'){
  567. $inventoryMission->re_checked_amount=$count;
  568. $inventoryMission->returned='否';
  569. }
  570. $inventoryMission->save();
  571. $inventoryMissionId=$inventoryMission->id??null;
  572. $inventoryMission->createSignStockInventoryPersons();
  573. }else{
  574. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  575. }
  576. $request=[
  577. 'id'=>$id,
  578. 'count'=>$count,
  579. 'inventoryId'=>$inventoryId,
  580. 'produced_at'=>$produced_at,
  581. 'valid_at'=>$valid_at,
  582. 'batch_number'=>$batch_number,
  583. ];
  584. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  585. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->whereIn('id',[$id,$inventoryMissionId])->get();
  586. return $inventoryAccountMission;
  587. }
  588. public function 盘点($inventoryId,$count,$inventoryAccountMission){
  589. $inventory=InventoryAccount::find($inventoryId);
  590. if($inventory->status=='复盘中'){
  591. $inventoryAccountMission->re_checked_amount=$count;
  592. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  593. $inventoryAccountMission->checked='已复核';
  594. if ($inventoryAccountMission->difference_amount==0){
  595. $inventoryAccountMission->returned='是';
  596. }else{
  597. $inventoryAccountMission->returned='否';
  598. }
  599. }else{//初盘
  600. $inventoryAccountMission->verified_amount=$count;
  601. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  602. $inventoryAccountMission->checked='是';
  603. }
  604. $inventoryAccountMission->update();
  605. $inventoryAccountMission->createSignStockInventoryPersons();
  606. return $inventoryAccountMission;
  607. }
  608. public function 删除盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  609. $inventoryAccountMission=InventoryAccountMission::query()->where('id',$inventoryAccountMissionId)->delete();
  610. Controller::logS(__METHOD__,__FUNCTION__,json_encode($inventoryAccountMissionId));
  611. if ($inventoryAccountMission>0){
  612. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  613. $inventoryAccount->total=$inventoryAccount->total-1;
  614. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  615. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  616. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  617. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  618. $inventoryAccount->update();
  619. Controller::logS(__METHOD__,'删除盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  620. }
  621. return $inventoryAccountMission;
  622. }
  623. public function 跳过盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  624. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryAccountMissionId);
  625. $inventoryAccountMission->checked='跳过';
  626. $inventoryAccountMission->update();
  627. app('LogService')->log(__METHOD__,"跳过盘点记录修改checked状态",json_encode($inventoryAccountMissionId));
  628. if ($inventoryAccountMission->checked=='跳过'){
  629. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  630. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  631. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  632. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  633. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  634. $inventoryAccount->update();
  635. Controller::logS(__METHOD__,'跳过盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  636. }
  637. return $inventoryAccountMission;
  638. }
  639. public function 确认盘点差异($inventoryAccountMissionId,$inventoryAccountId){
  640. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryAccountMissionId);
  641. $inventoryAccountMission->checked='确认差异';
  642. $inventoryAccountMission->update();
  643. app('LogService')->log(__METHOD__,"跳过盘点记录修改checked状态",json_encode($inventoryAccountMissionId));
  644. return $inventoryAccountMission;
  645. }
  646. public function 批量跳过或确认差异($checkData){
  647. $inventoryAccountMissions=[];
  648. $inventoryAccountId=null;
  649. foreach ($checkData as $inventoryMission){
  650. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryMission['id']);
  651. $inventoryAccountId=$inventoryAccountMission->inventory_account_id;
  652. if ($inventoryMission['mark']==='未盘')$inventoryAccountMission['checked']='跳过';
  653. if ($inventoryMission['mark']==='未复盘有差异'||$inventoryMission['mark']==='已复盘无差异')$inventoryAccountMission['checked']='确认差异';
  654. $inventoryAccountMission->update();
  655. array_push($inventoryAccountMissions,$inventoryAccountMission);
  656. }
  657. app('LogService')->log(__METHOD__,"批量跳过或确认差异",json_encode($checkData));
  658. if ($inventoryAccountId){
  659. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  660. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  661. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  662. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  663. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  664. $inventoryAccount->update();
  665. Controller::logS(__METHOD__,'批量跳过或确认差异修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  666. }
  667. return $inventoryAccountMissions;
  668. }
  669. public function searchCommodityByBarcode($barcode,$owner_code){
  670. $oracleBasSku=OracleBasSKU::query()
  671. ->where('ALTERNATE_SKU1',$barcode)
  672. ->orWhere('ALTERNATE_SKU2',$barcode)
  673. ->orWhere('ALTERNATE_SKU3',$barcode)
  674. ->where('customerid',$owner_code)->first();
  675. if (!$oracleBasSku)return null;
  676. return $oracleBasSku;
  677. }
  678. public function baseOnBlindReceive($location,$owner_code,$goodses,$inventoryId){
  679. $request=[
  680. 'location'=>$location,
  681. 'owner_code'=>$owner_code,
  682. 'goodses'=>$goodses,
  683. 'inventoryId'=>$inventoryId,
  684. ];
  685. app('LogService')->log(__METHOD__,"盘点基于盲收",json_encode($request));
  686. $inventoryAccountMissions=[];
  687. foreach ($goodses as $good){
  688. $barcode=$good['barcode']??'';
  689. $amount=$good['amount']??'';
  690. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  691. $query->whereHas('barcodes',function($sql)use($barcode){
  692. $sql->where('code','=',$barcode);
  693. });
  694. })->where('location',$location)->where('inventory_account_id',$inventoryId)->first();
  695. if (!$inventoryAccountMission){
  696. $oracleBasSku=$this->searchCommodityByBarcode($barcode,$owner_code);
  697. if ($oracleBasSku){
  698. $param='商品新增';
  699. $inventoryAccountMission=$this->增加系统之外的盘点记录($location,$barcode,$inventoryId,$amount,$owner_code,$param);
  700. }
  701. }else{
  702. $inventoryAccountMission=$this->盘点($inventoryId,$amount,$inventoryAccountMission);
  703. }
  704. array_push($inventoryAccountMissions,$inventoryAccountMission);
  705. }
  706. if (count($inventoryAccountMissions)<1) return null;
  707. return $inventoryAccountMissions;
  708. }
  709. public function batchStockByLocation($missions,$inventoryId)
  710. {
  711. $inventory=InventoryAccount::query()->find($inventoryId);
  712. if($inventory->status=='复盘中'){
  713. $updateParams = [[
  714. 'id','returned','difference_amount','re_checked_amount','checked','updated_at'
  715. ]];
  716. foreach ($missions as $mission){
  717. if (($mission['amount']-$mission['stored_amount'])==0){
  718. $return='是';
  719. }else{
  720. $return='否';
  721. }
  722. $updateParams[] = [
  723. 'id'=>$mission['id'],
  724. 're_checked_amount'=>$mission['amount'],
  725. 'difference_amount'=>$mission['amount']-$mission['stored_amount'],
  726. 'returned'=>$return,
  727. 'checked'=>'已复核',
  728. 'updated_at' =>Carbon::now()->format('Y-m-d H:i:s'),
  729. ];
  730. }
  731. }else{//初盘
  732. $updateParams = [[
  733. 'id','difference_amount','checked','verified_amount','updated_at'
  734. ]];
  735. foreach ($missions as $mission){
  736. $updateParams[] = [
  737. 'id'=>$mission['id'],
  738. 'verified_amount'=>$mission['amount'],
  739. 'difference_amount'=>$mission['amount']-$mission['stored_amount'],
  740. 'checked'=>'是',
  741. 'updated_at' =>Carbon::now()->format('Y-m-d H:i:s'),
  742. ];
  743. }
  744. }
  745. if(count($updateParams) > 1) app(BatchUpdateService::class)->batchUpdate('inventory_account_missions',$updateParams);
  746. LogService::log(__METHOD__,__FUNCTION__,'根据库位批量盘点'.count($updateParams).json_encode($updateParams));
  747. $ids = array_unique(data_get($missions,'*.id'));
  748. $inventoryMissions=InventoryAccountMission::query()->whereIn('id',$ids)->get();
  749. foreach ($inventoryMissions as $inventoryMission){
  750. $inventoryMission->createSignStockInventoryPersons();
  751. }
  752. return InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->whereIn('id',$ids)->get();
  753. }
  754. }