Files
fewo-stommer/app/Process/ProcessProfile.php
T

218 lines
7.5 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;
use App\Enums\CaseType;
use App\Enums\DataRequestMode;
use App\Enums\IntakeKind;
use App\Enums\ProcessingMode;
use App\Enums\ProcessSetting;
use App\Enums\Responsibility;
/**
* The rules that apply to one case: its intake kind, case type and the effective value of every process setting
* together with the level it came from. Frozen on the case when it comes in, so later changes of the settings only
* affect new cases.
*/
final class ProcessProfile
{
public const SYSTEM = 'system';
public const TENANT = 'tenant';
public const PROPERTY = 'property';
public const CHANNEL = 'channel';
/** The own website's entry of the channel manager (v0.11). */
public const WEBSITE = 'website';
/** Fixed by the process model chosen for the channel (Architektur V2). */
public const MODEL = 'model';
/** Fixed by the case type (A6), not settable. */
public const CASE_TYPE = 'case_type';
/** Settings resolved when they are used instead of being frozen (documents capture their own template). */
public const LIVE = [ProcessSetting::ContractTemplate];
/**
* @param array<string, string|int|bool|null> $values setting key => effective value
* @param array<string, string> $sources setting key => level the value came from
*/
public function __construct(
public readonly IntakeKind $intake,
public readonly CaseType $caseType,
public readonly array $values,
public readonly array $sources,
public readonly ?string $frozenAt = null,
public readonly string $origin = 'intake',
) {}
/**
* The case type of a constellation: internal occasions are A6; requests are A1 (own way) or A2 (intermediary);
* binding external bookings are A3–A5 by who handles documents and payments.
*/
public static function caseTypeFor(IntakeKind $intake, bool $viaChannel, Responsibility $documents, Responsibility $payments): CaseType
{
return match ($intake) {
IntakeKind::Internal => CaseType::A6,
IntakeKind::Inquiry => $viaChannel ? CaseType::A2 : CaseType::A1,
IntakeKind::Booking => match (true) {
$documents === Responsibility::Portal => CaseType::A5,
$payments === Responsibility::Portal => CaseType::A4,
default => CaseType::A3,
},
};
}
public function value(ProcessSetting $setting): string|int|bool|null
{
return array_key_exists($setting->value, $this->values) ? $this->values[$setting->value] : $setting->default();
}
public function source(ProcessSetting $setting): string
{
return $this->sources[$setting->value] ?? self::SYSTEM;
}
public function processing(): ProcessingMode
{
return ProcessingMode::tryFrom((string) $this->value(ProcessSetting::Processing)) ?? ProcessingMode::Manual;
}
public function preReservation(): bool
{
return (bool) $this->value(ProcessSetting::PreReservation);
}
public function holdHours(): int
{
return max(1, (int) $this->value(ProcessSetting::HoldHours));
}
public function documents(): Responsibility
{
return Responsibility::tryFrom((string) $this->value(ProcessSetting::Documents)) ?? Responsibility::Landlord;
}
public function payments(): Responsibility
{
return Responsibility::tryFrom((string) $this->value(ProcessSetting::Payments)) ?? Responsibility::Landlord;
}
public function guestMessages(): Responsibility
{
return Responsibility::tryFrom((string) $this->value(ProcessSetting::GuestMessages)) ?? Responsibility::Landlord;
}
public function arrivalInfoDaysBefore(): int
{
return max(1, (int) $this->value(ProcessSetting::ArrivalInfoDaysBefore));
}
public function dataRequestMode(): DataRequestMode
{
return DataRequestMode::tryFrom((string) $this->value(ProcessSetting::DataRequest)) ?? DataRequestMode::Approval;
}
/**
* Minutes of the clarification hold of an incomplete request (E-033): the value for automatic requests when the
* guest is asked automatically, otherwise the one for release or manual requests. 0 = no hold.
*/
public function clarificationHoldMinutes(): int
{
$setting = $this->dataRequestMode() === DataRequestMode::Automatic ? ProcessSetting::ClarificationHoldAutoMinutes : ProcessSetting::ClarificationHoldManualMinutes;
return max(0, (int) $this->value($setting));
}
public function remindersMax(): int
{
return max(0, (int) $this->value(ProcessSetting::DataRequestRemindersMax));
}
public function followupsMax(): int
{
return max(0, (int) $this->value(ProcessSetting::DataRequestFollowupsMax));
}
public function reminderHours(): int
{
return max(1, (int) $this->value(ProcessSetting::DataRequestReminderHours));
}
public function guestLinkValidDays(): int
{
return max(1, (int) $this->value(ProcessSetting::GuestLinkValidDays));
}
public function formRequiresAddress(): bool
{
return (bool) $this->value(ProcessSetting::FormRequiresAddress);
}
public function contractTemplateId(): ?int
{
$value = $this->value(ProcessSetting::ContractTemplate);
return $value === null ? null : (int) $value;
}
/**
* Plain words for a level, for the "where does this come from" hint.
*/
public static function sourceLabel(string $source): string
{
return match ($source) {
self::TENANT => 'Ihre Standardeinstellung',
self::PROPERTY => 'Abweichung der Wohnung',
self::CHANNEL => 'Abweichung des Portals',
self::WEBSITE => 'Einstellung der eigenen Webseite',
self::CASE_TYPE => 'fest für diese Vorgangsart',
self::MODEL => 'festgelegt durch das gewählte Ablaufmodell',
'migration' => 'aus dem bisherigen Stand übernommen',
default => 'Grundeinstellung',
};
}
/**
* The profile as it is frozen on the case (settings resolved live are left out).
*
* @return array{intake: string, case_type: string, values: array<string, string|int|bool|null>, sources: array<string, string>, frozen_at: string, origin: string}
*/
public function toArray(): array
{
$live = array_flip(array_map(fn (ProcessSetting $setting): string => $setting->value, self::LIVE));
return [
'intake' => $this->intake->value,
'case_type' => $this->caseType->value,
'values' => array_diff_key($this->values, $live),
'sources' => array_diff_key($this->sources, $live),
'frozen_at' => $this->frozenAt ?? now()->toIso8601String(),
'origin' => $this->origin,
];
}
/**
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
return new self(
IntakeKind::tryFrom((string) ($data['intake'] ?? '')) ?? IntakeKind::Inquiry,
CaseType::tryFrom((string) ($data['case_type'] ?? '')) ?? CaseType::A1,
(array) ($data['values'] ?? []),
(array) ($data['sources'] ?? []),
isset($data['frozen_at']) ? (string) $data['frozen_at'] : null,
(string) ($data['origin'] ?? 'intake'),
);
}
public function withOrigin(string $origin): self
{
return new self($this->intake, $this->caseType, $this->values, $this->sources, $this->frozenAt, $origin);
}
}