['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 $tickets * @return Collection> */ public function rows(Collection $tickets): Collection { return $tickets->values()->map(fn (Ticket $ticket): array => $this->row($ticket)); } /** @return array */ 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 $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 $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 $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', }; } }