Files
fewo-stommer/app/Documents/ContractPlaceholders.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

137 lines
6.2 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\Documents;
use App\Models\Booking;
use App\Models\CancellationPolicy;
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
{
/** Placeholders whose value is already safe HTML. */
private const RAW = ['storno_regel'];
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)',
'storno_regel' => 'Die Stornoregel dieser Vorlage als Aufzählung (Stufen und Prozent)',
'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]);
if (! array_key_exists($key, $values) || $values[$key] === null) {
return '[<b>'.e($key).'?</b>]';
}
return in_array($key, self::RAW, true) ? (string) $values[$key] : e((string) $values[$key]);
}, $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' => $booking->guests !== null ? (string) $booking->guests : null,
'gesamtpreis' => $booking->total !== null ? Money::format($booking->total) : null,
'anzahlung' => $booking->deposit !== null ? Money::format($booking->deposit) : null,
'restzahlung' => $booking->total !== null && $booking->deposit !== null ? Money::format($booking->total - $booking->deposit) : null,
'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 ?? '',
];
$policy = $booking->offer_terms['cancellation_policy'] ?? null;
if (is_array($policy) && ! empty($policy['tiers'])) {
$values['storno_regel'] = '<ul>'.collect(CancellationPolicy::describe($policy))->map(fn (string $line): string => '<li>'.e($line).'</li>')->implode('').'</ul>';
}
foreach ($propertyData as $name => $value) {
$values['wohnung.'.strtolower((string) $name)] = is_scalar($value) ? (string) $value : null;
}
return $values;
}
}