Spaces:
Running
Running
Create ajax_handlers_inactives.php
Browse files
easypay/ajax_handlers_inactives.php
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?php
|
| 2 |
+
/**
|
| 3 |
+
* AJAX Handlers
|
| 4 |
+
* Endpoints for student search and teller lookup
|
| 5 |
+
*/
|
| 6 |
+
|
| 7 |
+
require_once 'db_config.php';
|
| 8 |
+
|
| 9 |
+
header('Content-Type: application/json');
|
| 10 |
+
|
| 11 |
+
$action = $_GET['action'] ?? '';
|
| 12 |
+
|
| 13 |
+
try {
|
| 14 |
+
switch ($action) {
|
| 15 |
+
case 'search_students':
|
| 16 |
+
searchStudents($pdo);
|
| 17 |
+
break;
|
| 18 |
+
|
| 19 |
+
case 'lookup_teller':
|
| 20 |
+
lookupTeller($pdo);
|
| 21 |
+
break;
|
| 22 |
+
|
| 23 |
+
default:
|
| 24 |
+
echo json_encode(['error' => 'Invalid action']);
|
| 25 |
+
}
|
| 26 |
+
} catch (Exception $e) {
|
| 27 |
+
echo json_encode(['error' => $e->getMessage()]);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
/**
|
| 31 |
+
* Search for students by name or student code
|
| 32 |
+
*/
|
| 33 |
+
function searchStudents($pdo)
|
| 34 |
+
{
|
| 35 |
+
$search = $_GET['search'] ?? '';
|
| 36 |
+
|
| 37 |
+
if (strlen($search) < 2) {
|
| 38 |
+
echo json_encode([]);
|
| 39 |
+
return;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
$sql = "SELECT
|
| 43 |
+
id,
|
| 44 |
+
student_code,
|
| 45 |
+
CONCAT(last_name, ' ', first_name, ' ', COALESCE(other_name, '')) AS full_name
|
| 46 |
+
FROM tb_student_registrations
|
| 47 |
+
WHERE
|
| 48 |
+
admission_status = 'Active' -- New condition added here
|
| 49 |
+
AND (
|
| 50 |
+
student_code LIKE :search1
|
| 51 |
+
OR last_name LIKE :search2
|
| 52 |
+
OR first_name LIKE :search3
|
| 53 |
+
OR other_name LIKE :search4
|
| 54 |
+
OR CONCAT(last_name, ' ', first_name, ' ', COALESCE(other_name, '')) LIKE :search5
|
| 55 |
+
)
|
| 56 |
+
ORDER BY last_name, first_name
|
| 57 |
+
LIMIT 20";
|
| 58 |
+
|
| 59 |
+
$stmt = $pdo->prepare($sql);
|
| 60 |
+
$searchParam = '%' . $search . '%';
|
| 61 |
+
$stmt->execute([
|
| 62 |
+
'search1' => $searchParam,
|
| 63 |
+
'search2' => $searchParam,
|
| 64 |
+
'search3' => $searchParam,
|
| 65 |
+
'search4' => $searchParam,
|
| 66 |
+
'search5' => $searchParam
|
| 67 |
+
]);
|
| 68 |
+
|
| 69 |
+
$results = $stmt->fetchAll();
|
| 70 |
+
echo json_encode($results);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
/**
|
| 74 |
+
* Lookup bank statement by teller number
|
| 75 |
+
*/
|
| 76 |
+
function lookupTeller($pdo)
|
| 77 |
+
{
|
| 78 |
+
$tellerNumber = $_GET['teller_number'] ?? '';
|
| 79 |
+
|
| 80 |
+
if (empty($tellerNumber)) {
|
| 81 |
+
echo json_encode(['error' => 'Teller number is required']);
|
| 82 |
+
return;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// Query to find bank statement and calculate unreconciled amount
|
| 86 |
+
$sql = "SELECT
|
| 87 |
+
bs.id,
|
| 88 |
+
bs.description,
|
| 89 |
+
bs.amount_paid,
|
| 90 |
+
bs.payment_date,
|
| 91 |
+
COALESCE(fp.total_registered_fee, 0.00) AS registered_amount,
|
| 92 |
+
(bs.amount_paid - COALESCE(fp.total_registered_fee, 0.00)) AS unreconciled_amount
|
| 93 |
+
FROM tb_account_bank_statements bs
|
| 94 |
+
LEFT JOIN (
|
| 95 |
+
SELECT teller_no, SUM(amount_paid) AS total_registered_fee
|
| 96 |
+
FROM tb_account_school_fee_payments
|
| 97 |
+
GROUP BY teller_no
|
| 98 |
+
) fp ON SUBSTRING_INDEX(bs.description, ' ', -1) = fp.teller_no
|
| 99 |
+
WHERE SUBSTRING_INDEX(bs.description, ' ', -1) = :teller_number
|
| 100 |
+
HAVING unreconciled_amount >= 0
|
| 101 |
+
LIMIT 1";
|
| 102 |
+
|
| 103 |
+
$stmt = $pdo->prepare($sql);
|
| 104 |
+
$stmt->execute(['teller_number' => $tellerNumber]);
|
| 105 |
+
|
| 106 |
+
$result = $stmt->fetch();
|
| 107 |
+
|
| 108 |
+
if (!$result) {
|
| 109 |
+
echo json_encode(['error' => 'Teller number not found or fully reconciled']);
|
| 110 |
+
return;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// Extract teller name (description without last token)
|
| 114 |
+
$descParts = explode(' ', $result['description']);
|
| 115 |
+
array_pop($descParts); // Remove teller number
|
| 116 |
+
$tellerName = implode(' ', $descParts);
|
| 117 |
+
|
| 118 |
+
$result['teller_name'] = $tellerName;
|
| 119 |
+
$result['teller_no'] = $tellerNumber;
|
| 120 |
+
|
| 121 |
+
echo json_encode($result);
|
| 122 |
+
}
|