37684-vm/api/generate_pdf.php
2026-02-28 21:02:21 +00:00

216 lines
8.1 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../fpdf/fpdf.php';
$lpa_id = isset($_GET['id']) ? (int)$_GET['id'] : null;
if (!$lpa_id) {
die('LPA ID is required.');
}
// Fetch LPA data
$stmt = db()->prepare("SELECT * FROM lpa_applications WHERE id = ?");
$stmt->execute([$lpa_id]);
$lpa_data = $stmt->fetch();
if (!$lpa_data) {
die('LPA not found.');
}
// Fetch attorneys
$stmt = db()->prepare("SELECT * FROM lpa_attorneys WHERE lpa_id = ? AND type = 'primary' ORDER BY id ASC");
$stmt->execute([$lpa_id]);
$primary_attorneys = $stmt->fetchAll();
$stmt = db()->prepare("SELECT * FROM lpa_attorneys WHERE lpa_id = ? AND type = 'replacement' ORDER BY id ASC");
$stmt->execute([$lpa_id]);
$replacement_attorneys = $stmt->fetchAll();
// Fetch notified persons
$stmt = db()->prepare("SELECT * FROM lpa_notified_persons WHERE application_id = ? ORDER BY id ASC");
$stmt->execute([$lpa_id]);
$notified_persons = $stmt->fetchAll();
class LPAPDF extends FPDF {
function Header() {
$this->SetFont('Helvetica', 'B', 15);
$this->Cell(0, 10, 'Health and Welfare Lasting Power of Attorney', 0, 1, 'C');
$this->SetFont('Helvetica', 'I', 9);
$this->Cell(0, 5, 'Summary Draft (Not a registered legal document)', 0, 1, 'C');
$this->Ln(10);
}
function Footer() {
$this->SetY(-15);
$this->SetFont('Helvetica', 'I', 8);
$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb} - Generated on ' . date('Y-m-d H:i'), 0, 0, 'C');
}
function SectionTitle($title) {
$this->SetFont('Helvetica', 'B', 12);
$this->SetFillColor(230, 230, 230);
$this->Cell(0, 8, $title, 0, 1, 'L', true);
$this->Ln(2);
}
function Field($label, $value) {
$this->SetFont('Helvetica', 'B', 10);
$this->Cell(50, 6, $label . ':', 0, 0);
$this->SetFont('Helvetica', '', 10);
$this->MultiCell(0, 6, $value ? $value : 'None', 0, 'L');
}
}
$pdf = new LPAPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetAutoPageBreak(true, 15);
// 1. Donor
$pdf->SectionTitle('1. The Donor');
$pdf->Field('Full Name', $lpa_data['donor_name']);
if (!empty($lpa_data['other_names'])) $pdf->Field('Other Names', $lpa_data['other_names']);
$pdf->Field('Date of Birth', $lpa_data['donor_dob']);
$pdf->Field('Email', $lpa_data['customer_email']);
$donor_addr = $lpa_data['donor_address_line1'];
if (!empty($lpa_data['donor_address_line2'])) $donor_addr .= ', ' . $lpa_data['donor_address_line2'];
$donor_addr .= ', ' . $lpa_data['donor_town'] . ', ' . $lpa_data['donor_postcode'];
$pdf->Field('Address', $donor_addr);
$pdf->Ln(5);
// 2. Primary Attorneys
$pdf->SectionTitle('2. The Attorneys');
if (empty($primary_attorneys)) {
$pdf->Field('Attorneys', 'No attorneys listed.');
} else {
foreach ($primary_attorneys as $idx => $att) {
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(0, 6, 'Attorney ' . ($idx + 1) . ':', 0, 1);
$pdf->SetFont('Helvetica', '', 10);
$name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name'];
$pdf->Field('Name', $name);
$pdf->Field('DOB', $att['dob']);
$pdf->Field('Email', $att['email']);
$addr = $att['address_line1'];
if (!empty($att['address_line2'])) $addr .= ', ' . $att['address_line2'];
$addr .= ', ' . $att['town'] . ', ' . $att['postcode'];
$pdf->Field('Address', $addr);
// Witness
if (!empty($att['witness_first_name'])) {
$w_name = $att['witness_first_name'] . ' ' . $att['witness_last_name'];
$w_addr = $att['witness_address_line1'] . ', ' . $att['witness_postcode'];
$pdf->Field('Witness', $w_name . ' (' . $w_addr . ')');
}
$pdf->Ln(2);
}
}
$pdf->Ln(5);
// 3. Decisions
$pdf->SectionTitle('3. How should the attorneys make decisions?');
$pdf->Field('Decision Type', $lpa_data['attorney_decision_type']);
$pdf->Ln(5);
// 4. Replacement Attorneys
$pdf->SectionTitle('4. Replacement Attorneys');
if (empty($replacement_attorneys)) {
$pdf->Field('Replacements', 'No replacement attorneys listed.');
} else {
foreach ($replacement_attorneys as $idx => $att) {
$pdf->SetFont('Helvetica', 'B', 10);
$pdf->Cell(0, 6, 'Replacement ' . ($idx + 1) . ':', 0, 1);
$pdf->SetFont('Helvetica', '', 10);
$name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name'];
$pdf->Field('Name', $name);
$addr = $att['address_line1'] . ', ' . $att['town'] . ', ' . $att['postcode'];
$pdf->Field('Address', $addr);
$pdf->Ln(2);
}
}
$pdf->Ln(5);
// 5. Life Sustaining
$pdf->SectionTitle('5. Life-Sustaining Treatment');
$pdf->Field('Option', $lpa_data['life_sustaining_treatment']);
$pdf->Ln(5);
// 6. Witness Information
$pdf->SectionTitle('6. Donor\'s Witness');
$pdf->Field('Name', $lpa_data['witness_first_name'] . ' ' . $lpa_data['witness_last_name']);
$pdf->Field('Address', $lpa_data['witness_address_line1'] . ', ' . $lpa_data['witness_postcode']);
$pdf->Ln(5);
// 7. People to Notify
$pdf->SectionTitle('7. People to Notify');
if (empty($notified_persons)) {
$pdf->Field('Persons', 'None');
} else {
foreach ($notified_persons as $idx => $np) {
$pdf->Field('Person ' . ($idx + 1), $np['first_name'] . ' ' . $np['last_name'] . ' (' . $np['postcode'] . ')');
}
}
$pdf->Ln(5);
// 8. Preferences & Instructions
$pdf->SectionTitle('8. Preferences and Instructions');
$pdf->Field('Preferences', $lpa_data['preferences']);
$pdf->Field('Instructions', $lpa_data['instructions']);
$pdf->Ln(5);
// 9. Certificate Provider
$pdf->SectionTitle('9. Certificate Provider');
$name = ($lpa_data['certificate_provider_title'] ? $lpa_data['certificate_provider_title'] . ' ' : '') . $lpa_data['certificate_provider_first_name'] . ' ' . $lpa_data['certificate_provider_last_name'];
$pdf->Field('Name', $name);
$pdf->Field('Address', $lpa_data['certificate_provider_address_line1'] . ', ' . $lpa_data['certificate_provider_postcode']);
$pdf->Ln(5);
// 10. Attorney Witnesses
$pdf->SectionTitle('10. Attorney Witnesses');
foreach ($primary_attorneys as $idx => $att) {
if (!empty($att['witness_first_name'])) {
$w_name = $att['witness_first_name'] . ' ' . $att['witness_last_name'];
$pdf->Field('Witness for ' . $att['first_name'], $w_name);
}
}
foreach ($replacement_attorneys as $idx => $att) {
if (!empty($att['witness_first_name'])) {
$w_name = $att['witness_first_name'] . ' ' . $att['witness_last_name'];
$pdf->Field('Witness for ' . $att['first_name'], $w_name);
}
}
$pdf->Ln(5);
// 11. Registration
$pdf->SectionTitle('11. Registration');
$pdf->Field('Who is registering', ucfirst($lpa_data['registration_who'] ?? ''));
if (($lpa_data['registration_who'] ?? '') === 'attorneys') {
$reg_ids = explode(',', $lpa_data['registering_attorneys_ids'] ?? '');
$reg_names = [];
foreach ($primary_attorneys as $att) {
if (in_array($att['id'], $reg_ids)) {
$reg_names[] = $att['first_name'] . ' ' . $att['last_name'];
}
}
$pdf->Field('Attorneys', implode(', ', $reg_names));
}
$pdf->Ln(5);
// 12. Correspondence
$pdf->SectionTitle('12. Correspondence');
$pdf->Field('Recipient', $lpa_data['correspondence_who'] ?? '');
if (($lpa_data['correspondence_who'] ?? '') !== 'Donor') {
$pdf->Field('Name', ($lpa_data['correspondence_first_name'] ?? '') . ' ' . ($lpa_data['correspondence_last_name'] ?? ''));
$pdf->Field('Address', ($lpa_data['correspondence_address_line1'] ?? '') . ', ' . ($lpa_data['correspondence_postcode'] ?? ''));
$pdf->Field('Contact Preference', $lpa_data['correspondence_contact_preference'] ?? '');
}
$pdf->Ln(5);
// 13. Payment
$pdf->SectionTitle('13. Payment & Fee');
$pdf->Field('Payment Method', $lpa_data['payment_method'] ?? '');
$pdf->Field('Reduced Fee Eligibility', $lpa_data['reduced_fee_eligibility'] ?? '');
$pdf->Field('Repeat Application', ($lpa_data['is_repeat_application'] ?? false) ? 'Yes (Case: ' . ($lpa_data['repeat_case_number'] ?? '') . ')' : 'No');
$filename = 'LPA_' . str_replace(' ', '_', ($lpa_data['donor_name'] ?? 'Summary')) . '_' . date('Ymd') . '.pdf';
$pdf->Output('D', $filename);