InventoryService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Services;
  3. use App\inventoryDailyLog;
  4. use App\InventoryDailyLoggingOwner;
  5. use App\Owner;
  6. use App\Services\common\QueryService;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Traits\ServiceAppAop;
  9. class InventoryService
  10. {
  11. use ServiceAppAop;
  12. protected $modelClass=Inventory::class;
  13. public function getSql(array $params,$page=null,$paginate=null){
  14. $ownerCodes=Owner::filterAuthorities()->select('code')->get();
  15. $date_start=$params['date_start'] ?? null;
  16. $range = $params['range'] ?? null;
  17. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  18. $date_end=$params['date_end'] ?? null;
  19. $TOLocation=$params['TOLocation'] ?? null;
  20. $LotAtt05=$params['LotAtt05'] ?? null;
  21. $LotAtt02_start=$params['LotAtt02_start'] ?? null;
  22. $customerid=$params['customerid'] ?? null;
  23. $SKU=$params['SKU'] ?? null;
  24. $ALTERNATE_SKU1=$params['ALTERNATE_SKU1'] ?? null;
  25. $LotAtt02_end=$params['LotAtt02_end'] ?? null;
  26. $sql='select * from (select result.*,rownum rn from (';
  27. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  28. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  29. $sql.=' lot.LotAtt04 批号 ';
  30. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  31. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  32. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  33. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  34. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  35. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  36. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  37. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  38. if ($customerid){
  39. $sql .= ' and TOCustomerID in (';
  40. $arr = explode(',',$customerid);
  41. foreach ($arr as $index => $data){
  42. if ($index != 0)$sql .= ',';
  43. $sql .= "'".$data."'";
  44. }
  45. $sql .= ') ';
  46. }
  47. if (!$customerid&&$ownerCodes){
  48. $sql .= ' and TOCustomerID in (';
  49. foreach ($ownerCodes as $index => $data){
  50. if ($index != 0)$sql .= ',';
  51. $sql .= "'".$data['code']."'";
  52. }
  53. $sql .= ') ';
  54. }
  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. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  61. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  62. if ($customerid){
  63. $sql .= ' and FMCUSTOMERID in (';
  64. $arr = explode(',',$customerid);
  65. foreach ($arr as $index => $data){
  66. if ($index != 0)$sql .= ',';
  67. $sql .= "'".$data."'";
  68. }
  69. $sql .= ') ';
  70. }
  71. if (!$customerid&&$ownerCodes){
  72. $sql .= ' and FMCUSTOMERID in (';
  73. foreach ($ownerCodes as $index => $data){
  74. if ($index != 0)$sql .= ',';
  75. $sql .= "'".$data['code']."'";
  76. }
  77. $sql .= ') ';
  78. }
  79. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  80. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  81. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  82. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  83. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  84. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  85. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  86. if ($customerid){
  87. $sql .= ' and FMCUSTOMERID in (';
  88. $arr = explode(',',$customerid);
  89. foreach ($arr as $index => $data){
  90. if ($index != 0)$sql .= ',';
  91. $sql .= "'".$data."'";
  92. }
  93. $sql .= ') ';
  94. }
  95. if (!$customerid&&$ownerCodes){
  96. $sql .= ' and FMCUSTOMERID in (';
  97. foreach ($ownerCodes as $index => $data){
  98. if ($index != 0)$sql .= ',';
  99. $sql .= "'".$data['code']."'";
  100. }
  101. $sql .= ') ';
  102. }
  103. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  104. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  105. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  106. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  107. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  108. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  109. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  110. if ($customerid){
  111. $sql .= ' and TOCustomerID in (';
  112. $arr = explode(',',$customerid);
  113. foreach ($arr as $index => $data){
  114. if ($index != 0)$sql .= ',';
  115. $sql .= "'".$data."'";
  116. }
  117. $sql .= ') ';
  118. }
  119. if (!$customerid&&$ownerCodes){
  120. $sql .= ' and TOCustomerID in (';
  121. foreach ($ownerCodes as $index => $data){
  122. if ($index != 0)$sql .= ',';
  123. $sql .= "'".$data['code']."'";
  124. }
  125. $sql .= ') ';
  126. }
  127. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  128. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  129. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  130. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  131. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  132. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  133. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  134. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  135. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  136. $sql.=' )result where 1=1 ';
  137. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  138. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  139. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  140. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  141. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  142. $sql.=' ) ';
  143. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  144. return $sql;
  145. }
  146. public function paginate(array $params)
  147. {
  148. return DB::connection('oracle')->select(DB::raw($this->getSql($params, $params['page'] ?? 1, $params['paginate'] ?? 50)));
  149. }
  150. //库存体积条件
  151. private function conditionQueryDailyLog(array $param){
  152. $inventoryDailyLogs = InventoryDailyLog::query()->with(['owner','commodity'=>function($query){
  153. $query->with('barcodes');
  154. }])->orderByDesc('id');
  155. $columnQueryRules=[
  156. 'owner_id' => ['multi' => ','],
  157. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  158. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  159. ];
  160. $inventoryDailyLogs = app(QueryService::class)->query($param,$inventoryDailyLogs,$columnQueryRules);
  161. return $inventoryDailyLogs;
  162. }
  163. //库存体积
  164. public function getInventoryDailyLog(array $param){
  165. return $this->conditionQueryDailyLog($param)->paginate($param['paginate'] ?? 50);
  166. }
  167. //获取开启监听记录货主
  168. public function getInventoryDailyLoggingOwner($column = ['id','owner_id'], $status = "启用"){
  169. if (!is_array($column)) {
  170. $column = [$column];
  171. }
  172. return InventoryDailyLoggingOwner::query()->select($column)->where('status',$status)->get();
  173. }
  174. //录入监听记录货主
  175. public function firstOrCreate($param,$column = null){
  176. if ($column)return InventoryDailyLoggingOwner::query()->firstOrCreate($param,$column);
  177. return InventoryDailyLoggingOwner::query()->firstOrCreate($param);
  178. }
  179. }