InventoryService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\Http\Controllers\Controller;
  5. use App\Inventory;
  6. use App\InventoryMission;
  7. use App\OraccleBasCustomer;
  8. use App\OracleActTransactionLog;
  9. use App\Owner;
  10. use App\Services\common\QueryService;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Gate;
  15. class InventoryService
  16. {
  17. private function conditionQuery(Request $request){
  18. $inventories=Inventory::query()->with(['owner'])->orderBy('id','desc');
  19. $columnQueryRules=[
  20. 'owner_id' => ['multi' => ','],
  21. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  22. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  23. ];
  24. $inventories = app(QueryService::class)->query($request,$inventories,$columnQueryRules);
  25. return $inventories;
  26. }
  27. public function paginate(Request $request){
  28. $inventories = $this->conditionQuery($request);
  29. return $inventories->paginate($request->paginate ?? 50);
  30. }
  31. public function get(Request $request){
  32. $inventories = $this->conditionQuery($request);
  33. return $inventories->get();
  34. }
  35. public function some(Request $request){
  36. return Inventory::query()->with(['owner'])->orderBy('id','DESC')
  37. ->whereIn('id',explode(',',$request->data))->get();
  38. }
  39. public function conditionSearch(Request $request){
  40. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  41. $date_start=$request->input('formData.date_start');
  42. $date_end=$request->input('formData.date_end');
  43. $ownerId=$request->input('formData.owner_id')[0];
  44. $descr_c=Owner::where('id',$ownerId)->value('name');
  45. $sql='select * from (select result.*,rownum rn from (';
  46. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  47. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  48. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期, lot.LotAtt03 入库日期';
  49. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  50. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  51. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  52. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  53. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  54. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  55. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  56. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  57. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  58. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  59. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  60. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  61. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  62. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  63. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  64. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  65. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  66. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  67. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  68. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  69. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  70. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  71. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  72. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  73. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  74. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  75. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  76. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  77. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt01,lot.LotAtt03,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  78. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  79. $sql.=' )result where 1=1 ';
  80. if ($descr_c){
  81. $sql .= ' and 货主 in (';
  82. $descr_cs = explode(',',$descr_c);
  83. foreach ($descr_cs as $index => $descr_c){
  84. if ($index != 0)$sql .= ',';
  85. $sql .= "'".$descr_c."'";
  86. }
  87. $sql .= ') ';
  88. }
  89. $sql.=' ) ';
  90. return DB::connection('oracle')->select($sql);
  91. }
  92. //创建盘点任务
  93. public function createMission(Request $request){
  94. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  95. $date_start=$request->input('formData.date_start');
  96. $date_end=$request->input('formData.date_end');
  97. $ownerIds=$request->input('formData.owner_id');
  98. if (count($ownerIds)<=0) return null;
  99. if ($date_start&&$date_end){
  100. $type='动盘';
  101. }elseif (!$date_start&&!$date_end){
  102. $name=Owner::where('id',$ownerIds[0])->value('name');
  103. $ownerName=OraccleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  104. $date_start=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','asc')->value('addtime');
  105. $date_end=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','desc')->value('addtime');
  106. $type='全盘';
  107. }else{
  108. return null;
  109. }
  110. $ownerId=$ownerIds[0];
  111. $inventory=new Inventory([
  112. 'owner_id'=>$ownerId,
  113. 'type'=>$type,
  114. 'start_at'=>$date_start,
  115. 'end_at'=>$date_end,
  116. ]);
  117. $inventory->save();
  118. $this->createInventoryMissionRecord($request,$inventory['id'],$ownerId);
  119. Controller::logS(__METHOD__,"创建盘点记录任务__".__FUNCTION__,json_encode($request,$inventory['id'],$ownerId),Auth::user()['id']);
  120. $inventoryMissionCount=InventoryMission::where('inventory_id',$inventory['id'])->count();
  121. $inventory->total=$inventoryMissionCount;
  122. $inventory->update();
  123. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  124. return $inventory;
  125. }
  126. //创建盘点记录任务
  127. public function createInventoryMissionRecord($request,$inventoryId,$ownerId){
  128. $wmsInventories=$this->conditionSearch($request);
  129. foreach ($wmsInventories as $wmsInventory){
  130. $commodity=Commodity::query()->firstOrCreate([
  131. 'owner_id'=>$ownerId,
  132. 'sku'=>$wmsInventory->产品编码,
  133. 'name'=>$wmsInventory->商品名称,
  134. ]);
  135. $inventoryMission = new InventoryMission();
  136. $inventoryMission->commodity_id=$commodity->id;
  137. $inventoryMission->inventory_id=$inventoryId;
  138. $inventoryMission->location=$wmsInventory->库位;
  139. $inventoryMission->produced_at=$wmsInventory->生产日期;
  140. $inventoryMission->valid_at=$wmsInventory->失效日期;
  141. $inventoryMission->stored_at=$wmsInventory->入库日期;
  142. $inventoryMission->batch_number=$wmsInventory->批号;
  143. $inventoryMission->erp_type_position=$wmsInventory->属性仓;
  144. $inventoryMission->quality=$wmsInventory->质量状态;
  145. $inventoryMission->stored_amount=$wmsInventory->在库数量;
  146. $inventoryMission->occupied_amount=$wmsInventory->占用数量;
  147. $inventoryMission->save();
  148. }
  149. }
  150. }