InventoryAccountService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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\OraccleBasCustomer;
  8. use App\OracleBasSKU;
  9. use App\OracleInvLotAtt;
  10. use App\OracleBasCustomer;
  11. use App\OracleInvLotLocId;
  12. use App\Owner;
  13. use App\Services\common\QueryService;
  14. use Carbon\Carbon;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\DB;
  17. use function GuzzleHttp\Psr7\_parse_message;
  18. class InventoryAccountService
  19. {
  20. private function conditionQueryInventoryAccountMission(array $params){
  21. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->orderBy('difference_amount','desc');
  22. $columnQueryRules=[];
  23. $inventoryAccountMissions = app(QueryService::class)->query($params,$inventoryAccountMissions,$columnQueryRules,'inventory_account_missions');
  24. return $inventoryAccountMissions;
  25. }
  26. public function getSql(array $params){
  27. return $this->conditionQueryInventoryAccountMission($params)->selectRaw("inventory_account_missions.*")
  28. ->leftJoin('signs','inventory_account_missions.id','signs.signable_id')
  29. ->selectRaw('signs.mark stockInventoryPerson')
  30. ->leftJoin('commodities','inventory_account_missions.commodity_id','commodities.id')
  31. ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku')
  32. ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
  33. ->selectRaw('commodity_barcodes.code commodity_barcode_code')
  34. ->sql();
  35. }
  36. private function conditionQuery($queryParam){
  37. $inventories=InventoryAccount::query()->with(['owner','creator'])->orderBy('id','desc');
  38. $columnQueryRules=[
  39. 'owner_id' => ['multi' => ','],
  40. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  41. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  42. ];
  43. $inventories = app(QueryService::class)->query($queryParam,$inventories,$columnQueryRules);
  44. return $inventories;
  45. }
  46. public function paginate($queryParam){
  47. $inventories = $this->conditionQuery($queryParam);
  48. return $inventories->paginate($queryParam['paginate'] ?? 50);
  49. }
  50. public function get($queryParam){
  51. $inventories = $this->conditionQuery($queryParam);
  52. return $inventories->get();
  53. }
  54. public function some($queryParam){
  55. return InventoryAccount::query()->with(['owner'])->orderBy('id','DESC')
  56. ->whereIn('id',explode(',',$queryParam['data']))->get();
  57. }
  58. //动盘查询
  59. public function conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode){
  60. if (!$ownerId) return null;
  61. $descr_c=Owner::where('id',$ownerId)->value('name');
  62. $sql='select * from (select result.*,rownum rn from (';
  63. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  64. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  65. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期';
  66. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  67. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  68. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  69. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  70. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  71. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  72. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  73. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  74. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  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 FMCustomerID, FMLocation,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='MV' ";
  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 FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  83. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  84. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  85. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  86. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  87. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  88. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  89. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  90. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  91. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  92. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  93. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  94. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt01,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  95. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  96. $sql.=' )result where 1=1 ';
  97. if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
  98. if ($location)$sql.=" and 库位 like '".$location."%' ";
  99. if ($barcode)$sql=$this->动盘局部盘点按条码查询($sql,$barcode);
  100. $sql.=' ) ';
  101. return DB::connection('oracle')->select($sql);
  102. }
  103. //全盘查询
  104. private function conditionTotalStock($ownerId,$location,$barcode){
  105. $descr_c=Owner::where('id',$ownerId)->value('name');
  106. $sql='select * from (select result.* from (';
  107. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  108. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  109. $sql.=' lot.LotAtt04 批号,lot.LotAtt01 生产日期 ';
  110. $sql.=' , SUM(storeStatus.QTY) 在库数量, SUM(storeStatus.QtyAllocated) 占用数量 from ';
  111. $sql.=' INV_LOT_LOC_ID storeStatus';
  112. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  113. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  114. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  115. if ($location)$sql.=" where storeStatus.LocationID like '".$location."%' ";
  116. if ($barcode&&$location)$sql=$this->局部盘点按条码查询_有库位前缀字母($sql,$barcode);
  117. if ($barcode&&!$location)$sql=$this->局部盘点按条码查询_无库位前缀字母($sql,$barcode);
  118. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  119. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  120. $sql.=' ,storeStatus.CUSTOMERID,lot.LotAtt01 ';
  121. $sql.=' )result where 1=1 ';
  122. if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
  123. if ($location&&$barcode)$sql.=" and 库位 like '".$location."%' ";
  124. $sql.=' ) ';
  125. return DB::connection('oracle')->select($sql);
  126. }
  127. private function 局部盘点按条码查询_有库位前缀字母($sql,$barcode){
  128. $arr=array_values(array_filter(preg_split('/[,, ]+/is', $barcode)));
  129. if (count($arr)==1){
  130. $sql.=" and (sku.ALTERNATE_SKU1 like '".$barcode."%' or sku.ALTERNATE_SKU2 like '".$barcode."%' or sku.ALTERNATE_SKU3 like '".$barcode."%')";
  131. }
  132. if (count($arr)>1){
  133. $sql.=" and (sku.ALTERNATE_SKU1 in (";
  134. foreach ($arr as $index=>$str){
  135. if ($index==0){
  136. $sql.="'".$str."'";
  137. continue;
  138. }
  139. $sql.=",'".$str."'";
  140. }
  141. $sql.=")";
  142. $sql.=" or sku.ALTERNATE_SKU2 in (";
  143. foreach ($arr as $index=>$str){
  144. if ($index==0){
  145. $sql.="'".$str."'";
  146. continue;
  147. }
  148. $sql.=",'".$str."'";
  149. }
  150. $sql.=")";
  151. $sql.=" or sku.ALTERNATE_SKU3 in (";
  152. foreach ($arr as $index=>$str){
  153. if ($index==0){
  154. $sql.="'".$str."'";
  155. continue;
  156. }
  157. $sql.=",'".$str."'";
  158. }
  159. $sql.=")";
  160. $sql.=")";
  161. }
  162. return $sql;
  163. }
  164. private function 动盘局部盘点按条码查询($sql,$barcode){
  165. $arr=array_values(array_filter(preg_split('/[,, ]+/is', $barcode)));
  166. if (count($arr)==1){
  167. $sql.=" and (产品条码1 like '".$barcode."%' or 产品条码2 like '".$barcode."%' or 产品条码3 like '".$barcode."%')";
  168. }
  169. if (count($arr)>1){
  170. $sql.=" and (产品条码1 in (";
  171. foreach ($arr as $index=>$str){
  172. if ($index==0){
  173. $sql.="'".$str."'";
  174. continue;
  175. }
  176. $sql.=",'".$str."'";
  177. }
  178. $sql.=")";
  179. $sql.=" or 产品条码2 in (";
  180. foreach ($arr as $index=>$str){
  181. if ($index==0){
  182. $sql.="'".$str."'";
  183. continue;
  184. }
  185. $sql.=",'".$str."'";
  186. }
  187. $sql.=")";
  188. $sql.=" or 产品条码3 in (";
  189. foreach ($arr as $index=>$str){
  190. if ($index==0){
  191. $sql.="'".$str."'";
  192. continue;
  193. }
  194. $sql.=",'".$str."'";
  195. }
  196. $sql.=")";
  197. $sql.=")";
  198. }
  199. return $sql;
  200. }
  201. private function 局部盘点按条码查询_无库位前缀字母($sql,$barcode){
  202. $arr=array_values(array_filter(preg_split('/[,, ]+/is', $barcode)));
  203. if (count($arr)==1){
  204. $sql.="where sku.ALTERNATE_SKU1 like '".$barcode."%' or sku.ALTERNATE_SKU2 like '".$barcode."%' or sku.ALTERNATE_SKU3 like '".$barcode."%'";
  205. }
  206. if (count($arr)>1){
  207. $sql.="where sku.ALTERNATE_SKU1 in (";
  208. foreach ($arr as $index=>$str){
  209. if ($index==0){
  210. $sql.="'".$str."'";
  211. continue;
  212. }
  213. $sql.=",'".$str."'";
  214. }
  215. $sql.=")";
  216. $sql.=" or sku.ALTERNATE_SKU2 in (";
  217. foreach ($arr as $index=>$str){
  218. if ($index==0){
  219. $sql.="'".$str."'";
  220. continue;
  221. }
  222. $sql.=",'".$str."'";
  223. }
  224. $sql.=")";
  225. $sql.=" or sku.ALTERNATE_SKU3 in (";
  226. foreach ($arr as $index=>$str){
  227. if ($index==0){
  228. $sql.="'".$str."'";
  229. continue;
  230. }
  231. $sql.=",'".$str."'";
  232. }
  233. $sql.=")";
  234. }
  235. return $sql;
  236. }
  237. //创建盘点任务
  238. public function createMission($date_start,$date_end,$ownerId,$location,$barcode){
  239. if (!$ownerId) return null;
  240. if ($date_start&&$date_end){
  241. $date_end_time=$date_end.' 23:59:59';
  242. $type='动盘';
  243. $wmsInventories=$this->conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode);
  244. }elseif (!$date_start&&!$date_end){
  245. $name=Owner::where('id',$ownerId)->value('name');
  246. $ownerName=OracleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  247. $date_start=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','ASC')->value('addtime');
  248. $date_end_time=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','DESC')->value('addtime');
  249. $type='全盘';
  250. $wmsInventories=$this->conditionTotalStock($ownerId,$location,$barcode);
  251. }else{
  252. return null;
  253. }
  254. $inventory=new InventoryAccount([
  255. 'owner_id'=>$ownerId,
  256. 'type'=>$type,
  257. 'start_at'=>$date_start,
  258. 'end_at'=>$date_end_time,
  259. 'total'=>count($wmsInventories),
  260. ]);
  261. $inventory->save();
  262. $inventory->createSignCreator();
  263. LogService::log(__METHOD__,'createInventoryAccountMissionRecord -- start','createInventoryAccountMissionRecord');
  264. $this->createInventoryAccountMissionRecord($ownerId,$inventory['id'],$wmsInventories);
  265. LogService::log(__METHOD__,'createInventoryAccountMissionRecord -- end','createInventoryAccountMissionRecord');
  266. $request=[
  267. 'date_start'=>$date_start,
  268. 'date_end'=>$date_end,
  269. 'ownerId'=>$ownerId,
  270. 'location'=>$location,
  271. 'barcode'=>$barcode,
  272. 'inventoryId'=>$inventory['id'],
  273. ];
  274. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  275. return $inventory;
  276. }
  277. //创建盘点记录任务
  278. public function createInventoryAccountMissionRecord($ownerId,$inventoryAccountId,$wmsInventories){
  279. $commodities=Commodity::query()
  280. ->select('id','owner_id','name','sku')
  281. ->with('barcodes')
  282. ->where('owner_id',$ownerId)
  283. ->whereNotNull(['sku','name'])
  284. ->get();
  285. $commoditiesArr=[];
  286. foreach ($commodities as $commodity){
  287. $commoditiesArr[$commodity['name'].'|'.$commodity['sku']]=$commodity;
  288. }
  289. $commodityArr=[];
  290. $inventoryAccountMissions=[];
  291. LogService::log(__METHOD__,'foreach -- strat','createInventoryAccountMissionRecord');
  292. foreach ($wmsInventories as $wmsInventory){
  293. $commodity=$commoditiesArr[$wmsInventory->商品名称.'|'.$wmsInventory->产品编码]??null;
  294. if (!$commodity){
  295. $commodity=new Commodity();
  296. $commodity->owner_id=$ownerId;
  297. $commodity->sku=$wmsInventory->产品编码;
  298. $commodity->name=$wmsInventory->商品名称;
  299. $commodity->save();
  300. }
  301. if ($wmsInventory->产品条码1){
  302. $commodity->newBarcode($wmsInventory->产品条码1);
  303. $arr=[
  304. 'owner_id'=>$ownerId,
  305. 'sku'=>$wmsInventory->产品编码,
  306. 'name'=>$wmsInventory->商品名称,
  307. 'commodity_id'=>$commodity->id,
  308. 'barcode'=>$wmsInventory->产品条码1,
  309. ];
  310. }
  311. if($wmsInventory->产品条码2){
  312. $commodity->newBarcode($wmsInventory->产品条码2);
  313. $arr=[
  314. 'owner_id'=>$ownerId,
  315. 'sku'=>$wmsInventory->产品编码,
  316. 'name'=>$wmsInventory->商品名称,
  317. 'commodity_id'=>$commodity->id,
  318. 'barcode'=>$wmsInventory->产品条码2,
  319. ];
  320. }
  321. if($wmsInventory->产品条码3){
  322. $commodity->newBarcode($wmsInventory->产品条码3);
  323. $arr=[
  324. 'owner_id'=>$ownerId,
  325. 'sku'=>$wmsInventory->产品编码,
  326. 'name'=>$wmsInventory->商品名称,
  327. 'commodity_id'=>$commodity->id,
  328. 'barcode'=>$wmsInventory->产品条码3,
  329. ];
  330. }
  331. array_push($commodityArr,$arr);
  332. if($wmsInventory->质量状态=='ZP') $quality='正品';
  333. if ($wmsInventory->质量状态=='CC') $quality='残次';
  334. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  335. if ($wmsInventory->质量状态=='JS') $quality='机损';
  336. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  337. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  338. $inventoryAccountMission=[
  339. 'commodity_id'=>$commodity->id,
  340. 'inventory_account_id'=>$inventoryAccountId,
  341. 'location'=>$wmsInventory->库位,
  342. 'produced_at'=>$wmsInventory->生产日期,
  343. 'valid_at'=>$wmsInventory->失效日期,
  344. // 'stored_at'=>$wmsInventory->入库日期,
  345. 'batch_number'=>$wmsInventory->批号,
  346. 'erp_type_position'=>$wmsInventory->属性仓,
  347. 'quality'=>$quality,
  348. 'stored_amount'=>$wmsInventory->在库数量,
  349. 'occupied_amount'=>$wmsInventory->占用数量,
  350. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  351. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  352. ];
  353. array_push($inventoryAccountMissions,$inventoryAccountMission);
  354. }
  355. LogService::log(__METHOD__,'foreach -- strat','createInventoryAccountMissionRecord');
  356. $inventoryAccountMissions=InventoryAccountMission::query()->insert($inventoryAccountMissions);
  357. if ($commodityArr){
  358. Controller::logS(__METHOD__,"插入was中没有的商品信息__".__FUNCTION__,json_encode($commodityArr));
  359. }
  360. Controller::logS(__METHOD__,"批量插入盘点记录__".__FUNCTION__,json_encode($inventoryAccountMissions));
  361. }
  362. //盘点库存
  363. public function stockInventory($id,$location,$barcode,$count,$inventoryAccountId){
  364. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  365. // $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  366. // $query->whereHas('barcodes',function($sql)use($barcode){
  367. // $sql->where('code','=',$barcode);
  368. // });
  369. // })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->first();
  370. $this->盘点($inventoryAccountId,$count,$inventoryAccountMission);
  371. $request=[
  372. 'location'=>$location,
  373. 'barcode'=>$barcode,
  374. 'count'=>$count,
  375. 'inventoryId'=>$inventoryAccountId,
  376. ];
  377. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  378. // $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  379. // $query->whereHas('barcodes',function($sql)use($barcode){
  380. // $sql->where('code','=',$barcode);
  381. // });
  382. // })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->first();
  383. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  384. return $inventoryAccountMission;
  385. }
  386. //盘点修改盘点任务数据
  387. public function updateInventory($inventoryAccountId){
  388. $inventoryAccount=InventoryAccount::find($inventoryAccountId);
  389. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  390. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  391. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  392. if($inventoryAccount->status=='待盘点')
  393. $inventoryAccount->status='盘点中';
  394. $inventoryAccount->update();
  395. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($inventoryAccountId));
  396. return $inventoryAccount;
  397. }
  398. //根据该库存和产品条码查询该条盘点记录
  399. public function searchStockInventoryRecord($location,$barcode,$inventoryAccountId){
  400. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  401. $query->whereHas('barcodes',function($sql)use($barcode){
  402. $sql->where('code',$barcode);
  403. });
  404. })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->get();
  405. return $inventoryAccountMissions;
  406. }
  407. public function 修改质量状态($id,$location,$sku,$quality,$ownerCode){
  408. $lotnum=OracleInvLotLocId::query()->where('locationid',$location)->where('customerid',$ownerCode)->where('sku',$sku)->value('lotnum');
  409. if (!$lotnum) return null;
  410. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->first();
  411. if(!isset($oracleInvLotAtt))return null;
  412. if ($quality=='正品')$status='ZP';
  413. if ($quality=='残次')$status='CC';
  414. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->update([
  415. 'lotatt08'=>$status,
  416. ]);
  417. if ($oracleInvLotAtt>0){
  418. $inventoryAccountMission=InventoryAccountMission::query()->find($id);
  419. $inventoryAccountMission->quality=$quality;
  420. $inventoryAccountMission=$inventoryAccountMission->update();
  421. return $inventoryAccountMission;
  422. }else{
  423. return null;
  424. }
  425. }
  426. public function 完结盘点任务($id){
  427. $inventoryAccount=InventoryAccount::query()->find($id);
  428. if ($inventoryAccount->status=='复盘中'){
  429. $inventoryAccount->status='已完成';
  430. $inventoryAccount->update();
  431. Controller::logS(__METHOD__,"盘点修改盘点任务状态__".__FUNCTION__,json_encode($id));
  432. return $inventoryAccount;
  433. }else{
  434. return null;
  435. }
  436. }
  437. public function 增加系统之外的盘点记录($location,$barcode,$inventoryId,$count,$owner_code,$param){
  438. if($param=='商品新增'||$param=='库位和商品新增'){
  439. $oracleBasSku=OracleBasSKU::query()->where('ALTERNATE_SKU1',$barcode)->where('customerid',$owner_code)->first();
  440. //dd($oracleBasSku);
  441. $ownerId=Owner::query()->where('code',$owner_code)->value('id');
  442. $commodity=Commodity::query()->firstOrCreate([
  443. 'sku'=>$oracleBasSku->sku,
  444. 'owner_id'=>$ownerId,
  445. 'name'=>$oracleBasSku->descr_c
  446. ]);
  447. $commodity->newBarcode($barcode);
  448. }
  449. $commodity=Commodity::query()->whereHas('barcodes',function ($query)use($barcode){
  450. $query->where('code',$barcode);
  451. })->orderBy('id','DESC')->first();
  452. $inventoryAccountMission=new InventoryAccountMission();
  453. $inventoryAccountMission->location=$location;
  454. $inventoryAccountMission->inventory_account_id=$inventoryId;
  455. $inventoryAccountMission->commodity_id=$commodity->id;
  456. $inventoryAccountMission->quality='正品';
  457. $inventoryAccountMission->verified_amount=$count;//盘点数量
  458. $inventoryAccountMission->difference_amount=$count;//盘点差异
  459. $inventoryAccountMission->checked='是';
  460. $inventoryAccountMission->save();
  461. Controller::logS(__METHOD__,"".__FUNCTION__,json_encode($inventoryId));
  462. if ($inventoryAccountMission){
  463. $inventoryAccountMission->createSignStockInventoryPersons();
  464. Controller::logS(__METHOD__,"增加盘点人".__FUNCTION__,json_encode($inventoryId));
  465. $inventoryAccount=InventoryAccount::query()->find($inventoryId);
  466. $inventoryAccount->total=$inventoryAccount->total+1;
  467. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  468. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  469. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  470. $inventoryAccount->update();
  471. Controller::logS(__METHOD__,"修改盘点任务记录".__FUNCTION__,json_encode($inventoryId));
  472. return $inventoryAccountMission;
  473. }else{
  474. return null;
  475. }
  476. }
  477. public function 盘点选中任务($id,$count,$inventoryId){
  478. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  479. if (!$inventoryAccountMission) return null;
  480. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  481. $request=[
  482. 'id'=>$id,
  483. 'count'=>$count,
  484. 'inventoryId'=>$inventoryId,
  485. ];
  486. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  487. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  488. return $inventoryAccountMission;
  489. }
  490. public function 盘点生产日期_失效日期_批号有改动任务($id,$count,$inventoryId,$produced_at,$valid_at,$batch_number){
  491. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  492. $inventoryMissionId=null;
  493. if (!$inventoryAccountMission) return null;
  494. $inventory=InventoryAccount::find($inventoryId);
  495. if ($produced_at!=$inventoryAccountMission->produced_at||$valid_at!=$inventoryAccountMission->valid_at ||$batch_number!=$inventoryAccountMission->batch_number){
  496. if($inventory->status=='复盘中'){//盘点原记录
  497. $inventoryAccountMission->re_checked_amount=$count;
  498. $inventoryAccountMission->difference_amount=0-$count;
  499. $inventoryAccountMission->checked='已复核';
  500. if ($inventoryAccountMission->difference_amount==0){
  501. $inventoryAccountMission->returned='是';
  502. }else{
  503. $inventoryAccountMission->returned='否';
  504. }
  505. }else{//初盘
  506. $inventoryAccountMission->verified_amount=$count;
  507. $inventoryAccountMission->difference_amount=0-$count;
  508. $inventoryAccountMission->checked='是';
  509. }
  510. $inventoryAccountMission->update();
  511. $inventoryAccountMission->createSignStockInventoryPersons();
  512. //新增新记录
  513. $inventoryMission=new InventoryAccountMission();
  514. $inventoryMission->location=$inventoryAccountMission->location;
  515. $inventoryMission->inventory_account_id=$inventoryId;
  516. $inventoryMission->commodity_id=$inventoryAccountMission->commodity_id;
  517. $inventoryMission->produced_at=$produced_at;
  518. $inventoryMission->valid_at=$valid_at;
  519. $inventoryMission->batch_number=$batch_number;
  520. $inventoryMission->erp_type_position=$inventoryAccountMission->erp_type_position;
  521. $inventoryMission->quality=$inventoryAccountMission->quality;
  522. $inventoryMission->stored_amount=0;
  523. $inventoryMission->verified_amount=$count;
  524. $inventoryMission->difference_amount=$count;
  525. $inventoryMission->checked='是';
  526. if($inventory->status=='复盘中'){
  527. $inventoryMission->re_checked_amount=$count;
  528. $inventoryMission->returned='否';
  529. }
  530. $inventoryMission->save();
  531. $inventoryMissionId=$inventoryMission->id??null;
  532. $inventoryMission->createSignStockInventoryPersons();
  533. }else{
  534. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  535. }
  536. $request=[
  537. 'id'=>$id,
  538. 'count'=>$count,
  539. 'inventoryId'=>$inventoryId,
  540. 'produced_at'=>$produced_at,
  541. 'valid_at'=>$valid_at,
  542. 'batch_number'=>$batch_number,
  543. ];
  544. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  545. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->whereIn('id',[$id,$inventoryMissionId])->get();
  546. return $inventoryAccountMission;
  547. }
  548. public function 盘点($inventoryId,$count,$inventoryAccountMission){
  549. $inventory=InventoryAccount::find($inventoryId);
  550. if($inventory->status=='复盘中'){
  551. $inventoryAccountMission->re_checked_amount=$count;
  552. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  553. $inventoryAccountMission->checked='已复核';
  554. if ($inventoryAccountMission->difference_amount==0){
  555. $inventoryAccountMission->returned='是';
  556. }else{
  557. $inventoryAccountMission->returned='否';
  558. }
  559. }else{//初盘
  560. $inventoryAccountMission->verified_amount=$count;
  561. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  562. $inventoryAccountMission->checked='是';
  563. }
  564. $inventoryAccountMission->update();
  565. $inventoryAccountMission->createSignStockInventoryPersons();
  566. }
  567. public function 删除盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  568. $inventoryAccountMission=InventoryAccountMission::query()->where('id',$inventoryAccountMissionId)->delete();
  569. Controller::logS(__METHOD__,__FUNCTION__,json_encode($inventoryAccountMissionId));
  570. if ($inventoryAccountMission>0){
  571. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  572. $inventoryAccount->total=$inventoryAccount->total-1;
  573. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  574. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  575. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  576. $inventoryAccount->update();
  577. Controller::logS(__METHOD__,'删除盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  578. }
  579. return $inventoryAccountMission;
  580. }
  581. public function 跳过盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  582. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryAccountMissionId);
  583. $inventoryAccountMission->checked='跳过';
  584. $inventoryAccountMission->update();
  585. LogService::log(__METHOD__,"跳过盘点记录修改checked状态",json_encode($inventoryAccountMissionId));
  586. if ($inventoryAccountMission->checked=='跳过'){
  587. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  588. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  589. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  590. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  591. $inventoryAccount->update();
  592. Controller::logS(__METHOD__,'跳过盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  593. }
  594. return $inventoryAccountMission;
  595. }
  596. public function searchCommodityByBarcode($barcode,$owner_code){
  597. $oracleBasSku=OracleBasSKU::query()
  598. ->where('ALTERNATE_SKU1',$barcode)
  599. ->orWhere('ALTERNATE_SKU2',$barcode)
  600. ->orWhere('ALTERNATE_SKU3',$barcode)
  601. ->where('customerid',$owner_code)->first();
  602. if ($oracleBasSku){
  603. return $oracleBasSku;
  604. }else{
  605. return null;
  606. }
  607. }
  608. }