, guests?: ?int, extras?: ?array, rent?: ?int, tourist_tax_persons?: ?int, tourist_tax_note?: ?string, notes?: ?string} $data * * @throws ReservationException * @throws QuoteException */ public function handle(Property $property, InternalKind $kind, CarbonImmutable $arrival, CarbonImmutable $departure, array $data = []): Booking { if ($departure->lessThanOrEqualTo($arrival)) { throw new ReservationException('Das Ende muss nach dem Beginn liegen.'); } return DB::transaction(function () use ($property, $kind, $arrival, $departure, $data): Booking { Property::query()->whereKey($property->id)->lockForUpdate()->firstOrFail(); if (! $this->availability->isAvailable($property, $arrival, $departure)) { throw ReservationException::notAvailable(); } $guests = $kind === InternalKind::Block ? null : (filled($data['guests'] ?? null) ? (int) $data['guests'] : null); $extras = $kind === InternalKind::Block ? [] : array_filter(array_map('intval', (array) ($data['extras'] ?? []))); $taxPersons = $kind === InternalKind::Block ? null : (filled($data['tourist_tax_persons'] ?? null) ? (int) $data['tourist_tax_persons'] : null); $quote = $kind === InternalKind::Block ? null : $this->price->quote($property, $arrival, $departure, $guests, $extras, (int) ($data['rent'] ?? 0), $taxPersons); $guest = $kind->hasGuests() ? $this->guest((array) ($data['guest'] ?? [])) : null; $booking = $property->bookings()->create([ 'guest_id' => $guest?->id, 'status' => BookingStatus::Confirmed, 'source' => 'internal', 'intake' => IntakeKind::Internal, 'internal_kind' => $kind, 'occasion' => filled($data['occasion'] ?? null) ? trim((string) $data['occasion']) : null, 'arrival' => $arrival->toDateString(), 'departure' => $departure->toDateString(), 'guests' => $guests, 'extras' => $extras, 'quote' => $quote?->toArray(), 'total' => $quote?->total(), 'tourist_tax_persons' => $taxPersons, 'tourist_tax_note' => filled($data['tourist_tax_note'] ?? null) ? trim((string) $data['tourist_tax_note']) : null, 'confirmed_at' => now(), 'internal_notes' => filled($data['notes'] ?? null) ? trim((string) $data['notes']) : null, ]); CaseLog::record($booking, 'intake', $kind->getLabel().' eingetragen'.($booking->occasion ? ': '.$booking->occasion : '')); return $booking; }); } /** * The guest of a private stay – only with a real name; a known e-mail address finds the existing guest. * * @param array $data */ private function guest(array $data): ?Guest { $data = array_map(fn ($value) => filled($value) ? trim((string) $value) : null, Arr::only($data, ['first_name', 'last_name', 'email', 'phone', 'street', 'postal_code', 'city'])); if (blank($data['first_name'] ?? null) || blank($data['last_name'] ?? null)) { return null; } $email = filled($data['email'] ?? null) ? mb_strtolower($data['email']) : null; if ($email !== null && ($known = Guest::query()->where('email', $email)->whereNull('anonymized_at')->first()) !== null) { return $known; } return Guest::query()->create([...array_filter($data, fn ($value) => $value !== null), 'email' => $email ?? '']); } }