Vorgangskern und geführte Vorgangsbearbeitung (Pakete 1 und 2, Prozessarchitektur v0.10)
- Buchung ist der einheitliche Vorgang: Eingangsart, Vorgangsart A1–A6, eingefrorenes Abwicklungsprofil, gespeicherter Verlauf (booking_events); Gast, Personen und Beträge dürfen unbekannt sein, Platzhalter-Gäste werden migriert und nicht mehr angelegt - Einstellungen in Ebenen: Vermieterstandard (So arbeite ich), Abweichung der Wohnung (Experte), Abweichung je Portal und Eingangsart; ersetzt booking_model als Steuergröße - Ein Weg für extern verbindliche Buchungen (TakeOverExternalBooking) statt RecordPortalBooking und "Akte anlegen"; Portalpreis als belegte Gesamtposition - Startseite "Zu erledigen", neue Vorgangsseite mit nächstem Schritt, Automatik-Schalter, "Beenden" mit Storno statt Statusumschaltung; Status von Hand nur im Expertenmodus - Migration übernimmt bisheriges Verhalten unverändert (abweichende Wohnungswerte bleiben als Abweichung), Bericht: php artisan process:settings-report Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9005c12164
commit
3a728c3255
78 files changed
+4296
-1180
No files matched your search
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\ActivityState;
|
||||
|
||||
/**
|
||||
* One activity of a case as the landlord sees it: what it is, where it stands and – when something is to be done – the
|
||||
* action on the case page that does it (or a link elsewhere).
|
||||
*/
|
||||
final class Activity
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $key,
|
||||
public readonly string $label,
|
||||
public readonly ActivityState $state,
|
||||
public readonly ?string $detail = null,
|
||||
public readonly ?string $action = null,
|
||||
public readonly ?string $url = null,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\ActivityState;
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\BookingTaskStatus;
|
||||
use App\Enums\ChannelTaskStatus;
|
||||
use App\Enums\IntakeKind;
|
||||
use App\Enums\ProcessingMode;
|
||||
use App\Enums\Responsibility;
|
||||
use App\Filament\Resources\ChannelTasks\ChannelTaskResource;
|
||||
use App\Models\Booking;
|
||||
use App\Models\BookingTask;
|
||||
use App\Models\ChannelTask;
|
||||
use App\Support\Money;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The activities of a case derived from its process profile and its data (docs/Prozessarchitektur, P01–P05): which
|
||||
* apply, which are done (by us or by the portal), what is waiting for somebody else and what the landlord has to do
|
||||
* next. Nothing here is stored; the gates G1–G5 are these states, not an extra status machine.
|
||||
*/
|
||||
class CaseActivities
|
||||
{
|
||||
/** Activities that are listed on their own in the work list (they are not "the" next step of a case). */
|
||||
private const LISTED_SEPARATELY = ['channel_block'];
|
||||
|
||||
/**
|
||||
* @return list<Activity>
|
||||
*/
|
||||
public static function for(Booking $booking): array
|
||||
{
|
||||
$booking->loadMissing(['guest', 'property', 'payments']);
|
||||
$profile = $booking->process();
|
||||
$closed = in_array($booking->status, [BookingStatus::Cancelled, BookingStatus::Declined, BookingStatus::Expired], true);
|
||||
$binding = $booking->isBinding();
|
||||
$inquiry = $booking->intake !== IntakeKind::Booking;
|
||||
$today = CarbonImmutable::today();
|
||||
$later = fn (ActivityState $state): ActivityState => $closed && ! in_array($state, [ActivityState::Done, ActivityState::External], true) ? ActivityState::NotApplicable : $state;
|
||||
$activities = [];
|
||||
|
||||
$activities[] = new Activity('intake', $inquiry ? 'Anfrage eingegangen' : 'Buchung beim Portal eingegangen', ActivityState::Done, $booking->created_at?->format('d.m.Y').' · '.$booking->bookingChannelLabel());
|
||||
|
||||
$needsContact = $inquiry || $profile->documents() === Responsibility::Landlord || $profile->guestMessages() === Responsibility::Landlord || $profile->payments() === Responsibility::Landlord;
|
||||
if ($needsContact) {
|
||||
$hasEmail = filled($booking->guest?->email);
|
||||
$state = match (true) {
|
||||
$hasEmail => ActivityState::Done,
|
||||
$booking->contact_requested_at !== null => ActivityState::Waiting,
|
||||
default => ActivityState::Open,
|
||||
};
|
||||
$activities[] = new Activity('contact', $booking->guest === null ? 'Name und Kontaktdaten des Gastes eintragen' : 'E-Mail-Adresse des Gastes eintragen', $later($state),
|
||||
$hasEmail ? $booking->guestName() : ($booking->contact_requested_at ? 'beim Gast erfragt am '.$booking->contact_requested_at->format('d.m.Y') : 'fehlt noch'),
|
||||
$hasEmail ? null : 'provideContact');
|
||||
}
|
||||
|
||||
if ($inquiry) {
|
||||
$offerDone = $booking->offer_version > 0 || $binding;
|
||||
$activities[] = new Activity('offer', $booking->processingMode() === ProcessingMode::Automatic ? 'Angebot senden (automatisch)' : 'Angebot prüfen und senden', $later(match (true) {
|
||||
$offerDone => ActivityState::Done,
|
||||
$booking->status === BookingStatus::Reserved && filled($booking->guest?->email) => ActivityState::Open,
|
||||
default => ActivityState::Later,
|
||||
}), $booking->offer_version > 0 ? 'Version '.$booking->offer_version.' vom '.$booking->offer_sent_at?->format('d.m.Y') : ($booking->hold_expires_at ? 'Zeitraum reserviert bis '.$booking->hold_expires_at->format('d.m.Y, H:i').' Uhr' : null), $offerDone ? null : 'sendOffer');
|
||||
|
||||
$activities[] = new Activity('acceptance', 'Gast nimmt das Angebot an', $later(match (true) {
|
||||
$binding => ActivityState::Done,
|
||||
$booking->status === BookingStatus::OfferSent => ActivityState::Waiting,
|
||||
default => ActivityState::Later,
|
||||
}), match (true) {
|
||||
$booking->accepted_at !== null => 'angenommen am '.$booking->accepted_at->format('d.m.Y'),
|
||||
$binding => 'direkt bestätigt',
|
||||
$booking->status === BookingStatus::OfferSent => 'Angebot gültig bis '.$booking->hold_expires_at?->format('d.m.Y, H:i').' Uhr',
|
||||
default => null,
|
||||
});
|
||||
} else {
|
||||
$activities[] = new Activity('binding', 'Beim Portal fest gebucht', $binding || $closed ? ActivityState::External : ActivityState::Open, $booking->portal_reference ? 'Buchungsnummer '.$booking->portal_reference : null, $binding ? null : 'confirm');
|
||||
$activities[] = new Activity('price', 'Gesamtpreis laut Portal eintragen', $later($booking->total === null ? ActivityState::Open : ActivityState::Done), $booking->total === null ? 'noch nicht bekannt' : Money::format($booking->total), $booking->total === null ? 'portalPrice' : null);
|
||||
}
|
||||
|
||||
if ($profile->documents() === Responsibility::Portal) {
|
||||
$activities[] = new Activity('documents', 'Buchungsunterlagen an den Gast', ActivityState::External);
|
||||
} else {
|
||||
$state = match (true) {
|
||||
$booking->confirmation_sent_at !== null => ActivityState::Done,
|
||||
! $binding => ActivityState::Later,
|
||||
$booking->confirmation_failed_at !== null => ActivityState::Problem,
|
||||
default => ActivityState::Open,
|
||||
};
|
||||
$missing = $binding && $state === ActivityState::Open ? app(CaseAutomation::class)->documentBlockers($booking) : [];
|
||||
$activities[] = new Activity('documents', $inquiry ? 'Buchungsbestätigung mit Vertrag senden' : 'Buchungsunterlagen an den Gast senden', $later($missing !== [] ? ActivityState::Waiting : $state),
|
||||
match (true) {
|
||||
$booking->confirmation_sent_at !== null => 'gesendet am '.$booking->confirmation_sent_at->format('d.m.Y'),
|
||||
$state === ActivityState::Problem => 'Versand fehlgeschlagen',
|
||||
$missing !== [] => 'wartet: '.implode(' ', $missing),
|
||||
default => null,
|
||||
}, $binding && $missing === [] && $booking->confirmation_sent_at === null ? 'resendConfirmation' : null);
|
||||
}
|
||||
|
||||
foreach (self::channelTasks($booking) as $task) {
|
||||
$activities[] = new Activity('channel_block', ($task->action->value === 'block' ? 'In ' : 'Freigeben in ').$task->channel?->name.($task->action->value === 'block' ? ' sperren' : ''), $task->status === ChannelTaskStatus::Open ? ActivityState::Open : ActivityState::Done, $task->period(), url: ChannelTaskResource::getUrl());
|
||||
}
|
||||
|
||||
if ($profile->payments() === Responsibility::Portal) {
|
||||
$activities[] = new Activity('payment', 'Zahlung des Gastes', ActivityState::External, 'läuft über das Portal');
|
||||
} elseif ($booking->total === null) {
|
||||
$activities[] = new Activity('payment', 'Zahlungen überwachen', $later(ActivityState::Waiting), 'erst möglich, wenn der Preis bekannt ist');
|
||||
} else {
|
||||
$paid = $booking->paidAmount();
|
||||
$activities[] = new Activity('deposit', 'Anzahlung erhalten', $later(match (true) {
|
||||
$binding && $paid >= (int) $booking->deposit => ActivityState::Done,
|
||||
$binding => ActivityState::Waiting,
|
||||
default => ActivityState::Later,
|
||||
}), Money::format((int) $booking->deposit).($booking->deposit_due_on ? ', fällig '.$booking->deposit_due_on->format('d.m.Y') : ''));
|
||||
$activities[] = new Activity('balance', 'Restzahlung erhalten', $later(match (true) {
|
||||
$binding && $paid >= $booking->total => ActivityState::Done,
|
||||
$binding && $paid >= (int) $booking->deposit => ActivityState::Waiting,
|
||||
default => ActivityState::Later,
|
||||
}), Money::format(max(0, $booking->total - (int) $booking->deposit)).($booking->balance_due_on ? ', fällig '.$booking->balance_due_on->format('d.m.Y') : ''));
|
||||
}
|
||||
|
||||
foreach (BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get() as $task) {
|
||||
$activities[] = new Activity('task_'.$task->kind->value, $task->kind->getLabel(), ActivityState::Open, $task->due_on ? 'fällig '.$task->due_on->format('d.m.Y') : null, $task->kind->isPayment() ? null : 'end');
|
||||
}
|
||||
|
||||
$arrived = $binding && $booking->arrival <= $today;
|
||||
$left = $binding && $booking->departure <= $today;
|
||||
$activities[] = $profile->guestMessages() === Responsibility::Portal
|
||||
? new Activity('arrival_info', 'Anreise-Informationen an den Gast', ActivityState::External)
|
||||
: new Activity('arrival_info', 'Anreise-Informationen an den Gast (automatisch)', $later($booking->arrival_info_sent_at !== null ? ActivityState::Done : ActivityState::Later), $booking->arrival_info_sent_at ? 'gesendet am '.$booking->arrival_info_sent_at->format('d.m.Y') : $profile->arrivalInfoDaysBefore().' Tage vor Anreise');
|
||||
$activities[] = new Activity('stay', 'Aufenthalt', $later(match (true) {
|
||||
$left => ActivityState::Done,
|
||||
$arrived => ActivityState::Waiting,
|
||||
default => ActivityState::Later,
|
||||
}), $booking->arrival->format('d.m.Y').' – '.$booking->departure->format('d.m.Y'));
|
||||
if ($profile->guestMessages() === Responsibility::Landlord) {
|
||||
$activities[] = new Activity('review', 'Bitte um eine Bewertung (automatisch)', $later($booking->review_requested_at !== null ? ActivityState::Done : ActivityState::Later));
|
||||
}
|
||||
|
||||
return $activities;
|
||||
}
|
||||
|
||||
/**
|
||||
* What the landlord should do next on this case, or null when nothing is up to him.
|
||||
*/
|
||||
public static function next(Booking $booking): ?Activity
|
||||
{
|
||||
foreach (self::for($booking) as $activity) {
|
||||
if ($activity->state->needsAction() && ! in_array($activity->key, self::LISTED_SEPARATELY, true)) {
|
||||
return $activity;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The state of the case in one sentence of plain words.
|
||||
*/
|
||||
public static function situation(Booking $booking): string
|
||||
{
|
||||
$today = CarbonImmutable::today();
|
||||
$channel = $booking->processChannel()?->name;
|
||||
$kind = $booking->intake === IntakeKind::Booking ? 'Gebucht über '.($channel ?? 'ein Portal') : ($channel ? 'Anfrage über '.$channel : ($booking->source === 'website' ? 'Anfrage über die Webseite' : 'Anfrage'));
|
||||
|
||||
$state = match ($booking->status) {
|
||||
BookingStatus::Reserved => $booking->isHoldActive() ? 'Zeitraum reserviert bis '.$booking->hold_expires_at->format('d.m., H:i').' Uhr' : 'Zeitraum nicht reserviert',
|
||||
BookingStatus::OfferSent => $booking->isHoldActive() ? 'Angebot liegt beim Gast bis '.$booking->hold_expires_at->format('d.m., H:i').' Uhr' : 'Angebot abgelaufen',
|
||||
BookingStatus::Confirmed => 'fest gebucht',
|
||||
BookingStatus::DepositPaid => 'fest gebucht, Anzahlung erhalten',
|
||||
BookingStatus::Paid => 'fest gebucht, bezahlt',
|
||||
BookingStatus::Completed => 'abgeschlossen',
|
||||
BookingStatus::Declined => 'ohne Buchung beendet',
|
||||
BookingStatus::Expired => 'Frist abgelaufen, ohne Buchung beendet',
|
||||
BookingStatus::Cancelled => 'storniert',
|
||||
};
|
||||
|
||||
$when = match (true) {
|
||||
! $booking->isBinding() => null,
|
||||
$booking->departure <= $today => 'abgereist am '.$booking->departure->format('d.m.Y'),
|
||||
$booking->arrival <= $today => 'Gast ist vor Ort bis '.$booking->departure->format('d.m.Y'),
|
||||
default => 'Anreise in '.(int) $today->diffInDays($booking->arrival).' Tagen',
|
||||
};
|
||||
|
||||
return implode(' · ', array_filter([$kind, $state, $when]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Portal tasks for the dates of this case (block when it starts occupying, unblock when it ends).
|
||||
*
|
||||
* @return iterable<ChannelTask>
|
||||
*/
|
||||
private static function channelTasks(Booking $booking): iterable
|
||||
{
|
||||
return ChannelTask::query()
|
||||
->with('channel')
|
||||
->whereHas('channel', fn ($query) => $query->where('property_id', $booking->property_id))
|
||||
->whereDate('starts_on', $booking->arrival)
|
||||
->whereDate('ends_on', $booking->departure)
|
||||
->where('status', '!=', ChannelTaskStatus::Dismissed)
|
||||
->orderBy('id')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Actions\AutoOffer;
|
||||
use App\Actions\ConfirmBooking;
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\IntakeKind;
|
||||
use App\Enums\ProcessingMode;
|
||||
use App\Enums\Responsibility;
|
||||
use App\Models\Booking;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Carries a case on by itself as far as its data allows – only when the case is handled automatically. What it may do
|
||||
* is what the landlord would do next anyway: send the offer of a request, or send the documents of a booking that is
|
||||
* binding on a portal once the guest's e-mail address and the price are known. Whatever stops it is returned in plain
|
||||
* words; the case then shows it as a task. Never touches a case whose profile was taken over from the old data (the
|
||||
* automation did not exist for those cases before).
|
||||
*/
|
||||
class CaseAutomation
|
||||
{
|
||||
public function __construct(private AutoOffer $autoOffer, private ConfirmBooking $confirm) {}
|
||||
|
||||
/**
|
||||
* @return list<string> what keeps the case from going on automatically (empty: nothing to do or done)
|
||||
*/
|
||||
public function advance(Booking $booking): array
|
||||
{
|
||||
$booking->loadMissing(['guest', 'property']);
|
||||
|
||||
if ($booking->processingMode() !== ProcessingMode::Automatic) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
return CaseLog::context(fn (): array => $this->step($booking), actor: CaseLog::SYSTEM.' (automatisch)');
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return ['Technischer Fehler beim automatischen Weiterbearbeiten – bitte von Hand fortsetzen.'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function step(Booking $booking): array
|
||||
{
|
||||
if ($booking->status === BookingStatus::Reserved && $booking->offer_version === 0 && $booking->intake !== IntakeKind::Booking) {
|
||||
return $this->autoOffer->handle($booking);
|
||||
}
|
||||
|
||||
if ($booking->isBinding() && $booking->confirmation_sent_at === null && $booking->process()->documents() === Responsibility::Landlord && $booking->process()->origin !== 'migration') {
|
||||
$missing = $this->documentBlockers($booking);
|
||||
if ($missing !== []) {
|
||||
return $missing;
|
||||
}
|
||||
|
||||
return $this->confirm->deliver($booking) ? [] : ['Die Unterlagen konnten nicht versendet werden (siehe Postfach).'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* What is missing before the documents of a binding case can go to the guest.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function documentBlockers(Booking $booking): array
|
||||
{
|
||||
return array_values(array_filter([
|
||||
$booking->guest === null ? 'Name und Kontaktdaten des Gastes fehlen.' : null,
|
||||
$booking->guest !== null && blank($booking->guest->email) ? 'Die E-Mail-Adresse des Gastes fehlt.' : null,
|
||||
$booking->total === null ? 'Der Gesamtpreis (laut Portal) fehlt.' : null,
|
||||
]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Models\BookingEvent;
|
||||
use Carbon\CarbonInterface;
|
||||
|
||||
/**
|
||||
* Writes the stored history of a case. Model hooks record the facts (status, documents, payments, mails); actions
|
||||
* that know more (who acted on whose behalf, and why) wrap their work in {@see self::context()}.
|
||||
*/
|
||||
class CaseLog
|
||||
{
|
||||
public const SYSTEM = 'System';
|
||||
|
||||
public const GUEST = 'Gast';
|
||||
|
||||
private static ?string $actor = null;
|
||||
|
||||
private static ?string $reason = null;
|
||||
|
||||
/**
|
||||
* Runs the callback with a named actor and/or reason for every entry written meanwhile.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @param callable(): T $callback
|
||||
* @return T
|
||||
*/
|
||||
public static function context(callable $callback, ?string $actor = null, ?string $reason = null): mixed
|
||||
{
|
||||
[$previousActor, $previousReason] = [self::$actor, self::$reason];
|
||||
self::$actor = $actor ?? $previousActor;
|
||||
self::$reason = $reason ?? $previousReason;
|
||||
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
[self::$actor, self::$reason] = [$previousActor, $previousReason];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function record(Booking $booking, string $type, string $summary, ?string $reason = null, array $data = [], ?string $actor = null, ?CarbonInterface $at = null): BookingEvent
|
||||
{
|
||||
$event = new BookingEvent([
|
||||
'booking_id' => $booking->id,
|
||||
'type' => $type,
|
||||
'summary' => mb_substr($summary, 0, 500),
|
||||
'actor' => $actor ?? self::actor(),
|
||||
'reason' => $reason ?? self::$reason,
|
||||
'data' => $data === [] ? null : $data,
|
||||
'occurred_at' => $at ?? now(),
|
||||
]);
|
||||
$event->tenant_id = $booking->tenant_id;
|
||||
$event->save();
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Who is acting right now: the named actor, the signed-in user or the system (scheduler, mail import).
|
||||
*/
|
||||
public static function actor(): string
|
||||
{
|
||||
return self::$actor ?? auth()->user()?->name ?? self::SYSTEM;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Guest;
|
||||
|
||||
/**
|
||||
* 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('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',
|
||||
];
|
||||
foreach ($facts as $column => $summary) {
|
||||
if (array_key_exists($column, $changes) && $booking->{$column} !== null) {
|
||||
CaseLog::record($booking, 'fact', $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');
|
||||
}
|
||||
}
|
||||
|
||||
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->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',
|
||||
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',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\CaseType;
|
||||
use App\Enums\IntakeKind;
|
||||
use App\Enums\ProcessingMode;
|
||||
use App\Enums\ProcessSetting;
|
||||
use App\Enums\Responsibility;
|
||||
|
||||
/**
|
||||
* The rules that apply to one case: its intake kind, case type and the effective value of every process setting
|
||||
* together with the level it came from. Frozen on the case when it comes in, so later changes of the settings only
|
||||
* affect new cases.
|
||||
*/
|
||||
final class ProcessProfile
|
||||
{
|
||||
public const SYSTEM = 'system';
|
||||
|
||||
public const TENANT = 'tenant';
|
||||
|
||||
public const PROPERTY = 'property';
|
||||
|
||||
public const CHANNEL = 'channel';
|
||||
|
||||
/** Settings resolved when they are used instead of being frozen (documents capture their own template). */
|
||||
public const LIVE = [ProcessSetting::ContractTemplate];
|
||||
|
||||
/**
|
||||
* @param array<string, string|int|bool|null> $values setting key => effective value
|
||||
* @param array<string, string> $sources setting key => level the value came from
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly IntakeKind $intake,
|
||||
public readonly CaseType $caseType,
|
||||
public readonly array $values,
|
||||
public readonly array $sources,
|
||||
public readonly ?string $frozenAt = null,
|
||||
public readonly string $origin = 'intake',
|
||||
) {}
|
||||
|
||||
/**
|
||||
* The case type of a constellation: internal occasions are A6; requests are A1 (own way) or A2 (intermediary);
|
||||
* binding external bookings are A3–A5 by who handles documents and payments.
|
||||
*/
|
||||
public static function caseTypeFor(IntakeKind $intake, bool $viaChannel, Responsibility $documents, Responsibility $payments): CaseType
|
||||
{
|
||||
return match ($intake) {
|
||||
IntakeKind::Internal => CaseType::A6,
|
||||
IntakeKind::Inquiry => $viaChannel ? CaseType::A2 : CaseType::A1,
|
||||
IntakeKind::Booking => match (true) {
|
||||
$documents === Responsibility::Portal => CaseType::A5,
|
||||
$payments === Responsibility::Portal => CaseType::A4,
|
||||
default => CaseType::A3,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public function value(ProcessSetting $setting): string|int|bool|null
|
||||
{
|
||||
return array_key_exists($setting->value, $this->values) ? $this->values[$setting->value] : $setting->default();
|
||||
}
|
||||
|
||||
public function source(ProcessSetting $setting): string
|
||||
{
|
||||
return $this->sources[$setting->value] ?? self::SYSTEM;
|
||||
}
|
||||
|
||||
public function processing(): ProcessingMode
|
||||
{
|
||||
return ProcessingMode::tryFrom((string) $this->value(ProcessSetting::Processing)) ?? ProcessingMode::Manual;
|
||||
}
|
||||
|
||||
public function preReservation(): bool
|
||||
{
|
||||
return (bool) $this->value(ProcessSetting::PreReservation);
|
||||
}
|
||||
|
||||
public function holdHours(): int
|
||||
{
|
||||
return max(1, (int) $this->value(ProcessSetting::HoldHours));
|
||||
}
|
||||
|
||||
public function documents(): Responsibility
|
||||
{
|
||||
return Responsibility::tryFrom((string) $this->value(ProcessSetting::Documents)) ?? Responsibility::Landlord;
|
||||
}
|
||||
|
||||
public function payments(): Responsibility
|
||||
{
|
||||
return Responsibility::tryFrom((string) $this->value(ProcessSetting::Payments)) ?? Responsibility::Landlord;
|
||||
}
|
||||
|
||||
public function guestMessages(): Responsibility
|
||||
{
|
||||
return Responsibility::tryFrom((string) $this->value(ProcessSetting::GuestMessages)) ?? Responsibility::Landlord;
|
||||
}
|
||||
|
||||
public function arrivalInfoDaysBefore(): int
|
||||
{
|
||||
return max(1, (int) $this->value(ProcessSetting::ArrivalInfoDaysBefore));
|
||||
}
|
||||
|
||||
public function contractTemplateId(): ?int
|
||||
{
|
||||
$value = $this->value(ProcessSetting::ContractTemplate);
|
||||
|
||||
return $value === null ? null : (int) $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plain words for a level, for the "where does this come from" hint.
|
||||
*/
|
||||
public static function sourceLabel(string $source): string
|
||||
{
|
||||
return match ($source) {
|
||||
self::TENANT => 'Ihre Standardeinstellung',
|
||||
self::PROPERTY => 'Abweichung der Wohnung',
|
||||
self::CHANNEL => 'Abweichung des Portals',
|
||||
'migration' => 'aus dem bisherigen Stand übernommen',
|
||||
default => 'Grundeinstellung',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The profile as it is frozen on the case (settings resolved live are left out).
|
||||
*
|
||||
* @return array{intake: string, case_type: string, values: array<string, string|int|bool|null>, sources: array<string, string>, frozen_at: string, origin: string}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$live = array_flip(array_map(fn (ProcessSetting $setting): string => $setting->value, self::LIVE));
|
||||
|
||||
return [
|
||||
'intake' => $this->intake->value,
|
||||
'case_type' => $this->caseType->value,
|
||||
'values' => array_diff_key($this->values, $live),
|
||||
'sources' => array_diff_key($this->sources, $live),
|
||||
'frozen_at' => $this->frozenAt ?? now()->toIso8601String(),
|
||||
'origin' => $this->origin,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
return new self(
|
||||
IntakeKind::tryFrom((string) ($data['intake'] ?? '')) ?? IntakeKind::Inquiry,
|
||||
CaseType::tryFrom((string) ($data['case_type'] ?? '')) ?? CaseType::A1,
|
||||
(array) ($data['values'] ?? []),
|
||||
(array) ($data['sources'] ?? []),
|
||||
isset($data['frozen_at']) ? (string) $data['frozen_at'] : null,
|
||||
(string) ($data['origin'] ?? 'intake'),
|
||||
);
|
||||
}
|
||||
|
||||
public function withOrigin(string $origin): self
|
||||
{
|
||||
return new self($this->intake, $this->caseType, $this->values, $this->sources, $this->frozenAt, $origin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\ChannelBookingModel;
|
||||
use App\Enums\IntakeKind;
|
||||
use App\Enums\ProcessSetting;
|
||||
use App\Enums\Responsibility;
|
||||
use App\Models\Booking;
|
||||
use App\Models\Channel;
|
||||
use App\Models\Property;
|
||||
use App\Models\Tenant;
|
||||
|
||||
/**
|
||||
* Resolves the effective process settings: built-in default → landlord default (tenant) → deviation of the property
|
||||
* (expert area) → deviation of the channel for the intake kind. A value that is not set inherits from the level above;
|
||||
* a deliberately set 0, "no" or "manual" is a value of its own.
|
||||
*/
|
||||
class ProcessSettings
|
||||
{
|
||||
public function resolve(Property $property, ?Channel $channel, IntakeKind $intake): ProcessProfile
|
||||
{
|
||||
$propertyLevel = (array) ($property->process_overrides ?? []);
|
||||
if ($property->contract_template_id !== null) {
|
||||
$propertyLevel[ProcessSetting::ContractTemplate->value] = $property->contract_template_id;
|
||||
}
|
||||
|
||||
$levels = [
|
||||
ProcessProfile::TENANT => $this->tenantDefaults($property->tenant_id),
|
||||
ProcessProfile::PROPERTY => $propertyLevel,
|
||||
ProcessProfile::CHANNEL => $intake === IntakeKind::Internal || $channel === null ? [] : (array) (($channel->process_overrides ?? [])[$intake->value] ?? []),
|
||||
];
|
||||
|
||||
$values = [];
|
||||
$sources = [];
|
||||
foreach (ProcessSetting::cases() as $setting) {
|
||||
$values[$setting->value] = $setting->default();
|
||||
$sources[$setting->value] = ProcessProfile::SYSTEM;
|
||||
|
||||
foreach ($levels as $level => $settings) {
|
||||
if ($level === ProcessProfile::CHANNEL && ! in_array($intake, $setting->channelIntakes(), true)) {
|
||||
continue;
|
||||
}
|
||||
$value = $setting->normalize($settings[$setting->value] ?? null);
|
||||
if ($value !== null) {
|
||||
$values[$setting->value] = $value;
|
||||
$sources[$setting->value] = $level;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($intake !== IntakeKind::Booking) {
|
||||
// A request is answered with our own offer and an internal case is ours anyway: the documents are ours.
|
||||
$values[ProcessSetting::Documents->value] = Responsibility::Landlord->value;
|
||||
$sources[ProcessSetting::Documents->value] = ProcessProfile::SYSTEM;
|
||||
}
|
||||
|
||||
$documents = Responsibility::from((string) $values[ProcessSetting::Documents->value]);
|
||||
$payments = Responsibility::from((string) $values[ProcessSetting::Payments->value]);
|
||||
|
||||
return new ProcessProfile($intake, ProcessProfile::caseTypeFor($intake, $channel !== null, $documents, $payments), $values, $sources);
|
||||
}
|
||||
|
||||
/**
|
||||
* The settings that would apply to a case right now (its frozen profile is what counts for the case itself).
|
||||
*/
|
||||
public function forBooking(Booking $booking): ProcessProfile
|
||||
{
|
||||
// The case's own landlord counts, whichever landlord is current (e.g. a case written by the system).
|
||||
$property = $booking->property ?? Property::withoutGlobalScope('tenant')->findOrFail($booking->property_id);
|
||||
|
||||
return $this->resolve($property, $booking->processChannel(), $booking->intake ?? self::intakeFor($booking));
|
||||
}
|
||||
|
||||
/**
|
||||
* Landlord defaults as stored on the tenant (not set: empty).
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function tenantDefaults(?int $tenantId): array
|
||||
{
|
||||
if ($tenantId === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return (array) (Tenant::query()->find($tenantId)?->process_defaults ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Intake kind of a case that does not name one: a case from a channel that only passes on requests is a request,
|
||||
* from other channels a booking that is already binding; everything else (website, phone, e-mail) a request.
|
||||
*/
|
||||
public static function intakeFor(Booking $booking): IntakeKind
|
||||
{
|
||||
$channel = $booking->processChannel();
|
||||
if ($channel === null) {
|
||||
return IntakeKind::Inquiry;
|
||||
}
|
||||
|
||||
return $channel->booking_model === ChannelBookingModel::Inquiry ? IntakeKind::Inquiry : IntakeKind::Booking;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Process;
|
||||
|
||||
use App\Enums\ActivityState;
|
||||
use App\Enums\BookingStatus;
|
||||
use App\Enums\ChannelTaskAction;
|
||||
use App\Filament\Resources\Bookings\BookingResource;
|
||||
use App\Filament\Resources\ChannelTasks\ChannelTaskResource;
|
||||
use App\Filament\Resources\InboxMessages\InboxMessageResource;
|
||||
use App\Filament\Resources\PortalOccupations\PortalOccupationResource;
|
||||
use App\Models\BlockedPeriod;
|
||||
use App\Models\Booking;
|
||||
use App\Models\ChannelTask;
|
||||
use App\Models\InboxMessage;
|
||||
|
||||
/**
|
||||
* "Zu erledigen": everything the landlord has to do, in one list – the next step of every open case, portals to block
|
||||
* or release, calendar entries nobody knows yet and system notices that need a decision. Each entry names the thing in
|
||||
* plain words and leads to the one place where it is done.
|
||||
*/
|
||||
class WorkList
|
||||
{
|
||||
/**
|
||||
* @return list<array{title: string, detail: ?string, url: string, button: string, tone: string, sort: string}>
|
||||
*/
|
||||
public static function items(): array
|
||||
{
|
||||
$items = [];
|
||||
|
||||
$cases = Booking::query()
|
||||
->with(['guest', 'property', 'payments', 'importChannel', 'originChannel'])
|
||||
->whereNull('archived_at')
|
||||
->whereNotIn('status', [BookingStatus::Declined, BookingStatus::Expired, BookingStatus::Completed])
|
||||
->where(fn ($query) => $query->whereNot('status', BookingStatus::Cancelled)->orWhereHas('tasks', fn ($tasks) => $tasks->where('status', 'open')))
|
||||
->get();
|
||||
foreach ($cases as $case) {
|
||||
$next = CaseActivities::next($case);
|
||||
if ($next === null) {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
'title' => $next->label,
|
||||
'detail' => $case->guestName().' · '.$case->arrival->format('d.m.').'–'.$case->departure->format('d.m.Y').' · '.$case->property->name.($next->detail ? ' · '.$next->detail : ''),
|
||||
'url' => BookingResource::getUrl('view', ['record' => $case]),
|
||||
'button' => 'Vorgang öffnen',
|
||||
'tone' => $next->state === ActivityState::Problem ? 'danger' : 'warning',
|
||||
'sort' => ($next->state === ActivityState::Problem ? '0' : '1').$case->arrival->format('Ymd'),
|
||||
];
|
||||
}
|
||||
|
||||
foreach (ChannelTask::query()->open()->with('channel.property')->orderBy('starts_on')->get() as $task) {
|
||||
$items[] = [
|
||||
'title' => ($task->action === ChannelTaskAction::Block ? 'In '.$task->channel?->name.' sperren' : 'In '.$task->channel?->name.' wieder freigeben'),
|
||||
'detail' => $task->period().' · '.$task->channel?->property?->name.' · '.$task->reason,
|
||||
'url' => ChannelTaskResource::getUrl(),
|
||||
'button' => 'Erledigen',
|
||||
'tone' => 'danger',
|
||||
'sort' => '0'.$task->starts_on->format('Ymd'),
|
||||
];
|
||||
}
|
||||
|
||||
foreach (BlockedPeriod::query()->unclear()->with(['channel', 'property'])->whereDate('ends_on', '>=', today())->orderBy('starts_on')->get() as $occupation) {
|
||||
$items[] = [
|
||||
'title' => 'Unbekannte Belegung von '.($occupation->channel?->name ?? 'einem Portal').' – was ist das?',
|
||||
'detail' => $occupation->starts_on->format('d.m.').'–'.$occupation->ends_on->format('d.m.Y').' · '.$occupation->property?->name,
|
||||
'url' => PortalOccupationResource::getUrl('view', ['record' => $occupation]),
|
||||
'button' => 'Klären',
|
||||
'tone' => 'warning',
|
||||
'sort' => '1'.$occupation->starts_on->format('Ymd'),
|
||||
];
|
||||
}
|
||||
|
||||
foreach (InboxMessage::query()->whereNull('read_at')->latest()->get() as $message) {
|
||||
$items[] = [
|
||||
'title' => $message->subject,
|
||||
'detail' => str($message->body)->limit(160)->toString(),
|
||||
'url' => $message->action_url ?: InboxMessageResource::getUrl(),
|
||||
'button' => 'Ansehen',
|
||||
'tone' => 'info',
|
||||
'sort' => '2'.$message->created_at?->format('YmdHis'),
|
||||
];
|
||||
}
|
||||
|
||||
usort($items, fn (array $a, array $b): int => strcmp($a['sort'], $b['sort']));
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user