InventoryAccountService.php 34 KB

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