, guest?: array, acknowledged?: bool, expected_version?: ?int} $changes * * @throws QuoteException * @throws ReservationException */ public function handle(Booking $booking, array $changes = [], bool $sendMail = true): Booking { if ($sendMail && blank($booking->guest?->email)) { throw new ReservationException('Vor dem E-Mail-Versand bitte die E-Mail-Adresse des zugeordneten Gastes ergänzen.'); } if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'als Angebot versendet'); } // G2a (v0.11): never an own binding offer without the required details – not even confirmed by hand. if (($missing = CaseRequirements::blocker($booking->loadMissing('guest'), CaseRequirements::OFFER)) !== null) { throw new ReservationException($missing); } $toConfirm = app(DocumentComposer::class)->compose($booking, DocumentKind::Offer, $changes)->toConfirm(); if ($toConfirm !== [] && ! ($changes['acknowledged'] ?? false)) { throw new ReservationException('Bitte die Hinweise prüfen und ausdrücklich bestätigen: '.implode(' ', $toConfirm)); } DB::transaction(function () use ($booking, $changes): void { $booking->lockForTransition(); if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'als Angebot versendet'); } if (isset($changes['expected_version']) && $booking->offer_version !== (int) $changes['expected_version']) { throw new ReservationException('Inzwischen wurde schon ein Angebot gesendet (Version '.$booking->offer_version.'). Es wurde nichts erneut gesendet.'); } $arrival = CarbonImmutable::parse($changes['arrival'] ?? $booking->arrival); $departure = CarbonImmutable::parse($changes['departure'] ?? $booking->departure); if (! $this->availability->isAvailable($booking->property, $arrival, $departure, ignore: $booking)) { throw ReservationException::notAvailable(); } $promoCode = array_key_exists('promo_code', $changes) ? (filled($changes['promo_code']) ? trim((string) $changes['promo_code']) : null) : $booking->promo_code; $stay = new StayRequest($arrival, $departure, (int) ($changes['guests'] ?? $booking->guests), $booking->extras ?? [], $promoCode); $adjustment = (int) ($changes['price_adjustment'] ?? 0); $adjustmentLabel = trim((string) ($changes['price_adjustment_label'] ?? '')) ?: ($adjustment < 0 ? 'Nachlass' : 'Zuschlag'); $quote = $this->quoteFor($booking, $stay, $adjustment, $adjustmentLabel); if (filled($changes['guest'] ?? null)) { $booking->guest->update(Arr::only(array_map(fn ($value) => filled($value) ? trim((string) $value) : null, $changes['guest']), ['first_name', 'last_name', 'phone', 'street', 'postal_code', 'city', 'country'])); } $booking->update([ 'status' => BookingStatus::OfferSent, 'arrival' => $arrival->toDateString(), 'departure' => $departure->toDateString(), 'guests' => $stay->guests, 'promo_code' => $quote->promoCodeApplied ? $promoCode : null, 'price_adjustment' => $adjustment !== 0 ? $adjustment : null, 'price_adjustment_label' => $adjustment !== 0 ? $adjustmentLabel : null, 'quote' => $quote->toArray(), 'total' => $quote->total(), 'deposit' => $quote->deposit(), 'balance_due_on' => $quote->balanceDueOn->toDateString(), 'hold_expires_at' => filled($changes['valid_until'] ?? null) ? CarbonImmutable::parse($changes['valid_until']) : now()->addHours($booking->process()->holdHours()), 'hold_purpose' => Booking::HOLD_OFFER, 'offer_version' => $booking->offer_version + 1, 'offer_sent_at' => now(), 'offer_message' => $changes['offer_message'] ?? null, 'offer_terms' => OfferTerms::capture(OfferTerms::templateFor($booking, $changes['contract_template_id'] ?? null), $booking, array_key_exists('annex_ids', $changes) && is_array($changes['annex_ids']) ? array_map('intval', $changes['annex_ids']) : null), ]); $booking->refresh(); $booking->update(['offer_hash' => OfferTerms::hash($booking)]); }); $booking->refresh(); $this->contract->generate($booking, draft: true); CaseLog::record($booking, 'document', 'Angebot Version '.$booking->offer_version.' erstellt'.(isset($booking->offer_terms['template']['name']) ? ' (Vorlage '.$booking->offer_terms['template']['name'].', Version '.$booking->offer_terms['template']['version'].')' : ' (Texte der Webseite)'), data: array_filter([ 'kind' => DocumentKind::Offer->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->offer_pdf_sha256, 'offer_hash' => $booking->offer_hash, 'confirmed' => $toConfirm, ])); if ($sendMail) { $this->deliver($booking); } return $booking; } /** * Sends the offer mail. The offer is already saved (and its validity runs), so a failing mail server is * recorded in the admin inbox and reported to the caller instead of being lost. * * @throws ReservationException */ public function deliver(Booking $booking): void { try { Mail::to($booking->guest->email, $booking->guest->full_name)->send(new OfferMade($booking, $this->offerUrl($booking))); } catch (Throwable $exception) { // Only class and booking id are logged: mail errors can contain the recipient's address. Log::warning('Angebot nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]); $this->inbox->post( InboxMessageKind::OfferMailFailed, 'booking-'.$booking->id.':v'.$booking->offer_version, 'Angebot '.$booking->reference.' nicht zugestellt', 'Das Angebot ist gespeichert und der Zeitraum gehalten, aber die E-Mail konnte nicht versendet werden. Auf der Buchungsseite kann der Angebotslink kopiert und dem Gast anders übermittelt werden.', actionUrl: BookingResource::getUrl('view', ['record' => $booking]), propertyId: $booking->property_id, ); throw new ReservationException('Das Angebot wurde gespeichert, aber die E-Mail konnte nicht versendet werden. Der Angebotslink kann über „Angebotslink“ kopiert werden.'); } } /** * The guest's link to the offer page; valid while the offer is valid and only for this offer version. */ public function offerUrl(Booking $booking): string { $parameters = ['booking' => $booking, 'v' => $booking->offer_version]; return $booking->website()?->temporarySignedRoute('offers.show', $booking->hold_expires_at, $parameters) ?? URL::temporarySignedRoute('offers.show', $booking->hold_expires_at, $parameters); } /** * Price for the (possibly changed) stay plus the manual adjustment; a reduction never makes the stay negative. * * @throws QuoteException */ public function quoteFor(Booking $booking, StayRequest $stay, int $adjustment, string $label): PriceQuote { $quote = $this->calculator->calculate($booking->property, $stay); if ($adjustment === 0) { return $quote; } $adjustment = max($adjustment, -$quote->rent()); return $quote->withLine(new QuoteLine(QuoteLineType::Adjustment, $label, 1, $adjustment, $adjustment)); } }