Files
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

186 lines
7.1 KiB
PHP
Raw Permalink 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\CalendarClearance;
use App\Documents\OfferTerms;
use App\Enums\BookingStatus;
use App\Enums\InboxMessageKind;
use App\Exceptions\ReservationException;
use App\Filament\Resources\Bookings\BookingResource;
use App\Mail\OfferAnswered;
use App\Models\Booking;
use App\Support\Inbox;
use App\Support\Settings;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* The guest accepts the offer on the signed offer page: the booking is concluded. Everything happens in one
* locked transaction (property + booking row): status, validity, integrity of the frozen offer and availability
* are checked, the acceptance record is written and the booking becomes binding – or nothing changes at all.
* Accepting the same version again is idempotent (no second booking, no second mail). Mails follow after the commit
* and never undo the acceptance.
*/
class AcceptOffer
{
public function __construct(private ConfirmBooking $confirm, private Inbox $inbox, private CalendarClearance $clearance) {}
/**
* @return bool true when this call concluded the booking, false when the version had already been accepted
*
* @throws ReservationException
*/
public function handle(Booking $booking, int $offerVersion): bool
{
if ($booking->status === BookingStatus::OfferSent) {
$this->ensureCalendarFresh($booking);
}
try {
$accepted = DB::transaction(function () use ($booking, $offerVersion): bool {
$booking->lockForTransition();
if ($this->alreadyAccepted($booking, $offerVersion)) {
return false;
}
$this->ensureAcceptable($booking, $offerVersion);
$this->ensureUnchanged($booking);
$validUntil = $booking->hold_expires_at?->toIso8601String();
$this->confirm->bind($booking);
$booking->update([
'accepted_at' => now(),
'acceptance' => [
'offer_version' => $booking->offer_version,
'accepted_at' => now()->toIso8601String(),
'offer_sent_at' => $booking->offer_sent_at?->toIso8601String(),
'valid_until' => $validUntil,
'total' => $booking->total,
'offer_hash' => $booking->offer_hash,
'quote_sha256' => hash('sha256', json_encode($booking->quote)),
'terms_sha256' => hash('sha256', json_encode($booking->offer_terms['texts'] ?? [])),
'accept_button' => $booking->offerText('offer.accept_button'),
'via' => 'offer_link',
],
]);
return true;
});
} catch (ReservationException $exception) {
$this->noteRejection($booking, $offerVersion, $exception);
throw $exception;
}
if ($accepted) {
$this->confirm->deliver($booking);
$this->notifyLandlord($booking);
}
return $accepted;
}
/**
* Checks that make the offer page show the form: version, status and validity (no locks, read only).
*
* @throws ReservationException
*/
public function ensureAcceptable(Booking $booking, int $offerVersion): void
{
if ($offerVersion !== $booking->offer_version) {
throw ReservationException::offerOutdated();
}
if ($booking->status !== BookingStatus::OfferSent) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'angenommen');
}
if (! $booking->hold_expires_at?->isFuture()) {
throw ReservationException::offerExpired();
}
}
/**
* The frozen offer must still match its fingerprint (price, dates, terms); offers made before the
* fingerprint existed have none and are not checked.
*
* @throws ReservationException
*/
public function ensureUnchanged(Booking $booking): void
{
if ($booking->offer_hash !== null && ! hash_equals($booking->offer_hash, OfferTerms::hash($booking))) {
throw ReservationException::offerChanged();
}
}
/**
* Before the guest becomes bound, the imported portal calendars must be recent (an import is tried first –
* network access, so outside the transaction). Otherwise nothing is concluded and the landlord is told.
*
* @throws ReservationException
*/
private function ensureCalendarFresh(Booking $booking): void
{
$property = $booking->property;
$this->clearance->refresh($property);
$stale = $this->clearance->staleImports($property);
if ($stale->isEmpty()) {
return;
}
$this->inbox->post(
InboxMessageKind::SyncFailed,
'accept-'.$booking->id.':'.now()->format('YmdH'),
'Annahme von '.$booking->reference.' zurückgestellt: Kalenderstand nicht aktuell',
'Der Gast wollte das Angebot annehmen, aber der Import von '.$stale->pluck('name')->implode(', ').' ist nicht aktuell. Bitte Kalenderabgleich prüfen und den Gast kontaktieren.',
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
propertyId: $booking->property_id,
);
throw ReservationException::calendarUnverified();
}
private function alreadyAccepted(Booking $booking, int $offerVersion): bool
{
return in_array($booking->status, BookingStatus::occupying(), true)
&& $booking->accepted_at !== null
&& ($booking->acceptance['offer_version'] ?? null) === $offerVersion;
}
private function noteRejection(Booking $booking, int $offerVersion, ReservationException $exception): void
{
if ($offerVersion !== $booking->offer_version || $booking->status !== BookingStatus::OfferSent) {
return;
}
$reasonIsUnexpected = $exception->getMessage() === ReservationException::offerChanged()->getMessage()
|| $exception->getMessage() === ReservationException::notAvailable()->getMessage();
if (! $reasonIsUnexpected) {
return;
}
$this->inbox->post(
InboxMessageKind::OfferRejected,
'booking-'.$booking->id.':v'.$offerVersion,
'Annahme von Angebot '.$booking->reference.' abgewiesen',
'Der Gast wollte das Angebot annehmen, aber: '.$exception->getMessage().' Bitte den Gast kontaktieren oder ein neues Angebot senden.',
actionUrl: BookingResource::getUrl('view', ['record' => $booking]),
propertyId: $booking->property_id,
);
}
private function notifyLandlord(Booking $booking): void
{
try {
Mail::to(Settings::notificationRecipients())->send(new OfferAnswered($booking, accepted: true));
} catch (Throwable $exception) {
Log::warning('Benachrichtigung über Annahme nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
}
}
}