InventoryAccountService.php 41 KB

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