Files
shopit-back/database/seeders/FiestaFutbolInfantilProductSeeder.php
ncoronel 21b70777f2 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.
2026-08-07 11:57:38 -03:00

153 lines
5.6 KiB
PHP

<?php
namespace Database\Seeders;
use App\Domains\Catalog\Enums\EventProductType;
use App\Domains\Catalog\Enums\FeaturedGroupSource;
use App\Domains\Catalog\Enums\GroupLayout;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Enums\ProductLayout;
use App\Domains\Catalog\Models\CatalogItem;
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 Illuminate\Database\Seeder;
use Illuminate\Support\Collection;
use RuntimeException;
class FiestaFutbolInfantilProductSeeder extends Seeder
{
public function __construct(private readonly CatalogService $catalogService) {}
public function run(): void
{
$tenant = Tenant::query()->where('codigo', 'fiesta_futbol_infantil')->first();
if (! $tenant) {
throw new RuntimeException("Tenant 'fiesta_futbol_infantil' no encontrado.");
}
$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)->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'])
->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();
$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,
'values' => ['color' => $values[0], 'talle' => $values[1]],
])->all(),
]);
$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,
'values' => ['tipo_alojamiento' => $type],
])->all(),
]);
$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,
'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(),
]);
FeaturedGroup::query()->create([
'tenant_code' => $tenant->codigo,
'source_type' => FeaturedGroupSource::All,
'category_id' => null,
'product_layout' => ProductLayout::ColumnWithCart,
'group_layout' => GroupLayout::Simple,
'group_name' => 'Productos',
'group_order' => 0,
]);
}
private function deleteExistingCatalog(Tenant $tenant): void
{
CatalogItem::query()
->where('tenant_code', $tenant->codigo)
->orderByRaw("CASE WHEN type = 'bundle' THEN 0 ELSE 1 END")
->each(fn (CatalogItem $item) => $this->catalogService->delete($item));
}
/** @param array<string, mixed> $data */
private function createProduct(Tenant $tenant, array $data): CatalogItem
{
return $this->catalogService->create([
'tenant_code' => $tenant->codigo,
'event_product_type' => EventProductType::Product->value,
'descripcion' => $data['nombre'],
'inventory_policy' => InventoryPolicy::Unlimited->value,
...$data,
]);
}
/** @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();
}
}