InventoryController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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',InventoryAccountService::class);
  21. }
  22. //动库表表
  23. private function conditionQuery(Request $request,$page=null,$paginate=null){
  24. $date_start=$request->input('date_start');
  25. $range = $request->input('range');
  26. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  27. $date_end=$request->input('date_end');
  28. $TOLocation=$request->input('TOLocation');
  29. $LotAtt05=$request->input('LotAtt05');
  30. $LotAtt02_start=$request->input('LotAtt02_start');
  31. $descr_c=$request->input('descr_c');
  32. $SKU=$request->input('SKU');
  33. $ALTERNATE_SKU1=$request->input('ALTERNATE_SKU1');
  34. $LotAtt02_end=$request->input('LotAtt02_end');
  35. $sql='select * from (select result.*,rownum rn from (';
  36. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  37. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  38. $sql.=' lot.LotAtt04 批号 ';
  39. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  40. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  41. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  42. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  43. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  44. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  45. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  46. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  47. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  48. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  49. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  50. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  51. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  52. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  53. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  54. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  55. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  56. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  57. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  58. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  59. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  60. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  61. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  62. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  63. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  64. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  65. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  66. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  67. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  68. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  69. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  70. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  71. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  72. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  73. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  74. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  75. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  76. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  77. $sql.=' )result where 1=1 ';
  78. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  79. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  80. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  81. if ($descr_c){
  82. $sql .= ' and 货主 in (';
  83. $descr_cs = explode(',',$descr_c);
  84. foreach ($descr_cs as $index => $descr_c){
  85. if ($index != 0)$sql .= ',';
  86. $sql .= "'".$descr_c."'";
  87. }
  88. $sql .= ') ';
  89. }
  90. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  91. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  92. $sql.=' ) ';
  93. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  94. return DB::connection('oracle')->select($sql);
  95. }
  96. //全部库存
  97. private function conditionQueryAllInventory(Request $request,$page=null,$paginate=null){
  98. $date_start=$request->date_start;
  99. $range = $request->range;
  100. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  101. $date_end=$request->date_end;
  102. $TOLocation=$request->TOLocation;
  103. $LotAtt05=$request->LotAtt05;
  104. $LotAtt02_start=$request->LotAtt02_start;
  105. $descr_c=$request->descr_c;
  106. $SKU=$request->SKU;
  107. $ALTERNATE_SKU1=$request->ALTERNATE_SKU1;
  108. $LotAtt02_end=$request->LotAtt02_end;
  109. $sql='select * from (select result.*,rownum rn from (';
  110. $sql.=' select customer.Descr_C as 货主,storeStatus.CUSTOMERID 客户,storeStatus.LocationID 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  111. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, storeStatus.ADDTIME 创建时间, ';
  112. $sql.=' lot.LotAtt04 批号 ';
  113. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  114. $sql.=' INV_LOT_LOC_ID storeStatus';
  115. $sql.=' left join BAS_Customer customer on customer.CustomerID=storeStatus.CUSTOMERID ';
  116. $sql.=' left join BAS_SKU sku on sku.SKU=storeStatus.SKU and sku.CUSTOMERID=storeStatus.CUSTOMERID ';
  117. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM = storeStatus.LOTNUM AND lot.CUSTOMERID = storeStatus.CUSTOMERID ';
  118. $sql.=' group by storeStatus.LocationID,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  119. $sql.=' ,sku.Descr_C,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  120. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,storeStatus.CUSTOMERID,storeStatus.ADDTIME ';
  121. $sql.=' )result where 1=1 ';
  122. if ($TOLocation)$sql .= " and 库位 like '".$TOLocation."' ";
  123. if ($SKU)$sql.=" and 产品编码 like '".$SKU."' ";
  124. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  125. if ($date_start)$sql.=" and 创建时间 > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  126. if ($date_end)$sql.=" and 创建时间 < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  127. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  128. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  129. if ($descr_c){
  130. $sql .= ' and 货主 in (';
  131. $descr_cs = explode(',',$descr_c);
  132. foreach ($descr_cs as $index => $descr_c){
  133. if ($index != 0)$sql .= ',';
  134. $sql .= "'".$descr_c."'";
  135. }
  136. $sql .= ') ';
  137. }
  138. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  139. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  140. $sql.=' ) ';
  141. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  142. return DB::connection('oracle')->select($sql);
  143. }
  144. //动库报表
  145. public function changeInventory(Request $request){
  146. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  147. $page=$request->input('page')??1;
  148. $paginate=$request->input('paginate')??50;
  149. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  150. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  151. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  152. $isTotalStock=false;
  153. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  154. }
  155. //全部库存
  156. public function allInventory(Request $request){
  157. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  158. $page=$request->page??1;
  159. $paginate=$request->input('paginate')??50;
  160. $oracleActTransactingLogs=$this->conditionQueryAllInventory($request,$page,$paginate);
  161. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  162. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  163. $isTotalStock=true;
  164. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners','isTotalStock'));
  165. }
  166. public function exportData(Request $request){
  167. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  168. if (!$request->checkAllSign){
  169. $oracleActTransactingLogs=json_decode($request->input('data'),true);
  170. return $this->export($oracleActTransactingLogs);
  171. }
  172. $oracleActTransactingLogs=$this->conditionQuery($request);
  173. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  174. $oracleActTransactingLogs=json_decode($oracleActTransactingLogs,true);
  175. return $this->export($oracleActTransactingLogs);
  176. }
  177. public function export($oracleActTransactingLogs){
  178. $row=[[
  179. 'customer_descr_c'=>'货主',
  180. 'TOLocation'=>'库位',
  181. 'LotAtt05'=>'属性仓',
  182. 'LotAtt02'=>'失效日期',
  183. 'SKU'=>'产品编码',
  184. 'ALTERNATE_SKU1'=>'产品条码',
  185. 'sku_Descr_C'=>'商品名称',
  186. 'LotAtt08'=>'质量状态',
  187. 'LotAtt04'=>'批号',
  188. 'come_sum'=>'移出数量',
  189. 'join_sum'=>'移入数量',
  190. 'QTY'=>'在库数量',
  191. 'QtyAllocated'=>'占用数量',
  192. ]];
  193. $list=[];
  194. for ($i=0; $i<count($oracleActTransactingLogs);$i++){
  195. $w=[
  196. 'customer_descr_c'=>$oracleActTransactingLogs[$i]['货主'],
  197. 'TOLocation'=>$oracleActTransactingLogs[$i]['库位'],
  198. 'LotAtt05'=>$oracleActTransactingLogs[$i]['属性仓'],
  199. 'LotAtt02'=>$oracleActTransactingLogs[$i]['失效日期'],
  200. 'SKU'=>$oracleActTransactingLogs[$i]['产品编码'],
  201. 'ALTERNATE_SKU1'=>$oracleActTransactingLogs[$i]['产品条码'],
  202. 'sku_Descr_C'=>$oracleActTransactingLogs[$i]['商品名称'],
  203. 'LotAtt08'=>$oracleActTransactingLogs[$i]['质量状态'],
  204. 'LotAtt04'=>$oracleActTransactingLogs[$i]['批号'],
  205. 'come_sum'=>isset($oracleActTransactingLogs[$i]['移出数量'])?$oracleActTransactingLogs[$i]['移出数量']:'',
  206. 'join_sum'=>isset($oracleActTransactingLogs[$i]['移入数量'])?$oracleActTransactingLogs[$i]['移入数量']:'',
  207. 'QTY'=>$oracleActTransactingLogs[$i]['在库数量'],
  208. 'QtyAllocated'=>$oracleActTransactingLogs[$i]['占用数量'],
  209. ];
  210. $list[$i]=$w;
  211. }
  212. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-动库报表单.xlsx');
  213. }
  214. //创建盘点任务
  215. public function createStockInventoryMission(Request $request){
  216. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  217. $inventory=app('inventoryService')->createMission($request);
  218. if (is_null($inventory)) return ['success'=>false,'data'=>'参数错误!'];
  219. return ['success'=>true,'data'=>$inventory];
  220. }
  221. //盘点-任务页面
  222. public function mission(Request $request){
  223. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  224. $paginateParams = $request->input();
  225. $inventories=app('inventoryService')->paginate($request);
  226. $owners=Owner::select('id','name')->get();
  227. return view('inventory.stockInventory.mission',compact('owners','inventories','paginateParams'));
  228. }
  229. //进入盘点页面
  230. public function enterStockInventory($id){
  231. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  232. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  233. $inventory=Inventory::with('owner')->find($id);
  234. $inventoryMissions=InventoryMission::with(['commodity'])->where('inventory_id',$id)->get();
  235. return view('inventory.stockInventory.inventoryMission',compact('inventory','inventoryMissions'));
  236. }
  237. //依据盘点任务id进行 --盘点
  238. public function stockInventory(Request $request){
  239. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  240. $count=$request->input('count');
  241. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  242. $inventoryMission=app('inventoryService')->stockInventory($request);
  243. if (!$inventoryMission)return ['success'=>false,'data'=>'参数错误!'];
  244. $inventory=app('inventoryService')->updateInventoryMissionProcessed($request);
  245. return ['success'=>true,'inventoryMission'=>$inventoryMission,'inventory'=>$inventory];
  246. }
  247. //盘点任务导出
  248. public function stockInventoryExport(Request $request){
  249. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  250. ini_set('max_execution_time',3500);
  251. ini_set('memory_limit','3526M');
  252. if ($request->checkAllSign){
  253. $request->offsetUnset('checkAllSign');
  254. $inventories=app('inventoryService')->get($request);
  255. }else{
  256. $inventories=app('inventoryService')->some($request);
  257. }
  258. $row=[[
  259. 'id'=>'盘点编号',
  260. 'created_at'=>'创建时间',
  261. 'owner_id'=>'货主',
  262. 'type'=>'任务类型',
  263. 'start_at'=>'起始时间',
  264. 'end_at'=>'结束时间',
  265. 'total'=>'记录数',
  266. 'processed'=>'已盘数',
  267. 'surplus'=>'剩余数',
  268. 'difference'=>'复盘差异',
  269. 'returned'=>'复盘归位',
  270. ]];
  271. $list=[];
  272. for ($i=0; $i<count($inventories);$i++){
  273. $inventory=$inventories[$i];
  274. $w=[
  275. 'id'=>isset($inventory->id)?$inventory->id:'',
  276. 'created_at'=>isset($inventory->created_at)?$inventory->created_at:'',
  277. 'owner_id'=>isset($inventory->owner->name)?$inventory->owner->name:'',
  278. 'type'=>isset($inventory->type)?$inventory->type:'',
  279. 'start_at'=>isset($inventory->start_at)?$inventory->start_at:'',
  280. 'end_at'=>isset($inventory->end_at)?$inventory->end_at:'',
  281. 'total'=>isset($inventory->total)?$inventory->total:'',
  282. 'processed'=>isset($inventory->processed)?$inventory->processed:'',
  283. 'surplus'=>isset($inventory->surplus)?$inventory->surplus:'',
  284. 'difference'=>isset($inventory->difference)?$inventory->difference:'',
  285. 'returned'=>isset($inventory->returned)?$inventory->returned:'',
  286. ];
  287. $list[$i]=$w;
  288. }
  289. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  290. }
  291. /*
  292. * 库存体积
  293. */
  294. function dailyLog(Request $request,OwnerService $ownerService){
  295. if (!Gate::allows('库存管理-库存体积')){return redirect(url('/')); }
  296. $inventoryDailyLogs = app('inventoryService')->getInventoryDailyLog($request->input());
  297. $owners = $ownerService->getSelection();
  298. $param = $request->input();
  299. return view('inventory.statement.dailyLog',compact('inventoryDailyLogs','owners','param'));
  300. }
  301. //获取记录监听货主
  302. function getLoggingOwner(){
  303. $loggingOwners = app('inventoryService')->getInventoryDailyLoggingOwner('owner_id');
  304. return array_column($loggingOwners->toArray(),'owner_id');
  305. }
  306. //添加记录监听货主
  307. function addLoggingOwner(Request $request){
  308. if (!Gate::allows('库存管理-库存体积-管理监听货主')){return redirect(url('/')); }
  309. $owner_id = $request->owner_id;
  310. if (!$owner_id || !is_numeric($owner_id))return ['success'=>false,'data'=>'传递参数错误!'];
  311. $loggingOwner = app('inventoryService')->firstOrCreate(['owner_id'=>$owner_id]);
  312. if (!$loggingOwner)return ['success'=>false,'data'=>'录入失败!'];
  313. return ['success'=>true,'data'=>$loggingOwner->owner_id];
  314. }
  315. }