> */ public function records(?int $propertyId = null, ?int $guestId = null, bool $includeBlocks = false, ?CarbonImmutable $from = null, ?CarbonImmutable $until = null): Collection { Gate::authorize('viewAny', Booking::class); $representations = ['channelRepresentations' => fn ($query) => $query->select(['id', 'channel_id', 'canonical_block_id', 'linked_booking_id'])->with('channel:id,name')]; $bookings = Booking::query()->with(['guest', 'property', 'originChannel', 'importChannel'])->with($representations)->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, 'reason' => null, 'archived' => false, 'archived_booking' => $booking->archived_at !== null, 'channel_id' => $booking->origin_channel_id ?? $booking->import_channel_id, 'representations' => $booking->channel_representations_count, 'mirrored_channels' => $booking->channelRepresentations->pluck('channel.name')->filter()->unique()->values()->all(), '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', 'block_purpose', 'block_note', 'is_private', 'ical_event_type', 'ical_interpretation', 'is_representation', 'needs_review', 'review_note', 'deleted_at'])->withTrashed()->occupying()->with(['guest', 'property', 'channel', 'originChannel'])->with($representations)->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->bookingChannelLabel(), '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, 'reason' => $block->blockDescription(), 'archived' => $block->trashed(), 'archived_booking' => false, 'channel_id' => $block->origin_channel_id ?? $block->channel_id, 'representations' => $block->channel_representations_count, 'mirrored_channels' => $block->channelRepresentations->pluck('channel.name')->filter()->unique()->values()->all(), '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]; } }