InventoryAccountController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\Auth;
  10. use Illuminate\Support\Facades\Gate;
  11. use Maatwebsite\Excel\Facades\Excel;
  12. class InventoryAccountController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. app()->singleton('inventoryAccountService',InventoryAccountService::class);
  17. }
  18. //创建盘点任务
  19. public function createStockInventoryMission(Request $request){
  20. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  21. // $date_start=$request->input('formData.date_start');
  22. // $date_end=$request->input('formData.date_end');
  23. // $ownerId=$request->input('formData.owner_id')[0];
  24. $date_start=$request->input('date_start');
  25. $date_end=$request->input('date_end');
  26. $ownerId=$request->input('owner_id');
  27. $inventoryAccount=app('inventoryAccountService')->createMission($date_start,$date_end,$ownerId);
  28. $inventoryAccount=InventoryAccount::with('owner')->find($inventoryAccount->id);
  29. if (is_null($inventoryAccount)) return ['success'=>false,'data'=>'参数错误!'];
  30. return ['success'=>true,'data'=>$inventoryAccount];
  31. }
  32. //删除盘点任务
  33. public function deleteStockInventoryMission($id){
  34. if(!Gate::allows('库存管理-盘点')){return['success'=>0,'status'=>'没有权限'];}
  35. if(is_null($id)){return ['success'=>false,'data'=>'传入id为空'];}
  36. $inventoryAccount=InventoryAccount::where('id',$id)->delete();
  37. return ['success'=>true,'data'=>$inventoryAccount];
  38. }
  39. //盘点-任务页面
  40. public function mission(Request $request){
  41. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  42. $paginateParams = $request->input();
  43. $queryParam=$request->all();
  44. $inventoryAccounts=app('inventoryAccountService')->paginate($queryParam);
  45. $owners=Owner::select('id','name')->get();
  46. return view('inventory.stockInventory.mission',compact('owners','inventoryAccounts','paginateParams'));
  47. }
  48. //进入盘点中或复盘页面
  49. public function enterStockInventory($id){
  50. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  51. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  52. $inventoryAccount=InventoryAccount::with('owner')->find($id);
  53. $inventoryAccountMissions=InventoryAccountMission::with('commodity.barcodes')->where('inventory_account_id',$id)->orderBy('difference_amount','desc')->get();
  54. return view('inventory.stockInventory.inventoryMission',compact('inventoryAccount','inventoryAccountMissions'));
  55. }
  56. //依据盘点任务id进行 --盘点
  57. public function stockInventory(Request $request){
  58. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  59. $location=$request->input('location');
  60. $barcode=$request->input('barcode');
  61. $inventoryId=$request->input('inventoryId');
  62. $count=$request->input('count');
  63. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  64. $inventoryAccountMission=app('inventoryAccountService')->stockInventory($location,$barcode,$count,$inventoryId);
  65. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  66. $inventoryAccount=app('inventoryAccountService')->updateInventory($inventoryId);
  67. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount];
  68. }
  69. //根据该库存和产品条码查询该条盘点记录
  70. public function searchStockInventoryRecord(Request $request){
  71. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  72. $location=$request->input('location');
  73. $barcode=$request->input('barcode');
  74. $inventoryId=$request->input('inventoryId');
  75. $inventoryAccountMission=app('inventoryAccountService')->searchStockInventoryRecord($location,$barcode,$inventoryId);
  76. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  77. return ['success'=>true,'data'=>$inventoryAccountMission];
  78. }
  79. //盘点任务导出
  80. public function stockInventoryExport(Request $request){
  81. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  82. ini_set('max_execution_time',3500);
  83. ini_set('memory_limit','3526M');
  84. if ($request->checkAllSign){
  85. $request->offsetUnset('checkAllSign');
  86. $queryParam=$request->all();
  87. $inventoryAccounts=app('inventoryAccountService')->get($queryParam);
  88. }else{
  89. $queryParam=$request->all();
  90. $inventoryAccounts=app('inventoryAccountService')->some($queryParam);
  91. }
  92. $row=[[
  93. 'id'=>'盘点编号',
  94. 'status'=>'盘点状态',
  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. 'status'=>isset($inventoryAccount->status)?$inventoryAccount->status:'',
  112. 'created_at'=>isset($inventoryAccount->created_at)?$inventoryAccount->created_at:'',
  113. 'owner_id'=>isset($inventoryAccount->owner->name)?$inventoryAccount->owner->name:'',
  114. 'type'=>isset($inventoryAccount->type)?$inventoryAccount->type:'',
  115. 'start_at'=>isset($inventoryAccount->start_at)?$inventoryAccount->start_at:'',
  116. 'end_at'=>isset($inventoryAccount->end_at)?$inventoryAccount->end_at:'',
  117. 'total'=>isset($inventoryAccount->total)?$inventoryAccount->total:'',
  118. 'processed'=>isset($inventoryAccount->processed)?$inventoryAccount->processed:'',
  119. 'surplus'=>isset($inventoryAccount->surplus)?$inventoryAccount->surplus:'',
  120. 'difference'=>isset($inventoryAccount->difference)?$inventoryAccount->difference:'',
  121. 'returned'=>isset($inventoryAccount->returned)?$inventoryAccount->returned:'',
  122. ];
  123. $list[$i]=$w;
  124. }
  125. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  126. }
  127. public function stockInventoryEnd(Request $request){
  128. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  129. $id=$request->input('id');
  130. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  131. $inventoryAccount=InventoryAccount::query()->where('id',$id)->update(['status'=>'复盘中']);
  132. $this->log(__METHOD__,'结束初盘任务'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  133. if ($inventoryAccount>0) return ['success'=>true,'data'=>'复盘中'];
  134. return ['success'=>false,'data'=>'参数错误!'];
  135. }
  136. }