refactor(catalog): Complete catalog refactor to simplify its data model and its querying.
source commits: refactor/catalog
This commit is contained in:
@@ -2,20 +2,20 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Bundle\Models\Bundle;
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Category;
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use App\Domains\Catalog\Services\ProductService;
|
||||
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\Facades\DB;
|
||||
use RuntimeException;
|
||||
|
||||
class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
{
|
||||
public function __construct(private readonly ProductService $productService) {}
|
||||
public function __construct(private readonly CatalogService $catalogService) {}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
@@ -25,110 +25,160 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
throw new RuntimeException("Tenant 'fiesta_futbol_infantil' no encontrado.");
|
||||
}
|
||||
|
||||
// Bundles reference product variants, so remove them before reseeding products.
|
||||
Bundle::query()->where('tenant_codigo', $tenant->codigo)->delete();
|
||||
$this->deleteExistingCatalog($tenant);
|
||||
|
||||
// Delete existing products for this tenant
|
||||
$existingProducts = Product::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->get();
|
||||
|
||||
foreach ($existingProducts as $product) {
|
||||
$this->productService->delete($product);
|
||||
}
|
||||
|
||||
// We need a category, let's just use 'Accesorios' or create an 'Entradas' category
|
||||
$category = Category::firstOrCreate(['nombre' => 'Entradas', 'tenant_code' => null]);
|
||||
$ticketCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Entradas',
|
||||
'tenant_code' => null,
|
||||
]);
|
||||
$foodCategory = Category::query()->firstOrCreate([
|
||||
'nombre' => 'Gastronomía',
|
||||
'tenant_code' => null,
|
||||
]);
|
||||
|
||||
$dates = ['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'];
|
||||
$entradaVariants = [];
|
||||
|
||||
// 1. Entrada General
|
||||
$entrada = $this->productService->create($tenant, [
|
||||
'categoria_id' => $category->id,
|
||||
$generalAdmission = $this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'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,
|
||||
'stock' => 0,
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
'attribute_ids' => [Attribute::where('codigo', 'fecha')->where('tenant_codigo', $tenant->codigo)->first()?->id],
|
||||
'has_tickets' => true,
|
||||
'minimum_use_date' => $dates[0].' 00:00:00',
|
||||
'maximum_use_date' => $dates[array_key_last($dates)].' 23:59:59',
|
||||
'attribute_codes' => ['fecha'],
|
||||
'variants' => array_map(
|
||||
fn (string $date): array => [
|
||||
'real_stock' => 0,
|
||||
'values' => ['fecha' => $date],
|
||||
],
|
||||
$dates,
|
||||
),
|
||||
]);
|
||||
|
||||
foreach ($dates as $date) {
|
||||
$entradaVariants[] = $this->productService->createVariant($entrada, [
|
||||
'stock' => 0,
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
'has_tickets' => true,
|
||||
'minimum_use_date' => $date.' 00:00:00',
|
||||
'maximum_use_date' => $date.' 23:59:59',
|
||||
'definitions' => [
|
||||
[
|
||||
'products_attribute_id' => DB::table('products_attributes')
|
||||
->where('product_id', $entrada->id)
|
||||
->first()?->id,
|
||||
'value' => $date,
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
// Other products without variants
|
||||
$gastronomiaCategory = Category::firstOrCreate(['nombre' => 'Gastronomía', 'tenant_code' => null]);
|
||||
|
||||
$simpleProducts = [
|
||||
['slug' => 'hamburguesa-papa-frita', 'nombre' => 'Hamburguesa con papa frita', 'precio' => 8000, 'cat' => $gastronomiaCategory->id],
|
||||
['slug' => 'pancho', 'nombre' => 'Pancho', 'precio' => 4000, 'cat' => $gastronomiaCategory->id],
|
||||
['slug' => 'coca-cola-500ml', 'nombre' => 'Coca Cola 500ml', 'precio' => 3000, 'cat' => $gastronomiaCategory->id],
|
||||
['slug' => 'agua-mineral-1l', 'nombre' => 'Agua Mineral 1L', 'precio' => 2500, 'cat' => $gastronomiaCategory->id],
|
||||
['slug' => 'estacionamiento-auto', 'nombre' => 'Estacionamiento Auto', 'precio' => 5000, 'cat' => $category->id],
|
||||
['slug' => 'estacionamiento-moto', 'nombre' => 'Estacionamiento Moto', 'precio' => 2000, 'cat' => $category->id],
|
||||
$items = [
|
||||
['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],
|
||||
['slug' => 'coca-cola-500ml', 'nombre' => 'Coca Cola 500ml', 'precio' => 3000, 'category_id' => $foodCategory->id],
|
||||
['slug' => 'agua-mineral-1l', 'nombre' => 'Agua Mineral 1L', 'precio' => 2500, 'category_id' => $foodCategory->id],
|
||||
['slug' => 'estacionamiento-auto', 'nombre' => 'Estacionamiento Auto', 'precio' => 5000, 'category_id' => $ticketCategory->id],
|
||||
['slug' => 'estacionamiento-moto', 'nombre' => 'Estacionamiento Moto', 'precio' => 2000, 'category_id' => $ticketCategory->id],
|
||||
];
|
||||
|
||||
$createdProducts = [];
|
||||
|
||||
foreach ($simpleProducts as $p) {
|
||||
$createdProducts[$p['slug']] = $this->productService->create($tenant, [
|
||||
'categoria_id' => $p['cat'],
|
||||
'slug' => $p['slug'],
|
||||
'nombre' => $p['nombre'],
|
||||
'descripcion' => $p['nombre'],
|
||||
'precio' => $p['precio'],
|
||||
'stock' => 0,
|
||||
$createdItems = [];
|
||||
foreach ($items as $item) {
|
||||
$createdItems[$item['slug']] = $this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'descripcion' => $item['descripcion'] ?? $item['nombre'],
|
||||
'inventory_policy' => InventoryPolicy::Unlimited->value,
|
||||
'real_stock' => 0,
|
||||
...$item,
|
||||
]);
|
||||
}
|
||||
|
||||
$allDaysBundle = Bundle::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
$this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'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.',
|
||||
'precio' => 40000,
|
||||
'category_id' => $ticketCategory->id,
|
||||
'components' => $generalAdmission->variants
|
||||
->map(fn ($variant): array => [
|
||||
'catalog_item_id' => $generalAdmission->id,
|
||||
'variant_id' => $variant->id,
|
||||
'quantity' => 1,
|
||||
])
|
||||
->all(),
|
||||
]);
|
||||
|
||||
foreach ($entradaVariants as $variant) {
|
||||
$allDaysBundle->items()->create([
|
||||
'producto_variante_id' => $variant->id,
|
||||
'cantidad' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
$foodBundle = Bundle::query()->create([
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
$this->catalogService->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'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,
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$foodBundle->items()->createMany([
|
||||
[
|
||||
'producto_variante_id' => $createdProducts['pancho']->variants()->sole()->id,
|
||||
'cantidad' => 2,
|
||||
$this->seedFeaturedGroups($tenant);
|
||||
}
|
||||
|
||||
private function deleteExistingCatalog(Tenant $tenant): void
|
||||
{
|
||||
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)
|
||||
->each(fn (CatalogItem $item) => $this->catalogService->delete($item));
|
||||
}
|
||||
|
||||
private function seedFeaturedGroups(Tenant $tenant): void
|
||||
{
|
||||
FeaturedGroup::query()->where('tenant_code', $tenant->codigo)->delete();
|
||||
|
||||
$groups = [
|
||||
'Entradas' => [
|
||||
'entrada-general',
|
||||
'entrada-general-todos-los-dias',
|
||||
],
|
||||
[
|
||||
'producto_variante_id' => $createdProducts['hamburguesa-papa-frita']->variants()->sole()->id,
|
||||
'cantidad' => 2,
|
||||
'Estacionamiento' => [
|
||||
'estacionamiento-auto',
|
||||
'estacionamiento-moto',
|
||||
],
|
||||
]);
|
||||
'Comidas' => [
|
||||
'hamburguesa-papa-frita',
|
||||
'pancho',
|
||||
'combo-2-panchos-2-hamburguesas',
|
||||
],
|
||||
'Bebidas' => [
|
||||
'coca-cola-500ml',
|
||||
'agua-mineral-1l',
|
||||
],
|
||||
];
|
||||
|
||||
$catalogItems = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->whereIn('slug', collect($groups)->flatten()->all())
|
||||
->get()
|
||||
->keyBy('slug');
|
||||
|
||||
$groupOrder = 0;
|
||||
foreach ($groups as $groupName => $slugs) {
|
||||
$featuredGroup = FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'product_layout' => ProductLayout::Row,
|
||||
'group_name' => $groupName,
|
||||
'group_order' => $groupOrder++,
|
||||
]);
|
||||
|
||||
$featuredGroup->featuredItems()->createMany(
|
||||
collect($slugs)->values()->map(
|
||||
fn (string $slug, int $order): array => [
|
||||
'catalog_item_id' => $catalogItems->get($slug)->id,
|
||||
'order' => $order,
|
||||
]
|
||||
)->all()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user