kingkay000 commited on
Commit
4c4a5a5
·
verified ·
1 Parent(s): 5c2e2ab

Upload download_receipt.php

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