InventoryAccountController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\InventoryAccount;
  5. use App\InventoryAccountMission;
  6. use App\Owner;
  7. use App\Services\InventoryAccountService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Gate;
  10. use Maatwebsite\Excel\Facades\Excel;
  11. class InventoryAccountController extends Controller
  12. {
  13. public function __construct()
  14. {
  15. app()->singleton('inventoryService',InventoryAccountService::class);
  16. }
  17. //创建盘点任务
  18. public function createStockInventoryMission(Request $request){
  19. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  20. // $date_start=$request->input('formData.date_start');
  21. // $date_end=$request->input('formData.date_end');
  22. // $ownerId=$request->input('formData.owner_id')[0];
  23. $date_start=$request->input('date_start');
  24. $date_end=$request->input('date_end');
  25. $ownerId=$request->input('owner_id');
  26. $inventoryAccount=app('inventoryService')->createMission($date_start,$date_end,$ownerId);
  27. $inventoryAccount=InventoryAccount::with('owner')->find($inventoryAccount->id);
  28. if (is_null($inventoryAccount)) return ['success'=>false,'data'=>'参数错误!'];
  29. return ['success'=>true,'data'=>$inventoryAccount];
  30. }
  31. //删除盘点任务
  32. public function deleteStockInventoryMission($id){
  33. if(!Gate::allows('库存管理-盘点')){return['success'=>0,'status'=>'没有权限'];}
  34. if(is_null($id)){return ['success'=>false,'data'=>'传入id为空'];}
  35. $inventoryAccount=InventoryAccount::where('id',$id)->delete();
  36. return ['success'=>true,'data'=>$inventoryAccount];
  37. }
  38. //盘点-任务页面
  39. public function mission(Request $request){
  40. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  41. $paginateParams = $request->input();
  42. $queryParam=$request->all();
  43. $inventoryAccounts=app('inventoryService')->paginate($queryParam);
  44. $owners=Owner::select('id','name')->get();
  45. return view('inventory.stockInventory.mission',compact('owners','inventoryAccounts','paginateParams'));
  46. }
  47. //进入盘点中或复盘页面
  48. public function enterStockInventory($id){
  49. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  50. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  51. $inventoryAccount=InventoryAccount::with('owner')->find($id);
  52. $inventoryAccountMissions=InventoryAccountMission::with(['commodity'=>function($query){
  53. return $query->with(['barcodes']);
  54. }])->where('inventory_account_id',$id)->orderBy('difference_amount','desc')->get();
  55. return view('inventory.stockInventory.inventoryMission',compact('inventoryAccount','inventoryAccountMissions'));
  56. }
  57. //依据盘点任务id进行 --盘点
  58. public function stockInventory(Request $request){
  59. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  60. $location=$request->input('location');
  61. $barcode=$request->input('barcode');
  62. $inventoryId=$request->input('inventoryId');
  63. $count=$request->input('count');
  64. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  65. $inventoryAccountMission=app('inventoryService')->stockInventory($location,$barcode,$count,$inventoryId);
  66. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  67. $inventoryAccount=app('inventoryService')->updateInventory($inventoryId);
  68. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount];
  69. }
  70. //根据该库存和产品条码查询该条盘点记录
  71. public function searchStockInventoryRecord(Request $request){
  72. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  73. $location=$request->input('location');
  74. $barcode=$request->input('barcode');
  75. $inventoryId=$request->input('inventoryId');
  76. $inventoryAccountMission=app('inventoryService')->searchStockInventoryRecord($location,$barcode,$inventoryId);
  77. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  78. return ['success'=>true,'data'=>$inventoryAccountMission];
  79. }
  80. //盘点任务导出
  81. public function stockInventoryExport(Request $request){
  82. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  83. ini_set('max_execution_time',3500);
  84. ini_set('memory_limit','3526M');
  85. if ($request->checkAllSign){
  86. $request->offsetUnset('checkAllSign');
  87. $queryParam=$request->all();
  88. $inventoryAccounts=app('inventoryService')->get($queryParam);
  89. }else{
  90. $queryParam=$request->all();
  91. $inventoryAccounts=app('inventoryService')->some($queryParam);
  92. }
  93. $row=[[
  94. 'id'=>'盘点编号',
  95. 'created_at'=>'创建时间',
  96. 'owner_id'=>'货主',
  97. 'type'=>'任务类型',
  98. 'start_at'=>'起始时间',
  99. 'end_at'=>'结束时间',
  100. 'total'=>'记录数',
  101. 'processed'=>'已盘数',
  102. 'surplus'=>'剩余数',
  103. 'difference'=>'复盘差异',
  104. 'returned'=>'复盘归位',
  105. ]];
  106. $list=[];
  107. for ($i=0; $i<count($inventoryAccounts);$i++){
  108. $inventoryAccount=$inventoryAccounts[$i];
  109. $w=[
  110. 'id'=>isset($inventoryAccount->id)?$inventoryAccount->id:'',
  111. 'created_at'=>isset($inventoryAccount->created_at)?$inventoryAccount->created_at:'',
  112. 'owner_id'=>isset($inventoryAccount->owner->name)?$inventoryAccount->owner->name:'',
  113. 'type'=>isset($inventoryAccount->type)?$inventoryAccount->type:'',
  114. 'start_at'=>isset($inventoryAccount->start_at)?$inventoryAccount->start_at:'',
  115. 'end_at'=>isset($inventoryAccount->end_at)?$inventoryAccount->end_at:'',
  116. 'total'=>isset($inventoryAccount->total)?$inventoryAccount->total:'',
  117. 'processed'=>isset($inventoryAccount->processed)?$inventoryAccount->processed:'',
  118. 'surplus'=>isset($inventoryAccount->surplus)?$inventoryAccount->surplus:'',
  119. 'difference'=>isset($inventoryAccount->difference)?$inventoryAccount->difference:'',
  120. 'returned'=>isset($inventoryAccount->returned)?$inventoryAccount->returned:'',
  121. ];
  122. $list[$i]=$w;
  123. }
  124. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  125. }
  126. }