InventoryService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. return DB::connection('oracle')->select($sql);
  89. }
  90. //创建盘点任务
  91. public function createMission($date_start,$date_end,$ownerId){
  92. if (!$ownerId) return null;
  93. if ($date_start&&$date_end){
  94. $date_end_time=$date_end.' 23:59:59';
  95. $type='动盘';
  96. }elseif (!$date_start&&!$date_end){
  97. $name=Owner::where('id',$ownerId)->value('name');
  98. $ownerName=OraccleBasCustomer::where('customer_type','OW')->where('active_flag','Y')->where('descr_c',$name)->value('customerid');
  99. $date_start=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','asc')->value('addtime');
  100. $date_end_time=OracleActTransactionLog::where('fmcustomerid',$ownerName)->orderBy('addtime','desc')->value('addtime');
  101. $type='全盘';
  102. }else{
  103. return null;
  104. }
  105. $inventory=new Inventory([
  106. 'owner_id'=>$ownerId,
  107. 'type'=>$type,
  108. 'start_at'=>$date_start,
  109. 'end_at'=>$date_end_time,
  110. ]);
  111. $inventory->save();
  112. $this->createInventoryMissionRecord($date_start,$date_end,$ownerId,$inventory['id']);
  113. $request=[
  114. 'date_start'=>$date_start,
  115. 'date_end'=>$date_end,
  116. 'ownerId'=>$ownerId,
  117. 'inventoryId'=>$inventory['id'],
  118. ];
  119. Controller::logS(__METHOD__,"创建盘点记录任务__".__FUNCTION__,json_encode($request),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($date_start,$date_end,$ownerId,$inventoryId){
  128. if (!$ownerId) return null;
  129. $wmsInventories=$this->conditionSearch($date_start,$date_end,$ownerId);
  130. foreach ($wmsInventories as $wmsInventory){
  131. $commodity=Commodity::query()->firstOrCreate([
  132. 'owner_id'=>$ownerId,
  133. 'sku'=>$wmsInventory->产品编码,
  134. ]);
  135. if (!$commodity->name){
  136. $commodity->name=$wmsInventory->商品名称;
  137. $commodity->update();
  138. Controller::logS(__METHOD__,"根据wms产品编码和货主查询或创建商品信息__".__FUNCTION__,json_encode($wmsInventory));
  139. }
  140. $commodity->newBarcode($wmsInventory->产品条码);
  141. Controller::logS(__METHOD__,"根据wms产品条码和商品id查询或创建商品条码信息__".__FUNCTION__,json_encode($wmsInventory));
  142. $inventoryMission = new InventoryMission();
  143. $inventoryMission->commodity_id=$commodity->id;
  144. $inventoryMission->inventory_id=$inventoryId;
  145. $inventoryMission->location=$wmsInventory->库位;
  146. $inventoryMission->produced_at=$wmsInventory->生产日期;
  147. $inventoryMission->valid_at=$wmsInventory->失效日期;
  148. $inventoryMission->stored_at=$wmsInventory->入库日期;
  149. $inventoryMission->batch_number=$wmsInventory->批号;
  150. $inventoryMission->erp_type_position=$wmsInventory->属性仓;
  151. $inventoryMission->quality=$wmsInventory->质量状态;
  152. $inventoryMission->stored_amount=$wmsInventory->在库数量;
  153. $inventoryMission->occupied_amount=$wmsInventory->占用数量;
  154. $inventoryMission->save();
  155. }
  156. }
  157. //盘点库存
  158. public function stockInventory($location,$barcode,$count,$inventoryId){
  159. $inventoryMission=InventoryMission::with(['commodity'=>function($query)use($barcode){
  160. return $query->with(['barcodes'=>function($sql)use($barcode){
  161. return $sql->where('code',$barcode);
  162. }]);
  163. }])->where('location',$location)->where('inventory_id',$inventoryId)->first();
  164. if (!$inventoryMission) return null;
  165. $inventory=Inventory::find($inventoryId);
  166. if ($inventory->surplus!=0){
  167. $inventoryMission->verified_amount=$count;
  168. $inventoryMission->difference_amount=abs($inventoryMission->stored_amount-$count);
  169. $inventoryMission->checked='是';
  170. }else{
  171. $inventoryMission->re_checked_amount=$count;
  172. $inventoryMission->difference_amount=abs($inventoryMission->stored_amount-$count);
  173. if ($inventoryMission->difference_amount==0){
  174. $inventoryMission->returned='是';
  175. }else{
  176. $inventoryMission->returned='否';
  177. }
  178. }
  179. $inventoryMission->update();
  180. $request=[
  181. 'location'=>$location,
  182. 'barcode'=>$barcode,
  183. 'count'=>$count,
  184. 'inventoryId'=>$inventoryId,
  185. ];
  186. Controller::logS(__METHOD__,"盘点__".__FUNCTION__,json_encode($request));
  187. return $inventoryMission;
  188. }
  189. //盘点修改盘点任务数据
  190. public function updateInventory($inventoryId){
  191. $inventory=Inventory::find($inventoryId);
  192. $inventory->processed=$inventory->getProcessedAmount();
  193. $inventory->difference=$inventory->getDifferenceAmount();
  194. $inventory->returned=$inventory->getReturnedAmount();
  195. $inventory->update();
  196. Controller::logS(__METHOD__,"盘点修改盘点任务中的已盘条数__".__FUNCTION__,json_encode($inventoryId));
  197. return $inventory;
  198. }
  199. //根据该库存和产品条码查询该条盘点记录
  200. public function searchStockInventoryRecord($location,$barcode,$inventoryId){
  201. $inventoryMission=InventoryMission::with(['commodity'=>function($query)use($barcode){
  202. return $query->with(['barcodes'=>function($sql)use($barcode){
  203. return $sql->where('code',$barcode);
  204. }]);
  205. }])->where('location',$location)->where('inventory_id',$inventoryId)->first();
  206. if (!$inventoryMission) return null;
  207. return $inventoryMission;
  208. }
  209. }