loadMissing(['guest', 'property']); $kind = $draft ? DocumentKind::Offer : ($kind ?? DocumentKind::forCase($booking) ?? DocumentKind::Confirmation); $filename = $draft ? "Angebot-{$booking->reference}-v{$booking->offer_version}.pdf" : "{$kind->fileName()}-{$booking->reference}.pdf"; $path = "contracts/tenants/{$booking->tenant_id}/bookings/{$booking->id}/{$filename}"; // The document of record is never rewritten once stored (confirmation resends reuse it). if (! $draft && $booking->contract_path !== null && Storage::disk(self::DISK)->exists($booking->contract_path)) { return $booking->contract_path; } $pdf = $this->render($booking, $kind, $draft); Storage::disk(self::DISK)->put($path, $pdf); $booking->update([$draft ? 'offer_path' : 'contract_path' => $path, $draft ? 'offer_pdf_sha256' : 'contract_pdf_sha256' => hash('sha256', $pdf)]); return $path; } /** * The PDF bytes of a document, without storing anything (also used for previews of a composition). */ public function render(Booking $booking, DocumentKind $kind, bool $draft = false): string { [$view, $data] = $this->view($booking, $kind, $draft); return Pdf::loadView($view, $data)->setPaper('a4')->output(); } /** * The view and its data that make up the document. * * @return array{0: string, 1: array} */ public function view(Booking $booking, DocumentKind $kind, bool $draft = false): array { // The logo is embedded as a file: the PDF is rendered on the server and must not depend on // fetching anything over the network. Each website has its own (A14). $logo = $booking->website()?->brandPath(Site::BRAND_DOCUMENT_LOGO); $document = $kind === DocumentKind::StayConfirmation ? null : OfferTerms::document($booking); $view = match (true) { $kind === DocumentKind::StayConfirmation => 'pdf.stay-confirmation', $document !== null => 'pdf.contract-document', default => 'pdf.contract', }; return [$view, ['booking' => $booking, 'draft' => $draft, 'logo' => $logo, 'document' => $document, 'kind' => $kind]]; } public function absolutePath(Booking $booking, bool $draft = false): ?string { $path = $draft ? $booking->offer_path : $booking->contract_path; return $path && Storage::disk(self::DISK)->exists($path) ? Storage::disk(self::DISK)->path($path) : null; } }