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

+56
View File
@@ -0,0 +1,56 @@
<?php
namespace App\Models;
use Carbon\CarbonInterface;
use Database\Factories\BlockedPeriodFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Dates that are not bookable (own use, maintenance, bookings from portals).
* Like bookings, ends_on is the first free day again.
*/
#[Fillable(['property_id', 'starts_on', 'ends_on', 'reason', 'source', 'external_uid'])]
class BlockedPeriod extends Model
{
/** @use HasFactory<BlockedPeriodFactory> */
use HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'starts_on' => 'immutable_date',
'ends_on' => 'immutable_date',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @param Builder<BlockedPeriod> $query
* @return Builder<BlockedPeriod>
*/
#[Scope]
protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder
{
return $query
->whereDate('starts_on', '<', $until)
->whereDate('ends_on', '>', $from);
}
}
+124
View File
@@ -0,0 +1,124 @@
<?php
namespace App\Models;
use App\Enums\BookingStatus;
use Carbon\CarbonInterface;
use Database\Factories\BookingFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* A reservation or booking. arrival is the first night, departure the day the guest leaves,
* so a departure and the next arrival on the same day do not collide.
*/
#[Fillable([
'property_id',
'guest_id',
'status',
'source',
'arrival',
'departure',
'guests',
'extras',
'promo_code',
'quote',
'total',
'deposit',
'balance_due_on',
'hold_expires_at',
'message',
'internal_notes',
'confirmed_at',
'cancelled_at',
])]
class Booking extends Model
{
/** @use HasFactory<BookingFactory> */
use HasFactory;
protected static function booted(): void
{
static::created(function (Booking $booking): void {
$booking->forceFill(['reference' => $booking->created_at->format('Y').'-'.str_pad((string) $booking->id, 4, '0', STR_PAD_LEFT)])->saveQuietly();
});
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => BookingStatus::class,
'arrival' => 'immutable_date',
'departure' => 'immutable_date',
'extras' => 'array',
'quote' => 'array',
'balance_due_on' => 'immutable_date',
'hold_expires_at' => 'immutable_datetime',
'confirmed_at' => 'immutable_datetime',
'cancelled_at' => 'immutable_datetime',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @return BelongsTo<Guest, $this>
*/
public function guest(): BelongsTo
{
return $this->belongsTo(Guest::class);
}
/**
* Bookings that currently occupy their dates: confirmed ones and reservations whose hold has not expired.
*
* @param Builder<Booking> $query
* @return Builder<Booking>
*/
#[Scope]
protected function occupying(Builder $query): Builder
{
return $query->where(fn (Builder $query) => $query
->whereIn('status', BookingStatus::occupying())
->orWhere(fn (Builder $query) => $query
->where('status', BookingStatus::Reserved)
->where('hold_expires_at', '>', now())));
}
/**
* @param Builder<Booking> $query
* @return Builder<Booking>
*/
#[Scope]
protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder
{
return $query
->whereDate('arrival', '<', $until)
->whereDate('departure', '>', $from);
}
public function nights(): int
{
return (int) $this->arrival->diffInDays($this->departure);
}
public function isHoldActive(): bool
{
return $this->status === BookingStatus::Reserved && $this->hold_expires_at?->isFuture();
}
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Models;
use Database\Factories\GuestFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
#[Fillable(['first_name', 'last_name', 'email', 'phone', 'street', 'postal_code', 'city', 'country', 'locale', 'notes'])]
class Guest extends Model
{
/** @use HasFactory<GuestFactory> */
use HasFactory, Notifiable;
/**
* @return HasMany<Booking, $this>
*/
public function bookings(): HasMany
{
return $this->hasMany(Booking::class)->latest('arrival');
}
/**
* @return Attribute<string, never>
*/
protected function fullName(): Attribute
{
return Attribute::get(fn (): string => "{$this->first_name} {$this->last_name}");
}
}
+25
View File
@@ -23,6 +23,7 @@
'cleaning_fee',
'deposit_percent',
'balance_due_days_before_arrival',
'hold_hours',
'check_in_time',
'check_out_time',
'is_active',
@@ -52,6 +53,30 @@ public function seasons(): HasMany
return $this->hasMany(Season::class)->orderBy('starts_on');
}
/**
* @return HasMany<Booking, $this>
*/
public function bookings(): HasMany
{
return $this->hasMany(Booking::class);
}
/**
* @return HasMany<BlockedPeriod, $this>
*/
public function blockedPeriods(): HasMany
{
return $this->hasMany(BlockedPeriod::class)->orderBy('starts_on');
}
/**
* @return HasMany<SpecialOffer, $this>
*/
public function specialOffers(): HasMany
{
return $this->hasMany(SpecialOffer::class);
}
/**
* Periods within [$from, $until) for which no season defines a price.
*
+136
View File
@@ -0,0 +1,136 @@
<?php
namespace App\Models;
use App\Enums\DiscountType;
use App\Pricing\StayRequest;
use Carbon\CarbonImmutable;
use Database\Factories\SpecialOfferFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* A discount rule such as last minute, early bird, long stay, special weekends or a code for regular guests.
* Every condition left empty means "no restriction".
*/
#[Fillable([
'property_id',
'name',
'public_description',
'is_active',
'is_public',
'is_combinable',
'code',
'discount_type',
'discount_value',
'stay_from',
'stay_until',
'bookable_from',
'bookable_until',
'min_days_before_arrival',
'max_days_before_arrival',
'min_nights',
'max_nights',
'arrival_weekdays',
'season_names',
])]
class SpecialOffer extends Model
{
/** @use HasFactory<SpecialOfferFactory> */
use HasFactory;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_active' => 'boolean',
'is_public' => 'boolean',
'is_combinable' => 'boolean',
'discount_type' => DiscountType::class,
'stay_from' => 'immutable_date',
'stay_until' => 'immutable_date',
'bookable_from' => 'immutable_datetime',
'bookable_until' => 'immutable_datetime',
'arrival_weekdays' => 'array',
'season_names' => 'array',
];
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @param Builder<SpecialOffer> $query
* @return Builder<SpecialOffer>
*/
#[Scope]
protected function active(Builder $query): Builder
{
return $query->where('is_active', true);
}
/**
* Whether all conditions are met. Offers with a code only apply when the guest entered that code.
*/
public function appliesTo(StayRequest $stay, string $arrivalSeasonName, CarbonImmutable $bookedAt): bool
{
$nights = $stay->nights();
$daysBeforeArrival = (int) $bookedAt->startOfDay()->diffInDays($stay->arrival, false);
return $this->is_active
&& ($this->code === null || strcasecmp($this->code, (string) $stay->promoCode) === 0)
&& ($this->stay_from === null || $stay->arrival->greaterThanOrEqualTo($this->stay_from))
&& ($this->stay_until === null || $stay->departure->lessThanOrEqualTo($this->stay_until))
&& ($this->bookable_from === null || $bookedAt->greaterThanOrEqualTo($this->bookable_from))
&& ($this->bookable_until === null || $bookedAt->lessThanOrEqualTo($this->bookable_until))
&& ($this->min_days_before_arrival === null || $daysBeforeArrival >= $this->min_days_before_arrival)
&& ($this->max_days_before_arrival === null || $daysBeforeArrival <= $this->max_days_before_arrival)
&& ($this->min_nights === null || $nights >= $this->min_nights)
&& ($this->max_nights === null || $nights <= $this->max_nights)
&& (empty($this->arrival_weekdays) || in_array($stay->arrival->isoWeekday(), array_map('intval', $this->arrival_weekdays), true))
&& (empty($this->season_names) || in_array($arrivalSeasonName, $this->season_names, true));
}
/**
* The discount in cents for the given nightly rates (one entry per night, in cents).
*
* @param list<int> $nightlyRates
*/
public function discountFor(array $nightlyRates): int
{
$accommodation = array_sum($nightlyRates);
$discount = match ($this->discount_type) {
DiscountType::Percent => intdiv($accommodation * min($this->discount_value, 100) + 50, 100),
DiscountType::AmountPerStay => $this->discount_value,
DiscountType::NightlyRate => array_sum(array_map(fn (int $rate): int => max(0, $rate - $this->discount_value), $nightlyRates)),
DiscountType::FreeNights => array_sum(array_slice($this->sortedAscending($nightlyRates), 0, $this->discount_value)),
};
return min($discount, $accommodation);
}
/**
* @param list<int> $rates
* @return list<int>
*/
private function sortedAscending(array $rates): array
{
sort($rates);
return $rates;
}
}