Spaces:
Running
Running
| require_once 'db_config.php'; | |
| require_once 'includes/ReceiptGenerator.php'; | |
| // Prevent output from messing up image headers | |
| ob_start(); | |
| ini_set('display_errors', 0); | |
| error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); | |
| // Validate input | |
| $receiptNo = $_GET['receipt_no'] ?? ''; | |
| if (empty($receiptNo)) { | |
| die("Receipt number is required."); | |
| } | |
| try { | |
| // 1. Fetch main payment records grouped by receipt | |
| // We join with student registrations to get name info | |
| // We join with school fees to get description | |
| // We join with academic levels to get level name (for Senior/Junior college logic) | |
| $sql = "SELECT pr.*, | |
| sf.description as fee_description, | |
| sr.last_name, sr.first_name, sr.other_name, sr.student_code, | |
| al.level_name | |
| FROM tb_account_payment_registers pr | |
| JOIN tb_account_school_fees sf ON pr.fee_id = sf.id | |
| JOIN tb_student_registrations sr ON pr.student_id = sr.id | |
| LEFT JOIN tb_academic_levels al ON sr.level_id = al.id | |
| WHERE pr.receipt_no = :receipt_no"; | |
| $stmt = $pdo->prepare($sql); | |
| $stmt->execute(['receipt_no' => $receiptNo]); | |
| $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
| if (empty($rows)) { | |
| die("Receipt not found."); | |
| } | |
| // 2. Prepare data structure for generator | |
| $firstRow = $rows[0]; | |
| $data = [ | |
| 'receipt_no' => $receiptNo, | |
| 'student_name' => trim($firstRow['last_name'] . ' ' . $firstRow['first_name'] . ' ' . ($firstRow['other_name'] ?? '')), | |
| 'student_code' => $firstRow['student_code'], | |
| 'level_name' => $firstRow['level_name'] ?? '', | |
| 'payment_date' => $firstRow['payment_date'], | |
| 'total_paid' => 0, | |
| 'allocations' => [] | |
| ]; | |
| // 3. Process each fee allocation to get billed/balance context | |
| foreach ($rows as $row) { | |
| $amountPaidHere = floatval($row['amount_paid']); | |
| $data['total_paid'] += $amountPaidHere; | |
| // Fetch Billed Amount (Actual Value) from Receivables | |
| $sqlBilled = "SELECT actual_value | |
| FROM tb_account_receivables | |
| WHERE student_id = :sid | |
| AND fee_id = :fid | |
| AND academic_session = :as | |
| AND term_of_session = :ts | |
| LIMIT 1"; | |
| $stmtBilled = $pdo->prepare($sqlBilled); | |
| $stmtBilled->execute([ | |
| 'sid' => $row['student_id'], | |
| 'fid' => $row['fee_id'], | |
| 'as' => $row['academic_session'], | |
| 'ts' => $row['term_of_session'] | |
| ]); | |
| $billedRes = $stmtBilled->fetch(PDO::FETCH_ASSOC); | |
| $amountBilled = floatval($billedRes['actual_value'] ?? 0); | |
| // Fetch Total Paid To Date (inclusive of this payment's date) | |
| // We sum all payments for this fee/student/session/term up to this date | |
| // Note: This matches the state "at the time of receipt" roughly | |
| $sqlPaid = "SELECT SUM(amount_paid) as total_paid | |
| FROM tb_account_payment_registers | |
| WHERE student_id = :sid | |
| AND fee_id = :fid | |
| AND academic_session = :as | |
| AND term_of_session = :ts | |
| AND payment_date <= :pd"; | |
| $stmtPaid = $pdo->prepare($sqlPaid); | |
| $stmtPaid->execute([ | |
| 'sid' => $row['student_id'], | |
| 'fid' => $row['fee_id'], | |
| 'as' => $row['academic_session'], | |
| 'ts' => $row['term_of_session'], | |
| 'pd' => $row['payment_date'] | |
| ]); | |
| $paidRes = $stmtPaid->fetch(PDO::FETCH_ASSOC); | |
| $totalPaidToDate = floatval($paidRes['total_paid'] ?? 0); | |
| $balance = $amountBilled - $totalPaidToDate; | |
| $data['allocations'][] = [ | |
| 'description' => $row['fee_description'], | |
| 'academic_session' => $row['academic_session'], | |
| 'term_of_session' => $row['term_of_session'], | |
| 'amount' => $amountPaidHere, | |
| 'amount_billed' => $amountBilled, | |
| 'total_paid_to_date' => $totalPaidToDate, | |
| 'balance' => $balance | |
| ]; | |
| } | |
| // 4. Generate Image | |
| $generator = new ReceiptGenerator(); | |
| $imageData = $generator->generate($data); | |
| // 5. Output | |
| ob_end_clean(); // Discard any warnings/output buffered so far | |
| header('Content-Type: image/png'); | |
| header('Content-Disposition: attachment; filename="receipt_' . $receiptNo . '.png"'); | |
| header('Content-Length: ' . strlen($imageData)); | |
| echo $imageData; | |
| } catch (Exception $e) { | |
| die("Error generating receipt: " . $e->getMessage()); | |
| } |