*/ public function reservations(User $user, array $filters = []): LengthAwarePaginator { $result = $this->reservations->reservations($user, [ ...$filters, 'catalog_item_id' => $this->entryId($user), ]); $result->getCollection()->loadMissing(self::SELECTION_RELATIONS); return $result; } /** * @param array{tipo_pago?: string|null} $filters * @return Collection */ public function reservationsForExport(User $user, array $filters = []): Collection { return $this->reservations->reservationsForExport($user, [ ...$filters, 'catalog_item_id' => $this->entryId($user), ])->loadMissing(self::SELECTION_RELATIONS); } public function reservationTicket(User $user, int $reservationId): Ticket { return $this->reservations->reservationTicket($user, $reservationId, $this->entryId($user)); } public function cancel(User $user, int $reservationId): void { $this->reservations->cancel($user, $reservationId, $this->entryId($user)); } /** @param list $rows */ public function reserve(User $user, string $key, array $rows): Collection { $this->authorize($user); $entry = CatalogItem::query()->where('tenant_code', $user->tenant_codigo) ->where('slug', 'entrada')->firstOrFail(); $catalogRows = array_map(fn (array $row): array => [ ...$row, 'catalog_item_id' => $entry->id, ], $rows); try { return $this->reservations->reserve($user, $key, $catalogRows, $this->policy) ->loadMissing(self::SELECTION_RELATIONS); } catch (ValidationException $exception) { // Keep the row fields used by the form and describe the selected seat. $variants = $entry->variants()->whereKey(array_column($rows, 'variant_id')) ->with(['definitions.itemAttribute.attribute.options'])->get()->keyBy('id'); $errors = []; foreach ($exception->errors() as $field => $messages) { if (preg_match('/^rows\.(\d+)\.variant_id$/', $field, $match)) { $index = (int) $match[1]; $label = $this->entryLabel($variants->get($rows[$index]['variant_id'] ?? null), $index); $messages = array_map(fn (string $message): string => $label.': '.$message, $messages); } $errors[$field] = $messages; } throw ValidationException::withMessages($errors); } } private function authorize(User $user): void { abort_unless($user->tenant_codigo === 'desfile_pura_tendencia', 403); } private function entryId(User $user): int { $this->authorize($user); // Historical reservations remain accessible when the active event changes. // A missing entry must not remove the catalog filter from the generic query. return CatalogItem::query()->where('tenant_code', $user->tenant_codigo) ->where('slug', 'entrada')->value('id') ?? -1; } private function entryLabel(?Variant $variant, int $index): string { if ($variant === null) { return 'Entrada '.($index + 1); } $values = $variant->selectionValues(); return collect(['tipo' => 'Tipo', 'sector' => 'Sector', 'fila' => 'Fila', 'asiento' => 'Asiento']) ->map(fn (string $label, string $key): string => $label.': '.($values->get($key) ?? 'sin especificar')) ->implode(', '); } }