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

257 lines
13 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\ChannelBookingModel;
use App\Enums\DataRequestMode;
use App\Enums\IntakeKind;
use App\Enums\ProcessingMode;
use App\Enums\ProcessSetting;
use App\Enums\Responsibility;
use App\Models\Channel;
use App\Process\Models\ProcessModel;
use App\Process\Models\Responsibilities;
/**
* What the channel configurator offers and checks (docs/Prozessarchitektur/Architektur/03): per channel and intake
* kind the admissible models (portal profile, E2/O2), and per step of the chosen model its switch, mode, performer
* and parameters – each control mapped to the existing process setting where one exists, so the inheritance landlord →
* property → channel stays the one mechanism. Pure: reads, never writes (ConfigureChannelProcess saves).
*/
final class ChannelProcessConfiguration
{
/**
* Controls of the steps: mode and performer stored as existing settings, parameters per module.
*
* @var array<string, array{mode?: ProcessSetting, performer?: array{field: string, setting: ProcessSetting}, parameters?: list<ProcessSetting>}>
*/
public const STEP_CONTROLS = [
'guest.details' => ['parameters' => [ProcessSetting::FormRequiresAddress]],
'guest.data_request' => ['mode' => ProcessSetting::DataRequest, 'parameters' => [ProcessSetting::DataRequestReminderHours, ProcessSetting::DataRequestRemindersMax, ProcessSetting::DataRequestFollowupsMax, ProcessSetting::GuestLinkValidDays]],
'hold' => ['parameters' => [ProcessSetting::ClarificationHoldAutoMinutes, ProcessSetting::ClarificationHoldManualMinutes]],
'offer.binding' => ['mode' => ProcessSetting::Processing, 'parameters' => [ProcessSetting::HoldHours, ProcessSetting::ContractTemplate]],
'offer.information' => ['mode' => ProcessSetting::Processing, 'parameters' => [ProcessSetting::GuestLinkValidDays]],
'documents.send' => ['mode' => ProcessSetting::Processing, 'parameters' => [ProcessSetting::ContractTemplate]],
'arrival.info' => ['performer' => ['field' => 'guest_messages', 'setting' => ProcessSetting::GuestMessages], 'parameters' => [ProcessSetting::ArrivalInfoDaysBefore]],
];
public function __construct(private ProcessCatalogue $catalogue) {}
/**
* The intake kinds the channel delivers.
*
* @return list<IntakeKind>
*/
public function intakes(Channel $channel): array
{
if ($channel->isWebsite()) {
return [IntakeKind::Inquiry];
}
return match ($channel->booking_model) {
ChannelBookingModel::Inquiry => [IntakeKind::Inquiry],
ChannelBookingModel::PortalBooking => [IntakeKind::Booking],
default => [IntakeKind::Inquiry, IntakeKind::Booking],
};
}
public function profileKey(Channel $channel): string
{
if ($channel->isWebsite()) {
return 'website';
}
$chosen = (string) (($channel->settings ?? [])['portal_profile'] ?? '');
$profiles = (array) config('channels.portal_profiles');
if ($chosen !== '' && array_key_exists($chosen, $profiles)) {
return $chosen;
}
$name = mb_strtolower((string) $channel->name);
foreach ($profiles as $key => $profile) {
foreach ((array) ($profile['match'] ?? []) as $needle) {
if (str_contains($name, $needle)) {
return $key;
}
}
}
return 'generic';
}
/**
* The models the landlord may choose for this channel and intake kind.
*
* @return array<string, ProcessModel>
*/
public function admissibleModels(Channel $channel, IntakeKind $intake): array
{
$allowed = (array) (config('channels.portal_profiles.'.$this->profileKey($channel))[$intake->value] ?? []);
return array_filter($this->catalogue->admissible($intake), fn (ProcessModel $model): bool => in_array($model::key(), $allowed, true));
}
/**
* The model a new case of this channel and intake would follow now: the chosen one, or the one derived from the
* settings as before (marked as derived).
*
* @return array{model: string, variant: string, derived: bool}
*/
public function current(Channel $channel, IntakeKind $intake): array
{
$config = ProcessPlan::configurationOf($channel, $intake);
if (filled($config['model'] ?? null) && $this->catalogue->hasModel((string) $config['model'])) {
return ['model' => (string) $config['model'], 'variant' => (string) ($config['variant'] ?? 'standard'), 'derived' => false];
}
$property = $channel->property;
$profile = app(ProcessSettings::class)->resolve($property, $channel, $intake);
return ['model' => $this->catalogue->derivedModel($profile), 'variant' => 'standard', 'derived' => true];
}
/**
* Allowed values of the performer of a step under the model (variable responsibility), as setting values.
*
* @return array<string, string> value => label
*/
public function performerOptions(ProcessModel $model, string $module): array
{
$control = self::STEP_CONTROLS[$module]['performer'] ?? null;
if ($control === null) {
return [];
}
return collect($model::responsibilities()->allowed($control['field']))
->mapWithKeys(fn (Responsibility $value): array => [$value->value => $value === Responsibility::Portal ? 'Portal' : ($value === Responsibility::Landlord ? 'wir (automatisch)' : $value->getLabel())])
->all();
}
/**
* Allowed modes of a step (as values of its setting).
*
* @return array<string, string>
*/
public function modeOptions(string $module): array
{
return match (self::STEP_CONTROLS[$module]['mode'] ?? null) {
ProcessSetting::Processing => collect(ProcessingMode::cases())->mapWithKeys(fn (ProcessingMode $mode): array => [$mode->value => $mode->getLabel()])->all(),
ProcessSetting::DataRequest => collect(DataRequestMode::cases())->mapWithKeys(fn (DataRequestMode $mode): array => [$mode->value => $mode->getLabel()])->all(),
default => [],
};
}
/**
* The course a typical new case would take with the given (possibly unsaved) configuration: every step of the model
* with whether it takes part and why not, who carries it out and how.
*
* @param array<string, mixed> $draft model, variant, steps, settings of channels.process_overrides[intake]
* @return list<array{module: string, label: string, phase: string, on: bool, reason: ?string, performer: ?string, mode: ?string, toggle: string, tester_only: bool, inserted: bool}>
*/
public function preview(Channel $channel, IntakeKind $intake, array $draft): array
{
$model = $this->catalogue->model((string) ($draft['model'] ?? $this->current($channel, $intake)['model']));
$variant = (string) ($draft['variant'] ?? 'standard');
$profile = $this->profileWith($channel, $intake, $draft);
$standard = $model->modules('standard');
$rows = [];
foreach ($model->modules($variant) as $key) {
$class = $this->catalogue->modules()[$key];
$definition = $class::definition();
$on = ($draft['steps'][$key]['on'] ?? true) !== false || ! $definition->isOptional();
$reason = match (true) {
! $on => 'im Kanal abgeschaltet',
$key === 'hold' && ! $profile->preReservation() => 'keine Vorreservierung in diesem Modell',
$key === 'offer.information' && $profile->preReservation() => 'nicht in diesem Modell (verbindliches Angebot)',
$key === 'offer.binding' && ! $profile->preReservation() => 'erst, wenn ein Angebot von Hand gesendet wird',
$key === 'portal.payout' && $profile->payments() !== Responsibility::Portal => 'wir kassieren selbst',
$key === 'review.request' && $profile->guestMessages() !== Responsibility::Landlord => 'das Portal schreibt dem Gast',
$key === 'guest.details' && $intake === IntakeKind::Booking && $profile->documents() === Responsibility::Portal && $profile->payments() === Responsibility::Portal && $profile->guestMessages() === Responsibility::Portal => 'wir schreiben dem Gast nicht',
default => null,
};
$mode = isset(self::STEP_CONTROLS[$key]['mode']) ? (string) $profile->value(self::STEP_CONTROLS[$key]['mode']) : (in_array('automatic', $definition->modes, true) ? 'automatic' : null);
$performer = match ($key) {
'documents.send' => $profile->documents()->value,
'payments.monitor' => $profile->payments()->value,
'arrival.info' => $profile->guestMessages()->value,
default => $definition->performers[0] ?? null,
};
$rows[] = ['module' => $key, 'label' => $definition->label, 'phase' => $definition->phase, 'on' => $on && $reason === null, 'reason' => $reason,
'performer' => $performer, 'mode' => $mode, 'toggle' => $definition->toggle, 'tester_only' => $definition->isTesterOnly(), 'inserted' => ! in_array($key, $standard, true)];
}
return $rows;
}
/**
* Everything that is not admissible in a draft (empty = it may be saved).
*
* @param array<string, mixed> $draft
* @return list<string>
*/
public function problems(Channel $channel, IntakeKind $intake, array $draft, bool $sandbox = false): array
{
$problems = [];
if (! in_array($intake, $this->intakes($channel), true)) {
$problems[] = 'Dieser Kanal liefert keine '.($intake === IntakeKind::Booking ? 'festen Buchungen' : 'Anfragen').'.';
}
$key = (string) ($draft['model'] ?? '');
$models = $this->admissibleModels($channel, $intake);
if ($key === '' || ! array_key_exists($key, $models)) {
return [...$problems, 'Das Ablaufmodell „'.$key.'“ ist für diesen Kanal nicht zulässig. Zulässig: '.(implode(', ', array_map(fn (ProcessModel $model): string => $model::label(), $models)) ?: 'keines').'.'];
}
$model = $models[$key];
$variant = $model->variant((string) ($draft['variant'] ?? 'standard'));
if ($variant === null) {
return [...$problems, 'Das Modell hat diese Variante nicht.'];
}
if ($variant->isTesterOnly() && ! $sandbox) {
$problems[] = 'Die Variante „'.$variant->label.'“ ist nur im Prozess-Tester verwendbar und kann nicht gespeichert werden.';
}
foreach ((array) ($draft['steps'] ?? []) as $module => $step) {
if (! $this->catalogue->hasModule($module) || ! in_array($module, $model->modules($variant->key), true)) {
$problems[] = "Der Schritt {$module} gehört nicht zu diesem Modell.";
continue;
}
$definition = $this->catalogue->modules()[$module]::definition();
if (($step['on'] ?? true) === false && ! $definition->isOptional()) {
$problems[] = 'Der Schritt „'.$definition->label.'“ ist Pflicht und kann nicht abgeschaltet werden.';
}
if ($definition->isTesterOnly() && ! $sandbox && ($step['on'] ?? true) !== false && $variant->isTesterOnly()) {
$problems[] = 'Der Schritt „'.$definition->label.'“ ist nur im Prozess-Tester verwendbar.';
}
}
// Responsibilities: fixed ones are set by the model; variable ones only to the values the model allows.
$responsibilities = $model::responsibilities();
foreach (Responsibilities::SETTINGS as $field => $setting) {
$value = $draft['settings'][$setting->value] ?? null;
if ($value === null || $value === '') {
continue;
}
$responsibility = Responsibility::tryFrom((string) $value);
if ($responsibility === null || ! $responsibilities->admits($setting, $responsibility)) {
$problems[] = Responsibilities::LABELS[$field].': „'.($responsibility?->getLabel() ?? $value).'“ ist in diesem Modell nicht zulässig.';
}
}
return array_values(array_unique($problems));
}
/**
* The profile a new case would get with the draft (model fixes and setting values applied).
*
* @param array<string, mixed> $draft
*/
public function profileWith(Channel $channel, IntakeKind $intake, array $draft): ProcessProfile
{
$preview = clone $channel;
$overrides = (array) ($channel->process_overrides ?? []);
$overrides[$intake->value] = [...array_filter((array) ($draft['settings'] ?? []), fn (mixed $value): bool => $value !== null && $value !== ''), 'model' => $draft['model'] ?? null, 'variant' => $draft['variant'] ?? 'standard', 'steps' => (array) ($draft['steps'] ?? [])];
$preview->setAttribute('process_overrides', $overrides);
return app(ProcessSettings::class)->resolve($channel->property, $preview, $intake);
}
}