diff --git a/app/Domains/Catalog/Models/Inventory.php b/app/Domains/Catalog/Models/Inventory.php index 4a1e567..5ff7f6c 100644 --- a/app/Domains/Catalog/Models/Inventory.php +++ b/app/Domains/Catalog/Models/Inventory.php @@ -42,10 +42,10 @@ class Inventory extends Model return $this->hasOne(CatalogItem::class); } - /** @return HasOne */ - public function variant(): HasOne + /** @return HasMany */ + public function variants(): HasMany { - return $this->hasOne(Variant::class); + return $this->hasMany(Variant::class); } /** @return HasMany */ diff --git a/app/Domains/Catalog/Models/Variant.php b/app/Domains/Catalog/Models/Variant.php index 581afc3..522b781 100644 --- a/app/Domains/Catalog/Models/Variant.php +++ b/app/Domains/Catalog/Models/Variant.php @@ -20,6 +20,8 @@ use Illuminate\Support\Str; 'catalog_item_id', 'event_date_id', 'inventory_id', + 'replaced_by_variant_id', + 'sales_disabled_at', 'descripcion', 'precio', ])] @@ -37,6 +39,8 @@ class Variant extends Model 'catalog_item_id' => 'integer', 'event_date_id' => 'integer', 'inventory_id' => 'integer', + 'replaced_by_variant_id' => 'integer', + 'sales_disabled_at' => 'datetime', 'precio' => 'decimal:2', ]; } @@ -76,6 +80,24 @@ class Variant extends Model return $this->belongsTo(Inventory::class); } + /** @return BelongsTo */ + public function replacement(): BelongsTo + { + return $this->belongsTo(self::class, 'replaced_by_variant_id'); + } + + /** @return HasMany */ + public function replacedVariants(): HasMany + { + return $this->hasMany(self::class, 'replaced_by_variant_id'); + } + + public function isSellable(): bool + { + return $this->sales_disabled_at === null + && $this->replaced_by_variant_id === null; + } + /** @return HasMany */ public function definitions(): HasMany { diff --git a/database/migrations/2026_09_14_030000_add_variant_replacement_state.php b/database/migrations/2026_09_14_030000_add_variant_replacement_state.php new file mode 100644 index 0000000..2769c9d --- /dev/null +++ b/database/migrations/2026_09_14_030000_add_variant_replacement_state.php @@ -0,0 +1,86 @@ +dropForeign(['inventory_id']); + } + + $table->dropUnique('variantes_inventory_id_unique'); + $table->index('inventory_id'); + + if ($requiresForeignKeyRecreation) { + $table->foreign('inventory_id')->references('id')->on('inventories')->restrictOnDelete(); + } + + $table->foreignId('replaced_by_variant_id') + ->nullable() + ->after('inventory_id') + ->constrained('variantes') + ->nullOnDelete(); + $table->timestamp('sales_disabled_at') + ->nullable() + ->after('replaced_by_variant_id'); + $table->index( + ['sales_disabled_at', 'replaced_by_variant_id'], + 'variants_sellable_index', + ); + }); + } + + public function down(): void + { + $requiresForeignKeyRecreation = in_array(DB::getDriverName(), ['mysql', 'mariadb'], true); + + DB::table('variantes') + ->orderBy('id') + ->get() + ->groupBy('inventory_id') + ->each(function ($variants): void { + $variants->skip(1)->each(function (object $variant): void { + $inventory = DB::table('inventories')->where('id', $variant->inventory_id)->first(); + + if ($inventory === null) { + return; + } + + $inventoryId = DB::table('inventories')->insertGetId([ + 'sold_units' => $inventory->sold_units, + 'reserved_stock' => 0, + 'real_stock' => $inventory->real_stock, + ]); + + DB::table('variantes')->where('id', $variant->id)->update([ + 'inventory_id' => $inventoryId, + ]); + }); + }); + + Schema::table('variantes', function (Blueprint $table) use ($requiresForeignKeyRecreation): void { + $table->dropIndex('variants_sellable_index'); + $table->dropConstrainedForeignId('replaced_by_variant_id'); + $table->dropColumn('sales_disabled_at'); + + if ($requiresForeignKeyRecreation) { + $table->dropForeign(['inventory_id']); + } + + $table->dropIndex(['inventory_id']); + $table->unique('inventory_id'); + + if ($requiresForeignKeyRecreation) { + $table->foreign('inventory_id')->references('id')->on('inventories')->restrictOnDelete(); + } + }); + } +}; diff --git a/tests/Feature/Catalog/CatalogSchemaTest.php b/tests/Feature/Catalog/CatalogSchemaTest.php index 68b2508..94cb8a2 100644 --- a/tests/Feature/Catalog/CatalogSchemaTest.php +++ b/tests/Feature/Catalog/CatalogSchemaTest.php @@ -200,10 +200,12 @@ class CatalogSchemaTest extends TestCase ]); } - public function test_variants_can_override_catalog_item_use_dates(): void + public function test_variants_support_event_dates_and_commercial_replacements(): void { $this->assertTrue(Schema::hasColumns('variantes', [ 'event_date_id', + 'replaced_by_variant_id', + 'sales_disabled_at', ])); } diff --git a/tests/Unit/Catalog/CatalogModelsTest.php b/tests/Unit/Catalog/CatalogModelsTest.php index 84ef63d..e989b2c 100644 --- a/tests/Unit/Catalog/CatalogModelsTest.php +++ b/tests/Unit/Catalog/CatalogModelsTest.php @@ -309,7 +309,7 @@ class CatalogModelsTest extends TestCase $this->assertSame(2, $inventory->sold_units); $this->assertSame(7, $inventory->availableStock()); $this->assertInstanceOf(CatalogItem::class, $inventory->catalogItem()->getRelated()); - $this->assertInstanceOf(Variant::class, $inventory->variant()->getRelated()); + $this->assertInstanceOf(Variant::class, $inventory->variants()->getRelated()); } public function test_catalog_item_aggregates_variant_inventory(): void