, deposit_due_days: int, owner: array, bank: array} */ public static function capture(?ContractTemplate $template = null, ?Booking $booking = null, ?array $annexIds = null): array { $overrides = $template?->overrides() ?? []; $annexes = $annexIds !== null ? ContractAnnex::query()->whereIn('id', $annexIds)->orderBy('name')->get() : ($template !== null && $booking !== null ? $template->annexesFor($booking) : collect()); 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, 'annex_ids' => $annexes->pluck('id')->all(), 'annexes' => $annexes->map(fn (ContractAnnex $annex): array => ['id' => $annex->id, 'name' => $annex->name, 'version' => $annex->version, 'title' => $annex->title, 'body' => $annex->body])->values()->all(), 'cancellation_policy' => $template?->cancellationPolicy?->snapshot(), '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 case: chosen explicitly, else the one of its process settings – the channel's * template for the case's intake kind (request or binding booking), else the property's, else the landlord's. */ public static function templateFor(Booking $booking, int|string|null $explicitId = null): ?ContractTemplate { $id = filled($explicitId) ? (int) $explicitId : app(ProcessSettings::class)->forBooking($booking)->contractTemplateId(); 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)]; } /** * The annexes frozen in the offer, with placeholders filled. * * @return list */ public static function annexes(Booking $booking): array { $data = (array) ($booking->offer_terms['property_data'] ?? []); return collect((array) ($booking->offer_terms['annexes'] ?? []))->map(fn (array $annex): array => [ 'title' => (string) $annex['title'], 'name' => (string) $annex['name'], 'version' => (int) $annex['version'], 'html' => (string) RichContentRenderer::make(ContractPlaceholders::render((string) $annex['body'], $booking, $data))->toHtml(), ])->values()->all(); } 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 */ public static function owner(Booking $booking): array { return $booking->offer_terms['owner'] ?? (array) config('fewo.owner'); } /** * @return array */ 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; } }