forked from Kishore0-0-7/Hospital-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
366 lines (324 loc) · 11.5 KB
/
Copy pathexport.php
File metadata and controls
366 lines (324 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
require('fpdf/fpdf.php'); // Using FPDF library
$con = mysqli_connect("localhost","root","","myhmsdb");
// Get export parameters
$type = $_GET['type'] ?? '';
$format = $_GET['format'] ?? '';
// Validate parameters
if (empty($type) || empty($format)) {
die('Invalid parameters');
}
// Get filter parameters
$spec = $_GET['spec'] ?? '';
$maxFees = $_GET['maxFees'] ?? '';
$gender = $_GET['gender'] ?? '';
$ageRange = $_GET['ageRange'] ?? '';
$status = $_GET['status'] ?? '';
$date = $_GET['date'] ?? '';
$doctor = $_GET['doctor'] ?? '';
$search = $_GET['search'] ?? '';
// Get data based on type with filters
switch ($type) {
case 'doctors':
$query = "SELECT username as 'Doctor Name', spec as 'Specialization', email as 'Email', docFees as 'Fees' FROM doctb WHERE 1=1";
if (!empty($spec)) {
$query .= " AND spec = '" . mysqli_real_escape_string($con, $spec) . "'";
}
if (!empty($maxFees)) {
$query .= " AND docFees <= " . (float)$maxFees;
}
if (!empty($search)) {
$search = mysqli_real_escape_string($con, $search);
$query .= " AND (username LIKE '%$search%' OR email LIKE '%$search%')";
}
$headers = ['Doctor Name', 'Specialization', 'Email', 'Fees'];
$filename = 'doctors_list';
break;
case 'patients':
$query = "SELECT fname as 'First Name', lname as 'Last Name', gender as 'Gender', email as 'Email', contact as 'Contact' FROM patreg WHERE 1=1";
if (!empty($gender)) {
$query .= " AND gender = '" . mysqli_real_escape_string($con, $gender) . "'";
}
if (!empty($ageRange)) {
$ages = explode('-', $ageRange);
if (count($ages) == 2) {
$query .= " AND age BETWEEN " . (int)$ages[0] . " AND " . (int)$ages[1];
}
}
if (!empty($search)) {
$search = mysqli_real_escape_string($con, $search);
$query .= " AND (fname LIKE '%$search%' OR lname LIKE '%$search%' OR email LIKE '%$search%' OR contact LIKE '%$search%')";
}
$headers = ['First Name', 'Last Name', 'Gender', 'Email', 'Contact'];
$filename = 'patients_list';
break;
case 'appointments':
$query = "SELECT
a.doctor as 'Doctor',
a.fname as 'First Name',
a.lname as 'Last Name',
a.appdate as 'Date',
a.apptime as 'Time',
CASE
WHEN a.userStatus=1 AND a.doctorStatus=1 AND a.status='completed' THEN 'Completed'
WHEN a.userStatus=1 AND a.doctorStatus=1 THEN 'Active'
WHEN a.userStatus=0 AND a.doctorStatus=1 THEN 'Cancelled by Patient'
WHEN a.userStatus=1 AND a.doctorStatus=0 THEN 'Cancelled by Doctor'
ELSE 'Unknown'
END as 'Status'
FROM appointmenttb a WHERE 1=1";
if (!empty($status)) {
switch($status) {
case 'active':
$query .= " AND userStatus=1 AND doctorStatus=1 AND status!='completed'";
break;
case 'completed':
$query .= " AND status='completed'";
break;
case 'cancelled_patient':
$query .= " AND userStatus=0 AND doctorStatus=1";
break;
case 'cancelled_doctor':
$query .= " AND userStatus=1 AND doctorStatus=0";
break;
}
}
if (!empty($date)) {
$query .= " AND appdate = '" . mysqli_real_escape_string($con, $date) . "'";
}
if (!empty($doctor)) {
$query .= " AND doctor = '" . mysqli_real_escape_string($con, $doctor) . "'";
}
if (!empty($search)) {
$search = mysqli_real_escape_string($con, $search);
$query .= " AND (fname LIKE '%$search%' OR lname LIKE '%$search%' OR doctor LIKE '%$search%')";
}
$headers = ['Doctor', 'First Name', 'Last Name', 'Date', 'Time', 'Status'];
$filename = 'appointments_list';
break;
case 'prescriptions':
$query = "SELECT
p.doctor as 'Doctor',
p.pid as 'Patient ID',
p.fname as 'First Name',
p.lname as 'Last Name',
p.appdate as 'Date',
p.disease as 'Disease',
p.allergy as 'Allergy',
p.prescription as 'Prescription'
FROM prestb p WHERE 1=1";
if (!empty($search)) {
$search = mysqli_real_escape_string($con, $search);
$query .= " AND (fname LIKE '%$search%' OR lname LIKE '%$search%' OR doctor LIKE '%$search%' OR disease LIKE '%$search%')";
}
$headers = ['Doctor', 'Patient ID', 'First Name', 'Last Name', 'Date', 'Disease', 'Allergy', 'Prescription'];
$filename = 'prescriptions_list';
break;
default:
die('Invalid type');
}
$result = mysqli_query($con, $query);
if (!$result) {
die('Query failed: ' . mysqli_error($con));
}
// Export based on format
switch ($format) {
case 'csv':
exportCSV($result, $headers, $filename);
break;
case 'excel':
exportExcel($result, $headers, $filename);
break;
case 'pdf':
exportPDF($result, $headers, $filename);
break;
default:
die('Invalid format');
}
function exportCSV($result, $headers, $filename) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
$output = fopen('php://output', 'w');
// Add headers
fputcsv($output, $headers);
// Add data rows
while ($row = mysqli_fetch_assoc($result)) {
fputcsv($output, $row);
}
fclose($output);
}
function exportExcel($result, $headers, $filename) {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
echo '<table border="1">';
// Add headers
echo '<tr>';
foreach ($headers as $header) {
echo '<th style="background-color: #f5f5f5; font-weight: bold;">' . htmlspecialchars($header) . '</th>';
}
echo '</tr>';
// Add data rows
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
foreach ($headers as $header) {
echo '<td>' . htmlspecialchars($row[$header] ?? '') . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
function exportPDF($result, $headers, $filename) {
class PDF extends FPDF {
protected $widths;
protected $aligns;
// Page header
function Header() {
if (file_exists('images/favicon.png')) {
$this->Image('images/favicon.png', 10, 6, 30);
}
$this->SetFont('Arial', 'B', 15);
// Move title to center of page
$this->Cell(0, 10, 'Hospital Management System', 0, 0, 'C');
$this->Ln(20);
}
// Page footer
function Footer() {
$this->SetY(-15);
$this->SetFont('Arial', 'I', 8);
$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
}
// Set the array of column widths
function SetWidths($w) {
$this->widths = $w;
}
// Set the array of column alignments
function SetAligns($a) {
$this->aligns = $a;
}
// Calculate the height of the row
function Row($data) {
$nb = 0;
for($i = 0; $i < count($data); $i++) {
$nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
}
$h = 6 * $nb;
$this->CheckPageBreak($h);
for($i = 0; $i < count($data); $i++) {
$w = $this->widths[$i];
$a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
$x = $this->GetX();
$y = $this->GetY();
$this->Rect($x, $y, $w, $h);
$this->MultiCell($w, 6, $data[$i], 0, $a);
$this->SetXY($x + $w, $y);
}
$this->Ln($h);
}
function CheckPageBreak($h) {
if($this->GetY() + $h > $this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
}
function NbLines($w, $txt) {
$cw = &$this->CurrentFont['cw'];
if($w == 0)
$w = $this->w - $this->rMargin - $this->x;
$wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
$s = str_replace("\r", '', $txt);
$nb = strlen($s);
if($nb > 0 && $s[$nb - 1] == "\n")
$nb--;
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$nl = 1;
while($i < $nb) {
$c = $s[$i];
if($c == "\n") {
$i++;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
continue;
}
if($c == ' ')
$sep = $i;
$l += $cw[$c];
if($l > $wmax) {
if($sep == -1) {
if($i == $j)
$i++;
}
else
$i = $sep + 1;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
}
else
$i++;
}
return $nl;
}
}
// Create new PDF document
$pdf = new PDF('L'); // Set to Landscape orientation
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);
$pdf->SetMargins(10, 10, 10);
// Title
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 10, ucwords(str_replace('_', ' ', $filename)), 0, 1, 'C');
$pdf->Ln(5);
// Calculate column widths based on content
$column_widths = array();
$data = array();
mysqli_data_seek($result, 0);
while ($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
foreach ($headers as $header) {
$max_width = $pdf->GetStringWidth($header) + 4;
foreach ($data as $row) {
$width = $pdf->GetStringWidth($row[$header] ?? '') + 4;
$max_width = max($max_width, $width);
}
$column_widths[] = $max_width;
}
// Adjust widths if total exceeds page width
$total_width = array_sum($column_widths);
$page_width = $pdf->GetPageWidth() - 20; // 20mm margins
if ($total_width > $page_width) {
$ratio = $page_width / $total_width;
foreach ($column_widths as &$width) {
$width *= $ratio;
}
}
// Set column widths
$pdf->SetWidths($column_widths);
// Set alignments (center for headers, left for data)
$alignments = array_fill(0, count($headers), 'C');
$pdf->SetAligns($alignments);
// Add headers
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetFillColor(230, 230, 230);
$pdf->Row($headers);
// Add data rows
$pdf->SetFont('Arial', '', 10);
$pdf->SetFillColor(245, 245, 245);
$fill = false;
foreach ($data as $row) {
$row_data = array();
foreach ($headers as $header) {
$row_data[] = $row[$header] ?? '';
}
$pdf->Row($row_data);
$fill = !$fill;
}
// Output PDF
$pdf->Output('D', $filename . '.pdf');
}
?>