InventoryAccountController.php 7.7 KB

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