- Webseite > Videos (Datei, Standbild, Kapitel mit Raum im Grundriss); Upload bis 100 MB - Titelbereich: Video statt Foto (stumme Schleife, respektiert Bewegung reduzieren/Save-Data) und naechste freie Termine mit Gesamtpreis (NextFreeStays) - Neuer Baustein Rundgang (Video) mit waehlbarer Anordnung; Grundriss springt ins Video - Buchungsseite: Preis unter dem Kalender, Formular rechts, Ablauf als Grafik - Seebad: Fahrkarten, Fahrplan-Kapitel, Sonnenaufgang als einmaliges Ereignis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
3.7 KiB
PHP
104 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Availability;
|
|
|
|
use App\Models\Property;
|
|
use App\Pricing\PriceCalculator;
|
|
use App\Pricing\PriceQuote;
|
|
use App\Pricing\QuoteException;
|
|
use App\Pricing\StayRequest;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
/**
|
|
* The next bookable stays from tomorrow on, for the "next free dates" in the hero section: free nights
|
|
* that the PriceCalculator accepts (minimum stay, offers applied), at least $nights long, not overlapping.
|
|
* Cached briefly per apartment because each candidate is a full price calculation (dates as strings: the cache
|
|
* does not unserialize objects).
|
|
*/
|
|
class NextFreeStays
|
|
{
|
|
public const HORIZON_DAYS = 120;
|
|
|
|
public const MAX_NIGHTS = 7;
|
|
|
|
public function __construct(
|
|
private AvailabilityChecker $availability,
|
|
private PriceCalculator $calculator,
|
|
) {}
|
|
|
|
/**
|
|
* @return list<array{arrival: CarbonImmutable, departure: CarbonImmutable, nights: int, total: int, url: string}>
|
|
*/
|
|
public function for(Property $property, int $count = 3, int $nights = 3, int $guests = 2): array
|
|
{
|
|
$key = "next-free-stays:{$property->id}:{$count}:{$nights}:{$guests}:".today()->toDateString();
|
|
|
|
$stays = Cache::remember($key, now()->addMinutes(10), fn (): array => $this->find($property, $count, $nights, min($guests, $property->max_guests)));
|
|
|
|
return array_map(fn (array $stay): array => [
|
|
...$stay,
|
|
'arrival' => CarbonImmutable::parse($stay['arrival']),
|
|
'departure' => CarbonImmutable::parse($stay['departure']),
|
|
], $stays);
|
|
}
|
|
|
|
/**
|
|
* @return list<array{arrival: string, departure: string, nights: int, total: int, url: string}>
|
|
*/
|
|
private function find(Property $property, int $count, int $nights, int $guests): array
|
|
{
|
|
$from = CarbonImmutable::tomorrow();
|
|
$until = $from->addDays(self::HORIZON_DAYS + self::MAX_NIGHTS);
|
|
$occupied = $this->availability->occupiedNights($property, $from, $until);
|
|
$stays = [];
|
|
|
|
for ($arrival = $from; count($stays) < $count && $arrival->lessThan($from->addDays(self::HORIZON_DAYS)); $arrival = $arrival->addDay()) {
|
|
if (isset($occupied[$arrival->toDateString()])) {
|
|
continue;
|
|
}
|
|
|
|
$quote = $this->firstPricedStay($property, $arrival, $nights, $guests, $occupied);
|
|
if ($quote === null) {
|
|
continue;
|
|
}
|
|
|
|
$stays[] = [
|
|
'arrival' => $quote->arrival->toDateString(),
|
|
'departure' => $quote->departure->toDateString(),
|
|
'nights' => $quote->nights(),
|
|
'total' => $quote->total(),
|
|
'url' => route('booking.create', ['anreise' => $quote->arrival->toDateString(), 'abreise' => $quote->departure->toDateString(), 'personen' => $guests]),
|
|
];
|
|
$arrival = $quote->departure->subDay();
|
|
}
|
|
|
|
return $stays;
|
|
}
|
|
|
|
/**
|
|
* The shortest stay of at least $nights from $arrival that is free and priceable, if any.
|
|
*
|
|
* @param array<string, true> $occupied
|
|
*/
|
|
private function firstPricedStay(Property $property, CarbonImmutable $arrival, int $nights, int $guests, array $occupied): ?PriceQuote
|
|
{
|
|
for ($length = 1; $length <= self::MAX_NIGHTS; $length++) {
|
|
if (isset($occupied[$arrival->addDays($length - 1)->toDateString()])) {
|
|
return null;
|
|
}
|
|
if ($length < $nights) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
return $this->calculator->calculate($property, new StayRequest($arrival, $arrival->addDays($length), $guests));
|
|
} catch (QuoteException) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|