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 reportSyncFailure(int $propertyId, int $bookingId, string $reason, Throwable $exception): void { Log::error('Portalaufgaben für Buchung nicht angelegt', ['booking' => $bookingId, 'exception' => $exception::class]); rescue(function () use ($propertyId, $bookingId, $reason): void { $property = Property::withoutGlobalScope('tenant')->find($propertyId); if ($property === null) { return; } app(TenantContext::class)->run($property->tenant, fn () => app(Inbox::class)->post( InboxMessageKind::SyncFailed, 'booking-'.$bookingId.':'.now()->format('YmdH'), 'Portalabgleich für '.$reason.' fehlgeschlagen', 'Die Buchung ist gespeichert, aber die Sperr-/Freigabeaufgaben für die Portale konnten nicht angelegt werden. Bitte den Kalender der Portale prüfen und gegebenenfalls von Hand sperren.', propertyId: $propertyId, )); }, report: false); } 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}"; $bookingId = $booking->id; DB::afterCommit(function () use ($propertyId, $channelId, $originId, $external, $startsOn, $endsOn, $action, $reason, $bookingId): void { // The booking is already saved: a failing portal task must never turn into an error for the guest. try { $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)); } } catch (Throwable $exception) { $this->reportSyncFailure($propertyId, $bookingId, $reason, $exception); } }); } }