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>
110 lines
5.7 KiB
PHP
110 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Enums\IntakeKind;
|
||
use App\Enums\ProcessSetting;
|
||
use App\Models\Channel;
|
||
use App\Process\ChannelCapabilities;
|
||
use App\Process\ChannelProcessConfiguration;
|
||
use App\Process\ProcessCatalogue;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Validation\ValidationException;
|
||
|
||
/**
|
||
* Saves the process of a channel for one intake kind (Kanal › Ablauf, docs/Prozessarchitektur/Architektur/03 and V2.1/02):
|
||
* the confirmed capabilities of the portal, the chosen model and variant, the step switches and the step settings – in
|
||
* channels.settings and channels.process_overrides[intake], next to the existing settings, so their inheritance stays
|
||
* as it is. Refuses everything the configuration does not admit (mandatory steps off, responsibilities the model does not
|
||
* allow, a model the capabilities do not fit, tester-only variants). Stored settings that the chosen model overrides are
|
||
* removed only after the landlord confirmed that, and the change is kept in the channel's history. Changes apply to new
|
||
* cases only (decision E3): running cases keep their frozen plan.
|
||
*/
|
||
class ConfigureChannelProcess
|
||
{
|
||
public const HISTORY_LIMIT = 20;
|
||
|
||
public function __construct(private ChannelProcessConfiguration $configuration, private ChannelCapabilities $capabilities, private ProcessCatalogue $catalogue) {}
|
||
|
||
/**
|
||
* @param array{model: string, variant?: string, steps?: array<string, array{on?: bool}>, settings?: array<string, mixed>, templates?: array<string, string>, capabilities?: array<string, bool|string|null>} $draft
|
||
* @param bool $acknowledgeConflicts the landlord confirmed that stored settings the model overrides are removed
|
||
*
|
||
* @throws ValidationException
|
||
*/
|
||
public function handle(Channel $channel, IntakeKind $intake, array $draft, ?string $by = null, bool $acknowledgeConflicts = false): Channel
|
||
{
|
||
$problems = $this->configuration->problems($channel, $intake, $draft);
|
||
if ($problems !== []) {
|
||
throw ValidationException::withMessages(['process' => $problems]);
|
||
}
|
||
|
||
$conflicts = $this->configuration->conflicts($channel, $intake, (string) $draft['model'], (array) ($draft['templates'] ?? []));
|
||
if ($conflicts !== [] && ! $acknowledgeConflicts) {
|
||
throw ValidationException::withMessages(['process' => [
|
||
'Bisherige Einstellungen widersprechen dem gewählten Modell und würden still überstimmt. Bitte ausdrücklich bestätigen, dass sie entfernt werden:',
|
||
...array_column($conflicts, 'text'),
|
||
]]);
|
||
}
|
||
|
||
return DB::transaction(function () use ($channel, $intake, $draft, $by, $conflicts): Channel {
|
||
$channel = Channel::query()->lockForUpdate()->findOrFail($channel->id);
|
||
$by ??= auth()->user()?->name;
|
||
$overrides = (array) ($channel->process_overrides ?? []);
|
||
$current = (array) ($overrides[$intake->value] ?? []);
|
||
$removed = array_map(fn (array $conflict): string => $conflict['setting']->value, $conflicts);
|
||
|
||
$settings = [];
|
||
foreach (ProcessSetting::cases() as $setting) {
|
||
if (in_array($setting->value, $removed, true)) {
|
||
continue;
|
||
}
|
||
$value = array_key_exists($setting->value, (array) ($draft['settings'] ?? [])) ? $setting->normalize($draft['settings'][$setting->value]) : $setting->normalize($current[$setting->value] ?? null);
|
||
if ($value !== null && in_array($intake, $setting->channelIntakes(), true)) {
|
||
$settings[$setting->value] = $value;
|
||
}
|
||
}
|
||
|
||
$steps = [];
|
||
foreach ((array) ($draft['steps'] ?? []) as $module => $step) {
|
||
if (array_key_exists('on', $step) && $step['on'] === false) {
|
||
$steps[$module] = ['on' => false];
|
||
}
|
||
}
|
||
|
||
// A model with slots always stores its explicit template choice (keys; the version is frozen with each new case).
|
||
$model = $this->catalogue->model((string) $draft['model']);
|
||
$templates = array_map(fn (string $reference): string => explode('@', $reference)[0], $this->configuration->templatesOf($model, $draft));
|
||
if ($model->slots() === []) {
|
||
$templates = [];
|
||
}
|
||
|
||
$overrides[$intake->value] = [
|
||
...$settings,
|
||
'model' => $draft['model'],
|
||
'variant' => $draft['variant'] ?? 'standard',
|
||
'steps' => $steps,
|
||
'templates' => $templates,
|
||
'configured_at' => now()->toIso8601String(),
|
||
'configured_by' => $by,
|
||
];
|
||
|
||
$channelSettings = (array) ($channel->settings ?? []);
|
||
$changes = [];
|
||
if (! $channel->isWebsite() && filled($draft['capabilities'] ?? null)) {
|
||
$confirmed = $this->capabilities->confirm($channel, (array) $draft['capabilities'], $by);
|
||
$channelSettings['capabilities'] = $confirmed['capabilities'];
|
||
$changes = $confirmed['changes'];
|
||
}
|
||
$channelSettings['process_history'] = array_slice([...(array) ($channelSettings['process_history'] ?? []), [
|
||
'at' => now()->toIso8601String(), 'by' => $by, 'intake' => $intake->value, 'model' => $draft['model'], 'variant' => $draft['variant'] ?? 'standard', 'templates' => $templates,
|
||
'capabilities' => $changes, 'removed_settings' => array_map(fn (array $conflict): string => $conflict['text'], $conflicts),
|
||
]], -self::HISTORY_LIMIT);
|
||
|
||
$channel->update(['process_overrides' => $overrides, 'settings' => $channelSettings]);
|
||
|
||
return $channel;
|
||
});
|
||
}
|
||
}
|