Autosave: 20260228-202416
This commit is contained in:
parent
6a72450df9
commit
cad70b3f6e
364
api/save_lpa.php
364
api/save_lpa.php
@ -4,7 +4,8 @@ require_once __DIR__ . '/../db/config.php';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
// Check for specific actions first
|
||||
if (isset($_POST['action']) && $_POST['action'] === 'delete_attorney') {
|
||||
if (isset($_POST['action'])) {
|
||||
if ($_POST['action'] === 'delete_attorney') {
|
||||
$attorney_id = isset($_POST['attorney_id']) ? (int)$_POST['attorney_id'] : null;
|
||||
$lpa_id = isset($_POST['lpa_id']) ? (int)$_POST['lpa_id'] : null;
|
||||
|
||||
@ -21,6 +22,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
} elseif ($_POST['action'] === 'delete_notified_person') {
|
||||
$person_id = isset($_POST['person_id']) ? (int)$_POST['person_id'] : null;
|
||||
$lpa_id = isset($_POST['lpa_id']) ? (int)$_POST['lpa_id'] : null;
|
||||
|
||||
if (!$person_id || !$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'Missing IDs for deletion.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$stmt = db()->prepare("DELETE FROM lpa_notified_persons WHERE id = ? AND application_id = ?");
|
||||
$stmt->execute([$person_id, $lpa_id]);
|
||||
echo json_encode(['success' => true, 'message' => 'Person removed.']);
|
||||
} catch (PDOException $e) {
|
||||
echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$step = isset($_POST['step']) ? (int)$_POST['step'] : 1;
|
||||
@ -80,7 +99,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("INSERT INTO lpa_attorneys (lpa_id, title, first_name, last_name, email, dob, address_line1, address_line2, address_line3, town, postcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$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 (?, 'primary', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$lpa_id, $title, $first_name, $last_name, $email, $dob, $address1, $address2, $address3, $town, $postcode]);
|
||||
|
||||
// Update step reached
|
||||
@ -107,6 +126,347 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$stmt->execute([$attorney_decision_type, $lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 4, 'message' => 'Decision-making preference saved.']);
|
||||
} elseif ($step === 4) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 4.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$title = $_POST['title'] ?? '';
|
||||
$first_name = $_POST['first_name'] ?? '';
|
||||
$last_name = $_POST['last_name'] ?? '';
|
||||
$email = $_POST['email'] ?? '';
|
||||
$dob = $_POST['dob'] ?? '';
|
||||
$address1 = $_POST['address_line1'] ?? '';
|
||||
$address2 = $_POST['address_line2'] ?? '';
|
||||
$address3 = $_POST['address_line3'] ?? '';
|
||||
$town = $_POST['town'] ?? '';
|
||||
$postcode = $_POST['postcode'] ?? '';
|
||||
$next_action = $_POST['next_action'] ?? 'add_another';
|
||||
|
||||
if (empty($first_name) || empty($last_name) || empty($email) || empty($dob) || empty($address1) || empty($town) || empty($postcode)) {
|
||||
echo json_encode(['success' => false, 'error' => 'All fields are required to save a replacement attorney.']);
|
||||
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]);
|
||||
|
||||
// Update step reached
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET step_reached = GREATEST(step_reached, 4) WHERE id = ?");
|
||||
$stmt->execute([$lpa_id]);
|
||||
|
||||
$next_step = ($next_action === 'next_step') ? 5 : 4;
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => $next_step, 'message' => 'Replacement attorney saved successfully.']);
|
||||
} elseif ($step === 5) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 5.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$life_sustaining_treatment = $_POST['life_sustaining_treatment'] ?? '';
|
||||
$witness_title = $_POST['witness_title'] ?? '';
|
||||
$witness_first_name = $_POST['witness_first_name'] ?? '';
|
||||
$witness_last_name = $_POST['witness_last_name'] ?? '';
|
||||
$witness_address_line1 = $_POST['witness_address_line1'] ?? '';
|
||||
$witness_address_line2 = $_POST['witness_address_line2'] ?? '';
|
||||
$witness_address_line3 = $_POST['witness_address_line3'] ?? '';
|
||||
$witness_postcode = $_POST['witness_postcode'] ?? '';
|
||||
|
||||
if (empty($life_sustaining_treatment) || empty($witness_first_name) || empty($witness_last_name) || empty($witness_address_line1) || empty($witness_postcode)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please fill in all required fields.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
life_sustaining_treatment = ?,
|
||||
witness_title = ?,
|
||||
witness_first_name = ?,
|
||||
witness_last_name = ?,
|
||||
witness_address_line1 = ?,
|
||||
witness_address_line2 = ?,
|
||||
witness_address_line3 = ?,
|
||||
witness_postcode = ?,
|
||||
step_reached = GREATEST(step_reached, 5)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([
|
||||
$life_sustaining_treatment,
|
||||
$witness_title,
|
||||
$witness_first_name,
|
||||
$witness_last_name,
|
||||
$witness_address_line1,
|
||||
$witness_address_line2,
|
||||
$witness_address_line3,
|
||||
$witness_postcode,
|
||||
$lpa_id
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 6, 'message' => 'Step 5 saved successfully.']);
|
||||
} elseif ($step === 6) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 6.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$title = $_POST['title'] ?? '';
|
||||
$first_name = $_POST['first_name'] ?? '';
|
||||
$last_name = $_POST['last_name'] ?? '';
|
||||
$address1 = $_POST['address_line1'] ?? '';
|
||||
$address2 = $_POST['address_line2'] ?? '';
|
||||
$address3 = $_POST['address_line3'] ?? '';
|
||||
$postcode = $_POST['postcode'] ?? '';
|
||||
$next_action = $_POST['next_action'] ?? 'add_another';
|
||||
|
||||
if (empty($first_name) || empty($last_name) || empty($address1) || empty($postcode)) {
|
||||
echo json_encode(['success' => false, 'error' => 'All fields are required to save a person to notify.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("INSERT INTO lpa_notified_persons (application_id, title, first_name, last_name, address_line1, address_line2, address_line3, postcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([$lpa_id, $title, $first_name, $last_name, $address1, $address2, $address3, $postcode]);
|
||||
|
||||
// Update step reached
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET step_reached = GREATEST(step_reached, 6) WHERE id = ?");
|
||||
$stmt->execute([$lpa_id]);
|
||||
|
||||
$next_step = ($next_action === 'next_step') ? 7 : 6;
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => $next_step, 'message' => 'Person to notify saved successfully.']);
|
||||
} elseif ($step === 7) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 7.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$preferences = $_POST['preferences'] ?? '';
|
||||
$instructions = $_POST['instructions'] ?? '';
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
preferences = ?,
|
||||
instructions = ?,
|
||||
step_reached = GREATEST(step_reached, 7)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([$preferences, $instructions, $lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 8, 'message' => 'Step 7 saved successfully.']);
|
||||
} elseif ($step === 8) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 8.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$title = $_POST['certificate_provider_title'] ?? '';
|
||||
$first_name = $_POST['certificate_provider_first_name'] ?? '';
|
||||
$last_name = $_POST['certificate_provider_last_name'] ?? '';
|
||||
$address1 = $_POST['certificate_provider_address_line1'] ?? '';
|
||||
$address2 = $_POST['certificate_provider_address_line2'] ?? '';
|
||||
$address3 = $_POST['certificate_provider_address_line3'] ?? '';
|
||||
$postcode = $_POST['certificate_provider_postcode'] ?? '';
|
||||
|
||||
if (empty($first_name) || empty($last_name) || empty($address1) || empty($postcode)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please fill in all required fields for the certificate provider.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
certificate_provider_title = ?,
|
||||
certificate_provider_first_name = ?,
|
||||
certificate_provider_last_name = ?,
|
||||
certificate_provider_address_line1 = ?,
|
||||
certificate_provider_address_line2 = ?,
|
||||
certificate_provider_address_line3 = ?,
|
||||
certificate_provider_postcode = ?,
|
||||
step_reached = GREATEST(step_reached, 8)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([$title, $first_name, $last_name, $address1, $address2, $address3, $postcode, $lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 9, 'message' => 'Step 8 saved successfully.']);
|
||||
} elseif ($step === 9) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 9.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$attorney_witnesses = $_POST['attorney_witness'] ?? [];
|
||||
|
||||
foreach ($attorney_witnesses as $attorney_id => $witness_data) {
|
||||
$selection = $witness_data['selection'] ?? '';
|
||||
|
||||
if ($selection === 'new') {
|
||||
$w_title = $witness_data['title'] ?? '';
|
||||
$w_first_name = $witness_data['first_name'] ?? '';
|
||||
$w_last_name = $witness_data['last_name'] ?? '';
|
||||
$w_address1 = $witness_data['address_line1'] ?? '';
|
||||
$w_address2 = $witness_data['address_line2'] ?? '';
|
||||
$w_address3 = $witness_data['address_line3'] ?? '';
|
||||
$w_postcode = $witness_data['postcode'] ?? '';
|
||||
} else {
|
||||
$pw = json_decode($selection, true);
|
||||
if ($pw) {
|
||||
$w_title = $pw['title'] ?? '';
|
||||
$w_first_name = $pw['first_name'] ?? '';
|
||||
$w_last_name = $pw['last_name'] ?? '';
|
||||
$w_address1 = $pw['address1'] ?? '';
|
||||
$w_address2 = $pw['address2'] ?? '';
|
||||
$w_address3 = $pw['address3'] ?? '';
|
||||
$w_postcode = $pw['postcode'] ?? '';
|
||||
} else {
|
||||
// Skip if nothing selected and not new
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_attorneys SET
|
||||
witness_title = ?,
|
||||
witness_first_name = ?,
|
||||
witness_last_name = ?,
|
||||
witness_address_line1 = ?,
|
||||
witness_address_line2 = ?,
|
||||
witness_address_line3 = ?,
|
||||
witness_postcode = ?
|
||||
WHERE id = ? AND lpa_id = ?");
|
||||
$stmt->execute([$w_title, $w_first_name, $w_last_name, $w_address1, $w_address2, $w_address3, $w_postcode, $attorney_id, $lpa_id]);
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET step_reached = GREATEST(step_reached, 9) WHERE id = ?");
|
||||
$stmt->execute([$lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 10, 'message' => 'Attorney witnesses saved successfully.']);
|
||||
} elseif ($step === 10) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 10.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$registration_who = $_POST['registration_who'] ?? 'donor';
|
||||
$registering_attorneys = $_POST['registering_attorneys'] ?? [];
|
||||
$registering_attorneys_ids = implode(',', $registering_attorneys);
|
||||
|
||||
if ($registration_who === 'attorneys' && empty($registering_attorneys)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please select at least one attorney if they are registering the LPA.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
registration_who = ?,
|
||||
registering_attorneys_ids = ?,
|
||||
step_reached = GREATEST(step_reached, 10)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([$registration_who, $registering_attorneys_ids, $lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 11, 'message' => 'Registration choice saved successfully.']);
|
||||
} elseif ($step === 11) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 11.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$correspondence_who = $_POST['correspondence_who'] ?? 'Donor';
|
||||
$title = $_POST['correspondence_title'] ?? '';
|
||||
$first_name = $_POST['correspondence_first_name'] ?? '';
|
||||
$last_name = $_POST['correspondence_last_name'] ?? '';
|
||||
$company_name = $_POST['correspondence_company_name'] ?? '';
|
||||
$address1 = $_POST['correspondence_address_line1'] ?? '';
|
||||
$address2 = $_POST['correspondence_address_line2'] ?? '';
|
||||
$address3 = $_POST['correspondence_address_line3'] ?? '';
|
||||
$postcode = $_POST['correspondence_postcode'] ?? '';
|
||||
$contact_preference = $_POST['correspondence_contact_preference'] ?? 'Post';
|
||||
$phone = $_POST['correspondence_phone'] ?? '';
|
||||
$email = $_POST['correspondence_email'] ?? '';
|
||||
|
||||
if ($correspondence_who !== 'Donor') {
|
||||
if (empty($first_name) || empty($last_name) || empty($address1) || empty($postcode)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please fill in all required correspondence details.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
correspondence_who = ?,
|
||||
correspondence_title = ?,
|
||||
correspondence_first_name = ?,
|
||||
correspondence_last_name = ?,
|
||||
correspondence_company_name = ?,
|
||||
correspondence_address_line1 = ?,
|
||||
correspondence_address_line2 = ?,
|
||||
correspondence_address_line3 = ?,
|
||||
correspondence_postcode = ?,
|
||||
correspondence_contact_preference = ?,
|
||||
correspondence_phone = ?,
|
||||
correspondence_email = ?,
|
||||
step_reached = GREATEST(step_reached, 11)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([
|
||||
$correspondence_who,
|
||||
$title,
|
||||
$first_name,
|
||||
$last_name,
|
||||
$company_name,
|
||||
$address1,
|
||||
$address2,
|
||||
$address3,
|
||||
$postcode,
|
||||
$contact_preference,
|
||||
$phone,
|
||||
$email,
|
||||
$lpa_id
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 12, 'message' => 'Correspondence details saved successfully.']);
|
||||
} elseif ($step === 12) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 12.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payment_method = $_POST['payment_method'] ?? 'Card';
|
||||
$payment_phone = $_POST['payment_phone'] ?? '';
|
||||
$reduced_fee_eligibility = $_POST['reduced_fee_eligibility'] ?? 'No';
|
||||
$is_repeat_application = isset($_POST['is_repeat_application']) ? 1 : 0;
|
||||
$repeat_case_number = $_POST['repeat_case_number'] ?? '';
|
||||
|
||||
if ($payment_method === 'Card' && empty($payment_phone)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please provide a phone number for card payment.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($is_repeat_application && empty($repeat_case_number)) {
|
||||
echo json_encode(['success' => false, 'error' => 'Please provide the case number for your repeat application.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
payment_method = ?,
|
||||
payment_phone = ?,
|
||||
reduced_fee_eligibility = ?,
|
||||
is_repeat_application = ?,
|
||||
repeat_case_number = ?,
|
||||
step_reached = GREATEST(step_reached, 12)
|
||||
WHERE id = ?");
|
||||
$stmt->execute([
|
||||
$payment_method,
|
||||
$payment_phone,
|
||||
$reduced_fee_eligibility,
|
||||
$is_repeat_application,
|
||||
$repeat_case_number,
|
||||
$lpa_id
|
||||
]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => 13, 'message' => 'Payment details saved successfully.']);
|
||||
} elseif ($step === 13) {
|
||||
if (!$lpa_id) {
|
||||
echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 13.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = db()->prepare("UPDATE lpa_applications SET
|
||||
step_reached = GREATEST(step_reached, 13),
|
||||
status = 'completed'
|
||||
WHERE id = ?");
|
||||
$stmt->execute([$lpa_id]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $lpa_id, 'redirect' => 'dashboard.php', 'message' => 'Application submitted successfully.']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Invalid step provided.']);
|
||||
}
|
||||
|
||||
@ -15,8 +15,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
const formData = new FormData(form);
|
||||
|
||||
// If the button clicked was one of the Step 2 buttons, ensure next_action is set
|
||||
if (form.id === 'lpaFormStep2' && document.activeElement && document.activeElement.name === 'next_action') {
|
||||
// If the button clicked was one of the buttons with next_action, ensure it's set
|
||||
if (document.activeElement && document.activeElement.name === 'next_action') {
|
||||
formData.set('next_action', document.activeElement.value);
|
||||
}
|
||||
|
||||
@ -33,11 +33,14 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
submitBtn.innerHTML = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="me-2"><polyline points="20 6 9 17 4 12"></polyline></svg> Saved!';
|
||||
|
||||
setTimeout(() => {
|
||||
// Go to next step or reload to add another
|
||||
if (data.redirect) {
|
||||
window.location.href = data.redirect;
|
||||
} else if (data.next_step) {
|
||||
window.location.href = 'apply.php?step=' + data.next_step + '&id=' + data.id;
|
||||
if (data.next_step === parseInt(new URLSearchParams(window.location.search).get('step'))) {
|
||||
window.location.reload();
|
||||
}
|
||||
}
|
||||
}, 800);
|
||||
} else {
|
||||
alert(data.error || 'An error occurred. Please try again.');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user