InventoryService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace App\Services;
  3. use App\inventoryDailyLog;
  4. use App\InventoryDailyLoggingOwner;
  5. use App\Services\common\QueryService;
  6. use Illuminate\Support\Facades\DB;
  7. class InventoryService
  8. {
  9. public function getSql(array $params,$page=null,$paginate=null){
  10. $date_start=$params['date_start'] ?? null;
  11. $range = $params['range'] ?? null;
  12. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  13. $date_end=$params['date_end'] ?? null;
  14. $TOLocation=$params['TOLocation'] ?? null;
  15. $LotAtt05=$params['LotAtt05'] ?? null;
  16. $LotAtt02_start=$params['LotAtt02_start'] ?? null;
  17. $customerid=$params['customerid'] ?? null;
  18. $SKU=$params['SKU'] ?? null;
  19. $ALTERNATE_SKU1=$params['ALTERNATE_SKU1'] ?? null;
  20. $LotAtt02_end=$params['LotAtt02_end'] ?? null;
  21. $sql='select * from (select result.*,rownum rn from (';
  22. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  23. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  24. $sql.=' lot.LotAtt04 批号 ';
  25. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  26. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  27. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  28. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  29. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  30. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  31. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  32. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  33. if ($customerid){
  34. $sql .= ' and TOCustomerID in (';
  35. $arr = explode(',',$customerid);
  36. foreach ($arr as $index => $data){
  37. if ($index != 0)$sql .= ',';
  38. $sql .= "'".$data."'";
  39. }
  40. $sql .= ') ';
  41. }
  42. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  43. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  44. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  45. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  46. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  47. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  48. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  49. if ($customerid){
  50. $sql .= ' and FMCUSTOMERID in (';
  51. $arr = explode(',',$customerid);
  52. foreach ($arr as $index => $data){
  53. if ($index != 0)$sql .= ',';
  54. $sql .= "'".$data."'";
  55. }
  56. $sql .= ') ';
  57. }
  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. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  64. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  65. if ($customerid){
  66. $sql .= ' and FMCUSTOMERID in (';
  67. $arr = explode(',',$customerid);
  68. foreach ($arr as $index => $data){
  69. if ($index != 0)$sql .= ',';
  70. $sql .= "'".$data."'";
  71. }
  72. $sql .= ') ';
  73. }
  74. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  75. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  76. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  77. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  78. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  79. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  80. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  81. if ($customerid){
  82. $sql .= ' and TOCustomerID in (';
  83. $arr = explode(',',$customerid);
  84. foreach ($arr as $index => $data){
  85. if ($index != 0)$sql .= ',';
  86. $sql .= "'".$data."'";
  87. }
  88. $sql .= ') ';
  89. }
  90. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  91. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  92. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  93. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  94. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  95. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  96. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  97. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  98. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  99. $sql.=' )result where 1=1 ';
  100. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  101. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  102. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  103. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  104. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  105. $sql.=' ) ';
  106. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  107. return $sql;
  108. }
  109. public function paginate(array $params){
  110. return DB::connection('oracle')->select(DB::raw($this->getSql($params,$params['page'] ?? 1, $params['paginate'] ?? 50)));
  111. }
  112. }