- Wirksame Einstellungen: Antwort auf Anfragen, Aufenthalt je Wohnung, Dokument und beide Abschlüsse je Vorgang - Schritte vor dem Aufenthalt entfallen nach der Abreise; direkt bestaetigte Vorgaenge zeigen keine Auskunft; "Als Naechstes" ohne fremden Detailtext - Automatische Wunschbestaetigung nur mit freigegebenen Vertragstexten - Taeglicher Lauf bewertet auch bereits abgeschlossene Vorgaenge kaufmaennisch - Dokumente: UMSETZUNGSERGEBNIS_P7_P9, END_TO_END_ABNAHME (T01-T25), KONFIGURATION_UND_WIRKSAMKEIT, GESAMTABNAHME_v0.10, STATUS; Screenshots Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
6.2 KiB
PHP
134 lines
6.2 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Enums\BookingTaskStatus;
|
||
use App\Enums\Responsibility;
|
||
use App\Exceptions\ReservationException;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Pricing\CaseMoney;
|
||
use App\Process\CaseClosure;
|
||
use App\Process\CaseLog;
|
||
use App\Support\Money;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Package 9, P05 (gate B4 as decided by the operator): closes the stay after the departure – automatically once no task
|
||
* of the stay is open any more, or by the landlord, who marks the remaining stay tasks as done or handed over. Open
|
||
* money does not hold the stay up: it becomes a follow-up on the same case (claim against the guest, payout of the
|
||
* portal) and keeps the commercial closing open until it is clear.
|
||
*/
|
||
class CloseStay
|
||
{
|
||
/**
|
||
* Daily: closes every stay whose departure has passed and nothing of the stay is open any more.
|
||
*
|
||
* @return int the number of stays closed now
|
||
*/
|
||
public function due(): int
|
||
{
|
||
$closed = 0;
|
||
$stays = Booking::query()
|
||
->whereIn('status', [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid])
|
||
->whereDate('departure', '<', CarbonImmutable::today())
|
||
->with(['guest', 'property', 'payments', 'importChannel', 'originChannel'])
|
||
->get();
|
||
|
||
foreach ($stays as $booking) {
|
||
// After the departure the arrival details are of no use any more.
|
||
foreach (BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ArrivalDetails)->where('status', BookingTaskStatus::Open)->get() as $task) {
|
||
$task->update(['status' => BookingTaskStatus::Dismissed, 'note' => 'Nach der Abreise nicht mehr nötig.', 'completed_by' => CaseLog::SYSTEM, 'completed_at' => now()]);
|
||
}
|
||
|
||
if (CaseClosure::stayBlockers($booking)->isNotEmpty()) {
|
||
continue;
|
||
}
|
||
|
||
CaseLog::context(fn () => $this->close($booking), actor: CaseLog::SYSTEM.' (automatisch)');
|
||
$closed++;
|
||
}
|
||
|
||
// Cases closed before package 9 (or whose money became clear without a payment) get their commercial closing too.
|
||
$unsettled = Booking::query()->whereIn('status', [BookingStatus::Completed, BookingStatus::Cancelled])->whereNull('settled_at')
|
||
->whereDate('departure', '>', CarbonImmutable::today()->subYear())->with(['payments', 'importChannel', 'originChannel'])->get();
|
||
foreach ($unsettled as $booking) {
|
||
CaseLog::context(fn () => CaseClosure::refresh($booking), actor: CaseLog::SYSTEM.' (automatisch)');
|
||
}
|
||
|
||
return $closed;
|
||
}
|
||
|
||
/**
|
||
* The landlord closes the stay; open tasks of the stay are marked as done or handed over with the note.
|
||
*
|
||
* @throws ReservationException
|
||
*/
|
||
public function byHand(Booking $booking, string $by, ?string $note = null): void
|
||
{
|
||
if (! in_array($booking->status, [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid], true)) {
|
||
throw ReservationException::invalidTransition($booking->status->getLabel(), 'abgeschlossen');
|
||
}
|
||
if ($booking->departure->isAfter(CarbonImmutable::today())) {
|
||
throw new ReservationException('Der Aufenthalt endet erst am '.$booking->departure->format('d.m.Y').'.');
|
||
}
|
||
|
||
CaseLog::context(function () use ($booking, $by, $note): void {
|
||
foreach (CaseClosure::stayBlockers($booking) as $task) {
|
||
$task->complete($by, filled($note) ? $note : 'Mit dem Abschluss des Aufenthalts erledigt bzw. übergeben.');
|
||
}
|
||
BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ArrivalDetails)->where('status', BookingTaskStatus::Open)
|
||
->update(['status' => BookingTaskStatus::Dismissed, 'completed_by' => $by, 'completed_at' => now()]);
|
||
$this->close($booking);
|
||
}, actor: $by, reason: $note);
|
||
}
|
||
|
||
private function close(Booking $booking): void
|
||
{
|
||
DB::transaction(function () use ($booking): void {
|
||
$booking->lockForTransition();
|
||
if (! in_array($booking->status, [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid], true)) {
|
||
return;
|
||
}
|
||
$booking->update(['status' => BookingStatus::Completed, 'completed_at' => now()]);
|
||
$this->followUps($booking);
|
||
});
|
||
|
||
CaseClosure::refresh($booking->fresh());
|
||
}
|
||
|
||
/**
|
||
* Open money becomes a follow-up of the closed stay, so it stays visible and assigned.
|
||
*/
|
||
private function followUps(Booking $booking): void
|
||
{
|
||
$booking->unsetRelation('payments');
|
||
$money = CaseMoney::of($booking);
|
||
|
||
if ($money->collector() === Responsibility::Landlord && $money->openForUs() > 0) {
|
||
$chasing = BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get()->contains(fn (BookingTask $task): bool => $task->kind->isPayment());
|
||
if (! $chasing) {
|
||
BookingTask::openOnce($booking, BookingTaskKind::OpenClaim, '', ['due_on' => today(), 'note' => 'Noch offen vom Gast: '.Money::format($money->openForUs()).'.']);
|
||
}
|
||
}
|
||
|
||
if ($money->collector() === Responsibility::Portal) {
|
||
$payout = $money->payout();
|
||
if ($payout['received'] === 0 || ($payout['outstanding'] ?? 0) > 0) {
|
||
$portal = $booking->processChannel()?->name ?? 'des Portals';
|
||
BookingTask::openOnce($booking, BookingTaskKind::PayoutCheck, '', [
|
||
'due_on' => $booking->departure->addDays(14),
|
||
'responsible' => $booking->processChannel()?->name,
|
||
'note' => match (true) {
|
||
$payout['values'] !== [] => 'Angekündigt: '.implode(' bzw. ', array_map(fn (int $value): string => Money::format($value), $payout['values'])).'; erhalten: '.Money::format($payout['received']).'.',
|
||
default => 'Auszahlung von '.$portal.' noch nicht erfasst.',
|
||
},
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
}
|