-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbook_flight.php
More file actions
217 lines (181 loc) · 8.42 KB
/
book_flight.php
File metadata and controls
217 lines (181 loc) · 8.42 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
<?php
session_start();
header('Content-Type: application/json');
require_once 'db_config.php';
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
echo json_encode(['success' => false, 'message' => 'Please login to book flights.']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$bookingData = json_decode(file_get_contents('php://input'), true);
if (!$bookingData) {
echo json_encode(['success' => false, 'message' => 'Invalid booking data.']);
exit;
}
$flightId = $bookingData['flightId'] ?? '';
$passengers = $bookingData['passengers'] ?? [];
$isRoundTrip = isset($bookingData['returnFlightId']);
$returnFlightId = $bookingData['returnFlightId'] ?? '';
if (empty($flightId) || empty($passengers)) {
echo json_encode(['success' => false, 'message' => 'Missing required booking information.']);
exit;
}
try {
$conn->beginTransaction();
// Get flight information
$stmt = $conn->prepare("SELECT * FROM flights WHERE flight_id = ?");
$stmt->execute([$flightId]);
$flight = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$flight) {
throw new Exception("Flight not found.");
}
// Check available seats
$seatsNeeded = count($passengers);
if ($flight['available_seats'] < $seatsNeeded) {
throw new Exception("Not enough available seats.");
}
$basePrice = (float)$flight['price'];
$totalPrice = 0;
$bookingResults = [];
// Create one flight booking for all passengers
$flightBookingId = 'FB-' . time() . '-' . rand(1000, 9999);
// Process each passenger
foreach ($passengers as $passenger) {
$ssn = trim($passenger['ssn'] ?? '');
$firstName = trim($passenger['firstName'] ?? '');
$lastName = trim($passenger['lastName'] ?? '');
$dob = trim($passenger['dob'] ?? '');
$category = trim($passenger['category'] ?? 'adults');
if (empty($ssn) || empty($firstName) || empty($lastName) || empty($dob)) {
throw new Exception("Missing passenger information.");
}
// Calculate ticket price based on category
$ticketPrice = $basePrice;
if ($category === 'children') {
$ticketPrice = $basePrice * 0.7;
} elseif ($category === 'infants') {
$ticketPrice = $basePrice * 0.1;
}
$totalPrice += $ticketPrice;
// Insert or update passenger
$stmt = $conn->prepare("
INSERT INTO passenger (ssn, first_name, last_name, date_of_birth, category)
VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
first_name = VALUES(first_name),
last_name = VALUES(last_name),
date_of_birth = VALUES(date_of_birth),
category = VALUES(category)
");
$stmt->execute([$ssn, $firstName, $lastName, $dob, $category]);
// Create ticket
$ticketId = 'T-' . time() . '-' . rand(1000, 9999) . '-' . rand(100, 999);
$stmt = $conn->prepare("
INSERT INTO ticket (ticket_id, flight_booking_id, ssn, price)
VALUES (?, ?, ?, ?)
");
$stmt->execute([$ticketId, $flightBookingId, $ssn, $ticketPrice]);
$bookingResults[] = [
'flightBookingId' => $flightBookingId,
'ticketId' => $ticketId,
'ssn' => $ssn,
'firstName' => $firstName,
'lastName' => $lastName,
'dob' => $dob,
'category' => $category,
'price' => $ticketPrice
];
}
// Create flight booking with total price
$stmt = $conn->prepare("
INSERT INTO flight_booking (flight_booking_id, flight_id, total_price)
VALUES (?, ?, ?)
");
$stmt->execute([$flightBookingId, $flightId, $totalPrice]);
// Update available seats (one per passenger)
$stmt = $conn->prepare("
UPDATE flights SET available_seats = available_seats - ? WHERE flight_id = ?
");
$stmt->execute([count($passengers), $flightId]);
// Handle return flight if round trip
$returnBookingResults = [];
if ($isRoundTrip && !empty($returnFlightId)) {
$stmt = $conn->prepare("SELECT * FROM flights WHERE flight_id = ?");
$stmt->execute([$returnFlightId]);
$returnFlight = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$returnFlight) {
throw new Exception("Return flight not found.");
}
if ($returnFlight['available_seats'] < $seatsNeeded) {
throw new Exception("Not enough available seats for return flight.");
}
$returnBasePrice = (float)$returnFlight['price'];
$returnTotalPrice = 0;
// Create one return flight booking for all passengers
$returnFlightBookingId = 'FB-' . (time() + 1) . '-' . rand(1000, 9999);
foreach ($passengers as $passenger) {
$ssn = trim($passenger['ssn'] ?? '');
$category = trim($passenger['category'] ?? 'adults');
$ticketPrice = $returnBasePrice;
if ($category === 'children') {
$ticketPrice = $returnBasePrice * 0.7;
} elseif ($category === 'infants') {
$ticketPrice = $returnBasePrice * 0.1;
}
$returnTotalPrice += $ticketPrice;
// Create return ticket
$returnTicketId = 'T-' . (time() + 1) . '-' . rand(1000, 9999) . '-' . rand(100, 999);
$stmt = $conn->prepare("
INSERT INTO ticket (ticket_id, flight_booking_id, ssn, price)
VALUES (?, ?, ?, ?)
");
$stmt->execute([$returnTicketId, $returnFlightBookingId, $ssn, $ticketPrice]);
$returnBookingResults[] = [
'flightBookingId' => $returnFlightBookingId,
'ticketId' => $returnTicketId,
'ssn' => $ssn,
'firstName' => $passenger['firstName'],
'lastName' => $passenger['lastName'],
'dob' => $passenger['dob'],
'category' => $category,
'price' => $ticketPrice
];
}
// Create return flight booking with total price
$stmt = $conn->prepare("
INSERT INTO flight_booking (flight_booking_id, flight_id, total_price)
VALUES (?, ?, ?)
");
$stmt->execute([$returnFlightBookingId, $returnFlightId, $returnTotalPrice]);
// Update return flight seats (one per passenger)
$stmt = $conn->prepare("
UPDATE flights SET available_seats = available_seats - ? WHERE flight_id = ?
");
$stmt->execute([count($passengers), $returnFlightId]);
}
$conn->commit();
echo json_encode([
'success' => true,
'message' => 'Flight(s) booked successfully!',
'outbound' => [
'flight' => $flight,
'bookingId' => $bookingResults[0]['flightBookingId'] ?? '',
'totalPrice' => $totalPrice,
'tickets' => $bookingResults
],
'return' => $isRoundTrip && !empty($returnBookingResults) ? [
'flight' => $returnFlight ?? null,
'bookingId' => $returnBookingResults[0]['flightBookingId'] ?? '',
'totalPrice' => $returnTotalPrice ?? 0,
'tickets' => $returnBookingResults
] : null
]);
} catch (Exception $e) {
$conn->rollBack();
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
}
?>