InventoryService.php 9.0 KB

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