InventoryAccountService.php 13 KB

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