AllInventoryService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Services;
  3. use App\Owner;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Traits\ServiceAppAop;
  6. class AllInventoryService
  7. {
  8. use ServiceAppAop;
  9. protected $modelClass=AllInventory::class;
  10. public function getSql(array $params, $page=null, $paginate=null){
  11. $ownerCodes=Owner::filterAuthorities()->select('code')->get();
  12. $date_start=$params['date_start'] ?? null;
  13. $range = $params['range'] ?? null;
  14. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  15. $date_end=$params['date_end'] ?? null;
  16. $TOLocation=$params['TOLocation'] ?? null;
  17. $LotAtt05=$params['LotAtt05'] ?? null;
  18. $LotAtt02_start=$params['LotAtt02_start'] ?? null;
  19. $customerid=$params['customerid'] ?? null;
  20. $SKU=$params['SKU'] ?? null;
  21. $ALTERNATE_SKU1=$params['ALTERNATE_SKU1'] ?? null;
  22. $LotAtt02_end=$params['LotAtt02_end'] ?? null;
  23. $sql='select * from (select result.*,rownum rn from (';
  24. $sql.=' select customer.descr_c as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  25. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  26. $sql.=' lot.LotAtt04 批号 ';
  27. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  28. $sql.=' INV_LOT_LOC_ID storeStatus';
  29. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  30. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  31. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  32. if ($customerid){
  33. $sql .= ' where storeStatus.CUSTOMERID in (';
  34. $arr = explode(',',$customerid);
  35. foreach ($arr as $index => $data){
  36. if ($index != 0)$sql .= ',';
  37. $sql .= "'".$data."'";
  38. }
  39. $sql .= ') ';
  40. }
  41. if (!$customerid&&$ownerCodes){
  42. $sql .= ' where storeStatus.CUSTOMERID in (';
  43. foreach ($ownerCodes as $index => $data){
  44. if ($index != 0)$sql .= ',';
  45. $sql .= "'".$data['code']."'";
  46. }
  47. $sql .= ') ';
  48. }
  49. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  50. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  51. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  52. $sql.=' )result where 1=1 ';
  53. // if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
  54. // if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  55. // if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  56. if ($TOLocation)$sql=$this->searchWay($sql,$TOLocation,"库位");
  57. if ($SKU)$sql=$this->searchWay($sql,$SKU,"产品编码");
  58. if ($LotAtt05)$sql=$this->searchWay($sql,$LotAtt05,"属性仓");
  59. if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  60. if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  61. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  62. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  63. // if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  64. if ($ALTERNATE_SKU1)$sql=$this->searchWay($sql,$ALTERNATE_SKU1,"产品条码");
  65. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  66. $sql.=' ) ';
  67. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  68. return $sql;
  69. }
  70. public function paginate(array $params){
  71. return DB::connection('oracle')->select(DB::raw($this->getSql($params,$params['page'] ?? 1, $params['paginate'] ?? 50)));
  72. }
  73. private function searchWay($sql,$condition,$column)
  74. {
  75. if ($condition){
  76. if (strpos($condition, ',') || strpos($condition, ',') || strpos($condition, ' ')) {
  77. $arr = array_filter(preg_split('/[,, ]+/is', $condition));
  78. $sql .= ' and '.$column.' in (';
  79. foreach ($arr as $index => $arr){
  80. if ($index != 0)$sql .= ',';
  81. $sql .= "'".$arr."'";
  82. }
  83. $sql .= ') ';
  84. unset($condition);
  85. } else {
  86. $sql .= " and $column like '".$condition."' ";
  87. }
  88. }
  89. return $sql;
  90. }
  91. }