status === BookingStatus::OfferSent) { $this->ensureCalendarFresh($booking); } try { $accepted = DB::transaction(function () use ($booking, $offerVersion): bool { $booking->lockForTransition(); if ($this->alreadyAccepted($booking, $offerVersion)) { return false; } $this->ensureAcceptable($booking, $offerVersion); $this->ensureUnchanged($booking); $validUntil = $booking->hold_expires_at?->toIso8601String(); $this->confirm->bind($booking); $booking->update([ 'accepted_at' => now(), 'acceptance' => [ 'offer_version' => $booking->offer_version, 'accepted_at' => now()->toIso8601String(), 'offer_sent_at' => $booking->offer_sent_at?->toIso8601String(), 'valid_until' => $validUntil, 'total' => $booking->total, 'offer_hash' => $booking->offer_hash, 'quote_sha256' => hash('sha256', json_encode($booking->quote)), 'terms_sha256' => hash('sha256', json_encode($booking->offer_terms['texts'] ?? [])), 'accept_button' => $booking->offerText('offer.accept_button'), 'via' => 'offer_link', ], ]); return true; }); } catch (ReservationException $exception) { $this->noteRejection($booking, $offerVersion, $exception); throw $exception; } if ($accepted) { $this->confirm->deliver($booking); $this->notifyLandlord($booking); } return $accepted; } /** * Checks that make the offer page show the form: version, status and validity (no locks, read only). * * @throws ReservationException */ public function ensureAcceptable(Booking $booking, int $offerVersion): void { if ($offerVersion !== $booking->offer_version) { throw ReservationException::offerOutdated(); } if ($booking->status !== BookingStatus::OfferSent) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'angenommen'); } if (! $booking->hold_expires_at?->isFuture()) { throw ReservationException::offerExpired(); } } /** * The frozen offer must still match its fingerprint (price, dates, terms); offers made before the * fingerprint existed have none and are not checked. * * @throws ReservationException */ public function ensureUnchanged(Booking $booking): void { if ($booking->offer_hash !== null && ! hash_equals($booking->offer_hash, OfferTerms::hash($booking))) { throw ReservationException::offerChanged(); } } /** * Before the guest becomes bound, the imported portal calendars must be recent (an import is tried first – * network access, so outside the transaction). Otherwise nothing is concluded and the landlord is told. * * @throws ReservationException */ private function ensureCalendarFresh(Booking $booking): void { $property = $booking->property; $this->clearance->refresh($property); $stale = $this->clearance->staleImports($property); if ($stale->isEmpty()) { return; } $this->inbox->post( InboxMessageKind::SyncFailed, 'accept-'.$booking->id.':'.now()->format('YmdH'), 'Annahme von '.$booking->reference.' zurückgestellt: Kalenderstand nicht aktuell', 'Der Gast wollte das Angebot annehmen, aber der Import von '.$stale->pluck('name')->implode(', ').' ist nicht aktuell. Bitte Kalenderabgleich prüfen und den Gast kontaktieren.', actionUrl: BookingResource::getUrl('view', ['record' => $booking]), propertyId: $booking->property_id, ); throw ReservationException::calendarUnverified(); } private function alreadyAccepted(Booking $booking, int $offerVersion): bool { return in_array($booking->status, BookingStatus::occupying(), true) && $booking->accepted_at !== null && ($booking->acceptance['offer_version'] ?? null) === $offerVersion; } private function noteRejection(Booking $booking, int $offerVersion, ReservationException $exception): void { if ($offerVersion !== $booking->offer_version || $booking->status !== BookingStatus::OfferSent) { return; } $reasonIsUnexpected = $exception->getMessage() === ReservationException::offerChanged()->getMessage() || $exception->getMessage() === ReservationException::notAvailable()->getMessage(); if (! $reasonIsUnexpected) { return; } $this->inbox->post( InboxMessageKind::OfferRejected, 'booking-'.$booking->id.':v'.$offerVersion, 'Annahme von Angebot '.$booking->reference.' abgewiesen', 'Der Gast wollte das Angebot annehmen, aber: '.$exception->getMessage().' Bitte den Gast kontaktieren oder ein neues Angebot senden.', actionUrl: BookingResource::getUrl('view', ['record' => $booking]), propertyId: $booking->property_id, ); } private function notifyLandlord(Booking $booking): void { try { Mail::to(Settings::notificationRecipients())->send(new OfferAnswered($booking, accepted: true)); } catch (Throwable $exception) { Log::warning('Benachrichtigung über Annahme nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]); } } }