InventoryAccountService.php 36 KB

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