} $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'); } DB::transaction(function () use ($booking, $changes): void { $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->quoteWithAdjustment($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->property->hold_hours), 'offer_version' => $booking->offer_version + 1, 'offer_sent_at' => now(), 'offer_message' => $changes['offer_message'] ?? null, ]); }); $booking->refresh(); $this->contract->generate($booking, draft: true); if ($sendMail) { Mail::to($booking->guest->email, $booking->guest->full_name)->send(new OfferMade($booking, $this->offerUrl($booking))); } return $booking; } /** * 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 */ private function quoteWithAdjustment(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)); } }