Bonjour, mon projet ->des hotels contiennent des chambres. Ces chambres sont réservées pour une période plus ou moins longue.
On ne peut réserver une chambre si quelqu'un a déjà réservé la chambre sur la même période.
Voici mon code ->
class/Booking.php
class/bookinManager.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 class Booking { protected $id; protected $room_number; protected $customer_name; protected $hotel_name; protected $booking_date; protected static $error; const MSG_ERROR_ID = 'ID doit être un entier.'; const MSG_ERROR_ROOM_NUMBER = 'NOM DE LA CHAMBRE doit être une chaîne de caractères.'; const MSG_ERROR_CUSTOMER_NAME = 'NOM DU CLIENT doit être une chaîne de caractères.'; const MSG_ERROR_HOTEL_NAME = 'NOM DE L\'HOTEL doit être une chaîne de caractères.'; const MSG_ERROR_BOOKING_DATE = 'DATE DE RESERVATION doit être format YYYY-MM-DD.'; const MSG_ERROR_OBJET = 'L\'objet ne peut pas être crée.'; public function __construct(array $data) { $this->setId($data['id']); $this->setRoomNumber($data['room_number']); $this->setCustomerName($data['customer_name']); $this->setHotelName($data['hotel_name']); $this->setBookingDate($data['booking_date']); if(!empty(self::$error)) { throw new Exception(self::$error . self::MSG_ERROR_OBJET); } } public function setError($msg) { self::$error = $msg; } public function getError() { return self::$error; } public function setId($id) { if((is_int($id)) AND ($id > 0)) { $this->id = $id; } else { $this->setError(self::MSG_ERROR_ID); } } public function setRoomNumber($room_number) { if(is_string($room_number)) { $this->room_number = $room_number; } else { $this->setError(self::MSG_ERROR_ROOM_NUMBER); } } public function setCustomerName($customer_name) { if(is_string($customer_name)) { $this->customer_name = $customer_name; } else { $this->setError(self::MSG_ERROR_CUSTOMER_NAME); } } public function setHotelName($hotel_name) { if(is_string($hotel_name)) { $this->hotel_name = $hotel_name; } else { $this->setError(self::MSG_ERROR_HOTEL_NAME); } } public function setBookingDate($booking_date) { list($y, $m, $d) = explode("-", $booking_date); if(checkdate($m, $d, $y)) { $this->booking_date = $booking_date; } else { $this->setError(self::MSG_ERROR_BOOKING_DATE); } } public function getId() { return $this->id; } public function getRoomNumber() { return $this->room_number; } public function getCustomerName() { return $this->customer_name; } public function getHotelName() { return $this->hotel_name; } public function getBookingDate() { return $this->booking_date; } } class Breve extends Booking { protected $arrival_date; protected $departure_date; /*const MSG_ERROR_ARRIVAL_DATE = 'DATE D\'ARRIVER doit être format YYYY-MM-DD.'; const MSG_ERROR_DEPARTURE_DATE = 'DATE DE DEPART doit être format YYYY-MM-DD.';*/ public function __construct(array $data) { parent::__construct($data); $this->setArrivalDate($data['arrival_date']); $this->setDepartureDate($data['departure_date']); if(!empty(parent::$error)) { throw new Exception(parent::$error . parent::MSG_ERROR_OBJET); } } public function setArrivalDate($arrival_date) { list($y, $m, $d) = explode("-", $arrival_date); if(checkdate($m, $d, $y)) { $this->arrival_date = $arrival_date; } else { $this->setError(parent::MSG_ERROR_ARRIVAL_DATE); } } public function setDepartureDate($departure_date) { list($y, $m, $d) = explode("-", $departure_date); if(checkdate($m, $d, $y)) { $this->departure_date = $departure_date; } else { $this->setError(parent::MSG_ERROR_DEPARTURE_DATE); } } public function getArrivalDate() { return $this->arrival_date; } public function getDepartureDate() { return $this->departure_date; } }
process/process_booking.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 class bookingManager { private $_db; public function __construct(PDO $db) { $this->setDb($db); } public function setDb($dbh) { $this->_db = $dbh; } public function addBooking(Booking $booking) { $sql = 'INSERT INTO booking (room_number, customer_name, hotel_name, booking_date, arrival_date, departure_date) VALUES(:room_number, :customer_name, :hotel_name, :booking_date, :arrival_date, :departure_date)'; $room_number = htmlspecialchars($booking->getRoomNumber()); $customer_name = htmlspecialchars($booking->getCustomerName()); $hotel_name = htmlspecialchars($booking->getHotelName()); $booking_date = $booking->getBookingDate(); $arrival_date = $booking->getArrivalDate(); $departure_date = $booking->getDepartureDate(); $stmnt = $this->_db->prepare($sql); $stmnt->bindParam(':room_number', $room_number); $stmnt->bindParam(':customer_name', $customer_name); $stmnt->bindParam(':hotel_name', $hotel_name); $stmnt->bindParam(':booking_date', $booking_date); $stmnt->bindParam(':arrival_date', $arrival_date); $stmnt->bindParam(':departure_date', $departure_date); if($stmnt->execute()) { return true; } else { return false; } } public function getRoom($room_number) { $sql = 'SELECT RO.name FROM room AS RO LEFT JOIN booking AS BO ON (RO.id = BO.room_number AND (arrival_date BETWEEN BO.arrival_date AND BO.departure_date OR departure_date BETWEEN BO.arrival_date AND BO.departure_date OR BO.arrival_date BETWEEN arrival_date AND departure_date OR BO.departure_date BETWEEN arrival_date AND departure_date)) WHERE BO.room_number IS NULL'; $stmnt = $this->_db->prepare($sql); $stmnt->execute(); $count = $stmnt->rowCount(); return $count; } }
La fonction addBooking ne tient pas compte des dates (j'ai la même réservation plus d'une fois.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 require('../includes/inc_connexion.php'); require('../class/Booking.php'); require('../class/bookingManager.php'); if(isset($_POST['submit_form'])) { if((isset($_POST['room_number_form'])) AND (empty($_POST['username_form'])) OR (!isset($_POST['hotelname_form'])) AND (!isset($_POST['bookingdate_form'])) AND (!isset($_POST['arrivaldate_form'])) AND (!isset($_POST['departuredate_form']))) { $message = '<p class="error">Tous les champs doivent être remplis !</p>'; } else { $room_number_form = trim($_POST['room_number_form']); $username_form = trim($_POST['username_form']); $hotelname_form = trim($_POST['hotelname_form']); $bookingdate_form = trim($_POST['bookingdate_form']); $arrivaldate_form = trim($_POST['arrivaldate_form']); $departuredate_form = trim($_POST['departuredate_form']); $booking_data = array( 'id' => 1, 'room_number' => $room_number_form, 'customer_name' => $username_form, 'hotel_name' => $hotelname_form, 'booking_date' => $bookingdate_form, 'arrival_date' => $arrivaldate_form, 'departure_date' => $departuredate_form ); $booking = new Breve($booking_data); $manager = new bookingManager($db); if(!$manager->getRoom($room_number_form)) { $message = '<p class="error">Pas de chambre entre cette date</p>'; } else { $booking = $manager->addBooking($booking); var_dump($booking); } } }
Merci d'avance pour votre aide.
Partager