Files
fewo-stommer/app/Process/CaseRecorder.php
LarsandClaude Opus 5.5 40d20cb7d1 Pakete 7 und 9: Auskunft und Buchungswunsch, Aufenthalt und Abschluss (Zwischenstand)
- Haltefrist 0: unverbindliche Auskunft mit signiertem Link "Buchung anfragen",
  erneute Prüfung unter Sperre, automatische oder manuelle Bestätigung, Absage
  bei vergebenem Zeitraum, Ablauf unbeantworteter Auskünfte
- Aufenthalt: Anreisedaten, Aufgaben je Wohnung, gebuchte Leistungen,
  Abweichungen und Folgeaufgaben als Aufgaben am Vorgang
- Abschluss getrennt: Aufenthalt abgeschlossen / kaufmännisch abgeschlossen,
  Nachläufer (Forderung, Auszahlung) bleiben sichtbar
- Zahlungsstatus nur aus Geld auf unserem Konto; Altbestand per Vorschau-Befehl
- Verlauf weist Testzustellungen als solche aus
- Abnahmetests T16-T25, T23 über isolierten SMTP-Empfänger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 08:29:19 +01:00

134 lines
7.2 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\Process;
use App\Enums\BookingStatus;
use App\Models\Booking;
use App\Models\Guest;
use App\Models\Tenant;
use App\Support\Settings;
/**
* Model hooks of a case: freezes the process profile when the case comes in (and again when what defines it – intake
* kind, channel, property – changes) and writes the facts of every change into the stored history.
*/
class CaseRecorder
{
/** Attributes that decide which profile applies; changing one re-freezes the profile. */
private const DEFINING = ['intake', 'origin_channel_id', 'import_channel_id', 'property_id'];
public static function freeze(Booking $booking): void
{
if ($booking->isDirty('profile') && filled($booking->profile)) {
return;
}
$changed = $booking->exists && $booking->isDirty(self::DEFINING);
if (filled($booking->profile) && ! $changed) {
return;
}
$booking->intake ??= ProcessSettings::intakeFor($booking);
$profile = app(ProcessSettings::class)->forBooking($booking)->withOrigin($booking->exists ? 'changed' : 'intake');
$booking->profile = $profile->toArray();
$booking->case_type = $profile->caseType;
}
public static function created(Booking $booking): void
{
CaseLog::record($booking, 'created', 'Vorgang angelegt: '.$booking->caseType()->getLabel().' ('.$booking->bookingChannelLabel().')', data: [
'intake' => $booking->intake?->value,
'case_type' => $booking->case_type?->value,
]);
}
public static function updated(Booking $booking): void
{
$changes = $booking->getChanges();
$original = fn (string $key): mixed => $booking->getOriginal($key);
if (array_key_exists('profile', $changes) && $booking->wasChanged(self::DEFINING)) {
CaseLog::record($booking, 'profile', 'Abwicklung neu bestimmt: '.$booking->caseType()->getLabel());
}
if (array_key_exists('guest_id', $changes) && $booking->guest_id !== null) {
CaseLog::record($booking, 'guest', 'Gast zugeordnet: '.(Guest::query()->find($booking->guest_id)?->full_name ?? '–'));
}
if (array_key_exists('information_version', $changes) && $booking->information_version > 0) {
CaseLog::record($booking, 'information', 'Unverbindliche Auskunft Version '.$booking->information_version.' gesendet'.($booking->information_valid_until ? ', Buchung anfragen bis '.$booking->information_valid_until->format('d.m.Y, H:i').' Uhr' : '').' – ohne Reservierung');
}
if (array_key_exists('offer_version', $changes) && $booking->offer_version > 0) {
CaseLog::record($booking, 'offer', 'Angebot Version '.$booking->offer_version.' gesendet'.($booking->hold_expires_at ? ', gültig bis '.$booking->hold_expires_at->format('d.m.Y, H:i').' Uhr' : ''));
} elseif (array_key_exists('hold_expires_at', $changes) && $booking->hold_expires_at !== null && in_array($booking->status, BookingStatus::holding(), true) && ! array_key_exists('status', $changes)) {
CaseLog::record($booking, 'hold', 'Reservierung verlängert bis '.$booking->hold_expires_at->format('d.m.Y, H:i').' Uhr');
}
if (array_key_exists('status', $changes) && $original('status') !== $booking->status && ! ($booking->status === BookingStatus::OfferSent && array_key_exists('offer_version', $changes))) {
CaseLog::record($booking, 'status', self::statusSummary($booking, $original('status')), data: [
'from' => $original('status') instanceof BookingStatus ? $original('status')->value : $original('status'),
'to' => $booking->status->value,
]);
}
if (array_key_exists('arrival', $changes) || array_key_exists('departure', $changes)) {
CaseLog::record($booking, 'dates', 'Zeitraum: '.$booking->arrival->format('d.m.Y').' – '.$booking->departure->format('d.m.Y'));
}
$facts = [
'contact_requested_at' => 'Gast um Kontaktdaten gebeten',
'guest_confirmed_at' => 'Gast hat die Buchung per E-Mail bestätigt',
'confirmation_sent_at' => 'Bestätigung mit Unterlagen an den Gast gesendet',
'confirmation_failed_at' => 'Versand der Bestätigung fehlgeschlagen',
'arrival_info_sent_at' => 'Anreise-Informationen gesendet',
'review_requested_at' => 'Gast um eine Bewertung gebeten',
];
$mailed = ['contact_requested_at', 'confirmation_sent_at', 'arrival_info_sent_at', 'review_requested_at'];
foreach ($facts as $column => $summary) {
if (array_key_exists($column, $changes) && $booking->{$column} !== null) {
CaseLog::record($booking, 'fact', in_array($column, $mailed, true) ? self::mailFact($booking, $summary) : $summary);
}
}
if (array_key_exists('processing_mode', $changes)) {
CaseLog::record($booking, 'mode', 'Bearbeitung dieses Vorgangs: '.($booking->processing_mode?->getLabel() ?? 'wie eingestellt ('.$booking->process()->processing()->getLabel().')'));
}
if (array_key_exists('archived_at', $changes)) {
CaseLog::record($booking, 'archive', $booking->archived_at ? 'Archiviert' : 'Aus dem Archiv geholt');
}
}
/**
* A fact that says a mail went to the guest – in test operation it went to the test address only, and the history says so.
*/
private static function mailFact(Booking $booking, string $summary): string
{
$redirect = Settings::mailRedirectFor(Tenant::query()->find($booking->tenant_id));
return $redirect === null ? $summary : $summary.' – Testbetrieb: nur an die Testadresse, nicht an den Gast';
}
private static function statusSummary(Booking $booking, mixed $from): string
{
$from = $from instanceof BookingStatus ? $from : BookingStatus::tryFrom((string) $from);
return match ($booking->status) {
BookingStatus::Confirmed => ($booking->acceptance['via'] ?? null) === 'booking_wish' && $booking->wasChanged('acceptance')
? 'Buchungswunsch bestätigt – Buchung verbindlich'
: ($booking->wasChanged('accepted_at') && $booking->accepted_at !== null
? 'Angebot vom Gast angenommen – Buchung verbindlich'
: ($booking->caseType()->isExternallyBinding() ? 'Beim Portal verbindlich gebucht – übernommen' : 'Buchung verbindlich bestätigt')),
BookingStatus::DepositPaid => 'Anzahlung eingegangen',
BookingStatus::Paid => 'Vollständig bezahlt',
BookingStatus::Completed => 'Aufenthalt abgeschlossen'.($booking->settled_at === null ? ' – kaufmännische Punkte laufen weiter' : ''),
BookingStatus::Declined => 'Ohne Buchung beendet (abgelehnt'.($booking->declined_by ? ' durch '.($booking->declined_by === 'guest' ? 'den Gast' : 'uns') : '').')',
BookingStatus::Expired => 'Ohne Buchung beendet (Frist abgelaufen)',
BookingStatus::Cancelled => 'Storniert',
BookingStatus::Reserved => $from === null ? 'Anfrage vorgemerkt' : 'Wieder als Anfrage geführt (vorher: '.$from->getLabel().')',
BookingStatus::OfferSent => 'Angebot liegt beim Gast',
};
}
}