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