Spaces:
Running
Running
Upload download_receipt.php
Browse files- easypay/download_receipt.php +85 -81
easypay/download_receipt.php
CHANGED
|
@@ -1,99 +1,102 @@
|
|
| 1 |
-
<?php
|
| 2 |
require_once 'db_config.php';
|
| 3 |
require_once 'includes/ReceiptGenerator.php';
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
// Validate input
|
| 6 |
$receiptNo = $_GET['receipt_no'] ?? '';
|
| 7 |
if (empty($receiptNo)) {
|
| 8 |
-
|
| 9 |
}
|
| 10 |
|
| 11 |
try {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
$totalPaidToDate = floatval($paidRes['total_paid'] ?? 0);
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
}
|
| 98 |
|
| 99 |
// 4. Generate Image
|
|
@@ -101,11 +104,12 @@ try {
|
|
| 101 |
$imageData = $generator->generate($data);
|
| 102 |
|
| 103 |
// 5. Output
|
|
|
|
| 104 |
header('Content-Type: image/png');
|
| 105 |
header('Content-Disposition: attachment; filename="receipt_' . $receiptNo . '.png"');
|
| 106 |
header('Content-Length: ' . strlen($imageData));
|
| 107 |
echo $imageData;
|
| 108 |
|
| 109 |
-
} catch (Exception $e) {
|
| 110 |
die("Error generating receipt: " . $e->getMessage());
|
| 111 |
-
}
|
|
|
|
|
|
|
| 1 |
require_once 'db_config.php';
|
| 2 |
require_once 'includes/ReceiptGenerator.php';
|
| 3 |
|
| 4 |
+
// Prevent output from messing up image headers
|
| 5 |
+
ob_start();
|
| 6 |
+
ini_set('display_errors', 0);
|
| 7 |
+
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
|
| 8 |
+
|
| 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
|
| 23 |
+
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"; $stmtPaid=$pdo->prepare($sqlPaid);
|
| 79 |
+
$stmtPaid->execute([
|
| 80 |
+
'sid' => $row['student_id'],
|
| 81 |
+
'fid' => $row['fee_id'],
|
| 82 |
+
'as' => $row['academic_session'],
|
| 83 |
+
'ts' => $row['term_of_session'],
|
| 84 |
+
'pd' => $row['payment_date']
|
| 85 |
+
]);
|
| 86 |
+
$paidRes = $stmtPaid->fetch(PDO::FETCH_ASSOC);
|
| 87 |
+
$totalPaidToDate = floatval($paidRes['total_paid'] ?? 0);
|
|
|
|
| 88 |
|
| 89 |
+
$balance = $amountBilled - $totalPaidToDate;
|
| 90 |
|
| 91 |
+
$data['allocations'][] = [
|
| 92 |
+
'description' => $row['fee_description'],
|
| 93 |
+
'academic_session' => $row['academic_session'],
|
| 94 |
+
'term_of_session' => $row['term_of_session'],
|
| 95 |
+
'amount' => $amountPaidHere,
|
| 96 |
+
'amount_billed' => $amountBilled,
|
| 97 |
+
'total_paid_to_date' => $totalPaidToDate,
|
| 98 |
+
'balance' => $balance
|
| 99 |
+
];
|
| 100 |
}
|
| 101 |
|
| 102 |
// 4. Generate Image
|
|
|
|
| 104 |
$imageData = $generator->generate($data);
|
| 105 |
|
| 106 |
// 5. Output
|
| 107 |
+
ob_end_clean(); // Discard any warnings/output buffered so far
|
| 108 |
header('Content-Type: image/png');
|
| 109 |
header('Content-Disposition: attachment; filename="receipt_' . $receiptNo . '.png"');
|
| 110 |
header('Content-Length: ' . strlen($imageData));
|
| 111 |
echo $imageData;
|
| 112 |
|
| 113 |
+
} catch (Exception $e) {
|
| 114 |
die("Error generating receipt: " . $e->getMessage());
|
| 115 |
+
}
|