InventoryController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\Inventory;
  5. use App\InventoryMission;
  6. use App\OracleBasCustomer;
  7. use App\Owner;
  8. use App\Services\InventoryService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Gate;
  12. use Maatwebsite\Excel\Facades\Excel;
  13. class InventoryController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. app()->singleton('inventoryService',InventoryService::class);
  18. }
  19. private function conditionQuery(Request $request,$page=null,$paginate=null){
  20. $date_start=$request->input('date_start');
  21. $range = $request->input('range');
  22. if ($range)$date_start=date('Y-m-d',strtotime('-'.$range." day"));
  23. $date_end=$request->input('date_end');
  24. $TOLocation=$request->input('TOLocation');
  25. $LotAtt05=$request->input('LotAtt05');
  26. $LotAtt02_start=$request->input('LotAtt02_start');
  27. $descr_c=$request->input('descr_c');
  28. $SKU=$request->input('SKU');
  29. $ALTERNATE_SKU1=$request->input('ALTERNATE_SKU1');
  30. $LotAtt02_end=$request->input('LotAtt02_end');
  31. $sql='select * from (select result.*,rownum rn from (';
  32. $sql.=' select customer.Descr_C as 货主,stockLog.客户 客户, 库位, sku.SKU 产品编码, sku.ALTERNATE_SKU1 产品条码, ';
  33. $sql.=' sku.Descr_C 商品名称, lot.LotAtt05 属性仓, lot.LotAtt08 质量状态, lot.LotAtt02 失效日期, ';
  34. $sql.=' lot.LotAtt04 批号 ';
  35. $sql.=' , sum(移出数量)移出数量, sum(移入数量)移入数量 ';
  36. $sql.=' , storeStatus.QTY 在库数量, storeStatus.QtyAllocated 占用数量,count(1) over () as sum from ';
  37. $sql.=' (select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量, sum(TOQty_Each) as 移入数量, TOLocation as 库位 ';
  38. $sql.=" from ACT_Transaction_Log where TransactionType='PA' ";
  39. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  40. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  41. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  42. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  43. $sql.=' group by TOCustomerID, TOLocation,FMSKU,FMLotNum union all ';
  44. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量, 0 as 移入数量, FMLOCATION as 库位 ';
  45. $sql.=" from ACT_Transaction_Log where TransactionType='SO' ";
  46. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  47. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  48. if ($TOLocation)$sql .= " and FMLOCATION like '".$TOLocation."' ";
  49. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  50. $sql.=' group by FMCustomerID, FMLocation,FMSKU,FMLotNum union all ';
  51. $sql.=' select FMLotNum,FMSKU,FMCUSTOMERID 客户,sum(FMQty_Each) as 移出数量,0 as 移入数量, FMLocation as 库位 ';
  52. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  53. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  54. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  55. if ($TOLocation)$sql .= " and FMLocation like '".$TOLocation."' ";
  56. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  57. $sql.=' group by FMLocation,FMCUSTOMERID,FMSKU,FMLotNum union all ';
  58. $sql.=' select FMLotNum,FMSKU,TOCustomerID 客户,0 as 移出数量,sum(TOQty_Each)as 移入数量, TOLocation as 库位 ';
  59. $sql.=" from ACT_Transaction_Log where TransactionType='MV' ";
  60. if ($date_start) $sql.=" and addtime > to_date('".$date_start." 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
  61. if ($date_end) $sql.=" and addtime < to_date('".$date_end." 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
  62. if ($TOLocation)$sql .= " and TOLocation like '".$TOLocation."' ";
  63. if ($SKU)$sql.=" and FMSKU like '".$SKU."' ";
  64. $sql.=' group by TOLocation,TOCustomerID,FMSKU,FMLotNum)stockLog ';
  65. $sql.=' left join BAS_Customer customer on customer.CustomerID=stockLog.客户 ';
  66. $sql.=' left join BAS_SKU sku on sku.SKU=stockLog.FMSKU and sku.CUSTOMERID=stockLog.客户 ';
  67. $sql.=' left join INV_LOT_ATT lot on lot.LOTNUM=stockLog.FMLOTNUM ';
  68. $sql.=' left join INV_LOT_LOC_ID storeStatus on storeStatus.LOTNUM=stockLog.FMLOTNUM ';;
  69. $sql.=' and storeStatus.LocationID=stockLog.库位 ';
  70. $sql.=' group by 库位,customer.Descr_C,sku.SKU,sku.ALTERNATE_SKU1 ';
  71. $sql.=' ,sku.Descr_C,FMLotNum,lot.LotAtt05,lot.LotAtt08,lot.LotAtt02,lot.LotAtt04 ';
  72. $sql.=' , storeStatus.QTY, storeStatus.QtyAllocated,stockLog.客户 ';
  73. $sql.=' )result where 1=1 ';
  74. if ($LotAtt05)$sql .=" and 属性仓 like '".$LotAtt05."' ";
  75. if ($LotAtt02_start)$sql.=" and 失效日期 >='".$LotAtt02_start." 00:00:00' ";
  76. if ($LotAtt02_end)$sql.=" and 失效日期 <='".$LotAtt02_end." 23:59:59' ";
  77. if ($descr_c){
  78. $sql .= ' and 货主 in (';
  79. $descr_cs = explode(',',$descr_c);
  80. foreach ($descr_cs as $index => $descr_c){
  81. if ($index != 0)$sql .= ',';
  82. $sql .= "'".$descr_c."'";
  83. }
  84. $sql .= ') ';
  85. }
  86. if ($ALTERNATE_SKU1)$sql.=" and 产品条码 like '".$ALTERNATE_SKU1."' ";
  87. if ($page&&$paginate)$sql.=" and ROWNUM<='".$page*$paginate."'";
  88. $sql.=' ) ';
  89. if ($page&&$paginate)$sql.=" where rn>'".($page-1)*$paginate."'";
  90. return DB::connection('oracle')->select($sql);
  91. }
  92. //动库报表
  93. public function changeInventory(Request $request){
  94. if(!Gate::allows("库存管理-动库报表")){ return redirect(url('/')); }
  95. $page=$request->input('page')??1;
  96. $paginate=$request->input('paginate')??50;
  97. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  98. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  99. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  100. return view('inventory.statement.changeInventory',compact('oracleActTransactingLogs','page','owners'));
  101. }
  102. //全部库存
  103. public function allInventory(Request $request){
  104. if(!Gate::allows("库存管理-库存")){ return redirect(url('/')); }
  105. $page=$request->page??1;
  106. $paginate=$request->input('paginate')??50;
  107. $oracleActTransactingLogs=$this->conditionQuery($request,$page,$paginate);
  108. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  109. $owners=OracleBasCustomer::select('descr_c')->where('customer_type','OW')->where('active_flag','Y')->get();
  110. return view('inventory.statement.allInventory',compact('oracleActTransactingLogs','page','owners'));
  111. }
  112. public function exportData(Request $request){
  113. if(!Gate::allows("库存管理-动库报表")){ return redirect(url('/')); }
  114. if (!$request->checkAllSign){
  115. $oracleActTransactingLogs=json_decode($request->input('data'),true);
  116. return $this->export($oracleActTransactingLogs);
  117. }
  118. $oracleActTransactingLogs=$this->conditionQuery($request);
  119. $oracleActTransactingLogs=json_encode($oracleActTransactingLogs);
  120. $oracleActTransactingLogs=json_decode($oracleActTransactingLogs,true);
  121. return $this->export($oracleActTransactingLogs);
  122. }
  123. public function export($oracleActTransactingLogs){
  124. $row=[[
  125. 'customer_descr_c'=>'货主',
  126. 'TOLocation'=>'库位',
  127. 'LotAtt05'=>'属性仓',
  128. 'LotAtt02'=>'失效日期',
  129. 'SKU'=>'产品编码',
  130. 'ALTERNATE_SKU1'=>'产品条码',
  131. 'sku_Descr_C'=>'商品名称',
  132. 'LotAtt08'=>'质量状态',
  133. 'LotAtt04'=>'批号',
  134. 'come_sum'=>'移出数量',
  135. 'join_sum'=>'移入数量',
  136. 'QTY'=>'在库数量',
  137. 'QtyAllocated'=>'占用数量',
  138. ]];
  139. $list=[];
  140. for ($i=0; $i<count($oracleActTransactingLogs);$i++){
  141. $w=[
  142. 'customer_descr_c'=>$oracleActTransactingLogs[$i]['货主'],
  143. 'TOLocation'=>$oracleActTransactingLogs[$i]['库位'],
  144. 'LotAtt05'=>$oracleActTransactingLogs[$i]['属性仓'],
  145. 'LotAtt02'=>$oracleActTransactingLogs[$i]['失效日期'],
  146. 'SKU'=>$oracleActTransactingLogs[$i]['产品编码'],
  147. 'ALTERNATE_SKU1'=>$oracleActTransactingLogs[$i]['产品条码'],
  148. 'sku_Descr_C'=>$oracleActTransactingLogs[$i]['商品名称'],
  149. 'LotAtt08'=>$oracleActTransactingLogs[$i]['质量状态'],
  150. 'LotAtt04'=>$oracleActTransactingLogs[$i]['批号'],
  151. 'come_sum'=>$oracleActTransactingLogs[$i]['移出数量'],
  152. 'join_sum'=>$oracleActTransactingLogs[$i]['移入数量'],
  153. 'QTY'=>$oracleActTransactingLogs[$i]['在库数量'],
  154. 'QtyAllocated'=>$oracleActTransactingLogs[$i]['占用数量'],
  155. ];
  156. $list[$i]=$w;
  157. }
  158. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-动库报表单.xlsx');
  159. }
  160. //创建盘点任务
  161. public function createStockInventoryMission(Request $request){
  162. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  163. $date_start=$request->input('formData.date_start');
  164. $date_end=$request->input('formData.date_end');
  165. $ownerId=$request->input('formData.owner_id')[0];
  166. $inventory=app('inventoryService')->createMission($date_start,$date_end,$ownerId);
  167. $inventory=Inventory::with('owner')->find($inventory->id);
  168. if (is_null($inventory)) return ['success'=>false,'data'=>'参数错误!'];
  169. return ['success'=>true,'data'=>$inventory];
  170. }
  171. //删除盘点任务
  172. public function deleteStockInventoryMission($id){
  173. if(!Gate::allows('库存管理-盘点')){return['success'=>0,'status'=>'没有权限'];}
  174. if(is_null($id)){return ['success'=>false,'data'=>'传入id为空'];}
  175. $inventory=Inventory::where('id',$id)->delete();
  176. return ['success'=>true,'data'=>$inventory];
  177. }
  178. //盘点-任务页面
  179. public function mission(Request $request){
  180. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  181. $paginateParams = $request->input();
  182. $queryParam=$request->all();
  183. $inventories=app('inventoryService')->paginate($queryParam);
  184. $owners=Owner::select('id','name')->get();
  185. return view('inventory.stockInventory.mission',compact('owners','inventories','paginateParams'));
  186. }
  187. //进入盘点中或复盘页面
  188. public function enterStockInventory($id){
  189. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  190. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  191. $inventory=Inventory::with('owner')->find($id);
  192. $inventoryMissions=InventoryMission::with(['commodity'])->where('inventory_id',$id)->orderBy('difference_amount','desc')->get();
  193. return view('inventory.stockInventory.inventoryMission',compact('inventory','inventoryMissions'));
  194. }
  195. //依据盘点任务id进行 --盘点
  196. public function stockInventory(Request $request){
  197. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  198. $location=$request->input('location');
  199. $barcode=$request->input('barcode');
  200. $inventoryId=$request->input('inventoryId');
  201. $count=$request->input('count');
  202. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  203. $inventoryMission=app('inventoryService')->stockInventory($location,$barcode,$count,$inventoryId);
  204. if (!$inventoryMission)return ['success'=>false,'data'=>'参数错误!'];
  205. $inventory=app('inventoryService')->updateInventory($inventoryId);
  206. return ['success'=>true,'inventoryMission'=>$inventoryMission,'inventory'=>$inventory];
  207. }
  208. //根据该库存和产品条码查询该条盘点记录
  209. public function searchStockInventoryRecord(Request $request){
  210. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  211. $location=$request->input('location');
  212. $barcode=$request->input('barcode');
  213. $inventoryId=$request->input('inventoryId');
  214. $inventoryMission=app('inventoryService')->searchStockInventoryRecord($location,$barcode,$inventoryId);
  215. if (!$inventoryMission)return ['success'=>false,'data'=>'参数错误!'];
  216. return ['success'=>true,'data'=>$inventoryMission];
  217. }
  218. //盘点任务导出
  219. public function stockInventoryExport(Request $request){
  220. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  221. ini_set('max_execution_time',3500);
  222. ini_set('memory_limit','3526M');
  223. if ($request->checkAllSign){
  224. $request->offsetUnset('checkAllSign');
  225. $queryParam=$request->all();
  226. $inventories=app('inventoryService')->get($queryParam);
  227. }else{
  228. $queryParam=$request->all();
  229. $inventories=app('inventoryService')->some($queryParam);
  230. }
  231. $row=[[
  232. 'id'=>'盘点编号',
  233. 'created_at'=>'创建时间',
  234. 'owner_id'=>'货主',
  235. 'type'=>'任务类型',
  236. 'start_at'=>'起始时间',
  237. 'end_at'=>'结束时间',
  238. 'total'=>'记录数',
  239. 'processed'=>'已盘数',
  240. 'surplus'=>'剩余数',
  241. 'difference'=>'复盘差异',
  242. 'returned'=>'复盘归位',
  243. ]];
  244. $list=[];
  245. for ($i=0; $i<count($inventories);$i++){
  246. $inventory=$inventories[$i];
  247. $w=[
  248. 'id'=>isset($inventory->id)?$inventory->id:'',
  249. 'created_at'=>isset($inventory->created_at)?$inventory->created_at:'',
  250. 'owner_id'=>isset($inventory->owner->name)?$inventory->owner->name:'',
  251. 'type'=>isset($inventory->type)?$inventory->type:'',
  252. 'start_at'=>isset($inventory->start_at)?$inventory->start_at:'',
  253. 'end_at'=>isset($inventory->end_at)?$inventory->end_at:'',
  254. 'total'=>isset($inventory->total)?$inventory->total:'',
  255. 'processed'=>isset($inventory->processed)?$inventory->processed:'',
  256. 'surplus'=>isset($inventory->surplus)?$inventory->surplus:'',
  257. 'difference'=>isset($inventory->difference)?$inventory->difference:'',
  258. 'returned'=>isset($inventory->returned)?$inventory->returned:'',
  259. ];
  260. $list[$i]=$w;
  261. }
  262. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  263. }
  264. }