getKey())->lockForUpdate()->first(); if (! $this->availability->isAvailable($property, $stay->arrival, $stay->departure)) { throw ReservationException::notAvailable(); } $quote = $this->calculator->calculate($property, $stay); [$guest, $differences] = $this->guestFor($guestData); return $property->bookings()->create([ 'guest_id' => $guest->id, 'status' => BookingStatus::Reserved, 'source' => $source, ...Arr::only(array_filter($attribution), ['referral_source', 'utm_source', 'utm_medium', 'utm_campaign']), 'arrival' => $stay->arrival->toDateString(), 'departure' => $stay->departure->toDateString(), 'guests' => $stay->guests, 'extras' => array_filter($stay->extras), 'promo_code' => $quote->promoCodeApplied ? $stay->promoCode : null, 'quote' => $quote->toArray(), 'total' => $quote->total(), 'deposit' => $quote->deposit(), 'balance_due_on' => $quote->balanceDueOn->toDateString(), 'hold_expires_at' => now()->addHours($property->hold_hours), 'message' => $message, 'internal_notes' => $differences !== [] ? "Abweichende Angaben in dieser Anfrage (Gästekartei nicht geändert):\n".implode("\n", $differences) : null, ]); }); if ($sendMails) { $this->sendMails($booking); } return $booking; } /** * The guest record for the request. A known e-mail address never overwrites existing master data * (anybody can type somebody else's address); empty fields are completed, differing values are * returned so the landlord sees them with the request and can take them over in the offer. * * @param array $guestData * @return array{0: Guest, 1: list} */ private function guestFor(array $guestData): array { $email = mb_strtolower($guestData['email']); $guest = Guest::query()->where('email', $email)->first(); if ($guest === null) { return [Guest::create([...$guestData, 'email' => $email]), []]; } $labels = ['first_name' => 'Vorname', 'last_name' => 'Nachname', 'phone' => 'Telefon', 'street' => 'Straße', 'postal_code' => 'PLZ', 'city' => 'Ort', 'country' => 'Land']; $differences = []; foreach ($labels as $field => $label) { $value = trim((string) ($guestData[$field] ?? '')); if ($value === '') { continue; } if (blank($guest->{$field})) { $guest->{$field} = $value; } elseif (mb_strtolower((string) $guest->{$field}) !== mb_strtolower($value)) { $differences[] = "{$label}: {$value} (Kartei: {$guest->{$field}})"; } } $guest->save(); return [$guest, $differences]; } /** * A failing mail server must not lose the reservation; failures are reported and visible in the log. */ private function sendMails(Booking $booking): void { try { Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ReservationReceived($booking)); Mail::to(config('fewo.notification_email'))->send(new NewReservation($booking)); } catch (Throwable $exception) { report($exception); } } }