Files
fewo-stommer/app/Actions/DecideBookingWish.php
T
LarsandClaude Opus 5.5 ccac78a6c9 v0.11 U2: Angebots-Gate, gemeinsamer Fakteneingang mit Herkunft
- 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>
2026-11-03 13:07:18 +01:00

115 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Actions;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Exceptions\ReservationException;
use App\Mail\BookingWishAnswered;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Pricing\StayRequest;
use App\Process\CaseLog;
use App\Process\CaseRequirements;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* The landlord decides on a booking wish (package 7, "Buchungswunsch bestätigen?"): confirm it – availability is checked
* once more under the lock, so two wishes for the same dates can never both become bookings – or decline it with a
* friendly answer. The price is the one the guest was told; a lower price of today may be granted, a higher one needs a
* new information the guest asks for again.
*/
class DecideBookingWish
{
public function __construct(private ConfirmBooking $confirm, private SendOffer $offer) {}
/**
* @return bool whether the guest has the confirmation
*
* @throws ReservationException
*/
public function confirm(Booking $booking, bool $useTodaysPrice = false, bool $notifyGuest = true): bool
{
if ($notifyGuest && blank($booking->guest?->email)) {
throw new ReservationException('Vor dem E-Mail-Versand bitte die E-Mail-Adresse des zugeordneten Gastes ergänzen.');
}
DB::transaction(function () use ($booking, $useTodaysPrice): void {
$booking->lockForTransition();
if ($booking->status !== BookingStatus::Reserved || $booking->wish_at === null) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'als Buchungswunsch bestätigt');
}
// G2a also before the binding confirmation of a wish (v0.11, B3), by hand as well.
if (($missing = CaseRequirements::blocker($booking->load('guest'), CaseRequirements::WISH_CONFIRMATION)) !== null) {
throw new ReservationException($missing);
}
if ($useTodaysPrice) {
$quote = $this->offer->quoteFor($booking, new StayRequest($booking->arrival, $booking->departure, $booking->guests, $booking->extras ?? [], $booking->promo_code), (int) $booking->price_adjustment, (string) ($booking->price_adjustment_label ?: 'Zuschlag'));
if ($quote->total() > (int) $booking->total) {
throw new ReservationException('Der heutige Preis ist höher als der Preis der Auskunft. Ohne Zustimmung des Gastes geht das nicht – bitte eine neue Auskunft senden.');
}
$booking->update(['quote' => $quote->toArray(), 'total' => $quote->total(), 'deposit' => $quote->deposit(), 'balance_due_on' => $quote->balanceDueOn->toDateString()]);
}
$this->confirm->bind($booking);
$booking->update([
'accepted_at' => $booking->wish_at,
'acceptance' => [
'via' => 'booking_wish',
'information_version' => $booking->wish['information_version'] ?? $booking->information_version,
'wish_at' => $booking->wish_at?->toIso8601String(),
'total' => $booking->total,
'quote_sha256' => SendInformation::quoteHash($booking),
'terms_sha256' => $booking->wish['terms_sha256'] ?? null,
'confirmed_by' => CaseLog::actor(),
'confirmed_at' => now()->toIso8601String(),
],
]);
$this->closeTasks($booking, 'Bestätigt.');
});
$booking->refresh();
CaseLog::record($booking, 'wish', 'Buchungswunsch bestätigt – Buchung verbindlich');
return $this->confirm->deliver($booking, $notifyGuest);
}
/**
* @throws ReservationException
*/
public function decline(Booking $booking, ?string $reason = null, bool $notifyGuest = true): void
{
DB::transaction(function () use ($booking): void {
$booking->lockForTransition();
if ($booking->status !== BookingStatus::Reserved) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'abgesagt');
}
$booking->update(['status' => BookingStatus::Declined, 'declined_by' => 'us', 'hold_expires_at' => null]);
$this->closeTasks($booking, 'Abgesagt.');
});
CaseLog::record($booking, 'wish', 'Buchungswunsch abgesagt', reason: $reason);
if ($notifyGuest && filled($booking->guest?->email)) {
try {
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new BookingWishAnswered($booking->fresh(), 'declined'));
} catch (Throwable $exception) {
Log::warning('Absage zum Buchungswunsch nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
}
}
}
private function closeTasks(Booking $booking, string $note): void
{
foreach (BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::BookingWish)->where('status', BookingTaskStatus::Open)->get() as $task) {
$task->complete(CaseLog::actor(), $note);
}
}
}