InventoryAccountController.php 23 KB

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