InventoryAccountService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\CommodityBarcode;
  5. use App\Http\Controllers\Controller;
  6. use App\InventoryAccount;
  7. use App\InventoryAccountMission;
  8. use App\OraccleBasCustomer;
  9. use App\OracleInvLotAtt;
  10. use App\OracleInvLotLocId;
  11. use App\Owner;
  12. use App\Services\common\QueryService;
  13. use App\Sign;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\DB;
  18. class InventoryAccountService
  19. {
  20. private function conditionQuery($queryParam){
  21. $inventories=InventoryAccount::query()->with(['owner','creator'])->orderBy('id','desc');
  22. $columnQueryRules=[
  23. 'owner_id' => ['multi' => ','],
  24. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  25. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  26. ];
  27. $inventories = app(QueryService::class)->query($queryParam,$inventories,$columnQueryRules);
  28. return $inventories;
  29. }
  30. public function paginate($queryParam){
  31. $inventories = $this->conditionQuery($queryParam);
  32. return $inventories->paginate($queryParam['paginate'] ?? 50);
  33. }
  34. public function get($queryParam){
  35. $inventories = $this->conditionQuery($queryParam);
  36. return $inventories->get();
  37. }
  38. public function some($queryParam){
  39. return InventoryAccount::query()->with(['owner'])->orderBy('id','DESC')
  40. ->whereIn('id',explode(',',$queryParam['data']))->get();
  41. }
  42. //动盘查询
  43. public function conditionPortStock($date_start,$date_end,$ownerId){
  44. if (!$ownerId) return null;
  45. $descr_c=Owner::where('id',$ownerId)->value('name');
  46. $sql='select * from (select result.*,rownum rn from (';
  47. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  48. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  49. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期, lot.LotAtt03 入库日期';
  50. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  51. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  52. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  53. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  54. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  55. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  56. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  57. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  58. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  59. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  60. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  61. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  62. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  63. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  64. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  65. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  66. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  67. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  68. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  69. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  70. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  71. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  72. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  73. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  74. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  75. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  76. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  77. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  78. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt01,lot.LotAtt03,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  79. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  80. $sql.=' )result where 1=1 ';
  81. if ($descr_c){
  82. $sql .= ' and 货主 in (';
  83. $descr_cs = explode(',',$descr_c);
  84. foreach ($descr_cs as $index => $descr_c){
  85. if ($index != 0)$sql .= ',';
  86. $sql .= "'".$descr_c."'";
  87. }
  88. $sql .= ') ';
  89. }
  90. $sql.=' ) ';
  91. return DB::connection('oracle')->select($sql);
  92. }
  93. //全盘查询
  94. private function conditionTotalStock($ownerId){
  95. $descr_c=Owner::where('id',$ownerId)->value('name');
  96. $sql='select * from (select result.*,rownum rn from (';
  97. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  98. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  99. $sql.=' lot.LotAtt04 批号,lot.LotAtt01 生产日期,lot.LotAtt03 入库日期 ';
  100. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  101. $sql.=' INV_LOT_LOC_ID storeStatus';
  102. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  103. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  104. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  105. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  106. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  107. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME,lot.LotAtt01,lot.LotAtt03 ';
  108. $sql.=' )result where 1=1 ';
  109. if ($descr_c)$sql.=" and 货主 = '".$descr_c."' ";
  110. $sql.=' ) ';
  111. return DB::connection('oracle')->select($sql);
  112. }
  113. //创建盘点任务
  114. public function createMission($date_start,$date_end,$ownerId){
  115. if (!$ownerId) return null;
  116. if ($date_start&&$date_end){
  117. $date_end_time=$date_end.' 23:59:59';
  118. $type='动盘';
  119. $wmsInventories=$this->conditionPortStock($date_start,$date_end,$ownerId);
  120. }elseif (!$date_start&&!$date_end){
  121. $name=Owner::where('id',$ownerId)->value('name');
  122. $ownerName=OraccleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  123. $date_start=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','ASC')->value('addtime');
  124. $date_end_time=OracleInvLotLocId::where('customerid',$ownerName)->orderBy('addtime','DESC')->value('addtime');
  125. $type='全盘';
  126. $wmsInventories=$this->conditionTotalStock($ownerId);
  127. }else{
  128. return null;
  129. }
  130. $inventory=new InventoryAccount([
  131. 'owner_id'=>$ownerId,
  132. 'type'=>$type,
  133. 'start_at'=>$date_start,
  134. 'end_at'=>$date_end_time,
  135. 'total'=>count($wmsInventories),
  136. ]);
  137. $inventory->save();
  138. $inventory->createSignCreator();
  139. $this->createInventoryAccountMissionRecord($ownerId,$inventory['id'],$wmsInventories);
  140. $request=[
  141. 'date_start'=>$date_start,
  142. 'date_end'=>$date_end,
  143. 'ownerId'=>$ownerId,
  144. 'inventoryId'=>$inventory['id'],
  145. ];
  146. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  147. return $inventory;
  148. }
  149. //创建盘点记录任务
  150. public function createInventoryAccountMissionRecord($ownerId,$inventoryAccountId,$wmsInventories){
  151. $commodities=Commodity::query()
  152. ->select('id','owner_id','name','sku')
  153. ->where('owner_id',$ownerId)
  154. ->whereNotNull(['sku','name'])
  155. ->get();
  156. $commodityArr=[];
  157. $inventoryAccountMissions=[];
  158. foreach ($wmsInventories as $wmsInventory){
  159. $commodity=$commodities->where('name',$wmsInventory->商品名称)->where('sku',$wmsInventory->产品编码)->first();
  160. if (!$commodity){
  161. $commodity=new Commodity();
  162. $commodity->owner_id=$ownerId;
  163. $commodity->sku=$wmsInventory->产品编码;
  164. $commodity->name=$wmsInventory->商品名称;
  165. $commodity->save();
  166. $commodity->newBarcode($wmsInventory->产品条码);
  167. $arr=[
  168. 'owner_id'=>$ownerId,
  169. 'sku'=>$wmsInventory->产品编码,
  170. 'name'=>$wmsInventory->商品名称,
  171. 'commodity_id'=>$commodity->id,
  172. 'barcode'=>$wmsInventory->产品条码,
  173. ];
  174. array_push($commodityArr,$arr);
  175. }
  176. if($wmsInventory->质量状态=='ZP') $quality='正品';
  177. if ($wmsInventory->质量状态=='CC') $quality='残次';
  178. if ($wmsInventory->质量状态=='XS') $quality='箱损';
  179. if ($wmsInventory->质量状态=='JS') $quality='机损';
  180. if ($wmsInventory->质量状态=='DJ') $quality='冻结';
  181. if ($wmsInventory->质量状态=='FKT') $quality='封口贴';
  182. $inventoryAccountMission=[
  183. 'commodity_id'=>$commodity->id,
  184. 'inventory_account_id'=>$inventoryAccountId,
  185. 'location'=>$wmsInventory->库位,
  186. 'produced_at'=>$wmsInventory->生产日期,
  187. 'valid_at'=>$wmsInventory->失效日期,
  188. 'stored_at'=>$wmsInventory->入库日期,
  189. 'batch_number'=>$wmsInventory->批号,
  190. 'erp_type_position'=>$wmsInventory->属性仓,
  191. 'quality'=>$quality,
  192. 'stored_amount'=>$wmsInventory->在库数量,
  193. 'occupied_amount'=>$wmsInventory->占用数量,
  194. 'valid_amount'=>$wmsInventory->在库数量-$wmsInventory->占用数量,
  195. 'created_at'=>Carbon::now()->format('Y-m-d H:i:s'),
  196. ];
  197. array_push($inventoryAccountMissions,$inventoryAccountMission);
  198. }
  199. $inventoryAccountMissions=InventoryAccountMission::query()->insert($inventoryAccountMissions);
  200. if ($commodityArr){
  201. Controller::logS(__METHOD__,"插入was中没有的商品信息__".__FUNCTION__,json_encode($commodityArr));
  202. }
  203. Controller::logS(__METHOD__,"批量插入盘点记录__".__FUNCTION__,json_encode($inventoryAccountMissions));
  204. }
  205. //盘点库存
  206. public function stockInventory($location,$barcode,$count,$inventoryAccountId){
  207. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  208. $query->whereHas('barcodes',function($sql)use($barcode){
  209. $sql->where('code','=',$barcode);
  210. });
  211. })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->first();
  212. if (!$inventoryAccountMission) return null;
  213. $inventory=InventoryAccount::find($inventoryAccountId);
  214. if($inventory->status=='复盘中'){
  215. $inventoryAccountMission->re_checked_amount=$count;
  216. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  217. $inventoryAccountMission->checked='已复核';
  218. if ($inventoryAccountMission->difference_amount==0){
  219. $inventoryAccountMission->returned='是';
  220. }else{
  221. $inventoryAccountMission->returned='否';
  222. }
  223. }else{//初盘
  224. $inventoryAccountMission->verified_amount=$count;
  225. $inventoryAccountMission->difference_amount=$count-$inventoryAccountMission->stored_amount;
  226. $inventoryAccountMission->checked='是';
  227. }
  228. $inventoryAccountMission->update();
  229. $inventoryAccountMission->createSignStockInventoryPersons();
  230. $request=[
  231. 'location'=>$location,
  232. 'barcode'=>$barcode,
  233. 'count'=>$count,
  234. 'inventoryId'=>$inventoryAccountId,
  235. ];
  236. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  237. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->whereHas('commodity',function($query)use($barcode){
  238. $query->whereHas('barcodes',function($sql)use($barcode){
  239. $sql->where('code','=',$barcode);
  240. });
  241. })->where('location',$location)->where('inventory_account_id',$inventoryAccountId)->first();
  242. return $inventoryAccountMission;
  243. }
  244. //盘点修改盘点任务数据
  245. public function updateInventory($inventoryAccountId){
  246. $inventoryAccount=InventoryAccount::find($inventoryAccountId);
  247. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  248. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  249. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  250. if($inventoryAccount->status=='待盘点')
  251. $inventoryAccount->status='盘点中';
  252. $inventoryAccount->update();
  253. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($inventoryAccountId));
  254. return $inventoryAccount;
  255. }
  256. //根据该库存和产品条码查询该条盘点记录
  257. public function searchStockInventoryRecord($location,$barcode,$inventoryAccountId){
  258. $inventoryAccountMission=InventoryAccountMission::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. if (!$inventoryAccountMission) return null;
  264. return $inventoryAccountMission;
  265. }
  266. public function 修改质量状态($id,$location,$sku,$quality,$ownerCode){
  267. $lotnum=OracleInvLotLocId::query()->where('locationid',$location)->where('customerid',$ownerCode)->where('sku',$sku)->value('lotnum');
  268. if (!$lotnum) return null;
  269. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->first();
  270. if(!isset($oracleInvLotAtt))return null;
  271. if ($quality=='正品')$status='ZP';
  272. if ($quality=='残次')$status='CC';
  273. $oracleInvLotAtt=OracleInvLotAtt::query()->where('lotnum',$lotnum)->where('sku',$sku)->where('customerid',$ownerCode)->update([
  274. 'lotatt08'=>$status,
  275. ]);
  276. if ($oracleInvLotAtt>0){
  277. $inventoryAccountMission=InventoryAccountMission::query()->find($id);
  278. $inventoryAccountMission->quality=$quality;
  279. $inventoryAccountMission=$inventoryAccountMission->update();
  280. return $inventoryAccountMission;
  281. }else{
  282. return null;
  283. }
  284. }
  285. public function 盘点任务完结($id){
  286. $inventoryAccount=InventoryAccount::query()->find($id);
  287. if ($inventoryAccount->status=='复盘中'){
  288. $inventoryAccount->status='已完成';
  289. $inventoryAccount->update();
  290. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($id));
  291. return $inventoryAccount;
  292. }else{
  293. return null;
  294. }
  295. }
  296. public function 增加系统之外的库位记录($location,$barcode,$inventoryId,$count){
  297. $commodity=Commodity::query()->whereHas('barcodes',function ($query)use($barcode){
  298. $query->where('code',$barcode);
  299. })->first();
  300. $inventoryAccountMission=new InventoryAccountMission();
  301. $inventoryAccountMission->location=$location;
  302. $inventoryAccountMission->inventory_account_id=$inventoryId;
  303. $inventoryAccountMission->commodity_id=$commodity->id;
  304. $inventoryAccountMission->verified_amount=$count;//盘点数量
  305. $inventoryAccountMission->difference_amount=$count;//盘点差异
  306. $inventoryAccountMission->checked='是';
  307. $inventoryAccountMission->save();
  308. Controller::logS(__METHOD__,"".__FUNCTION__,json_encode($inventoryId));
  309. if ($inventoryAccountMission){
  310. $inventoryAccountMission->createSignStockInventoryPersons();
  311. Controller::logS(__METHOD__,"增加盘点人".__FUNCTION__,json_encode($inventoryId));
  312. $inventoryAccount=InventoryAccount::query()->find($inventoryId);
  313. $inventoryAccount->total=$inventoryAccount->total+1;
  314. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  315. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  316. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  317. $inventoryAccount->update();
  318. Controller::logS(__METHOD__,"修改盘点任务记录".__FUNCTION__,json_encode($inventoryId));
  319. return $inventoryAccountMission;
  320. }else{
  321. return null;
  322. }
  323. }
  324. }