InventoryController.php 12 KB

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