*/ public function current(Property $property, ?CarbonImmutable $now = null, ?int $limit = null, bool $featuredOnly = false): Collection { $now ??= CarbonImmutable::now(); $today = $now->startOfDay(); $offers = $property->specialOffers() ->active() ->where('is_public', true) ->whereNull('code') ->when($featuredOnly, fn ($query) => $query->where('is_featured', true)) ->where(fn ($query) => $query->whereNull('bookable_from')->orWhere('bookable_from', '<=', $now)) ->where(fn ($query) => $query->whereNull('bookable_until')->orWhere('bookable_until', '>=', $now)) ->get(); $advertised = $offers->flatMap(fn (SpecialOffer $offer): array => match ($offer->kind ?? OfferKind::Rule) { OfferKind::Rule => $this->rule($offer, $today), OfferKind::FixedDates => $this->fixedDates($property, $offer, $now), OfferKind::GapFiller => $this->gapFiller($property, $offer, $now), }); $sorted = $advertised->sortBy([ fn (AdvertisedOffer $a, AdvertisedOffer $b): int => $b->offer->is_featured <=> $a->offer->is_featured, fn (AdvertisedOffer $a, AdvertisedOffer $b): int => $b->offer->priority <=> $a->offer->priority, fn (AdvertisedOffer $a, AdvertisedOffer $b): int => ($a->arrival?->getTimestamp() ?? PHP_INT_MAX) <=> ($b->arrival?->getTimestamp() ?? PHP_INT_MAX), ])->values(); return $limit ? $sorted->take($limit)->values() : $sorted; } /** * @return list */ private function rule(SpecialOffer $offer, CarbonImmutable $today): array { if ($offer->stay_until !== null && $offer->stay_until->lessThanOrEqualTo($today)) { return []; } return [$this->advertised($offer)]; } /** * @return list */ private function fixedDates(Property $property, SpecialOffer $offer, CarbonImmutable $now): array { if ($offer->stay_from === null || $offer->stay_until === null || $offer->stay_from->lessThanOrEqualTo($now->startOfDay())) { return []; } if (! $this->availability->isAvailable($property, $offer->stay_from, $offer->stay_until)) { return []; } $quote = $this->pricedWith($property, $offer, $offer->stay_from, $offer->stay_until, $now); return $quote ? [$this->advertised($offer, $offer->stay_from, $offer->stay_until, $quote)] : []; } /** * @return list */ private function gapFiller(Property $property, SpecialOffer $offer, CarbonImmutable $now): array { $today = $now->startOfDay(); $from = $today->addDays(max(1, $offer->min_days_before_arrival ?? 1)); $until = $today->addDays($offer->max_days_before_arrival ?? self::GAP_HORIZON_DAYS)->addDay(); if ($offer->stay_from !== null && $offer->stay_from->greaterThan($from)) { $from = $offer->stay_from; } $advertised = []; foreach ($this->gaps->gaps($property, $from, $until, $offer->max_gap_nights ?? 7) as [$arrival, $departure]) { $quote = $this->pricedWith($property, $offer, $arrival, $departure, $now); if ($quote !== null) { $advertised[] = $this->advertised($offer, $arrival, $departure, $quote); } if (count($advertised) >= self::GAPS_PER_OFFER) { break; } } return $advertised; } /** * The quote for the stay, if the offer is actually applied to it (it may lose against a better offer * or fail a condition such as the minimum stay); priced for the number of guests included in the rent. */ private function pricedWith(Property $property, SpecialOffer $offer, CarbonImmutable $arrival, CarbonImmutable $departure, CarbonImmutable $now): ?PriceQuote { $quote = rescue( fn (): PriceQuote => $this->calculator->calculate($property, new StayRequest($arrival, $departure, max(1, $property->included_guests)), $now), null, false, ); $applied = $quote !== null && collect($quote->lines) ->contains(fn (QuoteLine $line): bool => $line->type === QuoteLineType::Discount && $line->label === $offer->name); return $applied ? $quote : null; } private function advertised(SpecialOffer $offer, ?CarbonImmutable $arrival = null, ?CarbonImmutable $departure = null, ?PriceQuote $quote = null): AdvertisedOffer { return new AdvertisedOffer( offer: $offer, title: $offer->name, description: $offer->public_description, advantage: $offer->advantageLabel(), conditions: $offer->conditionSummary(), arrival: $arrival, departure: $departure, quote: $quote, ); } }