Files
fewo-stommer/app/Documents/RentalContract.php
T
Lars 06df80e4db
Deploy FeWo mit PipelineManager / deploy (push) Successful in 2m23s
Mandantenabschluss: Marken, Vertragsdateien und Archive trennen
2026-10-26 17:07:11 +01:00

48 lines
1.7 KiB
PHP

<?php
namespace App\Documents;
use App\Models\Booking;
use App\Models\Site;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Storage;
/**
* The rental contract as PDF. As a draft it is part of the offer (offer_path, one file per offer version);
* the final contract is generated at confirmation (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): string
{
$booking->loadMissing(['guest', 'property']);
$filename = $draft
? "contracts/Angebot-{$booking->reference}-v{$booking->offer_version}.pdf"
: "contracts/Mietvertrag-{$booking->reference}.pdf";
$path = "contracts/tenants/{$booking->tenant_id}/bookings/{$booking->id}/".basename($filename);
// 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);
Storage::disk(self::DISK)->put($path, Pdf::loadView('pdf.contract', [
'booking' => $booking,
'draft' => $draft,
'logo' => $logo,
])->setPaper('a4')->output());
$booking->update([$draft ? 'offer_path' : 'contract_path' => $path]);
return $path;
}
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;
}
}