- data_requests: ein Beschaffungsvorgang je Vorgang mit Modus (automatisch / Freigabe / manuell), getrennten Zählern für Folgefragen und Erinnerungen ohne Rücksetzen, Antwortfrist ≠ Reservierungsfrist. - RequestCaseData: fragt nach, was für das nächste Angebot, die Wunschbestätigung oder (bei verbindlichen Buchungen) administrativ fehlt; ohne E-Mail keine Rückfrage. Teilantwort → gezielte Folgefrage, Schweigen → Erinnerungen, an der Grenze Aufgabe für die Vermieterin. Nie ein Hold. - Mail DataRequested mit Texten nach tatsächlichem Sperrstatus (M11); Dokumentart „Rückfrage“ – im Live-Betrieb ohne Freigabe nur nach Klick. - Signierter, versionierter Ergänzungslink (/angebot/angaben): Lesen schreibt nicht, Absenden über den Fakteneingang, idempotent. - Vorgang: „Rückfrage an den Gast senden“ / „Rückfrage beenden“; Eingangsbestätigung nennt die Klärungsreservierung korrekt. - Zeitplan: cases:data-requests alle fünf Minuten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
7.1 KiB
PHP
151 lines
7.1 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\IntakeKind;
|
||
use App\Enums\OccupationKind;
|
||
use App\Enums\ProcessingMode;
|
||
use App\Exceptions\ReservationException;
|
||
use App\Models\BlockedPeriod;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Models\Guest;
|
||
use App\Models\Property;
|
||
use App\Pricing\PortalPrice;
|
||
use App\Process\CaseAutomation;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Support\Arr;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Takes over a booking that a portal already made binding (case types A3–A5): one case with the portal as origin
|
||
* channel (owner), its reference and the facts the portal delivered – nothing more. There is no offer and no second
|
||
* acceptance; the agreed portal price is taken over as it is; guest, number of guests and price may be unknown and
|
||
* stay unknown until somebody enters them. The case is binding at once and occupies the dates.
|
||
*
|
||
* Used for the manual entry ("Buchung von einem Portal") and for a reservation found in a portal calendar – then the
|
||
* calendar entry stays as the portal's mirror of the case.
|
||
*/
|
||
class TakeOverExternalBooking
|
||
{
|
||
public function __construct(private ConfirmBooking $confirm, private PortalPrice $price) {}
|
||
|
||
/**
|
||
* @param array{guest?: ?array<string, ?string>, guests?: ?int, portal_reference?: ?string, portal_total?: ?int, notes?: ?string, guest_confirmed?: bool, processing_mode?: ?ProcessingMode} $facts
|
||
*
|
||
* @throws ReservationException
|
||
*/
|
||
public function handle(Property $property, Channel $channel, CarbonImmutable $arrival, CarbonImmutable $departure, array $facts = [], ?BlockedPeriod $evidence = null): Booking
|
||
{
|
||
if ($channel->property_id !== $property->id) {
|
||
throw new ReservationException('Der Kanal gehört nicht zu dieser Wohnung.');
|
||
}
|
||
if ($departure->lessThanOrEqualTo($arrival)) {
|
||
throw new ReservationException('Die Abreise muss nach der Anreise liegen.');
|
||
}
|
||
|
||
$booking = DB::transaction(function () use ($property, $channel, $arrival, $departure, $facts, $evidence): Booking {
|
||
Property::query()->whereKey($property->id)->lockForUpdate()->firstOrFail();
|
||
|
||
if ($evidence !== null) {
|
||
$evidence = BlockedPeriod::query()->whereKey($evidence->id)->lockForUpdate()->firstOrFail();
|
||
$existing = $this->checkEvidence($evidence);
|
||
if ($existing !== null) {
|
||
return $existing;
|
||
}
|
||
}
|
||
|
||
$guests = isset($facts['guests']) && (int) $facts['guests'] > 0 ? (int) $facts['guests'] : null;
|
||
$guest = $evidence?->guest ?? $this->guestFrom((array) ($facts['guest'] ?? []));
|
||
$portalTotal = isset($facts['portal_total']) && $facts['portal_total'] !== '' ? (int) $facts['portal_total'] : null;
|
||
$reference = filled($facts['portal_reference'] ?? null) ? trim((string) $facts['portal_reference']) : ($evidence?->ical_interpretation['fields']['booking_reference'] ?? null);
|
||
|
||
$booking = $property->bookings()->create([
|
||
'guest_id' => $guest?->id,
|
||
'status' => BookingStatus::Reserved,
|
||
'source' => 'portal',
|
||
'intake' => IntakeKind::Booking,
|
||
'origin_channel_id' => $evidence?->origin_channel_id ?? $channel->id,
|
||
'portal_reference' => $reference,
|
||
'arrival' => $arrival->toDateString(),
|
||
'departure' => $departure->toDateString(),
|
||
'guests' => $guests,
|
||
'hold_expires_at' => null,
|
||
'guest_confirmed_at' => ($facts['guest_confirmed'] ?? false) ? now() : null,
|
||
'internal_notes' => filled($facts['notes'] ?? null) ? trim((string) $facts['notes']) : null,
|
||
'processing_mode' => $facts['processing_mode'] ?? null,
|
||
...$this->price->columns($property, $arrival, $departure, $guests, $portalTotal, $channel->name),
|
||
]);
|
||
|
||
if ($evidence !== null) {
|
||
$evidence->forceFill([
|
||
'is_representation' => true, 'representation_automatic' => false, 'canonical_block_id' => null,
|
||
'linked_booking_id' => $booking->id, 'export_approved' => false, 'needs_review' => false, 'reviewed_at' => now(),
|
||
'occupation_kind' => OccupationKind::Reservation, 'guest_id' => $guest?->id,
|
||
'review_note' => 'Als Vorgang übernommen; der Vorgang führt diese Belegung.',
|
||
])->save();
|
||
}
|
||
|
||
$this->confirm->bind($booking);
|
||
|
||
if ($evidence !== null) {
|
||
app(ReconcileChannelRepresentations::class)->handle($property);
|
||
}
|
||
|
||
return $booking->refresh();
|
||
});
|
||
|
||
app(CaseAutomation::class)->advance($booking);
|
||
// v0.11 (E-036): the booking stays binding; only missing administrative details of a reachable guest are asked for.
|
||
rescue(fn () => app(RequestCaseData::class)->forNextStep($booking->fresh()), report: true);
|
||
|
||
return $booking->refresh();
|
||
}
|
||
|
||
/**
|
||
* A portal calendar entry can become a case once: returns the case it already belongs to, or throws when the entry
|
||
* is no reservation of a portal.
|
||
*
|
||
* @throws ReservationException
|
||
*/
|
||
private function checkEvidence(BlockedPeriod $evidence): ?Booking
|
||
{
|
||
if (! in_array($evidence->source, ['channel', 'email'], true) || $evidence->channel_id === null) {
|
||
throw new ReservationException('Übernehmen lassen sich nur Reservierungen aus einem Portalkalender.');
|
||
}
|
||
if ($evidence->occupationKind() === OccupationKind::Block) {
|
||
throw new ReservationException('Eine Sperrzeit ist keine Buchung. Bitte zuerst „Einordnen“ und als Portalreservierung kennzeichnen.');
|
||
}
|
||
if ($evidence->is_representation && $evidence->linked_booking_id !== null) {
|
||
return Booking::query()->findOrFail($evidence->linked_booking_id);
|
||
}
|
||
if ($evidence->is_representation) {
|
||
throw new ReservationException('Dieser Eintrag ist die Spiegelung einer anderen Belegung. Der Vorgang gehört an die führende Belegung.');
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* The guest the portal named – only with a real name. A known e-mail address finds the existing guest without
|
||
* overwriting his data; nothing is invented.
|
||
*
|
||
* @param array<string, ?string> $data
|
||
*/
|
||
private function guestFrom(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', 'country']));
|
||
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 ?? '']);
|
||
}
|
||
}
|