- CaseRequirements: Vor einem eigenen verbindlichen Angebot und vor der verbindlichen Bestätigung eines Buchungswunsches müssen Vor- und Nachname, E-Mail, vollständige Anschrift mit Land, Personenzahl und Zeitraum vorliegen – harter Blocker, auch von Hand (G2a, B3). Die Auskunft unter Vorbehalt bleibt ohne dieses Gate. - ContributeCaseFacts (S5): jede Angabe mit Herkunft und Ergebnis in case_facts; fehlend → übernommen, gleich → bestätigt, abweichend vom Gast → Prüfaufgabe (Kartei bleibt), Vermieterin maßgeblich, Zeitraum und Personen → Änderungswunsch; idempotent; danach einmal CaseAutomation. - ProvideGuestContact und Webformular nutzen den Fakteneingang. - Land als ISO-Code mit „Deutschland“ als änderbarem Formularstandard; guests.country ohne stillen Standardwert (B4). - Testdaten bestehender Tests um Anschriften ergänzt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
7.0 KiB
PHP
151 lines
7.0 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Resources\Bookings\Pages;
|
||
|
||
use App\Actions\ConfirmBooking;
|
||
use App\Actions\CreateInternalCase;
|
||
use App\Actions\CreateReservation;
|
||
use App\Actions\SendBookingDocuments;
|
||
use App\Actions\TakeOverExternalBooking;
|
||
use App\Enums\InternalKind;
|
||
use App\Exceptions\ReservationException;
|
||
use App\Filament\Resources\Bookings\BookingResource;
|
||
use App\Filament\Resources\Bookings\Schemas\BookingForm;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Models\Property;
|
||
use App\Pricing\QuoteException;
|
||
use App\Pricing\StayRequest;
|
||
use Carbon\CarbonImmutable;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Resources\Pages\CreateRecord;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Support\Arr;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Entry of a new case that did not come through the website: a request (A1/A2) goes the same way as a website request,
|
||
* a booking already binding on a portal (A3–A5) is taken over as it is.
|
||
*/
|
||
class CreateBooking extends CreateRecord
|
||
{
|
||
protected static string $resource = BookingResource::class;
|
||
|
||
protected static ?string $title = 'Neu eintragen';
|
||
|
||
/**
|
||
* @param array<string, mixed> $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<string, mixed> $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]);
|
||
}
|
||
}
|