- 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>
143 lines
6.4 KiB
PHP
143 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Process;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\BookingTaskStatus;
|
||
use App\Enums\Responsibility;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Pricing\CaseMoney;
|
||
use App\Support\Money;
|
||
use Illuminate\Support\Collection;
|
||
|
||
/**
|
||
* The two closings of a case (package 9, operator decision): **stay closed** – the activities the stay needs are done or
|
||
* handed over (status "Abgeschlossen") – and **commercially settled** – claims, payments, refunds and portal payouts are
|
||
* clear (`settled_at`). They are separate statements: a stay can be closed while the payout of the portal is still
|
||
* outstanding, and open money never disappears because the stay is closed.
|
||
*
|
||
* Nothing here is a status of its own: what keeps a case open is derived from its tasks and its money.
|
||
*/
|
||
final class CaseClosure
|
||
{
|
||
public const AUTOMATIC = 'automatisch';
|
||
|
||
/**
|
||
* Open tasks of the stay itself (services, issues, a portal change to check) that keep the stay from being closed.
|
||
*
|
||
* @return Collection<int, BookingTask>
|
||
*/
|
||
public static function stayBlockers(Booking $booking): Collection
|
||
{
|
||
return BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get()
|
||
->filter(fn (BookingTask $task): bool => $task->kind->blocksStay())->values();
|
||
}
|
||
|
||
/**
|
||
* What is still open commercially, in plain words (empty: nothing open). Only cases whose stay is closed or that were
|
||
* cancelled have a commercial closing; for all others the money is simply still running.
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public static function commercialPoints(Booking $booking): array
|
||
{
|
||
$points = [];
|
||
$tasks = BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get()
|
||
->filter(fn (BookingTask $task): bool => $task->kind->isCommercial());
|
||
|
||
if ($booking->status !== BookingStatus::Cancelled) {
|
||
$money = CaseMoney::of($booking);
|
||
$points = match ($money->collector()) {
|
||
Responsibility::Landlord => self::ourMoney($booking, $money),
|
||
Responsibility::Portal => self::portalMoney($booking, $money),
|
||
default => $booking->total !== null && $booking->total > 0 && $booking->paidAmount() < $booking->total
|
||
? ['Vereinbarte Zahlung noch offen: '.Money::format($booking->total - $booking->paidAmount()).' (ohne Erinnerungen).']
|
||
: [],
|
||
};
|
||
}
|
||
|
||
foreach ($tasks as $task) {
|
||
$points[] = 'Aufgabe offen: '.$task->label().'.';
|
||
}
|
||
|
||
return array_values(array_unique($points));
|
||
}
|
||
|
||
/**
|
||
* Closes or reopens the commercial side after anything that may have changed it (payment, task, stay closed).
|
||
* A settlement the landlord made by hand stays unless a commercial task is opened again.
|
||
*/
|
||
public static function refresh(Booking $booking): void
|
||
{
|
||
if (! in_array($booking->status, [BookingStatus::Completed, BookingStatus::Cancelled], true)) {
|
||
return;
|
||
}
|
||
|
||
$booking->unsetRelation('payments');
|
||
$points = self::commercialPoints($booking);
|
||
$openTasks = BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get()
|
||
->contains(fn (BookingTask $task): bool => $task->kind->isCommercial());
|
||
|
||
if ($booking->settled_at === null && $points === []) {
|
||
$booking->forceFill(['settled_at' => now(), 'settled_by' => self::AUTOMATIC, 'settled_note' => null])->save();
|
||
CaseLog::record($booking, 'settled', 'Kaufmännisch abgeschlossen – nichts mehr offen');
|
||
} elseif ($booking->settled_at !== null && ($openTasks || ($booking->settled_by === self::AUTOMATIC && $points !== []))) {
|
||
$booking->forceFill(['settled_at' => null, 'settled_by' => null, 'settled_note' => null])->save();
|
||
CaseLog::record($booking, 'settled', 'Kaufmännischer Abschluss wieder geöffnet: '.implode(' ', $points));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The landlord settles the case although points are open (e.g. a small payout difference accepted); open commercial
|
||
* tasks are closed with the same note. The reason is required and kept.
|
||
*/
|
||
public static function settle(Booking $booking, string $note, string $by): void
|
||
{
|
||
foreach (BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get() as $task) {
|
||
if ($task->kind->isCommercial()) {
|
||
$task->complete($by, 'Mit dem kaufmännischen Abschluss geklärt: '.$note);
|
||
}
|
||
}
|
||
$points = self::commercialPoints($booking);
|
||
$booking->forceFill(['settled_at' => now(), 'settled_by' => $by, 'settled_note' => mb_substr($note, 0, 500)])->save();
|
||
CaseLog::record($booking, 'settled', 'Kaufmännisch abgeschlossen (von Hand)', reason: $note, data: ['open_points' => $points], actor: $by);
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
private static function ourMoney(Booking $booking, CaseMoney $money): array
|
||
{
|
||
if ($booking->total === null) {
|
||
return ['Der Preis ist nicht bekannt.'];
|
||
}
|
||
|
||
$paid = $booking->paidAmount();
|
||
|
||
return array_values(array_filter([
|
||
$money->openForUs() > 0 ? 'Noch offen vom Gast: '.Money::format($money->openForUs()).'.' : null,
|
||
$paid > $booking->total ? 'Überzahlung von '.Money::format($paid - $booking->total).' – Erstattung klären.' : null,
|
||
]));
|
||
}
|
||
|
||
/**
|
||
* @return list<string>
|
||
*/
|
||
private static function portalMoney(Booking $booking, CaseMoney $money): array
|
||
{
|
||
$portal = $booking->processChannel()?->name ?? 'dem Portal';
|
||
if ($booking->total === null) {
|
||
return ['Der Gesamtpreis laut '.$portal.' fehlt.'];
|
||
}
|
||
|
||
$payout = $money->payout();
|
||
|
||
return array_values(array_filter([
|
||
$payout['received'] === 0 ? 'Die Auszahlung von '.$portal.' ist noch nicht eingegangen.' : null,
|
||
$payout['received'] > 0 && ($payout['outstanding'] ?? 0) > 0 ? 'Von der angekündigten Auszahlung fehlen noch '.Money::format((int) $payout['outstanding']).'.' : null,
|
||
$payout['difference'] > 0 ? 'Die Auszahlungsangaben weichen um '.Money::format($payout['difference']).' voneinander ab – ungeklärt.' : null,
|
||
]));
|
||
}
|
||
}
|