Files
casadoc/public/userportal/api/home-report.php
2026-07-27 21:04:07 +03:00

73 lines
2.3 KiB
PHP

<?php
require __DIR__ . '/_bootstrap.php';
/**
* @OA\Get(
* path="/home-report.php",
* tags={"Homes"},
* summary="Property PDF report",
* description="Access: owner or accepted share. Requires TCPDF library (composer).",
* security={{"bearerAuth":{}}},
* @OA\Parameter(name="idhome", in="query", required=true, @OA\Schema(type="integer")),
* @OA\Response(response=200, description="PDF",
* @OA\MediaType(mediaType="application/pdf", @OA\Schema(type="string", format="binary"))),
* @OA\Response(response=403, ref="#/components/responses/Forbidden")
* )
*/
require_method('GET');
$user = require_auth($pdo);
$idhome = (int) query('idhome', 0);
if ($idhome <= 0) {
json_error(422, 'idhome is required');
}
if (!user_can_access_home($pdo, $user, $idhome)) {
json_error(403, 'No access to this home');
}
$stmt = $pdo->prepare('SELECT * FROM home WHERE idhome = ? LIMIT 1');
$stmt->execute([$idhome]);
$home = $stmt->fetch();
if (!$home) {
json_error(404, 'Home not found');
}
// TCPDF is provided by the main app composer autoload.
$autoload = __DIR__ . '/../../../vendor/autoload.php';
if (is_file($autoload)) {
require_once $autoload;
}
if (!class_exists('TCPDF')) {
json_error(501, 'PDF library (TCPDF) not installed');
}
$pdf = new TCPDF();
$pdf->SetCreator('Casadoc');
$pdf->SetTitle('Report immobile');
$pdf->AddPage();
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 10, (string) ($home['name'] ?? 'Immobile'), 0, 1);
$pdf->SetFont('helvetica', '', 11);
$rows = [
'Indirizzo' => trim(($home['address'] ?? '') . ', ' . ($home['zip'] ?? '') . ' ' . ($home['city'] ?? '')),
'Comune cat.'=> $home['cadastral_municipality'] ?? '',
'Foglio' => $home['cadastral_sheet'] ?? '',
'Particella' => $home['cadastral_particle'] ?? '',
'Categoria' => $home['cadastral_category'] ?? '',
'Superficie' => $home['cadastral_surface'] ?? '',
'Rendita' => $home['cadastral_rendita'] ?? '',
];
foreach ($rows as $label => $value) {
$pdf->Cell(45, 8, $label . ':', 0, 0);
$pdf->Cell(0, 8, (string) $value, 0, 1);
}
$body = $pdf->Output('report.pdf', 'S');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="Report_' . $idhome . '.pdf"');
header('Content-Length: ' . strlen($body));
echo $body;
exit;