InventoryAccountService.php 36 KB

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