|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Modules\Inventory\Models\Product; |
| 6 | +use App\Modules\Inventory\Models\ProductAttribute; |
| 7 | +use App\Modules\Inventory\Models\ProductVariant; |
| 8 | +use App\Modules\Inventory\Models\ProductVariantValue; |
| 9 | +use Illuminate\Http\JsonResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | + |
| 12 | +class ProductVariantController extends ApiController |
| 13 | +{ |
| 14 | + // ── Attributes ──────────────────────────────────────────────── |
| 15 | + |
| 16 | + public function indexAttributes(Request $request): JsonResponse |
| 17 | + { |
| 18 | + $tenantId = $this->tenantId($request); |
| 19 | + $attributes = ProductAttribute::where('tenant_id', $tenantId)->get(); |
| 20 | + return $this->success($attributes); |
| 21 | + } |
| 22 | + |
| 23 | + public function storeAttribute(Request $request): JsonResponse |
| 24 | + { |
| 25 | + $tenantId = $this->tenantId($request); |
| 26 | + |
| 27 | + $data = $request->validate([ |
| 28 | + 'name' => ['required', 'string', 'max:100'], |
| 29 | + 'type' => ['required', 'in:text,select,color,size'], |
| 30 | + 'options' => ['nullable', 'array'], |
| 31 | + ]); |
| 32 | + |
| 33 | + $attribute = ProductAttribute::create([...$data, 'tenant_id' => $tenantId]); |
| 34 | + return $this->success($attribute, 201); |
| 35 | + } |
| 36 | + |
| 37 | + public function updateAttribute(Request $request, ProductAttribute $productAttribute): JsonResponse |
| 38 | + { |
| 39 | + $data = $request->validate([ |
| 40 | + 'name' => ['sometimes', 'string', 'max:100'], |
| 41 | + 'type' => ['sometimes', 'in:text,select,color,size'], |
| 42 | + 'options' => ['nullable', 'array'], |
| 43 | + ]); |
| 44 | + |
| 45 | + $productAttribute->update($data); |
| 46 | + return $this->success($productAttribute->fresh()); |
| 47 | + } |
| 48 | + |
| 49 | + public function destroyAttribute(ProductAttribute $productAttribute): JsonResponse |
| 50 | + { |
| 51 | + $productAttribute->delete(); |
| 52 | + return $this->success(['message' => 'Attribute deleted.']); |
| 53 | + } |
| 54 | + |
| 55 | + // ── Variants ───────────────────────────────────────────────── |
| 56 | + |
| 57 | + public function index(Request $request, Product $product): JsonResponse |
| 58 | + { |
| 59 | + $variants = $product->variants()->with('values.attribute')->get(); |
| 60 | + return $this->success($variants); |
| 61 | + } |
| 62 | + |
| 63 | + public function store(Request $request, Product $product): JsonResponse |
| 64 | + { |
| 65 | + $tenantId = $this->tenantId($request); |
| 66 | + |
| 67 | + $data = $request->validate([ |
| 68 | + 'name' => ['required', 'string', 'max:255'], |
| 69 | + 'sku' => ['required', 'string', 'unique:product_variants,sku'], |
| 70 | + 'price_adjustment' => ['nullable', 'numeric'], |
| 71 | + 'stock_quantity' => ['nullable', 'integer', 'min:0'], |
| 72 | + 'attributes' => ['nullable', 'array'], |
| 73 | + 'attributes.*.attribute_id' => ['required', 'exists:product_attributes,id'], |
| 74 | + 'attributes.*.value' => ['required', 'string'], |
| 75 | + ]); |
| 76 | + |
| 77 | + $variant = ProductVariant::create([ |
| 78 | + 'tenant_id' => $tenantId, |
| 79 | + 'product_id' => $product->id, |
| 80 | + 'name' => $data['name'], |
| 81 | + 'sku' => $data['sku'], |
| 82 | + 'price_adjustment' => $data['price_adjustment'] ?? 0, |
| 83 | + 'stock_quantity' => $data['stock_quantity'] ?? 0, |
| 84 | + ]); |
| 85 | + |
| 86 | + foreach ($data['attributes'] ?? [] as $attr) { |
| 87 | + ProductVariantValue::create([ |
| 88 | + 'tenant_id' => $tenantId, |
| 89 | + 'variant_id' => $variant->id, |
| 90 | + 'attribute_id' => $attr['attribute_id'], |
| 91 | + 'value' => $attr['value'], |
| 92 | + ]); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->success($variant->load('values.attribute'), 201); |
| 96 | + } |
| 97 | + |
| 98 | + public function update(Request $request, Product $product, ProductVariant $variant): JsonResponse |
| 99 | + { |
| 100 | + $data = $request->validate([ |
| 101 | + 'name' => ['sometimes', 'string', 'max:255'], |
| 102 | + 'price_adjustment' => ['sometimes', 'numeric'], |
| 103 | + 'stock_quantity' => ['sometimes', 'integer', 'min:0'], |
| 104 | + 'is_active' => ['sometimes', 'boolean'], |
| 105 | + ]); |
| 106 | + |
| 107 | + $variant->update($data); |
| 108 | + return $this->success($variant->fresh()->load('values.attribute')); |
| 109 | + } |
| 110 | + |
| 111 | + public function destroy(Product $product, ProductVariant $variant): JsonResponse |
| 112 | + { |
| 113 | + $variant->delete(); |
| 114 | + return $this->success(['message' => 'Variant deleted.']); |
| 115 | + } |
| 116 | + |
| 117 | + public function matrix(Request $request, Product $product): JsonResponse |
| 118 | + { |
| 119 | + $variants = $product->variants()->with('values.attribute')->active()->get(); |
| 120 | + $attributes = $variants->flatMap(fn ($v) => $v->values)->pluck('attribute')->unique('id')->values(); |
| 121 | + |
| 122 | + return $this->success([ |
| 123 | + 'product' => $product->only(['id', 'name', 'sale_price']), |
| 124 | + 'attributes' => $attributes, |
| 125 | + 'variants' => $variants->map(fn ($v) => [ |
| 126 | + 'id' => $v->id, |
| 127 | + 'name' => $v->name, |
| 128 | + 'sku' => $v->sku, |
| 129 | + 'price_adjustment' => $v->price_adjustment, |
| 130 | + 'effective_price' => $v->effective_price, |
| 131 | + 'stock_quantity' => $v->stock_quantity, |
| 132 | + 'is_active' => $v->is_active, |
| 133 | + 'attributes' => $v->values->mapWithKeys(fn ($val) => [$val->attribute?->name => $val->value]), |
| 134 | + ]), |
| 135 | + ]); |
| 136 | + } |
| 137 | + |
| 138 | + private function tenantId(Request $request): int |
| 139 | + { |
| 140 | + return app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 141 | + } |
| 142 | +} |
0 commit comments