feat(ticketing): add entry reservations management for admin app

- Introduced new routes for entry reservations in admin app, including form, index, store, and download functionalities.
- Created a migration to add the "Reserva de Tickets" menu item to the admin app.
- Updated MenuSeeder to include the new menu item for ticket reservations.
- Developed a PDF view for entry reservations.
- Implemented tests for the EntryReservationController and EntryReservationService to ensure proper functionality and access control.
- Refactored test setup to use a dedicated schema creation trait for better isolation and maintainability.
This commit is contained in:
2026-10-02 12:20:25 -03:00
parent f10237eb1f
commit 45e92602ed
19 changed files with 1081 additions and 148 deletions

View File

@@ -54,6 +54,8 @@ class MenuSeederTest extends TestCase
...$expectedMenus,
...$fiestaCategoryMenus,
'adminapp.desfile.entradas' => ['Entradas', '/admin/desfile/entradas'],
'adminapp.desfile.reservas' => ['Reserva de Tickets', '/admin/desfile/reservas'],
'adminapp.ticket-reservations' => ['Reserva de Tickets', '/admin/ticket-reservations'],
]))->sort()->values()->all(),
$adminApp->children->pluck('code')->sort()->values()->all()
);

View File

@@ -0,0 +1,313 @@
<?php
namespace Tests\Feature\Ticket;
use App\Domains\Commerce\Catalog\Models\Inventory;
use App\Domains\Core\Auth\Models\User;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use App\Domains\Ticketing\Ticket\Services\EntryReservationExcelService;
use App\Domains\Ticketing\Ticket\Services\EntryReservationPdfService;
use App\Domains\Ticketing\Ticket\Services\EntryReservationService;
use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticketing\Ticket\Services\TicketPdfService;
use App\Domains\Ticketing\Ticket\Services\TicketValidityResolver;
use Barryvdh\DomPDF\ServiceProvider as DomPdfServiceProvider;
use Database\Seeders\MenuSeeder;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Laravel\Sanctum\Sanctum;
use Mockery;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Tests\Support\CreatesTicketReservationSchema;
use Tests\TestCase;
class EntryReservationControllerTest extends TestCase
{
use CreatesTicketReservationSchema;
private const URL = '/api/v1/adminapp/tenant/entry-reservations';
protected function setUp(): void
{
parent::setUp();
$this->createTicketReservationSchema('onticket');
$this->app->register(DomPdfServiceProvider::class);
Schema::table('users', fn (Blueprint $table) => $table->string('rol_codigo')->default('adminapp'));
Schema::table('tenants', fn (Blueprint $table) => $table->string('nombre')->default('OnTicket'));
Schema::create('menues', function (Blueprint $table): void {
$table->string('code')->primary();
$table->string('label')->nullable();
$table->string('parent_menu_code')->nullable();
$table->string('content_type')->default('dynamic');
$table->text('static_content_schema')->nullable();
$table->string('route')->nullable();
$table->timestamps();
});
Schema::create('tenants_menues', function (Blueprint $table): void {
$table->string('tenant_code');
$table->string('menu_code');
$table->text('static_content')->nullable();
$table->timestamps();
$table->unique(['tenant_code', 'menu_code']);
});
Schema::create('roles', function (Blueprint $table): void {
$table->id();
$table->string('codigo')->unique();
});
Schema::create('roles_menues', function (Blueprint $table): void {
$table->string('rol_codigo');
$table->string('menu_codigo');
$table->unique(['rol_codigo', 'menu_codigo']);
});
DB::table('menues')->insert(['code' => 'main.adminapp', 'label' => 'Administración']);
DB::table('roles')->insert([['codigo' => 'admin'], ['codigo' => 'adminapp'], ['codigo' => 'user']]);
$this->migration()->up();
$validity = Mockery::mock(TicketValidityResolver::class);
$validity->shouldReceive('resolveVariant')->andReturn(ResolvedTicketValidity::unrestricted());
$validity->shouldReceive('resolveTicket')->andReturn(ResolvedTicketValidity::unrestricted());
$this->app->instance(TicketValidityResolver::class, $validity);
}
private function migration()
{
return require database_path('migrations/2026_10_02_010000_add_ticket_reservations_adminapp_menu.php');
}
private function login(int $id = 1): void
{
Sanctum::actingAs(User::findOrFail($id));
}
private function payload(): array
{
return ['idempotency_key' => (string) Str::uuid(), 'rows' => [
['catalog_item_id' => 1, 'variant_id' => 1, 'tipo_pago' => 'otro_metodo'],
['catalog_item_id' => 2, 'variant_id' => null, 'tipo_pago' => 'sin_cargo'],
]];
}
public function test_menu_migration_and_seeder_only_assign_onticket_and_admin_roles(): void
{
DB::table('tenants_menues')->insert(['tenant_code' => 'other', 'menu_code' => 'adminapp.ticket-reservations']);
$this->migration()->up();
$this->assertSame(['onticket'], DB::table('tenants_menues')->where('menu_code', 'adminapp.ticket-reservations')->pluck('tenant_code')->all());
$this->assertSame(['admin', 'adminapp'], DB::table('roles_menues')->where('menu_codigo', 'adminapp.ticket-reservations')->orderBy('rol_codigo')->pluck('rol_codigo')->all());
$this->seed(MenuSeeder::class);
$this->assertDatabaseHas('menues', ['code' => 'adminapp.ticket-reservations', 'parent_menu_code' => 'main.adminapp', 'route' => '/admin/ticket-reservations']);
$this->assertSame(['onticket'], DB::table('tenants_menues')->where('menu_code', 'adminapp.ticket-reservations')->pluck('tenant_code')->all());
$this->assertDatabaseMissing('roles_menues', ['rol_codigo' => 'user', 'menu_codigo' => 'adminapp.ticket-reservations']);
$this->migration()->down();
$this->assertDatabaseMissing('menues', ['code' => 'adminapp.ticket-reservations']);
$this->assertDatabaseHas('menues', ['code' => 'main.adminapp']);
}
public function test_authentication_adminapp_role_and_menu_are_required(): void
{
$this->getJson(self::URL.'/form')->assertUnauthorized();
$this->login();
DB::table('users')->where('id', 1)->update(['rol_codigo' => 'user']);
$this->login();
$this->getJson(self::URL)->assertForbidden();
DB::table('users')->where('id', 1)->update(['rol_codigo' => 'adminapp']);
$this->login();
DB::table('tenants_menues')->delete();
$this->getJson(self::URL.'/form')->assertNotFound();
$this->postJson(self::URL, $this->payload())->assertNotFound();
}
public function test_other_tenant_is_rejected_even_if_the_menu_is_assigned(): void
{
DB::table('tenants_menues')->insert(['tenant_code' => 'other', 'menu_code' => 'adminapp.ticket-reservations']);
$this->login(2);
foreach (['', '/form', '/pdf', '/excel', '/1/ticket/pdf'] as $suffix) {
$this->getJson(self::URL.$suffix)->assertForbidden();
}
$this->postJson(self::URL, $this->payload())->assertForbidden();
$this->deleteJson(self::URL.'/1')->assertForbidden();
}
public function test_form_uses_generic_variants_and_keeps_reserved_variants_with_remaining_stock(): void
{
$this->login();
$this->postJson(self::URL, $this->payload())->assertOk();
$form = $this->getJson(self::URL.'/form')->assertOk();
$this->assertSame([1, 2, 4], array_column($form->json('data.items'), 'id'));
$this->assertSame(2, $form->json('data.items.0.variants.0.available_stock'));
$this->assertSame(250, $form->json('data.items.0.variants.0.price'));
$this->assertStringContainsString('NORMAL', $form->json('data.items.0.variants.0.label'));
$form->assertJsonPath('data.items.1.requires_variant', false)->assertJsonPath('data.items.2.available_stock', null);
DB::table('variantes')->where('id', 2)->update(['sales_disabled_at' => now()]);
DB::table('inventories')->where('id', 1)->update(['real_stock' => 1]);
DB::table('catalog_items')->where('id', 2)->update(['sales_end_at' => now()->subMinute()]);
$hidden = $this->getJson(self::URL.'/form')->assertOk();
$this->assertSame([4], array_column($hidden->json('data.items'), 'id'));
$this->assertSame([1, 2, 4], array_column($hidden->json('data.filter_items'), 'id'));
}
public function test_active_event_only_limits_new_selections_and_not_history(): void
{
$this->login();
$this->postJson(self::URL, $this->payload())->assertOk();
DB::table('tenants')->where('codigo', 'onticket')->update(['active_event_id' => 10]);
DB::table('catalog_items')->where('id', 4)->update(['event_id' => 10]);
$form = $this->getJson(self::URL.'/form')->assertOk();
$this->assertSame([4], array_column($form->json('data.items'), 'id'));
$this->getJson(self::URL)->assertOk()->assertJsonPath('meta.total', 2);
}
public function test_reserves_mixed_items_replays_filters_and_cancels_with_the_real_generator(): void
{
$this->login();
$payload = $this->payload();
$created = $this->postJson(self::URL, $payload)->assertOk();
$created->assertJsonPath('data.0.catalog_item_id', 1)->assertJsonPath('data.0.importe', '250.00')
->assertJsonPath('data.1.variant', null)->assertJsonPath('data.1.importe', '0.00');
$replay = $this->postJson(self::URL, $payload)->assertOk();
$this->assertSame($created->json('data'), $replay->json('data'));
$this->getJson(self::URL.'?catalog_item_id=1&variant_id=1&tipo_pago=otro_metodo&per_page=1')
->assertOk()->assertJsonPath('meta.total', 1)->assertJsonPath('data.0.variant.id', 1);
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseHas('tickets', ['source_catalog_item_id' => 2, 'source_variant_id' => null]);
$id = $created->json('data.0.id');
$ticketId = $created->json('data.0.ticket_id');
$this->deleteJson(self::URL.'/'.$id)->assertNoContent();
$this->assertNotNull(Ticket::findOrFail($ticketId)->cancelled_at);
$this->assertSame(0, Inventory::findOrFail(1)->entry_reserved_stock);
$this->getJson(self::URL)->assertOk()->assertJsonPath('meta.total', 1);
}
public function test_invalid_selection_and_client_prices_are_rejected_atomically(): void
{
$this->login();
$payload = $this->payload();
$payload['rows'][0]['catalog_item_id'] = 3;
$this->postJson(self::URL, $payload)->assertUnprocessable()->assertJsonValidationErrors('rows.0.variant_id');
$payload = $this->payload();
$payload['rows'][1]['importe'] = 1;
$this->postJson(self::URL, $payload)->assertUnprocessable()->assertJsonValidationErrors('rows.1');
$this->assertDatabaseCount('tickets', 0);
$this->assertSame(0, (int) Inventory::sum('entry_reserved_stock'));
}
public function test_exports_use_generic_columns_filters_and_timezone(): void
{
$this->login();
$this->postJson(self::URL, $this->payload())->assertOk();
$query = '?catalog_item_id=2&timezone=America/Argentina/Buenos_Aires';
$this->getJson(self::URL.'/pdf?timezone=invalid')->assertUnprocessable();
$pdf = $this->get(self::URL.'/pdf'.$query)->assertOk()->assertHeader('Content-Type', 'application/pdf');
$this->assertStringStartsWith('%PDF', $pdf->getContent());
$excel = $this->get(self::URL.'/excel'.$query)->assertOk();
$content = $excel->streamedContent();
$this->assertStringStartsWith('PK', $content);
$filename = tempnam(sys_get_temp_dir(), 'ticket-reservations-test-');
try {
file_put_contents($filename, $content);
$sheet = IOFactory::load($filename)->getActiveSheet();
$this->assertSame(['Ítem', 'Variante', 'ID', 'Fecha', 'Importe', 'Pago'], $sheet->rangeToArray('A1:F1')[0]);
$this->assertSame(2, $sheet->getHighestRow());
$this->assertSame('Sin variante', $sheet->getCell('B2')->getValue());
$this->assertSame('Sin cargo', $sheet->getCell('F2')->getValue());
$this->assertSame('dd/mm/yyyy hh:mm', $sheet->getStyle('D2')->getNumberFormat()->getFormatCode());
} finally {
unlink($filename);
}
}
public function test_individual_ticket_download_and_cancellation_are_scoped_by_tenant(): void
{
$this->login();
$created = $this->postJson(self::URL, $this->payload())->assertOk();
$pdf = Mockery::mock(TicketPdfService::class);
$pdf->shouldReceive('download')->once()->andReturnUsing(function ($tenant, $tickets) use ($created) {
$this->assertSame('onticket', $tenant->codigo);
$this->assertSame($created->json('data.0.ticket_id'), $tickets->sole()->id);
return response('%PDF-test', 200, ['Content-Type' => 'application/pdf']);
});
$this->app->instance(TicketPdfService::class, $pdf);
$this->get(self::URL.'/'.$created->json('data.0.id').'/ticket/pdf')->assertOk();
$foreign = $this->app->make(EntryReservationService::class)
->reserve(User::findOrFail(2), (string) Str::uuid(), [
['catalog_item_id' => 3, 'tipo_pago' => 'sin_cargo'],
])->sole();
$this->getJson(self::URL.'/'.$foreign->id.'/ticket/pdf')->assertNotFound();
$this->deleteJson(self::URL.'/'.$foreign->id)->assertNotFound();
$this->assertNull(Ticket::findOrFail($foreign->ticket_id)->cancelled_at);
}
public function test_user_event_scope_limits_form_filters_history_exports_and_individual_actions(): void
{
DB::table('catalog_items')->where('id', 1)->update(['event_id' => 10]);
DB::table('catalog_items')->whereIn('id', [2, 4])->update(['event_id' => 20]);
$this->login();
$created = $this->postJson(self::URL, $this->payload())->assertOk();
$ownId = $created->json('data.0.id');
$outsideId = $created->json('data.1.id');
DB::table('users')->where('id', 1)->update(['event_id' => 10]);
DB::table('tenants')->where('codigo', 'onticket')->update(['active_event_id' => 20]);
$this->login();
$form = $this->getJson(self::URL.'/form')->assertOk();
$this->assertSame([1], array_column($form->json('data.items'), 'id'));
$this->assertSame([1], array_column($form->json('data.filter_items'), 'id'));
$this->getJson(self::URL)->assertOk()->assertJsonPath('meta.total', 1)->assertJsonPath('data.0.id', $ownId);
$this->getJson(self::URL.'?catalog_item_id=2&event_id=20')->assertOk()->assertJsonPath('meta.total', 0);
$this->getJson(self::URL.'/'.$outsideId.'/ticket/pdf')->assertNotFound();
$this->deleteJson(self::URL.'/'.$outsideId)->assertNotFound();
$this->assertNull(Ticket::findOrFail($created->json('data.1.ticket_id'))->cancelled_at);
$pdf = Mockery::mock(EntryReservationPdfService::class);
$pdf->shouldReceive('download')->once()->andReturnUsing(function ($tenant, $reservations) use ($ownId) {
$this->assertSame([$ownId], $reservations->pluck('id')->all());
return response('%PDF-test', 200);
});
$this->app->instance(EntryReservationPdfService::class, $pdf);
$this->get(self::URL.'/pdf?timezone=UTC')->assertOk();
$excel = Mockery::mock(EntryReservationExcelService::class);
$excel->shouldReceive('download')->once()->andReturnUsing(function ($tenant, $reservations) use ($ownId) {
$this->assertSame([$ownId], $reservations->pluck('id')->all());
return response()->streamDownload(fn () => print ('test'), 'reservations.xlsx');
});
$this->app->instance(EntryReservationExcelService::class, $excel);
$this->get(self::URL.'/excel?timezone=UTC')->assertOk();
$this->deleteJson(self::URL.'/'.$ownId)->assertNoContent();
}
public function test_user_event_scope_overrides_active_event_for_new_reservations_and_rejects_other_items(): void
{
DB::table('catalog_items')->where('id', 1)->update(['event_id' => 10]);
DB::table('catalog_items')->where('id', 2)->update(['event_id' => 20]);
DB::table('tenants')->where('codigo', 'onticket')->update(['active_event_id' => 20]);
DB::table('users')->where('id', 1)->update(['event_id' => 10]);
$this->login();
$this->postJson(self::URL, $this->payload())->assertUnprocessable()->assertJsonValidationErrors('rows.1.catalog_item_id');
$this->assertDatabaseCount('tickets', 0);
$payload = $this->payload();
$payload['rows'] = [$payload['rows'][0]];
$created = $this->postJson(self::URL, $payload)->assertOk();
$this->assertDatabaseHas('tickets', ['id' => $created->json('data.0.ticket_id'), 'event_id' => 10]);
$this->postJson(self::URL, $payload)->assertOk()->assertJsonPath('data.0.id', $created->json('data.0.id'));
$this->assertDatabaseCount('tickets', 1);
}
public function test_retries_cannot_replay_a_mixed_event_batch_after_user_scope_changes(): void
{
DB::table('catalog_items')->where('id', 1)->update(['event_id' => 10]);
DB::table('catalog_items')->where('id', 2)->update(['event_id' => 20]);
$this->login();
$payload = $this->payload();
$created = $this->postJson(self::URL, $payload)->assertOk();
// Cancelled rows still belong to the batch and cannot bypass the new scope.
$this->deleteJson(self::URL.'/'.$created->json('data.1.id'))->assertNoContent();
DB::table('users')->where('id', 1)->update(['event_id' => 10]);
$this->login();
$this->postJson(self::URL, $payload)->assertForbidden();
$this->assertDatabaseCount('tickets', 2);
$this->assertDatabaseCount('reservation_batches', 1);
}
}

