InventoryAccountController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. 'difference' => '复盘差异',
  194. 'returned' => '复盘归位',
  195. ]];
  196. $list = [];
  197. for ($i = 0; $i < count($inventoryAccounts); $i++) {
  198. $inventoryAccount = $inventoryAccounts[$i];
  199. $w = [
  200. 'id' => isset($inventoryAccount->id) ? $inventoryAccount->id : '',
  201. 'status' => isset($inventoryAccount->status) ? $inventoryAccount->status : '',
  202. 'created_at' => isset($inventoryAccount->created_at) ? $inventoryAccount->created_at : '',
  203. 'owner_id' => isset($inventoryAccount->owner->name) ? $inventoryAccount->owner->name : '',
  204. 'type' => isset($inventoryAccount->type) ? $inventoryAccount->type : '',
  205. 'start_at' => isset($inventoryAccount->start_at) ? $inventoryAccount->start_at : '',
  206. 'end_at' => isset($inventoryAccount->end_at) ? $inventoryAccount->end_at : '',
  207. 'total' => isset($inventoryAccount->total) ? $inventoryAccount->total : '',
  208. 'processed' => isset($inventoryAccount->processed) ? $inventoryAccount->processed : '',
  209. 'surplus' => isset($inventoryAccount->surplus) ? $inventoryAccount->surplus : '',
  210. 'difference' => isset($inventoryAccount->difference) ? $inventoryAccount->difference : '',
  211. 'returned' => isset($inventoryAccount->returned) ? $inventoryAccount->returned : '',
  212. ];
  213. $list[$i] = $w;
  214. }
  215. return Excel::download(new Export($row, $list), date('YmdHis', time()) . '-盘点任务记录单.xlsx');
  216. }
  217. public function stockInventoryEnd(Request $request)
  218. {
  219. if (!Gate::allows('库存管理-盘点-结束初盘')) {
  220. return ['success' => false, 'data' => '没有权限'];
  221. }
  222. $id = $request->input('id');
  223. if (!$id) return ['success' => false, 'data' => '参数错误!'];
  224. $inventoryAccount = InventoryAccount::query()->where('id', $id)->update(['status' => '复盘中']);
  225. app('LogService')->log(__METHOD__, '结束初盘任务' . __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  226. if ($inventoryAccount > 0) return ['success' => true, 'data' => '复盘中'];
  227. return ['success' => false, 'data' => '参数错误!'];
  228. }
  229. public function syncOwners(OwnerService $ownerService)
  230. {
  231. if (!Gate::allows('库存管理-盘点')) {
  232. return redirect(url('/'));
  233. }
  234. $owners = $ownerService->syncOwnersData();
  235. if (!$owners) return ['success' => false, 'data' => '同步货主失败!'];
  236. return ['success' => true, 'data' => $owners];
  237. }
  238. public function 修改质量状态(Request $request)
  239. {
  240. if (!Gate::allows('库存管理-盘点')) {
  241. return redirect(url('/'));
  242. }
  243. $id = $request->input('id');
  244. $location = $request->location;
  245. $sku = $request->sku;
  246. $quality = $request->quality;
  247. $ownerCode = $request->ownerCode;
  248. $inventoryAccountMission = app('inventoryAccountService')->修改质量状态($id, $location, $sku, $quality, $ownerCode);
  249. app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
  250. if ($inventoryAccountMission == null) return ['success' => false, 'data' => 'WMS中不存在该条记录!'];
  251. return ['success' => true, 'data' => '质量状态修改成功'];
  252. }
  253. public function 完结盘点任务($id)
  254. {
  255. if (!Gate::allows('库存管理-盘点-完结')) {
  256. return ['success' => false, 'status' => '没有权限'];
  257. }
  258. if (!$id) return ['success' => false, 'status' => '参数错误!'];
  259. $inventoryAccount = app('inventoryAccountService')->完结盘点任务($id);
  260. if (!$inventoryAccount) return ['success' => false, 'status' => '修改完结状态失败!'];
  261. return ['success' => true, 'data' => $inventoryAccount];
  262. }
  263. public function 增加系统之外的盘点记录(Request $request)
  264. {
  265. if (!Gate::allows('库存管理-盘点')) {
  266. return ['success' => false, 'data' => '没有权限'];
  267. }
  268. $location = $request->input('location');
  269. $barcode = $request->input('barcode');
  270. $inventoryId = $request->input('inventoryId');
  271. $count = $request->input('count');
  272. $owner_code = $request->input('owner_code');
  273. $param = $request->input('param');
  274. if (is_null($count)) return ['success' => false, 'data' => '盘点数不能为空!'];
  275. $inventoryAccountMission = app('inventoryAccountService')->增加系统之外的盘点记录($location, $barcode, $inventoryId, $count, $owner_code, $param);
  276. if (!$inventoryAccountMission) return ['success' => false, 'data' => '添加系统之外的库位记录失败!'];
  277. $inventoryAccountMission = InventoryAccountMission::with(['commodity.barcodes', 'stockInventoryPersons'])->where('id', $inventoryAccountMission->id)->first();
  278. $stockInventoryPersons = $inventoryAccountMission->stockInventoryPersons;
  279. return ['success' => true, 'inventoryAccountMission' => $inventoryAccountMission, 'stockInventoryPersons' => $stockInventoryPersons];
  280. }
  281. public function 盘点选中任务(Request $request)
  282. {
  283. if (!Gate::allows('库存管理-盘点')) {
  284. return ['success' => false, 'data' => '没有权限'];
  285. }
  286. $id = $request->input('id');
  287. $count = $request->count;
  288. $inventoryId = $request->input('inventoryId');
  289. $produced_at = $request->input('produced_at');
  290. $valid_at = $request->input('valid_at');
  291. $batch_number = $request->input('batch_number');
  292. if (is_null($count)) return ['success' => false, 'data' => '盘点数不能为空!'];
  293. if ($produced_at || $valid_at || $batch_number) {
  294. /** @var InventoryAccountService $inventoryAccountMission */
  295. $inventoryAccountService = app('inventoryAccountService');
  296. $inventoryAccountMission = $inventoryAccountService->盘点生产日期_失效日期_批号有改动任务($id, $count, $inventoryId, $produced_at, $valid_at, $batch_number);
  297. if (!$inventoryAccountMission) return ['success' => false, 'data' => '盘点生产日期_失效日期_批号有改动任务失败!'];
  298. /** @var InventoryAccountService $inventoryService */
  299. $inventoryService = app('inventoryAccountService');
  300. $inventoryAccount = $inventoryService->updateInventory($inventoryId);
  301. $stockInventoryPersons = $inventoryAccountMission[0]->stockInventoryPersons;
  302. return ['success' => true, 'inventoryMission' => $inventoryAccountMission, 'inventory' => $inventoryAccount, 'stockInventoryPersons' => $stockInventoryPersons];
  303. } else {
  304. /** @var InventoryAccountService $inventoryAccountMission */
  305. $inventoryAccountService = app('inventoryAccountService');
  306. $inventoryAccountMission = $inventoryAccountService->盘点选中任务($id, $count, $inventoryId);
  307. if (!$inventoryAccountMission) return ['success' => false, 'data' => '盘点选中任务失败!'];
  308. /** @var InventoryAccountService $inventoryService */
  309. $inventoryService = app('inventoryAccountService');
  310. $inventoryAccount = $inventoryService->updateInventory($inventoryId);
  311. $stockInventoryPersons = $inventoryAccountMission->stockInventoryPersons;
  312. return ['success' => true, 'inventoryMission' => $inventoryAccountMission, 'inventory' => $inventoryAccount, 'stockInventoryPersons' => $stockInventoryPersons];
  313. }
  314. }
  315. public function 删除盘点记录(Request $request)
  316. {
  317. if (!Gate::allows('库存管理-盘点-删除')) {
  318. return ['success' => false, 'data' => '没有权限'];
  319. }
  320. $inventoryAccountMissionId = $request->input('inventoryAccountMissionId');
  321. $inventoryAccountId = $request->input('inventoryAccountId');
  322. if (is_null($inventoryAccountMissionId)) {
  323. return ['success' => false, 'data' => '传入id为空'];
  324. }
  325. /** @var InventoryAccountService $inventoryService */
  326. $inventoryService = app('inventoryAccountService');
  327. $inventoryAccountMission = $inventoryService->删除盘点记录($inventoryAccountMissionId, $inventoryAccountId);
  328. return ['success' => true, 'data' => $inventoryAccountMission];
  329. }
  330. public function 跳过盘点记录(Request $request)
  331. {
  332. if (!Gate::allows('库存管理-盘点')) {
  333. return ['success' => false, 'data' => '没有权限'];
  334. }
  335. $inventoryAccountMissionId = $request->inventoryAccountMissionId;
  336. $inventoryAccountId = $request->input('inventoryAccountId');
  337. if (is_null($inventoryAccountMissionId)) {
  338. return ['success' => false, 'data' => '传入id为空'];
  339. }
  340. /** @var InventoryAccountService $inventoryService */
  341. $inventoryService = app('inventoryAccountService');
  342. $inventoryAccountMission = $inventoryService->跳过盘点记录($inventoryAccountMissionId, $inventoryAccountId);
  343. return ['success' => true, 'inventoryAccountMission' => $inventoryAccountMission];
  344. }
  345. public function 确认盘点差异(Request $request)
  346. {
  347. if (!Gate::allows('库存管理-盘点')) {
  348. return ['success' => false, 'data' => '没有权限'];
  349. }
  350. $inventoryAccountMissionId = $request->inventoryAccountMissionId;
  351. $inventoryAccountId = $request->input('inventoryAccountId');
  352. if (is_null($inventoryAccountMissionId)) {
  353. return ['success' => false, 'data' => '传入id为空'];
  354. }
  355. /** @var InventoryAccountService $inventoryService */
  356. $inventoryService = app('inventoryAccountService');
  357. $inventoryAccountMission = $inventoryService->确认盘点差异($inventoryAccountMissionId, $inventoryAccountId);
  358. return ['success' => true, 'inventoryAccountMission' => $inventoryAccountMission];
  359. }
  360. public function 批量跳过或确认差异(Request $request)
  361. {
  362. if (!Gate::allows('库存管理-盘点')) {
  363. return ['success' => false, 'data' => '没有权限'];
  364. }
  365. $checkData = $request->checkData;
  366. if (is_null($checkData)) {
  367. return ['success' => false, 'data' => '传入勾选盘点记录为空'];
  368. }
  369. $marks = [];
  370. foreach ($checkData as $inventoryMission) {
  371. array_push($marks, $inventoryMission['mark']);
  372. }
  373. if (in_array('确认差异', $marks) || in_array('跳过', $marks) || in_array('无差异', $marks) || in_array('已复盘无差异', $marks)) return ['success' => false, 'data' => '传入勾选盘点记录存在不可操作项!'];
  374. /** @var InventoryAccountService $inventoryService */
  375. $inventoryService = app('inventoryAccountService');
  376. $inventoryAccountMissions = $inventoryService->批量跳过或确认差异($checkData);
  377. return ['success' => true, 'inventoryAccountMissions' => $inventoryAccountMissions];
  378. }
  379. public function exportInventoryAccountMission(Request $request)
  380. {
  381. if (!Gate::allows("库存管理-盘点")) {
  382. return redirect(url('/'));
  383. }
  384. $post = Http::post(config('go.export.url'), ['type' => 'inventoryAccountMission', 'data' => $request->data]);
  385. if ($post->status() == 500) {
  386. throw new Exception($post->header("Msg"));
  387. }
  388. return response($post, 200, [
  389. "Content-type" => "application/octet-stream",
  390. "Content-Disposition" => "attachment; filename=库存盘点记录-" . date('ymdHis') . '.xlsx',
  391. ]);
  392. }
  393. public function searchCommodityByBarcode(Request $request)
  394. {
  395. if (!Gate::allows('库存管理-盘点')) {
  396. return ['success' => false, 'data' => '没有权限'];
  397. }
  398. $barcode = $request->input('barcode');
  399. $owner_code = $request->input('owner_code');
  400. /** @var InventoryAccountService $inventoryService */
  401. $inventoryService = app('inventoryAccountService');
  402. $commodity = $inventoryService->searchCommodityByBarcode($barcode, $owner_code);
  403. if ($commodity) {
  404. return ['success' => true, 'data' => $commodity];
  405. } else {
  406. return ['success' => false, 'data' => '输入的条码没有对应商品!'];
  407. }
  408. }
  409. //根据库位批量盘点该库位下盘点记录
  410. public function batchStockByLocation(Request $request)
  411. {
  412. if (!Gate::allows('库存管理-盘点')) return ['success' => false, 'msg' => '没有权限'];
  413. $missions=$request->mission;
  414. $inventoryId=$request->inventoryId;
  415. if (!$inventoryId||count($missions)<1) return ['success' => false, 'msg' => '参数错误'];
  416. /** @var InventoryAccountService $inventoryService */
  417. $inventoryService = app('inventoryAccountService');
  418. $inventoryMissions=$inventoryService->batchStockByLocation($missions,$inventoryId);
  419. $inventoryService = app('inventoryAccountService');
  420. $inventoryAccount = $inventoryService->updateInventory($inventoryId);
  421. $stockInventoryPersons = $inventoryMissions[0]->stockInventoryPersons;
  422. return ['success' => true, 'inventoryMission' => $inventoryMissions, 'inventory' => $inventoryAccount, 'stockInventoryPersons' => $stockInventoryPersons];
  423. }
  424. }