Files
fewo-stommer/app/Actions/SendBookingReminders.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

117 lines
5.3 KiB
PHP

<?php
namespace App\Actions;
use App\Enums\BookingStatus;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Enums\Responsibility;
use App\Mail\ArrivalInformation;
use App\Models\Booking;
use App\Models\BookingTask;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
/**
* Daily booking workflow. Payments are never chased automatically: due payments become tasks for the landlord
* (BookingTask, once per booking and kind) who checks the payment and sends the reminder on a click
* ({@see SendPaymentReminder}). Only the arrival information goes out by itself, once per booking, the configured
* number of days before arrival. Completion after departure.
*/
class SendBookingReminders
{
private const BALANCE_REQUEST_DAYS_BEFORE = 7;
/** Upper bound of the per-property setting; the property decides the actual day. */
private const ARRIVAL_INFO_MAX_DAYS_BEFORE = 14;
/**
* @return array{deposit_overdue: int, balance_request: int, balance_overdue: int, arrival_info: int, completed: int}
*/
public function handle(): array
{
$today = today();
$unpaid = [BookingStatus::Confirmed, BookingStatus::DepositPaid];
$depositOverdue = $this->bookings(fn (Builder $query) => $query
->where('status', BookingStatus::Confirmed)
->whereNull('deposit_reminded_at')
->whereDate('deposit_due_on', '<', $today))
->filter(fn (Booking $booking): bool => $booking->openDeposit() > 0);
$depositTasks = $this->createTasks($depositOverdue, BookingTaskKind::DepositReminder, fn (Booking $booking) => $booking->deposit_due_on);
$balanceRequest = $this->bookings(fn (Builder $query) => $query
->whereIn('status', $unpaid)
->whereNull('balance_requested_at')
->whereDate('balance_due_on', '>=', $today)
->whereDate('balance_due_on', '<=', $today->copy()->addDays(self::BALANCE_REQUEST_DAYS_BEFORE)))
->filter(fn (Booking $booking): bool => ! $booking->requiresFullPayment() && $booking->openAmount() > 0 && $booking->status === BookingStatus::DepositPaid);
$requestTasks = $this->createTasks($balanceRequest, BookingTaskKind::BalanceRequest, fn (Booking $booking) => $booking->balance_due_on);
$balanceOverdue = $this->bookings(fn (Builder $query) => $query
->whereIn('status', $unpaid)
->whereNull('balance_reminded_at')
->whereDate('balance_due_on', '<', $today))
->filter(fn (Booking $booking): bool => $booking->openAmount() > 0 && ($booking->status === BookingStatus::DepositPaid || $booking->requiresFullPayment()));
$overdueTasks = $this->createTasks($balanceOverdue, BookingTaskKind::BalanceReminder, fn (Booking $booking) => $booking->balance_due_on);
$arrivals = $this->bookings(fn (Builder $query) => $query
->whereIn('status', BookingStatus::occupying())
->whereNull('arrival_info_sent_at')
->whereDate('arrival', '>=', $today)
->whereDate('arrival', '<=', $today->copy()->addDays(self::ARRIVAL_INFO_MAX_DAYS_BEFORE)))
->filter(fn (Booking $booking): bool => $booking->process()->guestMessages() === Responsibility::Landlord
&& $booking->arrival->lessThanOrEqualTo($today->copy()->addDays($booking->process()->arrivalInfoDaysBefore())));
foreach ($arrivals as $booking) {
Mail::to($booking->guest->email, $booking->guest->full_name)->send(new ArrivalInformation($booking));
$booking->update(['arrival_info_sent_at' => now()]);
}
$completed = Booking::query()
->where('status', BookingStatus::Paid)
->whereDate('departure', '<', $today)
->get();
$completed->each->update(['status' => BookingStatus::Completed]);
return [
'deposit_overdue' => $depositTasks,
'balance_request' => $requestTasks,
'balance_overdue' => $overdueTasks,
'arrival_info' => $arrivals->count(),
'completed' => $completed->count(),
];
}
/**
* @param callable(Builder<Booking>): Builder<Booking> $constraints
* @return Collection<int, Booking>
*/
private function bookings(callable $constraints): Collection
{
return $constraints(Booking::query()->with(['guest', 'property', 'payments', 'importChannel', 'originChannel']))
->whereHas('guest', fn (Builder $query) => $query->whereNull('anonymized_at')->whereNotNull('email')->where('email', '!=', ''))
->get()
->toBase();
}
/**
* @param Collection<int, Booking> $bookings
* @param callable(Booking): mixed $dueOn
* @return int the number of tasks created now
*/
private function createTasks(Collection $bookings, BookingTaskKind $kind, callable $dueOn): int
{
$created = 0;
foreach ($bookings->filter(fn (Booking $booking): bool => $booking->wantsPaymentTasks()) as $booking) {
$task = BookingTask::query()->firstOrCreate(
['booking_id' => $booking->id, 'kind' => $kind],
['status' => BookingTaskStatus::Open, 'due_on' => $dueOn($booking)],
);
$created += $task->wasRecentlyCreated ? 1 : 0;
}
return $created;
}
}