Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m59s
Eine Zusammenstellung vor jedem Dokument (Angebot, Bestaetigung, Buchungsunterlagen extern), Dokumentart je Vorgangsart, Freigaben fuer Vorlagen und Dokumentarten, Schutz vor unvereinbarten Bedingungen bei Portalbuchungen (E-004), kein doppelter Versand, Absicherung Hold 0 bis Paket 7, A6-Aufenthaltsbestaetigung vorbereitet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
3.3 KiB
PHP
79 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Documents;
|
||
|
||
use App\Enums\DocumentKind;
|
||
use App\Models\Booking;
|
||
use App\Models\Site;
|
||
use Barryvdh\DomPDF\Facade\Pdf;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
/**
|
||
* The PDF of a case's document. As a draft it is part of the offer (offer_path, one file per offer version); the
|
||
* document of record – rental contract, booking documentation of a portal booking or stay confirmation, depending on
|
||
* the case – is generated once when it is sent (contract_path). Both are stored, so later changes of texts or prices do
|
||
* not alter a document the guest already received.
|
||
*/
|
||
class RentalContract
|
||
{
|
||
private const DISK = 'local';
|
||
|
||
public function generate(Booking $booking, bool $draft = false, ?DocumentKind $kind = null): string
|
||
{
|
||
$booking->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<string, mixed>}
|
||
*/
|
||
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;
|
||
}
|
||
}
|