1012 lines
40 KiB
PHP
1012 lines
40 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Ticket;
|
|
|
|
use App\Domains\Attachable\Enums\AttachmentType;
|
|
use App\Domains\Attachable\Models\Attachment;
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Authorization\Enums\RoleCode;
|
|
use App\Domains\Catalog\Models\Attribute;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Inventory;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use App\Domains\Menu\Models\Menu;
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use App\Domains\Purchase\Models\PurchaseItem;
|
|
use App\Domains\Shared\Enums\FieldType;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Domains\Tenant\Models\WebsiteType;
|
|
use App\Domains\Ticket\Models\Ticket;
|
|
use Barryvdh\DomPDF\ServiceProvider as DomPdfServiceProvider;
|
|
use Database\Seeders\AttributeSeeder;
|
|
use Database\Seeders\AuthorizationSeeder;
|
|
use Database\Seeders\FiestaFutbolInfantilProductSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class AdminAppTicketControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->app->register(DomPdfServiceProvider::class);
|
|
|
|
$this->seed(AuthorizationSeeder::class);
|
|
WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
|
|
}
|
|
|
|
public function test_authentication_is_required(): void
|
|
{
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')->assertUnauthorized();
|
|
}
|
|
|
|
public function test_the_tenant_must_have_the_tickets_menu(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')->assertNotFound();
|
|
}
|
|
|
|
public function test_it_lists_only_tickets_from_the_authenticated_tenant(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$otherTenant = $this->createTenant('other');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$otherUser = $this->createAdminAppUser($otherTenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$ticket = $this->createTicket($tenant, $admin);
|
|
$this->createTicket($otherTenant, $otherUser);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $ticket->id)
|
|
->assertJsonPath('data.0.tenant_code', $tenant->codigo)
|
|
->assertJsonPath('data.0.values.id', $ticket->id)
|
|
->assertJsonPath('data.0.values.status', Ticket::STATUS_ACTIVE)
|
|
->assertJsonPath('data.0.status_label', 'Activo')
|
|
->assertJsonPath('data.0.allow_refund', false)
|
|
->assertJsonPath('data.0.is_active', true)
|
|
->assertJsonPath('data.0.can_cancel', true)
|
|
->assertJsonPath('data.0.can_refund', false)
|
|
->assertJsonMissingPath('data.0.values.ticket')
|
|
->assertJsonPath('data.0.values.date', '-')
|
|
->assertJsonPath('data.0.values.size', '-')
|
|
->assertJsonMissingPath('data.0.date')
|
|
->assertJsonPath('meta.total', 1);
|
|
}
|
|
|
|
public function test_it_exposes_allow_refund_flag_in_tickets_list(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-allow-refund');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$this->createTicket($tenant, $admin);
|
|
|
|
// Default: neither total nor partial refund allowed
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.allow_refund', false)
|
|
->assertJsonPath('data.0.can_refund', false);
|
|
|
|
// Total refund allowed
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
]);
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.allow_refund', true)
|
|
->assertJsonPath('data.0.can_refund', true);
|
|
|
|
// Partial refund allowed with percentage set
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => false,
|
|
'allow_ticket_partial_refund' => true,
|
|
'ticket_partial_refund_percentage' => 20.00,
|
|
]);
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.allow_refund', true)
|
|
->assertJsonPath('data.0.can_refund', true);
|
|
|
|
// Master toggle disabled while preserving the partial refund preference
|
|
$tenant->update(['allow_ticket_refund' => false]);
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.allow_refund', false)
|
|
->assertJsonPath('data.0.can_refund', false);
|
|
$this->assertTrue($tenant->fresh()->allow_ticket_partial_refund);
|
|
$this->assertSame('20.00', $tenant->fresh()->ticket_partial_refund_percentage);
|
|
|
|
$tenant->update(['allow_ticket_refund' => true]);
|
|
|
|
// Partial refund enabled but percentage is 0
|
|
$tenant->update([
|
|
'ticket_partial_refund_percentage' => 0,
|
|
]);
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.allow_refund', false)
|
|
->assertJsonPath('data.0.can_refund', false);
|
|
}
|
|
|
|
public function test_it_cancels_a_ticket_from_the_authenticated_tenant(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-cancellation');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
$ticket = $this->createTicket($tenant, $admin);
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/cancel")
|
|
->assertOk()
|
|
->assertJsonPath('data.id', $ticket->id)
|
|
->assertJsonPath('data.status', Ticket::STATUS_CANCELLED)
|
|
->assertJsonPath('data.status_label', 'Cancelado');
|
|
|
|
$this->assertNotNull($ticket->fresh()->cancelled_at);
|
|
}
|
|
|
|
public function test_it_does_not_cancel_a_ticket_with_another_terminal_status(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-cancellation');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
$ticket = $this->createTicket($tenant, $admin, ['disabled_at' => now()]);
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/cancel")
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('status');
|
|
|
|
$this->assertNull($ticket->fresh()->cancelled_at);
|
|
}
|
|
|
|
public function test_it_totally_refunds_a_ticket_when_the_tenant_allows_it(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-total-refund');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund", [
|
|
'refund_type' => 'total',
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.id', $ticket->id)
|
|
->assertJsonPath('data.status', Ticket::STATUS_REFUNDED)
|
|
->assertJsonPath('data.refunded_amount', '100.00');
|
|
|
|
$this->assertNotNull($ticket->fresh()->refunded_at);
|
|
$this->assertSame('100.00', $purchaseItem->fresh()->refunded_amount);
|
|
}
|
|
|
|
public function test_it_partially_refunds_a_ticket_using_the_tenant_percentage(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-partial-refund');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_partial_refund' => true,
|
|
'ticket_partial_refund_percentage' => 25.50,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund", [
|
|
'refund_type' => 'partial',
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.status', Ticket::STATUS_REFUNDED)
|
|
->assertJsonPath('data.refunded_amount', '25.50');
|
|
|
|
$this->assertSame('25.50', $purchaseItem->fresh()->refunded_amount);
|
|
}
|
|
|
|
public function test_it_does_not_refund_a_ticket_when_the_requested_refund_type_is_disabled(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-refund-disabled');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund", [
|
|
'refund_type' => 'total',
|
|
])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('refund_type');
|
|
|
|
$this->assertNull($ticket->fresh()->refunded_at);
|
|
$this->assertSame('0.00', $purchaseItem->fresh()->refunded_amount);
|
|
}
|
|
|
|
public function test_it_validates_the_refund_type(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-refund-validation');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$this->postJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund", [
|
|
'refund_type' => 'invalid',
|
|
])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('refund_type');
|
|
}
|
|
|
|
public function test_it_calculates_total_and_partial_refund_for_a_ticket(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-calc-both');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
'allow_ticket_partial_refund' => true,
|
|
'ticket_partial_refund_percentage' => 30.00,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund")
|
|
->assertOk()
|
|
->assertJsonPath('data.total', '100.00')
|
|
->assertJsonPath('data.partial', '30.00');
|
|
}
|
|
|
|
public function test_it_calculates_only_total_when_partial_is_disabled(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-calc-total');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
'allow_ticket_partial_refund' => false,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket] = $this->createRefundableTicket($tenant, $admin, '150.00');
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund")
|
|
->assertOk()
|
|
->assertJsonPath('data.total', '150.00')
|
|
->assertJsonPath('data.partial', null);
|
|
}
|
|
|
|
public function test_it_returns_null_when_remaining_item_balance_is_insufficient(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-calc-insufficient');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
'allow_ticket_partial_refund' => true,
|
|
'ticket_partial_refund_percentage' => 50.00,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
// Simulate 70 already refunded out of 100 on the item (remaining is 30)
|
|
$purchaseItem->update(['refunded_amount' => '70.00']);
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund")
|
|
->assertOk()
|
|
// Total is 100, which exceeds remaining 30 -> total is null
|
|
->assertJsonPath('data.total', null)
|
|
// Partial is 50, which exceeds remaining 30 -> partial is null
|
|
->assertJsonPath('data.partial', null);
|
|
}
|
|
|
|
public function test_it_fails_calculating_refund_if_ticket_is_not_active(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-calc-inactive');
|
|
$tenant->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
]);
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
[$ticket] = $this->createRefundableTicket($tenant, $admin, '100.00');
|
|
|
|
$ticket->update(['cancelled_at' => now()]);
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets/{$ticket->id}/refund")
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('refund');
|
|
}
|
|
|
|
public function test_it_cannot_calculate_refund_for_another_tenants_ticket(): void
|
|
{
|
|
$tenantA = $this->createTenant('ticket-calc-a');
|
|
$tenantB = $this->createTenant('ticket-calc-b');
|
|
$tenantA->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
]);
|
|
$tenantB->update([
|
|
'allow_ticket_refund' => true,
|
|
'allow_ticket_total_refund' => true,
|
|
]);
|
|
|
|
$adminA = $this->createAdminAppUser($tenantA);
|
|
$adminB = $this->createAdminAppUser($tenantB);
|
|
$this->grantTicketsMenu($tenantA);
|
|
|
|
[$ticketB] = $this->createRefundableTicket($tenantB, $adminB, '100.00');
|
|
|
|
Sanctum::actingAs($adminA);
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets/{$ticketB->id}/refund")
|
|
->assertNotFound();
|
|
}
|
|
|
|
public function test_it_searches_by_id_and_does_not_search_by_uuid(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$matching = $this->createTicket($tenant, $admin);
|
|
$this->createTicket($tenant, $admin);
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets?q={$matching->id}")
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?q='.substr($matching->ticket, 0, 8))
|
|
->assertOk()
|
|
->assertJsonCount(0, 'data');
|
|
}
|
|
|
|
public function test_it_searches_by_visible_client_and_scanner_names(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$client = User::factory()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'nombre_apellido' => 'María Cliente',
|
|
]);
|
|
$scanner = $this->createAdminAppUser($tenant);
|
|
$scanner->update(['nombre_apellido' => 'Carlos Inspector']);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$clientTicket = $this->createTicket($tenant, $client);
|
|
$scannerTicket = $this->createTicket($tenant, $admin, [
|
|
'scanner_user_id' => $scanner->id,
|
|
'used_at' => now(),
|
|
]);
|
|
$this->createTicket($tenant, $admin);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query(['q' => 'maría']))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $clientTicket->id);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query(['q' => 'INSPECTOR']))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $scannerTicket->id);
|
|
}
|
|
|
|
public function test_it_searches_by_order_number_and_formatted_unit_amount(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
foreach (range(1, 10) as $number) {
|
|
$this->createPurchase($tenant, $admin, now()->subMinutes($number)->toDateTimeString());
|
|
}
|
|
|
|
$item = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => 'entrada-busqueda',
|
|
'nombre' => 'Entrada búsqueda',
|
|
'precio' => '1234.50',
|
|
]);
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'user_id' => $admin->id,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'nombre_apellido' => 'Cliente de la compra',
|
|
'total' => '1234.50',
|
|
]);
|
|
$purchaseItem = PurchaseItem::query()->create([
|
|
'compra_id' => $purchase->id,
|
|
'source_catalog_item_id' => $item->id,
|
|
'nombre' => $item->nombre,
|
|
'descripcion' => '',
|
|
'slug' => $item->slug,
|
|
'item_nombre' => $item->nombre,
|
|
'cantidad' => 1,
|
|
'precio_unitario' => '1234.50',
|
|
'total' => '1234.50',
|
|
]);
|
|
$matching = $this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $purchaseItem->id,
|
|
'source_catalog_item_id' => $item->id,
|
|
]);
|
|
$this->createTicket($tenant, $admin);
|
|
|
|
$this->getJson("/api/v1/adminapp/tenant/tickets?q={$purchase->id}")
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query(['q' => 'cliente de la compra']))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id);
|
|
|
|
foreach (['1234,50', '$1.234,50', '1,234.50'] as $amount) {
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query(['q' => $amount]))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id);
|
|
}
|
|
}
|
|
|
|
public function test_it_sorts_by_ticket_id_before_paginating(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$ids = collect(range(1, 4))
|
|
->map(fn (): int => $this->createTicket($tenant, $admin)->id)
|
|
->all();
|
|
|
|
$query = http_build_query([
|
|
'sort_by' => 'id',
|
|
'sort_direction' => 'asc',
|
|
'per_page' => 2,
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.$query)
|
|
->assertOk()
|
|
->assertJsonPath('data.0.values.id', $ids[0])
|
|
->assertJsonPath('data.1.values.id', $ids[1])
|
|
->assertJsonPath('meta.total', 4)
|
|
->assertJsonPath('meta.last_page', 2);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.$query.'&page=2')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.values.id', $ids[2])
|
|
->assertJsonPath('data.1.values.id', $ids[3]);
|
|
}
|
|
|
|
public function test_it_rejects_sort_columns_not_enabled_for_the_tenant(): void
|
|
{
|
|
$tenant = $this->createTenant('other');
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?sort_by=category&sort_direction=sideways')
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['sort_by', 'sort_direction']);
|
|
}
|
|
|
|
public function test_it_rejects_product_and_type_sorting_for_fiesta_futbol_infantil(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
|
|
foreach (['product', 'type'] as $column) {
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?sort_by='.$column)
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors('sort_by');
|
|
}
|
|
}
|
|
|
|
public function test_it_includes_tenant_scanned_and_total_ticket_counts(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$otherTenant = $this->createTenant('other');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$otherUser = $this->createAdminAppUser($otherTenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$this->createTicket($tenant, $admin)->update(['used_at' => now()]);
|
|
$this->createTicket($tenant, $admin);
|
|
$this->createTicket($tenant, $admin)->update(['cancelled_at' => now()]);
|
|
$this->createTicket($tenant, $admin)->update(['refunded_at' => now()]);
|
|
$this->createTicket($tenant, $admin)->update(['disabled_at' => now()]);
|
|
$this->createTicket($otherTenant, $otherUser)->update(['used_at' => now()]);
|
|
|
|
$catalogItem = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => 'refund-summary-item',
|
|
'nombre' => 'Entrada',
|
|
'precio' => '1000.00',
|
|
]);
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'total' => '1000.00',
|
|
]);
|
|
PurchaseItem::query()->create([
|
|
'compra_id' => $purchase->id,
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'nombre' => 'Entrada',
|
|
'item_nombre' => 'Entrada',
|
|
'cantidad' => 1,
|
|
'precio_unitario' => '1000.00',
|
|
'total' => '1000.00',
|
|
'refunded_amount' => '250.00',
|
|
]);
|
|
$otherPurchase = Purchase::query()->create([
|
|
'tenant_codigo' => $otherTenant->codigo,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'total' => '2000.00',
|
|
]);
|
|
PurchaseItem::query()->create([
|
|
'compra_id' => $otherPurchase->id,
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'nombre' => 'Otra entrada',
|
|
'item_nombre' => 'Otra entrada',
|
|
'cantidad' => 1,
|
|
'precio_unitario' => '2000.00',
|
|
'total' => '2000.00',
|
|
'refunded_amount' => '2000.00',
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?q=does-not-match')
|
|
->assertOk()
|
|
->assertJsonCount(0, 'data')
|
|
->assertJsonPath('scanned_tickets', 0)
|
|
->assertJsonPath('total_tickets', 0)
|
|
->assertJsonPath('refunded_total', '250.00');
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonCount(5, 'data')
|
|
->assertJsonPath('scanned_tickets', 1)
|
|
->assertJsonPath('total_tickets', 2)
|
|
->assertJsonPath('refunded_total', '250.00');
|
|
}
|
|
|
|
public function test_it_returns_structured_variant_properties(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$item = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => 'remera',
|
|
'nombre' => 'Remera',
|
|
'precio' => '8000.00',
|
|
'has_tickets' => true,
|
|
]);
|
|
$attribute = Attribute::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'codigo' => 'size',
|
|
'nombre' => 'Talle',
|
|
'type' => FieldType::Select,
|
|
]);
|
|
$attribute->options()->create(['value' => 'xl', 'label' => 'XL']);
|
|
$itemAttribute = $item->itemAttributes()->create([
|
|
'attribute_id' => $attribute->id,
|
|
'sort_order' => 1,
|
|
]);
|
|
$variant = Variant::query()->create([
|
|
'catalog_item_id' => $item->id,
|
|
'inventory_id' => Inventory::query()->create()->id,
|
|
]);
|
|
$variant->definitions()->create([
|
|
'item_attribute_id' => $itemAttribute->id,
|
|
'value' => 'xl',
|
|
]);
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'user_id' => $admin->id,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'nombre_apellido' => 'Nombre Apellido',
|
|
'total' => '8000.00',
|
|
]);
|
|
$purchaseItem = PurchaseItem::query()->create([
|
|
'compra_id' => $purchase->id,
|
|
'source_catalog_item_id' => $item->id,
|
|
'source_variant_id' => $variant->id,
|
|
'nombre' => 'Remera',
|
|
'descripcion' => '',
|
|
'slug' => 'remera',
|
|
'item_nombre' => 'Remera',
|
|
'cantidad' => 1,
|
|
'precio_unitario' => '8000.00',
|
|
'total' => '8000.00',
|
|
]);
|
|
$ticket = $this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $purchaseItem->id,
|
|
'source_catalog_item_id' => $item->id,
|
|
'source_variant_id' => $variant->id,
|
|
'scanner_user_id' => $admin->id,
|
|
'used_at' => now(),
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.id', $ticket->id)
|
|
->assertJsonPath('data.0.source_purchase_item_id', $purchaseItem->id)
|
|
->assertJsonPath('data.0.order_number', $purchase->id)
|
|
->assertJsonPath('data.0.product', 'Remera')
|
|
->assertJsonPath('data.0.amount', '8000.00')
|
|
->assertJsonPath('data.0.refunded_amount', '0.00')
|
|
->assertJsonPath('data.0.status', Ticket::STATUS_USED)
|
|
->assertJsonPath('data.0.scanned_by', $admin->nombre_apellido)
|
|
->assertJsonPath('data.0.variant_properties.0.code', 'size')
|
|
->assertJsonPath('data.0.variant_properties.0.label', 'Talle')
|
|
->assertJsonPath('data.0.variant_properties.0.values.0.value', 'xl')
|
|
->assertJsonPath('data.0.variant_properties.0.values.0.label', 'XL');
|
|
}
|
|
|
|
public function test_it_applies_the_ticket_filter_form_values(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$date = $tenant->eventDates()->orderByDesc('date')->firstOrFail();
|
|
$food = CatalogItem::query()
|
|
->where('tenant_code', $tenant->codigo)
|
|
->where('slug', 'comida')
|
|
->with([
|
|
'variants.definitions.itemAttribute.attribute.options',
|
|
'variants.eventDates',
|
|
'variants.eventDate',
|
|
])
|
|
->firstOrFail();
|
|
$dinner = $food->variants->first(fn (Variant $variant): bool => $variant
|
|
->selectedEventDates()->contains('id', $date->id)
|
|
&& $variant->selectionValues()->get('horario') === 'Cena');
|
|
$lunch = $food->variants->first(fn (Variant $variant): bool => $variant
|
|
->selectedEventDates()->contains('id', $date->id)
|
|
&& $variant->selectionValues()->get('horario') === 'Almuerzo');
|
|
$this->assertNotNull($dinner);
|
|
$this->assertNotNull($lunch);
|
|
$dinnerService = $dinner->selectionValues()->get('servicio');
|
|
$otherDate = $tenant->eventDates()->where('id', '!=', $date->id)->firstOrFail();
|
|
$otherDinner = $food->variants->first(fn (Variant $variant): bool => $variant
|
|
->selectedEventDates()->contains('id', $otherDate->id)
|
|
&& $variant->selectionValues()->get('horario') === 'Cena'
|
|
&& $variant->selectionValues()->get('servicio') === $dinnerService);
|
|
$this->assertNotNull($otherDinner);
|
|
|
|
$matchingPurchase = $this->createPurchase($tenant, $admin, '2026-08-20 10:00:00');
|
|
$otherPurchase = $this->createPurchase($tenant, $admin, '2026-08-21 10:00:00');
|
|
$dinnerItem = $this->createPurchaseItem($matchingPurchase, $food, $dinner);
|
|
$lunchItem = $this->createPurchaseItem($matchingPurchase, $food, $lunch);
|
|
$otherDinnerItem = $this->createPurchaseItem($otherPurchase, $food, $otherDinner);
|
|
$matching = $this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $dinnerItem->id,
|
|
'source_catalog_item_id' => $food->id,
|
|
'source_variant_id' => $dinner->id,
|
|
'used_at' => now(),
|
|
]);
|
|
$this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $lunchItem->id,
|
|
'source_catalog_item_id' => $food->id,
|
|
'source_variant_id' => $lunch->id,
|
|
'used_at' => now(),
|
|
]);
|
|
$this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $otherDinnerItem->id,
|
|
'source_catalog_item_id' => $food->id,
|
|
'source_variant_id' => $otherDinner->id,
|
|
'used_at' => now(),
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query([
|
|
'category' => 'comidas',
|
|
'product' => 'Cena',
|
|
'type' => $dinnerService,
|
|
'date' => $date->date->format('Y-m-d'),
|
|
'status' => Ticket::STATUS_USED,
|
|
]))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id)
|
|
->assertJsonPath('data.0.values.product', 'Cena')
|
|
->assertJsonPath('data.0.values.type', $dinnerService)
|
|
->assertJsonPath('data.0.values.date', $date->date->format('d/m'))
|
|
->assertJsonPath('scanned_tickets', 1)
|
|
->assertJsonPath('total_tickets', 1);
|
|
}
|
|
|
|
public function test_it_exposes_and_filters_merchandise_color_and_size(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$merchandise = CatalogItem::query()
|
|
->where('tenant_code', $tenant->codigo)
|
|
->where('slug', 'camiseta')
|
|
->with([
|
|
'variants.definitions.itemAttribute.attribute.options',
|
|
'variants.eventDates',
|
|
'variants.eventDate',
|
|
])
|
|
->firstOrFail();
|
|
$greenMedium = $merchandise->variants->first(fn (Variant $variant): bool => $variant->selectionValues()->get('color') === 'Verde'
|
|
&& $variant->selectionValues()->get('talle') === 'M');
|
|
$whiteMedium = $merchandise->variants->first(fn (Variant $variant): bool => $variant->selectionValues()->get('color') === 'Blanco'
|
|
&& $variant->selectionValues()->get('talle') === 'M');
|
|
$this->assertNotNull($greenMedium);
|
|
$this->assertNotNull($whiteMedium);
|
|
|
|
$matching = $this->createTicket($tenant, $admin, [
|
|
'source_catalog_item_id' => $merchandise->id,
|
|
'source_variant_id' => $greenMedium->id,
|
|
]);
|
|
$this->createTicket($tenant, $admin, [
|
|
'source_catalog_item_id' => $merchandise->id,
|
|
'source_variant_id' => $whiteMedium->id,
|
|
]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?'.http_build_query([
|
|
'category' => 'merchandising',
|
|
'product' => 'camiseta',
|
|
'type' => 'Verde',
|
|
'size' => 'M',
|
|
]))
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $matching->id)
|
|
->assertJsonPath('data.0.values.product', 'Camiseta')
|
|
->assertJsonPath('data.0.values.type', 'Verde')
|
|
->assertJsonPath('data.0.values.date', '-')
|
|
->assertJsonPath('data.0.values.size', 'M');
|
|
}
|
|
|
|
public function test_it_filters_computed_active_and_expired_statuses(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
$this->seed([AttributeSeeder::class, FiestaFutbolInfantilProductSeeder::class]);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$food = CatalogItem::query()
|
|
->where('tenant_code', $tenant->codigo)
|
|
->where('slug', 'comida')
|
|
->with('variants')
|
|
->firstOrFail();
|
|
$expired = $this->createTicket($tenant, $admin, [
|
|
'source_catalog_item_id' => $food->id,
|
|
'source_variant_id' => $food->variants->firstOrFail()->id,
|
|
]);
|
|
$active = $this->createTicket($tenant, $admin);
|
|
|
|
$this->travelTo('2026-10-20 12:00:00');
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?status=expired')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $expired->id);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?status=active')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $active->id);
|
|
}
|
|
|
|
public function test_it_filters_persisted_ticket_statuses(): void
|
|
{
|
|
$tenant = $this->createTenant('ticket-statuses');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
foreach ([
|
|
Ticket::STATUS_DISABLED => 'disabled_at',
|
|
Ticket::STATUS_CANCELLED => 'cancelled_at',
|
|
Ticket::STATUS_REFUNDED => 'refunded_at',
|
|
] as $status => $timestamp) {
|
|
$ticket = $this->createTicket($tenant, $admin, [$timestamp => now()]);
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets?status='.$status)
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonPath('data.0.id', $ticket->id)
|
|
->assertJsonPath('data.0.status', $status);
|
|
}
|
|
}
|
|
|
|
public function test_it_downloads_filtered_ticket_reports(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$admin = $this->createAdminAppUser($tenant);
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($admin);
|
|
|
|
$ticket = $this->createTicket($tenant, $admin);
|
|
$query = http_build_query([
|
|
'q' => (string) $ticket->id,
|
|
'timezone' => 'America/Argentina/Buenos_Aires',
|
|
]);
|
|
|
|
$this->get('/api/v1/adminapp/tenant/tickets/pdf?'.$query)
|
|
->assertOk()
|
|
->assertHeader('content-type', 'application/pdf');
|
|
|
|
$this->get('/api/v1/adminapp/tenant/tickets/excel?'.$query)
|
|
->assertOk()
|
|
->assertHeader(
|
|
'content-type',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
);
|
|
}
|
|
|
|
public function test_ticket_reports_require_a_valid_timezone(): void
|
|
{
|
|
$tenant = $this->createTenant('fiesta_futbol_infantil');
|
|
$this->grantTicketsMenu($tenant);
|
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
|
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets/pdf')->assertUnprocessable();
|
|
$this->getJson('/api/v1/adminapp/tenant/tickets/excel?timezone=Invalid')
|
|
->assertUnprocessable();
|
|
}
|
|
|
|
private function createTenant(string $code): Tenant
|
|
{
|
|
$headerLogo = $this->createAttachment("{$code}-header.png");
|
|
$footerLogo = $this->createAttachment("{$code}-footer.png");
|
|
|
|
return Tenant::query()->create([
|
|
'codigo' => $code,
|
|
'nombre' => ucfirst($code),
|
|
'dominio' => "{$code}.test",
|
|
'website_type_code' => 'onticket',
|
|
'primary_color' => '#111111',
|
|
'secondary_color' => '#222222',
|
|
'danger_color' => '#333333',
|
|
'success_color' => '#444444',
|
|
'header_bg_color' => '#ffffff',
|
|
'footer_bg_color' => '#ffffff',
|
|
'header_logo_id' => $headerLogo->id,
|
|
'footer_logo_id' => $footerLogo->id,
|
|
]);
|
|
}
|
|
|
|
private function createAttachment(string $filename): Attachment
|
|
{
|
|
return Attachment::query()->create([
|
|
'path' => "test/{$filename}",
|
|
'filename' => $filename,
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
]);
|
|
}
|
|
|
|
private function createAdminAppUser(Tenant $tenant): User
|
|
{
|
|
return User::factory()->create([
|
|
'rol_codigo' => RoleCode::AdminApp->value,
|
|
'tenant_codigo' => $tenant->codigo,
|
|
]);
|
|
}
|
|
|
|
private function createPurchase(Tenant $tenant, User $user, string $createdAt): Purchase
|
|
{
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'user_id' => $user->id,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'nombre_apellido' => $user->nombre_apellido,
|
|
'total' => '0.00',
|
|
]);
|
|
$purchase->forceFill(['created_at' => $createdAt, 'updated_at' => $createdAt])->saveQuietly();
|
|
|
|
return $purchase;
|
|
}
|
|
|
|
private function createPurchaseItem(Purchase $purchase, CatalogItem $item, Variant $variant): PurchaseItem
|
|
{
|
|
return $purchase->items()->create([
|
|
'source_catalog_item_id' => $item->id,
|
|
'source_variant_id' => $variant->id,
|
|
'nombre' => $item->nombre,
|
|
'descripcion' => $item->descripcion,
|
|
'slug' => $item->slug,
|
|
'item_nombre' => $item->nombre,
|
|
'variant_attributes' => [],
|
|
'cantidad' => 1,
|
|
'precio_unitario' => 0,
|
|
'total' => 0,
|
|
]);
|
|
}
|
|
|
|
/** @return array{Ticket, PurchaseItem} */
|
|
private function createRefundableTicket(Tenant $tenant, User $admin, string $amount): array
|
|
{
|
|
$catalogItem = CatalogItem::query()->create([
|
|
'tenant_code' => $tenant->codigo,
|
|
'slug' => 'ticket-reembolsable-'.Str::uuid(),
|
|
'nombre' => 'Ticket reembolsable',
|
|
'precio' => $amount,
|
|
]);
|
|
$purchase = Purchase::query()->create([
|
|
'tenant_codigo' => $tenant->codigo,
|
|
'user_id' => $admin->id,
|
|
'status' => Purchase::STATUS_PAID,
|
|
'nombre_apellido' => $admin->nombre_apellido,
|
|
'total' => $amount,
|
|
]);
|
|
$purchaseItem = PurchaseItem::query()->create([
|
|
'compra_id' => $purchase->id,
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
'nombre' => 'Ticket reembolsable',
|
|
'descripcion' => '',
|
|
'slug' => 'ticket-reembolsable',
|
|
'item_nombre' => 'Ticket reembolsable',
|
|
'cantidad' => 1,
|
|
'precio_unitario' => $amount,
|
|
'total' => $amount,
|
|
]);
|
|
|
|
return [
|
|
$this->createTicket($tenant, $admin, [
|
|
'source_purchase_item_id' => $purchaseItem->id,
|
|
'source_catalog_item_id' => $catalogItem->id,
|
|
]),
|
|
$purchaseItem,
|
|
];
|
|
}
|
|
|
|
private function grantTicketsMenu(Tenant $tenant): void
|
|
{
|
|
$menu = Menu::query()->create([
|
|
'code' => 'adminapp.tickets',
|
|
'label' => 'Tickets',
|
|
'route' => '/admin/tickets',
|
|
]);
|
|
|
|
$tenant->menues()->attach($menu->code);
|
|
}
|
|
|
|
/** @param array<string, mixed> $attributes */
|
|
private function createTicket(Tenant $tenant, User $user, array $attributes = []): Ticket
|
|
{
|
|
return Ticket::query()->create(array_merge([
|
|
'tenant_code' => $tenant->codigo,
|
|
'ticket' => (string) Str::uuid(),
|
|
'user_id' => $user->id,
|
|
], $attributes));
|
|
}
|
|
}
|