InventoryController.php 16 KB

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