InventoryAccountService.php 13 KB

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