Files
fewo-stommer/app/Actions/SetBookingStatus.php
LarsandClaude Opus 5.5 3a728c3255 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>
2026-11-01 16:25:46 +01:00

83 lines
4.0 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\Availability\AvailabilityChecker;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Exceptions\ReservationException;
use App\Models\Booking;
use App\Models\BookingTask;
use App\Process\CaseLog;
use Illuminate\Support\Facades\DB;
/**
* Manual status change for processes the system does not drive itself (bookings entered by hand, portal bookings):
* the landlord moves the booking along the stages and gives a reason, which is added to the internal notes.
* Cancelling and declining are separate actions (they inform people and record costs). Making a booking binding
* again checks the calendar like every other writer; no mail is sent and no contract is generated here.
*/
class SetBookingStatus
{
/** @var list<BookingStatus> */
public const TARGETS = [BookingStatus::Reserved, BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid, BookingStatus::Completed];
public function __construct(private AvailabilityChecker $availability) {}
/**
* @throws ReservationException
*/
public function handle(Booking $booking, BookingStatus $status, string $reason, string $by = 'Verwaltung'): Booking
{
if (! in_array($status, self::TARGETS, true)) {
throw new ReservationException('Auf diesen Status kann nicht von Hand gewechselt werden. Stornieren und Ablehnen haben eigene Aktionen.');
}
if ($status === BookingStatus::Reserved && in_array($booking->status, BookingStatus::occupying(), true)) {
throw new ReservationException('Eine verbindliche Buchung kann nicht wieder zur Anfrage gemacht werden. Soll sie enden, bitte „Beenden“ wählen – dann wird sie ordentlich storniert.');
}
if (blank(trim($reason))) {
throw new ReservationException('Bitte einen Grund für die Statusänderung angeben.');
}
DB::transaction(function () use ($booking, $status, $reason, $by): void {
$booking->lockForTransition();
$from = $booking->status;
if ($from === $status) {
throw new ReservationException('Die Buchung hat diesen Status bereits.');
}
if ($status === BookingStatus::Reserved && in_array($from, BookingStatus::occupying(), true)) {
throw new ReservationException('Eine verbindliche Buchung kann nicht wieder zur Anfrage gemacht werden.');
}
$becomesBinding = in_array($status, BookingStatus::occupying(), true) && ! in_array($from, BookingStatus::occupying(), true);
if ($becomesBinding && ! $this->availability->isAvailable($booking->property, $booking->arrival, $booking->departure, ignore: $booking)) {
throw ReservationException::notAvailable();
}
$changes = ['status' => $status];
if ($becomesBinding) {
$changes += ['hold_expires_at' => null, 'confirmed_at' => $booking->confirmed_at ?? now(), 'cancelled_at' => null];
}
if ($status === BookingStatus::Reserved) {
$changes['hold_expires_at'] = null;
}
$line = now()->format('d.m.Y H:i').' '.$by.': Status von „'.$from->getLabel().'“ auf „'.$status->getLabel().'“ gesetzt – '.trim($reason);
CaseLog::context(fn () => $booking->update($changes + ['internal_notes' => trim(($booking->internal_notes ? $booking->internal_notes."\n" : '').$line)]), actor: $by, reason: 'Von Hand gesetzt: '.trim($reason));
// A booking taken back into the process no longer needs the cancellation follow-up.
if ($becomesBinding) {
BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)
->whereIn('kind', [BookingTaskKind::CancellationFee, BookingTaskKind::CancellationRequest])
->update(['status' => BookingTaskStatus::Done, 'completed_by' => $by, 'completed_at' => now()]);
}
});
return $booking->refresh();
}
}