124 lines
4.8 KiB
PHP
124 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticketing\Desfile\Services;
|
|
|
|
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
|
use App\Domains\Commerce\Catalog\Models\Variant;
|
|
use App\Domains\Core\Auth\Models\User;
|
|
use App\Domains\Ticketing\Ticket\Models\EntryReservation;
|
|
use App\Domains\Ticketing\Ticket\Models\Ticket;
|
|
use App\Domains\Ticketing\Ticket\Services\EntryReservationService as TicketEntryReservationService;
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/** Adapts the Desfile API and seat rules to the catalog-based reservation workflow. */
|
|
class DesfileEntryReservationService
|
|
{
|
|
private const SELECTION_RELATIONS = [
|
|
'variant.catalogItem.itemAttributes.attribute.options',
|
|
'variant.definitions.itemAttribute.attribute.options',
|
|
'variant.eventDates',
|
|
'variant.eventDate',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly TicketEntryReservationService $reservations,
|
|
private readonly DesfileEntryReservationPolicy $policy,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{tipo_pago?: string|null, page?: int, per_page?: int} $filters
|
|
* @return LengthAwarePaginator<EntryReservation>
|
|
*/
|
|
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<int, EntryReservation>
|
|
*/
|
|
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<array{variant_id: int, tipo_pago: string}> $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(', ');
|
|
}
|
|
}
|