guest?->email)) { throw new ReservationException('Vor dem E-Mail-Versand bitte die E-Mail-Adresse des zugeordneten Gastes ergänzen.'); } DB::transaction(fn () => $this->bind($booking)); return $this->deliver($booking, $notifyGuest); } /** * Locks the property and the booking, checks status and availability and makes the booking binding. * Must run inside a transaction; every writer of bookings takes the same property lock, so two * overlapping stays can never both become binding. * * @throws ReservationException */ public function bind(Booking $booking): void { $booking->lockForTransition(); if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'bestätigt'); } if (! $this->availability->isAvailable($booking->property, $booking->arrival, $booking->departure, ignore: $booking)) { throw ReservationException::notAvailable(); } $terms = $booking->offer_terms ?? OfferTerms::capture(OfferTerms::templateFor($booking), $booking); $booking->update([ 'status' => BookingStatus::Confirmed, 'hold_expires_at' => null, 'confirmed_at' => now(), 'offer_terms' => $terms, 'deposit_due_on' => today()->addDays((int) ($terms['deposit_due_days'] ?? config('fewo.deposit_due_days')))->toDateString(), ]); } /** * Generates the contract and sends the confirmation – after the booking is saved, with every failure * contained: the booking stays binding, the failure is marked and shown in the admin inbox. * * @return bool whether the guest has the confirmation (true when no mail was requested) */ public function deliver(Booking $booking, bool $notifyGuest = true, ?DocumentKind $kind = null): bool { $booking->loadMissing(['guest', 'property']); $kind ??= DocumentKind::forCase($booking) ?? DocumentKind::Confirmation; try { $created = $booking->contract_path === null; $this->contract->generate($booking, kind: $kind); if ($created) { CaseLog::record($booking, 'document', $kind->getLabel().' erstellt'.(isset($booking->offer_terms['template']['name']) ? ' (Vorlage '.$booking->offer_terms['template']['name'].', Version '.$booking->offer_terms['template']['version'].')' : ' (Texte der Webseite)'), data: [ 'kind' => $kind->value, 'template' => $booking->offer_terms['template'] ?? null, 'annexes' => array_map(fn (array $annex): string => $annex['name'].' (Version '.$annex['version'].')', (array) ($booking->offer_terms['annexes'] ?? [])), 'pdf_sha256' => $booking->contract_pdf_sha256, ]); } if (! $notifyGuest) { return true; } Mail::to($booking->guest->email, $booking->guest->full_name)->send($kind === DocumentKind::ExternalRecord ? new BookingDocumentation($booking) : new BookingConfirmed($booking)); $booking->forceFill(['confirmation_sent_at' => now(), 'confirmation_failed_at' => null])->save(); return true; } catch (Throwable $exception) { // Only class and booking id are logged: mail errors can contain the recipient's address. Log::warning('Buchungsbestätigung nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]); $booking->forceFill(['confirmation_failed_at' => now()])->save(); $this->inbox->post( InboxMessageKind::ConfirmationFailed, 'booking-'.$booking->id, 'Buchungsbestätigung '.$booking->reference.' nicht zugestellt', 'Die Buchung ist verbindlich gespeichert, aber der Vertrag konnte nicht erzeugt oder die Bestätigung nicht versendet werden. Auf der Buchungsseite kann die Bestätigung erneut gesendet werden.', actionUrl: BookingResource::getUrl('view', ['record' => $booking]), propertyId: $booking->property_id, ); return false; } } /** * Sends the confirmation again (admin action / command). Does nothing for bookings that are not binding. */ public function resend(Booking $booking): bool { if (! in_array($booking->status, BookingStatus::occupying(), true)) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'erneut bestätigt'); } return $this->deliver($booking); } }