prepare("SELECT * FROM lpa_applications WHERE id = ?"); $stmt->execute([$lpa_id]); $lpa_data = $stmt->fetch(); if (!$lpa_data) { die('LPA not found.'); } // Authorization check if ((int)$lpa_data['user_id'] !== (int)$user_id && ($_SESSION['user_role'] ?? '') !== 'Super User') { die('Unauthorized access.'); } // 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(); $lpa_type = $lpa_data['lpa_type'] ?? 'Health & Welfare'; $is_hw = ($lpa_type === 'Health & Welfare'); class OfficialLPAPDF extends FPDF { public $lpa_title = ''; public $lpa_id = ''; function Header() { $this->SetFont('Helvetica', 'B', 14); $this->Cell(0, 10, $this->lpa_title, 0, 1, 'C'); $this->Ln(5); } function Footer() { $this->SetY(-15); $this->SetFont('Helvetica', 'I', 8); $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb} - LPA Ref: ' . $this->lpa_id, 0, 0, 'C'); } function SectionHeader($title) { $this->SetFont('Helvetica', 'B', 12); $this->SetFillColor(200, 200, 200); $this->Cell(0, 10, $title, 0, 1, 'L', true); $this->Ln(2); } function FieldBox($label, $value, $h = 10) { $this->SetFont('Helvetica', 'B', 9); $this->Cell(40, $h, $label . ':', 1, 0, 'L'); $this->SetFont('Helvetica', '', 9); $this->Cell(0, $h, $value, 1, 1, 'L'); } } $pdf = new OfficialLPAPDF(); $pdf->lpa_id = $lpa_id; $pdf->lpa_title = $lpa_type . ' Lasting Power of Attorney ' . ($is_hw ? '(LP1H)' : '(LP1F)'); $pdf->AliasNbPages(); $pdf->AddPage(); // Section 1: The Donor $pdf->SectionHeader('Section 1: The Donor'); $pdf->FieldBox('Full Name', $lpa_data['donor_name']); $pdf->FieldBox('Other Names', $lpa_data['other_names']); $pdf->FieldBox('Date of Birth', $lpa_data['donor_dob']); $pdf->FieldBox('Email', $lpa_data['customer_email']); $donor_addr = $lpa_data['donor_address_line1'] . (empty($lpa_data['donor_address_line2']) ? '' : ', ' . $lpa_data['donor_address_line2']) . ', ' . $lpa_data['donor_town'] . ', ' . $lpa_data['donor_postcode']; $pdf->FieldBox('Address', $donor_addr); $pdf->Ln(5); // Section 2: The Attorneys $pdf->SectionHeader('Section 2: The Attorneys'); $main_attorneys = array_slice($primary_attorneys, 0, 5); $continuation_attorneys = array_slice($primary_attorneys, 5); foreach ($main_attorneys as $idx => $att) { $name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name']; $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Attorney ' . ($idx + 1), 0, 1); $pdf->FieldBox('Name', $name); $pdf->FieldBox('Date of Birth', $att['dob']); $pdf->FieldBox('Email', $att['email']); $addr = $att['address_line1'] . (empty($att['address_line2']) ? '' : ', ' . $att['address_line2']) . ', ' . $att['town'] . ', ' . $att['postcode']; $pdf->FieldBox('Address', $addr); $pdf->Ln(2); } $pdf->Ln(5); // Section 3: How should the attorneys make decisions? $pdf->SectionHeader('Section 3: How should the attorneys make decisions?'); $pdf->FieldBox('Decision Type', $lpa_data['attorney_decision_type']); $pdf->Ln(5); // Section 4: Replacement Attorneys $pdf->SectionHeader('Section 4: Replacement Attorneys'); $main_replacements = array_slice($replacement_attorneys, 0, 2); $continuation_replacements = array_slice($replacement_attorneys, 2); foreach ($main_replacements as $idx => $att) { $name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name']; $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Replacement Attorney ' . ($idx + 1), 0, 1); $pdf->FieldBox('Name', $name); $addr = $att['address_line1'] . (empty($att['address_line2']) ? '' : ', ' . $att['town'] . ', ' . $att['postcode']); $pdf->FieldBox('Address', $addr); $pdf->Ln(2); } $pdf->Ln(5); // Section 5: Difference by type if ($is_hw) { $pdf->SectionHeader('Section 5: Life-Sustaining Treatment'); $pdf->FieldBox('Option Selected', $lpa_data['life_sustaining_treatment']); } else { $pdf->SectionHeader('Section 5: When can the attorneys make decisions?'); $val = ($lpa_data['life_sustaining_treatment'] === 'Option A') ? 'As soon as registered' : (($lpa_data['life_sustaining_treatment'] === 'Option B') ? 'Only when I cannot make my own decisions' : 'Not specified'); $pdf->FieldBox('Option Selected', $val); } $pdf->Ln(5); // Section 6: People to Notify $pdf->SectionHeader('Section 6: People to Notify'); foreach ($notified_persons as $idx => $np) { if ($idx >= 5) break; // Main form usually has space for 5 $pdf->FieldBox('Person ' . ($idx + 1), $np['first_name'] . ' ' . $np['last_name'] . ' (' . $np['postcode'] . ')'); } $pdf->Ln(5); // Section 7: Preferences and Instructions $pdf->SectionHeader('Section 7: Preferences and Instructions'); if (!function_exists('count_lines')) { function count_lines($text) { if (empty($text)) return 0; return substr_count($text, "\n") + 1; } } $pref_lines = count_lines($lpa_data['preferences']); $instr_lines = count_lines($lpa_data['instructions']); $pref_on_main = $lpa_data['preferences']; $instr_on_main = $lpa_data['instructions']; $pref_continuation = ''; $instr_continuation = ''; if (strlen($lpa_data['preferences']) > 800 || $pref_lines > 9) { $pref_on_main = "See Continuation Sheet LPC"; $pref_continuation = $lpa_data['preferences']; } if (strlen($lpa_data['instructions']) > 800 || $instr_lines > 9) { $instr_on_main = "See Continuation Sheet LPC"; $instr_continuation = $lpa_data['instructions']; } $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Preferences', 0, 1); $pdf->SetFont('Helvetica', '', 9); $pdf->MultiCell(0, 6, $pref_on_main, 1, 'L'); $pdf->Ln(2); $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Instructions', 0, 1); $pdf->SetFont('Helvetica', '', 9); $pdf->MultiCell(0, 6, $instr_on_main, 1, 'L'); $pdf->Ln(5); // Section 8-10: Signatures and Certificate Provider (Skipped/Simplified for now as per Summary) $pdf->SectionHeader('Section 8-10: Signatures & Certificate Provider'); $pdf->FieldBox('Certificate Provider', $lpa_data['certificate_provider_first_name'] . ' ' . $lpa_data['certificate_provider_last_name']); $pdf->Ln(10); // CONTINUATION SHEETS if (!empty($continuation_attorneys) || !empty($continuation_replacements) || !empty($pref_continuation) || !empty($instr_continuation)) { $pdf->AddPage(); $pdf->SetFont('Helvetica', 'B', 16); $pdf->Cell(0, 10, 'Continuation Sheet LPC', 0, 1, 'C'); $pdf->Ln(10); if (!empty($continuation_attorneys)) { $pdf->SectionHeader('Section 2 (Continued): More Attorneys'); foreach ($continuation_attorneys as $idx => $att) { $name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name']; $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Attorney ' . ($idx + 6), 0, 1); $pdf->FieldBox('Name', $name); $pdf->FieldBox('Date of Birth', $att['dob']); $pdf->FieldBox('Email', $att['email']); $addr = $att['address_line1'] . (empty($att['address_line2']) ? '' : ', ' . $att['address_line2']) . ', ' . $att['town'] . ', ' . $att['postcode']; $pdf->FieldBox('Address', $addr); $pdf->Ln(2); } } if (!empty($continuation_replacements)) { $pdf->SectionHeader('Section 4 (Continued): More Replacement Attorneys'); foreach ($continuation_replacements as $idx => $att) { $name = ($att['title'] ? $att['title'] . ' ' : '') . $att['first_name'] . ' ' . $att['last_name']; $pdf->SetFont('Helvetica', 'B', 10); $pdf->Cell(0, 8, 'Replacement Attorney ' . ($idx + 3), 0, 1); $pdf->FieldBox('Name', $name); $addr = $att['address_line1'] . (empty($att['address_line2']) ? '' : ', ' . $att['town'] . ', ' . $att['postcode']); $pdf->FieldBox('Address', $addr); $pdf->Ln(2); } } if (!empty($pref_continuation)) { $pdf->SectionHeader('Section 7 (Continued): Preferences'); $pdf->SetFont('Helvetica', '', 9); $pdf->MultiCell(0, 6, $pref_continuation, 1, 'L'); $pdf->Ln(5); } if (!empty($instr_continuation)) { $pdf->SectionHeader('Section 7 (Continued): Instructions'); $pdf->SetFont('Helvetica', '', 9); $pdf->MultiCell(0, 6, $instr_continuation, 1, 'L'); $pdf->Ln(5); } } $filename = 'LPA_Official_Form_' . str_replace(' ', '_', ($lpa_data['donor_name'] ?? 'Form')) . '_' . date('Ymd') . '.pdf'; $pdf->Output('D', $filename);