Files
shopit-back/app/Domains/Ticket/Services/AdminAppTicketReportService.php

113 lines
4.2 KiB
PHP

<?php
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'],
];
/**
* @param Collection<int, Ticket> $tickets
* @return Collection<int, array<string, mixed>>
*/
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'] ?? '-',
];
}
/**
* @param array<string, mixed> $ticket
* @return array{category: string, product: string, type: string}
*/
private function presentation(array $ticket): array
{
$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']) ?: '-'),
];
}
/** @param array<string, mixed> $ticket */
private function propertyLabels(array $ticket, string $code): 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',
};
}
}