InventoryAccountService.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\Http\Controllers\Controller;
  5. use App\InventoryAccount;
  6. use App\InventoryAccountMission;
  7. use App\OracleBasSKU;
  8. use App\OracleInvLotAtt;
  9. use App\OracleInvLotLocId;
  10. use App\Owner;
  11. use App\Services\common\BatchUpdateService;
  12. use App\Services\common\DataHandlerService;
  13. use App\Services\common\QueryService;
  14. use Carbon\Carbon;
  15. <<<<<<< HEAD
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\DB;
  18. use App\Traits\ServiceAppAop;
  19. =======
  20. use Faker\Provider\Uuid;
  21. use Illuminate\Support\Facades\Auth;
  22. use Illuminate\Support\Facades\DB;
  23. use App\Traits\ServiceAppAop;
  24. use Illuminate\Support\Facades\Http;
  25. >>>>>>> b0347ca84d8eeedd9c1ac88b1c55b77c6a0613e8
  26. class InventoryAccountService
  27. {
  28. use ServiceAppAop;
  29. protected $modelClass=InventoryAccount::class;
  30. private function conditionQueryInventoryAccountMission(array $params){
  31. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->orderBy('difference_amount','desc');
  32. $columnQueryRules=[];
  33. $inventoryAccountMissions = app(QueryService::class)->query($params,$inventoryAccountMissions,$columnQueryRules,'inventory_account_missions');
  34. return $inventoryAccountMissions;
  35. }
  36. public function getSql(array $params){
  37. return $this->conditionQueryInventoryAccountMission($params)->selectRaw("inventory_account_missions.*")
  38. ->leftJoin('signs','inventory_account_missions.id','signs.signable_id')
  39. ->selectRaw('signs.mark stockInventoryPerson')
  40. ->leftJoin('commodities','inventory_account_missions.commodity_id','commodities.id')
  41. ->selectRaw('commodities.name commodity_name,commodities.sku commodity_sku')
  42. ->leftJoin('commodity_barcodes','commodity_barcodes.commodity_id','commodities.id')
  43. ->selectRaw('commodity_barcodes.code commodity_barcode_code')
  44. ->sql();
  45. }
  46. private function conditionQuery($queryParam){
  47. $inventories=InventoryAccount::query()->with(['owner','creator'])->orderBy('id','desc')->whereIn('owner_id',app("OwnerService")->getQuery());
  48. $columnQueryRules=[
  49. 'owner' => ['alias'=>'owner_id','multi' => ','],
  50. 'type' => ['multi' => ','],
  51. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  52. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  53. ];
  54. $inventories = app(QueryService::class)->query($queryParam,$inventories,$columnQueryRules);
  55. return $inventories;
  56. }
  57. public function paginate($queryParam){
  58. $inventories = $this->conditionQuery($queryParam);
  59. return $inventories->paginate($queryParam['paginate'] ?? 50);
  60. }
  61. public function get($queryParam){
  62. $inventories = $this->conditionQuery($queryParam);
  63. return $inventories->get();
  64. }
  65. public function some($queryParam){
  66. return InventoryAccount::query()->with(['owner'])->orderBy('id','DESC')
  67. ->whereIn('id',explode(',',$queryParam['data']))->get();
  68. }
  69. //动盘查询
  70. public function conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode){
  71. if (!$ownerId) return null;
  72. $code=Owner::where('id',$ownerId)->value('code');
  73. $sql='select * from (select result.*,rownum rn from (';
  74. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  75. $sql.=' sku.Descr_C 商品名称, MAX(lot.LotAtt05) 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  76. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期';
  77. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  78. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  79. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  80. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  81. if ($code)$sql.=" and TOCustomerID = '".$code."' ";
  82. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  83. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  84. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  85. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  86. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  87. if ($code)$sql.=" and FMCUSTOMERID = '".$code."' ";
  88. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  89. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  90. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  91. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  92. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  93. if ($code)$sql.=" and FMCUSTOMERID = '".$code."' ";
  94. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  95. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  96. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  97. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  98. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  99. if ($code)$sql.=" and TOCustomerID = '".$code."' ";
  100. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  101. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  102. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  103. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  104. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  105. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  106. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  107. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  108. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  109. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt01,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  110. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  111. $sql.=' )result where 1=1 ';
  112. if ($code)$sql.=" and 客户 = '".$code."' ";
  113. // if ($location)$sql.=" and 库位 like '".$location."%' ";
  114. if ($location){
  115. $arr=array_filter(preg_split('/[,, ]+/is', $location));
  116. if (count($arr)==1){
  117. $sql.=" and 库位 like '".$location."%'";
  118. }
  119. if (count($arr)>1){
  120. $sql.=" and (库位 like ";
  121. foreach ($arr as $index=>$str){
  122. if ($index==0){
  123. $sql.="'".$str."%'";
  124. continue;
  125. }
  126. $sql.= "or 库位 like'".$str."%'";
  127. }
  128. $sql.=")";
  129. }
  130. }
  131. if ($barcode)$sql=$this->按条码查询($sql,$barcode);
  132. $sql.=' ) ';
  133. return DB::connection('oracle')->select($sql);
  134. }
  135. //全盘查询
  136. public function conditionTotalStock($ownerId,$location,$barcode){
  137. $code=Owner::where('id',$ownerId)->value('code');
  138. $sql='select * from (select result.* from (';
  139. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码1, sku.ALTERNATE_SKU2 产品条码2, sku.ALTERNATE_SKU3 产品条码3, ';
  140. $sql.=' sku.Descr_C 商品名称, MAX(lot.LotAtt05) 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  141. $sql.=' lot.LotAtt04 批号,lot.LotAtt01 生产日期 ';
  142. $sql.=' , SUM(storeStatus.QTY) 在库数量, SUM(storeStatus.QtyAllocated) 占用数量 from ';
  143. $sql.=' INV_LOT_LOC_ID storeStatus';
  144. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  145. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  146. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  147. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1,sku.ALTERNATE_SKU2,sku.ALTERNATE_SKU3 ';
  148. $sql.=' ,sku.Descr_C,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  149. $sql.=' ,storeStatus.CUSTOMERID,lot.LotAtt01 ';
  150. $sql.=' )result where 1=1 ';
  151. if ($code)$sql.=" and 客户 = '".$code."' ";
  152. if ($location){
  153. $arr=array_filter(preg_split('/[,, ]+/is', $location));
  154. if (count($arr)==1){
  155. $sql.=" and 库位 like '".$location."%'";
  156. }
  157. if (count($arr)>1){
  158. $sql.=" and (库位 like ";
  159. foreach ($arr as $index=>$str){
  160. if ($index==0){
  161. $sql.="'".$str."%'";
  162. continue;
  163. }
  164. $sql.= "or 库位 like'".$str."%'";
  165. }
  166. $sql.=")";
  167. }
  168. }
  169. if ($barcode)$sql=$this->按条码查询($sql,$barcode);
  170. $sql.=' ) ';
  171. return DB::connection('oracle')->select($sql);
  172. }
  173. private function 按条码查询($sql,$barcode){
  174. $arr=array_filter(preg_split('/[,, ]+/is', $barcode));
  175. if (count($arr)==1){
  176. $sql.=" and (产品条码1 like '".$barcode."%' or 产品条码2 like '".$barcode."%' or 产品条码3 like '".$barcode."%')";
  177. }
  178. if (count($arr)>1){
  179. $sql.=" and (产品条码1 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 产品条码2 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.=" or 产品条码3 in (";
  198. foreach ($arr as $index=>$str){
  199. if ($index==0){
  200. $sql.="'".$str."'";
  201. continue;
  202. }
  203. $sql.=",'".$str."'";
  204. }
  205. $sql.=")";
  206. $sql.=")";
  207. }
  208. return $sql;
  209. }
  210. //创建盘点任务
  211. public function createMission($date_start,$date_end,$ownerId,$location,$barcode){
  212. if (!$ownerId) return null;
  213. if ($date_start&&$date_end){
  214. $date_end_time=$date_end.' 23:59:59';
  215. if ($location||$barcode)$type='局部盘点';
  216. if (!$location&&!$barcode)$type='动盘';
  217. $wmsInventories=$this->conditionPortStock($date_start,$date_end,$ownerId,$location,$barcode);
  218. //动盘 判断库存为0的ide料想是否放有其他商品
  219. foreach ($wmsInventories as $key=>$wmsInventory){
  220. if (!$wmsInventory->在库数量 && strpos($wmsInventory->库位,'IDE') !== false){
  221. /** @var HandInStorageService $handInStorageService */
  222. $handInStorageService=app('HandInStorageService');
  223. $invs=$handInStorageService->getInventoryInfos($wmsInventory->库位);
  224. if (count($invs)>0) unset($wmsInventories[$key]);
  225. }
  226. }
  227. }
  228. if (!$date_start&&!$date_end){
  229. $ownerName=Owner::where('id',$ownerId)->value('code');
  230. //$ownerName=OracleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  231. $date_start=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','ASC')->value('addtime');
  232. $date_end_time=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','DESC')->value('addtime');
  233. if ($location||$barcode)$type='局部盘点';
  234. if (!$location&&!$barcode)$type='全盘';
  235. $wmsInventories=$this->conditionTotalStock($ownerId,$location,$barcode);
  236. }
  237. $inventory=new InventoryAccount([
  238. 'owner_id'=>$ownerId,
  239. 'remark'=>$location,
  240. 'type'=>$type,
  241. 'start_at'=>$date_start,
  242. 'end_at'=>$date_end_time,
  243. 'total'=>count($wmsInventories),
  244. ]);
  245. $inventory->save();
  246. $inventory->createSignCreator();
  247. $this->createInventoryAccountMissionRecord($ownerId,$inventory['id'],$wmsInventories);
  248. $request=[
  249. 'date_start'=>$date_start,
  250. 'date_end'=>$date_end,
  251. 'ownerId'=>$ownerId,
  252. 'location'=>$location,
  253. 'barcode'=>$barcode,
  254. 'inventoryId'=>$inventory['id'],
  255. ];
  256. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  257. return $inventory;
  258. }
  259. //更新盘点记录任务
  260. public function updateInventoryAccountMissionRecord($ownerId,$inventoryAccountId,array $locations,$wmsInventories){
  261. ini_set('memory_limit','1526M');
  262. $ownerCode=Owner::query()->where('id',$ownerId)->value('code');
  263. $commodities=Commodity::query()
  264. ->select('id','owner_id','name','sku')
  265. ->where('owner_id',$ownerId)
  266. ->get();
  267. $commoditiesArr=[];
  268. foreach ($commodities as $commodity){
  269. $commoditiesArr[$ownerCode.'|'.$commodity['sku']]=$commodity;
  270. }
  271. $inventoryAccountMissions = InventoryAccountMission::query()
  272. ->where('inventory_account_id', $inventoryAccountId)
  273. ->whereIn('location', array_unique($locations))
  274. ->get();
  275. /** @var DataHandlerService $dataHandlerService */
  276. $dataHandlerService = app(DataHandlerService::class);
  277. $inventoryAccountMissions_map = $dataHandlerService->dataHeader(['inventory_account_id','commodity_id','location'], $inventoryAccountMissions);
  278. $updateParams = [[
  279. 'id', 'commodity_id', 'inventory_account_id', 'location', 'produced_at', 'valid_at', 'batch_number',
  280. 'erp_type_position', 'quality', 'stored_amount', 'occupied_amount', 'valid_amount','created_at','updated_at'
  281. ]];
  282. foreach ($wmsInventories as $wmsInventory){
  283. $commodity=$commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]??null;
  284. if (!$commodity)continue;
  285. $inventoryAccountMission = $dataHandlerService
  286. ->getKeyValue(['inventory_account_id'=>$inventoryAccountId,'commodity_id' => $commodity['id'],'location' => $wmsInventory->库位], $inventoryAccountMissions_map);
  287. if (!$inventoryAccountMission)continue;
  288. if($wmsInventory->质量状态=='ZP') $quality='正品';
  289. if ($wmsInventory->质量状态=='CC') $quality='残次';
  290. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  291. if ($wmsInventory->质量状态=='JS') $quality='机损';
  292. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  293. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  294. if (
  295. $inventoryAccountMission->produced_at != $wmsInventory->生产日期 ||
  296. $inventoryAccountMission->valid_at != $wmsInventory->失效日期 ||
  297. $inventoryAccountMission->batch_number != $wmsInventory->批号 ||
  298. $inventoryAccountMission->erp_type_position != $wmsInventory->属性仓 ||
  299. $inventoryAccountMission->quality != $quality ||
  300. $inventoryAccountMission->stored_amount != $wmsInventory->在库数量 ||
  301. $inventoryAccountMission->occupied_amount != $wmsInventory->占用数量 ||
  302. $inventoryAccountMission->valid_amount != ($wmsInventory->在库数量-$wmsInventory->占用数量)
  303. ){
  304. $updateParams[] = [
  305. 'id' => $inventoryAccountMission->id,
  306. 'commodity_id'=>$commodity->id,
  307. 'inventory_account_id'=>$inventoryAccountId,
  308. 'location'=>$wmsInventory->库位,
  309. 'produced_at'=>$wmsInventory->生产日期,
  310. 'valid_at'=>$wmsInventory->失效日期,
  311. 'batch_number'=>$wmsInventory->批号,
  312. 'erp_type_position'=>$wmsInventory->属性仓,
  313. 'quality'=>$quality,
  314. 'stored_amount'=>$wmsInventory->在库数量,
  315. 'occupied_amount'=>$wmsInventory->占用数量,
  316. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  317. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  318. 'updated_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  319. ];
  320. }
  321. }
  322. app(BatchUpdateService::class)->batchUpdate('inventory_account_missions', $updateParams);
  323. Controller::logS(__METHOD__,"批量更新盘点记录__".__FUNCTION__,json_encode($updateParams));
  324. }
  325. //创建盘点记录任务
  326. public function createInventoryAccountMissionRecord($ownerId,$inventoryAccountId,$wmsInventories){
  327. ini_set('memory_limit','1526M');
  328. $ownerCode=Owner::query()->where('id',$ownerId)->value('code');
  329. $commodities=Commodity::query()
  330. ->select('id','owner_id','name','sku')
  331. ->where('owner_id',$ownerId)
  332. ->get();
  333. $commoditiesArr=[];
  334. foreach ($commodities as $commodity){
  335. $commoditiesArr[$ownerCode.'|'.$commodity['sku']]=$commodity;
  336. }
  337. $commodityArrBarcode=[];
  338. $commodityArr=[];
  339. $inventoryAccountMissions=[];
  340. foreach ($wmsInventories as $wmsInventory){
  341. $commodity=$commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]??null;
  342. if (!$commodity){
  343. $commodity=new Commodity();
  344. $commodity->owner_id=$ownerId;
  345. $commodity->sku=$wmsInventory->产品编码;
  346. $commodity->name=$wmsInventory->商品名称;
  347. $commodity->save();
  348. $commoditiesArr[$wmsInventory->客户.'|'.$wmsInventory->产品编码]=$commodity;
  349. array_push($commodityArr,$commodity);
  350. }
  351. if ($wmsInventory->产品条码1){
  352. $commodity->newBarcode($wmsInventory->产品条码1);
  353. $arr=[
  354. 'owner_id'=>$ownerId,
  355. 'sku'=>$wmsInventory->产品编码,
  356. 'name'=>$wmsInventory->商品名称,
  357. 'commodity_id'=>$commodity->id,
  358. 'barcode'=>$wmsInventory->产品条码1,
  359. ];
  360. }
  361. if($wmsInventory->产品条码2){
  362. $commodity->newBarcode($wmsInventory->产品条码2);
  363. $arr=[
  364. 'owner_id'=>$ownerId,
  365. 'sku'=>$wmsInventory->产品编码,
  366. 'name'=>$wmsInventory->商品名称,
  367. 'commodity_id'=>$commodity->id,
  368. 'barcode'=>$wmsInventory->产品条码2,
  369. ];
  370. }
  371. if($wmsInventory->产品条码3){
  372. $commodity->newBarcode($wmsInventory->产品条码3);
  373. $arr=[
  374. 'owner_id'=>$ownerId,
  375. 'sku'=>$wmsInventory->产品编码,
  376. 'name'=>$wmsInventory->商品名称,
  377. 'commodity_id'=>$commodity->id,
  378. 'barcode'=>$wmsInventory->产品条码3,
  379. ];
  380. }
  381. array_push($commodityArrBarcode,$arr);
  382. if($wmsInventory->质量状态=='ZP') $quality='正品';
  383. if ($wmsInventory->质量状态=='CC') $quality='残次';
  384. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  385. if ($wmsInventory->质量状态=='JS') $quality='机损';
  386. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  387. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  388. $inventoryAccountMission=[
  389. 'commodity_id'=>$commodity->id,
  390. 'inventory_account_id'=>$inventoryAccountId,
  391. 'location'=>$wmsInventory->库位,
  392. 'produced_at'=>$wmsInventory->生产日期,
  393. 'valid_at'=>$wmsInventory->失效日期,
  394. 'batch_number'=>$wmsInventory->批号,
  395. 'erp_type_position'=>$wmsInventory->属性仓,
  396. 'quality'=>$quality,
  397. 'stored_amount'=>$wmsInventory->在库数量,
  398. 'occupied_amount'=>$wmsInventory->占用数量,
  399. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  400. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  401. ];
  402. array_push($inventoryAccountMissions,$inventoryAccountMission);
  403. }
  404. $inventoryAccountMissions=InventoryAccountMission::query()->insert($inventoryAccountMissions);
  405. if ($commodityArr){
  406. Controller::logS(__METHOD__,"插入was中商品信息__".__FUNCTION__,json_encode($commodityArr));
  407. Controller::logS(__METHOD__,"插入was中商品信息的条码信息__".__FUNCTION__,json_encode($commodityArrBarcode));
  408. }
  409. Controller::logS(__METHOD__,"批量插入盘点记录__".__FUNCTION__,json_encode($inventoryAccountMissions));
  410. }
  411. //盘点库存
  412. public function stockInventory($id,$location,$barcode,$count,$inventoryAccountId){
  413. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  414. if (!$inventoryAccountMission)return null;
  415. $this->盘点($inventoryAccountId,$count,$inventoryAccountMission);
  416. $request=[
  417. 'location'=>$location,
  418. 'barcode'=>$barcode,
  419. 'count'=>$count,
  420. 'inventoryId'=>$inventoryAccountId,
  421. ];
  422. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  423. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  424. return $inventoryAccountMission;
  425. }
  426. //盘点修改盘点任务数据
  427. public function updateInventory($inventoryAccountId){
  428. $inventoryAccount=InventoryAccount::find($inventoryAccountId);
  429. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  430. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  431. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  432. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount(); //跳过数
  433. if($inventoryAccount->status=='待盘点')
  434. $inventoryAccount->status='盘点中';
  435. $inventoryAccount->update();
  436. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($inventoryAccountId));
  437. return $inventoryAccount;
  438. }
  439. //根据该库存和产品条码查询该条盘点记录
  440. public function searchStockInventoryRecord($location,$barcode,$inventoryAccountId){
  441. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  442. $query->whereHas('barcodes',function($sql)use($barcode){
  443. $sql->where('code',$barcode);
  444. });
  445. })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->get();
  446. return $inventoryAccountMissions;
  447. }
  448. public function 修改质量状态($id,$location,$sku,$quality,$ownerCode){
  449. $lotnum=OracleInvLotLocId::query()->where('locationid',$location)->where('customerid',$ownerCode)->where('sku',$sku)->value('lotnum');
  450. if (!$lotnum) return null;
  451. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->first();
  452. if(!isset($oracleInvLotAtt))return null;
  453. if ($quality=='正品')$status='ZP';
  454. if ($quality=='残次')$status='CC';
  455. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->update([
  456. 'lotatt08'=>$status,
  457. ]);
  458. if ($oracleInvLotAtt>0){
  459. $inventoryAccountMission=InventoryAccountMission::query()->find($id);
  460. $inventoryAccountMission->quality=$quality;
  461. $inventoryAccountMission=$inventoryAccountMission->update();
  462. return $inventoryAccountMission;
  463. }else{
  464. return null;
  465. }
  466. }
  467. public function 完结盘点任务($id){
  468. $inventoryAccount=InventoryAccount::query()->find($id);
  469. if ($inventoryAccount->status=='复盘中'){
  470. $inventoryAccount->status='已完成';
  471. $inventoryAccount->update();
  472. Controller::logS(__METHOD__,"盘点修改盘点任务状态__".__FUNCTION__,json_encode($id));
  473. return $inventoryAccount;
  474. }else{
  475. return null;
  476. }
  477. }
  478. public function 增加系统之外的盘点记录($location,$barcode,$inventoryId,$count,$owner_code,$param){
  479. if($param=='商品新增'||$param=='库位和商品新增'){
  480. $oracleBasSku=$this->searchCommodityByBarcode($barcode,$owner_code);
  481. if ($oracleBasSku){
  482. $ownerId=Owner::query()->where('code',$owner_code)->value('id');
  483. $commodity=Commodity::query()->where('sku',$oracleBasSku->sku)->where('owner_id',$ownerId)->first();
  484. if (!$commodity){
  485. $commodity=Commodity::query()->create([
  486. 'sku'=>$oracleBasSku->sku,
  487. 'owner_id'=>$ownerId,
  488. 'name'=>$oracleBasSku->descr_c
  489. ]);
  490. $commodity->newBarcode($barcode);
  491. }
  492. }
  493. }
  494. $commodity=Commodity::query()->whereHas('barcodes',function ($query)use($barcode){
  495. $query->where('code',$barcode);
  496. })->orderBy('id','DESC')->first();
  497. $inventoryAccountMission=new InventoryAccountMission();
  498. $inventoryAccountMission->location=$location;
  499. $inventoryAccountMission->inventory_account_id=$inventoryId;
  500. $inventoryAccountMission->commodity_id=$commodity->id;
  501. $inventoryAccountMission->quality='正品';
  502. $inventoryAccountMission->verified_amount=$count;//盘点数量
  503. $inventoryAccountMission->difference_amount=$count;//盘点差异
  504. $inventoryAccountMission->checked='是';
  505. $inventoryAccountMission->save();
  506. Controller::logS(__METHOD__,"增加系统之外的盘点记录".__FUNCTION__,json_encode($inventoryAccountMission));
  507. if ($inventoryAccountMission){
  508. $inventoryAccountMission->createSignStockInventoryPersons();
  509. Controller::logS(__METHOD__,"增加盘点人".__FUNCTION__,json_encode($inventoryId));
  510. $inventoryAccount=InventoryAccount::query()->find($inventoryId);
  511. $inventoryAccount->total=$inventoryAccount->total+1;
  512. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  513. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  514. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  515. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  516. $inventoryAccount->update();
  517. Controller::logS(__METHOD__,"修改盘点任务记录".__FUNCTION__,json_encode($inventoryId));
  518. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('id',$inventoryAccountMission->id)->first();
  519. return $inventoryAccountMission;
  520. }else{
  521. return null;
  522. }
  523. }
  524. public function 盘点选中任务($id,$count,$inventoryId){
  525. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  526. if (!$inventoryAccountMission) return null;
  527. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  528. $request=[
  529. 'id'=>$id,
  530. 'count'=>$count,
  531. 'inventoryId'=>$inventoryId,
  532. ];
  533. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  534. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  535. return $inventoryAccountMission;
  536. }
  537. public function 盘点生产日期_失效日期_批号有改动任务($id,$count,$inventoryId,$produced_at,$valid_at,$batch_number){
  538. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->find($id);
  539. $inventoryMissionId=null;
  540. if (!$inventoryAccountMission) return null;
  541. $inventory=InventoryAccount::find($inventoryId);
  542. if ($produced_at!=$inventoryAccountMission->produced_at||$valid_at!=$inventoryAccountMission->valid_at ||$batch_number!=$inventoryAccountMission->batch_number){
  543. if($inventory->status=='复盘中'){//盘点原记录
  544. $inventoryAccountMission->re_checked_amount=$count;
  545. $inventoryAccountMission->difference_amount=0-$count;
  546. $inventoryAccountMission->checked='已复核';
  547. if ($inventoryAccountMission->difference_amount==0){
  548. $inventoryAccountMission->returned='是';
  549. }else{
  550. $inventoryAccountMission->returned='否';
  551. }
  552. }else{//初盘
  553. $inventoryAccountMission->verified_amount=$count;
  554. $inventoryAccountMission->difference_amount=0-$count;
  555. $inventoryAccountMission->checked='是';
  556. }
  557. $inventoryAccountMission->update();
  558. $inventoryAccountMission->createSignStockInventoryPersons();
  559. //新增新记录
  560. $inventoryMission=new InventoryAccountMission();
  561. $inventoryMission->location=$inventoryAccountMission->location;
  562. $inventoryMission->inventory_account_id=$inventoryId;
  563. $inventoryMission->commodity_id=$inventoryAccountMission->commodity_id;
  564. $inventoryMission->produced_at=$produced_at;
  565. $inventoryMission->valid_at=$valid_at;
  566. $inventoryMission->batch_number=$batch_number;
  567. $inventoryMission->erp_type_position=$inventoryAccountMission->erp_type_position;
  568. $inventoryMission->quality=$inventoryAccountMission->quality;
  569. $inventoryMission->stored_amount=0;
  570. $inventoryMission->verified_amount=$count;
  571. $inventoryMission->difference_amount=$count;
  572. $inventoryMission->checked='是';
  573. if($inventory->status=='复盘中'){
  574. $inventoryMission->re_checked_amount=$count;
  575. $inventoryMission->returned='否';
  576. }
  577. $inventoryMission->save();
  578. $inventoryMissionId=$inventoryMission->id??null;
  579. $inventoryMission->createSignStockInventoryPersons();
  580. }else{
  581. $this->盘点($inventoryId,$count,$inventoryAccountMission);
  582. }
  583. $request=[
  584. 'id'=>$id,
  585. 'count'=>$count,
  586. 'inventoryId'=>$inventoryId,
  587. 'produced_at'=>$produced_at,
  588. 'valid_at'=>$valid_at,
  589. 'batch_number'=>$batch_number,
  590. ];
  591. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  592. $inventoryAccountMission=InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->whereIn('id',[$id,$inventoryMissionId])->get();
  593. return $inventoryAccountMission;
  594. }
  595. public function 盘点($inventoryId,$count,$inventoryAccountMission){
  596. $inventory=InventoryAccount::find($inventoryId);
  597. if($inventory->status=='复盘中'){
  598. $inventoryAccountMission->re_checked_amount=$count;
  599. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  600. $inventoryAccountMission->checked='已复核';
  601. if ($inventoryAccountMission->difference_amount==0){
  602. $inventoryAccountMission->returned='是';
  603. }else{
  604. $inventoryAccountMission->returned='否';
  605. }
  606. }else{//初盘
  607. $inventoryAccountMission->verified_amount=$count;
  608. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  609. $inventoryAccountMission->checked='是';
  610. }
  611. $inventoryAccountMission->update();
  612. $inventoryAccountMission->createSignStockInventoryPersons();
  613. <<<<<<< HEAD
  614. return $inventoryAccountMission;
  615. }
  616. =======
  617. $owner=Owner::query()->where('id',$inventory->owner_id)->first();
  618. if ($owner){
  619. $this->sendPiece(Uuid::uuid().'inventory',Auth::user()['id']??'0',
  620. $inventory->owner_id,$owner->warehouse_id,date("Y-m-d H:i:s"),$count);
  621. }
  622. return $inventoryAccountMission;
  623. }
  624. public function sendPiece($messageId, $userId, $ownerId, $warehouseId, $date, $number) {
  625. $url = config('api.java.base').config('api.java.piece.log.record');
  626. $request = [
  627. "user_type" => 0,
  628. "job_type" => "盘点",
  629. "job_name" => "存储盘点",
  630. "form_number" => 0,
  631. "piece_time" => $date,
  632. "message_id" => $messageId,
  633. "user_id" => $userId,
  634. "warehouse_id" => $warehouseId,
  635. "owner_id" => $ownerId,
  636. "item_number" => $number,
  637. ];
  638. app('LogService')->log("盘点计件", "通知服务端", json_encode($request));
  639. $http = Http::post($url, $request);
  640. if (!$http->successful()) {
  641. app('LogService')->log("盘点计件", "sendPiece", '发送失败:'.$http->body());
  642. return;
  643. }
  644. $result = $http->json();
  645. if ($result["code"] != 200) {
  646. app('LogService')->log("盘点计件", "sendPiece", '服务端错误:'.$http->body());
  647. }
  648. }
  649. >>>>>>> b0347ca84d8eeedd9c1ac88b1c55b77c6a0613e8
  650. public function 删除盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  651. $inventoryAccountMission=InventoryAccountMission::query()->where('id',$inventoryAccountMissionId)->delete();
  652. Controller::logS(__METHOD__,__FUNCTION__,json_encode($inventoryAccountMissionId));
  653. if ($inventoryAccountMission>0){
  654. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  655. $inventoryAccount->total=$inventoryAccount->total-1;
  656. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  657. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  658. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  659. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  660. $inventoryAccount->update();
  661. Controller::logS(__METHOD__,'删除盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  662. }
  663. return $inventoryAccountMission;
  664. }
  665. public function 跳过盘点记录($inventoryAccountMissionId,$inventoryAccountId){
  666. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryAccountMissionId);
  667. $inventoryAccountMission->checked='跳过';
  668. $inventoryAccountMission->update();
  669. app('LogService')->log(__METHOD__,"跳过盘点记录修改checked状态",json_encode($inventoryAccountMissionId));
  670. if ($inventoryAccountMission->checked=='跳过'){
  671. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  672. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  673. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  674. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  675. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  676. $inventoryAccount->update();
  677. Controller::logS(__METHOD__,'跳过盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  678. }
  679. return $inventoryAccountMission;
  680. }
  681. public function 确认盘点差异($inventoryAccountMissionId,$inventoryAccountId){
  682. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryAccountMissionId);
  683. $inventoryAccountMission->checked='确认差异';
  684. $inventoryAccountMission->update();
  685. app('LogService')->log(__METHOD__,"跳过盘点记录修改checked状态",json_encode($inventoryAccountMissionId));
  686. return $inventoryAccountMission;
  687. }
  688. public function 批量跳过或确认差异($checkData){
  689. $inventoryAccountMissions=[];
  690. $inventoryAccountId=null;
  691. foreach ($checkData as $inventoryMission){
  692. $inventoryAccountMission=InventoryAccountMission::query()->find($inventoryMission['id']);
  693. $inventoryAccountId=$inventoryAccountMission->inventory_account_id;
  694. if ($inventoryMission['mark']==='未盘')$inventoryAccountMission['checked']='跳过';
  695. if ($inventoryMission['mark']==='未复盘有差异'||$inventoryMission['mark']==='已复盘无差异')$inventoryAccountMission['checked']='确认差异';
  696. $inventoryAccountMission->update();
  697. array_push($inventoryAccountMissions,$inventoryAccountMission);
  698. }
  699. app('LogService')->log(__METHOD__,"批量跳过或确认差异",json_encode($checkData));
  700. if ($inventoryAccountId){
  701. $inventoryAccount=InventoryAccount::query()->find($inventoryAccountId);
  702. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  703. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  704. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  705. $inventoryAccount->ignored=$inventoryAccount->getIgnoredAmount();
  706. $inventoryAccount->update();
  707. Controller::logS(__METHOD__,'批量跳过或确认差异修改盘点任务信息'.__FUNCTION__,json_encode($inventoryAccountId));
  708. }
  709. return $inventoryAccountMissions;
  710. }
  711. public function searchCommodityByBarcode($barcode,$owner_code){
  712. $oracleBasSku=OracleBasSKU::query()
  713. ->where('ALTERNATE_SKU1',$barcode)
  714. ->orWhere('ALTERNATE_SKU2',$barcode)
  715. ->orWhere('ALTERNATE_SKU3',$barcode)
  716. ->where('customerid',$owner_code)->first();
  717. if (!$oracleBasSku)return null;
  718. return $oracleBasSku;
  719. }
  720. public function baseOnBlindReceive($location,$owner_code,$goodses,$inventoryId){
  721. $request=[
  722. 'location'=>$location,
  723. 'owner_code'=>$owner_code,
  724. 'goodses'=>$goodses,
  725. 'inventoryId'=>$inventoryId,
  726. ];
  727. app('LogService')->log(__METHOD__,"盘点基于盲收",json_encode($request));
  728. $inventoryAccountMissions=[];
  729. foreach ($goodses as $good){
  730. $barcode=$good['barcode']??'';
  731. $amount=$good['amount']??'';
  732. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  733. $query->whereHas('barcodes',function($sql)use($barcode){
  734. $sql->where('code','=',$barcode);
  735. });
  736. })->where('location',$location)->where('inventory_account_id',$inventoryId)->first();
  737. if (!$inventoryAccountMission){
  738. $oracleBasSku=$this->searchCommodityByBarcode($barcode,$owner_code);
  739. if ($oracleBasSku){
  740. $param='商品新增';
  741. $inventoryAccountMission=$this->增加系统之外的盘点记录($location,$barcode,$inventoryId,$amount,$owner_code,$param);
  742. }
  743. }else{
  744. $inventoryAccountMission=$this->盘点($inventoryId,$amount,$inventoryAccountMission);
  745. }
  746. array_push($inventoryAccountMissions,$inventoryAccountMission);
  747. }
  748. if (count($inventoryAccountMissions)<1) return null;
  749. return $inventoryAccountMissions;
  750. }
  751. public function batchStockByLocation($missions,$inventoryId)
  752. {
  753. $inventory=InventoryAccount::query()->find($inventoryId);
  754. if($inventory->status=='复盘中'){
  755. $updateParams = [[
  756. 'id','returned','difference_amount','re_checked_amount','checked','updated_at'
  757. ]];
  758. foreach ($missions as $mission){
  759. if (($mission['amount']-$mission['stored_amount'])==0){
  760. $return='是';
  761. }else{
  762. $return='否';
  763. }
  764. $updateParams[] = [
  765. 'id'=>$mission['id'],
  766. 're_checked_amount'=>$mission['amount'],
  767. 'difference_amount'=>$mission['amount']-$mission['stored_amount'],
  768. 'returned'=>$return,
  769. 'checked'=>'已复核',
  770. 'updated_at' =>Carbon::now()->format('Y-m-d H:i:s'),
  771. ];
  772. }
  773. }else{//初盘
  774. $updateParams = [[
  775. 'id','difference_amount','checked','verified_amount','updated_at'
  776. ]];
  777. foreach ($missions as $mission){
  778. $updateParams[] = [
  779. 'id'=>$mission['id'],
  780. 'verified_amount'=>$mission['amount'],
  781. 'difference_amount'=>$mission['amount']-$mission['stored_amount'],
  782. 'checked'=>'是',
  783. 'updated_at' =>Carbon::now()->format('Y-m-d H:i:s'),
  784. ];
  785. }
  786. }
  787. if(count($updateParams) > 1) app(BatchUpdateService::class)->batchUpdate('inventory_account_missions',$updateParams);
  788. LogService::log(__METHOD__,__FUNCTION__,'根据库位批量盘点'.count($updateParams).json_encode($updateParams));
  789. $ids = array_unique(data_get($missions,'*.id'));
  790. $inventoryMissions=InventoryAccountMission::query()->whereIn('id',$ids)->get();
  791. foreach ($inventoryMissions as $inventoryMission){
  792. $inventoryMission->createSignStockInventoryPersons();
  793. }
  794. return InventoryAccountMission::query()->with(['commodity.barcodes','stockInventoryPersons'])->whereIn('id',$ids)->get();
  795. }
  796. }