InventoryAccountController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $paginateParams = $request->input();
  44. $queryParam=$request->all();
  45. $inventoryAccounts=app('inventoryAccountService')->paginate($queryParam);
  46. $owners=$ownerService->getSelection();
  47. return view('inventory.stockInventory.mission',compact('owners','inventoryAccounts','paginateParams'));
  48. }
  49. //进入盘点中或复盘页面
  50. public function enterStockInventory($id,Request $request){
  51. if($request->input('listMode') ?? false){
  52. if (!Gate::allows('库存管理-盘点-查看')){return redirect(url('/')); }
  53. }else{
  54. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  55. }
  56. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  57. $inventoryAccount=InventoryAccount::with('owner')->find($id);
  58. $inventoryAccountMissions=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('inventory_account_id',$id)->orderBy('difference_amount','desc')->get();
  59. return view('inventory.stockInventory.inventoryMission',compact('inventoryAccount','inventoryAccountMissions'));
  60. }
  61. //依据盘点任务id进行 --盘点
  62. public function stockInventory(Request $request){
  63. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  64. $location=$request->input('location');
  65. $barcode=$request->input('barcode');
  66. $inventoryId=$request->input('inventoryId');
  67. $count=$request->input('count');
  68. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  69. /** @var InventoryAccountService $inventoryAccountMission */
  70. $inventoryAccountService=app('inventoryAccountService');
  71. $inventoryAccountMission=$inventoryAccountService->stockInventory($location,$barcode,$count,$inventoryId);
  72. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  73. /** @var InventoryAccountService $inventoryService */
  74. $inventoryService=app('inventoryAccountService');
  75. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  76. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  77. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  78. }
  79. //根据该库存和产品条码查询该条盘点记录
  80. public function searchStockInventoryRecord(Request $request){
  81. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  82. $location=$request->input('location');
  83. $barcode=$request->input('barcode');
  84. $inventoryId=$request->input('inventoryId');
  85. $inventoryAccountMission=app('inventoryAccountService')->searchStockInventoryRecord($location,$barcode,$inventoryId);
  86. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  87. return ['success'=>true,'data'=>$inventoryAccountMission];
  88. }
  89. //盘点任务导出
  90. public function stockInventoryExport(Request $request){
  91. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  92. ini_set('max_execution_time',3500);
  93. ini_set('memory_limit','3526M');
  94. if ($request->checkAllSign){
  95. $request->offsetUnset('checkAllSign');
  96. $queryParam=$request->all();
  97. $inventoryAccounts=app('inventoryAccountService')->get($queryParam);
  98. }else{
  99. $queryParam=$request->all();
  100. $inventoryAccounts=app('inventoryAccountService')->some($queryParam);
  101. }
  102. $row=[[
  103. 'id'=>'盘点编号',
  104. 'status'=>'盘点状态',
  105. 'created_at'=>'创建时间',
  106. 'owner_id'=>'货主',
  107. 'type'=>'任务类型',
  108. 'start_at'=>'起始时间',
  109. 'end_at'=>'结束时间',
  110. 'total'=>'记录数',
  111. 'processed'=>'已盘数',
  112. 'surplus'=>'剩余数',
  113. 'difference'=>'复盘差异',
  114. 'returned'=>'复盘归位',
  115. ]];
  116. $list=[];
  117. for ($i=0; $i<count($inventoryAccounts);$i++){
  118. $inventoryAccount=$inventoryAccounts[$i];
  119. $w=[
  120. 'id'=>isset($inventoryAccount->id)?$inventoryAccount->id:'',
  121. 'status'=>isset($inventoryAccount->status)?$inventoryAccount->status:'',
  122. 'created_at'=>isset($inventoryAccount->created_at)?$inventoryAccount->created_at:'',
  123. 'owner_id'=>isset($inventoryAccount->owner->name)?$inventoryAccount->owner->name:'',
  124. 'type'=>isset($inventoryAccount->type)?$inventoryAccount->type:'',
  125. 'start_at'=>isset($inventoryAccount->start_at)?$inventoryAccount->start_at:'',
  126. 'end_at'=>isset($inventoryAccount->end_at)?$inventoryAccount->end_at:'',
  127. 'total'=>isset($inventoryAccount->total)?$inventoryAccount->total:'',
  128. 'processed'=>isset($inventoryAccount->processed)?$inventoryAccount->processed:'',
  129. 'surplus'=>isset($inventoryAccount->surplus)?$inventoryAccount->surplus:'',
  130. 'difference'=>isset($inventoryAccount->difference)?$inventoryAccount->difference:'',
  131. 'returned'=>isset($inventoryAccount->returned)?$inventoryAccount->returned:'',
  132. ];
  133. $list[$i]=$w;
  134. }
  135. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  136. }
  137. public function stockInventoryEnd(Request $request){
  138. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  139. $id=$request->input('id');
  140. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  141. $inventoryAccount=InventoryAccount::query()->where('id',$id)->update(['status'=>'复盘中']);
  142. $this->log(__METHOD__,'结束初盘任务'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  143. if ($inventoryAccount>0) return ['success'=>true,'data'=>'复盘中'];
  144. return ['success'=>false,'data'=>'参数错误!'];
  145. }
  146. public function syncOwners(OwnerService $ownerService){
  147. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  148. $owners=$ownerService->syncOwnersData();
  149. if (!$owners)return ['success'=>false,'data'=>'同步货主失败!'];
  150. return ['success'=>true,'data'=>$owners];
  151. }
  152. public function 修改质量状态(Request $request){
  153. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  154. $id=$request->input('id');
  155. $location=$request->location;
  156. $sku=$request->sku;
  157. $quality=$request->quality;
  158. $ownerCode=$request->ownerCode;
  159. $inventoryAccountMission=app('inventoryAccountService')->修改质量状态($id,$location,$sku,$quality,$ownerCode);
  160. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  161. if ($inventoryAccountMission==null) return ['success'=>false,'data'=>'WMS中不存在该条记录!'];
  162. return ['success'=>true,'data'=>'质量状态修改成功'];
  163. }
  164. public function 盘点任务完结($id){
  165. if(!Gate::allows('库存管理-盘点-完结')){return['success'=>false,'status'=>'没有权限'];}
  166. if (!$id)return['success'=>false,'status'=>'参数错误!'];
  167. $inventoryAccount=app('inventoryAccountService')->盘点任务完结($id);
  168. if (!$inventoryAccount)return['success'=>false,'status'=>'修改完结状态失败!'];
  169. return['success'=>true,'data'=>$inventoryAccount];
  170. }
  171. public function 增加系统之外的库位记录(Request $request){
  172. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  173. $location=$request->input('location');
  174. $barcode=$request->input('barcode');
  175. $inventoryId=$request->input('inventoryId');
  176. $count=$request->input('count');
  177. $inventoryAccountMission=app('inventoryAccountService')->增加系统之外的库位记录($location,$barcode,$inventoryId,$count);
  178. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'添加系统之外的库位记录失败!'];
  179. dd($inventoryAccountMission);
  180. return ['success'=>true,'data'=>$inventoryAccountMission];
  181. }
  182. public function 删除盘点记录(Request $request){
  183. if(!Gate::allows('库存管理-盘点-删除')){return['success'=>false,'data'=>'没有权限'];}
  184. $inventoryAccountMissionId=$request->inventoryAccountMissionId;
  185. $inventoryAccountId=$request->inventoryAccountId;
  186. if(is_null($inventoryAccountMissionId)){return ['success'=>false,'data'=>'传入id为空'];}
  187. $inventoryAccountMission=InventoryAccountMission::where('id',$inventoryAccountMissionId)->delete();
  188. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  189. if ($inventoryAccountMission>0){
  190. $inventoryAccount=InventoryAccount::where('id',$inventoryAccountId)->first();
  191. $inventoryAccount->total=$inventoryAccount->total-1;
  192. $inventoryAccount->processed=$inventoryAccount->getProcessedAmount();//已盘点数
  193. $inventoryAccount->difference=$inventoryAccount->getDifferenceAmount();//盘点差异数
  194. $inventoryAccount->returned=$inventoryAccount->getReturnedAmount(); //复盘归位数
  195. $inventoryAccount->update();
  196. $this->log(__METHOD__,'删除盘点记录时修改盘点任务信息'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  197. }
  198. return ['success'=>true,'data'=>$inventoryAccountMission];
  199. }
  200. }