Skip to content

Commit beb036f

Browse files
committed
Edited files
1 parent 0e1bba0 commit beb036f

File tree

7 files changed

+242
-193
lines changed

7 files changed

+242
-193
lines changed

admin/bill-details.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function getPaymentStatus($amount_paid, $total_amount)
331331
</div>
332332
</div>
333333
<div class="export-button-container" style="margin-bottom: 1rem;">
334-
<form action="export_payments.php" method="POST">
334+
<form action="./bill-sheet" method="POST">
335335
<input type="hidden" name="bill_id" value="<?php echo $bill_id; ?>">
336336
<button type="submit" class="export-button">Export as Excel</button>
337337
</form>

admin/bill-sheet.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
ini_set('display_errors', 1);
3+
ini_set('display_startup_errors', 1);
4+
error_reporting(E_ALL);
5+
session_start();
6+
7+
require '../includes/phpspreadsheet/vendor/autoload.php';
8+
include '../config/config.php';
9+
10+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
11+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
12+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
13+
use PhpOffice\PhpSpreadsheet\Style\Border;
14+
use PhpOffice\PhpSpreadsheet\Style\Fill;
15+
16+
// Verify bill_id is provided and admin is logged in
17+
if (!isset($_POST['bill_id']) || !isset($_SESSION['admin_id'])) {
18+
header("Location: bills.php");
19+
exit();
20+
}
21+
22+
$bill_id = $_POST['bill_id'];
23+
24+
// Get bill details with student info
25+
$stmt = $conn->prepare("
26+
SELECT b.*, u.fullname as student_name, u.email as student_email,
27+
u.phone as student_phone, u.matric_no as student_matric
28+
FROM bills b
29+
LEFT JOIN users u ON u.matric_no = b.matric_no
30+
WHERE b.id = ? AND b.creator_id = ?
31+
");
32+
$stmt->bind_param("ii", $bill_id, $_SESSION['admin_id']);
33+
$stmt->execute();
34+
$bill = $stmt->get_result()->fetch_assoc();
35+
36+
if (!$bill) {
37+
header("Location: bills.php");
38+
exit();
39+
}
40+
41+
// Get payment history
42+
$stmt = $conn->prepare("
43+
SELECT p.*, u.fullname as paid_by, u.matric_no as student_matric
44+
FROM payments p
45+
LEFT JOIN users u ON u.id = p.uid
46+
WHERE p.bill_id = ?
47+
ORDER BY p.created_at DESC
48+
");
49+
$stmt->bind_param("i", $bill_id);
50+
$stmt->execute();
51+
$payments = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
52+
53+
// Create new Spreadsheet
54+
$spreadsheet = new Spreadsheet();
55+
$sheet = $spreadsheet->getActiveSheet();
56+
57+
// Set document properties
58+
$spreadsheet->getProperties()
59+
->setCreator('Payment System')
60+
->setLastModifiedBy('Payment System')
61+
->setTitle('Payment History - ' . $bill['name'])
62+
->setSubject('Payment History Export')
63+
->setDescription('Payment history export for ' . $bill['name']);
64+
65+
// Style header cells
66+
$headerStyle = [
67+
'font' => [
68+
'bold' => true,
69+
'color' => ['rgb' => 'FFFFFF'],
70+
],
71+
'fill' => [
72+
'fillType' => Fill::FILL_SOLID,
73+
'startColor' => ['rgb' => '4B5563'],
74+
],
75+
'alignment' => [
76+
'horizontal' => Alignment::HORIZONTAL_CENTER,
77+
'vertical' => Alignment::VERTICAL_CENTER,
78+
],
79+
'borders' => [
80+
'allBorders' => [
81+
'borderStyle' => Border::BORDER_THIN,
82+
'color' => ['rgb' => '000000'],
83+
],
84+
],
85+
];
86+
87+
// Add bill information
88+
$sheet->setCellValue('A1', 'Bill Details');
89+
$sheet->mergeCells('A1:F1');
90+
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
91+
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
92+
93+
$sheet->setCellValue('A3', 'Bill Name:');
94+
$sheet->setCellValue('B3', $bill['name']);
95+
$sheet->setCellValue('A4', 'Department:');
96+
$sheet->setCellValue('B4', $bill['department']);
97+
$sheet->setCellValue('A5', 'Level:');
98+
$sheet->setCellValue('B5', $bill['level']);
99+
$sheet->setCellValue('A6', 'Amount:');
100+
$sheet->setCellValue('B6', '' . number_format($bill['price'], 2));
101+
$sheet->setCellValue('A7', 'Start Date:');
102+
$sheet->setCellValue('B7', date('M j, Y', strtotime($bill['start_date'])));
103+
$sheet->setCellValue('A8', 'End Date:');
104+
$sheet->setCellValue('B8', date('M j, Y', strtotime($bill['end_date'])));
105+
106+
// Add student information
107+
$sheet->setCellValue('D3', 'Student Name:');
108+
$sheet->setCellValue('E3', $bill['student_name']);
109+
$sheet->setCellValue('D4', 'Matric Number:');
110+
$sheet->setCellValue('E4', $bill['student_matric']);
111+
$sheet->setCellValue('D5', 'Email:');
112+
$sheet->setCellValue('E5', $bill['student_email']);
113+
$sheet->setCellValue('D6', 'Phone:');
114+
$sheet->setCellValue('E6', $bill['student_phone']);
115+
116+
// Style info cells
117+
$sheet->getStyle('A3:A8')->getFont()->setBold(true);
118+
$sheet->getStyle('D3:D6')->getFont()->setBold(true);
119+
120+
// Add payment history header
121+
$sheet->setCellValue('A10', 'Payment History');
122+
$sheet->mergeCells('A10:F10');
123+
$sheet->getStyle('A10')->getFont()->setBold(true)->setSize(12);
124+
$sheet->getStyle('A10')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
125+
126+
// Set payment history headers
127+
$headers = ['Date', 'Reference ID', 'Paid By', 'Matric Number', 'Amount', 'Status'];
128+
$col = 'A';
129+
$row = 12;
130+
foreach ($headers as $header) {
131+
$sheet->setCellValue($col . $row, $header);
132+
$sheet->getStyle($col . $row)->applyFromArray($headerStyle);
133+
$col++;
134+
}
135+
136+
// Add payment data
137+
$row = 13;
138+
foreach ($payments as $payment) {
139+
$sheet->setCellValue('A' . $row, date('M j, Y H:i', strtotime($payment['created_at'])));
140+
$sheet->setCellValue('B' . $row, $payment['reference_id']);
141+
$sheet->setCellValue('C' . $row, $payment['paid_by']);
142+
$sheet->setCellValue('D' . $row, $payment['student_matric']);
143+
$sheet->setCellValue('E' . $row, '' . number_format($payment['amount_paid'], 2));
144+
$sheet->setCellValue('F' . $row, $payment['status']);
145+
146+
// Style amount cells
147+
$sheet->getStyle('E' . $row)->getNumberFormat()->setFormatCode('#,##0.00');
148+
$row++;
149+
}
150+
151+
// Auto-size columns
152+
foreach (range('A', 'F') as $col) {
153+
$sheet->getColumnDimension($col)->setAutoSize(true);
154+
}
155+
156+
// Create Excel file
157+
$writer = new Xlsx($spreadsheet);
158+
159+
// Set headers for download
160+
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
161+
header('Content-Disposition: attachment;filename="payment_history_' . date('Y-m-d') . '.xlsx"');
162+
header('Cache-Control: max-age=0');
163+
164+
// Save file to PHP output
165+
$writer->save('php://output');
166+
167+
$stmt->close();
168+
$conn->close();
60 KB
Loading
60 KB
Loading
4.35 KB
Loading

includes/admin/nav.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
include '../config/secrets.php';
88
include '../includes/admin/functions.php';
99
$admin_id = $_SESSION['admin_id'];
10-
$admin_details = getAdminDetails($_SESSION["university_id"]);
10+
$admin_details = getAdminDetails($_SESSION["admin_id"]);
1111

1212
?>
1313
<!DOCTYPE html>

0 commit comments

Comments
 (0)