occupies($booking->status)) { $this->tasks($booking, ChannelTaskAction::Block); } } public function updated(Booking $booking): void { if (! $booking->wasChanged(['status', 'arrival', 'departure'])) { return; } $before = $this->occupies($booking->getRawOriginal('status')); $after = $this->occupies($booking->status); if ($before && (! $after || $booking->wasChanged(['arrival', 'departure']))) { $this->tasks($booking, ChannelTaskAction::Unblock, CarbonImmutable::parse($booking->getRawOriginal('arrival')), CarbonImmutable::parse($booking->getRawOriginal('departure'))); } if ($after && (! $before || $booking->wasChanged(['arrival', 'departure']))) { $this->tasks($booking, ChannelTaskAction::Block); } } private function occupies(BookingStatus|string|null $status): bool { $status = is_string($status) ? BookingStatus::tryFrom($status) : $status; return in_array($status, BookingStatus::occupying(), true); } private function tasks(Booking $booking, ChannelTaskAction $action, ?CarbonImmutable $startsOn = null, ?CarbonImmutable $endsOn = null): void { $propertyId = $booking->property_id; $channelId = $booking->import_channel_id; $originId = $booking->origin_channel_id; $external = ! $booking->isDirect(); $startsOn ??= $booking->arrival; $endsOn ??= $booking->departure; $reason = "Buchung {$booking->reference}"; 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)); } }); } }