InventoryService.php 9.3 KB

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