Files
fewo-stommer/app/Filament/Forms/DocumentCompositionFields.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

111 lines
4.2 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\OfferMade;
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::ExternalRecord => (new BookingDocumentation($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: $composition->kind === DocumentKind::Offer);
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>';
}
}
}