Files
fewo-stommer/app/Documents/OfferTerms.php
T
LarsandClaude Sonnet 5.5 a73b164ea1
Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m36s
Zahlungsaufgaben, Freigabe-/Storno-Link, Vertragsvorlagen, Anreise-Mail
- Zahlungen werden nicht mehr automatisch angemahnt: faellige Anzahlung/Restzahlung werden Aufgaben
  (Zahlungen & Wuensche, Tages-/Wochenuebersicht); Mail nur per Klick; je Portal und Buchungstyp abschaltbar
- Freigabe-Link in der Eingangsbestaetigung, Storno-Link in der Buchungsbestaetigung; je Wohnung
  Wunsch / sofort / sofort bis Frist; Stornowuensche und Kosten bleiben Aufgabe des Gastgebers
- Vertragsvorlagen mit Versionen (je Angebot, Portal oder Wohnung waehlbar), benutzte Versionen gesperrt;
  Angebot und finaler Vertrag als unveraenderliche PDF mit SHA-256, Vertrag wird nie neu geschrieben
- Anreise-Mail je Wohnung einstellbar (Standard 3 Tage), neue Textfelder Schluessel/Parken/Ankunftstag

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 12:46:29 +01:00

116 lines
4.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\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): array
{
$overrides = $template?->overrides() ?? [];
return [
'captured_at' => now()->toIso8601String(),
'template' => $template ? ['id' => $template->id, 'name' => $template->name, 'version' => $template->version] : null,
'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'),
];
}
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;
}
}