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

@@ -41,7 +41,9 @@ class AdminAppTicketExcelService
$row = $index + 2;
foreach ($columns as $columnIndex => $column) {
$coordinate = Coordinate::stringFromColumnIndex($columnIndex + 1).$row;
$value = $ticket[$column['key']] ?? null;
$value = $column['type'] === 'status'
? ($ticket['status_label'] ?? $ticket[$column['key']] ?? null)
: ($ticket[$column['key']] ?? null);
if ($column['type'] === 'currency' && $value !== null) {
$sheet->setCellValue($coordinate, (float) $value);

View File

@@ -23,6 +23,7 @@ class AdminAppTicketRowService
public function details(Ticket $ticket): array
{
$purchaseItem = $ticket->sourcePurchaseItem;
$refund = $ticket->refund;
return [
'source_purchase_item_id' => $ticket->source_purchase_item_id,
@@ -31,7 +32,9 @@ class AdminAppTicketRowService
?? $ticket->sourceCatalogItem?->nombre
?? $ticket->name,
'amount' => $purchaseItem?->precio_unitario,
'refunded_amount' => $purchaseItem?->refunded_amount,
'refunded_amount' => $refund?->amount ?? $purchaseItem?->refunded_amount,
'refund_type' => $refund?->type,
'refund_type_label' => $refund?->typeLabel(),
'client' => $purchaseItem?->purchase?->nombre_apellido ?? $ticket->user?->nombre_apellido,
'status' => $ticket->status,
'scanned_by' => $ticket->scannerUser?->nombre_apellido,
@@ -57,6 +60,7 @@ class AdminAppTicketRowService
'client' => $details['client'] ?? 'Sin nombre',
'id' => $ticket->id,
'status' => $details['status'],
'status_label' => $ticket->status_label,
'scanned_by' => $details['scanned_by'] ?? '-',
];
}
@@ -80,7 +84,9 @@ class AdminAppTicketRowService
return $rows->map(fn (array $row): array => collect($columns)
->mapWithKeys(fn (array $column): array => [
$column['key'] => $this->displayValue(
$row[$column['key']] ?? null,
$column['type'] === 'status'
? ($row['status_label'] ?? $row[$column['key']] ?? null)
: ($row[$column['key']] ?? null),
$column['type'],
$timeZone,
),

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,
};
}