InventoryService.php 10 KB

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