InventoryController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\Imports\InventoryCompareImport;
  5. use App\Imports\OrderIssueImport;
  6. use App\Inventory;
  7. use App\InventoryCompare;
  8. use App\InventoryDailyLoggingOwner;
  9. use App\InventoryMission;
  10. use App\OracleBasCustomer;
  11. use App\Owner;
  12. use App\Services\InventoryCompareService;
  13. use App\Services\InventoryService;
  14. use App\Services\OwnerService;
  15. use App\Services\InventoryAccountService;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Cache;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Gate;
  21. use Maatwebsite\Excel\Facades\Excel;
  22. class InventoryController extends Controller
  23. {
  24. public function __construct()
  25. {
  26. app()->singleton('inventoryService',InventoryService::class);
  27. app()->singleton('inventoryCompareService',InventoryCompareService::class);
  28. }
  29. private function conditionQuery(Request $request,$page=null,$paginate=null){
  30. $date_start=$request->input('date_start');
  31. $range = $request->input('range');
  32. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  33. $date_end=$request->input('date_end');
  34. $TOLocation=$request->input('TOLocation');
  35. $LotAtt05=$request->input('LotAtt05');
  36. $LotAtt02_start=$request->input('LotAtt02_start');
  37. $customerid=$request->input('customerid');
  38. $SKU=$request->input('SKU');
  39. $ALTERNATE_SKU1=$request->input('ALTERNATE_SKU1');
  40. $LotAtt02_end=$request->input('LotAtt02_end');
  41. $sql='select * from (select result.*,rownum rn from (';
  42. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  43. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  44. $sql.=' lot.LotAtt04 批号 ';
  45. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  46. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  47. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  48. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  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 TOLocation like '".$TOLocation."' ";
  52. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  53. if ($customerid){
  54. $sql .= ' and TOCustomerID in (';
  55. $arr = explode(',',$customerid);
  56. foreach ($arr as $index => $data){
  57. if ($index != 0)$sql .= ',';
  58. $sql .= "'".$data."'";
  59. }
  60. $sql .= ') ';
  61. }
  62. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  63. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  64. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  65. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  66. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  67. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  68. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  69. if ($customerid){
  70. $sql .= ' and FMCUSTOMERID in (';
  71. $arr = explode(',',$customerid);
  72. foreach ($arr as $index => $data){
  73. if ($index != 0)$sql .= ',';
  74. $sql .= "'".$data."'";
  75. }
  76. $sql .= ') ';
  77. }
  78. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  79. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  80. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  81. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  82. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  83. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  84. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  85. if ($customerid){
  86. $sql .= ' and FMCUSTOMERID in (';
  87. $arr = explode(',',$customerid);
  88. foreach ($arr as $index => $data){
  89. if ($index != 0)$sql .= ',';
  90. $sql .= "'".$data."'";
  91. }
  92. $sql .= ') ';
  93. }
  94. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  95. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  96. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  97. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  98. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  99. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  100. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  101. if ($customerid){
  102. $sql .= ' and TOCustomerID in (';
  103. $arr = explode(',',$customerid);
  104. foreach ($arr as $index => $data){
  105. if ($index != 0)$sql .= ',';
  106. $sql .= "'".$data."'";
  107. }
  108. $sql .= ') ';
  109. }
  110. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  111. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  112. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  113. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  114. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  115. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  116. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  117. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  118. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  119. $sql.=' )result where 1=1 ';
  120. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  121. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  122. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  123. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  124. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  125. $sql.=' ) ';
  126. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  127. return DB::connection('oracle')->select($sql);
  128. }
  129. private function conditionQueryAllInventory(Request $request,$page=null,$paginate=null){
  130. $date_start=$request->date_start;
  131. $range = $request->range;
  132. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  133. $date_end=$request->date_end;
  134. $TOLocation=$request->TOLocation;
  135. $LotAtt05=$request->LotAtt05;
  136. $LotAtt02_start=$request->LotAtt02_start;
  137. $descr_c=$request->descr_c;
  138. $SKU=$request->SKU;
  139. $ALTERNATE_SKU1=$request->ALTERNATE_SKU1;
  140. $LotAtt02_end=$request->LotAtt02_end;
  141. $sql='select * from (select result.*,rownum rn from (';
  142. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  143. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  144. $sql.=' lot.LotAtt04 批号 ';
  145. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  146. $sql.=' INV_LOT_LOC_ID storeStatus';
  147. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  148. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  149. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  150. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  151. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  152. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  153. $sql.=' )result where 1=1 ';
  154. if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
  155. if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  156. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  157. if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  158. if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  159. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  160. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  161. if ($descr_c){
  162. $sql .= ' and 货主 in (';
  163. $descr_cs = explode(',',$descr_c);
  164. foreach ($descr_cs as $index => $descr_c){
  165. if ($index != 0)$sql .= ',';
  166. $sql .= "'".$descr_c."'";
  167. }
  168. $sql .= ') ';
  169. }
  170. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  171. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  172. $sql.=' ) ';
  173. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  174. return DB::connection('oracle')->select($sql);
  175. }
  176. //动库报表
  177. public function changeInventory(Request $request){
  178. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  179. $page=$request->input('page')??1;
  180. $paginate=$request->input('paginate')??50;
  181. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  182. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  183. $owners=OracleBasCustomer::select('descr_c','customerid')->where('customer_type','OW')->where('active_flag','Y')->get();
  184. $isTotalStock=false;
  185. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  186. }
  187. //全部库存
  188. public function allInventory(Request $request){
  189. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  190. $page=$request->page??1;
  191. $paginate=$request->input('paginate')??50;
  192. $oracleActTransactingLogs=$this->conditionQueryAllInventory($request,$page,$paginate);
  193. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  194. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  195. $isTotalStock=true;
  196. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  197. }
  198. public function exportData(Request $request){
  199. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  200. if (!$request->checkAllSign){
  201. $oracleActTransactingLogs=json_decode($request->input('data'),true);
  202. return $this->export($oracleActTransactingLogs);
  203. }
  204. $oracleActTransactingLogs=$this->conditionQuery($request);
  205. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  206. $oracleActTransactingLogs=json_decode($oracleActTransactingLogs,true);
  207. return $this->export($oracleActTransactingLogs);
  208. }
  209. public function export($oracleActTransactingLogs){
  210. $row=[[
  211. 'customer_descr_c'=>'货主',
  212. 'TOLocation'=>'库位',
  213. 'LotAtt05'=>'属性仓',
  214. 'LotAtt02'=>'失效日期',
  215. 'SKU'=>'产品编码',
  216. 'ALTERNATE_SKU1'=>'产品条码',
  217. 'sku_Descr_C'=>'商品名称',
  218. 'LotAtt08'=>'质量状态',
  219. 'LotAtt04'=>'批号',
  220. 'come_sum'=>'移出数量',
  221. 'join_sum'=>'移入数量',
  222. 'QTY'=>'在库数量',
  223. 'QtyAllocated'=>'占用数量',
  224. ]];
  225. $list=[];
  226. for ($i=0; $i<count($oracleActTransactingLogs);$i++){
  227. $w=[
  228. 'customer_descr_c'=>$oracleActTransactingLogs[$i]['货主'],
  229. 'TOLocation'=>$oracleActTransactingLogs[$i]['库位'],
  230. 'LotAtt05'=>$oracleActTransactingLogs[$i]['属性仓'],
  231. 'LotAtt02'=>$oracleActTransactingLogs[$i]['失效日期'],
  232. 'SKU'=>$oracleActTransactingLogs[$i]['产品编码'],
  233. 'ALTERNATE_SKU1'=>$oracleActTransactingLogs[$i]['产品条码'],
  234. 'sku_Descr_C'=>$oracleActTransactingLogs[$i]['商品名称'],
  235. 'LotAtt08'=>$oracleActTransactingLogs[$i]['质量状态'],
  236. 'LotAtt04'=>$oracleActTransactingLogs[$i]['批号'],
  237. 'come_sum'=>isset($oracleActTransactingLogs[$i]['移出数量'])?$oracleActTransactingLogs[$i]['移出数量']:'',
  238. 'join_sum'=>isset($oracleActTransactingLogs[$i]['移入数量'])?$oracleActTransactingLogs[$i]['移入数量']:'',
  239. 'QTY'=>$oracleActTransactingLogs[$i]['在库数量'],
  240. 'QtyAllocated'=>$oracleActTransactingLogs[$i]['占用数量'],
  241. ];
  242. $list[$i]=$w;
  243. }
  244. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-动库报表单.xlsx');
  245. }
  246. /*
  247. * 库存体积
  248. */
  249. function dailyLog(Request $request,OwnerService $ownerService){
  250. if (!Gate::allows('库存管理-库存体积')){return redirect(url('/')); }
  251. $inventoryDailyLogs = app('inventoryService')->getInventoryDailyLog($request->input());
  252. $owners = $ownerService->getSelection();
  253. $param = $request->input();
  254. return view('inventory.statement.dailyLog',compact('inventoryDailyLogs','owners','param'));
  255. }
  256. //获取记录监听货主
  257. function getLoggingOwner(){
  258. $loggingOwners = app('inventoryService')->getInventoryDailyLoggingOwner('owner_id');
  259. return array_column($loggingOwners->toArray(),'owner_id');
  260. }
  261. //添加记录监听货主
  262. function addLoggingOwner(Request $request){
  263. if (!Gate::allows('库存管理-库存体积-管理监听货主')){return redirect(url('/')); }
  264. $owner_id = $request->owner_id;
  265. if (!$owner_id || !is_numeric($owner_id))return ['success'=>false,'data'=>'传递参数错误!'];
  266. $loggingOwner = app('inventoryService')->firstOrCreate(['owner_id'=>$owner_id]);
  267. if (!$loggingOwner)return ['success'=>false,'data'=>'录入失败!'];
  268. return ['success'=>true,'data'=>$loggingOwner->owner_id];
  269. }
  270. function inventoryCompare(Request $request,OwnerService $ownerService){
  271. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  272. $owners = $ownerService->getSelection();
  273. $inventoryCompares=app('inventoryCompareService')->getInventoryCompare($request->all());
  274. $param = $request->input();
  275. return view('inventory.statement.inventoryCompare',compact('owners','inventoryCompares','param'));
  276. }
  277. function importExcel(Request $request){
  278. if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
  279. $owner_id=$request->owner_id;
  280. if(!$owner_id) return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">您还未选择相应货主!</p></h1>';
  281. $fileSuffix = $request->file()['file']->getClientOriginalExtension();
  282. if (in_array($fileSuffix, ['xlsx', 'xlsm', 'xltx', 'xltm', 'xls', 'xlt', 'ods', 'ots', 'slk', 'xml', 'gnumeric', 'htm', 'html', 'csv', 'tsv','txt'])) {
  283. ini_set('max_execution_time', 2100);
  284. ini_set('memory_limit', '512M');
  285. $extension = $request->file()['file']->getClientOriginalExtension();
  286. $extension[0] = strtoupper($extension[0]);
  287. Excel::import(new InventoryCompareImport($owner_id), $request->file('file')->path(), null, $extension);
  288. if (Cache::has('error')) {
  289. return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">' . Cache::pull('error') . '</p></h1>';
  290. } else {
  291. $exception = Cache::get('exception');
  292. $a = '';
  293. for ($i = 0; $i < count($exception); $i++) {
  294. $a .= implode(',', $exception[$i]) . '&#10';
  295. };
  296. $this->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  297. return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">' . $a . '</textarea></h1>';
  298. }
  299. } else {
  300. return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
  301. }
  302. }
  303. }