feat: add is_default field to product variants and implement default variant management logic in ProductService
This commit is contained in:
@@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
|||||||
#[Fillable([
|
#[Fillable([
|
||||||
'producto_id',
|
'producto_id',
|
||||||
'stock',
|
'stock',
|
||||||
|
'is_default',
|
||||||
])]
|
])]
|
||||||
class ProductVariant extends Model
|
class ProductVariant extends Model
|
||||||
{
|
{
|
||||||
@@ -25,6 +26,7 @@ class ProductVariant extends Model
|
|||||||
return [
|
return [
|
||||||
'producto_id' => 'integer',
|
'producto_id' => 'integer',
|
||||||
'stock' => 'integer',
|
'stock' => 'integer',
|
||||||
|
'is_default' => 'boolean',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ class ProductService
|
|||||||
// Create default variant with stock
|
// Create default variant with stock
|
||||||
$this->createVariant($product, [
|
$this->createVariant($product, [
|
||||||
'stock' => $stock,
|
'stock' => $stock,
|
||||||
|
'is_default' => true,
|
||||||
'definitions' => [],
|
'definitions' => [],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -116,11 +117,14 @@ class ProductService
|
|||||||
$images = $data['images'] ?? [];
|
$images = $data['images'] ?? [];
|
||||||
unset($data['images']);
|
unset($data['images']);
|
||||||
|
|
||||||
// If the variant being created has definitions, it is a real variant.
|
// Determine if the variant being created is a default one
|
||||||
// Remove any default variants (those without definitions).
|
|
||||||
$hasDefinitions = ! empty($data['definitions']);
|
$hasDefinitions = ! empty($data['definitions']);
|
||||||
if ($hasDefinitions) {
|
$isDefault = $data['is_default'] ?? (! $hasDefinitions);
|
||||||
$defaultVariants = $product->variants()->whereDoesntHave('definitions')->get();
|
$data['is_default'] = $isDefault;
|
||||||
|
|
||||||
|
// If the variant being created is not a default variant, remove any existing default variants
|
||||||
|
if (! $isDefault) {
|
||||||
|
$defaultVariants = $product->variants()->where('is_default', true)->get();
|
||||||
foreach ($defaultVariants as $defaultVariant) {
|
foreach ($defaultVariants as $defaultVariant) {
|
||||||
$this->deleteVariantAttachments($defaultVariant);
|
$this->deleteVariantAttachments($defaultVariant);
|
||||||
$product->deleteVariant($defaultVariant);
|
$product->deleteVariant($defaultVariant);
|
||||||
@@ -176,6 +180,7 @@ class ProductService
|
|||||||
if ($product->variants()->count() === 0) {
|
if ($product->variants()->count() === 0) {
|
||||||
$product->createVariant([
|
$product->createVariant([
|
||||||
'stock' => 0,
|
'stock' => 0,
|
||||||
|
'is_default' => true,
|
||||||
'definitions' => [],
|
'definitions' => [],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('productos_variantes', function (Blueprint $table) {
|
||||||
|
$table->boolean('is_default')->default(false)->after('stock');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('productos_variantes', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('is_default');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -635,6 +635,7 @@ class ProductControllerTest extends TestCase
|
|||||||
|
|
||||||
$variant = $product->variants()->first();
|
$variant = $product->variants()->first();
|
||||||
$this->assertEquals(15, $variant->stock);
|
$this->assertEquals(15, $variant->stock);
|
||||||
|
$this->assertTrue($variant->is_default);
|
||||||
// Should have no definitions
|
// Should have no definitions
|
||||||
$this->assertEquals(0, $variant->definitions()->count());
|
$this->assertEquals(0, $variant->definitions()->count());
|
||||||
}
|
}
|
||||||
@@ -653,6 +654,7 @@ class ProductControllerTest extends TestCase
|
|||||||
|
|
||||||
$defaultVariant = $product->variants()->create([
|
$defaultVariant = $product->variants()->create([
|
||||||
'stock' => 10,
|
'stock' => 10,
|
||||||
|
'is_default' => true,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals(1, $product->variants()->count());
|
$this->assertEquals(1, $product->variants()->count());
|
||||||
@@ -678,7 +680,9 @@ class ProductControllerTest extends TestCase
|
|||||||
|
|
||||||
// Only the new variant should remain
|
// Only the new variant should remain
|
||||||
$this->assertEquals(1, $product->variants()->count());
|
$this->assertEquals(1, $product->variants()->count());
|
||||||
$this->assertEquals(5, $product->variants()->first()->stock);
|
$newVariant = $product->variants()->first();
|
||||||
|
$this->assertEquals(5, $newVariant->stock);
|
||||||
|
$this->assertFalse($newVariant->is_default);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_it_restores_default_variant_when_all_variants_are_deleted(): void
|
public function test_it_restores_default_variant_when_all_variants_are_deleted(): void
|
||||||
@@ -695,6 +699,7 @@ class ProductControllerTest extends TestCase
|
|||||||
|
|
||||||
$realVariant = $product->createVariant([
|
$realVariant = $product->createVariant([
|
||||||
'stock' => 5,
|
'stock' => 5,
|
||||||
|
'is_default' => false,
|
||||||
'definitions' => [
|
'definitions' => [
|
||||||
[
|
[
|
||||||
'products_attribute_id' => $productAttributes['size']->id,
|
'products_attribute_id' => $productAttributes['size']->id,
|
||||||
@@ -709,10 +714,11 @@ class ProductControllerTest extends TestCase
|
|||||||
$response = $this->deleteJson("/api/tenants/{$this->tenant->codigo}/productos/{$product->id}/variants/{$realVariant->id}");
|
$response = $this->deleteJson("/api/tenants/{$this->tenant->codigo}/productos/{$product->id}/variants/{$realVariant->id}");
|
||||||
$response->assertNoContent();
|
$response->assertNoContent();
|
||||||
|
|
||||||
// A default variant should be recreated with stock 0
|
// A default variant should be recreated with stock 0 and is_default = true
|
||||||
$this->assertEquals(1, $product->variants()->count());
|
$this->assertEquals(1, $product->variants()->count());
|
||||||
$defaultVariant = $product->variants()->first();
|
$defaultVariant = $product->variants()->first();
|
||||||
$this->assertEquals(0, $defaultVariant->stock);
|
$this->assertEquals(0, $defaultVariant->stock);
|
||||||
|
$this->assertTrue($defaultVariant->is_default);
|
||||||
$this->assertEquals(0, $defaultVariant->definitions()->count());
|
$this->assertEquals(0, $defaultVariant->definitions()->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user