Files
fewo-stommer/app/Documents/OfferTerms.php
T
LarsandClaude Sonnet 5.5 89d569b6c8
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m38s
Vertragsvorlagen als ganzes Dokument mit Platzhaltern, Wohnungsdaten, Portal-Zuordnung
- 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>
2026-10-30 16:04:30 +01:00

152 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\Enums\ChannelBookingModel;
use App\Models\Booking;
use App\Models\ContractTemplate;
use App\Support\Content;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Illuminate\Support\HtmlString;
/**
* The terms of one offer version, frozen when the offer is made (bookings.offer_terms): contract texts,
* payment period and landlord/bank data. Offer page, PDFs, acceptance and confirmation read from this snapshot,
* so later changes of the website texts never alter an offer the guest already received.
* Bookings without a snapshot (created before it existed) fall back to the live texts.
*/
class OfferTerms
{
/** Website text keys that form part of the contract. */
public const TEXT_KEYS = [
'contract.intro',
'contract.terms',
'contract.closing',
'stay.cancellation',
'stay.house_rules',
'offer.contract_note',
'offer.accept_note',
'offer.accept_button',
];
/**
* @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, ?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'),
'bank' => (array) config('fewo.bank'),
];
}
/**
* 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;
return is_array($texts) ? ($texts[$key] ?? null) : Content::get($key);
}
public static function html(Booking $booking, string $key): HtmlString
{
$value = self::text($booking, $key);
return new HtmlString(filled($value) ? RichContentRenderer::make($value)->toHtml() : '');
}
public static function depositDueDays(Booking $booking): int
{
return (int) ($booking->offer_terms['deposit_due_days'] ?? config('fewo.deposit_due_days'));
}
/**
* @return array<string, mixed>
*/
public static function owner(Booking $booking): array
{
return $booking->offer_terms['owner'] ?? (array) config('fewo.owner');
}
/**
* @return array<string, mixed>
*/
public static function bank(Booking $booking): array
{
return $booking->offer_terms['bank'] ?? (array) config('fewo.bank');
}
/**
* Fingerprint of everything the guest agrees to in this offer version. The validity (hold_expires_at) is
* not part of it – the landlord may extend it – but is checked separately.
*/
public static function hash(Booking $booking): string
{
return hash('sha256', json_encode(self::canonical([
'version' => $booking->offer_version,
'arrival' => $booking->arrival->toDateString(),
'departure' => $booking->departure->toDateString(),
'guests' => $booking->guests,
'quote' => $booking->quote,
'total' => $booking->total,
'deposit' => $booking->deposit,
'balance_due_on' => $booking->balance_due_on->toDateString(),
'terms' => $booking->offer_terms,
]), JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
}
private static function canonical(mixed $value): mixed
{
if (! is_array($value)) {
return $value;
}
$value = array_map(self::canonical(...), $value);
if (! array_is_list($value)) {
ksort($value);
}
return $value;
}
}