Files
fewo-stommer/app/Observers/BookingObserver.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

99 lines
4.0 KiB
PHP

<?php
namespace App\Observers;
use App\Actions\CreateChannelTasks;
use App\Enums\BookingStatus;
use App\Enums\ChannelTaskAction;
use App\Enums\InboxMessageKind;
use App\Models\Booking;
use App\Models\Property;
use App\Support\Inbox;
use App\Tenancy\TenantContext;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;
/**
* Keeps manually maintained portals in step when a booking starts or stops occupying the calendar.
*/
class BookingObserver
{
public function __construct(private CreateChannelTasks $createChannelTasks) {}
public function created(Booking $booking): void
{
if ($this->occupies($booking->status)) {
$this->tasks($booking, ChannelTaskAction::Block);
}
}
public function updated(Booking $booking): void
{
if (! $booking->wasChanged(['status', 'arrival', 'departure'])) {
return;
}
$before = $this->occupies($booking->getRawOriginal('status'));
$after = $this->occupies($booking->status);
if ($before && (! $after || $booking->wasChanged(['arrival', 'departure']))) {
$this->tasks($booking, ChannelTaskAction::Unblock, CarbonImmutable::parse($booking->getRawOriginal('arrival')), CarbonImmutable::parse($booking->getRawOriginal('departure')));
}
if ($after && (! $before || $booking->wasChanged(['arrival', 'departure']))) {
$this->tasks($booking, ChannelTaskAction::Block);
}
}
private function reportSyncFailure(int $propertyId, int $bookingId, string $reason, Throwable $exception): void
{
Log::error('Portalaufgaben für Buchung nicht angelegt', ['booking' => $bookingId, 'exception' => $exception::class]);
rescue(function () use ($propertyId, $bookingId, $reason): void {
$property = Property::withoutGlobalScope('tenant')->find($propertyId);
if ($property === null) {
return;
}
app(TenantContext::class)->run($property->tenant, fn () => app(Inbox::class)->post(
InboxMessageKind::SyncFailed,
'booking-'.$bookingId.':'.now()->format('YmdH'),
'Portalabgleich für '.$reason.' fehlgeschlagen',
'Die Buchung ist gespeichert, aber die Sperr-/Freigabeaufgaben für die Portale konnten nicht angelegt werden. Bitte den Kalender der Portale prüfen und gegebenenfalls von Hand sperren.',
propertyId: $propertyId,
));
}, report: false);
}
private function occupies(BookingStatus|string|null $status): bool
{
$status = is_string($status) ? BookingStatus::tryFrom($status) : $status;
return in_array($status, BookingStatus::occupying(), true);
}
private function tasks(Booking $booking, ChannelTaskAction $action, ?CarbonImmutable $startsOn = null, ?CarbonImmutable $endsOn = null): void
{
$propertyId = $booking->property_id;
$channelId = $booking->import_channel_id;
$originId = $booking->origin_channel_id;
$external = ! $booking->isDirect();
$startsOn ??= $booking->arrival;
$endsOn ??= $booking->departure;
$reason = "Buchung {$booking->reference}";
$bookingId = $booking->id;
DB::afterCommit(function () use ($propertyId, $channelId, $originId, $external, $startsOn, $endsOn, $action, $reason, $bookingId): void {
// The booking is already saved: a failing portal task must never turn into an error for the guest.
try {
$property = Property::withoutGlobalScope('tenant')->find($propertyId);
if ($property !== null) {
app(TenantContext::class)->run($property->tenant, fn () => $this->createChannelTasks->handle($property, $startsOn, $endsOn, $action, $reason, $channelId, $originId, $external));
}
} catch (Throwable $exception) {
$this->reportSyncFailure($propertyId, $bookingId, $reason, $exception);
}
});
}
}