Files
fewo-stommer/app/Actions/ConfirmBooking.php
T
LarsandClaude Sonnet 5.5 eafab8a2cb
Deploy FeWo mit PipelineManager / deploy (push) Failing after 3m38s
Buchungsprozess: Angebots-Snapshot, atomare Annahme, Auto-Angebot, Kanalfreigabe
- Angebotsbedingungen und Zahlungsdaten werden je Version eingefroren (offer_terms, offer_hash)
- Annahme in einer gesperrten Transaktion, idempotent, mit Nachweis; Mail- und Portalfehler
  machen eine gespeicherte Buchung nicht ungueltig (Postfach, Nachversand)
- Je Wohnung: manuelle/automatische Angebote, optionale Vorreservierung
- Automatische Freigabe abhaengig von aktuellen Importen und Feed-Abruf bei Sofortbuchungsportalen
- Deutsche Fehlerseiten, Buchungsseite zeigt Angebots- und Zustellstatus
- Dokumentation: BOOKING-E2E-*.md, BOOKING-COMPLIANCE-HANDOVER.md

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 12:01:02 +01:00

131 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\Availability\AvailabilityChecker;
use App\Documents\OfferTerms;
use App\Documents\RentalContract;
use App\Enums\BookingStatus;
use App\Enums\InboxMessageKind;
use App\Exceptions\ReservationException;
use App\Filament\Resources\Bookings\BookingResource;
use App\Mail\BookingConfirmed;
use App\Models\Booking;
use App\Support\Inbox;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* Makes a booking binding: payment due dates are fixed, the rental contract is generated and sent with the
* confirmation. Used when the guest accepts an offer (AcceptOffer) and when the landlord confirms directly
* (e.g. a booking agreed on the phone). Expired requests/offers can still be confirmed while the dates are free.
*
* Binding is one locked database step ({@see bind()}); contract and mail follow afterwards ({@see deliver()}).
* A failing mail server never undoes a binding booking – the failure is recorded and the mail can be resent.
*/
class ConfirmBooking
{
public function __construct(
private AvailabilityChecker $availability,
private RentalContract $contract,
private Inbox $inbox,
) {}
/**
* @return bool whether the guest has the confirmation (true when no mail was requested)
*
* @throws ReservationException
*/
public function handle(Booking $booking, 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(fn () => $this->bind($booking));
return $this->deliver($booking, $notifyGuest);
}
/**
* Locks the property and the booking, checks status and availability and makes the booking binding.
* Must run inside a transaction; every writer of bookings takes the same property lock, so two
* overlapping stays can never both become binding.
*
* @throws ReservationException
*/
public function bind(Booking $booking): void
{
$booking->lockForTransition();
if (! in_array($booking->status, [BookingStatus::Reserved, BookingStatus::OfferSent, BookingStatus::Expired], true)) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'bestätigt');
}
if (! $this->availability->isAvailable($booking->property, $booking->arrival, $booking->departure, ignore: $booking)) {
throw ReservationException::notAvailable();
}
$terms = $booking->offer_terms ?? OfferTerms::capture();
$booking->update([
'status' => BookingStatus::Confirmed,
'hold_expires_at' => null,
'confirmed_at' => now(),
'offer_terms' => $terms,
'deposit_due_on' => today()->addDays((int) ($terms['deposit_due_days'] ?? config('fewo.deposit_due_days')))->toDateString(),
]);
}
/**
* Generates the contract and sends the confirmation – after the booking is saved, with every failure
* contained: the booking stays binding, the failure is marked and shown in the admin inbox.
*
* @return bool whether the guest has the confirmation (true when no mail was requested)
*/
public function deliver(Booking $booking, bool $notifyGuest = true): bool
{
$booking->loadMissing(['guest', 'property']);
try {
$this->contract->generate($booking);
if (! $notifyGuest) {
return true;
}
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new BookingConfirmed($booking));
$booking->forceFill(['confirmation_sent_at' => now(), 'confirmation_failed_at' => null])->saveQuietly();
return true;
} catch (Throwable $exception) {
// Only class and booking id are logged: mail errors can contain the recipient's address.
Log::warning('Buchungsbestätigung nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
$booking->forceFill(['confirmation_failed_at' => now()])->saveQuietly();
$this->inbox->post(
InboxMessageKind::ConfirmationFailed,
'booking-'.$booking->id,
'Buchungsbestätigung '.$booking->reference.' nicht zugestellt',
'Die Buchung ist verbindlich gespeichert, aber der Vertrag konnte nicht erzeugt oder die Bestätigung nicht versendet werden. Auf der Buchungsseite kann die Bestätigung erneut gesendet werden.',
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
propertyId: $booking->property_id,
);
return false;
}
}
/**
* Sends the confirmation again (admin action / command). Does nothing for bookings that are not binding.
*/
public function resend(Booking $booking): bool
{
if (! in_array($booking->status, BookingStatus::occupying(), true)) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'erneut bestätigt');
}
return $this->deliver($booking);
}
}