InventoryController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\OracleBasCustomer;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Gate;
  8. use Maatwebsite\Excel\Facades\Excel;
  9. class InventoryController extends Controller
  10. {
  11. private function conditionQuery(Request $request,$page=null,$paginate=null){
  12. $date_start=$request->input('date_start');
  13. $range = $request->input('range');
  14. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  15. $date_end=$request->input('date_end');
  16. $TOLocation=$request->input('TOLocation');
  17. $LotAtt05=$request->input('LotAtt05');
  18. $LotAtt02_start=$request->input('LotAtt02_start');
  19. $descr_c=$request->input('descr_c');
  20. $SKU=$request->input('SKU');
  21. $ALTERNATE_SKU1=$request->input('ALTERNATE_SKU1');
  22. $LotAtt02_end=$request->input('LotAtt02_end');
  23. $sql='select * from (select result.*,rownum rn from (';
  24. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  25. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  26. $sql.=' lot.LotAtt04 批号 ';
  27. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  28. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  29. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  30. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  31. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  32. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  33. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  34. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  35. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  36. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  37. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  38. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  39. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  40. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  41. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  42. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  43. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  44. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  45. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  46. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  47. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  48. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  49. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  50. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  51. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  52. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  53. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  54. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  55. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  56. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  57. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  58. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  59. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  60. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  61. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  62. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  63. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  64. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  65. $sql.=' )result where 1=1 ';
  66. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  67. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  68. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  69. if ($descr_c){
  70. $sql .= ' and 货主 in (';
  71. $descr_cs = explode(',',$descr_c);
  72. foreach ($descr_cs as $index => $descr_c){
  73. if ($index != 0)$sql .= ',';
  74. $sql .= "'".$descr_c."'";
  75. }
  76. $sql .= ') ';
  77. }
  78. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  79. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  80. $sql.=' ) ';
  81. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  82. return DB::connection('oracle')->select($sql);
  83. }
  84. private function conditionQueryAllInventory(Request $request,$page=null,$paginate=null){
  85. $date_start=$request->date_start;
  86. $range = $request->range;
  87. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  88. $date_end=$request->date_end;
  89. $TOLocation=$request->TOLocation;
  90. $LotAtt05=$request->LotAtt05;
  91. $LotAtt02_start=$request->LotAtt02_start;
  92. $descr_c=$request->descr_c;
  93. $SKU=$request->SKU;
  94. $ALTERNATE_SKU1=$request->ALTERNATE_SKU1;
  95. $LotAtt02_end=$request->LotAtt02_end;
  96. $sql='select * from (select result.*,rownum rn from (';
  97. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  98. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  99. $sql.=' lot.LotAtt04 批号 ';
  100. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  101. $sql.=' INV_LOT_LOC_ID storeStatus';
  102. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  103. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  104. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  105. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  106. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  107. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  108. $sql.=' )result where 1=1 ';
  109. if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
  110. if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  111. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  112. if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  113. if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  114. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  115. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  116. if ($descr_c){
  117. $sql .= ' and 货主 in (';
  118. $descr_cs = explode(',',$descr_c);
  119. foreach ($descr_cs as $index => $descr_c){
  120. if ($index != 0)$sql .= ',';
  121. $sql .= "'".$descr_c."'";
  122. }
  123. $sql .= ') ';
  124. }
  125. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  126. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  127. $sql.=' ) ';
  128. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  129. return DB::connection('oracle')->select($sql);
  130. }
  131. public function changeInventory(Request $request){
  132. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  133. $page=$request->input('page')??1;
  134. $paginate=$request->input('paginate')??50;
  135. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  136. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  137. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  138. $isTotalStock=false;
  139. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  140. }
  141. public function allInventory(Request $request){
  142. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  143. $page=$request->page??1;
  144. $paginate=$request->input('paginate')??50;
  145. $oracleActTransactingLogs=$this->conditionQueryAllInventory($request,$page,$paginate);
  146. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  147. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  148. $isTotalStock=true;
  149. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  150. }
  151. public function exportData(Request $request){
  152. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  153. if (!$request->checkAllSign){
  154. $oracleActTransactingLogs=json_decode($request->input('data'),true);
  155. return $this->export($oracleActTransactingLogs);
  156. }
  157. $oracleActTransactingLogs=$this->conditionQuery($request);
  158. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  159. $oracleActTransactingLogs=json_decode($oracleActTransactingLogs,true);
  160. return $this->export($oracleActTransactingLogs);
  161. }
  162. public function export($oracleActTransactingLogs){
  163. $row=[[
  164. 'customer_descr_c'=>'货主',
  165. 'TOLocation'=>'库位',
  166. 'LotAtt05'=>'属性仓',
  167. 'LotAtt02'=>'失效日期',
  168. 'SKU'=>'产品编码',
  169. 'ALTERNATE_SKU1'=>'产品条码',
  170. 'sku_Descr_C'=>'商品名称',
  171. 'LotAtt08'=>'质量状态',
  172. 'LotAtt04'=>'批号',
  173. 'come_sum'=>'移出数量',
  174. 'join_sum'=>'移入数量',
  175. 'QTY'=>'在库数量',
  176. 'QtyAllocated'=>'占用数量',
  177. ]];
  178. $list=[];
  179. for ($i=0; $i<count($oracleActTransactingLogs);$i++){
  180. $w=[
  181. 'customer_descr_c'=>$oracleActTransactingLogs[$i]['货主'],
  182. 'TOLocation'=>$oracleActTransactingLogs[$i]['库位'],
  183. 'LotAtt05'=>$oracleActTransactingLogs[$i]['属性仓'],
  184. 'LotAtt02'=>$oracleActTransactingLogs[$i]['失效日期'],
  185. 'SKU'=>$oracleActTransactingLogs[$i]['产品编码'],
  186. 'ALTERNATE_SKU1'=>$oracleActTransactingLogs[$i]['产品条码'],
  187. 'sku_Descr_C'=>$oracleActTransactingLogs[$i]['商品名称'],
  188. 'LotAtt08'=>$oracleActTransactingLogs[$i]['质量状态'],
  189. 'LotAtt04'=>$oracleActTransactingLogs[$i]['批号'],
  190. 'come_sum'=>isset($oracleActTransactingLogs[$i]['移出数量'])?$oracleActTransactingLogs[$i]['移出数量']:'',
  191. 'join_sum'=>isset($oracleActTransactingLogs[$i]['移入数量'])?$oracleActTransactingLogs[$i]['移入数量']:'',
  192. 'QTY'=>$oracleActTransactingLogs[$i]['在库数量'],
  193. 'QtyAllocated'=>$oracleActTransactingLogs[$i]['占用数量'],
  194. ];
  195. $list[$i]=$w;
  196. }
  197. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-动库报表单.xlsx');
  198. }
  199. }