InventoryAccountController.php 20 KB

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