feat: refactor product variant handling to use product pricing and remove unnecessary fields

This commit is contained in:
2026-06-30 15:58:19 -03:00
parent 8b10996319
commit fdeba7540b
10 changed files with 5 additions and 56 deletions

View File

@@ -22,8 +22,8 @@ class CartItemResource extends JsonResource
return [ return [
'id' => $this->id, 'id' => $this->id,
'cantidad' => $this->cantidad, 'cantidad' => $this->cantidad,
'precio_unitario' => $this->formatMoney($variant?->precio), 'precio_unitario' => $this->formatMoney($product?->precio),
'subtotal' => $this->formatMoney(($variant?->precio ?? 0) * $this->cantidad), 'subtotal' => $this->formatMoney(($product?->precio ?? 0) * $this->cantidad),
'product' => $product === null ? null : [ 'product' => $product === null ? null : [
'id' => $product->id, 'id' => $product->id,
'nombre' => $product->nombre, 'nombre' => $product->nombre,
@@ -31,8 +31,6 @@ class CartItemResource extends JsonResource
], ],
'variant' => $variant === null ? null : [ 'variant' => $variant === null ? null : [
'id' => $variant->id, 'id' => $variant->id,
'nombre' => $variant->nombre,
'slug' => $variant->slug,
'definitions' => ProductVariantDefinitionResource::collection($variant->definitions), 'definitions' => ProductVariantDefinitionResource::collection($variant->definitions),
], ],
]; ];

View File

@@ -20,7 +20,7 @@ class CartResource extends JsonResource
: collect(); : collect();
$subtotal = $items->reduce( $subtotal = $items->reduce(
fn (float $carry, $item): float => $carry + ((float) ($item->variant?->precio ?? 0) * $item->cantidad), fn (float $carry, $item): float => $carry + ((float) ($item->variant?->product?->precio ?? 0) * $item->cantidad),
0.0, 0.0,
); );

View File

@@ -12,11 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable([ #[Fillable([
'producto_id', 'producto_id',
'slug',
'nombre',
'stock', 'stock',
'descripcion',
'precio',
])] ])]
class ProductVariant extends Model class ProductVariant extends Model
{ {
@@ -29,7 +25,6 @@ class ProductVariant extends Model
return [ return [
'producto_id' => 'integer', 'producto_id' => 'integer',
'stock' => 'integer', 'stock' => 'integer',
'precio' => 'decimal:2',
]; ];
} }

View File

@@ -18,22 +18,8 @@ class StoreProductVariantRequest extends FormRequest
*/ */
public function rules(): array public function rules(): array
{ {
/** @var \App\Domains\Catalog\Models\Product|null $product */
$product = $this->route('producto');
return [ return [
'slug' => [
'nullable',
'string',
'max:255',
Rule::unique('productos_variantes', 'slug')->where(
fn ($query) => $query->where('producto_id', $product?->id)
),
],
'nombre' => ['nullable', 'string', 'max:255'],
'stock' => ['sometimes', 'integer', 'min:0'], 'stock' => ['sometimes', 'integer', 'min:0'],
'descripcion' => ['nullable', 'string'],
'precio' => ['required', 'numeric', 'min:0'],
'definitions' => ['sometimes', 'array'], 'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [ 'definitions.*.attribute_id' => [
'required', 'required',

View File

@@ -2,7 +2,6 @@
namespace App\Domains\Catalog\Requests; namespace App\Domains\Catalog\Requests;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Shared\Rules\ImageOrBase64Rule; use App\Domains\Shared\Rules\ImageOrBase64Rule;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@@ -19,24 +18,8 @@ class UpdateProductVariantRequest extends FormRequest
*/ */
public function rules(): array public function rules(): array
{ {
/** @var ProductVariant|null $productVariant */
$productVariant = $this->route('productVariant');
/** @var \App\Domains\Catalog\Models\Product|null $product */
$product = $this->route('producto');
return [ return [
'slug' => [
'nullable',
'string',
'max:255',
Rule::unique('productos_variantes', 'slug')
->ignore($productVariant?->id)
->where(fn ($query) => $query->where('producto_id', $product?->id)),
],
'nombre' => ['nullable', 'string', 'max:255'],
'stock' => ['sometimes', 'integer', 'min:0'], 'stock' => ['sometimes', 'integer', 'min:0'],
'descripcion' => ['nullable', 'string'],
'precio' => ['required', 'numeric', 'min:0'],
'definitions' => ['sometimes', 'array'], 'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [ 'definitions.*.attribute_id' => [
'required', 'required',

View File

@@ -18,10 +18,7 @@ class ProductVariantResource extends JsonResource
return [ return [
'id' => $this->id, 'id' => $this->id,
'producto_id' => $this->producto_id, 'producto_id' => $this->producto_id,
'slug' => $this->slug, 'stock' => $this->stock,
'nombre' => $this->nombre,
'descripcion' => $this->descripcion,
'precio' => $this->precio,
'product' => ProductResource::make($this->whenLoaded('product')), 'product' => ProductResource::make($this->whenLoaded('product')),
'definitions' => ProductVariantDefinitionResource::collection($this->whenLoaded('definitions')), 'definitions' => ProductVariantDefinitionResource::collection($this->whenLoaded('definitions')),
'images' => $this->whenLoaded('attachments', function () { 'images' => $this->whenLoaded('attachments', function () {

View File

@@ -104,7 +104,7 @@ class PurchaseController extends Controller
/** @var ProductVariant $variant */ /** @var ProductVariant $variant */
$variant = $variants->get((int) $item['producto_variante_id']); $variant = $variants->get((int) $item['producto_variante_id']);
$quantity = (int) $item['cantidad']; $quantity = (int) $item['cantidad'];
$unitPrice = (float) $variant->precio; $unitPrice = (float) ($variant->product?->precio ?? 0);
return [ return [
'producto_variante_id' => $variant->getKey(), 'producto_variante_id' => $variant->getKey(),

View File

@@ -31,8 +31,6 @@ class PurchaseItemResource extends JsonResource
], ],
'variant' => $variant === null ? null : [ 'variant' => $variant === null ? null : [
'id' => $variant->id, 'id' => $variant->id,
'nombre' => $variant->nombre,
'slug' => $variant->slug,
'definitions' => ProductVariantDefinitionResource::collection($variant->definitions), 'definitions' => ProductVariantDefinitionResource::collection($variant->definitions),
], ],
]; ];

View File

@@ -14,11 +14,7 @@ return new class extends Migration
Schema::create('productos_variantes', function (Blueprint $table) { Schema::create('productos_variantes', function (Blueprint $table) {
$table->id(); $table->id();
$table->unsignedBigInteger('producto_id'); $table->unsignedBigInteger('producto_id');
$table->string('slug')->nullable();
$table->string('nombre')->nullable();
$table->unsignedInteger('stock')->default(0); $table->unsignedInteger('stock')->default(0);
$table->text('descripcion')->nullable();
$table->decimal('precio', 10, 2);
$table->timestamps(); $table->timestamps();
$table->foreign('producto_id') $table->foreign('producto_id')

View File

@@ -200,11 +200,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
]; ];
$this->productService->createVariant($product, [ $this->productService->createVariant($product, [
'slug' => $metadata['product_slug'].'-'.Str::slug((string) $size),
'nombre' => $metadata['product_name'].' - Talle '.$size,
'stock' => $pricing['stock'], 'stock' => $pricing['stock'],
'descripcion' => $metadata['description'],
'precio' => $pricing['price'],
'definitions' => $definitions, 'definitions' => $definitions,
'images' => $images, 'images' => $images,
]); ]);