Files
fewo-stommer/app/Process/Modules/Intake/PortalConfirmationStep.php

129 lines
5.8 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\Process\Modules\Intake;
use App\Enums\ActivityState;
use App\Enums\BookingTaskKind;
use App\Enums\BookingTaskStatus;
use App\Enums\Execution;
use App\Enums\ProcessSetting;
use App\Models\BookingTask;
use App\Process\Activity;
use App\Process\CaseContext;
use App\Process\CaseLog;
use App\Process\Modules\Dependency;
use App\Process\Modules\Module;
use App\Process\Modules\ModuleDefinition;
use App\Process\Modules\RunsAutomatically;
use App\Process\Modules\StepState;
use App\Process\PortalConfirmation;
use App\Process\StepResult;
/**
* P02 for extern binding bookings (V2.1, docs/Prozessarchitektur/V2.1/02 §5): waits for the portal's booking confirmation
* mail, assigned to the case deterministically, before the steps that depend on it hard (contract documents, booking notice)
* go on. The occupation does not wait: the booking is binding from the start and its calendar entries are protected
* (binding.portal, channels.block do not depend on this step). Without the mail within the configured time the landlord gets
* one task; nothing is sent or released automatically. The landlord can confirm in the case, or send by hand.
*/
final class PortalConfirmationStep extends Module implements RunsAutomatically
{
public static function key(): string
{
return 'portal.confirmation';
}
public static function definition(): ModuleDefinition
{
return new ModuleDefinition(
label: 'Bestätigung des Portals abwarten',
purpose: 'Wartet auf die Bestätigungsmail des Portals, bevor Unterlagen und Nachrichten an den Gast gehen. Die Belegung ist von Anfang an gesichert.',
phase: 'P02',
why: 'Solange das Portal die Buchung nicht bestätigt hat, sollen keine Unterlagen an den Gast gehen; die Belegung ist trotzdem gesichert.',
executions: [Execution::Automatic, Execution::Manual, Execution::NotRequired],
condition: 'nur bei extern verbindlicher Buchung und nur, wenn für den Kanal „Bestätigungsmail abwarten“ eingestellt ist',
requires: [Dependency::hard('intake.record')],
effects: ['case_write'],
waits: ['portal', 'time', 'landlord'],
triggers: ['portal.mail', 'schedule:waiting', 'booking.taken_over'],
parameters: [ProcessSetting::AwaitPortalConfirmation, ProcessSetting::PortalConfirmationWaitHours],
toggle: ModuleDefinition::TOGGLE_CONDITIONAL,
performers: ['portal', 'landlord'],
modes: ['automatic'],
events: ['portal_confirmation', 'evidence', 'task'],
);
}
public function applies(CaseContext $case): bool
{
return ! $case->inquiry && ! $case->closed && $case->profile->awaitsPortalConfirmation();
}
public function state(CaseContext $case): ?StepState
{
if (PortalConfirmation::evidence($case->booking) !== null) {
return StepState::Done;
}
return PortalConfirmation::isOverdue($case->booking) ? StepState::Problem : StepState::Waiting;
}
public function activities(CaseContext $case): array
{
$evidence = PortalConfirmation::evidence($case->booking);
$deadline = PortalConfirmation::deadline($case->booking);
return [new Activity('portal_confirmation', 'Bestätigung des Portals abwarten', match (true) {
$evidence !== null => ActivityState::Done,
PortalConfirmation::isOverdue($case->booking) => ActivityState::Problem,
default => ActivityState::Waiting,
}, match (true) {
$evidence !== null => $evidence['label'],
PortalConfirmation::isOverdue($case->booking) => 'ausgeblieben seit '.$deadline->format('d.m.Y, H:i').' Uhr – bitte beim Portal prüfen; Unterlagen und Nachrichten warten weiter, bis Sie bestätigen oder von Hand senden',
default => 'erwartet bis '.$deadline->format('d.m.Y, H:i').' Uhr – Unterlagen und Nachrichten warten; die Belegung ist gesichert',
}, $evidence === null ? 'confirmPortal' : null)];
}
/** Due when the time ran out without the mail: escalate once; or when the mail arrived and the escalation is still open. */
public function due(CaseContext $case): bool
{
$open = $this->openTask($case) !== null;
$evidence = PortalConfirmation::evidence($case->booking) !== null;
return ($evidence && $open) || (! $evidence && ! $open && PortalConfirmation::isOverdue($case->booking));
}
public function blockers(CaseContext $case): array
{
return [];
}
public function run(CaseContext $case): StepResult
{
$task = $this->openTask($case);
if (PortalConfirmation::evidence($case->booking) !== null) {
$task?->complete(CaseLog::SYSTEM, 'Die Bestätigung des Portals liegt jetzt vor.');
return StepResult::ran();
}
BookingTask::openOnce($case->booking, BookingTaskKind::PortalConfirmationMissing, 'portal-confirmation', [
'due_on' => today(),
'note' => 'Die Bestätigungsmail des Portals ist bis '.PortalConfirmation::deadline($case->booking)->format('d.m.Y, H:i').' Uhr nicht eingegangen. Die Belegung ist gesichert. Bitte beim Portal prüfen und im Vorgang bestätigen – oder die Unterlagen von Hand senden.',
]);
CaseLog::record($case->booking, 'task', 'Bestätigungsmail des Portals fehlt – Aufgabe angelegt', actor: CaseLog::SYSTEM);
return StepResult::ran();
}
public function isOutward(): bool
{
return false;
}
private function openTask(CaseContext $case): ?BookingTask
{
return BookingTask::query()->where('booking_id', $case->booking->id)->where('kind', BookingTaskKind::PortalConfirmationMissing)->where('status', BookingTaskStatus::Open)->first();
}
}