Files
fewo-stommer/app/Availability/AvailabilityChecker.php
Lars 783068501c
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m33s
Generische iCal-Kanäle mit Bestandsklärung und sicheren Exporten bereitstellen
2026-10-27 17:14:45 +01: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()->occupying()->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()->occupying()->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;
}
}