InventoryService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\Http\Controllers\Controller;
  5. use App\Inventory;
  6. use App\inventoryDailyLog;
  7. use App\InventoryDailyLoggingOwner;
  8. use App\InventoryMission;
  9. use App\OraccleBasCustomer;
  10. use App\OracleActTransactionLog;
  11. use App\Owner;
  12. use App\Services\common\QueryService;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Gate;
  17. class InventoryService
  18. {
  19. private function conditionQuery(Request $request){
  20. $inventories=Inventory::query()->with(['owner'])->orderBy('id','desc');
  21. $columnQueryRules=[
  22. 'owner_id' => ['multi' => ','],
  23. 'date_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  24. 'date_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  25. ];
  26. $inventories = app(QueryService::class)->query($request->input(),$inventories,$columnQueryRules);
  27. return $inventories;
  28. }
  29. public function paginate(Request $request){
  30. $inventories = $this->conditionQuery($request);
  31. return $inventories->paginate($request->paginate ?? 50);
  32. }
  33. public function get(Request $request){
  34. $inventories = $this->conditionQuery($request);
  35. return $inventories->get();
  36. }
  37. public function some(Request $request){
  38. return Inventory::query()->with(['owner'])->orderBy('id','DESC')
  39. ->whereIn('id',explode(',',$request->data))->get();
  40. }
  41. public function conditionSearch(Request $request){
  42. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  43. $date_start=$request->input('formData.date_start');
  44. $date_end=$request->input('formData.date_end');
  45. $ownerId=$request->input('formData.owner_id')[0];
  46. $descr_c=Owner::where('id',$ownerId)->value('name');
  47. $sql='select * from (select result.*,rownum rn from (';
  48. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  49. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  50. $sql.=' lot.LotAtt04 批号, lot.LotAtt01 生产日期, lot.LotAtt03 入库日期';
  51. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  52. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  53. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  54. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  55. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  56. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  57. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  58. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  59. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  60. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  61. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  62. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  63. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  64. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  65. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  66. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  67. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  68. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  69. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  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 TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  73. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  74. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  75. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  76. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  77. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  78. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  79. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt01,lot.LotAtt03,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  80. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  81. $sql.=' )result where 1=1 ';
  82. if ($descr_c){
  83. $sql .= ' and 货主 in (';
  84. $descr_cs = explode(',',$descr_c);
  85. foreach ($descr_cs as $index => $descr_c){
  86. if ($index != 0)$sql .= ',';
  87. $sql .= "'".$descr_c."'";
  88. }
  89. $sql .= ') ';
  90. }
  91. $sql.=' ) ';
  92. return DB::connection('oracle')->select($sql);
  93. }
  94. //创建盘点任务
  95. public function createMission(Request $request){
  96. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  97. $date_start=$request->input('formData.date_start');
  98. $date_end=$request->input('formData.date_end');
  99. $ownerIds=$request->input('formData.owner_id');
  100. if (count($ownerIds)<=0) return null;
  101. if ($date_start&&$date_end){
  102. $type='动盘';
  103. }elseif (!$date_start&&!$date_end){
  104. $name=Owner::where('id',$ownerIds[0])->value('name');
  105. $ownerName=OraccleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  106. $date_start=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','asc')->value('addtime');
  107. $date_end=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','desc')->value('addtime');
  108. $type='全盘';
  109. }else{
  110. return null;
  111. }
  112. $ownerId=$ownerIds[0];
  113. $inventory=new Inventory([
  114. 'owner_id'=>$ownerId,
  115. 'type'=>$type,
  116. 'start_at'=>$date_start,
  117. 'end_at'=>$date_end,
  118. ]);
  119. $inventory->save();
  120. $this->createInventoryMissionRecord($request,$inventory['id'],$ownerId);
  121. Controller::logS(__METHOD__,"创建盘点记录任务__".__FUNCTION__,json_encode($request,$inventory['id'],$ownerId),Auth::user()['id']);
  122. $inventoryMissionCount=InventoryMission::where('inventory_id',$inventory['id'])->count();
  123. $inventory->total=$inventoryMissionCount;
  124. $inventory->update();
  125. Controller::logS(__METHOD__,"创建盘点任务__".__FUNCTION__,json_encode($request),Auth::user()['id']);
  126. return $inventory;
  127. }
  128. //创建盘点记录任务
  129. public function createInventoryMissionRecord($request,$inventoryId,$ownerId){
  130. $wmsInventories=$this->conditionSearch($request);
  131. foreach ($wmsInventories as $wmsInventory){
  132. $commodity=Commodity::query()->firstOrCreate([
  133. 'owner_id'=>$ownerId,
  134. 'sku'=>$wmsInventory->产品编码,
  135. ]);
  136. if (!$commodity->name){
  137. $commodity->name=$wmsInventory->商品名称;
  138. $commodity->update();
  139. Controller::logS(__METHOD__,"根据wms产品编码和货主查询或创建商品信息__".__FUNCTION__,json_encode($wmsInventory));
  140. }
  141. $commodity->newBarcode($wmsInventory->产品条码);
  142. Controller::logS(__METHOD__,"根据wms产品条码和商品id查询或创建商品条码信息__".__FUNCTION__,json_encode($wmsInventory));
  143. $inventoryMission = new InventoryMission();
  144. $inventoryMission->commodity_id=$commodity->id;
  145. $inventoryMission->inventory_id=$inventoryId;
  146. $inventoryMission->location=$wmsInventory->库位;
  147. $inventoryMission->produced_at=$wmsInventory->生产日期;
  148. $inventoryMission->valid_at=$wmsInventory->失效日期;
  149. $inventoryMission->stored_at=$wmsInventory->入库日期;
  150. $inventoryMission->batch_number=$wmsInventory->批号;
  151. $inventoryMission->erp_type_position=$wmsInventory->属性仓;
  152. $inventoryMission->quality=$wmsInventory->质量状态;
  153. $inventoryMission->stored_amount=$wmsInventory->在库数量;
  154. $inventoryMission->occupied_amount=$wmsInventory->占用数量;
  155. $inventoryMission->save();
  156. }
  157. }
  158. //盘点库存
  159. public function stockInventory($request){
  160. $inventoryId=$request->inventoryId;
  161. $count=$request->count;
  162. $location=$request->location;
  163. $barcode=$request->barcode;
  164. $inventoryMission=InventoryMission::with(['commodity'=>function($query)use($barcode){
  165. return $query->with(['barcodes'=>function($sql)use($barcode){
  166. return $sql->where('code',$barcode);
  167. }]);
  168. }])->where('location',$location)->where('inventory_id',$inventoryId)->first();
  169. if (!$inventoryMission) return null;
  170. $inventoryMission->verified_amount=$count;
  171. $inventoryMission->difference_amount=abs($inventoryMission->stored_amount-$count);
  172. $inventoryMission->checked='是';
  173. $inventoryMission->update();
  174. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  175. return $inventoryMission;
  176. }
  177. //盘点修改盘点任务中的已盘条数
  178. public function updateInventoryMissionProcessed($request){
  179. $inventoryId=$request->inventoryId;
  180. $inventory=Inventory::find($inventoryId);
  181. $processed=InventoryMission::where('checked','是')->where('inventory_id',$inventoryId)->count();
  182. $inventory->processed=$processed;
  183. $inventory->update();
  184. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($request));
  185. return $inventory;
  186. }
  187. //库存体积条件
  188. function conditionQueryDailyLog(array $param){
  189. $inventoryDailyLogs = InventoryDailyLog::query()->with(['owner','commodity'=>function($query){
  190. $query->with('barcodes');
  191. }])->orderByDesc('id');
  192. $columnQueryRules=[
  193. 'owner_id' => ['multi' => ','],
  194. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  195. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  196. ];
  197. $inventoryDailyLogs = app(QueryService::class)->query($param,$inventoryDailyLogs,$columnQueryRules);
  198. return $inventoryDailyLogs;
  199. }
  200. //库存体积
  201. public function getInventoryDailyLog(array $param){
  202. return $this->conditionQueryDailyLog($param)->paginate($param['paginate'] ?? 50);
  203. }
  204. //获取开启监听记录货主
  205. public function getInventoryDailyLoggingOwner($column = ['id','owner_id'], $status = "启用"){
  206. if (!is_array($column)) {
  207. $column = [$column];
  208. }
  209. return InventoryDailyLoggingOwner::query()->select($column)->where('status',$status)->get();
  210. }
  211. //录入监听记录货主
  212. public function firstOrCreate($param,$column = null){
  213. if ($column)return InventoryDailyLoggingOwner::query()->firstOrCreate($param,$column);
  214. return InventoryDailyLoggingOwner::query()->firstOrCreate($param);
  215. }
  216. }