$data */ protected function handleRecordCreation(array $data): Model { if (in_array($data['kind'] ?? null, [BookingForm::OWN_USE, BookingForm::BLOCK], true)) { return $this->createInternal($data); } try { $property = Property::query()->findOrFail($data['property_id']); $channel = filled($data['channel_id'] ?? null) ? Channel::query()->findOrFail($data['channel_id']) : null; $arrival = CarbonImmutable::parse($data['arrival']); $departure = CarbonImmutable::parse($data['departure']); $guest = Arr::only($data, ['first_name', 'last_name', 'email', 'phone', 'street', 'postal_code', 'city', 'country']); if ($data['kind'] === BookingForm::EXTERNAL) { return app(TakeOverExternalBooking::class)->handle($property, $channel, $arrival, $departure, [ 'guest' => $guest, 'guests' => filled($data['guests'] ?? null) ? (int) $data['guests'] : null, 'portal_reference' => $data['portal_reference'] ?? null, 'portal_total' => filled($data['portal_total'] ?? null) ? (int) $data['portal_total'] : null, 'notes' => $data['internal_notes'] ?? null, 'guest_confirmed' => (bool) ($data['guest_confirmed'] ?? false), ]); } $stay = new StayRequest($arrival, $departure, (int) $data['guests'], promoCode: filled($data['promo_code'] ?? null) ? $data['promo_code'] : null); $sendMails = (bool) ($data['send_mails'] ?? false); $booking = app(CreateReservation::class)->handle($property, $stay, $guest, source: 'manual', sendMails: $sendMails && ! ($data['confirm_immediately'] ?? false), originChannel: $channel); if ($data['confirm_immediately'] ?? false) { DB::transaction(fn () => app(ConfirmBooking::class)->bind($booking)); if ($sendMails && filled($booking->guest?->email)) { // The documents go out only when their composition needs no check; otherwise they wait on the case page. $waiting = app(SendBookingDocuments::class)->automatically($booking->fresh()); if ($waiting !== []) { Notification::make()->title('Gebucht – Unterlagen noch nicht gesendet')->body(implode(' ', $waiting))->warning()->send(); } } else { app(ConfirmBooking::class)->deliver($booking->fresh(), notifyGuest: false); } } } catch (QuoteException|ReservationException $exception) { Notification::make()->title('Nicht möglich')->body($exception->getMessage())->danger()->send(); $this->halt(); } if (filled($data['internal_notes'] ?? null)) { $booking->update(['internal_notes' => trim(($booking->internal_notes ? $booking->internal_notes."\n\n" : '').$data['internal_notes'])]); } return $booking; } /** * Pre-filled from the calendar or the block list (?kind=block&von=…&bis=…&wohnung=…). */ protected function afterFill(): void { $kind = request()->query('kind'); if (in_array($kind, [BookingForm::INQUIRY, BookingForm::EXTERNAL, BookingForm::OWN_USE, BookingForm::BLOCK], true)) { $this->data['kind'] = $kind; } foreach (['von' => 'arrival', 'bis' => 'departure'] as $parameter => $field) { $value = request()->query($parameter); if (is_string($value) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) === 1) { $this->data[$field] = $value; } } if (is_numeric(request()->query('wohnung')) && Property::query()->whereKey((int) request()->query('wohnung'))->exists()) { $this->data['property_id'] = (int) request()->query('wohnung'); } } /** * Own use, a private stay or a block as a case (A6). * * @param array $data */ private function createInternal(array $data): Model { $kind = $data['kind'] === BookingForm::BLOCK ? InternalKind::Block : InternalKind::from((string) ($data['internal_kind'] ?? InternalKind::OwnUse->value)); $extras = []; foreach ((array) ($data['items'] ?? []) as $item) { if (filled($item['extra_id'] ?? null)) { $extras[(int) $item['extra_id']] = ((int) ($extras[(int) $item['extra_id']] ?? 0)) + max(1, (int) ($item['quantity'] ?? 1)); } } try { return app(CreateInternalCase::class)->handle(Property::query()->findOrFail($data['property_id']), $kind, CarbonImmutable::parse($data['arrival']), CarbonImmutable::parse($data['departure']), [ 'occasion' => $data['occasion'] ?? null, 'guest' => ['first_name' => $data['stay_first_name'] ?? null, 'last_name' => $data['stay_last_name'] ?? null, 'email' => $data['stay_email'] ?? null, 'phone' => $data['stay_phone'] ?? null], 'guests' => $data['guests'] ?? null, 'extras' => $extras, 'rent' => (int) ($data['rent'] ?? 0), 'tourist_tax_persons' => filled($data['tourist_tax_persons'] ?? null) ? (int) $data['tourist_tax_persons'] : null, 'tourist_tax_note' => $data['tourist_tax_note'] ?? null, 'notes' => $data['internal_notes'] ?? null, ]); } catch (QuoteException|ReservationException $exception) { Notification::make()->title('Nicht möglich')->body($exception->getMessage())->danger()->send(); $this->halt(); } } protected function getRedirectUrl(): string { /** @var Booking $record */ $record = $this->getRecord(); return BookingResource::getUrl('view', ['record' => $record]); } }