Files
fewo-stommer/app/Pricing/OfferShowcase.php
T
Lars StommerandClaude Opus 5.5 7d0bc93abc Offers by their own rules, seasonal photos per section, theme authoring tooling
- Special offers: kinds (rule, fixed dates, gap filler), package price, priority, highlight;
  GapFinder detects calendar gaps (optionally waiving the minimum stay); OfferShowcase decides
  what the website advertises and prices fixed dates/gaps with PriceCalculator; offers section
  shows them side by side, as a list or rotating; hero can show the top offer. Appearances no
  longer carry a featured offer.
- Sections get stable ids; appearances set photos per section ("Bilder je Baustein") and
  sections can be limited to appearances. Photo pickers show thumbnails everywhere.
- Themes: themes:make (scaffold, never overwrites), themes:check (contract, build, markup,
  colours, external resources, WCAG contrast), themes:preview (signed links per page and
  appearance), signed ?theme= preview and preview action in the site form, inherited colour
  tokens in the admin. Base CSS split into theme.css + reusable theme-base.css.
- docs/THEME-ERSTELLUNG.md: binding guide for building additional themes elsewhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:51:38 +02:00

157 lines
6.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Pricing;
use App\Availability\AvailabilityChecker;
use App\Availability\GapFinder;
use App\Enums\OfferKind;
use App\Enums\QuoteLineType;
use App\Models\Property;
use App\Models\SpecialOffer;
use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
/**
* Which offers the website advertises right now – independent of the seasonal appearance.
*
* Shown are active, public offers without a code whose booking window is open. Fixed dates only while they
* are still free; gap fillers once per free gap found in the calendar. Fixed dates and gaps are priced
* with PriceCalculator, so the advertised price is exactly what the guest is quoted. Order: highlighted
* first, then priority, then the earliest date.
*/
class OfferShowcase
{
/** Gap fillers without a lead time limit look this many days ahead. */
public const GAP_HORIZON_DAYS = 60;
/** At most this many gaps per gap filler. */
public const GAPS_PER_OFFER = 3;
public function __construct(
private PriceCalculator $calculator,
private AvailabilityChecker $availability = new AvailabilityChecker,
private GapFinder $gaps = new GapFinder,
) {}
/**
* @return Collection<int, AdvertisedOffer>
*/
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<AdvertisedOffer>
*/
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<AdvertisedOffer>
*/
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<AdvertisedOffer>
*/
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,
);
}
}