Files
fewo-stommer/app/Filament/Forms/DocumentCompositionFields.php
T
LarsandClaude Opus 5.5 40d20cb7d1 Pakete 7 und 9: Auskunft und Buchungswunsch, Aufenthalt und Abschluss (Zwischenstand)
- Haltefrist 0: unverbindliche Auskunft mit signiertem Link "Buchung anfragen",
  erneute Prüfung unter Sperre, automatische oder manuelle Bestätigung, Absage
  bei vergebenem Zeitraum, Ablauf unbeantworteter Auskünfte
- Aufenthalt: Anreisedaten, Aufgaben je Wohnung, gebuchte Leistungen,
  Abweichungen und Folgeaufgaben als Aufgaben am Vorgang
- Abschluss getrennt: Aufenthalt abgeschlossen / kaufmännisch abgeschlossen,
  Nachläufer (Forderung, Auszahlung) bleiben sichtbar
- Zahlungsstatus nur aus Geld auf unserem Konto; Altbestand per Vorschau-Befehl
- Verlauf weist Testzustellungen als solche aus
- Abnahmetests T16-T25, T23 über isolierten SMTP-Empfänger

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

115 lines
4.5 KiB
PHP

<?php
namespace App\Filament\Forms;
use App\Documents\DocumentComposer;
use App\Documents\DocumentComposition;
use App\Documents\RentalContract;
use App\Enums\AdminMode;
use App\Enums\DocumentKind;
use App\Mail\BookingConfirmed;
use App\Mail\BookingDocumentation;
use App\Mail\InformationSent;
use App\Mail\OfferMade;
use App\Mail\StayConfirmed;
use App\Models\Booking;
use App\Models\User;
use Closure;
use Filament\Forms\Components\Checkbox;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\View;
use Throwable;
/**
* The composition of a document inside an action modal: the same picture for offer, confirmation and booking
* documentation, recalculated whenever the landlord adjusts something, plus the explicit confirmation that is
* required when there is something to check.
*/
class DocumentCompositionFields
{
/** @var array<string, DocumentComposition> */
private static array $compositions = [];
/**
* @param Closure(Get): array<string, mixed> $changes the adjustments currently in the form
* @return list<View|Checkbox>
*/
public static function make(Booking $booking, DocumentKind $kind, Closure $changes, ?Closure $visible = null): array
{
$compose = fn (Get $get): DocumentComposition => self::compose($booking, $kind, $changes($get));
return [
View::make('filament.document-composition')
->viewData(fn (Get $get): array => self::viewData($compose($get), $changes($get))),
Checkbox::make('acknowledged')
->label('Ich habe die Hinweise geprüft und möchte das Dokument so senden.')
->visible(fn (Get $get): bool => ($visible === null || $visible($get)) && $compose($get)->toConfirm() !== [])
->accepted(),
];
}
/**
* One composition per form state and request (the view and the confirmation box both need it).
*
* @param array<string, mixed> $changes
*/
public static function compose(Booking $booking, DocumentKind $kind, array $changes): DocumentComposition
{
$key = $booking->id.':'.$kind->value.':'.md5(serialize($changes)).':'.$booking->updated_at?->getTimestamp();
return self::$compositions[$key] ??= app(DocumentComposer::class)->compose($booking, $kind, $changes);
}
/**
* @param array<string, mixed> $changes
* @return array<string, mixed>
*/
private static function viewData(DocumentComposition $composition, array $changes): array
{
$user = auth()->user();
return [
'composition' => $composition,
'expert' => $user instanceof User && $user->adminMode() === AdminMode::Expert,
'mailPreview' => self::mailPreview($composition),
'documentPreview' => self::documentPreview($composition),
'pdfUrl' => route('filament.admin.document-preview', ['booking' => $composition->draft->getRouteKey(), 'kind' => $composition->kind->value, 'changes' => base64_encode(json_encode($changes) ?: '[]')]),
];
}
private static function mailPreview(DocumentComposition $composition): string
{
$draft = $composition->draft;
if ($draft->guest === null) {
return '<p style="font-family:sans-serif">Keine Vorschau: Der Gast ist noch nicht bekannt.</p>';
}
try {
return match ($composition->kind) {
DocumentKind::Offer => (new OfferMade($draft, '#'))->render(),
DocumentKind::Information => (new InformationSent($draft, '#'))->render(),
DocumentKind::ExternalRecord => (new BookingDocumentation($draft))->render(),
DocumentKind::StayConfirmation => (new StayConfirmed($draft))->render(),
default => (new BookingConfirmed($draft))->render(),
};
} catch (Throwable $exception) {
report($exception);
return '<p style="font-family:sans-serif">Die Vorschau der E-Mail ist gerade nicht möglich.</p>';
}
}
private static function documentPreview(DocumentComposition $composition): string
{
try {
[$view, $data] = app(RentalContract::class)->view($composition->draft, $composition->kind, draft: in_array($composition->kind, [DocumentKind::Offer, DocumentKind::Information], true));
return view($view, $data)->render();
} catch (Throwable $exception) {
report($exception);
return '<p style="font-family:sans-serif">Die Vorschau des Dokuments ist gerade nicht möglich.</p>';
}
}
}