InventoryService.php 11 KB

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