feat(ticket): persist and expose refund details

This commit is contained in:
2026-09-14 11:57:31 -03:00
parent adec5d1725
commit caed44fcc8
10 changed files with 133 additions and 15 deletions

View File

@@ -17,6 +17,7 @@ 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 App\Domains\Ticket\Models\TicketRefund;
use Barryvdh\DomPDF\ServiceProvider as DomPdfServiceProvider;
use Database\Seeders\AttributeSeeder;
use Database\Seeders\AuthorizationSeeder;
@@ -192,10 +193,22 @@ class AdminAppTicketControllerTest extends TestCase
->assertOk()
->assertJsonPath('data.id', $ticket->id)
->assertJsonPath('data.status', Ticket::STATUS_REFUNDED)
->assertJsonPath('data.refunded_amount', '100.00');
->assertJsonPath('data.status_label', 'Reembolso total')
->assertJsonPath('data.refunded_amount', '100.00')
->assertJsonPath('data.refund.type', TicketRefund::TYPE_TOTAL)
->assertJsonPath('data.refund.type_label', 'Reembolso total')
->assertJsonPath('data.refund.amount', '100.00')
->assertJsonPath('data.refund.created_by', $admin->nombre_apellido);
$this->assertNotNull($ticket->fresh()->refunded_at);
$this->assertSame('100.00', $purchaseItem->fresh()->refunded_amount);
$this->assertDatabaseHas('ticket_refunds', [
'ticket_id' => $ticket->id,
'purchase_item_id' => $purchaseItem->id,
'created_by_user_id' => $admin->id,
'type' => TicketRefund::TYPE_TOTAL,
'amount' => '100.00',
]);
}
public function test_it_partially_refunds_a_ticket_using_the_tenant_percentage(): void
@@ -216,9 +229,58 @@ class AdminAppTicketControllerTest extends TestCase
])
->assertOk()
->assertJsonPath('data.status', Ticket::STATUS_REFUNDED)
->assertJsonPath('data.refunded_amount', '25.50');
->assertJsonPath('data.status_label', 'Reembolso parcial')
->assertJsonPath('data.refunded_amount', '25.50')
->assertJsonPath('data.refund.type', TicketRefund::TYPE_PARTIAL)
->assertJsonPath('data.refund.amount', '25.50');
$this->assertSame('25.50', $purchaseItem->fresh()->refunded_amount);
$this->assertDatabaseHas('ticket_refunds', [
'ticket_id' => $ticket->id,
'type' => TicketRefund::TYPE_PARTIAL,
'amount' => '25.50',
]);
}
public function test_it_records_different_refund_types_for_tickets_from_the_same_purchase_item(): void
{
$tenant = $this->createTenant('ticket-mixed-refunds');
$tenant->update([
'allow_ticket_refund' => true,
'allow_ticket_total_refund' => true,
'allow_ticket_partial_refund' => true,
'ticket_partial_refund_percentage' => 25.00,
]);
$admin = $this->createAdminAppUser($tenant);
$this->grantTicketsMenu($tenant);
Sanctum::actingAs($admin);
[$partialTicket, $purchaseItem] = $this->createRefundableTicket($tenant, $admin, '100.00');
$purchaseItem->update(['cantidad' => 2, 'total' => '200.00']);
$purchaseItem->purchase->update(['total' => '200.00']);
$totalTicket = $this->createTicket($tenant, $admin, [
'source_purchase_item_id' => $purchaseItem->id,
'source_catalog_item_id' => $partialTicket->source_catalog_item_id,
]);
$this->postJson("/api/v1/adminapp/tenant/tickets/{$partialTicket->id}/refund", [
'refund_type' => TicketRefund::TYPE_PARTIAL,
])->assertOk()->assertJsonPath('data.status_label', 'Reembolso parcial');
$this->postJson("/api/v1/adminapp/tenant/tickets/{$totalTicket->id}/refund", [
'refund_type' => TicketRefund::TYPE_TOTAL,
])->assertOk()->assertJsonPath('data.status_label', 'Reembolso total');
$this->assertSame('125.00', $purchaseItem->fresh()->refunded_amount);
$this->assertDatabaseHas('ticket_refunds', [
'ticket_id' => $partialTicket->id,
'type' => TicketRefund::TYPE_PARTIAL,
'amount' => '25.00',
]);
$this->assertDatabaseHas('ticket_refunds', [
'ticket_id' => $totalTicket->id,
'type' => TicketRefund::TYPE_TOTAL,
'amount' => '100.00',
]);
}
public function test_it_does_not_refund_a_ticket_when_the_requested_refund_type_is_disabled(): void