feat: add support for multiple event dates in variants
- Updated the FeaturedGroupService to include 'variants.eventDates' in the items query. - Introduced a BelongsToMany relationship in EventDate for selected variants. - Modified PurchaseItemResource and PurchaseItemSnapshotFactory to handle multiple event dates for variants. - Enhanced StartCheckoutService to load event dates for variants. - Updated TicketGeneratorService to accommodate event dates in ticket generation. - Created migrations to support multi-value event dates and allow multiple variant values per attribute. - Adjusted seeders to reflect new event date handling and added new attributes. - Added tests to ensure correct functionality for multi-date variants and their integration with ticket generation.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('item_attributes', function (Blueprint $table): void {
|
||||
$table->boolean('allow_multi_select')->default(false)->after('attribute_id');
|
||||
});
|
||||
|
||||
Schema::create('variant_event_dates', function (Blueprint $table): void {
|
||||
$table->foreignId('variant_id')->constrained('variantes')->cascadeOnDelete();
|
||||
$table->foreignId('event_date_id')->constrained('event_dates')->cascadeOnDelete();
|
||||
$table->primary(['variant_id', 'event_date_id']);
|
||||
$table->index(['event_date_id', 'variant_id']);
|
||||
});
|
||||
|
||||
DB::table('variantes')
|
||||
->whereNotNull('event_date_id')
|
||||
->orderBy('id')
|
||||
->each(function (object $variant): void {
|
||||
DB::table('variant_event_dates')->insertOrIgnore([
|
||||
'variant_id' => $variant->id,
|
||||
'event_date_id' => $variant->event_date_id,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('variant_event_dates');
|
||||
|
||||
Schema::table('item_attributes', function (Blueprint $table): void {
|
||||
$table->dropColumn('allow_multi_select');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('variant_values', function (Blueprint $table): void {
|
||||
$table->dropUnique('variant_values_variant_id_item_attribute_id_unique');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('variant_values', function (Blueprint $table): void {
|
||||
$table->unique(['variant_id', 'item_attribute_id']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -104,7 +104,14 @@ class AttributeSeeder extends Seeder
|
||||
{
|
||||
Attribute::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->whereIn('codigo', ['talle_numerico', 'fecha'])
|
||||
->whereNotIn('codigo', [
|
||||
'event_date',
|
||||
'servicio',
|
||||
'color',
|
||||
'horario',
|
||||
'talle',
|
||||
'tipo_alojamiento',
|
||||
])
|
||||
->delete();
|
||||
|
||||
$this->seedAttribute($tenant, [
|
||||
@@ -118,6 +125,17 @@ class AttributeSeeder extends Seeder
|
||||
$lunchValidityTime = $this->timeWindow('12:00:00', '15:00:00');
|
||||
$dinnerValidityTime = $this->timeWindow('20:00:00', '24:00:00');
|
||||
|
||||
$this->seedAttribute($tenant, [
|
||||
'codigo' => 'tipo_alojamiento',
|
||||
'nombre' => 'TipoAlojamiento',
|
||||
'type' => FieldType::Select->value,
|
||||
'is_required' => true,
|
||||
'options' => [
|
||||
['value' => 'Carpa', 'label' => 'Carpa', 'sort_order' => 1],
|
||||
['value' => 'Motorhome', 'label' => 'Motorhome', 'sort_order' => 2],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->seedAttribute($tenant, [
|
||||
'codigo' => 'servicio',
|
||||
'nombre' => 'Servicio',
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Enums\EventProductType;
|
||||
use App\Domains\Catalog\Enums\FeaturedGroupSource;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
@@ -13,9 +12,8 @@ use App\Domains\Catalog\Models\Category;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use App\Domains\Ticket\Models\ValidityTime;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use RuntimeException;
|
||||
|
||||
class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
@@ -32,141 +30,82 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
|
||||
$this->deleteExistingCatalog($tenant);
|
||||
FeaturedGroup::query()->where('tenant_code', $tenant->codigo)->delete();
|
||||
Category::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->update(['categoria_id' => null]);
|
||||
Category::query()->where('tenant_code', $tenant->codigo)->update(['categoria_id' => null]);
|
||||
Category::query()->where('tenant_code', $tenant->codigo)->delete();
|
||||
|
||||
$tenant->update([
|
||||
'event_title' => 'Fiesta Nacional del Fútbol Infantil',
|
||||
'event_location' => 'Sunchales, Santa Fe',
|
||||
]);
|
||||
|
||||
$tenant->eventDates()->delete();
|
||||
|
||||
$eventDates = collect(['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'])
|
||||
->mapWithKeys(function (string $date) use ($tenant): array {
|
||||
$eventDate = $tenant->eventDates()->create([
|
||||
'date' => $date,
|
||||
'time_start' => '00:00:00',
|
||||
'time_end' => '23:59:59',
|
||||
]);
|
||||
->map(fn (string $date) => $tenant->eventDates()->create([
|
||||
'date' => $date,
|
||||
'time_start' => '00:00:00',
|
||||
'time_end' => '23:59:59',
|
||||
]));
|
||||
$dateIds = $eventDates->pluck('id')->map(fn ($id): int => (int) $id)->values();
|
||||
|
||||
return [$date => $eventDate];
|
||||
});
|
||||
|
||||
$ticketCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Entradas',
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
$accommodationCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Alojamientos',
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
$merchandisingCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Merchandising',
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
$foodCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Comida',
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
|
||||
$dates = $eventDates->keys()->all();
|
||||
$minimumUseDate = $dates[0].' 00:00:00';
|
||||
$maximumUseDate = $dates[array_key_last($dates)].' 23:59:59';
|
||||
$eventValidityTime = ValidityTime::query()->firstOrCreate([
|
||||
'type' => ValidityTimeType::FixedWindow,
|
||||
'fixed_starts_at' => $minimumUseDate,
|
||||
'fixed_expires_at' => $maximumUseDate,
|
||||
]);
|
||||
|
||||
$generalAdmission = $this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'event_product_type' => EventProductType::Entry->value,
|
||||
'category_id' => $ticketCategory->id,
|
||||
'slug' => 'entrada-general',
|
||||
'nombre' => 'Entrada General',
|
||||
'descripcion' => 'Acceso total al predio. No incluye acceso a estacionamiento. Niños menores de 5 años ingresan gratis.',
|
||||
'precio' => 10000,
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
'has_tickets' => true,
|
||||
'validity_time_id' => $eventValidityTime->id,
|
||||
'attribute_codes' => ['event_date'],
|
||||
'variants' => array_map(
|
||||
fn (string $date): array => [
|
||||
$this->createProduct($tenant, [
|
||||
'slug' => 'camiseta',
|
||||
'nombre' => 'Camiseta',
|
||||
'precio' => 18000,
|
||||
'attribute_codes' => ['color', 'talle'],
|
||||
'variants' => collect(['Verde', 'Blanco'])
|
||||
->crossJoin(['14', 'S', 'M', 'L', 'XL', 'XXL'])
|
||||
->map(fn (array $values): array => [
|
||||
'real_stock' => 0,
|
||||
'event_date_id' => $eventDates->get($date)->id,
|
||||
],
|
||||
$dates,
|
||||
),
|
||||
'values' => ['color' => $values[0], 'talle' => $values[1]],
|
||||
])->all(),
|
||||
]);
|
||||
|
||||
$items = [
|
||||
['slug' => 'alojamiento-por-noche', 'nombre' => 'Alojamiento por noche', 'precio' => 35000, 'category_id' => $accommodationCategory->id],
|
||||
['slug' => 'alojamiento-fin-de-semana', 'nombre' => 'Alojamiento fin de semana', 'precio' => 90000, 'category_id' => $accommodationCategory->id],
|
||||
['slug' => 'remera-oficial', 'nombre' => 'Remera oficial', 'precio' => 18000, 'category_id' => $merchandisingCategory->id],
|
||||
['slug' => 'gorra-oficial', 'nombre' => 'Gorra oficial', 'precio' => 12000, 'category_id' => $merchandisingCategory->id],
|
||||
['slug' => 'hamburguesa-papa-frita', 'nombre' => 'Hamburguesa con papa frita', 'precio' => 8000, 'category_id' => $foodCategory->id],
|
||||
['slug' => 'pancho', 'nombre' => 'Pancho', 'precio' => 4000, 'category_id' => $foodCategory->id],
|
||||
];
|
||||
|
||||
$createdItems = [];
|
||||
foreach ($items as $item) {
|
||||
$createdItems[$item['slug']] = $this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'event_product_type' => EventProductType::Product->value,
|
||||
'descripcion' => $item['descripcion'] ?? $item['nombre'],
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
$this->createProduct($tenant, [
|
||||
'slug' => 'alojamiento',
|
||||
'nombre' => 'Alojamiento',
|
||||
'precio' => 35000,
|
||||
'attribute_codes' => ['tipo_alojamiento'],
|
||||
'variants' => collect(['Carpa', 'Motorhome'])->map(fn (string $type): array => [
|
||||
'real_stock' => 0,
|
||||
'validity_time_id' => $eventValidityTime->id,
|
||||
...$item,
|
||||
]);
|
||||
}
|
||||
'values' => ['tipo_alojamiento' => $type],
|
||||
])->all(),
|
||||
]);
|
||||
|
||||
$this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'event_product_type' => EventProductType::Entry->value,
|
||||
'type' => CatalogItemType::Bundle->value,
|
||||
'slug' => 'entrada-general-todos-los-dias',
|
||||
'nombre' => 'Entrada General - Todos los días',
|
||||
'descripcion' => 'Incluye una entrada para cada día de la Fiesta Nacional del Fútbol Infantil.',
|
||||
$this->createProduct($tenant, [
|
||||
'slug' => 'comida',
|
||||
'nombre' => 'Comida',
|
||||
'precio' => 8000,
|
||||
'attribute_codes' => ['event_date', 'horario', 'servicio'],
|
||||
'variants' => $dateIds
|
||||
->crossJoin(['Desayuno', 'Almuerzo', 'Cena'], ['Comedor', 'Vianda'])
|
||||
->map(fn (array $values): array => [
|
||||
'real_stock' => 0,
|
||||
'event_date_ids' => [(int) $values[0]],
|
||||
'values' => ['horario' => $values[1], 'servicio' => $values[2]],
|
||||
])->all(),
|
||||
]);
|
||||
|
||||
$this->createProduct($tenant, [
|
||||
'slug' => 'abono',
|
||||
'nombre' => 'Abono',
|
||||
'precio' => 40000,
|
||||
'category_id' => $ticketCategory->id,
|
||||
'components' => $generalAdmission->variants
|
||||
->map(fn ($variant): array => [
|
||||
'catalog_item_id' => $generalAdmission->id,
|
||||
'variant_id' => $variant->id,
|
||||
'quantity' => 1,
|
||||
])
|
||||
'event_product_type' => EventProductType::Entry->value,
|
||||
'has_tickets' => true,
|
||||
'attribute_codes' => ['event_date'],
|
||||
'multi_select_attribute_codes' => ['event_date'],
|
||||
'variants' => $this->nonEmptySubsets($dateIds)
|
||||
->map(fn (array $ids): array => ['real_stock' => 0, 'event_date_ids' => $ids])
|
||||
->all(),
|
||||
]);
|
||||
|
||||
$this->catalogService->create([
|
||||
FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'event_product_type' => EventProductType::Product->value,
|
||||
'type' => CatalogItemType::Bundle->value,
|
||||
'slug' => 'combo-2-panchos-2-hamburguesas',
|
||||
'nombre' => 'Combo 2 Panchos + 2 Hamburguesas',
|
||||
'descripcion' => 'Incluye 2 panchos y 2 hamburguesas con papa frita.',
|
||||
'precio' => 24000,
|
||||
'category_id' => $foodCategory->id,
|
||||
'components' => [
|
||||
[
|
||||
'catalog_item_id' => $createdItems['pancho']->id,
|
||||
'quantity' => 2,
|
||||
],
|
||||
[
|
||||
'catalog_item_id' => $createdItems['hamburguesa-papa-frita']->id,
|
||||
'quantity' => 2,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->seedFeaturedGroups($tenant, [
|
||||
'Entradas' => $ticketCategory,
|
||||
'Alojamientos' => $accommodationCategory,
|
||||
'Merchandising' => $merchandisingCategory,
|
||||
'Comida' => $foodCategory,
|
||||
'source_type' => FeaturedGroupSource::All,
|
||||
'category_id' => null,
|
||||
'product_layout' => ProductLayout::ColumnWithCart,
|
||||
'group_layout' => GroupLayout::Simple,
|
||||
'group_name' => 'Productos',
|
||||
'group_order' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -174,49 +113,40 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
{
|
||||
CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('type', CatalogItemType::Bundle->value)
|
||||
->each(fn (CatalogItem $item) => $this->catalogService->delete($item));
|
||||
|
||||
CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('type', CatalogItemType::Standard->value)
|
||||
->orderByRaw("CASE WHEN type = 'bundle' THEN 0 ELSE 1 END")
|
||||
->each(fn (CatalogItem $item) => $this->catalogService->delete($item));
|
||||
}
|
||||
|
||||
/** @param array<string, Category> $categories */
|
||||
private function seedFeaturedGroups(Tenant $tenant, array $categories): void
|
||||
/** @param array<string, mixed> $data */
|
||||
private function createProduct(Tenant $tenant, array $data): CatalogItem
|
||||
{
|
||||
$groups = [
|
||||
'Entradas' => [
|
||||
'product_layout' => ProductLayout::Row,
|
||||
'group_layout' => GroupLayout::SimpleVertical,
|
||||
],
|
||||
'Alojamientos' => [
|
||||
'product_layout' => ProductLayout::ColumnWithCart,
|
||||
'group_layout' => GroupLayout::Simple,
|
||||
],
|
||||
'Merchandising' => [
|
||||
'product_layout' => ProductLayout::ColumnWithCart,
|
||||
'group_layout' => GroupLayout::Simple,
|
||||
],
|
||||
'Comida' => [
|
||||
'product_layout' => ProductLayout::ColumnWithCart,
|
||||
'group_layout' => GroupLayout::Simple,
|
||||
],
|
||||
];
|
||||
return $this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'event_product_type' => EventProductType::Product->value,
|
||||
'descripcion' => $data['nombre'],
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
...$data,
|
||||
]);
|
||||
}
|
||||
|
||||
$groupOrder = 0;
|
||||
foreach ($groups as $groupName => $config) {
|
||||
FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'source_type' => FeaturedGroupSource::Category,
|
||||
'category_id' => $categories[$groupName]->id,
|
||||
'product_layout' => $config['product_layout'],
|
||||
'group_layout' => $config['group_layout'],
|
||||
'group_name' => $groupName,
|
||||
'group_order' => $groupOrder++,
|
||||
]);
|
||||
/** @return Collection<int, array<int, int>> */
|
||||
private function nonEmptySubsets(Collection $values): Collection
|
||||
{
|
||||
$items = $values->all();
|
||||
$subsets = collect();
|
||||
|
||||
for ($mask = 1; $mask < (1 << count($items)); $mask++) {
|
||||
$subset = [];
|
||||
foreach ($items as $index => $item) {
|
||||
if (($mask & (1 << $index)) !== 0) {
|
||||
$subset[] = $item;
|
||||
}
|
||||
}
|
||||
$subsets->push($subset);
|
||||
}
|
||||
|
||||
return $subsets
|
||||
->sortBy(fn (array $subset): string => count($subset).':'.implode(',', $subset))
|
||||
->values();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user