View File

@@ -11,162 +11,23 @@ use App\Domains\Ticketing\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticketing\Ticket\Services\TicketGeneratorService;
use App\Domains\Ticketing\Ticket\Services\TicketValidityResolver;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Mockery;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\Support\CreatesTicketReservationSchema;
use Tests\TestCase;
/** Uses real transactions on the guarded SQLite :memory: connection. */
class EntryReservationServiceTest extends TestCase
{
use CreatesTicketReservationSchema;
protected function setUp(): void
{
parent::setUp();
// Minimal domain schema isolates this workflow from unrelated legacy migrations.
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->string('tenant_codigo');
$table->softDeletes();
});
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->string('codigo')->unique();
$table->unsignedBigInteger('active_event_id')->nullable();
});
Schema::create('catalog_items', function (Blueprint $table): void {
$table->id();
$table->string('tenant_code');
$table->string('slug');
$table->string('nombre')->default('Entrada');
$table->string('type')->default('standard');
$table->unsignedBigInteger('inventory_id')->nullable();
$table->string('inventory_policy');
$table->decimal('precio', 10, 2);
$table->timestamp('sales_end_at')->nullable();
$table->softDeletes();
$table->boolean('has_tickets')->default(true);
$table->unsignedBigInteger('event_id')->nullable();
});
Schema::create('inventories', function (Blueprint $table): void {
$table->id();
foreach (['real_stock', 'reserved_stock', 'sold_units', 'refunded_units'] as $column) {
$table->integer($column)->default(0);
}
});
Schema::create('variantes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('inventory_id');
$table->decimal('precio', 10, 2);
$table->unsignedBigInteger('event_date_id')->nullable();
$table->unsignedBigInteger('replaced_by_variant_id')->nullable();
$table->timestamp('sales_disabled_at')->nullable();
$table->softDeletes();
});
Schema::create('event_dates', function (Blueprint $table): void {
$table->id();
$table->date('date');
$table->time('time_start')->nullable();
$table->unsignedBigInteger('rescheduled_to_event_date_id')->nullable();
$table->timestamp('suspended_at')->nullable();
});
Schema::create('attribute', function (Blueprint $table): void {
$table->id();
$table->string('codigo');
$table->string('type')->default('string');
});
Schema::create('item_attributes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('attribute_id');
$table->boolean('allow_multi_select')->default(false);
$table->integer('sort_order')->default(0);
});
Schema::create('attribute_options', function (Blueprint $table): void {
$table->id();
$table->foreignId('attribute_id');
$table->string('value');
$table->string('label');
$table->integer('sort_order')->default(0);
});
Schema::create('variant_values', function (Blueprint $table): void {
$table->id();
$table->foreignId('variant_id');
$table->foreignId('item_attribute_id');
$table->string('value');
});
Schema::create('variant_event_dates', function (Blueprint $table): void {
$table->foreignId('variant_id');
$table->foreignId('event_date_id');
});
Schema::create('tickets', function (Blueprint $table): void {
$table->id();
$table->foreignId('user_id');
$table->foreignId('source_variant_id')->nullable();
$table->string('tenant_code')->nullable();
$table->uuid('ticket')->nullable()->unique();
$table->unsignedBigInteger('source_catalog_item_id')->nullable();
$table->unsignedBigInteger('source_purchase_item_id')->nullable();
$table->unsignedBigInteger('event_id')->nullable();
$table->timestamp('used_at')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamp('cancelled_at')->nullable();
$table->timestamp('refunded_at')->nullable();
});
Schema::create('value_changes', function (Blueprint $table): void {
$table->id();
$table->morphs('trackable');
$table->string('tenant_code');
$table->string('attribute');
$table->text('old_value')->nullable();
$table->text('new_value')->nullable();
$table->timestamp('changed_at');
$table->string('actor_type');
$table->foreignId('user_id')->nullable();
});
(require database_path('migrations/2026_09_23_010000_create_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_09_24_000000_add_administrative_entry_reservation_stock.php'))->up();
(require database_path('migrations/2026_09_24_010000_add_soft_deletes_to_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_10_02_000000_generalize_ticket_reservations.php'))->up();
DB::table('tenants')->insert(['codigo' => 'ticket_shop']);
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => 'ticket_shop']);
DB::table('catalog_items')->insert([
'id' => 1, 'tenant_code' => 'ticket_shop', 'slug' => 'entrada',
'inventory_policy' => 'tracked', 'precio' => 100,
]);
foreach ([1, 2] as $id) {
DB::table('inventories')->insert(['id' => $id, 'real_stock' => 3]);
DB::table('variantes')->insert(['id' => $id, 'catalog_item_id' => 1, 'inventory_id' => $id, 'precio' => 250]);
}
DB::table('tenants')->insert(['codigo' => 'other']);
DB::table('users')->insert(['id' => 2, 'tenant_codigo' => 'other']);
DB::table('inventories')->insert(['id' => 3, 'real_stock' => 3]);
DB::table('catalog_items')->insert([
['id' => 2, 'tenant_code' => 'ticket_shop', 'slug' => 'general', 'inventory_policy' => 'tracked', 'inventory_id' => 3, 'precio' => 700],
['id' => 3, 'tenant_code' => 'other', 'slug' => 'other', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 900],
['id' => 4, 'tenant_code' => 'ticket_shop', 'slug' => 'unlimited', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 500],
]);
foreach (['tipo' => 'NORMAL', 'sector' => 'A', 'fila' => '3', 'asiento' => '17'] as $code => $value) {
$attributeId = DB::table('attribute')->insertGetId(['codigo' => $code]);
$itemAttributeId = DB::table('item_attributes')->insertGetId([
'catalog_item_id' => 1,
'attribute_id' => $attributeId,
]);
DB::table('attribute_options')->insert([
'attribute_id' => $attributeId,
'value' => $value,
'label' => $value,
]);
foreach ([1, 2] as $variantId) {
DB::table('variant_values')->insert([
'variant_id' => $variantId, 'item_attribute_id' => $itemAttributeId, 'value' => $value,
]);
}
}
$this->createTicketReservationSchema();
}
private function service(bool $failSecond = false): EntryReservationService

