Files
fewo-stommer/app/Availability/AvailabilityChecker.php
T
Lars StommerandClaude Opus 5.5 08b6582fa1 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>
2026-10-22 09:43:12 +02:00

49 lines
1.8 KiB
PHP

<?php
namespace App\Availability;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Property;
use Carbon\CarbonImmutable;
/**
* Works on nights: a stay from $from to $until occupies the nights $from … $until - 1 day,
* so changeover days (departure and arrival on the same day) are possible.
*/
class AvailabilityChecker
{
public function isAvailable(Property $property, CarbonImmutable $from, CarbonImmutable $until, ?Booking $ignore = null): bool
{
$bookingConflict = $property->bookings()
->occupying()
->overlapping($from, $until)
->when($ignore, fn ($query) => $query->whereKeyNot($ignore->getKey()))
->exists();
return ! $bookingConflict && ! $property->blockedPeriods()->overlapping($from, $until)->exists();
}
/**
* Occupied nights within [$from, $until) as a set of 'Y-m-d' strings.
*
* @return array<string, true>
*/
public function occupiedNights(Property $property, CarbonImmutable $from, CarbonImmutable $until): array
{
$periods = $property->bookings()->occupying()->overlapping($from, $until)->get(['arrival', 'departure'])
->map(fn (Booking $booking): array => [$booking->arrival, $booking->departure])
->concat($property->blockedPeriods()->overlapping($from, $until)->get(['starts_on', 'ends_on'])
->map(fn (BlockedPeriod $period): array => [$period->starts_on, $period->ends_on]));
$nights = [];
foreach ($periods as [$start, $end]) {
for ($night = $start->max($from); $night->lessThan($end) && $night->lessThan($until); $night = $night->addDay()) {
$nights[$night->toDateString()] = true;
}
}
return $nights;
}
}