Files
LarsandClaude Opus 5.5 7e22e12cac U-A/U-B: Prozesskern mit Fachmodulen, versionierten Modellen, eingefrorenem Prozessplan, ProcessRunner (harte/weiche Abhängigkeiten), CasePricing, Loyalty nur im Tester
CaseActivities/CaseAutomation sind Fassaden über ProcessRunner. Verhaltensvergleich v0.11:
11 Abläufe und 198 Konstellationen gleich, beabsichtigte Abweichungen I-004/I-005 (F-001/F-002).
Volle Suite vor U-B: 785/785.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 14:49:44 +01:00

159 lines
8.4 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\Channel;
use App\Models\Guest;
use App\Models\Property;
use App\Models\Tenant;
use App\Support\Settings;
use DomainException;
/**
* 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');
// Architektur V2: the process plan (model, variant, module versions, step settings) is frozen with the profile.
try {
$plan = ProcessPlan::freeze($profile, ProcessPlan::configurationOf(self::configurationChannel($booking), $profile->intake));
} catch (DomainException $exception) {
// Never lose an incoming case over its configuration (e.g. a tester-only variant): it follows the derived plan.
report($exception);
$plan = ProcessPlan::derive($profile);
}
$booking->profile = [...$profile->toArray(), 'plan' => $plan->toArray()];
$booking->case_type = $profile->caseType;
}
/**
* The channel whose configuration (Kanal › Ablauf) applies to the case: its portal, or the own website's entry.
*/
private static function configurationChannel(Booking $booking): ?Channel
{
$channel = $booking->processChannel();
if ($channel === null && $booking->source === 'website' && $booking->property_id !== null) {
$property = $booking->property ?? Property::withoutGlobalScope('tenant')->find($booking->property_id);
$channel = $property === null ? null : Channel::existingWebsiteOf($property);
}
return $channel;
}
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',
};
}
}