View File

@@ -0,0 +1,156 @@
<?php
namespace Tests\Support;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
trait CreatesTicketReservationSchema
{
protected function createTicketReservationSchema(string $tenantCode = 'ticket_shop'): void
{
// Minimal domain schema isolates this workflow from unrelated legacy migrations.
Schema::create('users', function (Blueprint $table): void {
$table->id();
$table->string('tenant_codigo');
$table->unsignedBigInteger('event_id')->nullable();
$table->softDeletes();
});
Schema::create('tenants', function (Blueprint $table): void {
$table->id();
$table->string('codigo')->unique();
$table->unsignedBigInteger('active_event_id')->nullable();
});
Schema::create('catalog_items', function (Blueprint $table): void {
$table->id();
$table->string('tenant_code');
$table->string('slug');
$table->string('nombre')->default('Entrada');
$table->string('type')->default('standard');
$table->unsignedBigInteger('inventory_id')->nullable();
$table->string('inventory_policy');
$table->decimal('precio', 10, 2);
$table->timestamp('sales_end_at')->nullable();
$table->softDeletes();
$table->boolean('has_tickets')->default(true);
$table->unsignedBigInteger('event_id')->nullable();
});
Schema::create('inventories', function (Blueprint $table): void {
$table->id();
foreach (['real_stock', 'reserved_stock', 'sold_units', 'refunded_units'] as $column) {
$table->integer($column)->default(0);
}
});
Schema::create('variantes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('inventory_id');
$table->decimal('precio', 10, 2);
$table->unsignedBigInteger('event_date_id')->nullable();
$table->unsignedBigInteger('replaced_by_variant_id')->nullable();
$table->timestamp('sales_disabled_at')->nullable();
$table->softDeletes();
});
Schema::create('event_dates', function (Blueprint $table): void {
$table->id();
$table->date('date');
$table->time('time_start')->nullable();
$table->unsignedBigInteger('rescheduled_to_event_date_id')->nullable();
$table->timestamp('suspended_at')->nullable();
});
Schema::create('attribute', function (Blueprint $table): void {
$table->id();
$table->string('codigo');
$table->string('type')->default('string');
});
Schema::create('item_attributes', function (Blueprint $table): void {
$table->id();
$table->foreignId('catalog_item_id');
$table->foreignId('attribute_id');
$table->boolean('allow_multi_select')->default(false);
$table->integer('sort_order')->default(0);
});
Schema::create('attribute_options', function (Blueprint $table): void {
$table->id();
$table->foreignId('attribute_id');
$table->string('value');
$table->string('label');
$table->integer('sort_order')->default(0);
});
Schema::create('variant_values', function (Blueprint $table): void {
$table->id();
$table->foreignId('variant_id');
$table->foreignId('item_attribute_id');
$table->string('value');
});
Schema::create('variant_event_dates', function (Blueprint $table): void {
$table->foreignId('variant_id');
$table->foreignId('event_date_id');
});
Schema::create('tickets', function (Blueprint $table): void {
$table->id();
$table->foreignId('user_id');
$table->foreignId('source_variant_id')->nullable();
$table->string('tenant_code')->nullable();
$table->uuid('ticket')->nullable()->unique();
$table->unsignedBigInteger('source_catalog_item_id')->nullable();
$table->unsignedBigInteger('source_purchase_item_id')->nullable();
$table->unsignedBigInteger('event_id')->nullable();
$table->timestamp('used_at')->nullable();
$table->timestamp('disabled_at')->nullable();
$table->timestamp('cancelled_at')->nullable();
$table->timestamp('refunded_at')->nullable();
});
Schema::create('value_changes', function (Blueprint $table): void {
$table->id();
$table->morphs('trackable');
$table->string('tenant_code');
$table->string('attribute');
$table->text('old_value')->nullable();
$table->text('new_value')->nullable();
$table->timestamp('changed_at');
$table->string('actor_type');
$table->foreignId('user_id')->nullable();
});
(require database_path('migrations/2026_09_23_010000_create_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_09_24_000000_add_administrative_entry_reservation_stock.php'))->up();
(require database_path('migrations/2026_09_24_010000_add_soft_deletes_to_desfile_entry_reservations_table.php'))->up();
(require database_path('migrations/2026_10_02_000000_generalize_ticket_reservations.php'))->up();
DB::table('tenants')->insert(['codigo' => $tenantCode]);
DB::table('users')->insert(['id' => 1, 'tenant_codigo' => $tenantCode]);
DB::table('catalog_items')->insert([
'id' => 1, 'tenant_code' => $tenantCode, 'slug' => 'entrada',
'inventory_policy' => 'tracked', 'precio' => 100,
]);
foreach ([1, 2] as $id) {
DB::table('inventories')->insert(['id' => $id, 'real_stock' => 3]);
DB::table('variantes')->insert(['id' => $id, 'catalog_item_id' => 1, 'inventory_id' => $id, 'precio' => 250]);
}
DB::table('tenants')->insert(['codigo' => 'other']);
DB::table('users')->insert(['id' => 2, 'tenant_codigo' => 'other']);
DB::table('inventories')->insert(['id' => 3, 'real_stock' => 3]);
DB::table('catalog_items')->insert([
['id' => 2, 'tenant_code' => $tenantCode, 'slug' => 'general', 'inventory_policy' => 'tracked', 'inventory_id' => 3, 'precio' => 700],
['id' => 3, 'tenant_code' => 'other', 'slug' => 'other', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 900],
['id' => 4, 'tenant_code' => $tenantCode, 'slug' => 'unlimited', 'inventory_policy' => 'unlimited', 'inventory_id' => null, 'precio' => 500],
]);
foreach (['tipo' => 'NORMAL', 'sector' => 'A', 'fila' => '3', 'asiento' => '17'] as $code => $value) {
$attributeId = DB::table('attribute')->insertGetId(['codigo' => $code]);
$itemAttributeId = DB::table('item_attributes')->insertGetId([
'catalog_item_id' => 1,
'attribute_id' => $attributeId,
]);
DB::table('attribute_options')->insert([
'attribute_id' => $attributeId,
'value' => $value,
'label' => $value,
]);
foreach ([1, 2] as $variantId) {
DB::table('variant_values')->insert([
'variant_id' => $variantId, 'item_attribute_id' => $itemAttributeId, 'value' => $value,
]);
}
}
}
}