InventoryAccountService.php 14 KB

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