- Datenmodell Property/Season/Extra (Beträge in Cent), Seeder mit Preisliste 2026 - PriceCalculator: Saison-Splitting, Mindestaufenthalt, Zusatzperson, Endreinigung, Extras, Anzahlung/Restzahlung - Filament-Verwaltung: Wohnungen, Saisons (Überschneidungsprüfung, Lückenwarnung, Kopie ins Folgejahr, Preisrechner), Zusatzleistungen - Tests für Preisberechnung und Saisonverwaltung Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
4.8 KiB
PHP
134 lines
4.8 KiB
PHP
<?php
|
||
|
||
namespace App\Pricing;
|
||
|
||
use App\Enums\ExtraPricing;
|
||
use App\Enums\QuoteLineType;
|
||
use App\Models\Extra;
|
||
use App\Models\Property;
|
||
use App\Models\Season;
|
||
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
|
||
*
|
||
* @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)) {
|
||
throw QuoteException::invalidPeriod();
|
||
}
|
||
|
||
if ($guests < 1 || $guests > $property->max_guests) {
|
||
throw QuoteException::invalidGuestCount($property->max_guests);
|
||
}
|
||
|
||
$nights = (int) $arrival->diffInDays($departure);
|
||
$lines = $this->accommodationLines($property, $arrival, $departure);
|
||
|
||
$extraGuests = max(0, $guests - $property->included_guests);
|
||
if ($extraGuests > 0 && $property->extra_guest_fee_per_night > 0) {
|
||
$lines[] = new QuoteLine(
|
||
QuoteLineType::ExtraGuest,
|
||
"Zusätzliche Person ({$extraGuests} × {$nights} Nächte)",
|
||
$extraGuests * $nights,
|
||
$property->extra_guest_fee_per_night,
|
||
$extraGuests * $nights * $property->extra_guest_fee_per_night,
|
||
);
|
||
}
|
||
|
||
if ($property->cleaning_fee > 0) {
|
||
$lines[] = new QuoteLine(QuoteLineType::Cleaning, 'Endreinigung & Service', 1, $property->cleaning_fee, $property->cleaning_fee);
|
||
}
|
||
|
||
array_push($lines, ...$this->extraLines($property, $extraQuantities, $nights));
|
||
|
||
return new PriceQuote(
|
||
arrival: $arrival,
|
||
departure: $departure,
|
||
guests: $guests,
|
||
lines: $lines,
|
||
depositPercent: $property->deposit_percent,
|
||
balanceDueOn: $arrival->subDays($property->balance_due_days_before_arrival),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* One line per consecutive run of nights in the same season.
|
||
*
|
||
* @return list<QuoteLine>
|
||
*/
|
||
private function accommodationLines(Property $property, CarbonImmutable $arrival, CarbonImmutable $departure): 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);
|
||
|
||
$lastRun = array_key_last($runs);
|
||
if ($lastRun !== null && $runs[$lastRun]['season']->is($season)) {
|
||
$runs[$lastRun]['nights']++;
|
||
} else {
|
||
$runs[] = ['season' => $season, 'nights' => 1];
|
||
}
|
||
}
|
||
|
||
$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').')',
|
||
$run['nights'],
|
||
$run['season']->nightly_rate,
|
||
$run['nights'] * $run['season']->nightly_rate,
|
||
), $runs);
|
||
}
|
||
|
||
/**
|
||
* @param array<int, int> $extraQuantities
|
||
* @return list<QuoteLine>
|
||
*/
|
||
private function extraLines(Property $property, array $extraQuantities, int $nights): array
|
||
{
|
||
$extraQuantities = array_filter($extraQuantities, fn (int $quantity): bool => $quantity > 0);
|
||
if ($extraQuantities === []) {
|
||
return [];
|
||
}
|
||
|
||
$extras = $property->extras()->active()->whereKey(array_keys($extraQuantities))->get()->keyBy('id');
|
||
|
||
$lines = [];
|
||
foreach ($extraQuantities as $extraId => $quantity) {
|
||
/** @var Extra $extra */
|
||
$extra = $extras->get($extraId) ?? throw QuoteException::unknownExtra($extraId);
|
||
$units = $extra->pricing === ExtraPricing::PerNight ? $quantity * $nights : $quantity;
|
||
|
||
$lines[] = new QuoteLine(QuoteLineType::Extra, $extra->name, $units, $extra->price, $units * $extra->price);
|
||
}
|
||
|
||
return $lines;
|
||
}
|
||
}
|