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

View File

@@ -20,7 +20,7 @@ class CartResource extends JsonResource
: collect();
$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,
);

View File

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

View File

@@ -18,22 +18,8 @@ class StoreProductVariantRequest extends FormRequest
*/
public function rules(): array
{
/** @var \App\Domains\Catalog\Models\Product|null $product */
$product = $this->route('producto');
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'],
'descripcion' => ['nullable', 'string'],
'precio' => ['required', 'numeric', 'min:0'],
'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [
'required',

View File

@@ -2,7 +2,6 @@
namespace App\Domains\Catalog\Requests;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Shared\Rules\ImageOrBase64Rule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
@@ -19,24 +18,8 @@ class UpdateProductVariantRequest extends FormRequest
*/
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 [
'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'],
'descripcion' => ['nullable', 'string'],
'precio' => ['required', 'numeric', 'min:0'],
'definitions' => ['sometimes', 'array'],
'definitions.*.attribute_id' => [
'required',

View File

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

View File

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

View File

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

View File

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

View File

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