InventoryAccountService.php 41 KB

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