Vermieterdaten, Bank, Steuerangabe, Adressen, Betriebsart und Fristen sind
Spalten von `tenants`. `App\Tenancy\TenantConfig` legt sie über
`config('fewo.*')` und `mail.from`, sobald der Mandantenkontext wechselt –
so lesen Vorlagen, Mails und der Mietvertrag weiter dieselben Schlüssel und
`resources/themes/**` bleibt unangetastet.
Die `.env` ist die Grundlinie: Rückfall, solange kein Mandant feststeht, und
Grenze für Löschfristen und Bot-Schutz (nur verschärfen, nicht lockern). Die
Flutgrenze bleibt global. Bank, Steuerangabe und eigene Dienst-Schlüssel
liegen verschlüsselt.
Die Seite System › Betrieb schreibt jetzt den Mandanten und enthält
zusätzlich Vermieterdaten, Bank und die Fristen. Tabelle `settings` und
Modell `Setting` entfallen; die Migration übernimmt die bisherigen Werte
aus `settings` und der Konfiguration in den Mandanten Stommer.
Neu: `tenant_services` (Freischaltung je Dienst mit optionalem eigenem
API-Schlüssel, nur im Betreiber-Bereich schaltbar) und `service_usages`.
Es zählt noch nichts, weil kein Dienst aufruft.
235 Feature-Tests grün. Der Umbauweg wurde zusätzlich auf einer Kopie der
lokalen Datenbank durchgespielt.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
282 lines
13 KiB
PHP
282 lines
13 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Enums\UserRole;
|
||
use App\Filament\Concerns\FollowsAdminMode;
|
||
use App\Models\Tenant;
|
||
use App\Support\Settings;
|
||
use App\Tenancy\TenantConfig;
|
||
use BackedEnum;
|
||
use Filament\Actions\Action;
|
||
use Filament\Forms\Components\Radio;
|
||
use Filament\Forms\Components\TagsInput;
|
||
use Filament\Forms\Components\TextInput;
|
||
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\Components\Utilities\Get;
|
||
use Filament\Schemas\Schema;
|
||
use Filament\Support\Icons\Heroicon;
|
||
use UnitEnum;
|
||
|
||
/**
|
||
* Operating data of the landlord: operating mode (test mode sends every e-mail to one test recipient only;
|
||
* live delivers to the real recipients), the addresses of the internal communication, the landlord's own
|
||
* address and bank details, and the periods. Administrators only.
|
||
*/
|
||
class Operations extends Page
|
||
{
|
||
use FollowsAdminMode;
|
||
|
||
/** Periods and counters the landlord may set (column => label). Empty means the system default. */
|
||
private const PERIODS = [
|
||
'deposit_due_days' => 'Anzahlung fällig nach (Tage)',
|
||
'review_request_after_days' => 'Bewertungsbitte nach Abreise (Tage)',
|
||
'guest_retention_years' => 'Gästedaten löschen nach (Jahre)',
|
||
'retention_inquiry_months' => 'Nicht gebuchte Anfragen löschen nach (Monate)',
|
||
'retention_portal_email_excerpt_days' => 'Portal-Mail-Auszug löschen nach (Tage)',
|
||
'retention_attribution_months' => 'Herkunftsangaben löschen nach (Monate)',
|
||
'spam_min_seconds' => 'Bot-Schutz: Mindestzeit im Formular (Sekunden)',
|
||
'spam_per_ip_per_hour' => 'Bot-Schutz: Anfragen je IP und Stunde',
|
||
'spam_per_email_per_day' => 'Bot-Schutz: Anfragen je E-Mail und Tag',
|
||
'spam_open_requests_per_email' => 'Bot-Schutz: gleichzeitig offene Anfragen je E-Mail',
|
||
'spam_max_links' => 'Bot-Schutz: erlaubte Links im Text',
|
||
'channel_stale_after_minutes' => 'Portal-Abgleich gilt als veraltet nach (Minuten)',
|
||
'channel_task_reminder_after_minutes' => 'Erinnerung an Portal-Aufgaben nach (Minuten)',
|
||
];
|
||
|
||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedPower;
|
||
|
||
protected static string|UnitEnum|null $navigationGroup = 'System';
|
||
|
||
protected static ?string $navigationLabel = 'Betrieb';
|
||
|
||
protected static ?string $title = 'Betrieb';
|
||
|
||
protected static ?string $slug = 'betrieb';
|
||
|
||
protected static ?int $navigationSort = 1;
|
||
|
||
/** @var array<string, mixed> */
|
||
public array $data = [];
|
||
|
||
public static function canAccess(): bool
|
||
{
|
||
return (bool) auth()->user()?->hasRole(UserRole::Admin);
|
||
}
|
||
|
||
public function mount(): void
|
||
{
|
||
$tenant = $this->tenant();
|
||
|
||
$this->form->fill([
|
||
'mode' => $tenant->operating_mode ?: Settings::MODE_TEST,
|
||
'test_recipient' => $tenant->test_recipient ?: auth()->user()?->email,
|
||
'notification_recipients' => $tenant->notificationRecipients(),
|
||
'contact_email' => $tenant->contact_email,
|
||
'sender_name' => $tenant->sender_name,
|
||
'sender_email' => $tenant->sender_email,
|
||
'owner_name' => $tenant->owner_name,
|
||
'owner_street' => $tenant->owner_street,
|
||
'owner_city' => $tenant->owner_city,
|
||
'owner_phone' => $tenant->owner_phone,
|
||
'bank_account_holder' => $tenant->bank_account_holder,
|
||
'bank_name' => $tenant->bank_name,
|
||
'bank_iban' => $tenant->bank_iban,
|
||
'bank_bic' => $tenant->bank_bic,
|
||
'tax_note' => $tenant->tax_note,
|
||
...$this->periodValues($tenant),
|
||
]);
|
||
}
|
||
|
||
public function form(Schema $schema): Schema
|
||
{
|
||
return $schema
|
||
->statePath('data')
|
||
->components([
|
||
Section::make('E-Mail-Versand')
|
||
->description('Im Testbetrieb gehen alle E-Mails – Eingangsbestätigungen, Angebote, Bestätigungen, Erinnerungen, Benachrichtigungen – ausschließlich an die Testadresse, egal welche Adresse auf der Webseite eingetragen wurde.')
|
||
->schema([
|
||
Radio::make('mode')
|
||
->label('Betriebsart')
|
||
->options([
|
||
Settings::MODE_TEST => 'Testbetrieb – alle E-Mails an die Testadresse',
|
||
Settings::MODE_LIVE => 'Scharf geschaltet – E-Mails an Gäste und Vermieterin',
|
||
])
|
||
->required()
|
||
->live(),
|
||
TextInput::make('test_recipient')
|
||
->label('Testadresse')
|
||
->email()
|
||
->required(fn (Get $get): bool => $get('mode') === Settings::MODE_TEST)
|
||
->visible(fn (Get $get): bool => $get('mode') === Settings::MODE_TEST),
|
||
Text::make(fn (): string => filled(config('fewo.mail_redirect_to'))
|
||
? 'Hinweis: Auf diesem Server ist zusätzlich FEWO_MAIL_REDIRECT_TO='.config('fewo.mail_redirect_to').' gesetzt – diese Umleitung gilt immer, auch wenn hier scharf geschaltet ist.'
|
||
: ''),
|
||
]),
|
||
Section::make('Adressen der internen Kommunikation')
|
||
->description('Gelten sofort für alle E-Mails, die Webseite und den Mietvertrag. Im Testbetrieb gehen trotzdem alle E-Mails nur an die Testadresse.')
|
||
->columns(2)
|
||
->schema([
|
||
TagsInput::make('notification_recipients')
|
||
->label('Benachrichtigungen an')
|
||
->helperText('Neue Anfragen, angenommene/abgelehnte Angebote, abgelaufene Fristen, Bewertungen, Portal-Mails und -Aufgaben, Kopie der Zahlungserinnerungen. Mehrere Adressen möglich (Enter nach jeder Adresse).')
|
||
->placeholder('adresse@beispiel.de')
|
||
->nestedRecursiveRules(['email'])
|
||
->required()
|
||
->columnSpanFull(),
|
||
TextInput::make('contact_email')
|
||
->label('Kontakt- und Antwortadresse')
|
||
->helperText('Steht auf der Webseite und im Mietvertrag; Antworten der Gäste auf unsere E-Mails gehen hierhin.')
|
||
->email()
|
||
->required(),
|
||
TextInput::make('sender_name')
|
||
->label('Absendername')
|
||
->required()
|
||
->maxLength(100),
|
||
TextInput::make('sender_email')
|
||
->label('Absenderadresse')
|
||
->helperText(fn (): string => 'Muss das Postfach sein, mit dem der Server versendet (SMTP-Anmeldung: '.(config('mail.mailers.smtp.username') ?: 'nicht gesetzt').') – sonst lehnt Strato den Versand ab.')
|
||
->email()
|
||
->required(),
|
||
Text::make(fn (Get $get): string => filled(config('mail.mailers.smtp.username')) && filled($get('sender_email')) && strcasecmp((string) $get('sender_email'), (string) config('mail.mailers.smtp.username')) !== 0
|
||
? '⚠ Die Absenderadresse weicht vom SMTP-Postfach ('.config('mail.mailers.smtp.username').') ab. Strato stellt solche E-Mails in der Regel nicht zu.'
|
||
: '')
|
||
->columnSpanFull(),
|
||
]),
|
||
Section::make('Vermieterin')
|
||
->description('Steht in E-Mails, im Angebot, im Mietvertrag und auf der Webseite.')
|
||
->columns(2)
|
||
->schema([
|
||
TextInput::make('owner_name')->label('Name')->required()->maxLength(255),
|
||
TextInput::make('owner_phone')->label('Telefon')->maxLength(50),
|
||
TextInput::make('owner_street')->label('Straße und Hausnummer')->maxLength(255),
|
||
TextInput::make('owner_city')->label('PLZ und Ort')->maxLength(255),
|
||
]),
|
||
Section::make('Bankverbindung und Steuer')
|
||
->description('Steht im Mietvertrag und in den Zahlungsaufforderungen. Die Angaben liegen verschlüsselt in der Datenbank.')
|
||
->columns(2)
|
||
->schema([
|
||
TextInput::make('bank_account_holder')->label('Kontoinhaber')->maxLength(255),
|
||
TextInput::make('bank_name')->label('Bank')->maxLength(255),
|
||
TextInput::make('bank_iban')->label('IBAN')->maxLength(40),
|
||
TextInput::make('bank_bic')->label('BIC')->maxLength(20),
|
||
TextInput::make('tax_note')
|
||
->label('Steuerangabe')
|
||
->helperText('Zum Beispiel die Steuernummer oder der Hinweis auf die Kleinunternehmerregelung.')
|
||
->maxLength(255)
|
||
->columnSpanFull(),
|
||
]),
|
||
Section::make('Fristen und Grenzen')
|
||
->description('Leer bedeutet: die Vorgabe des Systems gilt. Löschfristen und Bot-Schutz lassen sich verschärfen, aber nicht über die Vorgabe hinaus lockern.')
|
||
->columns(3)
|
||
->collapsed()
|
||
->schema($this->periodFields()),
|
||
]);
|
||
}
|
||
|
||
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
|
||
{
|
||
$data = $this->form->getState();
|
||
$tenant = $this->tenant();
|
||
$wasLive = $tenant->isLive();
|
||
|
||
$tenant->update([
|
||
'operating_mode' => $data['mode'],
|
||
'test_recipient' => $data['test_recipient'] ?? $tenant->test_recipient,
|
||
'notification_emails' => implode(', ', Settings::addressList(implode(',', $data['notification_recipients'] ?? []))),
|
||
'contact_email' => $data['contact_email'],
|
||
'sender_name' => $data['sender_name'],
|
||
'sender_email' => $data['sender_email'],
|
||
'owner_name' => $data['owner_name'],
|
||
'owner_street' => $data['owner_street'] ?? null,
|
||
'owner_city' => $data['owner_city'] ?? null,
|
||
'owner_phone' => $data['owner_phone'] ?? null,
|
||
'bank_account_holder' => $data['bank_account_holder'] ?? null,
|
||
'bank_name' => $data['bank_name'] ?? null,
|
||
'bank_iban' => $data['bank_iban'] ?? null,
|
||
'bank_bic' => $data['bank_bic'] ?? null,
|
||
'tax_note' => $data['tax_note'] ?? null,
|
||
...array_map(fn (mixed $value): ?int => filled($value) ? (int) $value : null, $this->periodInput($data)),
|
||
]);
|
||
|
||
app(TenantConfig::class)->refresh();
|
||
|
||
if ($data['mode'] === Settings::MODE_LIVE && ! $wasLive) {
|
||
Notification::make()->title('Scharf geschaltet')->body('Ab jetzt erhalten Gäste und Vermieterin die E-Mails direkt.')->warning()->send();
|
||
} else {
|
||
Notification::make()->title('Gespeichert')->success()->send();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The landlord this page edits: the one of the request, else the only one there is.
|
||
*/
|
||
private function tenant(): Tenant
|
||
{
|
||
$tenant = Settings::tenant();
|
||
|
||
abort_if($tenant === null, 404);
|
||
|
||
return $tenant;
|
||
}
|
||
|
||
/**
|
||
* @return array<string, ?int>
|
||
*/
|
||
private function periodValues(Tenant $tenant): array
|
||
{
|
||
return array_combine(array_keys(self::PERIODS), array_map(fn (string $column): ?int => $tenant->{$column}, array_keys(self::PERIODS)));
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $data
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function periodInput(array $data): array
|
||
{
|
||
return array_combine(array_keys(self::PERIODS), array_map(fn (string $column): mixed => $data[$column] ?? null, array_keys(self::PERIODS)));
|
||
}
|
||
|
||
/**
|
||
* @return list<TextInput>
|
||
*/
|
||
private function periodFields(): array
|
||
{
|
||
return array_map(function (string $column) {
|
||
$field = TextInput::make($column)
|
||
->label(self::PERIODS[$column])
|
||
->numeric()
|
||
->integer()
|
||
->minValue(0);
|
||
|
||
$config = app(TenantConfig::class);
|
||
|
||
if (($ceiling = $config->ceilingFor($column)) !== null) {
|
||
return $field->maxValue($ceiling)->helperText('Vorgabe des Systems: höchstens '.$ceiling);
|
||
}
|
||
|
||
if (($floor = $config->floorFor($column)) !== null) {
|
||
return $field->minValue($floor)->helperText('Vorgabe des Systems: mindestens '.$floor);
|
||
}
|
||
|
||
return $field;
|
||
}, array_keys(self::PERIODS));
|
||
}
|
||
}
|