Spaces:
Running
Running
Upload download_receipt.php
Browse files- easypay/download_receipt.php +134 -0
easypay/download_receipt.php
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?php
|
| 2 |
+
require_once 'db_config.php';
|
| 3 |
+
require_once 'includes/ReceiptGenerator.php';
|
| 4 |
+
|
| 5 |
+
// Prevent output from messing up image headers
|
| 6 |
+
ob_start();
|
| 7 |
+
ini_set('display_errors', 0);
|
| 8 |
+
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
|
| 9 |
+
|
| 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 Receipt Meta Info (Student, Date)
|
| 18 |
+
$sqlInfo = "SELECT pr.student_id, pr.payment_date,
|
| 19 |
+
sr.last_name, sr.first_name, sr.other_name, sr.student_code,
|
| 20 |
+
al.level_name
|
| 21 |
+
FROM tb_account_payment_registers pr
|
| 22 |
+
JOIN tb_student_registrations sr ON pr.student_id = sr.id
|
| 23 |
+
LEFT JOIN tb_academic_levels al ON sr.level_id = al.id
|
| 24 |
+
WHERE pr.receipt_no = :receipt_no
|
| 25 |
+
LIMIT 1";
|
| 26 |
+
|
| 27 |
+
$stmt = $pdo->prepare($sqlInfo);
|
| 28 |
+
$stmt->execute(['receipt_no' => $receiptNo]);
|
| 29 |
+
$receiptInfo = $stmt->fetch(PDO::FETCH_ASSOC);
|
| 30 |
+
|
| 31 |
+
if (!$receiptInfo) {
|
| 32 |
+
die("Receipt not found.");
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
$studentId = $receiptInfo['student_id'];
|
| 36 |
+
$paymentDate = $receiptInfo['payment_date'];
|
| 37 |
+
|
| 38 |
+
// 2. Fetch All Fees for Student (from receivables)
|
| 39 |
+
$sqlFees = "SELECT ar.fee_id, ar.actual_value as amount_billed,
|
| 40 |
+
ar.academic_session, ar.term_of_session,
|
| 41 |
+
sf.description as fee_description
|
| 42 |
+
FROM tb_account_receivables ar
|
| 43 |
+
JOIN tb_account_school_fees sf ON ar.fee_id = sf.id
|
| 44 |
+
WHERE ar.student_id = :sid
|
| 45 |
+
ORDER BY ar.academic_session ASC, ar.term_of_session ASC";
|
| 46 |
+
|
| 47 |
+
$stmtFees = $pdo->prepare($sqlFees);
|
| 48 |
+
$stmtFees->execute(['sid' => $studentId]);
|
| 49 |
+
$allFees = $stmtFees->fetchAll(PDO::FETCH_ASSOC);
|
| 50 |
+
|
| 51 |
+
$allocations = [];
|
| 52 |
+
$receiptTotalPaid = 0;
|
| 53 |
+
|
| 54 |
+
foreach ($allFees as $fee) {
|
| 55 |
+
// Calculate Paid To Date (up to this receipt's date)
|
| 56 |
+
$sqlPaid = "SELECT SUM(amount_paid) as total_paid
|
| 57 |
+
FROM tb_account_payment_registers
|
| 58 |
+
WHERE student_id = :sid
|
| 59 |
+
AND fee_id = :fid
|
| 60 |
+
AND academic_session = :as
|
| 61 |
+
AND term_of_session = :ts
|
| 62 |
+
AND payment_date <= :pd";
|
| 63 |
+
|
| 64 |
+
$stmtPaid = $pdo->prepare($sqlPaid);
|
| 65 |
+
$stmtPaid->execute([
|
| 66 |
+
'sid' => $studentId,
|
| 67 |
+
'fid' => $fee['fee_id'],
|
| 68 |
+
'as' => $fee['academic_session'],
|
| 69 |
+
'ts' => $fee['term_of_session'],
|
| 70 |
+
'pd' => $paymentDate
|
| 71 |
+
]);
|
| 72 |
+
$paidResult = $stmtPaid->fetch(PDO::FETCH_ASSOC);
|
| 73 |
+
$paidToDate = floatval($paidResult['total_paid'] ?? 0);
|
| 74 |
+
|
| 75 |
+
// Calculate Amount paid IN THIS RECEIPT (for total calculation)
|
| 76 |
+
$sqlReceiptPay = "SELECT SUM(amount_paid) as receipt_paid
|
| 77 |
+
FROM tb_account_payment_registers
|
| 78 |
+
WHERE receipt_no = :rno
|
| 79 |
+
AND fee_id = :fid
|
| 80 |
+
AND academic_session = :as
|
| 81 |
+
AND term_of_session = :ts";
|
| 82 |
+
$stmtReceiptPay = $pdo->prepare($sqlReceiptPay);
|
| 83 |
+
$stmtReceiptPay->execute([
|
| 84 |
+
'rno' => $receiptNo,
|
| 85 |
+
'fid' => $fee['fee_id'],
|
| 86 |
+
'as' => $fee['academic_session'],
|
| 87 |
+
'ts' => $fee['term_of_session']
|
| 88 |
+
]);
|
| 89 |
+
$receiptPayResult = $stmtReceiptPay->fetch(PDO::FETCH_ASSOC);
|
| 90 |
+
$paidInReceipt = floatval($receiptPayResult['receipt_paid'] ?? 0);
|
| 91 |
+
|
| 92 |
+
$receiptTotalPaid += $paidInReceipt;
|
| 93 |
+
$balance = floatval($fee['amount_billed']) - $paidToDate;
|
| 94 |
+
|
| 95 |
+
// Condition: Show if (Balance > 0) OR (PaidInReceipt > 0)
|
| 96 |
+
// Helps filter out old fully paid fees, but keeps current payments even if they zeroed the balance
|
| 97 |
+
if ($balance > 0.001 || $paidInReceipt > 0.001) {
|
| 98 |
+
$allocations[] = [
|
| 99 |
+
'description' => $fee['fee_description'],
|
| 100 |
+
'academic_session' => $fee['academic_session'],
|
| 101 |
+
'term_of_session' => $fee['term_of_session'],
|
| 102 |
+
'amount_billed' => floatval($fee['amount_billed']),
|
| 103 |
+
'amount' => $paidInReceipt,
|
| 104 |
+
'total_paid_to_date' => $paidToDate,
|
| 105 |
+
'balance' => $balance
|
| 106 |
+
];
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// 3. Prepare data structure for generator
|
| 111 |
+
$data = [
|
| 112 |
+
'receipt_no' => $receiptNo,
|
| 113 |
+
'student_name' => trim($receiptInfo['last_name'] . ' ' . $receiptInfo['first_name'] . ' ' . ($receiptInfo['other_name'] ?? '')),
|
| 114 |
+
'student_code' => $receiptInfo['student_code'],
|
| 115 |
+
'level_name' => $receiptInfo['level_name'] ?? '',
|
| 116 |
+
'payment_date' => $paymentDate,
|
| 117 |
+
'total_paid' => $receiptTotalPaid,
|
| 118 |
+
'allocations' => $allocations
|
| 119 |
+
];
|
| 120 |
+
|
| 121 |
+
// 4. Generate Image
|
| 122 |
+
$generator = new ReceiptGenerator();
|
| 123 |
+
$imageData = $generator->generate($data);
|
| 124 |
+
|
| 125 |
+
// 5. Output
|
| 126 |
+
ob_end_clean(); // Discard any warnings/output buffered so far
|
| 127 |
+
header('Content-Type: image/png');
|
| 128 |
+
header('Content-Disposition: attachment; filename="receipt_' . $receiptNo . '.png"');
|
| 129 |
+
header('Content-Length: ' . strlen($imageData));
|
| 130 |
+
echo $imageData;
|
| 131 |
+
|
| 132 |
+
} catch (Exception $e) {
|
| 133 |
+
die("Error generating receipt: " . $e->getMessage());
|
| 134 |
+
}
|