-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooking.php
More file actions
133 lines (115 loc) · 3.58 KB
/
booking.php
File metadata and controls
133 lines (115 loc) · 3.58 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
<?php
declare(strict_types=1);
function isRoomAvailable($database, $roomType, $arrivalDate, $departureDate)
{
$stmt = $database->prepare("
SELECT COUNT(*) as count
FROM Bookings b
INNER JOIN Booking_Rooms br ON b.id = br.bookingId
INNER JOIN Rooms r ON br.roomId = r.id
WHERE r.name = :roomType
AND (
(b.arrival <= :departureDate AND b.departure >= :arrivalDate)
)
");
$stmt->execute([
':roomType' => $roomType,
':arrivalDate' => $arrivalDate,
':departureDate' => $departureDate
]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result['count'] == 0;
}
// Fetch all bookings for a specific room in January
function getRoomBookingsForJanuary($database, $roomName, $year)
{
$stmt = $database->prepare("
SELECT b.arrival, b.departure
FROM Bookings b
INNER JOIN Booking_Rooms br ON b.id = br.bookingId
INNER JOIN Rooms r ON br.roomId = r.id
WHERE r.name = :roomName
AND (
(b.arrival BETWEEN :start AND :end)
OR (b.departure BETWEEN :start AND :end)
OR (b.arrival <= :start AND b.departure >= :end)
)
");
$startOfJanuary = "$year-01-01";
$endOfJanuary = "$year-01-31";
$stmt->execute([
':roomName' => $roomName,
':start' => $startOfJanuary,
':end' => $endOfJanuary,
]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function generateRoomCalendar($bookings, $year)
{
$monthName = 'January';
$weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$bookedDates = [];
foreach ($bookings as $booking) {
$start = new DateTime($booking['arrival']);
$end = new DateTime($booking['departure']);
while ($start <= $end) {
$bookedDates[] = $start->format('Y-m-d');
$start->modify('+1 day');
}
}
$today = (new DateTime())->format('Y-m-d');
$date = new DateTime("$year-01-01");
$startDayOfWeek = $date->format('w');
$totalDays = 31;
$calendar = [];
// Add header
$calendar['header'] = ['month' => $monthName, 'weekdays' => $weekdays];
// Add empty cells before the 1st
for ($i = 0; $i < $startDayOfWeek; $i++) {
$calendar['days'][] = ['day' => '', 'isBooked' => false, 'isToday' => false];
}
// Add days of the month
for ($day = 1; $day <= $totalDays; $day++) {
$currentDate = $date->format('Y-m-d');
$calendar['days'][] = [
'day' => $day,
'isBooked' => in_array($currentDate, $bookedDates),
'isToday' => $currentDate === $today
];
$date->modify('+1 day');
}
return $calendar;
}
// Fetch all unique rooms from the database for January bookings
function getUniqueRoomsForBookings($database, $year)
{
// Fetch all distinct roomIds that have bookings in January
$stmt = $database->prepare("
SELECT DISTINCT r.name
FROM Rooms r
INNER JOIN Booking_Rooms br ON br.roomId = r.id
INNER JOIN Bookings b ON br.bookingId = b.id
WHERE b.arrival BETWEEN :start AND :end
OR b.departure BETWEEN :start AND :end
OR (b.arrival <= :start AND b.departure >= :end)
");
$startOfJanuary = "$year-01-01";
$endOfJanuary = "$year-01-31";
$stmt->execute([
':start' => $startOfJanuary,
':end' => $endOfJanuary,
]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
const ROOM_TYPES = ['economy', 'standard', 'luxury'];
function generateAllCalendars($database)
{
$year = 2025;
$allCalendars = [];
foreach (ROOM_TYPES as $roomName) {
$bookings = getRoomBookingsForJanuary($database, $roomName, $year);
$calendarData = generateRoomCalendar($bookings, $year);
$allCalendars[$roomName] = $calendarData;
}
return $allCalendars;
}