InventoryAccountController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\Export;
  4. use App\InventoryAccount;
  5. use App\InventoryAccountMission;
  6. use App\Services\InventoryAccountService;
  7. use App\Services\OwnerService;
  8. use Exception;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. use Illuminate\Support\Facades\Http;
  13. use Illuminate\Support\Facades\Redis;
  14. use Maatwebsite\Excel\Facades\Excel;
  15. class InventoryAccountController extends Controller
  16. {
  17. public function __construct()
  18. {
  19. app()->singleton('inventoryAccountService',InventoryAccountService::class);
  20. }
  21. //创建盘点任务
  22. public function createStockInventoryMission(Request $request){
  23. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  24. // $date_start=$request->input('formData.date_start');
  25. // $date_end=$request->input('formData.date_end');
  26. // $ownerId=$request->input('formData.owner_id')[0];
  27. $date_start=$request->input('date_start');
  28. $date_end=$request->input('date_end');
  29. $ownerId=$request->input('owner_id');
  30. $location=$request->input('location');
  31. $barcode=$request->input('barcode');
  32. $inventoryAccount=app('inventoryAccountService')->createMission($date_start,$date_end,$ownerId,$location,$barcode);
  33. $inventoryAccount=InventoryAccount::with('owner')->find($inventoryAccount->id);
  34. if (is_null($inventoryAccount)) return ['success'=>false,'data'=>'参数错误!'];
  35. return ['success'=>true,'data'=>$inventoryAccount];
  36. }
  37. //删除盘点任务
  38. public function deleteStockInventoryMission($id){
  39. if(!Gate::allows('库存管理-盘点')){return['success'=>0,'status'=>'没有权限'];}
  40. if(is_null($id)){return ['success'=>false,'data'=>'传入id为空'];}
  41. $inventoryAccount=InventoryAccount::where('id',$id)->delete();
  42. return ['success'=>true,'data'=>$inventoryAccount];
  43. }
  44. //盘点-任务页面
  45. public function mission(Request $request,OwnerService $ownerService){
  46. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  47. $paginateParams = $request->input();
  48. $queryParam=$request->all();
  49. $inventoryAccounts=app('inventoryAccountService')->paginate($queryParam);
  50. $owners=$ownerService->getSelection();
  51. return view('inventory.stockInventory.mission',compact('owners','inventoryAccounts','paginateParams'));
  52. }
  53. //进入盘点中或复盘页面
  54. public function enterStockInventory($id,Request $request){
  55. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  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. public function enterBlindReceive($id){
  62. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  63. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  64. $inventoryAccount=InventoryAccount::with('owner')->find($id);
  65. return view('inventory.stockInventory.blindReceive',compact('inventoryAccount'));
  66. }
  67. //依据盘点任务id进行 --盘点
  68. public function stockInventory(Request $request){
  69. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  70. $location=$request->input('location');
  71. $barcode=$request->input('barcode');
  72. $inventoryId=$request->input('inventoryId');
  73. $count=$request->input('count');
  74. $id=$request->input('id');
  75. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  76. /** @var InventoryAccountService $inventoryAccountMission */
  77. $inventoryAccountService=app('inventoryAccountService');
  78. $inventoryAccountMission=$inventoryAccountService->stockInventory($id,$location,$barcode,$count,$inventoryId);
  79. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'参数错误!'];
  80. /** @var InventoryAccountService $inventoryService */
  81. $inventoryService=app('inventoryAccountService');
  82. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  83. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  84. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  85. }
  86. public function baseOnBlindReceive(Request $request){
  87. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  88. $location=$request->input('location');
  89. $inventoryId=$request->input('inventoryId');
  90. $owner_code=$request->input('owner_code');
  91. $goodses=$request->input('goodses');
  92. if (!$location) return ['success'=>false,'fail_info'=>'盘点库位不存在!'];
  93. if (count($goodses)<1) return ['success'=>false,'fail_info'=>'盘点商品不存在!'];
  94. //dd($location,$owner_code,$goodses,$inventoryId,$owner_id);
  95. /** @var InventoryAccountService $inventoryAccountMission */
  96. $inventoryAccountService=app('inventoryAccountService');
  97. $inventoryAccountMissions=$inventoryAccountService->baseOnBlindReceive($location,$owner_code,$goodses,$inventoryId);
  98. if (count($inventoryAccountMissions)<0)return ['success'=>false,'fail_info'=>'盲收盘点失败!'];
  99. /** @var InventoryAccountService $inventoryService */
  100. $inventoryService=app('inventoryAccountService');
  101. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  102. if ($inventoryAccountMissions&&$inventoryAccount)
  103. $stockInventoryPersons=$inventoryAccountMissions[0]->stockInventoryPersons;
  104. return ['success'=>true,'inventoryMissions'=>$inventoryAccountMissions,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  105. }
  106. //根据该库存和产品条码查询该条盘点记录??
  107. public function searchStockInventoryRecord(Request $request){
  108. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  109. $location=$request->input('location');
  110. $barcode=$request->input('barcode');
  111. $inventoryId=$request->input('inventoryId');
  112. /** @var InventoryAccountService $application */
  113. $application = app('inventoryAccountService');
  114. $inventoryAccountMissions= $application->searchStockInventoryRecord($location,$barcode,$inventoryId);
  115. if ($inventoryAccountMissions->isEmpty())return ['success'=>false,'data'=>'没有找到相应记录!'];
  116. // $stockInventoryPersons=$inventoryAccountMissions->stockInventoryPersons;
  117. // return ['success'=>true,'data'=>$inventoryAccountMissions,'stockInventoryPersons'=>$stockInventoryPersons];
  118. return ['success'=>true,'data'=>$inventoryAccountMissions];
  119. }
  120. //盘点任务导出
  121. public function stockInventoryExport(Request $request){
  122. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  123. ini_set('max_execution_time',3500);
  124. ini_set('memory_limit','3526M');
  125. if ($request->checkAllSign){
  126. $request->offsetUnset('checkAllSign');
  127. $queryParam=$request->all();
  128. $inventoryAccounts=app('inventoryAccountService')->get($queryParam);
  129. }else{
  130. $queryParam=$request->all();
  131. $inventoryAccounts=app('inventoryAccountService')->some($queryParam);
  132. }
  133. $row=[[
  134. 'id'=>'盘点编号',
  135. 'status'=>'盘点状态',
  136. 'created_at'=>'创建时间',
  137. 'owner_id'=>'货主',
  138. 'type'=>'任务类型',
  139. 'start_at'=>'起始时间',
  140. 'end_at'=>'结束时间',
  141. 'total'=>'记录数',
  142. 'processed'=>'已盘数',
  143. 'surplus'=>'剩余数',
  144. 'difference'=>'复盘差异',
  145. 'returned'=>'复盘归位',
  146. ]];
  147. $list=[];
  148. for ($i=0; $i<count($inventoryAccounts);$i++){
  149. $inventoryAccount=$inventoryAccounts[$i];
  150. $w=[
  151. 'id'=>isset($inventoryAccount->id)?$inventoryAccount->id:'',
  152. 'status'=>isset($inventoryAccount->status)?$inventoryAccount->status:'',
  153. 'created_at'=>isset($inventoryAccount->created_at)?$inventoryAccount->created_at:'',
  154. 'owner_id'=>isset($inventoryAccount->owner->name)?$inventoryAccount->owner->name:'',
  155. 'type'=>isset($inventoryAccount->type)?$inventoryAccount->type:'',
  156. 'start_at'=>isset($inventoryAccount->start_at)?$inventoryAccount->start_at:'',
  157. 'end_at'=>isset($inventoryAccount->end_at)?$inventoryAccount->end_at:'',
  158. 'total'=>isset($inventoryAccount->total)?$inventoryAccount->total:'',
  159. 'processed'=>isset($inventoryAccount->processed)?$inventoryAccount->processed:'',
  160. 'surplus'=>isset($inventoryAccount->surplus)?$inventoryAccount->surplus:'',
  161. 'difference'=>isset($inventoryAccount->difference)?$inventoryAccount->difference:'',
  162. 'returned'=>isset($inventoryAccount->returned)?$inventoryAccount->returned:'',
  163. ];
  164. $list[$i]=$w;
  165. }
  166. return Excel::download(new Export($row,$list),date('YmdHis', time()).'-盘点任务记录单.xlsx');
  167. }
  168. public function stockInventoryEnd(Request $request){
  169. if (!Gate::allows('库存管理-盘点-结束初盘')){return ['success'=>false,'data'=>'没有权限']; }
  170. $id=$request->input('id');
  171. if (!$id) return ['success'=>false,'data'=>'参数错误!'];
  172. $inventoryAccount=InventoryAccount::query()->where('id',$id)->update(['status'=>'复盘中']);
  173. $this->log(__METHOD__,'结束初盘任务'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  174. if ($inventoryAccount>0) return ['success'=>true,'data'=>'复盘中'];
  175. return ['success'=>false,'data'=>'参数错误!'];
  176. }
  177. public function syncOwners(OwnerService $ownerService){
  178. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  179. $owners=$ownerService->syncOwnersData();
  180. if (!$owners)return ['success'=>false,'data'=>'同步货主失败!'];
  181. return ['success'=>true,'data'=>$owners];
  182. }
  183. public function 修改质量状态(Request $request){
  184. if (!Gate::allows('库存管理-盘点')){return redirect(url('/')); }
  185. $id=$request->input('id');
  186. $location=$request->location;
  187. $sku=$request->sku;
  188. $quality=$request->quality;
  189. $ownerCode=$request->ownerCode;
  190. $inventoryAccountMission=app('inventoryAccountService')->修改质量状态($id,$location,$sku,$quality,$ownerCode);
  191. $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
  192. if ($inventoryAccountMission==null) return ['success'=>false,'data'=>'WMS中不存在该条记录!'];
  193. return ['success'=>true,'data'=>'质量状态修改成功'];
  194. }
  195. public function 完结盘点任务($id){
  196. if(!Gate::allows('库存管理-盘点-完结')){return['success'=>false,'status'=>'没有权限'];}
  197. if (!$id)return['success'=>false,'status'=>'参数错误!'];
  198. $inventoryAccount=app('inventoryAccountService')->完结盘点任务($id);
  199. if (!$inventoryAccount)return['success'=>false,'status'=>'修改完结状态失败!'];
  200. return['success'=>true,'data'=>$inventoryAccount];
  201. }
  202. public function 增加系统之外的盘点记录(Request $request){
  203. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  204. $location=$request->input('location');
  205. $barcode=$request->input('barcode');
  206. $inventoryId=$request->input('inventoryId');
  207. $count=$request->input('count');
  208. $owner_code=$request->input('owner_code');
  209. $param=$request->input('param');
  210. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  211. $inventoryAccountMission=app('inventoryAccountService')->增加系统之外的盘点记录($location,$barcode,$inventoryId,$count,$owner_code,$param);
  212. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'添加系统之外的库位记录失败!'];
  213. $inventoryAccountMission=InventoryAccountMission::with(['commodity.barcodes','stockInventoryPersons'])->where('id',$inventoryAccountMission->id)->first();
  214. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  215. return ['success'=>true,'inventoryAccountMission'=>$inventoryAccountMission,'stockInventoryPersons'=>$stockInventoryPersons];
  216. }
  217. public function 盘点选中任务(Request $request){
  218. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  219. $id=$request->input('id');
  220. $count=$request->count;
  221. $inventoryId=$request->input('inventoryId');
  222. $produced_at=$request->input('produced_at');
  223. $valid_at=$request->input('valid_at');
  224. $batch_number=$request->input('batch_number');
  225. if (is_null($count)) return ['success'=>false,'data'=>'盘点数不能为空!'];
  226. if ($produced_at||$valid_at||$batch_number){
  227. /** @var InventoryAccountService $inventoryAccountMission */
  228. $inventoryAccountService=app('inventoryAccountService');
  229. $inventoryAccountMission=$inventoryAccountService ->盘点生产日期_失效日期_批号有改动任务($id,$count,$inventoryId,$produced_at,$valid_at,$batch_number);
  230. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'盘点生产日期_失效日期_批号有改动任务失败!'];
  231. /** @var InventoryAccountService $inventoryService */
  232. $inventoryService=app('inventoryAccountService');
  233. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  234. $stockInventoryPersons=$inventoryAccountMission[0]->stockInventoryPersons;
  235. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  236. }else{
  237. /** @var InventoryAccountService $inventoryAccountMission */
  238. $inventoryAccountService=app('inventoryAccountService');
  239. $inventoryAccountMission=$inventoryAccountService ->盘点选中任务($id,$count,$inventoryId);
  240. if (!$inventoryAccountMission)return ['success'=>false,'data'=>'盘点选中任务失败!'];
  241. /** @var InventoryAccountService $inventoryService */
  242. $inventoryService=app('inventoryAccountService');
  243. $inventoryAccount=$inventoryService->updateInventory($inventoryId);
  244. $stockInventoryPersons=$inventoryAccountMission->stockInventoryPersons;
  245. return ['success'=>true,'inventoryMission'=>$inventoryAccountMission,'inventory'=>$inventoryAccount,'stockInventoryPersons'=>$stockInventoryPersons];
  246. }
  247. }
  248. public function 删除盘点记录(Request $request){
  249. if(!Gate::allows('库存管理-盘点-删除')){return['success'=>false,'data'=>'没有权限'];}
  250. $inventoryAccountMissionId=$request->input('inventoryAccountMissionId');
  251. $inventoryAccountId=$request->input('inventoryAccountId');
  252. if(is_null($inventoryAccountMissionId)){return ['success'=>false,'data'=>'传入id为空'];}
  253. /** @var InventoryAccountService $inventoryService */
  254. $inventoryService=app('inventoryAccountService');
  255. $inventoryAccountMission=$inventoryService->删除盘点记录($inventoryAccountMissionId,$inventoryAccountId);
  256. return ['success'=>true,'data'=>$inventoryAccountMission];
  257. }
  258. public function 跳过盘点记录(Request $request){
  259. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  260. $inventoryAccountMissionId=$request->inventoryAccountMissionId;
  261. $inventoryAccountId=$request->input('inventoryAccountId');
  262. if(is_null($inventoryAccountMissionId)){return ['success'=>false,'data'=>'传入id为空'];}
  263. /** @var InventoryAccountService $inventoryService */
  264. $inventoryService=app('inventoryAccountService');
  265. $inventoryAccountMission=$inventoryService->跳过盘点记录($inventoryAccountMissionId,$inventoryAccountId);
  266. return ['success'=>true,'inventoryAccountMission'=>$inventoryAccountMission];
  267. }
  268. public function 确认盘点差异(Request $request){
  269. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  270. $inventoryAccountMissionId=$request->inventoryAccountMissionId;
  271. $inventoryAccountId=$request->input('inventoryAccountId');
  272. if(is_null($inventoryAccountMissionId)){return ['success'=>false,'data'=>'传入id为空'];}
  273. /** @var InventoryAccountService $inventoryService */
  274. $inventoryService=app('inventoryAccountService');
  275. $inventoryAccountMission=$inventoryService->确认盘点差异($inventoryAccountMissionId,$inventoryAccountId);
  276. return ['success'=>true,'inventoryAccountMission'=>$inventoryAccountMission];
  277. }
  278. public function 批量跳过或确认差异(Request $request){
  279. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  280. $checkData=$request->checkData;
  281. if(is_null($checkData)){return ['success'=>false,'data'=>'传入勾选盘点记录为空'];}
  282. $marks=[];
  283. foreach ($checkData as $inventoryMission){
  284. array_push($marks,$inventoryMission['mark']);
  285. }
  286. if (in_array('确认差异',$marks)||in_array('跳过',$marks)||in_array('无差异',$marks)||in_array('已复盘无差异',$marks))return ['success'=>false,'data'=>'传入勾选盘点记录存在不可操作项!'];
  287. /** @var InventoryAccountService $inventoryService */
  288. $inventoryService=app('inventoryAccountService');
  289. $inventoryAccountMissions=$inventoryService->批量跳过或确认差异($checkData);
  290. return ['success'=>true,'inventoryAccountMissions'=>$inventoryAccountMissions];
  291. }
  292. public function exportInventoryAccountMission(Request $request){
  293. if(!Gate::allows("库存管理-盘点")){ return redirect(url('/')); }
  294. $post = Http::post(config('go.export.url'),['type'=>'inventoryAccountMission','data'=>$request->data]);
  295. if ($post->status() == 500){
  296. throw new Exception($post->header("Msg"));
  297. }
  298. return response($post,200, [
  299. "Content-type"=>"application/octet-stream",
  300. "Content-Disposition"=>"attachment; filename=库存盘点记录-".date('ymdHis').'.xlsx',
  301. ]);
  302. }
  303. public function searchCommodityByBarcode(Request $request){
  304. if(!Gate::allows('库存管理-盘点')){return['success'=>false,'data'=>'没有权限'];}
  305. $barcode=$request->input('barcode');
  306. $owner_code=$request->input('owner_code');
  307. /** @var InventoryAccountService $inventoryService */
  308. $inventoryService=app('inventoryAccountService');
  309. $commodity=$inventoryService->searchCommodityByBarcode($barcode,$owner_code);
  310. if ($commodity){
  311. return ['success'=>true,'data'=>$commodity];
  312. }else{
  313. return ['success'=>false,'data'=>'输入的条码没有对应商品!'];
  314. }
  315. }
  316. }