114 lines
6.4 KiB
PHP
114 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\ChannelEchoBehavior;
|
|
use App\Enums\OccupationKind;
|
|
use App\Models\BlockedPeriod;
|
|
use App\Models\Property;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ReconcileChannelRepresentations
|
|
{
|
|
public const OVERLAP_NOTE = 'Mehrere Belegungsrepräsentationen oder Überschneidungen prüfen; keine automatische Zusammenführung anhand von Terminen.';
|
|
|
|
private const LINK_NOTE = 'Automatisch als Kalenderspiegelung einer eindeutig passenden führenden Portalreservierung zugeordnet.';
|
|
|
|
private const RECHECK_NOTE = 'Automatische Kalenderspiegelung nicht mehr eindeutig. Quellen, Termine und mögliche Stornierung prüfen; der weiterhin gemeldete Zeitraum bleibt belegt.';
|
|
|
|
/** @return array{linked: int, unlinked: int} */
|
|
public function handle(Property $property): array
|
|
{
|
|
return DB::transaction(function () use ($property): array {
|
|
$property = Property::query()->whereKey($property->id)->lockForUpdate()->firstOrFail();
|
|
$blocks = $property->blockedPeriods()->with('channel')->withCount('channelRepresentations')->orderBy('id')->lockForUpdate()->get()->keyBy('id');
|
|
$independent = $blocks->filter(fn (BlockedPeriod $block): bool => ! $block->is_representation || $block->representation_automatic);
|
|
$mirrors = $independent->filter(fn (BlockedPeriod $block): bool => $this->isMirror($block));
|
|
$leaders = $independent->filter(fn (BlockedPeriod $block): bool => $this->isLeader($block));
|
|
$bookings = $property->bookings()->occupying()->get(['id', 'arrival', 'departure']);
|
|
$targets = [];
|
|
foreach ($mirrors as $mirror) {
|
|
$matches = $leaders->filter(fn (BlockedPeriod $leader): bool => $leader->channel_id !== $mirror->channel_id && $this->sameDates($leader, $mirror));
|
|
if ($matches->count() !== 1) {
|
|
continue;
|
|
}
|
|
$leader = $matches->sole();
|
|
$contradiction = $independent->contains(fn (BlockedPeriod $other): bool => $other->id !== $mirror->id && $other->id !== $leader->id
|
|
&& $other->starts_on->lessThan($leader->ends_on) && $other->ends_on->greaterThan($leader->starts_on)
|
|
&& ! ($mirrors->has($other->id) && $this->sameDates($other, $leader)));
|
|
if ($contradiction || $bookings->contains(fn ($booking): bool => $booking->arrival->lessThan($leader->ends_on) && $booking->departure->greaterThan($leader->starts_on))) {
|
|
continue;
|
|
}
|
|
$targets[$mirror->id] = $leader;
|
|
}
|
|
|
|
$counts = ['linked' => 0, 'unlinked' => 0];
|
|
foreach ($blocks as $block) {
|
|
$leader = $targets[$block->id] ?? null;
|
|
if ($leader === null) {
|
|
if ($block->representation_automatic) {
|
|
$block->forceFill(['representation_automatic' => false, 'is_representation' => false, 'canonical_block_id' => null, 'export_approved' => false,
|
|
'needs_review' => true, 'review_note' => self::RECHECK_NOTE])->save();
|
|
$counts['unlinked']++;
|
|
}
|
|
|
|
continue;
|
|
}
|
|
$newLink = ! $block->is_representation || $block->canonical_block_id !== $leader->id;
|
|
$block->forceFill(['representation_automatic' => true, 'is_representation' => true, 'canonical_block_id' => $leader->id,
|
|
'needs_review' => false, 'review_note' => self::LINK_NOTE]);
|
|
if ($block->isDirty()) {
|
|
$block->save();
|
|
}
|
|
$counts['linked'] += (int) $newLink;
|
|
if ($leader->reviewed_at === null) {
|
|
if ($leader->channel?->echo_behavior !== ChannelEchoBehavior::Reexports) {
|
|
$leader->forceFill(['export_approved' => true]);
|
|
}
|
|
if ($leader->review_note === self::OVERLAP_NOTE) {
|
|
$leader->forceFill(['needs_review' => false, 'review_note' => null]);
|
|
}
|
|
if ($leader->isDirty()) {
|
|
$leader->save();
|
|
}
|
|
}
|
|
}
|
|
|
|
return $counts;
|
|
});
|
|
}
|
|
|
|
private function isMirror(BlockedPeriod $block): bool
|
|
{
|
|
$interpretation = $block->ical_interpretation ?? [];
|
|
|
|
return $block->source === 'channel' && $block->channel_id !== null && $block->missing_imports === 0
|
|
&& $block->occupation_kind === null && $block->guest_id === null && ! $block->is_private
|
|
&& $block->block_purpose === null && blank($block->block_note) && $block->reviewed_at === null
|
|
&& $block->origin_channel_id === null && $block->linked_booking_id === null && $block->channel_representations_count === 0
|
|
&& $block->ical_event_type === 'block' && ($interpretation['matched'] ?? false)
|
|
&& ($interpretation['representation'] ?? null) === 'additional_block' && ($interpretation['origin_channel_id'] ?? null) === null
|
|
&& blank($interpretation['fields']['booking_reference'] ?? null) && blank($interpretation['fields']['guest_full_name'] ?? null)
|
|
&& blank($interpretation['fields']['guest_first_name'] ?? null) && $this->hasCompatibleReview($block);
|
|
}
|
|
|
|
private function isLeader(BlockedPeriod $block): bool
|
|
{
|
|
return $block->source === 'channel' && $block->channel_id !== null && ! $block->is_representation
|
|
&& $block->occupationKind() === OccupationKind::Reservation && $block->ical_event_type === 'portal_reservation'
|
|
&& $block->missing_imports === 0 && ! $block->is_private && $block->leading_priority > 0
|
|
&& $block->origin_channel_id === $block->channel_id && ($block->ical_interpretation['matched'] ?? false)
|
|
&& ($block->ical_interpretation['representation'] ?? null) === 'leading_candidate' && $this->hasCompatibleReview($block);
|
|
}
|
|
|
|
private function hasCompatibleReview(BlockedPeriod $block): bool
|
|
{
|
|
return ! $block->needs_review || in_array($block->review_note, [self::OVERLAP_NOTE, self::LINK_NOTE, self::RECHECK_NOTE], true);
|
|
}
|
|
|
|
private function sameDates(BlockedPeriod $first, BlockedPeriod $second): bool
|
|
{
|
|
return $first->starts_on->equalTo($second->starts_on) && $first->ends_on->equalTo($second->ends_on);
|
|
}
|
|
}
|