diff --git a/api/generate_official_pdf.php b/api/generate_official_pdf.php new file mode 100644 index 0000000..4964d2b --- /dev/null +++ b/api/generate_official_pdf.php @@ -0,0 +1,251 @@ +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 = ''; + + 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?'); + $pdf->FieldBox('Option Selected', $lpa_data['life_sustaining_treatment'] ?? 'Not specified'); +} +$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); \ No newline at end of file diff --git a/api/generate_pdf.php b/api/generate_pdf.php index 44585de..77e646d 100644 --- a/api/generate_pdf.php +++ b/api/generate_pdf.php @@ -1,10 +1,12 @@ execute([$lpa_id]); $notified_persons = $stmt->fetchAll(); class LPAPDF extends FPDF { + public $lpa_type_title = 'Lasting Power of Attorney'; + function Header() { $this->SetFont('Helvetica', 'B', 15); - $this->Cell(0, 10, 'Health and Welfare Lasting Power of Attorney', 0, 1, 'C'); + $this->Cell(0, 10, $this->lpa_type_title, 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); @@ -73,6 +77,7 @@ class LPAPDF extends FPDF { } $pdf = new LPAPDF(); +$pdf->lpa_type_title = ($lpa_data['lpa_type'] ?? 'Health and Welfare') . ' Lasting Power of Attorney'; $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetAutoPageBreak(true, 15); @@ -141,9 +146,14 @@ if (empty($replacement_attorneys)) { } $pdf->Ln(5); -// 5. Life Sustaining -$pdf->SectionTitle('5. Life-Sustaining Treatment'); -$pdf->Field('Option', $lpa_data['life_sustaining_treatment']); +// 5. Section 5 varies by type +if (($lpa_data['lpa_type'] ?? '') === 'Property & Financial') { + $pdf->SectionTitle('5. When can the attorneys make decisions?'); + $pdf->Field('Option', $lpa_data['life_sustaining_treatment'] ?? 'Not specified'); // Reusing field for simplicity if needed +} else { + $pdf->SectionTitle('5. Life-Sustaining Treatment'); + $pdf->Field('Option', $lpa_data['life_sustaining_treatment']); +} $pdf->Ln(5); // 6. Witness Information diff --git a/api/save_lpa.php b/api/save_lpa.php index 45567af..d92bbf7 100644 --- a/api/save_lpa.php +++ b/api/save_lpa.php @@ -236,6 +236,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { exit; } + // Check if max 3 replacement attorneys reached + $count_stmt = db()->prepare("SELECT count(*) FROM lpa_attorneys WHERE lpa_id = ? AND type = 'replacement'"); + $count_stmt->execute([$lpa_id]); + if ((int)$count_stmt->fetchColumn() >= 3) { + echo json_encode(['success' => false, 'error' => 'A maximum of 3 replacement attorneys is allowed.']); + exit; + } + $stmt = db()->prepare("INSERT INTO lpa_attorneys (lpa_id, type, title, first_name, last_name, email, dob, address_line1, address_line2, address_line3, town, postcode) VALUES (?, 'replacement', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([$lpa_id, $title, $first_name, $last_name, $email, $dob, $address1, $address2, $address3, $town, $postcode]); diff --git a/apply.php b/apply.php index 6faeae3..272e262 100644 --- a/apply.php +++ b/apply.php @@ -110,7 +110,7 @@ foreach ($notified_persons as $np) { "postcode" => $np["postcode"] ]; } - +session_write_close(); ?> @@ -292,6 +292,18 @@ foreach ($notified_persons as $np) { + 5): ?> +
Be careful – if you give instructions that are not legally correct they would have to be removed before your LPA could be registered.
Instructions – use words like ‘must’ and ‘have to’
+