|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\Inventory\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\Inventory\Models\QualityAlert; |
| 7 | +use Illuminate\Http\RedirectResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Inertia\Response; |
| 11 | + |
| 12 | +class QualityAlertController extends Controller |
| 13 | +{ |
| 14 | + public function index(Request $request): Response |
| 15 | + { |
| 16 | + $this->authorize('viewAny', QualityAlert::class); |
| 17 | + |
| 18 | + $alerts = QualityAlert::with(['product']) |
| 19 | + ->latest() |
| 20 | + ->paginate(20) |
| 21 | + ->withQueryString(); |
| 22 | + |
| 23 | + return Inertia::render('Inventory/QualityAlerts/Index', [ |
| 24 | + 'alerts' => $alerts, |
| 25 | + ]); |
| 26 | + } |
| 27 | + |
| 28 | + public function create(): Response |
| 29 | + { |
| 30 | + $this->authorize('create', QualityAlert::class); |
| 31 | + |
| 32 | + return Inertia::render('Inventory/QualityAlerts/Create'); |
| 33 | + } |
| 34 | + |
| 35 | + public function store(Request $request): RedirectResponse |
| 36 | + { |
| 37 | + $this->authorize('create', QualityAlert::class); |
| 38 | + |
| 39 | + $validated = $request->validate([ |
| 40 | + 'title' => 'required|string', |
| 41 | + 'alert_type' => 'nullable|in:defect,contamination,non-conformance,recall,expiry', |
| 42 | + 'severity' => 'nullable|in:low,medium,high,critical', |
| 43 | + 'product_id' => 'nullable|exists:products,id', |
| 44 | + ]); |
| 45 | + |
| 46 | + QualityAlert::create([ |
| 47 | + 'tenant_id' => app('tenant')->id, |
| 48 | + 'created_by' => auth()->id(), |
| 49 | + 'reported_by' => auth()->id(), |
| 50 | + ...$validated, |
| 51 | + ]); |
| 52 | + |
| 53 | + return redirect()->route('inventory.quality-alerts.index') |
| 54 | + ->with('success', 'Quality alert created.'); |
| 55 | + } |
| 56 | + |
| 57 | + public function show(QualityAlert $qualityAlert): Response |
| 58 | + { |
| 59 | + $this->authorize('view', $qualityAlert); |
| 60 | + |
| 61 | + $qualityAlert->load(['product']); |
| 62 | + |
| 63 | + return Inertia::render('Inventory/QualityAlerts/Show', [ |
| 64 | + 'alert' => $qualityAlert, |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + public function edit(QualityAlert $qualityAlert): Response |
| 69 | + { |
| 70 | + $this->authorize('update', $qualityAlert); |
| 71 | + |
| 72 | + $qualityAlert->load(['product']); |
| 73 | + |
| 74 | + return Inertia::render('Inventory/QualityAlerts/Edit', [ |
| 75 | + 'alert' => $qualityAlert, |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + public function update(Request $request, QualityAlert $qualityAlert): RedirectResponse |
| 80 | + { |
| 81 | + $this->authorize('update', $qualityAlert); |
| 82 | + |
| 83 | + $validated = $request->validate([ |
| 84 | + 'title' => 'required|string', |
| 85 | + 'alert_type' => 'nullable|in:defect,contamination,non-conformance,recall,expiry', |
| 86 | + 'severity' => 'nullable|in:low,medium,high,critical', |
| 87 | + 'product_id' => 'nullable|exists:products,id', |
| 88 | + ]); |
| 89 | + |
| 90 | + $qualityAlert->update($validated); |
| 91 | + |
| 92 | + return redirect()->route('inventory.quality-alerts.index') |
| 93 | + ->with('success', 'Quality alert updated.'); |
| 94 | + } |
| 95 | + |
| 96 | + public function destroy(QualityAlert $qualityAlert): RedirectResponse |
| 97 | + { |
| 98 | + $this->authorize('delete', $qualityAlert); |
| 99 | + |
| 100 | + $qualityAlert->delete(); |
| 101 | + |
| 102 | + return redirect()->route('inventory.quality-alerts.index') |
| 103 | + ->with('success', 'Quality alert deleted.'); |
| 104 | + } |
| 105 | + |
| 106 | + public function investigate(QualityAlert $qualityAlert): RedirectResponse |
| 107 | + { |
| 108 | + $this->authorize('investigate', $qualityAlert); |
| 109 | + |
| 110 | + $qualityAlert->investigate(); |
| 111 | + |
| 112 | + return redirect()->route('inventory.quality-alerts.index') |
| 113 | + ->with('success', 'Quality alert is now under investigation.'); |
| 114 | + } |
| 115 | + |
| 116 | + public function resolve(Request $request, QualityAlert $qualityAlert): RedirectResponse |
| 117 | + { |
| 118 | + $this->authorize('resolve', $qualityAlert); |
| 119 | + |
| 120 | + $data = $request->validate([ |
| 121 | + 'root_cause' => 'required|string', |
| 122 | + 'corrective_action' => 'required|string', |
| 123 | + ]); |
| 124 | + |
| 125 | + $qualityAlert->resolve($data['root_cause'], $data['corrective_action']); |
| 126 | + |
| 127 | + return redirect()->route('inventory.quality-alerts.index') |
| 128 | + ->with('success', 'Quality alert resolved.'); |
| 129 | + } |
| 130 | + |
| 131 | + public function close(QualityAlert $qualityAlert): RedirectResponse |
| 132 | + { |
| 133 | + $this->authorize('close', $qualityAlert); |
| 134 | + |
| 135 | + $qualityAlert->close(); |
| 136 | + |
| 137 | + return redirect()->route('inventory.quality-alerts.index') |
| 138 | + ->with('success', 'Quality alert closed.'); |
| 139 | + } |
| 140 | +} |
0 commit comments