InventoryService.php 9.9 KB

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