-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
executable file
·166 lines (151 loc) · 6.9 KB
/
Copy pathcheckout.php
File metadata and controls
executable file
·166 lines (151 loc) · 6.9 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
<?php
// checkout.php — inventory-aware pre-confirmation page
require_once __DIR__.'/config.php';
require_once __DIR__.'/db.php';
require_once __DIR__.'/auth.php';
require_once __DIR__.'/header.php';
// ---- read & validate inputs ----
$room_id = (int)($_GET['room_id'] ?? 0);
$ci = trim($_GET['ci'] ?? '');
$co = trim($_GET['co'] ?? '');
$guests = max(1, (int)($_GET['guests'] ?? 1));
$services_raw = $_GET['services'] ?? [];
$services = [];
$services_total = 0;
function valid_ymd($s){
$dt = DateTime::createFromFormat('Y-m-d', $s);
return $dt && $dt->format('Y-m-d') === $s;
}
if ($room_id <= 0 || !valid_ymd($ci) || !valid_ymd($co) || $ci >= $co) {
echo "<section class='container'><div class='card' style='padding:22px;max-width:700px;margin:0 auto'>
<h2>Invalid selection</h2>
<p>Please select valid check-in and check-out dates.</p>
<a class='btn' href='rooms_list.php'>Back to rooms</a>
</div></section>";
require 'footer.php'; exit;
}
// ---- load room (this represents a room *type* with inventory) ----
$st = $pdo->prepare("SELECT * FROM rooms WHERE id=? AND is_active=1 LIMIT 1");
$st->execute([$room_id]);
$room = $st->fetch(PDO::FETCH_ASSOC);
if (!$room) {
echo "<section class='container'><div class='card' style='padding:22px;max-width:700px;margin:0 auto'>
<h2>Room not found</h2>
<a class='btn' href='rooms_list.php'>Back to rooms</a>
</div></section>";
require 'footer.php'; exit;
}
// guest cap
if ($guests > (int)$room['max_guests']) {
echo "<section class='container'><div class='card' style='padding:22px;max-width:700px;margin:0 auto'>
<h2>Too many guests</h2>
<p>This room allows up to ".(int)$room['max_guests']." guest(s).</p>
<a class='btn' href='rooms_list.php'>Back to rooms</a>
</div></section>";
require 'footer.php'; exit;
}
// ---- inventory-aware availability (sold out only if all units taken) ----
$inventory = isset($room['inventory']) ? max(1, (int)$room['inventory']) : 1;
$overlap = $pdo->prepare("
SELECT COUNT(*) FROM bookings
WHERE room_id = ?
AND status IN ('pending','confirmed')
AND NOT (check_out <= ? OR check_in >= ?)
");
$overlap->execute([$room_id, $ci, $co]);
$used = (int)$overlap->fetchColumn();
if ($used >= $inventory) {
echo "<section class='container'><div class='card' style='padding:22px;max-width:760px;margin:0 auto'>
<h2>Invalid selection</h2>
<p>Selected dates are no longer available for this room.</p>
<a class='btn' href='rooms_list.php?ci=".urlencode($ci)."&co=".urlencode($co)."&guests=".(int)$guests."'>Back to rooms</a>
</div></section>";
require 'footer.php'; exit;
}
// Services calculation and display
if (!empty($services_raw)) {
$service_ids = array_map('intval', $services_raw);
$placeholders = rtrim(str_repeat('?,', count($service_ids)), ',');
$stmt = $pdo->prepare("SELECT * FROM room_services WHERE id IN ($placeholders)");
$stmt->execute($service_ids);
$services = $stmt->fetchAll(PDO::FETCH_ASSOC);
$services_total = array_sum(array_column($services, 'price'));
}
// Create checkout display line
$services_summary = "";
if (!empty($services)) {
$names = array_column($services, 'name');
$services_summary = implode(', ', $names) . " (Total: U$" . number_format($services_total, 2) . ")";
}
// ---- price estimate ----
$nights = (new DateTime($ci))->diff(new DateTime($co))->days;
$nights = max(1, $nights);
$total_cents = $nights * (int)$room['rate_cents'];
?>
<section class="container">
<div class="card" style="max-width:900px;margin:0 auto;padding:18px 18px 24px">
<div class="grid">
<div class="span-4">
<div class="hero-img-wrap" style="border-radius:12px;overflow:hidden">
<img src="<?= htmlspecialchars($room['image_url'] ?: 'https://via.placeholder.com/800x600?text=Room') ?>"
alt="Room image" style="width:100%;height:180px;object-fit:cover">
</div>
<div class="muted tiny" style="margin-top:6px">
You won’t be charged now. You’ll pay at the property.
</div>
</div>
<div class="span-8">
<h2 class="h2" style="margin:0 0 10px">Review your stay</h2>
<table class="table" style="width:100%;border-collapse:collapse">
<tr>
<td style="width:40%;border:1px solid var(--line);padding:10px">Room Type</td>
<td style="border:1px solid var(--line);padding:10px"><?= htmlspecialchars($room['type']) ?></td>
</tr>
<tr>
<td style="border:1px solid var(--line);padding:10px">Guests</td>
<td style="border:1px solid var(--line);padding:10px"><?= (int)$guests ?> (max <?= (int)$room['max_guests'] ?>)</td>
</tr>
<tr>
<td style="border:1px solid var(--line);padding:10px">Check-in</td>
<td style="border:1px solid var(--line);padding:10px"><?= htmlspecialchars($ci) ?></td>
</tr>
<tr>
<td style="border:1px solid var(--line);padding:10px">Check-out</td>
<td style="border:1px solid var(--line);padding:10px"><?= htmlspecialchars($co) ?></td>
</tr>
<tr>
<td style="border:1px solid var(--line);padding:10px">Estimate</td>
<td style="border:1px solid var(--line);padding:10px">
$<?= number_format($total_cents/100, 2) ?> for <?= $nights ?> night<?= $nights>1?'s':'' ?>
</td>
</tr>
<?php if ($services_summary): ?>
<tr>
<td style="border:1px solid var(--line);padding:10px">Services</td>
<td style="border:1px solid var(--line);padding:10px">
<?= htmlspecialchars($services_summary) ?>
</td>
</tr>
<?php endif; ?>
</table>
<form method="post" action="bookings_new.php" style="margin-top:14px;display:flex;gap:10px">
<?php csrf_input(); ?>
<input type="hidden" name="confirm" value="1">
<input type="hidden" name="room_id" value="<?= (int)$room_id ?>">
<input type="hidden" name="ci" value="<?= htmlspecialchars($ci) ?>">
<input type="hidden" name="co" value="<?= htmlspecialchars($co) ?>">
<input type="hidden" name="guests" value="<?= (int)$guests ?>">
<?php foreach ($services as $s): ?>
<input type="hidden" name="services[]" value="<?= (int)$s['id'] ?>">
<?php endforeach; ?>
<button class="btn primary" type="submit">Confirm reservation</button>
<a class="btn" href="rooms_list.php?ci=<?= urlencode($ci) ?>&co=<?= urlencode($co) ?>&guests=<?= (int)$guests ?>">Cancel</a>
</form>
<div class="muted tiny" style="margin-top:8px">
The specific room number will be assigned automatically after you confirm, based on availability.
</div>
</div>
</div>
</div>
</section>
<?php require_once __DIR__.'/footer.php'; ?>