Files
fewo-stommer/app/Channels/ConflictDetector.php
T
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

38 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Channels;
use App\Models\BlockedPeriod;
use App\Models\Booking;
/**
* Finds occupations from other sources that overlap a block reported by a portal
* (a possible double booking). Blocks of the same portal never conflict with each other.
*/
class ConflictDetector
{
/**
* @return list<string> human readable descriptions
*/
public function conflictsFor(BlockedPeriod $block): array
{
$property = $block->property;
$channelName = $block->channel->name ?? 'Sperre';
$period = $block->starts_on->format('d.m.Y').' – '.$block->ends_on->format('d.m.Y');
$bookings = $property->bookings()->occupying()->overlapping($block->starts_on, $block->ends_on)->with('guest')->get()
->map(fn (Booking $booking): string => "{$channelName} meldet {$period}, überschneidet sich mit Buchung {$booking->reference} ({$booking->guest->full_name}, {$booking->arrival->format('d.m.Y')} – {$booking->departure->format('d.m.Y')})");
$blocks = $property->blockedPeriods()
->occupying()
->overlapping($block->starts_on, $block->ends_on)
->whereKeyNot($block->getKey())
->when($block->channel_id, fn ($query) => $query->where(fn ($query) => $query->whereNull('channel_id')->orWhere('channel_id', '!=', $block->channel_id)))
->with('channel')
->get()
->map(fn (BlockedPeriod $other): string => "{$channelName} meldet {$period}, überschneidet sich mit Sperre „".($other->channel->name ?? $other->reason ?? 'manuell')."“ ({$other->starts_on->format('d.m.Y')} – {$other->ends_on->format('d.m.Y')})");
return [...$bookings->all(), ...$blocks->all()];
}
}