InventoryAccountService.php 34 KB

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