Files
fewo-stommer/app/Actions/CloseStay.php
T
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

127 lines
5.6 KiB
PHP
Raw 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\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++;
}
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.',
},
]);
}
}
}
}