InventoryAccountService.php 28 KB

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