|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Models\ScrapEntry; |
| 7 | +use App\Models\WorkOrder; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Validation\Rule; |
| 11 | + |
| 12 | +class ScrapEntryController extends Controller |
| 13 | +{ |
| 14 | + public function index(Request $request): JsonResponse |
| 15 | + { |
| 16 | + $this->authorize('viewAny', ScrapEntry::class); |
| 17 | + |
| 18 | + // Tenant isolation: only entries whose work order is visible under the |
| 19 | + // WorkOrder tenant scope (ScrapEntry itself has no tenant column). |
| 20 | + // No-op for single-tenant installs (tenant scope is inactive there). |
| 21 | + $query = ScrapEntry::query()->whereHas('workOrder')->with(['scrapReason', 'reportedBy', 'workOrder']); |
| 22 | + if ($woId = $request->query('work_order_id')) { |
| 23 | + $query->where('work_order_id', $woId); |
| 24 | + } |
| 25 | + if ($lineId = $request->query('line_id')) { |
| 26 | + $query->whereHas('workOrder', fn ($q) => $q->where('line_id', $lineId)); |
| 27 | + } |
| 28 | + if ($reasonId = $request->query('scrap_reason_id')) { |
| 29 | + $query->where('scrap_reason_id', $reasonId); |
| 30 | + } |
| 31 | + if ($from = $request->query('from')) { |
| 32 | + $query->where('reported_at', '>=', $from); |
| 33 | + } |
| 34 | + if ($to = $request->query('to')) { |
| 35 | + $query->where('reported_at', '<=', $to); |
| 36 | + } |
| 37 | + |
| 38 | + $perPage = max(1, min((int) $request->query('per_page', 30), 100)); |
| 39 | + $page = $query->orderByDesc('reported_at')->paginate($perPage); |
| 40 | + |
| 41 | + return response()->json([ |
| 42 | + 'data' => $page->items(), |
| 43 | + 'meta' => [ |
| 44 | + 'current_page' => $page->currentPage(), |
| 45 | + 'per_page' => $page->perPage(), |
| 46 | + 'total' => $page->total(), |
| 47 | + 'last_page' => $page->lastPage(), |
| 48 | + ], |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * List scrap entries for a single work order, with the work order totals. |
| 54 | + */ |
| 55 | + public function forWorkOrder(WorkOrder $workOrder): JsonResponse |
| 56 | + { |
| 57 | + $this->authorize('viewAny', ScrapEntry::class); |
| 58 | + |
| 59 | + $entries = $workOrder->scrapEntries() |
| 60 | + ->with(['scrapReason', 'reportedBy', 'batchStep', 'shift']) |
| 61 | + ->orderByDesc('reported_at') |
| 62 | + ->get(); |
| 63 | + |
| 64 | + return response()->json([ |
| 65 | + 'data' => $entries, |
| 66 | + 'meta' => [ |
| 67 | + 'work_order_id' => $workOrder->id, |
| 68 | + 'total_scrap_qty' => $workOrder->totalScrapQty(), |
| 69 | + 'quality_pct' => $workOrder->qualityPct(), |
| 70 | + ], |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + public function show(ScrapEntry $scrapEntry): JsonResponse |
| 75 | + { |
| 76 | + $this->authorize('view', $scrapEntry); |
| 77 | + $this->assertTenantVisible($scrapEntry); |
| 78 | + $scrapEntry->load(['scrapReason', 'reportedBy', 'workOrder', 'batchStep', 'shift']); |
| 79 | + |
| 80 | + return response()->json(['data' => $scrapEntry]); |
| 81 | + } |
| 82 | + |
| 83 | + public function store(Request $request, WorkOrder $workOrder): JsonResponse |
| 84 | + { |
| 85 | + $this->authorize('create', ScrapEntry::class); |
| 86 | + $data = $request->validate([ |
| 87 | + 'scrap_reason_id' => ['required', 'integer', Rule::exists('scrap_reasons', 'id')->where('is_active', true)], |
| 88 | + 'quantity' => ['required', 'numeric', 'min:0.01', 'max:99999999'], |
| 89 | + 'batch_step_id' => ['nullable', 'integer', 'exists:batch_steps,id'], |
| 90 | + 'shift_id' => ['nullable', 'integer', 'exists:shifts,id'], |
| 91 | + 'notes' => ['nullable', 'string'], |
| 92 | + 'reported_at' => ['nullable', 'date'], |
| 93 | + ]); |
| 94 | + $data['work_order_id'] = $workOrder->id; |
| 95 | + $data['reported_by'] = $request->user()->id; |
| 96 | + $data['reported_at'] = $data['reported_at'] ?? now(); |
| 97 | + |
| 98 | + $entry = ScrapEntry::create($data); |
| 99 | + |
| 100 | + return response()->json([ |
| 101 | + 'message' => 'Scrap recorded', |
| 102 | + 'data' => $entry->load(['scrapReason', 'reportedBy']), |
| 103 | + ], 201); |
| 104 | + } |
| 105 | + |
| 106 | + public function update(Request $request, ScrapEntry $scrapEntry): JsonResponse |
| 107 | + { |
| 108 | + $this->authorize('update', $scrapEntry); |
| 109 | + $this->assertTenantVisible($scrapEntry); |
| 110 | + $data = $request->validate([ |
| 111 | + 'scrap_reason_id' => ['sometimes', 'integer', Rule::exists('scrap_reasons', 'id')->where('is_active', true)], |
| 112 | + 'quantity' => ['sometimes', 'numeric', 'min:0.01', 'max:99999999'], |
| 113 | + 'batch_step_id' => ['sometimes', 'nullable', 'integer', 'exists:batch_steps,id'], |
| 114 | + 'shift_id' => ['sometimes', 'nullable', 'integer', 'exists:shifts,id'], |
| 115 | + 'notes' => ['sometimes', 'nullable', 'string'], |
| 116 | + ]); |
| 117 | + $scrapEntry->update($data); |
| 118 | + |
| 119 | + return response()->json(['message' => 'Scrap entry updated', 'data' => $scrapEntry->fresh(['scrapReason'])]); |
| 120 | + } |
| 121 | + |
| 122 | + public function destroy(ScrapEntry $scrapEntry): JsonResponse |
| 123 | + { |
| 124 | + $this->authorize('delete', $scrapEntry); |
| 125 | + $this->assertTenantVisible($scrapEntry); |
| 126 | + $scrapEntry->delete(); |
| 127 | + |
| 128 | + return response()->json(['message' => 'Scrap entry deleted']); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Guard against cross-tenant access to a directly-bound scrap entry. |
| 133 | + * |
| 134 | + * ScrapEntry has no tenant column; isolation rides on its work order, |
| 135 | + * which carries the tenant scope. If the entry's work order is not |
| 136 | + * visible to the current tenant, treat the entry as not found. |
| 137 | + * No-op for single-tenant installs (the tenant scope is inactive). |
| 138 | + */ |
| 139 | + private function assertTenantVisible(ScrapEntry $scrapEntry): void |
| 140 | + { |
| 141 | + abort_unless($scrapEntry->workOrder()->exists(), 404); |
| 142 | + } |
| 143 | +} |
0 commit comments