|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Modules\Subcontracting\Models\SubcontractOrder; |
| 6 | +use App\Modules\Subcontracting\Models\SubcontractComponent; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | + |
| 10 | +class SubcontractingApiController extends ApiController |
| 11 | +{ |
| 12 | + /** |
| 13 | + * GET /api/v1/subcontracting/orders |
| 14 | + */ |
| 15 | + public function index(Request $request): JsonResponse |
| 16 | + { |
| 17 | + $tenantId = app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 18 | + |
| 19 | + $query = SubcontractOrder::where('tenant_id', $tenantId); |
| 20 | + |
| 21 | + if ($status = $request->query('status')) { |
| 22 | + $query->where('status', $status); |
| 23 | + } |
| 24 | + |
| 25 | + if ($vendorId = $request->query('vendor_id')) { |
| 26 | + $query->where('vendor_id', $vendorId); |
| 27 | + } |
| 28 | + |
| 29 | + $paginator = $query->latest()->paginate(20); |
| 30 | + |
| 31 | + return $this->paginated($paginator); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * GET /api/v1/subcontracting/orders/{id} |
| 36 | + */ |
| 37 | + public function show(int $id): JsonResponse |
| 38 | + { |
| 39 | + $order = SubcontractOrder::with(['components'])->findOrFail($id); |
| 40 | + |
| 41 | + return $this->success($order); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * POST /api/v1/subcontracting/orders |
| 46 | + */ |
| 47 | + public function store(Request $request): JsonResponse |
| 48 | + { |
| 49 | + $validated = $request->validate([ |
| 50 | + 'vendor_id' => 'required|integer', |
| 51 | + 'reference' => 'nullable|string|max:100', |
| 52 | + 'finished_product' => 'required|string|max:255', |
| 53 | + 'finished_qty' => 'required|numeric|min:0', |
| 54 | + 'unit_price' => 'nullable|numeric|min:0', |
| 55 | + 'notes' => 'nullable|string', |
| 56 | + 'components' => 'nullable|array', |
| 57 | + 'components.*.component_name' => 'required_with:components|string|max:255', |
| 58 | + 'components.*.quantity' => 'required_with:components|numeric|min:0', |
| 59 | + 'components.*.unit' => 'nullable|string|max:50', |
| 60 | + ]); |
| 61 | + |
| 62 | + $tenantId = app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 63 | + |
| 64 | + $components = $validated['components'] ?? []; |
| 65 | + unset($validated['components']); |
| 66 | + |
| 67 | + $validated['tenant_id'] = $tenantId; |
| 68 | + $validated['status'] = 'draft'; |
| 69 | + |
| 70 | + $order = SubcontractOrder::create($validated); |
| 71 | + |
| 72 | + foreach ($components as $component) { |
| 73 | + SubcontractComponent::create([ |
| 74 | + 'tenant_id' => $tenantId, |
| 75 | + 'subcontract_id' => $order->id, |
| 76 | + 'component_name' => $component['component_name'], |
| 77 | + 'quantity' => $component['quantity'], |
| 78 | + 'unit' => $component['unit'] ?? null, |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + return $this->success($order->load('components'), 201); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * PUT /api/v1/subcontracting/orders/{id} |
| 87 | + */ |
| 88 | + public function update(Request $request, int $id): JsonResponse |
| 89 | + { |
| 90 | + $order = SubcontractOrder::findOrFail($id); |
| 91 | + |
| 92 | + $validated = $request->validate([ |
| 93 | + 'vendor_id' => 'sometimes|integer', |
| 94 | + 'reference' => 'nullable|string|max:100', |
| 95 | + 'finished_product' => 'sometimes|string|max:255', |
| 96 | + 'finished_qty' => 'sometimes|numeric|min:0', |
| 97 | + 'unit_price' => 'nullable|numeric|min:0', |
| 98 | + 'notes' => 'nullable|string', |
| 99 | + 'status' => 'nullable|string|in:draft,sent,in_progress,done,cancelled', |
| 100 | + 'sent_at' => 'nullable|date', |
| 101 | + 'received_at' => 'nullable|date', |
| 102 | + ]); |
| 103 | + |
| 104 | + $order->update($validated); |
| 105 | + |
| 106 | + return $this->success($order); |
| 107 | + } |
| 108 | +} |
0 commit comments