- 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>
90 lines
4.1 KiB
PHP
90 lines
4.1 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\FactSource;
|
||
use App\Enums\IntakeKind;
|
||
use App\Enums\ProcessingMode;
|
||
use App\Exceptions\ReservationException;
|
||
use App\Models\Booking;
|
||
use App\Models\Guest;
|
||
use Illuminate\Support\Arr;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* The guest's contact data arrived (portal message, e-mail, phone) and is entered on the booking. The landlord chooses
|
||
* whether the process continues automatically – a binding portal booking gets its contract, a request gets the
|
||
* offer – or by hand (data only). An already existing e-mail confirmation of the guest can be recorded as well.
|
||
*/
|
||
class ProvideGuestContact
|
||
{
|
||
public const AUTOMATIC = 'automatic';
|
||
|
||
public const MANUAL = 'manual';
|
||
|
||
public function __construct(private ConfirmBooking $confirm, private AutoOffer $autoOffer, private SendBookingDocuments $documents) {}
|
||
|
||
/**
|
||
* @param array<string, ?string> $contact first_name, last_name, email, phone, street, postal_code, city, country
|
||
* @return string what was done, for the notification
|
||
*
|
||
* @throws ReservationException
|
||
*/
|
||
public function handle(Booking $booking, array $contact, string $process = self::MANUAL, bool $guestConfirmed = false): string
|
||
{
|
||
$contact = array_map(fn ($value) => filled($value) ? trim((string) $value) : null, Arr::only($contact, ['first_name', 'last_name', 'email', 'phone', 'street', 'postal_code', 'city', 'country']));
|
||
if (isset($contact['email'])) {
|
||
$contact['email'] = mb_strtolower($contact['email']);
|
||
}
|
||
|
||
if ($process === self::AUTOMATIC && blank($contact['email'] ?? $booking->guest?->email)) {
|
||
throw new ReservationException('Für den automatischen Ablauf wird die E-Mail-Adresse des Gastes benötigt.');
|
||
}
|
||
|
||
if ($booking->guest === null && (blank($contact['first_name'] ?? null) || blank($contact['last_name'] ?? null))) {
|
||
throw new ReservationException('Bitte Vor- und Nachnamen des Gastes angeben.');
|
||
}
|
||
|
||
// The landlord's entry goes through the common fact intake (v0.11, S5): taken over with its source, the previous
|
||
// value kept as evidence. Carrying on stays with the choice below.
|
||
app(ContributeCaseFacts::class)->handle($booking, collect($contact)->mapWithKeys(fn (?string $value, string $field): array => ['guest.'.$field => $value])->all(), FactSource::Landlord, ['advance' => false, 'reference' => 'landlord:'.now()->getTimestampMs()]);
|
||
if ($guestConfirmed) {
|
||
$booking->forceFill(['guest_confirmed_at' => $booking->guest_confirmed_at ?? now()])->save();
|
||
}
|
||
$booking->refresh()->load('guest');
|
||
|
||
if ($process !== self::AUTOMATIC) {
|
||
return 'Kontaktdaten gespeichert';
|
||
}
|
||
|
||
$booking->update(['processing_mode' => ProcessingMode::Automatic]);
|
||
|
||
if (in_array($booking->status, BookingStatus::occupying(), true)) {
|
||
$reasons = $this->documents->automatically($booking);
|
||
if ($reasons !== []) {
|
||
throw new ReservationException('Kontaktdaten gespeichert. Die Unterlagen werden noch nicht automatisch versendet: '.implode(' ', $reasons));
|
||
}
|
||
|
||
return 'Kontaktdaten gespeichert, Unterlagen gesendet';
|
||
}
|
||
|
||
if ($booking->intake === IntakeKind::Booking) {
|
||
DB::transaction(fn () => $this->confirm->bind($booking));
|
||
$reasons = $this->documents->automatically($booking->fresh());
|
||
if ($reasons !== []) {
|
||
throw new ReservationException('Kontaktdaten gespeichert und Buchung bestätigt. Die Unterlagen werden noch nicht automatisch versendet: '.implode(' ', $reasons));
|
||
}
|
||
|
||
return 'Kontaktdaten gespeichert, Buchung bestätigt und Unterlagen gesendet';
|
||
}
|
||
|
||
$reasons = $this->autoOffer->handle($booking);
|
||
if ($reasons !== []) {
|
||
throw new ReservationException('Kontaktdaten gespeichert. Das Angebot wird noch nicht automatisch gesendet: '.implode(' ', $reasons));
|
||
}
|
||
|
||
return 'Kontaktdaten gespeichert, Angebot gesendet';
|
||
}
|
||
}
|