103 lines
6.9 KiB
PHP
103 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Availability;
|
|
|
|
use App\Enums\BookingStatus;
|
|
use App\Filament\Resources\BlockedPeriods\BlockedPeriodResource;
|
|
use App\Filament\Resources\Bookings\BookingResource;
|
|
use App\Filament\Resources\Guests\GuestResource;
|
|
use App\Filament\Resources\PortalOccupations\PortalOccupationResource;
|
|
use App\Models\BlockedPeriod;
|
|
use App\Models\Booking;
|
|
use App\Models\Property;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class OccupancyOverview
|
|
{
|
|
/**
|
|
* One read model over local booking workflows and their external calendar counterparts.
|
|
* Source records retain their identities; linked representations never become a second stay.
|
|
*
|
|
* @return Collection<int|string, array<string, mixed>>
|
|
*/
|
|
public function records(?int $propertyId = null, ?int $guestId = null, bool $includeBlocks = false, ?CarbonImmutable $from = null, ?CarbonImmutable $until = null): Collection
|
|
{
|
|
Gate::authorize('viewAny', Booking::class);
|
|
$bookings = Booking::query()->with(['guest', 'property', 'originChannel', 'importChannel'])->withCount('channelRepresentations')
|
|
->when($propertyId, fn ($query) => $query->where('property_id', $propertyId))
|
|
->when($guestId, fn ($query) => $query->where('guest_id', $guestId))
|
|
->when($from, fn ($query) => $query->whereDate('departure', '>=', $from))
|
|
->when($until, fn ($query) => $query->whereDate('arrival', '<', $until))
|
|
->get();
|
|
$records = collect();
|
|
foreach ($bookings as $booking) {
|
|
$records->put($booking->id, [
|
|
'record_type' => 'booking', 'record_id' => $booking->id, 'reference' => $booking->reference,
|
|
'kind' => 'booking', 'kind_label' => 'Anfrage / Buchung',
|
|
'property_id' => $booking->property_id, 'property' => $booking->property->name,
|
|
'arrival' => $booking->arrival->toDateString(), 'departure' => $booking->departure->toDateString(),
|
|
'nights' => $booking->nights(), 'persons' => $booking->guests, 'total' => $booking->total,
|
|
'guest_id' => $booking->guest_id, 'guest' => $booking->guest?->full_name ?? 'Gastzuordnung offen',
|
|
'guest_email' => $booking->guest?->email, 'guest_assigned' => $booking->guest_id !== null,
|
|
'source' => $booking->bookingChannelLabel(), 'status' => $booking->status->getLabel(),
|
|
'status_key' => $booking->status->value, 'color' => $booking->status->getColor(),
|
|
'occupies' => in_array($booking->status, BookingStatus::occupying(), true) || $booking->isHoldActive(),
|
|
'needs_review' => false, 'review_note' => null, 'archived' => false,
|
|
'representations' => $booking->channel_representations_count,
|
|
'url' => BookingResource::getUrl('view', ['record' => $booking->id]),
|
|
'guest_url' => $booking->guest_id ? GuestResource::getUrl('edit', ['record' => $booking->guest_id]) : null,
|
|
]);
|
|
}
|
|
$blocks = BlockedPeriod::query()->select(['id', 'tenant_id', 'property_id', 'guest_id', 'channel_id', 'origin_channel_id', 'starts_on', 'ends_on', 'reason', 'source', 'occupation_kind', 'ical_event_type', 'ical_interpretation', 'is_representation', 'needs_review', 'review_note', 'deleted_at'])->withTrashed()->occupying()->with(['guest', 'property', 'channel', 'originChannel'])->withCount('channelRepresentations')
|
|
->when(! $includeBlocks, fn ($query) => $query->reservations())
|
|
->when($propertyId, fn ($query) => $query->where('property_id', $propertyId))
|
|
->when($guestId, fn ($query) => $query->where('guest_id', $guestId)->reservations())
|
|
->when($from, fn ($query) => $query->whereDate('ends_on', '>=', $from))
|
|
->when($until, fn ($query) => $query->whereDate('starts_on', '<', $until))
|
|
->get();
|
|
foreach ($blocks as $block) {
|
|
$kind = $block->occupationKind();
|
|
$records->put('portal-'.$block->id, [
|
|
'record_type' => 'portal', 'record_id' => $block->id,
|
|
'reference' => $block->ical_interpretation['fields']['booking_reference'] ?? 'Kalender-'.$block->id,
|
|
'kind' => $kind->value, 'kind_label' => $kind->getLabel(),
|
|
'property_id' => $block->property_id, 'property' => $block->property->name,
|
|
'arrival' => $block->starts_on->toDateString(), 'departure' => $block->ends_on->toDateString(),
|
|
'nights' => (int) $block->starts_on->diffInDays($block->ends_on), 'persons' => null, 'total' => null,
|
|
'guest_id' => $block->guest_id, 'guest' => $block->guestName() ?? 'Gastzuordnung offen',
|
|
'guest_email' => $block->guest?->email, 'guest_assigned' => $block->guest_id !== null,
|
|
'source' => $block->originChannel?->name ?? $block->channel?->name ?? ($block->source === 'manual' ? 'Eigene Sperre' : 'Ursprung unbekannt'),
|
|
'status' => $block->trashed() ? 'Nicht mehr im Kalenderfeed' : ($block->needs_review ? 'Klärung erforderlich' : 'Im Kalender geführt'),
|
|
'status_key' => $block->trashed() ? 'archived' : 'imported', 'color' => $block->needs_review ? 'warning' : $kind->getColor(),
|
|
'occupies' => ! $block->trashed(), 'needs_review' => $block->needs_review, 'review_note' => $block->review_note,
|
|
'archived' => $block->trashed(), 'representations' => $block->channel_representations_count,
|
|
'url' => $block->source === 'manual' ? BlockedPeriodResource::getUrl() : PortalOccupationResource::getUrl('view', ['record' => $block->id]),
|
|
'guest_url' => $block->guest_id ? GuestResource::getUrl('edit', ['record' => $block->guest_id]) : null,
|
|
]);
|
|
}
|
|
|
|
return $records;
|
|
}
|
|
|
|
/** @return array{month: CarbonImmutable, days: array, rows: Collection, free: int, occupied: int} */
|
|
public function calendar(Property $property, CarbonImmutable $month): array
|
|
{
|
|
$month = $month->startOfMonth();
|
|
$until = $month->addMonth();
|
|
$records = $this->records($property->id, includeBlocks: true, from: $month, until: $until);
|
|
$occupying = $records->where('occupies', true);
|
|
$days = [];
|
|
for ($date = $month; $date->lessThan($until); $date = $date->addDay()) {
|
|
$day = $date->toDateString();
|
|
$nights = $occupying->filter(fn (array $record): bool => $record['arrival'] <= $day && $day < $record['departure']);
|
|
$departures = $occupying->where('departure', $day);
|
|
$days[] = ['date' => $date, 'free' => $nights->isEmpty(), 'occupations' => $nights->values()->all(), 'departures' => $departures->values()->all(), 'overlap' => $nights->count() > 1];
|
|
}
|
|
$free = collect($days)->where('free', true)->count();
|
|
|
|
return ['month' => $month, 'days' => $days, 'rows' => $records->sortBy('arrival'), 'free' => $free, 'occupied' => count($days) - $free];
|
|
}
|
|
}
|