InventoryController.php 15 KB

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