- 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>
152 lines
6.2 KiB
PHP
152 lines
6.2 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\BookingStatus;
|
||
use App\Enums\BookingTaskKind;
|
||
use App\Enums\BookingTaskStatus;
|
||
use App\Enums\InternalKind;
|
||
use App\Models\Booking;
|
||
use App\Models\BookingTask;
|
||
use App\Models\Extra;
|
||
use App\Process\CaseLog;
|
||
use Carbon\CarbonImmutable;
|
||
|
||
/**
|
||
* Package 9, P04: prepares the stays of the coming two weeks – once per case and task, however often it runs.
|
||
*
|
||
* - **Arrival details:** a few days before the arrival information is due, a task asks for what is missing (a way to
|
||
* reach the guest; the arrival time where the property asks for it). It closes itself once the data is there. For a
|
||
* portal booking the contact may have to be fetched from the portal.
|
||
* - **Stay tasks of the property:** cleaning, linen, key handover … as configured on the property, with their due date.
|
||
* - **Booked services:** every extra of our own price (or of a private stay) becomes a task to provide it.
|
||
*
|
||
* Nothing is sent to guests here; the arrival information keeps going out by itself where we are responsible.
|
||
*/
|
||
class PrepareStays
|
||
{
|
||
/** How far ahead stays are prepared. */
|
||
public const LEAD_DAYS = 14;
|
||
|
||
/** Days before the arrival information that the arrival details are checked. */
|
||
private const DETAILS_BEFORE_INFO = 2;
|
||
|
||
/**
|
||
* @return array{arrival_details: int, stay_tasks: int}
|
||
*/
|
||
public function handle(): array
|
||
{
|
||
$today = CarbonImmutable::today();
|
||
$created = ['arrival_details' => 0, 'stay_tasks' => 0];
|
||
|
||
$stays = Booking::query()
|
||
->whereIn('status', [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid])
|
||
->whereDate('arrival', '<=', $today->addDays(self::LEAD_DAYS))
|
||
->whereDate('departure', '>=', $today)
|
||
->with(['guest', 'property', 'importChannel', 'originChannel'])
|
||
->get();
|
||
|
||
foreach ($stays as $booking) {
|
||
$created['arrival_details'] += $this->arrivalDetails($booking, $today) ? 1 : 0;
|
||
$created['stay_tasks'] += $this->stayTasks($booking);
|
||
}
|
||
|
||
return $created;
|
||
}
|
||
|
||
/**
|
||
* What is still missing for the arrival, in plain words (empty: nothing).
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public static function missingArrivalDetails(Booking $booking): array
|
||
{
|
||
if ($booking->isInternal()) {
|
||
return [];
|
||
}
|
||
|
||
return array_values(array_filter([
|
||
$booking->guest === null ? 'Name und Kontakt des Gastes' : null,
|
||
$booking->guest !== null && blank($booking->guest->email) && blank($booking->guest->phone) ? 'eine E-Mail-Adresse oder Telefonnummer des Gastes' : null,
|
||
$booking->property?->asks_arrival_time && blank($booking->arrival_time) ? 'die Anreisezeit' : null,
|
||
]));
|
||
}
|
||
|
||
/**
|
||
* Opens (or closes) the task for missing arrival details.
|
||
*/
|
||
private function arrivalDetails(Booking $booking, CarbonImmutable $today): bool
|
||
{
|
||
$missing = self::missingArrivalDetails($booking);
|
||
$open = BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ArrivalDetails)->where('status', BookingTaskStatus::Open)->first();
|
||
|
||
if ($missing === []) {
|
||
$open?->complete(CaseLog::SYSTEM, 'Die Angaben liegen jetzt vor.');
|
||
|
||
return false;
|
||
}
|
||
|
||
$checkFrom = $booking->arrival->subDays($booking->process()->arrivalInfoDaysBefore() + self::DETAILS_BEFORE_INFO);
|
||
if ($today->lessThan($checkFrom)) {
|
||
return false;
|
||
}
|
||
|
||
$portal = $booking->caseType()->isExternallyBinding() ? ' (ggf. im Portal '.($booking->processChannel()?->name ?? '').' nachsehen)' : '';
|
||
$task = BookingTask::openOnce($booking, BookingTaskKind::ArrivalDetails, '', [
|
||
'due_on' => $booking->arrival->subDays($booking->process()->arrivalInfoDaysBefore()),
|
||
'note' => 'Es fehlt: '.implode(', ', $missing).$portal.'.',
|
||
]);
|
||
|
||
return $task->wasRecentlyCreated;
|
||
}
|
||
|
||
/**
|
||
* Opens the configured stay tasks and the tasks for booked services; returns how many were opened now.
|
||
*/
|
||
private function stayTasks(Booking $booking): int
|
||
{
|
||
$kind = match (true) {
|
||
! $booking->isInternal() => 'rental',
|
||
$booking->internal_kind === InternalKind::PrivateStay => 'private',
|
||
$booking->internal_kind === InternalKind::OwnUse => 'own',
|
||
default => 'block',
|
||
};
|
||
$opened = 0;
|
||
|
||
foreach ($booking->property?->stayTaskDefinitions() ?? [] as $definition) {
|
||
if (! in_array($kind, $definition['applies_to'], true)) {
|
||
continue;
|
||
}
|
||
$due = match ($definition['when']) {
|
||
'after_departure' => $booking->departure,
|
||
'on_arrival' => $booking->arrival,
|
||
default => $booking->arrival->subDays($definition['days']),
|
||
};
|
||
$task = BookingTask::openOnce($booking, BookingTaskKind::Service, $definition['key'], [
|
||
'title' => $definition['title'],
|
||
'responsible' => $definition['responsible'],
|
||
'due_on' => $due,
|
||
]);
|
||
// Dates of the stay may have changed since the task was opened.
|
||
if (! $task->wasRecentlyCreated && $task->status === BookingTaskStatus::Open && ! $task->due_on?->equalTo($due)) {
|
||
$task->update(['due_on' => $due]);
|
||
}
|
||
$opened += $task->wasRecentlyCreated ? 1 : 0;
|
||
}
|
||
|
||
// Booked services of our own price (a portal's extras are not known item by item).
|
||
if (($booking->quote['source'] ?? null) !== 'portal') {
|
||
$extras = array_filter((array) ($booking->extras ?? []), fn ($quantity): bool => (int) $quantity > 0);
|
||
foreach (Extra::query()->whereIn('id', array_keys($extras))->get() as $extra) {
|
||
$task = BookingTask::openOnce($booking, BookingTaskKind::Service, 'extra:'.$extra->id, [
|
||
'title' => 'Bereitstellen: '.((int) $extras[$extra->id] > 1 ? $extras[$extra->id].' × ' : '').$extra->name,
|
||
'due_on' => $booking->arrival->subDay(),
|
||
]);
|
||
$opened += $task->wasRecentlyCreated ? 1 : 0;
|
||
}
|
||
}
|
||
|
||
return $opened;
|
||
}
|
||
}
|