-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathticket.php
523 lines (464 loc) · 22.7 KB
/
ticket.php
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
require_once './nav.php';
require_once './conn.php';
?>
<?php
// Function to calculate the distance between two latitude and longitude points using Haversine formula
function calculateDistance($lat1, $lon1, $lat2, $lon2)
{
$R = 6371000; // Earth's radius in meters
$lat1Rad = deg2rad($lat1);
$lon1Rad = deg2rad($lon1);
$lat2Rad = deg2rad($lat2);
$lon2Rad = deg2rad($lon2);
$deltaLat = $lat2Rad - $lat1Rad;
$deltaLon = $lon2Rad - $lon1Rad;
$a = sin($deltaLat / 2) * sin($deltaLat / 2) + cos($lat1Rad) * cos($lat2Rad) * sin($deltaLon / 2) * sin($deltaLon / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$distance = $R * $c; // Distance in meters
return $distance;
}
////////////////// cost configuration ////////////////////
// Read the JSONC configuration file
$configFile = './cost.jsonc';
$jsonContent = file_get_contents($configFile);
if ($jsonContent === false) {
die("Failed to read JSONC file.");
}
// Remove both single-line and multi-line comments from JSONC
$jsonContent = preg_replace('/\s*(?:(?:\/\/[^\n]*)|(?:\/\*(?:(?!\*\/).)*\*\/))\s*/', '', $jsonContent); // ⚠️multi-line not removed!
// Decode the JSON content
$config = json_decode($jsonContent, true);
if ($config === null) {
die("Error decoding JSON: " . json_last_error_msg());
}
// cost config //
$cost_per_kilometer = $config['cost_per_kilometer'];
$discount = $config['discount'] / 100;
$round_trip_multiplayer = $config['round_trip_multiplayer'];
$round_trip_discount = $config['round_trip_discount'] / 100;
///////////////////////////////////////////////////////////////
// Set the time zone to Asia/Dhaka
date_default_timezone_set('Asia/Dhaka');
// Get the current timestamp with milliseconds
// Show this as ticket printing time
$timestamp = microtime(true);
// Split the timestamp into seconds and microseconds
list($seconds, $microseconds) = explode('.', $timestamp);
// Format the date and time
$printTime = date("Y-m-d H:i:s", $seconds) . '.' . substr($microseconds, 0, 3); // Print the current date with milliseconds
// handel ticket submission
if (isset($_POST['submit'])) {
$from = $_POST['from'];
$to = $_POST['to'];
// Convert $from and $to to integers
$from = intval($from);
$to = intval($to);
$date = new DateTime($_POST['date']);
$today = new DateTime();
$class = $_POST['class'];
$passengers = $_POST['passengers'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$trip = $_POST['trip'];
// Validate date
if ($date < $today) {
// Date is in the past
echo "<script>alert('Please select a valid date');</script>";
} else {
//If Date is valid
$sql = "SELECT latitude, longitude FROM locations WHERE id IN (?, ?)";
$params = array($from, $to);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
$locationsData = array();
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$locationsData[] = $row;
}
if (count($locationsData) == 2) {
// Extract latitude and longitude for "from" and "to" locations
$fromLat = $locationsData[0]['latitude'];
$fromLon = $locationsData[0]['longitude'];
$toLat = $locationsData[1]['latitude'];
$toLon = $locationsData[1]['longitude'];
// Calculate the distance using the Haversine formula
$distance = calculateDistance($fromLat, $fromLon, $toLat, $toLon);
// Calculate the cost based on the distance
$cost = $distance * $cost_per_kilometer;
// Multiply cost by number of passengers
$cost *= $passengers;
$cost *= (1 - $discount);
$cost = round($cost); //convert cost to top
// Check if round trip
if ($trip == 'round-trip') {
// Get return date
$returnDate = new DateTime($_POST['return-date']);
// Validate return date
if ($returnDate < $date) {
// Return date is before departure date
echo "<script>alert('Please select a valid return date');</script>";
} else {
// Return date is valid
$cost *= $round_trip_multiplayer;
$cost *= (1 - $round_trip_discount);
$cost = round($cost); //convert cost to top
// Query locations from database to get airport names
$sql = "SELECT id, destination FROM locations";
$stmt = sqlsrv_query($conn, $sql);
$locations = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$locations[$row['id']] = $row['destination'];
}
// Convert airport IDs to names
$fromName = $locations[$from];
$toName = $locations[$to];
// Generate a random 8-digit ID
$id = mt_rand(10000000, 99999999);
// Insert into database with the generated ID
$sql = "INSERT INTO bookings (id, [from], [to], date, class, passengers, email, phone, trip, return_date, cost, printTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$params = array($id, $fromName, $toName, $date->format('Y-m-d'), $class, $passengers, $email, $phone, $trip, $returnDate->format('Y-m-d'), $cost, $printTime);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
} else {
// Generate PDF
require('../vendor/autoload.php');
require('./fpdf/rotate.php');
$pdf = new PDF('p', 'mm', 'A4');
$pdf->AddPage();
// ticket background
$pdf->Image('./image/ticket.png', -10, -5, 230);
$pdf->SetFont('Arial', 'BU', 24);
$pdf->Cell(71, 10, '', 0, 0);
$pdf->Cell(59, 5, 'Private Jet!', 0, 0);
$pdf->Cell(59, 10, '', 0, 0);
$pdf->SetFont('Arial', 'B', 12);
// Display ID
if (isset($id)) {
$pdf->Ln();
$pdf->Cell(10, 10, 'FID: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(140, 10, ' ' . $id);
}
$pdf->Ln();
// from
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 10, 'From:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(1, 10, ' ' . htmlspecialchars($fromName));
$pdf->Ln();
// to
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 10, 'To:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(1, 10, ' ' . htmlspecialchars($toName));
$pdf->Ln();
// $pdf->Ln();
$pdf->Cell(95, 10, 'Travel Date: ' . htmlspecialchars($date->format('Y-m-d')), 1, 0, 'C');
// Display return date
if ($trip == 'round-trip') {
$pdf->Cell(95, 10, 'Return Date: ' . htmlspecialchars($returnDate->format('Y-m-d')), 1, 0, 'C');
}
// Display class
if (isset($class)) {
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(15, 10, 'Class:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($class));
}
// Display passengers
if (isset($passengers)) {
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(25, 10, 'Passengers: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($passengers));
}
// Display phone
if (isset($phone)) {
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(15, 10, 'Phone: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($phone));
}
// Display cost
if (isset($cost)) {
$pdf->SetTextColor(255, 0, 0);
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(175, 10, 'Cost: ', 0, 0, 'R');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(18, 10, ' $' . htmlspecialchars($cost), 0, 1, 'R');
}
// validate
$pdf->Rotate(12); // Rotate by 90 degrees
$pdf->SetTextColor(140, 140, 140);
$pdf->SetFont('Arial', 'I', 9);
$pdf->Cell(160, 30, ' ' . htmlspecialchars($printTime), 0, 0, 'R');
// RESET ELEMENT IF NEEDED
// /* ///////////////////////////////////////////////////
// $pdf->SetFont('Arial', 'B', 12); // Reset font ///////
// $pdf->Rotate(0); // Reset rotation to 0 degrees //////
// $pdf->SetTextColor(0, 0, 0); // Reset color //////////
// $pdf->Cell(0, 0, ''); // Reset position //////////////
// */ ///////////////////////////////////////////////////
// Output PDF
if (isset($pdf)) {
ob_end_clean();
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='ticket.pdf'");
echo base64_encode($pdf->Output());
exit;
}
// Display success message
echo "<script>alert('Booking successful!');</script>";
}
}
} else {
// One-way trip
// Query locations from database to get airport names
$sql = "SELECT id, destination FROM locations";
$stmt = sqlsrv_query($conn, $sql);
$locations = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$locations[$row['id']] = $row['destination'];
}
// Convert airport IDs to names
$fromName = $locations[$from];
$toName = $locations[$to];
// Generate a random 8-digit ID
$id = mt_rand(10000000, 99999999);
// Insert into database with the generated ID
$sql = "INSERT INTO bookings (id, [from], [to], date, class, passengers, email, phone, trip, cost, printTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$params = array($id, $fromName, $toName, $date->format('Y-m-d'), $class, $passengers, $email, $phone, $trip, $cost, $printTime);
$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
} else {
// Generate PDF
require('../vendor/autoload.php');
require('./fpdf/rotate.php');
$pdf = new PDF('p', 'mm', 'A4');
$pdf->AddPage();
// ticket background
$pdf->Image('./image/ticket.png', -10, -5, 230);
$pdf->SetFont('Arial', 'BU', 24);
$pdf->Cell(71, 10, '', 0, 0);
$pdf->Cell(59, 5, 'Private Jet!', 0, 0);
$pdf->Cell(59, 10, '', 0, 0);
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
// Display ID
if (isset($id)) {
$pdf->Ln();
$pdf->Cell(10, 10, 'FID: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(140, 10, ' ' . $id);
$pdf->Cell(40, 10, 'Travel Date: ' . htmlspecialchars($date->format('Y-m-d')));
}
$pdf->Ln();
// from
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 10, 'From:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(1, 10, ' ' . htmlspecialchars($fromName));
$pdf->Ln();
// to
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(10, 10, 'To:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(1, 10, ' ' . htmlspecialchars($toName));
$pdf->Ln();
// Display class
if (isset($class)) {
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(15, 10, 'Class:');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($class));
}
// Display passengers
if (isset($passengers)) {
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(25, 10, 'Passengers: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($passengers));
}
// Display phone
if (isset($phone)) {
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(15, 10, 'Phone: ');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(40, 10, ' ' . htmlspecialchars($phone));
}
// Display cost
if (isset($cost)) {
$pdf->SetTextColor(255, 0, 0);
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(175, 10, 'Cost: ', 0, 0, 'R');
$pdf->SetFont('Arial', 'I', 12);
$pdf->Cell(18, 10, ' $' . htmlspecialchars($cost), 0, 1, 'R');
}
// validate
$pdf->Rotate(12); // Rotate by 90 degrees
$pdf->SetTextColor(140, 140, 140);
$pdf->SetFont('Arial', 'I', 9);
$pdf->Cell(160, 30, ' ' . htmlspecialchars($printTime), 0, 0, 'R');
// RESET ELEMENT IF NEEDED
// /* ///////////////////////////////////////////////////
// $pdf->SetFont('Arial', 'B', 12); // Reset font ///////
// $pdf->Rotate(0); // Reset rotation to 0 degrees //////
// $pdf->SetTextColor(0, 0, 0); // Reset color //////////
// $pdf->Cell(0, 0, ''); // Reset position //////////////
// */ ///////////////////////////////////////////////////
// Output PDF
if (isset($pdf)) {
ob_end_clean();
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='ticket.pdf'");
echo base64_encode($pdf->Output());
exit;
}
// Display success message😂
echo "<script>alert('Booking successful!');</script>";
}
}
}
}
}
// Query locations from database to populate the dropdowns
$sql = "SELECT id, destination FROM locations Order by destination";
$stmt = sqlsrv_query($conn, $sql);
$locations = [];
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$locations[$row['id']] = $row['destination'];
}
// Get the first two keys (IDs) from the locations array
$firstTwoKeys = array_keys($locations);
$from = $firstTwoKeys[0]; // Set the default value to the ID of the 1st element
$to = $firstTwoKeys[1]; // Set the default value to the ID of the 2nd element
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$from = isset($_POST['from']) ? $_POST['from'] : $from;
$to = isset($_POST['to']) ? $_POST['to'] : $to;
if ($from === $to) {
// show error message
echo "<script>alert('From and To cannot be the same.');</script>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./style/ticket.css">
<title>ticket</title>
</head>
<body>
<header>
<div>
<h1>YOUR PRIVATE AIRLINE!</h1>
<p>Your trusted source for premium airline tickets at an affordable price! Our goal is to ensure you get the most comfortable flying experience all around the globe all while at the convenience of your own home devices. We also provide the service of rescheduling your flight to ensure your experience is the most optimal that we can provide.</p>
</div>
</header>
<form method="post">
<div class="border">
<div class="destination">
<div class="box">
<label for="from">From:</label>
<select id="from" name="from">
<?php foreach ($locations as $id => $destination) : ?>
<option value="<?php echo htmlspecialchars($id); ?>" <?php if ($from == $id) echo 'selected'; ?>><?php echo htmlspecialchars($destination); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="box">
<label for="to">To:</label>
<select id="to" name="to">
<?php foreach ($locations as $id => $destination) : ?>
<option value="<?php echo htmlspecialchars($id); ?>" <?php if ($to == $id) echo 'selected'; ?>><?php echo htmlspecialchars($destination); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="date">
<div class="box">
<label for="trip">Trip:</label>
<select id="trip" name="trip">
<option value="one-way">One-way</option>
<option value="round-trip">Round-trip</option>
</select>
</div>
<div class="travel_date box">
<label for="date">Date:</label>
<input type="date" id="date" name="date" required>
</div>
<!-- Add return date input -->
<div class="box" id="return-date-input" style="display: none;">
<label for="return-date">Return Date:</label><br>
<input type="date" id="return-date" name="return-date">
</div>
</div>
</div>
<?php
$jsonFilePath = './classes.json';
// Check if the JSON file exists
if (file_exists($jsonFilePath)) {
// Read the JSON file contents
$jsonData = file_get_contents($jsonFilePath);
// Decode the JSON data into a PHP array
$classesData = json_decode($jsonData, true);
// Check if decoding was successful
if (is_array($classesData) && isset($classesData['classes'])) {
$classes = $classesData['classes'];
} else {
$classes = array("server error"); // Default to an empty array if JSON decoding failed
}
} else {
$classes = array("server error"); // Default to an empty array if the JSON file doesn't exist
}
?>
<div class="more_info">
<div class="quality box_2">
<label for="class">Class:</label>
<select id="class" name="class">
<?php foreach ($classes as $class) : ?>
<option value="<?php echo htmlspecialchars(strtolower($class)); ?>"><?php echo htmlspecialchars($class); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="box_2">
<label for="passengers">Passengers:</label>
<!-- set 1 to 12 passengers -->
<input type="number" id="passengers" name="passengers" required min="1" max="12" placeholder="Maximum 12 passenger per flight">
</div>
<!-- Add email and phone inputs -->
<div class="box_2">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required placeholder="[email protected]">
</div>
<div class="box_2">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" required placeholder="Mobile no.">
</div>
</div>
</div>
<div id="submit">
<input class="submit" type="submit" name="submit" value="Submit">
</div>
</form>
<script>
// Show/hide return date input based on trip selection
document.getElementById('trip').addEventListener('change', function() {
if (this.value === 'round-trip') {
document.getElementById('return-date-input').style.display = 'block';
} else {
document.getElementById('return-date-input').style.display = 'none';
}
});
</script>
</body>
</html>