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

@@ -7,6 +7,7 @@ use App\Domains\Purchase\Models\PurchaseItem;
use App\Domains\Purchase\Services\PurchaseRefundSummaryService;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Models\Ticket;
use App\Domains\Ticket\Models\TicketRefund;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@@ -23,6 +24,7 @@ class AdminAppTicketService
'scannerUser',
'sourceCatalogItem.category',
'sourcePurchaseItem.purchase',
'refund.createdBy',
];
public function __construct(
@@ -162,11 +164,15 @@ class AdminAppTicketService
];
}
public function refund(Tenant $tenant, int $ticketId, string $refundType): Ticket
{
public function refund(
Tenant $tenant,
int $ticketId,
string $refundType,
?User $createdBy = null,
): Ticket {
$this->ensureRefundIsAllowed($tenant, $refundType);
return DB::transaction(function () use ($tenant, $ticketId, $refundType): Ticket {
return DB::transaction(function () use ($tenant, $ticketId, $refundType, $createdBy): Ticket {
$ticket = Ticket::query()
->where('tenant_code', $tenant->codigo)
->lockForUpdate()
@@ -206,6 +212,14 @@ class AdminAppTicketService
$ticket->markAsRefunded();
$ticket->save();
TicketRefund::query()->create([
'ticket_id' => $ticket->id,
'purchase_item_id' => $purchaseItem->id,
'created_by_user_id' => $createdBy?->id,
'type' => $refundType,
'amount' => number_format($refundAmount, 2, '.', ''),
]);
$purchaseItem->update([
'refunded_amount' => number_format($refundedAmount, 2, '.', ''),
]);
@@ -217,8 +231,8 @@ class AdminAppTicketService
private function ensureRefundIsAllowed(Tenant $tenant, string $refundType): void
{
$isAllowed = match ($refundType) {
'partial' => $tenant->allow_refund() && $tenant->allow_partial_refund(),
'total' => $tenant->allow_refund() && (bool) $tenant->allow_ticket_total_refund,
TicketRefund::TYPE_PARTIAL => $tenant->allow_refund() && $tenant->allow_partial_refund(),
TicketRefund::TYPE_TOTAL => $tenant->allow_refund() && (bool) $tenant->allow_ticket_total_refund,
};
if (! $isAllowed) {
@@ -233,8 +247,8 @@ class AdminAppTicketService
$ticketAmount = (float) $purchaseItem->precio_unitario;
return match ($refundType) {
'partial' => round($ticketAmount * (float) $tenant->ticket_partial_refund_percentage / 100, 2),
'total' => $ticketAmount,
TicketRefund::TYPE_PARTIAL => round($ticketAmount * (float) $tenant->ticket_partial_refund_percentage / 100, 2),
TicketRefund::TYPE_TOTAL => $ticketAmount,
};
}