Files
fewo-stommer/app/Actions/CancelBookingByGuest.php
LarsandClaude Sonnet 5.5 0c4187edaf
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s
Stornoregeln mit automatischer Kostenberechnung, Storno-Verwaltung, Vertragsanlagen
- Stornoregel (Stufen nach Tagen vor Anreise, Mietpreis oder Gesamtpreis, Ersatzmieter-Gebuehr), je Vertragsvorlage,
  beim Angebot eingefroren, als Platzhalter im Vertrag
- Stornierung (Gast-Link oder Verwaltung) berechnet die Kosten, legt einen Stornofall an; Verwaltung Stornierungen
  mit Vorschlag, Festlegung, Saldo, Abrechnung und Erlass; Gast sieht die Kosten vorab auf der Storno-Seite
- Vertragsanlagen als eigene versionierte Dokumente mit Bedingung (immer, Zusatzleistung, Portal, von Hand),
  je Angebot ausgewaehlt und eingefroren; Zusatzleistungen in der Buchung erfassbar
- FeWo-direkt-Installation legt die Stornoregel aus Paragraf 13 an

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 16:32:48 +01:00

103 lines
4.1 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\Actions;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Enums\CancellationMode;
use App\Exceptions\ReservationException;
use App\Mail\BookingCancelled;
use App\Mail\CancellationRequested;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Support\CancellationCost;
use App\Support\Settings;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;
/**
* The guest uses the cancellation link of the booking confirmation. Depending on the property's setting the
* booking is cancelled at once (dates released) or only a request task is created for the landlord – costs
* are never settled automatically. Repeating the click changes nothing.
*/
class CancelBookingByGuest
{
/**
* What a click on the link does for this booking today.
*/
public function modeFor(Booking $booking): CancellationMode
{
$property = $booking->property;
$freeUntil = $booking->arrival->subDays($property->free_cancellation_days);
return match ($property->cancellation_mode) {
CancellationMode::Instant => CancellationMode::Instant,
CancellationMode::InstantWhenFree => ($cost = CancellationCost::calculate($booking)) !== null
? ($cost['free'] ? CancellationMode::Instant : CancellationMode::Request)
: (today()->lessThanOrEqualTo($freeUntil) ? CancellationMode::Instant : CancellationMode::Request),
default => CancellationMode::Request,
};
}
/**
* @return CancellationMode|null what happened (Instant = cancelled, Request = request recorded); null when it had happened before
*
* @throws ReservationException
*/
public function handle(Booking $booking, ?string $message = null): ?CancellationMode
{
$outcome = DB::transaction(function () use ($booking, $message): ?CancellationMode {
$booking->lockForTransition();
$alreadyAsked = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::CancellationRequest)->exists();
if ($booking->status === BookingStatus::Cancelled || $alreadyAsked) {
return null;
}
if (! in_array($booking->status, BookingStatus::occupying(), true) || $booking->arrival->isPast()) {
throw ReservationException::invalidTransition($booking->status->getLabel(), 'storniert');
}
$mode = $this->modeFor($booking);
if ($mode === CancellationMode::Instant) {
$booking->update(['status' => BookingStatus::Cancelled, 'cancelled_at' => now()]);
app(RecordCancellation::class)->handle($booking, 'guest');
}
BookingTask::query()->create([
'booking_id' => $booking->id,
'kind' => BookingTaskKind::CancellationRequest,
'status' => BookingTaskStatus::Open,
'note' => trim(($mode === CancellationMode::Instant ? 'Vom Gast storniert – bitte Stornokosten und Rückzahlung prüfen. ' : '').($message ?? '')) ?: null,
]);
return $mode;
});
if ($outcome !== null) {
$this->notify($booking, $outcome);
}
return $outcome;
}
private function notify(Booking $booking, CancellationMode $mode): void
{
$booking->loadMissing(['guest', 'property']);
$cancelled = $mode === CancellationMode::Instant;
$sends = [
fn () => Mail::to(Settings::notificationRecipients())->send(new CancellationRequested($booking, $cancelled)),
fn () => $booking->guest?->email ? Mail::to($booking->guest->email, $booking->guest->full_name)->send(new BookingCancelled($booking, $cancelled)) : null,
];
foreach ($sends as $send) {
try {
$send();
} catch (Throwable $exception) {
Log::warning('Stornomitteilung nicht zugestellt', ['booking' => $booking->id, 'exception' => $exception::class]);
}
}
}
}