Files
fewo-stommer/app/Observers/BlockedPeriodObserver.php
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

68 lines
2.6 KiB
PHP
Raw Permalink 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\Observers;
use App\Actions\CreateChannelTasks;
use App\Enums\ChannelTaskAction;
use App\Models\BlockedPeriod;
use App\Models\Property;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
/**
* Manual blocks and occupations imported from one portal must also reach the manually maintained
* portals – except the portal they came from.
*/
class BlockedPeriodObserver
{
public function __construct(private CreateChannelTasks $createChannelTasks) {}
public function created(BlockedPeriod $block): void
{
if (! $block->is_representation && $block->export_approved) {
$this->tasks($block, $block->starts_on, $block->ends_on, ChannelTaskAction::Block);
}
}
public function restored(BlockedPeriod $block): void
{
$this->created($block);
}
public function updated(BlockedPeriod $block): void
{
if (! $block->wasChanged(['starts_on', 'ends_on', 'is_representation', 'export_approved'])) {
return;
}
$this->tasks($block, CarbonImmutable::parse($block->getRawOriginal('starts_on')), CarbonImmutable::parse($block->getRawOriginal('ends_on')), ChannelTaskAction::Unblock);
if (! $block->is_representation && $block->export_approved && ! $block->trashed()) {
$this->tasks($block, $block->starts_on, $block->ends_on, ChannelTaskAction::Block);
}
}
public function deleted(BlockedPeriod $block): void
{
if (! $block->is_representation) {
$this->tasks($block, $block->starts_on, $block->ends_on, ChannelTaskAction::Unblock);
}
}
private function tasks(BlockedPeriod $block, CarbonImmutable $startsOn, CarbonImmutable $endsOn, ChannelTaskAction $action): void
{
$reason = $block->channel ? "Belegung über {$block->channel->name}" : 'Sperrzeit'.($block->reason ? ": {$block->reason}" : '');
$propertyId = $block->property_id;
$channelId = $block->channel_id;
$originId = $block->origin_channel_id;
$external = $channelId !== null || $originId !== null || $block->source !== 'manual';
DB::afterCommit(function () use ($propertyId, $channelId, $originId, $external, $startsOn, $endsOn, $action, $reason): void {
$property = Property::withoutGlobalScope('tenant')->find($propertyId);
if ($property !== null) {
app(TenantContext::class)->run($property->tenant, fn () => $this->createChannelTasks->handle($property, $startsOn, $endsOn, $action, $reason, $channelId, $originId, $external));
}
});
}
}