feat(tickets): expose normalized values for dynamic columns

This commit is contained in:
2026-08-31 12:31:24 -03:00
parent adce4ab59e
commit fbfcb5cf63
5 changed files with 237 additions and 154 deletions

View File

@@ -3,19 +3,11 @@
namespace App\Domains\Ticket\Services;
use App\Domains\Ticket\Models\Ticket;
use App\Domains\Ticket\Resources\AdminApp\AdminAppTicketResource;
use Illuminate\Support\Collection;
class AdminAppTicketReportService
{
private const CATEGORY_PRESENTATIONS = [
'alojamientos' => ['category' => 'Camping', 'product' => 'tipo_alojamiento', 'type' => null],
'camping' => ['category' => null, 'product' => 'tipo_alojamiento', 'type' => null],
'entradas' => ['category' => null, 'product' => 'product', 'type' => null],
'comidas' => ['category' => 'Comida', 'product' => 'event_date', 'type' => 'horario'],
'comida' => ['category' => null, 'product' => 'event_date', 'type' => 'horario'],
'merchandising' => ['category' => null, 'product' => 'product', 'type' => 'color'],
];
public function __construct(private readonly AdminAppTicketRowService $rowService) {}
/**
* @param Collection<int, Ticket> $tickets
@@ -23,90 +15,21 @@ class AdminAppTicketReportService
*/
public function rows(Collection $tickets): Collection
{
return $tickets->values()->map(fn (Ticket $ticket): array => $this->row($ticket));
}
/** @return array<string, mixed> */
private function row(Ticket $ticket): array
{
$data = (new AdminAppTicketResource($ticket))->resolve();
$presentation = $this->presentation($data);
return [
'order_number' => $data['order_number'],
'category' => $presentation['category'],
'product' => $presentation['product'],
'type' => $presentation['type'],
'amount' => $data['amount'] === null ? null : (float) $data['amount'],
'client' => $data['client'] ?? 'Sin nombre',
'ticket' => $data['ticket'],
'date' => $data['date'],
'status' => $this->statusLabel($data['status']),
'scanned_by' => $data['scanned_by'] ?? '-',
];
return $this->rowService->rows($tickets);
}
/**
* @param array<string, mixed> $ticket
* @return array{category: string, product: string, type: string}
* @param Collection<int, array<string, mixed>> $rows
* @param list<array<string, mixed>> $columns
* @return Collection<int, array<string, string>>
*/
private function presentation(array $ticket): array
public function displayRows(Collection $rows, array $columns, string $timeZone): Collection
{
$sourceCategory = trim((string) ($ticket['category'] ?? '')) ?: '-';
$configuration = self::CATEGORY_PRESENTATIONS[mb_strtolower($sourceCategory)] ?? null;
if ($configuration === null) {
return [
'category' => $sourceCategory,
'product' => (string) ($ticket['product'] ?: $ticket['name'] ?: '-'),
'type' => $this->allPropertyLabels($ticket) ?: '-',
];
}
return [
'category' => $configuration['category'] ?? $sourceCategory,
'product' => $configuration['product'] === 'product'
? (string) ($ticket['product'] ?: $ticket['name'] ?: '-')
: ($this->propertyLabels($ticket, $configuration['product']) ?: '-'),
'type' => $configuration['type'] === null
? '-'
: ($this->propertyLabels($ticket, $configuration['type']) ?: '-'),
];
return $this->rowService->displayRows($rows, $columns, $timeZone);
}
/** @param array<string, mixed> $ticket */
private function propertyLabels(array $ticket, string $code): string
public function displayValue(mixed $value, string $type, string $timeZone): string
{
$property = collect($ticket['variant_properties'] ?? [])->firstWhere('code', $code);
$labels = collect($property['values'] ?? [])->pluck('label')->filter();
if ($code === 'event_date') {
$labels = $labels->map(function (string $label): string {
[$day, $month] = array_pad(explode('/', $label), 2, null);
return $day !== null && $month !== null ? "{$day}/{$month}" : $label;
});
}
return $labels->implode(', ');
}
/** @param array<string, mixed> $ticket */
private function allPropertyLabels(array $ticket): string
{
return collect($ticket['variant_properties'] ?? [])
->flatMap(fn (array $property): array => $property['values'] ?? [])
->pluck('label')
->filter()
->implode(', ');
}
private function statusLabel(string $status): string
{
return match ($status) {
Ticket::STATUS_USED => 'Usado',
Ticket::STATUS_EXPIRED => 'Vencido',
default => 'Activo',
};
return $this->rowService->displayValue($value, $type, $timeZone);
}
}