InventoryController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. $customerid=$request->input('customerid');
  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. if ($customerid){
  47. $sql .= ' and TOCustomerID in (';
  48. $arr = explode(',',$customerid);
  49. foreach ($arr as $index => $data){
  50. if ($index != 0)$sql .= ',';
  51. $sql .= "'".$data."'";
  52. }
  53. $sql .= ') ';
  54. }
  55. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  56. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  57. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  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 FMLOCATION like '".$TOLocation."' ";
  61. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  62. if ($customerid){
  63. $sql .= ' and FMCUSTOMERID in (';
  64. $arr = explode(',',$customerid);
  65. foreach ($arr as $index => $data){
  66. if ($index != 0)$sql .= ',';
  67. $sql .= "'".$data."'";
  68. }
  69. $sql .= ') ';
  70. }
  71. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  72. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  73. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  74. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  75. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  76. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  77. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  78. if ($customerid){
  79. $sql .= ' and FMCUSTOMERID in (';
  80. $arr = explode(',',$customerid);
  81. foreach ($arr as $index => $data){
  82. if ($index != 0)$sql .= ',';
  83. $sql .= "'".$data."'";
  84. }
  85. $sql .= ') ';
  86. }
  87. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  88. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  89. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  90. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  91. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  92. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  93. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  94. if ($customerid){
  95. $sql .= ' and TOCustomerID in (';
  96. $arr = explode(',',$customerid);
  97. foreach ($arr as $index => $data){
  98. if ($index != 0)$sql .= ',';
  99. $sql .= "'".$data."'";
  100. }
  101. $sql .= ') ';
  102. }
  103. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  104. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  105. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  106. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  107. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  108. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  109. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  110. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  111. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  112. $sql.=' )result where 1=1 ';
  113. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  114. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  115. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  116. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  117. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  118. $sql.=' ) ';
  119. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  120. return DB::connection('oracle')->select($sql);
  121. }
  122. private function conditionQueryAllInventory(Request $request,$page=null,$paginate=null){
  123. $date_start=$request->date_start;
  124. $range = $request->range;
  125. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  126. $date_end=$request->date_end;
  127. $TOLocation=$request->TOLocation;
  128. $LotAtt05=$request->LotAtt05;
  129. $LotAtt02_start=$request->LotAtt02_start;
  130. $descr_c=$request->descr_c;
  131. $SKU=$request->SKU;
  132. $ALTERNATE_SKU1=$request->ALTERNATE_SKU1;
  133. $LotAtt02_end=$request->LotAtt02_end;
  134. $sql='select * from (select result.*,rownum rn from (';
  135. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  136. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  137. $sql.=' lot.LotAtt04 批号 ';
  138. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  139. $sql.=' INV_LOT_LOC_ID storeStatus';
  140. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  141. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  142. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  143. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  144. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  145. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  146. $sql.=' )result where 1=1 ';
  147. if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
  148. if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  149. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  150. if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  151. if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  152. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  153. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  154. if ($descr_c){
  155. $sql .= ' and 货主 in (';
  156. $descr_cs = explode(',',$descr_c);
  157. foreach ($descr_cs as $index => $descr_c){
  158. if ($index != 0)$sql .= ',';
  159. $sql .= "'".$descr_c."'";
  160. }
  161. $sql .= ') ';
  162. }
  163. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  164. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  165. $sql.=' ) ';
  166. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  167. return DB::connection('oracle')->select($sql);
  168. }
  169. //动库报表
  170. public function changeInventory(Request $request){
  171. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  172. $page=$request->input('page')??1;
  173. $paginate=$request->input('paginate')??50;
  174. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  175. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  176. $owners=OracleBasCustomer::select('descr_c','customerid')->where('customer_type','OW')->where('active_flag','Y')->get();
  177. $isTotalStock=false;
  178. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  179. }
  180. //全部库存
  181. public function allInventory(Request $request){
  182. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  183. $page=$request->page??1;
  184. $paginate=$request->input('paginate')??50;
  185. $oracleActTransactingLogs=$this->conditionQueryAllInventory($request,$page,$paginate);
  186. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  187. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  188. $isTotalStock=true;
  189. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  190. }
  191. public function exportData(Request $request){
  192. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  193. if (!$request->checkAllSign){
  194. $oracleActTransactingLogs=json_decode($request->input('data'),true);
  195. return $this->export($oracleActTransactingLogs);
  196. }
  197. $oracleActTransactingLogs=$this->conditionQuery($request);
  198. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  199. $oracleActTransactingLogs=json_decode($oracleActTransactingLogs,true);
  200. return $this->export($oracleActTransactingLogs);
  201. }
  202. public function export($oracleActTransactingLogs){
  203. $row=[[
  204. 'customer_descr_c'=>'货主',
  205. 'TOLocation'=>'库位',
  206. 'LotAtt05'=>'属性仓',
  207. 'LotAtt02'=>'失效日期',
  208. 'SKU'=>'产品编码',
  209. 'ALTERNATE_SKU1'=>'产品条码',
  210. 'sku_Descr_C'=>'商品名称',
  211. 'LotAtt08'=>'质量状态',
  212. 'LotAtt04'=>'批号',
  213. 'come_sum'=>'移出数量',
  214. 'join_sum'=>'移入数量',
  215. 'QTY'=>'在库数量',
  216. 'QtyAllocated'=>'占用数量',
  217. ]];
  218. $list=[];
  219. for ($i=0; $i<count($oracleActTransactingLogs);$i++){
  220. $w=[
  221. 'customer_descr_c'=>$oracleActTransactingLogs[$i]['货主'],
  222. 'TOLocation'=>$oracleActTransactingLogs[$i]['库位'],
  223. 'LotAtt05'=>$oracleActTransactingLogs[$i]['属性仓'],
  224. 'LotAtt02'=>$oracleActTransactingLogs[$i]['失效日期'],
  225. 'SKU'=>$oracleActTransactingLogs[$i]['产品编码'],
  226. 'ALTERNATE_SKU1'=>$oracleActTransactingLogs[$i]['产品条码'],
  227. 'sku_Descr_C'=>$oracleActTransactingLogs[$i]['商品名称'],
  228. 'LotAtt08'=>$oracleActTransactingLogs[$i]['质量状态'],
  229. 'LotAtt04'=>$oracleActTransactingLogs[$i]['批号'],
  230. 'come_sum'=>isset($oracleActTransactingLogs[$i]['移出数量'])?$oracleActTransactingLogs[$i]['移出数量']:'',
  231. 'join_sum'=>isset($oracleActTransactingLogs[$i]['移入数量'])?$oracleActTransactingLogs[$i]['移入数量']:'',
  232. 'QTY'=>$oracleActTransactingLogs[$i]['在库数量'],
  233. 'QtyAllocated'=>$oracleActTransactingLogs[$i]['占用数量'],
  234. ];
  235. $list[$i]=$w;
  236. }
  237. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-动库报表单.xlsx');
  238. }
  239. /*
  240. * 库存体积
  241. */
  242. function dailyLog(Request $request,OwnerService $ownerService){
  243. if (!Gate::allows('库存管理-库存体积')){return redirect(url('/')); }
  244. $inventoryDailyLogs = app('inventoryService')->getInventoryDailyLog($request->input());
  245. $owners = $ownerService->getSelection();
  246. $param = $request->input();
  247. return view('inventory.statement.dailyLog',compact('inventoryDailyLogs','owners','param'));
  248. }
  249. //获取记录监听货主
  250. function getLoggingOwner(){
  251. $loggingOwners = app('inventoryService')->getInventoryDailyLoggingOwner('owner_id');
  252. return array_column($loggingOwners->toArray(),'owner_id');
  253. }
  254. //添加记录监听货主
  255. function addLoggingOwner(Request $request){
  256. if (!Gate::allows('库存管理-库存体积-管理监听货主')){return redirect(url('/')); }
  257. $owner_id = $request->owner_id;
  258. if (!$owner_id || !is_numeric($owner_id))return ['success'=>false,'data'=>'传递参数错误!'];
  259. $loggingOwner = app('inventoryService')->firstOrCreate(['owner_id'=>$owner_id]);
  260. if (!$loggingOwner)return ['success'=>false,'data'=>'录入失败!'];
  261. return ['success'=>true,'data'=>$loggingOwner->owner_id];
  262. }
  263. function inventoryCompare(Request $request,OwnerService $ownerService){
  264. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  265. $owners = $ownerService->getSelection();
  266. return view('inventory.statement.inventoryCompare',compact('owners'));
  267. }
  268. }