Spaces:
Running
Running
Upload download_receipt.php
Browse files- easypay/download_receipt.php +60 -58
easypay/download_receipt.php
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
require_once 'db_config.php';
|
| 2 |
require_once 'includes/ReceiptGenerator.php';
|
| 3 |
|
|
@@ -9,14 +10,14 @@ error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
|
|
| 9 |
// Validate input
|
| 10 |
$receiptNo = $_GET['receipt_no'] ?? '';
|
| 11 |
if (empty($receiptNo)) {
|
| 12 |
-
die("Receipt number is required.");
|
| 13 |
}
|
| 14 |
|
| 15 |
try {
|
| 16 |
-
// 1. Fetch main payment records grouped by receipt
|
| 17 |
// We join with student registrations to get name info
|
| 18 |
// We join with school fees to get description
|
| 19 |
-
$sql = "SELECT pr.*,
|
| 20 |
sf.description as fee_description,
|
| 21 |
sr.last_name, sr.first_name, sr.other_name, sr.student_code
|
| 22 |
FROM tb_account_payment_registers pr
|
|
@@ -24,79 +25,80 @@ JOIN tb_account_school_fees sf ON pr.fee_id = sf.id
|
|
| 24 |
JOIN tb_student_registrations sr ON pr.student_id = sr.id
|
| 25 |
WHERE pr.receipt_no = :receipt_no";
|
| 26 |
|
| 27 |
-
$stmt = $pdo->prepare($sql);
|
| 28 |
-
$stmt->execute(['receipt_no' => $receiptNo]);
|
| 29 |
-
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
| 30 |
|
| 31 |
-
if (empty($rows)) {
|
| 32 |
-
die("Receipt not found.");
|
| 33 |
-
}
|
| 34 |
|
| 35 |
-
// 2. Prepare data structure for generator
|
| 36 |
-
$firstRow = $rows[0];
|
| 37 |
-
$data = [
|
| 38 |
-
'receipt_no' => $receiptNo,
|
| 39 |
-
'student_name' => trim($firstRow['last_name'] . ' ' . $firstRow['first_name'] . ' ' . ($firstRow['other_name'] ?? '')),
|
| 40 |
-
'student_code' => $firstRow['student_code'],
|
| 41 |
-
'payment_date' => $firstRow['payment_date'],
|
| 42 |
-
'total_paid' => 0,
|
| 43 |
-
'allocations' => []
|
| 44 |
-
];
|
| 45 |
|
| 46 |
-
// 3. Process each fee allocation to get billed/balance context
|
| 47 |
-
foreach ($rows as $row) {
|
| 48 |
-
$amountPaidHere = floatval($row['amount_paid']);
|
| 49 |
-
$data['total_paid'] += $amountPaidHere;
|
| 50 |
|
| 51 |
-
// Fetch Billed Amount (Actual Value) from Receivables
|
| 52 |
-
$sqlBilled = "SELECT actual_value
|
| 53 |
FROM tb_account_receivables
|
| 54 |
WHERE student_id = :sid
|
| 55 |
AND fee_id = :fid
|
| 56 |
AND academic_session = :as
|
| 57 |
AND term_of_session = :ts
|
| 58 |
LIMIT 1";
|
| 59 |
-
$stmtBilled = $pdo->prepare($sqlBilled);
|
| 60 |
-
$stmtBilled->execute([
|
| 61 |
-
'sid' => $row['student_id'],
|
| 62 |
-
'fid' => $row['fee_id'],
|
| 63 |
-
'as' => $row['academic_session'],
|
| 64 |
-
'ts' => $row['term_of_session']
|
| 65 |
-
]);
|
| 66 |
-
$billedRes = $stmtBilled->fetch(PDO::FETCH_ASSOC);
|
| 67 |
-
$amountBilled = floatval($billedRes['actual_value'] ?? 0);
|
| 68 |
|
| 69 |
-
// Fetch Total Paid To Date (inclusive of this payment's date)
|
| 70 |
// We sum all payments for this fee/student/session/term up to this date
|
| 71 |
// Note: This matches the state "at the time of receipt" roughly
|
| 72 |
-
$sqlPaid = "SELECT SUM(amount_paid) as total_paid
|
| 73 |
FROM tb_account_payment_registers
|
| 74 |
WHERE student_id = :sid
|
| 75 |
AND fee_id = :fid
|
| 76 |
AND academic_session = :as
|
| 77 |
AND term_of_session = :ts
|
| 78 |
-
AND payment_date <= :pd";
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
}
|
| 101 |
|
| 102 |
// 4. Generate Image
|
|
@@ -110,6 +112,6 @@ AND payment_date <= :pd"; $stmtPaid=$pdo->prepare($sqlPaid);
|
|
| 110 |
header('Content-Length: ' . strlen($imageData));
|
| 111 |
echo $imageData;
|
| 112 |
|
| 113 |
-
|
| 114 |
die("Error generating receipt: " . $e->getMessage());
|
| 115 |
-
|
|
|
|
| 1 |
+
<?php
|
| 2 |
require_once 'db_config.php';
|
| 3 |
require_once 'includes/ReceiptGenerator.php';
|
| 4 |
|
|
|
|
| 10 |
// Validate input
|
| 11 |
$receiptNo = $_GET['receipt_no'] ?? '';
|
| 12 |
if (empty($receiptNo)) {
|
| 13 |
+
die("Receipt number is required.");
|
| 14 |
}
|
| 15 |
|
| 16 |
try {
|
| 17 |
+
// 1. Fetch main payment records grouped by receipt
|
| 18 |
// We join with student registrations to get name info
|
| 19 |
// We join with school fees to get description
|
| 20 |
+
$sql = "SELECT pr.*,
|
| 21 |
sf.description as fee_description,
|
| 22 |
sr.last_name, sr.first_name, sr.other_name, sr.student_code
|
| 23 |
FROM tb_account_payment_registers pr
|
|
|
|
| 25 |
JOIN tb_student_registrations sr ON pr.student_id = sr.id
|
| 26 |
WHERE pr.receipt_no = :receipt_no";
|
| 27 |
|
| 28 |
+
$stmt = $pdo->prepare($sql);
|
| 29 |
+
$stmt->execute(['receipt_no' => $receiptNo]);
|
| 30 |
+
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
| 31 |
|
| 32 |
+
if (empty($rows)) {
|
| 33 |
+
die("Receipt not found.");
|
| 34 |
+
}
|
| 35 |
|
| 36 |
+
// 2. Prepare data structure for generator
|
| 37 |
+
$firstRow = $rows[0];
|
| 38 |
+
$data = [
|
| 39 |
+
'receipt_no' => $receiptNo,
|
| 40 |
+
'student_name' => trim($firstRow['last_name'] . ' ' . $firstRow['first_name'] . ' ' . ($firstRow['other_name'] ?? '')),
|
| 41 |
+
'student_code' => $firstRow['student_code'],
|
| 42 |
+
'payment_date' => $firstRow['payment_date'],
|
| 43 |
+
'total_paid' => 0,
|
| 44 |
+
'allocations' => []
|
| 45 |
+
];
|
| 46 |
|
| 47 |
+
// 3. Process each fee allocation to get billed/balance context
|
| 48 |
+
foreach ($rows as $row) {
|
| 49 |
+
$amountPaidHere = floatval($row['amount_paid']);
|
| 50 |
+
$data['total_paid'] += $amountPaidHere;
|
| 51 |
|
| 52 |
+
// Fetch Billed Amount (Actual Value) from Receivables
|
| 53 |
+
$sqlBilled = "SELECT actual_value
|
| 54 |
FROM tb_account_receivables
|
| 55 |
WHERE student_id = :sid
|
| 56 |
AND fee_id = :fid
|
| 57 |
AND academic_session = :as
|
| 58 |
AND term_of_session = :ts
|
| 59 |
LIMIT 1";
|
| 60 |
+
$stmtBilled = $pdo->prepare($sqlBilled);
|
| 61 |
+
$stmtBilled->execute([
|
| 62 |
+
'sid' => $row['student_id'],
|
| 63 |
+
'fid' => $row['fee_id'],
|
| 64 |
+
'as' => $row['academic_session'],
|
| 65 |
+
'ts' => $row['term_of_session']
|
| 66 |
+
]);
|
| 67 |
+
$billedRes = $stmtBilled->fetch(PDO::FETCH_ASSOC);
|
| 68 |
+
$amountBilled = floatval($billedRes['actual_value'] ?? 0);
|
| 69 |
|
| 70 |
+
// Fetch Total Paid To Date (inclusive of this payment's date)
|
| 71 |
// We sum all payments for this fee/student/session/term up to this date
|
| 72 |
// Note: This matches the state "at the time of receipt" roughly
|
| 73 |
+
$sqlPaid = "SELECT SUM(amount_paid) as total_paid
|
| 74 |
FROM tb_account_payment_registers
|
| 75 |
WHERE student_id = :sid
|
| 76 |
AND fee_id = :fid
|
| 77 |
AND academic_session = :as
|
| 78 |
AND term_of_session = :ts
|
| 79 |
+
AND payment_date <= :pd";
|
| 80 |
+
$stmtPaid = $pdo->prepare($sqlPaid);
|
| 81 |
+
$stmtPaid->execute([
|
| 82 |
+
'sid' => $row['student_id'],
|
| 83 |
+
'fid' => $row['fee_id'],
|
| 84 |
+
'as' => $row['academic_session'],
|
| 85 |
+
'ts' => $row['term_of_session'],
|
| 86 |
+
'pd' => $row['payment_date']
|
| 87 |
+
]);
|
| 88 |
+
$paidRes = $stmtPaid->fetch(PDO::FETCH_ASSOC);
|
| 89 |
+
$totalPaidToDate = floatval($paidRes['total_paid'] ?? 0);
|
| 90 |
|
| 91 |
+
$balance = $amountBilled - $totalPaidToDate;
|
| 92 |
|
| 93 |
+
$data['allocations'][] = [
|
| 94 |
+
'description' => $row['fee_description'],
|
| 95 |
+
'academic_session' => $row['academic_session'],
|
| 96 |
+
'term_of_session' => $row['term_of_session'],
|
| 97 |
+
'amount' => $amountPaidHere,
|
| 98 |
+
'amount_billed' => $amountBilled,
|
| 99 |
+
'total_paid_to_date' => $totalPaidToDate,
|
| 100 |
+
'balance' => $balance
|
| 101 |
+
];
|
| 102 |
}
|
| 103 |
|
| 104 |
// 4. Generate Image
|
|
|
|
| 112 |
header('Content-Length: ' . strlen($imageData));
|
| 113 |
echo $imageData;
|
| 114 |
|
| 115 |
+
} catch (Exception $e) {
|
| 116 |
die("Error generating receipt: " . $e->getMessage());
|
| 117 |
+
}
|