InventoryController.php 14 KB

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