'Januar', 2 => 'Februar', 3 => 'März', 4 => 'April', 5 => 'Mai', 6 => 'Juni', 7 => 'Juli', 8 => 'August', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Dezember', ]; private const TYPE_LABELS = [ 'flat' => 'pauschal', 'per_person' => 'p.P.', 'per_night' => 'p.Nacht', 'per_person_per_night' => 'p.P./Nacht', ]; public Accommodation $accommodation; public ?string $startMonth = null; /** @var list */ public array $months = []; public string $prevMonth = ''; public string $nextMonth = ''; public string $prevLabel = ''; public string $nextLabel = ''; public function __construct( private readonly ContingentsClient $contingentsClient, private readonly CacheInterface $cache, ) { } public function mount(Accommodation $accommodation, ?string $startMonth = null): void { $this->accommodation = $accommodation; $this->startMonth = $startMonth; if ($startMonth !== null && preg_match('/^\d{4}-\d{2}$/', $startMonth)) { $start = CarbonImmutable::createFromFormat('Y-m-d', $startMonth . '-01'); } else { $allDates = $this->collectAllDates(); $start = empty($allDates) ? CarbonImmutable::now()->startOfMonth() : $this->determineStartMonth($allDates); } $map = $this->buildCoverageMap(); $blockedDates = $this->fetchBlockedDates( $accommodation->getCalendarCode() ?? '', $start->format('Y-m-d'), $start->addMonths(3)->subDay()->format('Y-m-d'), ); $months = []; $current = $start; for ($i = 0; $i < 3; $i++) { $months[] = $this->buildMonth($current, $map, $blockedDates); $current = $current->addMonth(); } $this->months = $months; $prevStart = $start->subMonths(3); $prevEnd = $prevStart->addMonths(2); $this->prevMonth = $prevStart->format('Y-m'); $this->prevLabel = self::MONTHS[$prevStart->month] . ' – ' . self::MONTHS[$prevEnd->month] . ' ' . $prevEnd->year; $nextStart = $start->addMonths(3); $nextEnd = $nextStart->addMonths(2); $this->nextMonth = $nextStart->format('Y-m'); $this->nextLabel = self::MONTHS[$nextStart->month] . ' – ' . self::MONTHS[$nextEnd->month] . ' ' . $nextEnd->year; } /** * @return array keyed by Y-m-d, only BLOCKED dates present */ private function fetchBlockedDates(string $hotelCode, string $dateFrom, string $dateTo): array { if ($hotelCode === '') { return []; } try { $cacheKey = sprintf('contingents_calendar_%s_%s_%s', $hotelCode, $dateFrom, $dateTo); $response = $this->cache->get( $cacheKey, function (ItemInterface $item) use ($hotelCode, $dateFrom, $dateTo) { $item->expiresAfter(3600); return $this->contingentsClient->getContingentCalendar($hotelCode, $dateFrom, $dateTo); }, ); } catch (BpnConnectException|InvalidArgumentException) { return []; } $blocked = []; foreach ($response->data as $entry) { if ($entry->status === ContingentStatus::Blocked) { $blocked[(new \DateTimeImmutable($entry->date))->format('Y-m-d')] = true; } } return $blocked; } /** * @return CarbonImmutable[] */ private function collectAllDates(): array { $dates = []; foreach ($this->accommodation->getAccommodationPrices() as $price) { if ($price->getDateFrom() !== null) { $dates[] = CarbonImmutable::instance($price->getDateFrom()); } if ($price->getDateTo() !== null) { $dates[] = CarbonImmutable::instance($price->getDateTo()); } } foreach ($this->accommodation->getBoardServices() as $service) { if ($service->getDateFrom() !== null) { $dates[] = CarbonImmutable::instance($service->getDateFrom()); } if ($service->getDateTo() !== null) { $dates[] = CarbonImmutable::instance($service->getDateTo()); } } foreach ($this->accommodation->getAdditionalServices() as $service) { if ($service->getDateFrom() !== null) { $dates[] = CarbonImmutable::instance($service->getDateFrom()); } if ($service->getDateTo() !== null) { $dates[] = CarbonImmutable::instance($service->getDateTo()); } } return $dates; } /** * @param CarbonImmutable[] $dates */ private function determineStartMonth(array $dates): CarbonImmutable { $earliest = min($dates); $cutoff = CarbonImmutable::now()->subDays(60); if ($earliest >= $cutoff) { return $earliest->startOfMonth(); } return CarbonImmutable::now()->startOfMonth(); } /** * @return array */ private function buildCoverageMap(): array { $map = []; foreach ($this->accommodation->getAccommodationPrices() as $price) { if ($price->getDateFrom() === null || $price->getDateTo() === null) { continue; } $priceFormatted = number_format($price->getPricePerNight() / 100, 2, ',', '.') . ' €'; $label = sprintf('Preis: %s/Nacht (min. %d Nächte)', $priceFormatted, $price->getMinNights()); $day = CarbonImmutable::instance($price->getDateFrom()); $end = CarbonImmutable::instance($price->getDateTo()); $span = $day->diffInDays($end); $priority = $price->getType()?->priority() ?? 0; $limit = $day->addYears(3); // $end is the last night (inclusive), so <= is correct while ($day <= $end && $day <= $limit) { $key = $day->format('Y-m-d'); $map[$key] ??= ['price' => null, 'board' => null, 'additional' => null]; $existing = $map[$key]['price']; $replace = $existing === null || $priority > $existing['priority'] || ($priority === $existing['priority'] && $span < $existing['span']); if ($replace) { $map[$key]['price'] = ['label' => $label, 'span' => $span, 'priority' => $priority]; } $day = $day->addDay(); } } foreach ($this->accommodation->getBoardServices() as $service) { if ($service->getDateFrom() === null || $service->getDateTo() === null) { continue; } $label = 'Verpflegung: ' . $service->getLabel(); $day = CarbonImmutable::instance($service->getDateFrom()); $end = CarbonImmutable::instance($service->getDateTo()); $span = $day->diffInDays($end); $limit = $day->addYears(3); while ($day <= $end && $day <= $limit) { $key = $day->format('Y-m-d'); $map[$key] ??= ['price' => null, 'board' => null, 'additional' => null]; if ($map[$key]['board'] === null || $span < $map[$key]['board']['span']) { $map[$key]['board'] = ['label' => $label, 'span' => $span]; } $day = $day->addDay(); } } foreach ($this->accommodation->getAdditionalServices() as $service) { if ($service->getDateFrom() === null || $service->getDateTo() === null) { continue; } $typeLabel = $service->getType() !== null ? self::TYPE_LABELS[$service->getType()->value] : ''; $label = sprintf('Zusatz: %s%s', $service->getLabel(), $typeLabel !== '' ? ' (' . $typeLabel . ')' : ''); $day = CarbonImmutable::instance($service->getDateFrom()); $end = CarbonImmutable::instance($service->getDateTo()); $span = $day->diffInDays($end); $limit = $day->addYears(3); while ($day <= $end && $day <= $limit) { $key = $day->format('Y-m-d'); $map[$key] ??= ['price' => null, 'board' => null, 'additional' => null]; if ($map[$key]['additional'] === null || $span < $map[$key]['additional']['span']) { $map[$key]['additional'] = ['label' => $label, 'span' => $span]; } $day = $day->addDay(); } } return $map; } /** * @param array $map * @param array $blockedDates */ private function buildMonth(CarbonImmutable $month, array $map, array $blockedDates): CalendarMonth { $start = $month->startOfMonth(); $today = CarbonImmutable::now()->format('Y-m-d'); // Mon=0 … Sun=6 (Carbon: 0=Sunday, 1=Monday … 6=Saturday) $firstDow = ($start->dayOfWeek + 6) % 7; $days = []; for ($d = 1; $d <= $start->daysInMonth; $d++) { $dateStr = $month->format('Y-m') . '-' . str_pad((string) $d, 2, '0', STR_PAD_LEFT); $coverage = $map[$dateStr] ?? []; $parts = []; if (isset($coverage['price'])) { $parts[] = $coverage['price']['label']; } if (isset($coverage['board'])) { $parts[] = $coverage['board']['label']; } if (isset($coverage['additional'])) { $parts[] = $coverage['additional']['label']; } $isBlocked = isset($blockedDates[$dateStr]); if ($isBlocked) { $parts[] = 'Buchungsstopp'; } $days[$d] = new CalendarDay( date: $dateStr, hasPrice: isset($coverage['price']), hasBoard: isset($coverage['board']), hasAdditional: isset($coverage['additional']), isBlocked: $isBlocked, tooltip: !empty($parts) ? implode(' | ', $parts) : null, isToday: $dateStr === $today, ); } return new CalendarMonth( label: self::MONTHS[$month->month] . ' ' . $month->year, firstDow: $firstDow, days: $days, ); } }