Phase 2: Kalender, Anfrage, Vorreservierung & Sonderangebote

- Gäste, Buchungen (Status-Workflow), Sperrzeiten, Sonderangebote
- Vorreservierung mit Haltezeit je Wohnung; abgelaufene Holds geben den Kalender sofort frei, Cron setzt Status und benachrichtigt
- Sonderangebote/Sonderkonditionen: %, € pro Aufenthalt, Sonderpreis/Nacht, Gratis-Nächte; Bedingungen u. a. Saison, Vorlauf, Wochentag, Nächte, Aktionscode; bestes Einzel- vs. kombinierte Angebote
- Öffentliche Seite /buchen: Belegungskalender, Live-Preis, Anfrageformular (Honeypot, Rate-Limit)
- E-Mails: Angebot an Gast, Benachrichtigung Vermieter, Bestätigung, Absage, Ablauf
- Filament: Anfragen & Buchungen (bestätigen, ablehnen, verlängern, stornieren, Telefonbuchung), Gäste, Sperrzeiten, Sonderangebote
- Deutsche Validierungstexte

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lars StommerandClaude Opus 5.5 committed 2026-10-22 09:43:12 +02:00
1 parent 2e9a391b37
commit 08b6582fa1
79 files changed
+6213 -76

No files matched your search

