37684-vm/api/save_lpa.php
2026-02-28 19:00:07 +00:00

119 lines
6.1 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check for specific actions first
if (isset($_POST['action']) && $_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;
if (!$attorney_id || !$lpa_id) {
echo json_encode(['success' => 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;
}
$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, title, first_name, last_name, email, dob, address_line1, address_line2, address_line3, town, postcode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$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.']);
} 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.']);
}