Vier Schablonen (Eigenes/Portal-Inkasso, Eigene/Externe Vertragsdokumentation) in app/Process/Templates, TemplateComposer prüft Ergebnisse, Verantwortlichkeiten, Ausschlüsse und harte Modulabhängigkeiten. Der Plan friert die Schablonenversionen ein. A3–A5 bleiben als Voreinstellungen; die vierte Kombination (eigenes Inkasso + externe Unterlagen) ist darstellbar. Konfigurator-Logik und ConfigureChannelProcess kennen die Schablonenwahl. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
242 lines
9.7 KiB
PHP
242 lines
9.7 KiB
PHP
<?php
|
||
|
||
namespace App\Process;
|
||
|
||
use App\Enums\IntakeKind;
|
||
use App\Models\Booking;
|
||
use App\Models\Channel;
|
||
use App\Process\Models\ProcessModel;
|
||
use App\Process\Templates\TemplateComposer;
|
||
use DomainException;
|
||
|
||
/**
|
||
* The frozen process plan of a case (docs/Prozessarchitektur/Architektur/02 §1, §5.3): model and version, variant,
|
||
* the versions of its modules and the step settings of the channel (on/off, mode, performer, parameters) as they were
|
||
* when the case came in. Stored in `bookings.profile.plan`; cases without one (all before V2) get the derived plan of
|
||
* their profile on reading, which reproduces the v0.11 course (legacy mapping). The plan also keeps the labels of its
|
||
* steps, so a finished case stays readable even if a model definition is ever retired (requirement T3).
|
||
*/
|
||
final class ProcessPlan
|
||
{
|
||
public const DERIVED = 'derived';
|
||
|
||
public const CONFIGURED = 'configured';
|
||
|
||
public const TESTER = 'tester';
|
||
|
||
/**
|
||
* @param array<string, int> $modules module key => frozen version
|
||
* @param array<string, array{on?: bool, mode?: ?string, performer?: ?string, params?: array<string, mixed>}> $steps
|
||
* @param array<string, string> $labels module key => label at the time of freezing
|
||
* @param array<string, string> $templates area => function template key@version the plan was composed with
|
||
*/
|
||
public function __construct(
|
||
public readonly string $model,
|
||
public readonly int $modelVersion,
|
||
public readonly string $variant,
|
||
public readonly array $modules,
|
||
public readonly array $steps = [],
|
||
public readonly string $source = self::DERIVED,
|
||
public readonly array $labels = [],
|
||
public readonly ?string $notice = null,
|
||
public readonly array $templates = [],
|
||
) {}
|
||
|
||
/**
|
||
* The plan of a case: frozen with it, or derived from its profile (never stored by reading).
|
||
*/
|
||
public static function of(Booking $booking): self
|
||
{
|
||
$data = (array) (($booking->profile ?? [])['plan'] ?? []);
|
||
|
||
return $data !== [] ? self::fromArray($data) : self::derive($booking->process());
|
||
}
|
||
|
||
/**
|
||
* The plan a case gets from its profile when no model was chosen for its channel.
|
||
*/
|
||
public static function derive(ProcessProfile $profile): self
|
||
{
|
||
$catalogue = app(ProcessCatalogue::class);
|
||
|
||
return self::build($catalogue->model($catalogue->derivedModel($profile)), 'standard', [], self::DERIVED);
|
||
}
|
||
|
||
/**
|
||
* The plan frozen for a new case: the model and step settings chosen for its channel and intake kind (Kanal ›
|
||
* Ablauf), when admissible for the case's type; otherwise the derived one with a notice. Tester-only variants or
|
||
* modules are refused outside the process tester (decision E7).
|
||
*
|
||
* @param array<string, mixed> $configuration model, variant, steps, templates of channels.process_overrides[intake]
|
||
*/
|
||
public static function freeze(ProcessProfile $profile, array $configuration, bool $sandbox = false): self
|
||
{
|
||
$catalogue = app(ProcessCatalogue::class);
|
||
// Inside a process tester run (its own process, SandboxBootstrapper) tester-only variants are allowed.
|
||
$sandbox = $sandbox || filled(config('sandbox.run'));
|
||
$chosen = (string) ($configuration['model'] ?? '');
|
||
if ($chosen === '') {
|
||
return self::build($catalogue->model($catalogue->derivedModel($profile)), 'standard', (array) ($configuration['steps'] ?? []), self::DERIVED);
|
||
}
|
||
|
||
if (! $catalogue->hasModel($chosen)) {
|
||
return self::derive($profile)->withNotice("Gewähltes Modell {$chosen} ist nicht registriert – abgeleitetes Modell verwendet.");
|
||
}
|
||
|
||
$model = $catalogue->model($chosen);
|
||
if (! in_array($profile->caseType, $model::caseTypes(), true) || ! in_array($profile->intake, $model::intakes(), true)) {
|
||
return self::derive($profile)->withNotice("Gewähltes Modell {$chosen} passt nicht zu Vorgangsart {$profile->caseType->value} – abgeleitetes Modell verwendet.");
|
||
}
|
||
|
||
$variant = (string) ($configuration['variant'] ?? 'standard');
|
||
$chosenVariant = $model->variant($variant) ?? throw new DomainException("Modell {$chosen} hat keine Variante {$variant}.");
|
||
if (! $sandbox && $chosenVariant->isTesterOnly()) {
|
||
throw new DomainException("Die Variante „{$chosenVariant->label}“ ist nur im Prozess-Tester verwendbar.");
|
||
}
|
||
foreach ($model->modules($variant) as $module) {
|
||
if (! $sandbox && $catalogue->module($module)::definition()->isTesterOnly()) {
|
||
throw new DomainException("Das Modul {$module} ist nur im Prozess-Tester verwendbar.");
|
||
}
|
||
}
|
||
|
||
$templates = [];
|
||
if ($model->slots() !== []) {
|
||
$composer = app(TemplateComposer::class);
|
||
$templates = $composer->resolve($model, (array) ($configuration['templates'] ?? []));
|
||
if (($problems = $composer->problems($model, $templates, $variant)) !== []) {
|
||
return self::derive($profile)->withNotice("Schablonen von {$chosen} nicht zulässig (".implode(' ', $problems).') – abgeleitetes Modell verwendet.');
|
||
}
|
||
}
|
||
|
||
return self::build($model, $variant, (array) ($configuration['steps'] ?? []), $sandbox ? self::TESTER : self::CONFIGURED, $templates);
|
||
}
|
||
|
||
/**
|
||
* The configuration of a channel for one intake kind as the plan reads it (model, variant, steps).
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
public static function configurationOf(?Channel $channel, IntakeKind $intake): array
|
||
{
|
||
if ($channel === null || $intake === IntakeKind::Internal) {
|
||
return [];
|
||
}
|
||
|
||
return array_intersect_key((array) (($channel->process_overrides ?? [])[$intake->value] ?? []), array_flip(['model', 'variant', 'steps', 'templates']));
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $steps
|
||
* @param array<string, string> $templates area => key@version
|
||
*/
|
||
private static function build(ProcessModel $model, string $variant, array $steps, string $source, array $templates = []): self
|
||
{
|
||
$catalogue = app(ProcessCatalogue::class);
|
||
$modules = [];
|
||
$labels = [];
|
||
foreach ($model->modules($variant, $templates) as $key) {
|
||
$class = $catalogue->modules()[$key];
|
||
$modules[$key] = $class::version();
|
||
$labels[$key] = $class::definition()->label;
|
||
}
|
||
|
||
return new self($model::key(), $model::version(), $variant, $modules, array_intersect_key($steps, $modules), $source, $labels, null, $templates);
|
||
}
|
||
|
||
private function withNotice(string $notice): self
|
||
{
|
||
return new self($this->model, $this->modelVersion, $this->variant, $this->modules, $this->steps, $this->source, $this->labels, $notice, $this->templates);
|
||
}
|
||
|
||
public function modelDefinition(): ProcessModel
|
||
{
|
||
$catalogue = app(ProcessCatalogue::class);
|
||
|
||
// A retired version (T3): its cases stay readable through the latest definition of the same model.
|
||
return $catalogue->hasModel($this->model, $this->modelVersion) ? $catalogue->model($this->model, $this->modelVersion) : $catalogue->model($this->model);
|
||
}
|
||
|
||
public function isHistoric(): bool
|
||
{
|
||
return ! app(ProcessCatalogue::class)->hasModel($this->model, $this->modelVersion);
|
||
}
|
||
|
||
/**
|
||
* The module keys of the case's course, in order.
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public function moduleKeys(): array
|
||
{
|
||
return array_keys($this->modules);
|
||
}
|
||
|
||
/** Whether a step is switched on (only optional steps can be switched off; mandatory ones are always on). */
|
||
public function enabled(string $module): bool
|
||
{
|
||
if (($this->steps[$module]['on'] ?? true) !== false) {
|
||
return true;
|
||
}
|
||
|
||
$catalogue = app(ProcessCatalogue::class);
|
||
|
||
return $catalogue->hasModule($module) && ! $catalogue->module($module)::definition()->isOptional();
|
||
}
|
||
|
||
public function mode(string $module): ?string
|
||
{
|
||
return $this->steps[$module]['mode'] ?? null;
|
||
}
|
||
|
||
public function performer(string $module): ?string
|
||
{
|
||
return $this->steps[$module]['performer'] ?? null;
|
||
}
|
||
|
||
public function parameter(string $module, string $name): mixed
|
||
{
|
||
return $this->steps[$module]['params'][$name] ?? null;
|
||
}
|
||
|
||
public function reference(): string
|
||
{
|
||
return $this->model.'@'.$this->modelVersion.($this->variant !== 'standard' ? ':'.$this->variant : '');
|
||
}
|
||
|
||
/**
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function toArray(): array
|
||
{
|
||
return array_filter([
|
||
'model' => $this->model,
|
||
'model_version' => $this->modelVersion,
|
||
'variant' => $this->variant,
|
||
'modules' => $this->modules,
|
||
'steps' => $this->steps,
|
||
'source' => $this->source,
|
||
'labels' => $this->labels,
|
||
'notice' => $this->notice,
|
||
'templates' => $this->templates === [] ? null : $this->templates,
|
||
], fn (mixed $value): bool => $value !== null);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
*/
|
||
public static function fromArray(array $data): self
|
||
{
|
||
return new self(
|
||
(string) $data['model'],
|
||
(int) $data['model_version'],
|
||
(string) ($data['variant'] ?? 'standard'),
|
||
array_map('intval', (array) ($data['modules'] ?? [])),
|
||
(array) ($data['steps'] ?? []),
|
||
(string) ($data['source'] ?? self::DERIVED),
|
||
(array) ($data['labels'] ?? []),
|
||
isset($data['notice']) ? (string) $data['notice'] : null,
|
||
array_map('strval', (array) ($data['templates'] ?? [])),
|
||
);
|
||
}
|
||
}
|