InventoryAccountController.php 19 KB

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