| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Reservation;
- use App\Models\Shortage;
- use App\Services\SparePartIssueService;
- use App\Services\SparePartReservationService;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- /**
- * Контроллер управления резервами запчастей
- */
- class SparePartReservationController extends Controller
- {
- public function __construct(
- protected SparePartReservationService $reservationService,
- protected SparePartIssueService $issueService
- ) {}
- /**
- * Список резервов для рекламации (API)
- */
- public function forReclamation(int $reclamationId): JsonResponse
- {
- $reservations = $this->reservationService->getReservationsForReclamation($reclamationId);
- return response()->json([
- 'reservations' => $reservations->map(fn($r) => [
- 'id' => $r->id,
- 'spare_part_id' => $r->spare_part_id,
- 'article' => $r->sparePart->article,
- 'used_in_maf' => $r->sparePart->used_in_maf,
- 'reserved_qty' => $r->reserved_qty,
- 'with_documents' => $r->with_documents,
- 'status' => $r->status,
- 'order_id' => $r->spare_part_order_id,
- 'created_at' => $r->created_at->format('d.m.Y H:i'),
- ]),
- ]);
- }
- /**
- * Дефициты для рекламации (API)
- */
- public function shortagesForReclamation(int $reclamationId): JsonResponse
- {
- $shortages = Shortage::query()
- ->where('reclamation_id', $reclamationId)
- ->with('sparePart')
- ->get();
- return response()->json([
- 'shortages' => $shortages->map(fn($s) => [
- 'id' => $s->id,
- 'spare_part_id' => $s->spare_part_id,
- 'article' => $s->sparePart->article,
- 'used_in_maf' => $s->sparePart->used_in_maf,
- 'required_qty' => $s->required_qty,
- 'reserved_qty' => $s->reserved_qty,
- 'missing_qty' => $s->missing_qty,
- 'with_documents' => $s->with_documents,
- 'status' => $s->status,
- 'coverage_percent' => $s->coverage_percent,
- 'created_at' => $s->created_at->format('d.m.Y H:i'),
- ]),
- ]);
- }
- /**
- * Отменить резерв
- */
- public function cancel(Request $request, Reservation $reservation): RedirectResponse
- {
- $reason = $request->input('reason', 'Отмена пользователем');
- if (!$reservation->isActive()) {
- return back()->with(['error' => 'Резерв не активен!']);
- }
- $this->reservationService->cancelReservation($reservation, $reason);
- return back()->with(['success' => 'Резерв отменён!']);
- }
- /**
- * Списать резерв (выполнить отгрузку)
- */
- public function issue(Request $request, Reservation $reservation): RedirectResponse
- {
- $note = $request->input('note', '');
- if (!$reservation->isActive()) {
- return back()->with(['error' => 'Резерв не активен!']);
- }
- try {
- $this->issueService->issueReservation($reservation, $note);
- return back()->with(['success' => 'Списание выполнено!']);
- } catch (\Exception $e) {
- return back()->with(['error' => $e->getMessage()]);
- }
- }
- /**
- * Списать все резервы для рекламации
- */
- public function issueAllForReclamation(Request $request, int $reclamationId): RedirectResponse
- {
- $results = $this->issueService->issueForReclamation($reclamationId);
- $totalIssued = array_sum(array_map(fn($r) => $r->issued, $results));
- return back()->with([
- 'success' => "Списано запчастей: {$totalIssued} шт."
- ]);
- }
- /**
- * Отменить все резервы для рекламации
- */
- public function cancelAllForReclamation(Request $request, int $reclamationId): RedirectResponse
- {
- $reason = $request->input('reason', 'Массовая отмена');
- $totalCancelled = $this->reservationService->cancelForReclamation($reclamationId);
- return back()->with([
- 'success' => "Отменено резервов на {$totalCancelled} шт."
- ]);
- }
- }
|