-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserve.php
261 lines (226 loc) · 9 KB
/
reserve.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
<?php
# Start output buffering
ob_start();
# database
include ("header.php");
$reservationSuccess = false;
$reservationError = "";
$selectedHome = null;
$user_id = $_SESSION['user_id'];
if (isset($_GET['hotel_id'])) {
$homeId = intval($_GET['hotel_id']);
$query = "SELECT * FROM hotel WHERE hotel_id = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $homeId);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows > 0) {
$selectedHome = $result->fetch_assoc();
} else {
$reservationError = "Selected hotel not found.";
}
$stmt->close();
}
$checkIn = isset($_GET['checkIn']) ? $_GET['checkIn'] : "";
$checkOut = isset($_GET['checkOut']) ? $_GET['checkOut'] : "";
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['hotel_id'], $_POST['checkIn'], $_POST['checkOut'], $_POST['tp'])) {
$homeId = intval($_POST['hotel_id']);
$checkIn = $_POST['checkIn'];
$checkOut = $_POST['checkOut'];
$tp = $_POST['tp'];
$curr_date = date("Y-m-d");
if (strtotime($checkIn) < strtotime($curr_date) || strtotime($checkOut) < strtotime($curr_date)) {
$reservationError = "Check-in date or check-out date cannot be earlier than today's date.";
} elseif (strtotime($checkIn) > strtotime($checkOut)) {
$reservationError = "Check-in date cannot be greater than the check-out date.";
} else {
$query = "SELECT price FROM hotel WHERE hotel_id = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("i", $homeId);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$bookingDuration = (strtotime($checkOut) - strtotime($checkIn)) / (60 * 60 * 24);
$basePrice = $row['price'];
if ($bedType === 'double') {
$totalPrice = $bookingDuration * ($basePrice * 1.3);
} else {
$totalPrice = $bookingDuration * $basePrice;
}
$insertQuery = "INSERT INTO reservations (user_id, hotel_id, check_in_date, check_out_date, total_price)
VALUES (?, ?, ?, ?, ?)";
$stmt = $con->prepare($insertQuery);
$stmt->bind_param("iissd", $user_id, $homeId, $checkIn, $checkOut, $tp);
$insertResult = $stmt->execute();
if ($insertResult) {
$reservationId = $stmt->insert_id;
$reservationSuccess = true;
$updateAvailabilityQuery = "UPDATE hotel SET availability_status = 'not_available' WHERE hotel_id = ?";
$stmt = $con->prepare($updateAvailabilityQuery);
$stmt->bind_param("i", $homeId);
$updateAvailabilityResult = $stmt->execute();
if (!$updateAvailabilityResult) {
$reservationError = "Error updating availability status: " . $stmt->error;
} else {
ob_end_clean();
header("Location: paymenthotel.php?reservation_id=" . $reservationId);
exit();
}
} else {
$reservationError = "Error creating reservation: " . $stmt->error;
}
$stmt->close();
} else {
$reservationError = "Error fetching hotel price: " . $stmt->error;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="../assets/css/searches.css">
<link rel="stylesheet" href="../assets/css/responsive.css">
<style>
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f9f9f9;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.holiday-home {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}
.home-image {
width: 100%;
max-width: 500px;
border-radius: 10px;
}
.holiday-details {
margin-top: 20px;
text-align: left;
}
.holiday-details h2 {
margin-bottom: 10px;
}
.label {
font-weight: bold;
}
form {
display: flex;
flex-direction: column;
gap: 10px;
}
label {
font-weight: bold;
}
input[type="date"] {
padding: 8px;
border-radius: 5px;
border: 1px solid #ccc;
}
.btn {
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: white;
cursor: pointer;
}
.btn:hover {
background-color: #0056b3;
}
.error {
color: red;
font-weight: bold;
}
.success {
color: green;
font-weight: bold.
}
.btn-container {
display: flex;
justify-content: space-between;
}
.btn-left {
order: 1;
background-color: red;
}
.btn-right {
order: 2;
}
</style>
</head>
<body>
<div class="container">
<?php
if ($selectedHome) {
echo "<div class='holiday-home'>";
echo "<img src='{$selectedHome['image_path']}' alt='{$selectedHome['name']}' class='home-image'>";
echo "<div class='holiday-details'>";
echo "<h2>Hotel Name: {$selectedHome['name']}</h2>";
echo "<p class='label'>Location: {$selectedHome['location']}</p>";
echo "<p class='label'>Description:</p>";
echo "<p>{$selectedHome['description']}</p>";
echo "<p class='label'>Ratings: {$selectedHome['rating']}</p>";
echo "<p class='label'>Start from: <span class='text-primary'>{$selectedHome['price']} TK</span></p>";
echo "<input type='hidden' id='price' value='{$selectedHome['price']}'>";
echo "</div>";
echo "</div>";
echo "<form method='post' action=''>";
echo "<input type='hidden' name='hotel_id' value='{$selectedHome['hotel_id']}'>";
echo "<label for='checkIn'>Check-in Date: <input type='date' name='checkIn' required value='$checkIn'> Check-out Date: <input type='date' name='checkOut' required value='$checkOut'></label>";
echo "<label for='checkOut'></label>";
echo "<label><input type='radio' name='bed_type' value='single' checked onclick='calculateTotalPrice()'> Single Bed</label>";
echo "<label><input type='radio' name='bed_type' value='double' onclick='calculateTotalPrice()'> Double Bed (+30%)</label>";
echo "<label for='tp'> Total Price: <input type='text' name='tp' id='tp' value='' readonly></label>";
if ($reservationError) {
echo "<p class='error'>$reservationError</p>";
} elseif ($reservationSuccess) {
echo "<p class='success'>Successfully Reserved!</p>";
}
echo "<div class='btn-container'>";
echo "<button type='reset' class='btn btn-left'>Reset</button>";
echo "<button type='submit' class='btn btn-right'>Reserve Now</button>";
echo "</div>";
echo "</form>";
} elseif ($reservationError) {
echo "<p class='error'>$reservationError</p>";
} else {
echo "<p>No hotel selected.</p>";
}
?>
</div>
<!-- Footer -->
<?php include ("footer.php"); ?>
</body>
<script>
function calculateTotalPrice() {
const pricePerNight = document.getElementById('price').value;
const checkInDate = document.getElementsByName('checkIn')[0].value;
const checkOutDate = document.getElementsByName('checkOut')[0].value;
if (checkInDate && checkOutDate) {
const checkIn = new Date(checkInDate);
const checkOut = new Date(checkOutDate);
const bookingDuration = (checkOut - checkIn) / (1000 * 60 * 60 * 24);
const bedType = document.querySelector('input[name="bed_type"]:checked').value;
let totalPrice;
if (bedType === 'double') {
totalPrice = bookingDuration * pricePerNight * 1.3;
} else {
totalPrice = bookingDuration * pricePerNight;
}
document.getElementById('tp').value = totalPrice.toFixed(2);
}
}
</script>
</html>