ReclamationController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\CreateReclamationRequest;
  4. use App\Http\Requests\StoreReclamationDetailsRequest;
  5. use App\Http\Requests\StoreReclamationRequest;
  6. use App\Http\Requests\StoreReclamationSparePartsRequest;
  7. use App\Jobs\ExportReclamationsJob;
  8. use App\Jobs\GenerateFilesPack;
  9. use App\Jobs\GenerateReclamationPaymentPack;
  10. use App\Jobs\GenerateReclamationPack;
  11. use App\Models\File;
  12. use App\Models\Order;
  13. use App\Models\Reclamation;
  14. use App\Models\ReclamationDetail;
  15. use App\Models\ReclamationStatus;
  16. use App\Models\ReclamationView;
  17. use App\Models\Role;
  18. use App\Models\User;
  19. use App\Services\FileService;
  20. use App\Services\NotificationService;
  21. use App\Services\SparePartReservationService;
  22. use Illuminate\Http\Request;
  23. use Illuminate\Support\Carbon;
  24. use Illuminate\Support\Facades\Storage;
  25. use Throwable;
  26. class ReclamationController extends Controller
  27. {
  28. protected array $data = [
  29. 'active' => 'reclamations',
  30. 'title' => 'Рекламации',
  31. 'id' => 'reclamations',
  32. 'header' => [
  33. 'id' => 'ID',
  34. 'user_name' => 'Менеджер',
  35. 'status_name' => 'Статус',
  36. 'district_name' => 'Округ',
  37. 'area_name' => 'Район',
  38. 'object_address' => 'Адрес объекта',
  39. 'maf_installation_year' => 'Год установки МАФ',
  40. 'create_date' => 'Дата создания',
  41. 'finish_date' => 'Дата завершения',
  42. 'start_work_date' => 'Дата начала работ',
  43. 'work_days' => 'Срок работ, дней',
  44. 'brigadier_name' => 'Бригадир',
  45. 'reason' => 'Причина',
  46. 'guarantee' => 'Гарантии',
  47. 'whats_done' => 'Что сделано',
  48. 'comment' => 'Комментарий',
  49. ],
  50. 'searchFields' => [
  51. 'reason',
  52. 'guarantee',
  53. 'whats_done',
  54. 'comment',
  55. ],
  56. 'ranges' => [],
  57. ];
  58. public function __construct()
  59. {
  60. $this->data['users'] = User::query()->whereIn('role', [Role::MANAGER, Role::ADMIN])->get()->pluck('name', 'id');
  61. $this->data['statuses'] = ReclamationStatus::query()->get()->pluck('name', 'id');
  62. }
  63. public function index(Request $request)
  64. {
  65. session(['gp_reclamations' => $request->all()]);
  66. $nav = $this->startNavigationContext($request);
  67. $model = new ReclamationView();
  68. // fill filters
  69. $this->createFilters($model, 'user_name', 'status_name');
  70. $this->createDateFilters($model, 'create_date', 'finish_date');
  71. $q = $model::query();
  72. $this->acceptFilters($q, $request);
  73. $this->acceptSearch($q, $request);
  74. $this->setSortAndOrderBy($model, $request);
  75. if (hasRole(Role::BRIGADIER)) {
  76. $q->where('brigadier_id', auth()->id())
  77. ->whereIn('status_id', Reclamation::visibleStatusIdsForBrigadier());
  78. } elseif (hasRole(Role::WAREHOUSE_HEAD)) {
  79. $q->whereNotNull('brigadier_id')
  80. ->whereIn('status_id', Reclamation::visibleStatusIdsForBrigadier());
  81. }
  82. $this->applyStableSorting($q);
  83. $this->data['reclamations'] = $q->paginate($this->data['per_page'])->withQueryString();
  84. $this->data['nav'] = $nav;
  85. return view('reclamations.index', $this->data);
  86. }
  87. public function export(Request $request)
  88. {
  89. $request->validate([
  90. 'withFilter' => 'nullable',
  91. 'filters' => 'nullable|array',
  92. 's' => 'nullable|string',
  93. ]);
  94. $filterRequest = $request->boolean('withFilter')
  95. ? new Request(array_filter([
  96. 'filters' => $request->input('filters', []),
  97. 's' => $request->input('s'),
  98. ], static fn ($value) => $value !== null))
  99. : new Request();
  100. $model = new ReclamationView();
  101. $this->createFilters($model, 'user_name', 'status_name');
  102. $this->createDateFilters($model, 'create_date', 'finish_date');
  103. $q = $model::query();
  104. $this->acceptFilters($q, $filterRequest);
  105. $this->acceptSearch($q, $filterRequest);
  106. $this->setSortAndOrderBy($model, $filterRequest);
  107. $this->applyStableSorting($q);
  108. $reclamationIds = $q->pluck('id')->toArray();
  109. ExportReclamationsJob::dispatch($reclamationIds, $request->user()->id);
  110. return redirect()->route('reclamations.index', session('gp_reclamations'))
  111. ->with(['success' => 'Задача экспорта рекламаций создана!']);
  112. }
  113. public function create(CreateReclamationRequest $request, Order $order, NotificationService $notificationService)
  114. {
  115. $nav = $this->resolveNavToken($request);
  116. $reclamation = Reclamation::query()->create([
  117. 'order_id' => $order->id,
  118. 'user_id' => $request->user()->id,
  119. 'status_id' => Reclamation::STATUS_NEW,
  120. 'create_date' => Carbon::now(),
  121. 'finish_date' => Carbon::now()->addDays(30),
  122. ]);
  123. $skus = $request->validated('skus');
  124. $reclamation->skus()->attach($skus);
  125. $notificationService->notifyReclamationCreated($reclamation->fresh(['order', 'status']), auth()->user());
  126. return redirect()->route('reclamations.show', $this->withNav(['reclamation' => $reclamation], $nav));
  127. }
  128. public function show(Request $request, Reclamation $reclamation)
  129. {
  130. $this->ensureCanViewReclamation($reclamation);
  131. $this->data['brigadiers'] = User::query()->where('role', Role::BRIGADIER)->get()->pluck('name', 'id');
  132. $this->data['reclamation'] = $reclamation->load([
  133. 'order',
  134. 'chatMessages.user',
  135. 'chatMessages.targetUser',
  136. 'chatMessages.notifiedUsers',
  137. 'chatMessages.files',
  138. ]);
  139. $chatUsers = User::query()->orderBy('name')->get(['id', 'name', 'role']);
  140. $responsibleUserIds = User::query()
  141. ->where('role', Role::ADMIN)
  142. ->pluck('id')
  143. ->map(static fn ($id) => (int) $id)
  144. ->all();
  145. $responsibleUserIds = array_values(array_unique(array_filter(array_merge($responsibleUserIds, [
  146. $reclamation->user_id ? (int) $reclamation->user_id : null,
  147. $reclamation->brigadier_id ? (int) $reclamation->brigadier_id : null,
  148. ]))));
  149. $this->data['chatUsers'] = $chatUsers->map(fn ($u) => [
  150. 'id' => $u->id,
  151. 'name' => $u->name,
  152. 'role' => $u->role,
  153. ])->keyBy('id');
  154. $this->data['chatResponsibleUserIds'] = $responsibleUserIds;
  155. $this->data['chatManagerUserId'] = $reclamation->user_id ? (int) $reclamation->user_id : null;
  156. $this->data['chatBrigadierUserId'] = $reclamation->brigadier_id ? (int) $reclamation->brigadier_id : null;
  157. $nav = $this->resolveNavToken($request);
  158. $this->rememberNavigation($request, $nav);
  159. $this->data['nav'] = $nav;
  160. $this->data['back_url'] = $this->navigationBackUrl(
  161. $request,
  162. $nav,
  163. route('reclamations.index', session('gp_reclamations'))
  164. );
  165. return view('reclamations.edit', $this->data);
  166. }
  167. public function update(StoreReclamationRequest $request, Reclamation $reclamation, NotificationService $notificationService)
  168. {
  169. $data = $request->validated();
  170. $oldStatusId = $reclamation->status_id;
  171. $reclamation->update($data);
  172. if ((int) $oldStatusId !== (int) $reclamation->status_id) {
  173. $notificationService->notifyReclamationStatusChanged($reclamation->fresh(['order', 'status']), auth()->user());
  174. }
  175. $nav = $this->resolveNavToken($request);
  176. if ($request->ajax()) {
  177. return response()->noContent();
  178. }
  179. return redirect()->route('reclamations.show', $this->withNav(['reclamation' => $reclamation], $nav));
  180. }
  181. public function updateStatus(Request $request, Reclamation $reclamation, NotificationService $notificationService)
  182. {
  183. if (!hasRole('admin,manager')) {
  184. abort(403);
  185. }
  186. $validated = $request->validate([
  187. 'status_id' => 'required|exists:reclamation_statuses,id',
  188. ]);
  189. $reclamation->update(['status_id' => $validated['status_id']]);
  190. $notificationService->notifyReclamationStatusChanged($reclamation->fresh(['order', 'status']), auth()->user());
  191. return response()->noContent();
  192. }
  193. public function delete(Reclamation $reclamation)
  194. {
  195. $reclamation->delete();
  196. return redirect()->route('reclamations.index');
  197. }
  198. public function uploadPhotoBefore(Request $request, Reclamation $reclamation, FileService $fileService)
  199. {
  200. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  201. $this->ensureCanViewReclamation($reclamation);
  202. $data = $request->validate([
  203. 'photo.*' => 'mimes:jpeg,jpg,png,webp|max:8192',
  204. ]);
  205. try {
  206. $f = [];
  207. foreach ($data['photo'] as $photo) {
  208. $f[] = $fileService->saveUploadedFile('reclamations/' . $reclamation->id . '/photo_before', $photo);
  209. }
  210. $reclamation->photos_before()->syncWithoutDetaching($f);
  211. } catch (Throwable $e) {
  212. report($e);
  213. return $this->redirectToReclamationShow($request, $reclamation)
  214. ->with(['error' => 'Ошибка загрузки фотографий проблемы. Проверьте имя файла и повторите попытку.']);
  215. }
  216. return $this->redirectToReclamationShow($request, $reclamation)
  217. ->with(['success' => 'Фотографии проблемы успешно загружены!']);
  218. }
  219. public function uploadPhotoAfter(Request $request, Reclamation $reclamation, FileService $fileService)
  220. {
  221. $this->ensureCanViewReclamation($reclamation);
  222. $data = $request->validate([
  223. 'photo.*' => 'mimes:jpeg,jpg,png,webp|max:8192',
  224. ]);
  225. try {
  226. $f = [];
  227. foreach ($data['photo'] as $photo) {
  228. $f[] = $fileService->saveUploadedFile('reclamations/' . $reclamation->id . '/photo_after', $photo);
  229. }
  230. $reclamation->photos_after()->syncWithoutDetaching($f);
  231. } catch (Throwable $e) {
  232. report($e);
  233. return $this->redirectToReclamationShow($request, $reclamation)
  234. ->with(['error' => 'Ошибка загрузки фотографий после устранения. Проверьте имя файла и повторите попытку.']);
  235. }
  236. return $this->redirectToReclamationShow($request, $reclamation)
  237. ->with(['success' => 'Фотографии после устранения успешно загружены!']);
  238. }
  239. public function deletePhotoBefore(Request $request, Reclamation $reclamation, File $file, FileService $fileService)
  240. {
  241. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  242. $this->ensureCanViewReclamation($reclamation);
  243. $reclamation->photos_before()->detach($file);
  244. Storage::disk('public')->delete($file->path);
  245. $file->delete();
  246. return $this->redirectToReclamationShow($request, $reclamation);
  247. }
  248. public function deletePhotoAfter(Request $request, Reclamation $reclamation, File $file, FileService $fileService)
  249. {
  250. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  251. $this->ensureCanViewReclamation($reclamation);
  252. $reclamation->photos_after()->detach($file);
  253. Storage::disk('public')->delete($file->path);
  254. $file->delete();
  255. return $this->redirectToReclamationShow($request, $reclamation);
  256. }
  257. public function uploadDocument(Request $request, Reclamation $reclamation, FileService $fileService)
  258. {
  259. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  260. $this->ensureCanViewReclamation($reclamation);
  261. $data = $request->validate([
  262. 'document.*' => 'file',
  263. ]);
  264. try {
  265. $f = [];
  266. $i = 0;
  267. foreach ($data['document'] as $document) {
  268. if ($i++ >= 5) break;
  269. $f[] = $fileService->saveUploadedFile('reclamations/' . $reclamation->id . '/document', $document);
  270. }
  271. $reclamation->documents()->syncWithoutDetaching($f);
  272. } catch (Throwable $e) {
  273. report($e);
  274. return $this->redirectToReclamationShow($request, $reclamation)
  275. ->with(['error' => 'Ошибка загрузки документов рекламации. Проверьте имя файла и повторите попытку.']);
  276. }
  277. return $this->redirectToReclamationShow($request, $reclamation)
  278. ->with(['success' => 'Документы рекламации успешно загружены!']);
  279. }
  280. public function deleteDocument(Request $request, Reclamation $reclamation, File $file)
  281. {
  282. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  283. $this->ensureCanViewReclamation($reclamation);
  284. $reclamation->documents()->detach($file);
  285. Storage::disk('public')->delete($file->path);
  286. $file->delete();
  287. return $this->redirectToReclamationShow($request, $reclamation);
  288. }
  289. public function uploadAct(Request $request, Reclamation $reclamation, FileService $fileService)
  290. {
  291. $this->ensureHasRole([Role::ADMIN, Role::MANAGER, Role::BRIGADIER, Role::WAREHOUSE_HEAD]);
  292. $this->ensureCanViewReclamation($reclamation);
  293. $data = $request->validate([
  294. 'acts.*' => 'file',
  295. ]);
  296. try {
  297. $f = [];
  298. $i = 0;
  299. foreach ($data['acts'] as $document) {
  300. if ($i++ >= 5) break;
  301. $f[] = $fileService->saveUploadedFile('reclamations/' . $reclamation->id . '/act', $document);
  302. }
  303. $reclamation->acts()->syncWithoutDetaching($f);
  304. } catch (Throwable $e) {
  305. report($e);
  306. return $this->redirectToReclamationShow($request, $reclamation)
  307. ->with(['error' => 'Ошибка загрузки актов. Проверьте имя файла и повторите попытку.']);
  308. }
  309. return $this->redirectToReclamationShow($request, $reclamation)
  310. ->with(['success' => 'Акты успешно загружены!']);
  311. }
  312. public function deleteAct(Request $request, Reclamation $reclamation, File $file)
  313. {
  314. $this->ensureHasRole([Role::ADMIN, Role::MANAGER]);
  315. $this->ensureCanViewReclamation($reclamation);
  316. $reclamation->acts()->detach($file);
  317. Storage::disk('public')->delete($file->path);
  318. $file->delete();
  319. return $this->redirectToReclamationShow($request, $reclamation);
  320. }
  321. public function updateDetails(StoreReclamationDetailsRequest $request, Reclamation $reclamation)
  322. {
  323. $names = $request->validated('name');
  324. $quantity = $request->validated('quantity');
  325. $withDocuments = $request->validated('with_documents');
  326. $reservationService = app(SparePartReservationService::class);
  327. foreach ($names as $key => $name) {
  328. if (!$name) continue;
  329. if ((int)$quantity[$key] >= 1) {
  330. // Проверяем, является ли это запчастью
  331. $sparePart = \App\Models\SparePart::where('article', $name)->first();
  332. if ($sparePart) {
  333. // Резервирование вместо прямого списания
  334. $withDocs = isset($withDocuments[$key]) && $withDocuments[$key];
  335. $qty = (int)$quantity[$key];
  336. // Получаем текущее количество в pivot
  337. $currentPivot = $reclamation->spareParts()->find($sparePart->id);
  338. $currentQty = $currentPivot?->pivot->quantity ?? 0;
  339. $diff = $qty - $currentQty;
  340. if ($diff > 0) {
  341. // Нужно зарезервировать дополнительное количество
  342. $result = $reservationService->reserve(
  343. $sparePart->id,
  344. $diff,
  345. $withDocs,
  346. $reclamation->id
  347. );
  348. // Обновляем pivot с учётом результата
  349. $reclamation->spareParts()->syncWithoutDetaching([
  350. $sparePart->id => [
  351. 'quantity' => $qty,
  352. 'with_documents' => $withDocs,
  353. 'status' => $result->isFullyReserved() ? 'reserved' : 'pending',
  354. 'reserved_qty' => $currentQty + $result->reserved,
  355. ]
  356. ]);
  357. } elseif ($diff < 0) {
  358. // Уменьшение — отменяем часть резерва
  359. $reservationService->adjustReservation(
  360. $reclamation->id,
  361. $sparePart->id,
  362. $withDocs,
  363. $qty
  364. );
  365. $reclamation->spareParts()->syncWithoutDetaching([
  366. $sparePart->id => [
  367. 'quantity' => $qty,
  368. 'with_documents' => $withDocs,
  369. 'reserved_qty' => $qty,
  370. ]
  371. ]);
  372. } else {
  373. // Количество не изменилось, возможно изменился with_documents
  374. $reclamation->spareParts()->syncWithoutDetaching([
  375. $sparePart->id => [
  376. 'quantity' => $qty,
  377. 'with_documents' => $withDocs,
  378. ]
  379. ]);
  380. }
  381. } else {
  382. // Обычная деталь
  383. ReclamationDetail::query()->updateOrCreate(
  384. ['reclamation_id' => $reclamation->id, 'name' => $name],
  385. ['quantity' => $quantity[$key]]
  386. );
  387. }
  388. } else {
  389. // Удаление
  390. // Проверяем, является ли это запчастью — отменяем резервы
  391. $sparePartToRemove = \App\Models\SparePart::where('article', $name)->first();
  392. if ($sparePartToRemove) {
  393. // Отменяем все резервы для этой запчасти в рекламации
  394. $reservationService->cancelForReclamation(
  395. $reclamation->id,
  396. $sparePartToRemove->id
  397. );
  398. // Удаляем связь
  399. $reclamation->spareParts()->detach($sparePartToRemove->id);
  400. } else {
  401. // Обычная деталь
  402. ReclamationDetail::query()
  403. ->where('reclamation_id', $reclamation->id)
  404. ->where('name', $name)
  405. ->delete();
  406. }
  407. }
  408. }
  409. return $this->redirectToReclamationShow($request, $reclamation);
  410. }
  411. public function updateSpareParts(StoreReclamationSparePartsRequest $request, Reclamation $reclamation)
  412. {
  413. $rows = $request->validated('rows') ?? [];
  414. $reservationService = app(SparePartReservationService::class);
  415. // Получаем текущие привязки для сравнения
  416. $currentSpareParts = $reclamation->spareParts->keyBy('id');
  417. // Определяем какие запчасти были удалены
  418. $newSparePartIds = collect($rows)->pluck('spare_part_id')->filter()->toArray();
  419. $removedIds = $currentSpareParts->keys()->diff($newSparePartIds);
  420. // Отменяем резервы для удалённых запчастей
  421. foreach ($removedIds as $removedId) {
  422. $current = $currentSpareParts->get($removedId);
  423. if ($current) {
  424. $reservationService->cancelForReclamation(
  425. $reclamation->id,
  426. $removedId,
  427. $current->pivot->with_documents
  428. );
  429. }
  430. }
  431. // Собираем новые привязки
  432. $newSpareParts = [];
  433. foreach ($rows as $row) {
  434. $sparePartId = $row['spare_part_id'] ?? null;
  435. if (empty($sparePartId)) continue;
  436. $quantity = (int)($row['quantity'] ?? 0);
  437. if ($quantity < 1) continue;
  438. $withDocs = !empty($row['with_documents']) && $row['with_documents'] != '0';
  439. // Проверяем, изменилось ли количество
  440. $currentQty = $currentSpareParts->get($sparePartId)?->pivot->quantity ?? 0;
  441. $currentReserved = $currentSpareParts->get($sparePartId)?->pivot->reserved_qty ?? 0;
  442. $diff = $quantity - $currentQty;
  443. $status = 'pending';
  444. $reservedQty = $currentReserved;
  445. if ($diff > 0) {
  446. // Нужно зарезервировать дополнительное количество
  447. $result = $reservationService->reserve(
  448. $sparePartId,
  449. $diff,
  450. $withDocs,
  451. $reclamation->id
  452. );
  453. $reservedQty = $currentReserved + $result->reserved;
  454. $status = $reservedQty >= $quantity ? 'reserved' : 'pending';
  455. } elseif ($diff < 0) {
  456. // Уменьшение — отменяем часть резерва
  457. $reservationService->adjustReservation(
  458. $reclamation->id,
  459. $sparePartId,
  460. $withDocs,
  461. $quantity
  462. );
  463. $reservedQty = $quantity;
  464. $status = 'reserved';
  465. } else {
  466. // Количество не изменилось
  467. $status = $currentReserved >= $quantity ? 'reserved' : 'pending';
  468. }
  469. $newSpareParts[$sparePartId] = [
  470. 'quantity' => $quantity,
  471. 'with_documents' => $withDocs,
  472. 'status' => $status,
  473. 'reserved_qty' => $reservedQty,
  474. ];
  475. }
  476. // Синхронизируем (заменяем все старые привязки новыми)
  477. $reclamation->spareParts()->sync($newSpareParts);
  478. return $this->redirectToReclamationShow($request, $reclamation);
  479. }
  480. public function generateReclamationPack(Request $request, Reclamation $reclamation)
  481. {
  482. GenerateReclamationPack::dispatch($reclamation, auth()->user()->id);
  483. return $this->redirectToReclamationShow($request, $reclamation)
  484. ->with(['success' => 'Задача генерации документов создана!']);
  485. }
  486. public function generateReclamationPaymentPack(Request $request, Reclamation $reclamation)
  487. {
  488. GenerateReclamationPaymentPack::dispatch($reclamation, auth()->user()->id);
  489. return $this->redirectToReclamationShow($request, $reclamation)
  490. ->with(['success' => 'Задача генерации пакета документов на оплату создана!']);
  491. }
  492. public function generatePhotosBeforePack(Request $request, Reclamation $reclamation)
  493. {
  494. GenerateFilesPack::dispatch($reclamation, $reclamation->photos_before, auth()->user()->id, 'Фото проблемы');
  495. return $this->redirectToReclamationShow($request, $reclamation)
  496. ->with(['success' => 'Задача архивации создана!']);
  497. }
  498. public function generatePhotosAfterPack(Request $request, Reclamation $reclamation)
  499. {
  500. GenerateFilesPack::dispatch($reclamation, $reclamation->photos_after, auth()->user()->id, 'Фото после');
  501. return $this->redirectToReclamationShow($request, $reclamation)
  502. ->with(['success' => 'Задача архивации создана!']);
  503. }
  504. private function ensureCanViewReclamation(Reclamation $reclamation): void
  505. {
  506. if (hasRole(Role::BRIGADIER)) {
  507. $canView = (int)$reclamation->brigadier_id === (int)auth()->id()
  508. && in_array((int)$reclamation->status_id, Reclamation::visibleStatusIdsForBrigadier(), true);
  509. if (!$canView) {
  510. abort(403);
  511. }
  512. }
  513. if (hasRole(Role::WAREHOUSE_HEAD)) {
  514. $canView = $reclamation->brigadier_id !== null
  515. && in_array((int)$reclamation->status_id, Reclamation::visibleStatusIdsForBrigadier(), true);
  516. if (!$canView) {
  517. abort(403);
  518. }
  519. }
  520. }
  521. private function ensureHasRole(array $roles): void
  522. {
  523. if (!count(array_intersect($roles, Role::effectiveRoles((string)auth()->user()?->role)))) {
  524. abort(403);
  525. }
  526. }
  527. private function redirectToReclamationShow(Request $request, Reclamation $reclamation)
  528. {
  529. $nav = $this->resolveNavToken($request);
  530. return redirect()->route('reclamations.show', $this->withNav(['reclamation' => $reclamation], $nav));
  531. }
  532. }