Phase 1: Wohnungen, Saisons & Preisberechnung
- 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>
This commit is contained in:
1 parent
bf4c1c0838
commit
2e9a391b37
35 files changed
+1766
-12
No files matched your search
@@ -0,0 +1,133 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Pricing;
|
||||
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The complete price of a stay. Amounts are in euro cents.
|
||||
*/
|
||||
final readonly class PriceQuote
|
||||
{
|
||||
/**
|
||||
* @param list<QuoteLine> $lines
|
||||
*/
|
||||
public function __construct(
|
||||
public CarbonImmutable $arrival,
|
||||
public CarbonImmutable $departure,
|
||||
public int $guests,
|
||||
public array $lines,
|
||||
public int $depositPercent,
|
||||
public CarbonImmutable $balanceDueOn,
|
||||
) {}
|
||||
|
||||
public function nights(): int
|
||||
{
|
||||
return (int) $this->arrival->diffInDays($this->departure);
|
||||
}
|
||||
|
||||
public function total(): int
|
||||
{
|
||||
return array_sum(array_map(fn (QuoteLine $line): int => $line->amount, $this->lines));
|
||||
}
|
||||
|
||||
public function rent(): int
|
||||
{
|
||||
return array_sum(array_map(
|
||||
fn (QuoteLine $line): int => $line->type->isRent() ? $line->amount : 0,
|
||||
$this->lines,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deposit as a share of the rent (nights and extra guests, without cleaning and extras), rounded to full cents.
|
||||
*/
|
||||
public function deposit(): int
|
||||
{
|
||||
return intdiv($this->rent() * $this->depositPercent + 50, 100);
|
||||
}
|
||||
|
||||
public function balance(): int
|
||||
{
|
||||
return $this->total() - $this->deposit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{arrival: string, departure: string, nights: int, guests: int, lines: list<array<string, mixed>>, total: int, deposit: int, balance: int, balance_due_on: string}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'arrival' => $this->arrival->toDateString(),
|
||||
'departure' => $this->departure->toDateString(),
|
||||
'nights' => $this->nights(),
|
||||
'guests' => $this->guests,
|
||||
'lines' => array_map(fn (QuoteLine $line): array => $line->toArray(), $this->lines),
|
||||
'total' => $this->total(),
|
||||
'deposit' => $this->deposit(),
|
||||
'balance' => $this->balance(),
|
||||
'balance_due_on' => $this->balanceDueOn->toDateString(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Pricing;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* A stay that cannot be priced. Messages are shown to guests and administrators.
|
||||
*/
|
||||
class QuoteException extends RuntimeException
|
||||
{
|
||||
public static function invalidPeriod(): self
|
||||
{
|
||||
return new self('Die Abreise muss nach der Anreise liegen.');
|
||||
}
|
||||
|
||||
public static function invalidGuestCount(int $maxGuests): self
|
||||
{
|
||||
return new self("Die Wohnung bietet Platz für 1 bis {$maxGuests} Personen.");
|
||||
}
|
||||
|
||||
public static function noRateForNight(CarbonInterface $night): self
|
||||
{
|
||||
return new self("Für den {$night->format('d.m.Y')} sind noch keine Preise hinterlegt.");
|
||||
}
|
||||
|
||||
public static function minimumStay(int $minNights, string $seasonName): self
|
||||
{
|
||||
return new self("In der Saison „{$seasonName}“ beträgt der Mindestaufenthalt {$minNights} Nächte.");
|
||||
}
|
||||
|
||||
public static function unknownExtra(int $extraId): self
|
||||
{
|
||||
return new self("Die Zusatzleistung #{$extraId} ist nicht buchbar.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Pricing;
|
||||
|
||||
use App\Enums\QuoteLineType;
|
||||
|
||||
/**
|
||||
* One position of a price quote. Amounts are in euro cents.
|
||||
*/
|
||||
final readonly class QuoteLine
|
||||
{
|
||||
public function __construct(
|
||||
public QuoteLineType $type,
|
||||
public string $label,
|
||||
public int $quantity,
|
||||
public int $unitPrice,
|
||||
public int $amount,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{type: string, label: string, quantity: int, unit_price: int, amount: int}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->type->value,
|
||||
'label' => $this->label,
|
||||
'quantity' => $this->quantity,
|
||||
'unit_price' => $this->unitPrice,
|
||||
'amount' => $this->amount,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user