Files
fewo-stommer/app/Actions/ResolveChannelOccupation.php
T
Lars 4389894959
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m49s
Sperrzeiten um Eigenbelegung und privaten Buchungskanal erweitern
2026-10-27 22:26:47 +01:00

51 lines
3.3 KiB
PHP

<?php
namespace App\Actions;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Channel;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class ResolveChannelOccupation
{
public function handle(BlockedPeriod $block, string $resolution, ?int $targetId = null, ?int $originId = null): BlockedPeriod
{
return DB::transaction(function () use ($block, $resolution, $targetId, $originId): BlockedPeriod {
$block = BlockedPeriod::query()->whereKey($block->id)->lockForUpdate()->firstOrFail();
if (! in_array($block->source, ['channel', 'email'], true)) {
throw ValidationException::withMessages(['resolution' => 'Nur importierte Portalereignisse benötigen diese Zuordnung.']);
}
if (! in_array($resolution, ['independent', 'block', 'booking'], true)) {
throw ValidationException::withMessages(['resolution' => 'Bitte eine gültige Klärung wählen.']);
}
if ($originId !== null && ! Channel::query()->whereKey($originId)->where('property_id', $block->property_id)->exists()) {
throw ValidationException::withMessages(['origin_channel_id' => 'Der Ursprungskanal muss zur selben Wohnung gehören.']);
}
$attributes = ['is_representation' => false, 'canonical_block_id' => null, 'linked_booking_id' => null,
'origin_channel_id' => $originId, 'is_private' => $originId === null && $block->is_private, 'export_approved' => true, 'needs_review' => false,
'reviewed_at' => now(), 'review_note' => 'Als eigenständige Belegung persönlich bestätigt.'];
if ($resolution !== 'independent') {
$query = $resolution === 'booking' ? Booking::query()->occupying() : BlockedPeriod::query()->occupying()->whereKeyNot($block->id);
$target = $query->where('property_id', $block->property_id)->whereKey($targetId)->lockForUpdate()->first();
$start = $resolution === 'booking' ? $target?->arrival : $target?->starts_on;
$end = $resolution === 'booking' ? $target?->departure : $target?->ends_on;
if ($target === null || ! $block->starts_on->equalTo($start) || ! $block->ends_on->equalTo($end)) {
throw ValidationException::withMessages(['target_id' => 'Eine belegende Buchung oder führende Sperre derselben Wohnung mit genau diesem Zeitraum wählen.']);
}
if (BlockedPeriod::query()->where('canonical_block_id', $block->id)->exists()) {
throw ValidationException::withMessages(['target_id' => 'Diese Sperre führt bereits andere Kalenderdarstellungen. Die Zuordnung an der untergeordneten Darstellung ändern.']);
}
$attributes = [...$attributes, 'is_representation' => true, 'export_approved' => false,
'canonical_block_id' => $resolution === 'block' ? $target->id : null,
'linked_booking_id' => $resolution === 'booking' ? $target->id : null,
'review_note' => 'Persönlich zugeordnet; nur die führende Belegung steuert Verfügbarkeit und Export.'];
}
$block->update($attributes);
return $block->refresh();
});
}
}