InventoryAccountService.php 36 KB

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