property; $freeUntil = $booking->arrival->subDays($property->free_cancellation_days); return match ($property->cancellation_mode) { CancellationMode::Instant => CancellationMode::Instant, CancellationMode::InstantWhenFree => ($cost = CancellationCost::calculate($booking)) !== null ? ($cost['free'] ? CancellationMode::Instant : CancellationMode::Request) : (today()->lessThanOrEqualTo($freeUntil) ? CancellationMode::Instant : CancellationMode::Request), default => CancellationMode::Request, }; } /** * @return CancellationMode|null what happened (Instant = cancelled, Request = request recorded); null when it had happened before * * @throws ReservationException */ public function handle(Booking $booking, ?string $message = null): ?CancellationMode { $outcome = DB::transaction(function () use ($booking, $message): ?CancellationMode { $booking->lockForTransition(); $alreadyAsked = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::CancellationRequest)->exists(); if ($booking->status === BookingStatus::Cancelled || $alreadyAsked) { return null; } if (! in_array($booking->status, BookingStatus::occupying(), true) || $booking->arrival->isPast()) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'storniert'); } $mode = $this->modeFor($booking); if ($mode === CancellationMode::Instant) { $booking->update(['status' => BookingStatus::Cancelled, 'cancelled_at' => now()]); app(RecordCancellation::class)->handle($booking, 'guest'); } BookingTask::query()->create([ 'booking_id' => $booking->id, 'kind' => BookingTaskKind::CancellationRequest, 'status' => BookingTaskStatus::Open, 'note' => trim(($mode === CancellationMode::Instant ? 'Vom Gast storniert – bitte Stornokosten und Rückzahlung prüfen. ' : '').($message ?? '')) ?: null, ]); return $mode; }); if ($outcome !== null) { $this->notify($booking, $outcome); } return $outcome; } private function notify(Booking $booking, CancellationMode $mode): void { $booking->loadMissing(['guest', 'property']); $cancelled = $mode === CancellationMode::Instant; $sends = [ fn () => Mail::to(Settings::notificationRecipients())->send(new CancellationRequested($booking, $cancelled)), fn () => $booking->guest?->email ? Mail::to($booking->guest->email, $booking->guest->full_name)->send(new BookingCancelled($booking, $cancelled)) : null, ]; foreach ($sends as $send) { try { $send(); } catch (Throwable $exception) { Log::warning('Stornomitteilung nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]); } } } }