Files
fewo-stommer/app/Process/Modules/Settlement/BookingDocuments.php
T
LarsandClaude Sonnet 5.5 8442d0a3d8 V2.1: Kanalfähigkeiten, Ausführungsvarianten, Dokumentenentkopplung, Portalbestätigung
Modellwahl über Kanalfähigkeiten mit erklärten Einschränkungen, Abgleich alter Werte,
Ausführungsvarianten je Modul, getrennte Vertrags-/Buchungsinformation, Warten auf
die Portalbestätigung mit Eskalation und Wiederaufnahme (Fälle C und D getestet).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 07:54:11 +01:00

145 lines
7.2 KiB
PHP
Raw 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\Settlement;
use App\Actions\SendBookingDocuments;
use App\Enums\ActivityState;
use App\Enums\ContractDelivery;
use App\Enums\Execution;
use App\Enums\ProcessSetting;
use App\Enums\Responsibility;
use App\Process\Activity;
use App\Process\CaseAutomation;
use App\Process\CaseContext;
use App\Process\Modules\Dependency;
use App\Process\Modules\Module;
use App\Process\Modules\ModuleDefinition;
use App\Process\Modules\RunsAutomatically;
use App\Process\PortalConfirmation;
use App\Process\StepResult;
/**
* P03: the binding contract documentation for the guest (booking confirmation with contract, or the documents of a
* portal booking). Who is responsible for it decides the case type (decision O1): ours in A1–A4, the portal's in A5 –
* then the step is shown as external.
*/
final class BookingDocuments extends Module implements RunsAutomatically
{
public static function key(): string
{
return 'documents.send';
}
public static function version(): int
{
// 2: the contract document is created and filed separately from sending it (contract_delivery); waits for the portal's
// confirmation where the channel says so. Default behaviour is the one of version 1.
return 2;
}
public static function definition(): ModuleDefinition
{
return new ModuleDefinition(
label: 'Buchungsunterlagen (Vertragsdokumentation)',
purpose: 'Sendet dem Gast die verbindliche Buchungsbestätigung mit Vertrag bzw. die Unterlagen der Portalbuchung.',
phase: 'P03',
executions: [Execution::Automatic, Execution::Approval, Execution::Manual, Execution::Portal],
condition: 'bei jeder verbindlichen Buchung, für die wir die Unterlagen verantworten',
why: 'Die verbindliche Vertragsdokumentation muss den Gast erreichen – von uns oder vom Portal (O1). Wie, ist einstellbar.',
requires: [Dependency::hard('guest.details'), Dependency::hard('portal.confirmation')],
effects: ['case_write', 'guest_mail', 'document'],
waits: ['landlord', 'portal'],
triggers: ['automation', 'booking.taken_over', 'portal.mail', 'schedule:waiting'],
parameters: [ProcessSetting::Documents, ProcessSetting::Processing, ProcessSetting::ContractDelivery, ProcessSetting::ContractTemplate],
performers: ['system', 'landlord', 'portal'],
modes: ['automatic', 'manual'],
events: ['document', 'mail'],
readsContractData: true,
);
}
public function applies(CaseContext $case): bool
{
return ! $case->booking->isInternal();
}
public function activities(CaseContext $case): array
{
$booking = $case->booking;
if ($case->profile->documents() === Responsibility::Portal) {
return [new Activity('documents', 'Buchungsunterlagen an den Gast', ActivityState::External)];
}
$binding = $case->binding;
$delivery = $case->profile->contractDelivery();
$internal = $delivery === ContractDelivery::Internal;
$stored = $booking->contract_path !== null;
$awaiting = $binding && ! $case->inquiry && $case->profile->awaitsPortalConfirmation() && PortalConfirmation::evidence($booking) === null;
$state = match (true) {
$booking->confirmation_sent_at !== null => ActivityState::Done,
// Created and filed internally is how the landlord chose to fulfil the step; sending stays possible by hand.
$internal && $stored => ActivityState::Done,
! $binding => ActivityState::Later,
$booking->confirmation_failed_at !== null => ActivityState::Problem,
default => ActivityState::Open,
};
$missing = $binding && $state === ActivityState::Open ? app(CaseAutomation::class)->documentBlockers($booking) : [];
// What the landlord still has to confirm (unreleased texts, terms not shown to be agreed on the portal).
$toConfirm = $binding && $state === ActivityState::Open && $missing === [] ? app(SendBookingDocuments::class)->compose($booking)->toConfirm() : [];
$waiting = $missing !== [] || ($awaiting && $state === ActivityState::Open);
return [new Activity('documents', match (true) {
$internal && $state === ActivityState::Done && $booking->confirmation_sent_at === null => 'Vertrag intern abgelegt',
$internal && $state === ActivityState::Open => 'Vertrag erstellen und intern ablegen',
$state !== ActivityState::Open => $case->inquiry ? 'Buchungsbestätigung mit Vertrag senden' : 'Buchungsunterlagen an den Gast senden',
$case->inquiry => 'Buchungsbestätigung mit Vertrag prüfen und senden',
default => 'Buchungsunterlagen prüfen und senden',
}, $case->later($waiting ? ActivityState::Waiting : $state),
match (true) {
$booking->confirmation_sent_at !== null => 'gesendet am '.$booking->confirmation_sent_at->format('d.m.Y'),
$internal && $state === ActivityState::Done => 'erstellt und intern abgelegt – nicht an den Gast gesendet (Einstellung des Kanals); Senden von Hand bleibt möglich',
$state === ActivityState::Problem => 'Versand fehlgeschlagen',
$missing !== [] => 'wartet: '.implode(' ', $missing),
$awaiting => 'wartet auf die Bestätigungsmail des Portals (die Belegung ist gesichert)',
$toConfirm !== [] => 'vor dem Versand zu prüfen: '.implode(' ', $toConfirm),
default => null,
}, match (true) {
$state === ActivityState::Problem => 'resendConfirmation',
$binding && $missing === [] && $booking->confirmation_sent_at === null && ! ($internal && $stored) => 'sendDocuments',
default => null,
})];
}
/**
* The guest's e-mail and the price are what the documents need – not every detail of a request. Due when they are
* to go out automatically (as the case is processed or as chosen for the delivery), or – delivery „internal“ – when the
* contract is still to be created and filed.
*/
public function due(CaseContext $case): bool
{
$booking = $case->booking;
$delivery = $case->profile->contractDelivery();
$mine = $case->binding && $booking->confirmation_sent_at === null && $case->profile->documents() === Responsibility::Landlord && $case->profile->origin !== 'migration';
return $mine && ($delivery === ContractDelivery::Internal ? $booking->contract_path === null : $delivery->sendsAutomatically($booking->processingMode()));
}
public function blockers(CaseContext $case): array
{
return app(CaseAutomation::class)->documentBlockers($case->booking);
}
public function run(CaseContext $case): StepResult
{
$documents = app(SendBookingDocuments::class);
$reasons = $case->profile->contractDelivery() === ContractDelivery::Internal ? $documents->storeInternally($case->booking) : $documents->automatically($case->booking);
return $reasons === [] ? StepResult::ran() : StepResult::blocked($reasons);
}
public function isOutward(): bool
{
return true;
}
}