Files
fewo-stommer/app/Documents/ContractPlaceholders.php
T
LarsandClaude Sonnet 5.5 0c4187edaf
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m34s
Stornoregeln mit automatischer Kostenberechnung, Storno-Verwaltung, Vertragsanlagen
- Stornoregel (Stufen nach Tagen vor Anreise, Mietpreis oder Gesamtpreis, Ersatzmieter-Gebuehr), je Vertragsvorlage,
  beim Angebot eingefroren, als Platzhalter im Vertrag
- Stornierung (Gast-Link oder Verwaltung) berechnet die Kosten, legt einen Stornofall an; Verwaltung Stornierungen
  mit Vorschlag, Festlegung, Saldo, Abrechnung und Erlass; Gast sieht die Kosten vorab auf der Storno-Seite
- Vertragsanlagen als eigene versionierte Dokumente mit Bedingung (immer, Zusatzleistung, Portal, von Hand),
  je Angebot ausgewaehlt und eingefroren; Zusatzleistungen in der Buchung erfassbar
- FeWo-direkt-Installation legt die Stornoregel aus Paragraf 13 an

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 16:32:48 +01:00

137 lines
6.0 KiB
PHP
Raw 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' => (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 ?? '',
];
$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;
}
}