Files
fewo-stommer/app/Filament/Pages/HandlingSettings.php
LarsandClaude Opus 5.5 cf11c8a474
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m59s
Dokumentenzusammenstellung und Vorlagensteuerung (Paket 3, Prozessarchitektur v0.10)
Eine Zusammenstellung vor jedem Dokument (Angebot, Bestaetigung, Buchungsunterlagen
extern), Dokumentart je Vorgangsart, Freigaben fuer Vorlagen und Dokumentarten,
Schutz vor unvereinbarten Bedingungen bei Portalbuchungen (E-004), kein doppelter
Versand, Absicherung Hold 0 bis Paket 7, A6-Aufenthaltsbestaetigung vorbereitet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 19:44:52 +01:00

179 lines
7.9 KiB
PHP
Raw Permalink 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\Filament\Pages;
use App\Documents\DocumentReleases;
use App\Enums\DocumentKind;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Enums\UserRole;
use App\Filament\Concerns\FollowsAdminMode;
use App\Filament\Forms\ProcessSettingFields;
use App\Models\Channel;
use App\Models\Property;
use App\Models\Tenant;
use App\Support\Settings;
use BackedEnum;
use Carbon\CarbonImmutable;
use Filament\Actions\Action;
use Filament\Forms\Components\Toggle;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\EmbeddedSchema;
use Filament\Schemas\Components\Form;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Text;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\HtmlString;
use UnitEnum;
/**
* "So arbeite ich": the landlord's own defaults for handling cases. A portal (Kanäle › Portal) and – in the expert
* area – a property may deviate; everything not set there follows these defaults.
*/
class HandlingSettings extends Page
{
use FollowsAdminMode;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedAdjustmentsVertical;
protected static string|UnitEnum|null $navigationGroup = 'System';
protected static ?string $navigationLabel = 'So arbeite ich';
protected static ?string $title = 'So arbeite ich';
protected static ?string $slug = 'abwicklung';
protected static ?int $navigationSort = 2;
/** @var array<string, mixed> */
public array $data = [];
public static function canAccess(): bool
{
return (bool) auth()->user()?->hasRole(UserRole::Admin);
}
/**
* @return list<ProcessSetting>
*/
public static function settings(): array
{
return array_values(array_filter(ProcessSetting::cases(), fn (ProcessSetting $setting): bool => $setting->isLandlordSetting()));
}
public function mount(): void
{
$this->form->fill([
...ProcessSettingFields::state((array) ($this->tenant()->process_defaults ?? [])),
'document_releases' => collect(self::releasableKinds())->mapWithKeys(fn (DocumentKind $kind): array => [$kind->value => DocumentReleases::isReleased($kind, $this->tenant())])->all(),
]);
}
public function form(Schema $schema): Schema
{
return $schema
->statePath('data')
->components([
Section::make('Ihre Standardeinstellungen')
->description('Gilt für alle Wohnungen und Portale, solange dort nichts anderes eingestellt ist. Neue Einstellungen gelten für neue Vorgänge; laufende Vorgänge behalten ihre Regeln.')
->columns(2)
->schema(ProcessSettingFields::make('', self::settings(), fn (ProcessSetting $setting): string => 'Grundeinstellung ('.$setting->describe($setting->default()).')')),
Section::make('Dokumente')
->description('Welche Dokumente an Gäste gehen dürfen. Eine Arbeitsfassung wird nie automatisch versendet, von Hand nur nach ausdrücklicher Bestätigung im Vorgang. Vorlagen selbst geben Sie unter Vorlagen frei.')
->schema([
...array_map(fn (DocumentKind $kind): Toggle => Toggle::make('document_releases.'.$kind->value)
->label($kind->getLabel().' – freigegeben')
->helperText(fn (): string => $kind->description().self::decisionNote($kind)), self::releasableKinds()),
Text::make(fn (): HtmlString => new HtmlString('<ul style="list-style:disc;padding-left:1.25rem">'.collect(DocumentKind::cases())
->reject(fn (DocumentKind $kind): bool => in_array($kind, self::releasableKinds(), true) || ! $kind->isForGuests())
->map(fn (DocumentKind $kind): string => '<li><strong>'.e($kind->getLabel()).'</strong>: '.e($kind->description()).' '.e($kind->isAvailable() ? '(in Verwendung; Texte unter Webseite › Texte bzw. Vorlagen)' : '(Arbeitsfassung vorbereitet, noch nicht nutzbar)').'</li>')
->implode('').'</ul>')),
]),
Section::make('Abweichungen')
->description('Wo Portale oder Wohnungen anders arbeiten. Ändern unter Portale bzw. Wohnungen (Expertenansicht).')
->collapsed()
->schema([Text::make(fn (): HtmlString => $this->deviations())]),
]);
}
public function content(Schema $schema): Schema
{
return $schema->components([
Form::make([EmbeddedSchema::make('form')])
->id('form')
->livewireSubmitHandler('save')
->footer([Actions::make([Action::make('save')->label('Speichern')->submit('save')])]),
]);
}
public function save(): void
{
$state = $this->form->getState();
$this->tenant()->update(['process_defaults' => ProcessSettingFields::values($state, self::settings())]);
foreach (self::releasableKinds() as $kind) {
DocumentReleases::set($kind, (bool) ($state['document_releases'][$kind->value] ?? false), (string) auth()->user()?->name, $this->tenant());
}
Notification::make()->title('Gespeichert')->body('Gilt für neue Vorgänge.')->success()->send();
}
/**
* Document kinds whose texts the landlord releases here: the new kinds that can already be sent. Offer and
* confirmation are in use since the start; the others are drafts of later packages.
*
* @return list<DocumentKind>
*/
public static function releasableKinds(): array
{
return array_values(array_filter(DocumentKind::cases(), fn (DocumentKind $kind): bool => $kind->isAvailable() && ! $kind->releasedByDefault()));
}
private static function decisionNote(DocumentKind $kind): string
{
$decision = DocumentReleases::decision($kind);
return $decision === null || $decision['at'] === null ? ' Bisher: Arbeitsfassung.' : ' Zuletzt geändert am '.CarbonImmutable::parse($decision['at'])->format('d.m.Y').' von '.$decision['by'].'.';
}
/**
* Every deviation of a property or a channel, in plain words.
*/
private function deviations(): HtmlString
{
$lines = [];
foreach (Property::query()->orderBy('name')->get() as $property) {
foreach ((array) ($property->process_overrides ?? []) as $key => $value) {
$setting = ProcessSetting::tryFrom((string) $key);
if ($setting !== null && $setting->normalize($value) !== null) {
$lines[] = e($property->name).': '.e($setting->question()).' → <strong>'.e($setting->describe($setting->normalize($value))).'</strong>';
}
}
}
foreach (Channel::query()->with('property')->orderBy('name')->get() as $channel) {
foreach ((array) ($channel->process_overrides ?? []) as $intake => $values) {
foreach ((array) $values as $key => $value) {
$setting = ProcessSetting::tryFrom((string) $key);
if ($setting !== null && $setting->normalize($value) !== null) {
$lines[] = e($channel->name.' ('.$channel->property?->name.', '.(IntakeKind::tryFrom((string) $intake)?->getLabel() ?? $intake).')').': '.e($setting->question()).' → <strong>'.e($setting->describe($setting->normalize($value))).'</strong>';
}
}
}
}
return new HtmlString($lines === [] ? 'Keine – überall gelten Ihre Standardeinstellungen.' : '<ul class="list-disc ps-5 space-y-1"><li>'.implode('</li><li>', $lines).'</li></ul>');
}
private function tenant(): Tenant
{
$tenant = Settings::tenant();
abort_if($tenant === null, 404);
return $tenant;
}
}