Vertragsvorlagen als ganzes Dokument mit Platzhaltern, Wohnungsdaten, Portal-Zuordnung
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m38s
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m38s
- Vorlage = kompletter Vertragstext plus Anlage mit Platzhaltern ({{ mieter }}, {{ anreise }}, {{ wohnung.x }}),
eigene Vertrags-PDF, Anzeige auf der Angebotsseite; Text und Wohnungsdaten werden beim Angebot eingefroren
- Wohnungsdaten (WLAN, Codes, ...) als Vertragsdaten der Wohnung statt im Vorlagentext
- Je Portal eigene Vorlage fuer Anfragen und fuer beim Portal verbindliche Buchungen; direkte Bestaetigung
nutzt dieselbe Aufloesung; Bestaetigungsmail ohne Zahlungsanweisung bei Portalzahlung
- Befehl contracts:install-fewo-direkt legt die FeWo-direkt-Vorlage aus dem Vertragstext des Vermieters an
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a73b164ea1
commit
89d569b6c8
17 files changed
+648
-28
No files matched your search
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Documents;
|
||||
|
||||
use App\Models\Booking;
|
||||
use App\Support\Money;
|
||||
|
||||
/**
|
||||
* Placeholders of contract documents, written as {{ name }} in the template text. Values are taken from the
|
||||
* booking and the facts of the property ({{ wohnung.wlan_passwort }}); they are HTML-escaped. A placeholder that
|
||||
* cannot be filled stays visible as [name?] so that it is never silently missing in a contract.
|
||||
*/
|
||||
class ContractPlaceholders
|
||||
{
|
||||
private const PATTERN = '/\{\{\s*([a-z][a-z0-9_]*(?:\.[a-z0-9_]+)?)\s*\}\}/i';
|
||||
|
||||
/**
|
||||
* Placeholders with their meaning, for the template editor.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function catalog(): array
|
||||
{
|
||||
return [
|
||||
'mieter' => 'Name des Gastes',
|
||||
'mieter_anschrift' => 'Anschrift des Gastes',
|
||||
'mieter_email' => 'E-Mail des Gastes',
|
||||
'anreise' => 'Anreisetag mit Wochentag, z. B. Samstag, 07.11.2026',
|
||||
'abreise' => 'Abreisetag mit Wochentag',
|
||||
'anreise_datum' => 'Anreisedatum, z. B. 07.11.2026',
|
||||
'abreise_datum' => 'Abreisedatum',
|
||||
'naechte' => 'Anzahl der Nächte',
|
||||
'personen' => 'Anzahl der Personen',
|
||||
'gesamtpreis' => 'Gesamtpreis, z. B. 1.234,00 €',
|
||||
'anzahlung' => 'Anzahlung',
|
||||
'restzahlung' => 'Restzahlung',
|
||||
'restzahlung_faellig' => 'Fälligkeit der Restzahlung',
|
||||
'buchungsnummer' => 'Buchungs-/Angebotsnummer',
|
||||
'datum' => 'Datum des Vertrags',
|
||||
'vermieter' => 'Name des Vermieters',
|
||||
'vermieter_anschrift' => 'Anschrift des Vermieters',
|
||||
'wohnung' => 'Name der Wohnung',
|
||||
'wohnung_adresse' => 'Adresse der Wohnung',
|
||||
'checkin' => 'Anreisezeit (Uhr)',
|
||||
'checkout' => 'Abreisezeit (Uhr)',
|
||||
'portal' => 'Name des Portals (leer bei eigener Buchung)',
|
||||
'wohnung.<name>' => 'Eigene Wohnungsdaten (Wohnung bearbeiten › Vertragsdaten), z. B. wohnung.wlan_passwort',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $propertyData facts of the property as frozen in the offer
|
||||
*/
|
||||
public static function render(string $html, Booking $booking, array $propertyData = []): string
|
||||
{
|
||||
$values = self::values($booking, $propertyData);
|
||||
|
||||
return (string) preg_replace_callback(self::PATTERN, function (array $match) use ($values): string {
|
||||
$key = strtolower($match[1]);
|
||||
|
||||
return array_key_exists($key, $values) && $values[$key] !== null ? e((string) $values[$key]) : '[<b>'.e($key).'?</b>]';
|
||||
}, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Placeholders in the text that have no value for this booking.
|
||||
*
|
||||
* @param array<string, mixed> $propertyData
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function missing(string $html, Booking $booking, array $propertyData = []): array
|
||||
{
|
||||
$values = self::values($booking, $propertyData);
|
||||
preg_match_all(self::PATTERN, $html, $found);
|
||||
|
||||
return array_values(array_unique(array_filter(array_map('strtolower', $found[1]), fn (string $key): bool => ! isset($values[$key]))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $propertyData
|
||||
* @return array<string, ?string>
|
||||
*/
|
||||
private static function values(Booking $booking, array $propertyData): array
|
||||
{
|
||||
$booking->loadMissing(['guest', 'property', 'importChannel', 'originChannel']);
|
||||
$guest = $booking->guest;
|
||||
$owner = OfferTerms::owner($booking);
|
||||
$property = $booking->property;
|
||||
$address = trim(implode(', ', array_filter([$guest?->street, trim(($guest?->postal_code ?? '').' '.($guest?->city ?? ''))])));
|
||||
|
||||
$values = [
|
||||
'mieter' => $guest?->full_name,
|
||||
'mieter_anschrift' => $address ?: null,
|
||||
'mieter_email' => $guest?->email,
|
||||
'anreise' => $booking->arrival->translatedFormat('l, d.m.Y'),
|
||||
'abreise' => $booking->departure->translatedFormat('l, d.m.Y'),
|
||||
'anreise_datum' => $booking->arrival->format('d.m.Y'),
|
||||
'abreise_datum' => $booking->departure->format('d.m.Y'),
|
||||
'naechte' => (string) $booking->nights(),
|
||||
'personen' => (string) $booking->guests,
|
||||
'gesamtpreis' => Money::format($booking->total),
|
||||
'anzahlung' => Money::format($booking->deposit),
|
||||
'restzahlung' => Money::format($booking->total - $booking->deposit),
|
||||
'restzahlung_faellig' => $booking->balance_due_on?->format('d.m.Y'),
|
||||
'buchungsnummer' => $booking->reference,
|
||||
'datum' => ($booking->confirmed_at ?? $booking->offer_sent_at ?? now())->format('d.m.Y'),
|
||||
'vermieter' => $owner['name'] ?? null,
|
||||
'vermieter_anschrift' => trim(($owner['street'] ?? '').', '.($owner['city'] ?? ''), ', ') ?: null,
|
||||
'wohnung' => $property->name,
|
||||
'wohnung_adresse' => $property->address,
|
||||
'checkin' => $property->check_in_time ? substr((string) $property->check_in_time, 0, 5) : null,
|
||||
'checkout' => $property->check_out_time ? substr((string) $property->check_out_time, 0, 5) : null,
|
||||
'portal' => ($booking->importChannel ?? $booking->originChannel)?->name ?? '',
|
||||
];
|
||||
|
||||
foreach ($propertyData as $name => $value) {
|
||||
$values['wohnung.'.strtolower((string) $name)] = is_scalar($value) ? (string) $value : null;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Documents;
|
||||
|
||||
use App\Enums\ChannelBookingModel;
|
||||
use App\Models\Booking;
|
||||
use App\Models\ContractTemplate;
|
||||
use App\Support\Content;
|
||||
@@ -31,13 +32,15 @@ class OfferTerms
|
||||
/**
|
||||
* @return array{captured_at: string, template: ?array{id: int, name: string, version: int}, texts: array<string, ?string>, deposit_due_days: int, owner: array<string, mixed>, bank: array<string, mixed>}
|
||||
*/
|
||||
public static function capture(?ContractTemplate $template = null): array
|
||||
public static function capture(?ContractTemplate $template = null, ?Booking $booking = null): array
|
||||
{
|
||||
$overrides = $template?->overrides() ?? [];
|
||||
|
||||
return [
|
||||
'captured_at' => now()->toIso8601String(),
|
||||
'template' => $template ? ['id' => $template->id, 'name' => $template->name, 'version' => $template->version] : null,
|
||||
'document' => $template?->hasDocument() ? ['body' => $template->body, 'annex_title' => $template->annex_title, 'annex_body' => $template->annex_body] : null,
|
||||
'property_data' => (array) ($booking?->property?->contract_data ?? []),
|
||||
'texts' => collect(self::TEXT_KEYS)->mapWithKeys(fn (string $key): array => [$key => $overrides[$key] ?? Content::get($key)])->all(),
|
||||
'deposit_due_days' => (int) config('fewo.deposit_due_days'),
|
||||
'owner' => (array) config('fewo.owner'),
|
||||
@@ -45,6 +48,39 @@ public static function capture(?ContractTemplate $template = null): array
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The contract template for a booking: chosen explicitly, else the portal's (for bookings that are binding at the
|
||||
* portal its reservation template, otherwise its offer template), else the property's.
|
||||
*/
|
||||
public static function templateFor(Booking $booking, int|string|null $explicitId = null): ?ContractTemplate
|
||||
{
|
||||
$channel = $booking->importChannel ?? $booking->originChannel;
|
||||
$portalId = $channel === null ? null : ($channel->booking_model === ChannelBookingModel::PortalBooking
|
||||
? ($channel->contract_template_reservation_id ?? $channel->contract_template_id)
|
||||
: $channel->contract_template_id);
|
||||
$id = filled($explicitId) ? (int) $explicitId : ($portalId ?? $booking->property->contract_template_id);
|
||||
|
||||
return $id ? ContractTemplate::query()->find($id) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The contract as a complete document (placeholders filled), or null when the offer uses the text blocks.
|
||||
*
|
||||
* @return array{body: string, annex_title: ?string, annex: ?string}|null
|
||||
*/
|
||||
public static function document(Booking $booking): ?array
|
||||
{
|
||||
$document = $booking->offer_terms['document'] ?? null;
|
||||
if (! is_array($document) || blank($document['body'] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = (array) ($booking->offer_terms['property_data'] ?? []);
|
||||
$render = fn (?string $html): ?string => filled($html) ? (string) RichContentRenderer::make(ContractPlaceholders::render($html, $booking, $data))->toHtml() : null;
|
||||
|
||||
return ['body' => (string) $render($document['body']), 'annex_title' => $document['annex_title'] ?? null, 'annex' => $render($document['annex_body'] ?? null)];
|
||||
}
|
||||
|
||||
public static function text(Booking $booking, string $key): ?string
|
||||
{
|
||||
$texts = $booking->offer_terms['texts'] ?? null;
|
||||
|
||||
@@ -33,10 +33,12 @@ public function generate(Booking $booking, bool $draft = false): string
|
||||
return $booking->contract_path;
|
||||
}
|
||||
|
||||
$pdf = Pdf::loadView('pdf.contract', [
|
||||
$document = OfferTerms::document($booking);
|
||||
$pdf = Pdf::loadView($document !== null ? 'pdf.contract-document' : 'pdf.contract', [
|
||||
'booking' => $booking,
|
||||
'draft' => $draft,
|
||||
'logo' => $logo,
|
||||
'document' => $document,
|
||||
])->setPaper('a4')->output();
|
||||
Storage::disk(self::DISK)->put($path, $pdf);
|
||||
$booking->update([$draft ? 'offer_path' : 'contract_path' => $path, $draft ? 'offer_pdf_sha256' : 'contract_pdf_sha256' => hash('sha256', $pdf)]);
|
||||
|
||||
Reference in new issue
Block a user