InventoryAccountService.php 36 KB

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