false, 'error' => 'Missing IDs for deletion.']); exit; } try { $stmt = db()->prepare("DELETE FROM lpa_attorneys WHERE id = ? AND lpa_id = ?"); $stmt->execute([$attorney_id, $lpa_id]); echo json_encode(['success' => true, 'message' => 'Attorney removed.']); } catch (PDOException $e) { 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; $lpa_id = isset($_POST['lpa_id']) ? (int)$_POST['lpa_id'] : null; try { if ($step === 1) { $lpa_type = $_POST['lpa_type'] ?? ''; $donor_name = $_POST['donor_name'] ?? ''; $other_names = $_POST['other_names'] ?? ''; $donor_dob = $_POST['donor_dob'] ?? ''; $customer_email = $_POST['customer_email'] ?? ''; $address1 = $_POST['donor_address_line1'] ?? ''; $address2 = $_POST['donor_address_line2'] ?? ''; $town = $_POST['donor_town'] ?? ''; $postcode = $_POST['donor_postcode'] ?? ''; if (empty($lpa_type) || empty($donor_name) || empty($donor_dob) || empty($customer_email) || empty($address1) || empty($town) || empty($postcode)) { echo json_encode(['success' => false, 'error' => 'All fields are required for Step 1, including the address.']); exit; } if ($lpa_id) { // Update existing $stmt = db()->prepare("UPDATE lpa_applications SET lpa_type = ?, donor_name = ?, other_names = ?, donor_dob = ?, customer_email = ?, donor_address_line1 = ?, donor_address_line2 = ?, donor_town = ?, donor_postcode = ?, step_reached = GREATEST(step_reached, 1) WHERE id = ?"); $stmt->execute([$lpa_type, $donor_name, $other_names, $donor_dob, $customer_email, $address1, $address2, $town, $postcode, $lpa_id]); $id = $lpa_id; } else { // Create new $stmt = db()->prepare("INSERT INTO lpa_applications (practice_id, lpa_type, donor_name, other_names, donor_dob, customer_email, donor_address_line1, donor_address_line2, donor_town, donor_postcode, step_reached) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute([1, $lpa_type, $donor_name, $other_names, $donor_dob, $customer_email, $address1, $address2, $town, $postcode, 1]); $id = db()->lastInsertId(); } echo json_encode(['success' => true, 'id' => $id, 'next_step' => 2, 'message' => 'Step 1 saved successfully.']); } elseif ($step === 2) { if (!$lpa_id) { echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 2.']); 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 an 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 (?, 'primary', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $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, 2) WHERE id = ?"); $stmt->execute([$lpa_id]); $next_step = ($next_action === 'next_step') ? 3 : 2; echo json_encode(['success' => true, 'id' => $lpa_id, 'next_step' => $next_step, 'message' => 'Attorney saved successfully.']); } elseif ($step === 3) { if (!$lpa_id) { echo json_encode(['success' => false, 'error' => 'LPA ID is required for Step 3.']); exit; } $attorney_decision_type = $_POST['attorney_decision_type'] ?? ''; if (empty($attorney_decision_type)) { echo json_encode(['success' => false, 'error' => 'Please select how your attorneys should make decisions.']); exit; } $stmt = db()->prepare("UPDATE lpa_applications SET attorney_decision_type = ?, step_reached = GREATEST(step_reached, 3) WHERE id = ?"); $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.']); } } catch (PDOException $e) { error_log($e->getMessage()); echo json_encode(['success' => false, 'error' => 'Database error: ' . $e->getMessage()]); } } else { echo json_encode(['success' => false, 'error' => 'Invalid request method.']); }