+88 -37
View File
@@ -7,40 +7,44 @@
use App\Models\Extra;
use App\Models\Property;
use App\Models\Season;
use App\Models\SpecialOffer;
use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
class PriceCalculator
{
/**
* Price a stay from $arrival (first night) to $departure (day of departure, not a night).
*
* @param array<int, int> $extraQuantities quantity per extra id
* Price a stay. Special offers are applied automatically; $bookedAt (default: now) decides
* time-dependent offers such as last minute or early bird.
*
* @throws QuoteException
*/
public function calculate(
Property $property,
CarbonImmutable $arrival,
CarbonImmutable $departure,
int $guests,
array $extraQuantities = [],
): PriceQuote {
$arrival = $arrival->startOfDay();
$departure = $departure->startOfDay();
if ($departure->lessThanOrEqualTo($arrival)) {
public function calculate(Property $property, StayRequest $stay, ?CarbonImmutable $bookedAt = null): PriceQuote
{
if ($stay->departure->lessThanOrEqualTo($stay->arrival)) {
throw QuoteException::invalidPeriod();
}
if ($guests < 1 || $guests > $property->max_guests) {
if ($stay->guests < 1 || $stay->guests > $property->max_guests) {
throw QuoteException::invalidGuestCount($property->max_guests);
}
$nights = (int) $arrival->diffInDays($departure);
$lines = $this->accommodationLines($property, $arrival, $departure);
$nights = $stay->nights();
$nightSeasons = $this->seasonPerNight($property, $stay);
$extraGuests = max(0, $guests - $property->included_guests);
$arrivalSeason = $nightSeasons[0];
if ($nights < $arrivalSeason->min_nights) {
throw QuoteException::minimumStay($arrivalSeason->min_nights, $arrivalSeason->name);
}
$lines = $this->accommodationLines($nightSeasons);
$appliedOffers = $this->bestOffers($property, $stay, $nightSeasons, $bookedAt ?? CarbonImmutable::now());
foreach ($appliedOffers as ['offer' => $offer, 'discount' => $discount]) {
$lines[] = new QuoteLine(QuoteLineType::Discount, $offer->name, 1, -$discount, -$discount);
}
$extraGuests = max(0, $stay->guests - $property->included_guests);
if ($extraGuests > 0 && $property->extra_guest_fee_per_night > 0) {
$lines[] = new QuoteLine(
QuoteLineType::ExtraGuest,
@@ -55,34 +59,47 @@ public function calculate(
$lines[] = new QuoteLine(QuoteLineType::Cleaning, 'Endreinigung & Service', 1, $property->cleaning_fee, $property->cleaning_fee);
}
array_push($lines, ...$this->extraLines($property, $extraQuantities, $nights));
array_push($lines, ...$this->extraLines($property, $stay->extras, $nights));
return new PriceQuote(
arrival: $arrival,
departure: $departure,
guests: $guests,
arrival: $stay->arrival,
departure: $stay->departure,
guests: $stay->guests,
lines: $lines,
depositPercent: $property->deposit_percent,
balanceDueOn: $arrival->subDays($property->balance_due_days_before_arrival),
balanceDueOn: $stay->arrival->subDays($property->balance_due_days_before_arrival),
promoCodeApplied: $stay->promoCode === null ? null : collect($appliedOffers)->contains(fn (array $applied): bool => $applied['offer']->code !== null),
);
}
/**
* @return non-empty-list<Season>
*/
private function seasonPerNight(Property $property, StayRequest $stay): array
{
/** @var Collection<int, Season> $seasons */
$seasons = $property->seasons()->overlapping($stay->arrival, $stay->departure)->get();
$nightSeasons = [];
for ($night = $stay->arrival; $night->lessThan($stay->departure); $night = $night->addDay()) {
$nightSeasons[] = $seasons->first(fn (Season $season): bool => $season->containsNight($night))
?? throw QuoteException::noRateForNight($night);
}
return $nightSeasons;
}
/**
* One line per consecutive run of nights in the same season.
*
* @param non-empty-list<Season> $nightSeasons
* @return list<QuoteLine>
*/
private function accommodationLines(Property $property, CarbonImmutable $arrival, CarbonImmutable $departure): array
private function accommodationLines(array $nightSeasons): array
{
/** @var Collection<int, Season> $seasons */
$seasons = $property->seasons()->overlapping($arrival, $departure)->get();
/** @var list<array{season: Season, nights: int}> $runs */
$runs = [];
for ($night = $arrival; $night->lessThan($departure); $night = $night->addDay()) {
$season = $seasons->first(fn (Season $season): bool => $season->containsNight($night))
?? throw QuoteException::noRateForNight($night);
foreach ($nightSeasons as $season) {
$lastRun = array_key_last($runs);
if ($lastRun !== null && $runs[$lastRun]['season']->is($season)) {
$runs[$lastRun]['nights']++;
@@ -91,12 +108,6 @@ private function accommodationLines(Property $property, CarbonImmutable $arrival
}
}
$arrivalSeason = $runs[0]['season'];
$nights = (int) $arrival->diffInDays($departure);
if ($nights < $arrivalSeason->min_nights) {
throw QuoteException::minimumStay($arrivalSeason->min_nights, $arrivalSeason->name);
}
return array_map(fn (array $run): QuoteLine => new QuoteLine(
QuoteLineType::Accommodation,
"{$run['season']->name} ({$run['nights']} ".($run['nights'] === 1 ? 'Nacht' : 'Nächte').')',
@@ -106,6 +117,46 @@ private function accommodationLines(Property $property, CarbonImmutable $arrival
), $runs);
}
/**
* The guest gets whichever is cheaper for them: the single best non-combinable offer,
* or all combinable offers together (capped at the accommodation price).
*
* @param non-empty-list<Season> $nightSeasons
* @return list<array{offer: SpecialOffer, discount: int}>
*/
private function bestOffers(Property $property, StayRequest $stay, array $nightSeasons, CarbonImmutable $bookedAt): array
{
$nightlyRates = array_map(fn (Season $season): int => $season->nightly_rate, $nightSeasons);
$accommodation = array_sum($nightlyRates);
$candidates = $property->specialOffers()->active()->get()
->filter(fn (SpecialOffer $offer): bool => $offer->appliesTo($stay, $nightSeasons[0]->name, $bookedAt))
->map(fn (SpecialOffer $offer): array => ['offer' => $offer, 'discount' => $offer->discountFor($nightlyRates)])
->filter(fn (array $candidate): bool => $candidate['discount'] > 0);
[$combinable, $exclusive] = $candidates->partition(fn (array $candidate): bool => $candidate['offer']->is_combinable);
$bestExclusive = $exclusive->sortByDesc('discount')->first();
$combined = [];
$remaining = $accommodation;
foreach ($combinable->sortByDesc('discount') as $candidate) {
$discount = min($candidate['discount'], $remaining);
if ($discount > 0) {
$combined[] = ['offer' => $candidate['offer'], 'discount' => $discount];
$remaining -= $discount;
}
}
$combinedTotal = $accommodation - $remaining;
if ($bestExclusive !== null && $bestExclusive['discount'] >= $combinedTotal) {
return [$bestExclusive];
}
return $combined;
}
/**
* @param array<int, int> $extraQuantities
* @return list<QuoteLine>