PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / booking / subunit / record.php

record.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/booking/subunit/record.php

318 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2026 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Room-Booking sub-unit record implementation.
16 *
17 * @since 1.18.7 (J) - 1.8.7 (WP)
18 */
19 final class VBOBookingSubunitRecord
20 {
21 /**
22 * @var array
23 */
24 private array $data = [];
25
26 /**
27 * @var ?int
28 */
29 private ?int $index = null;
30
31 /**
32 * @var array
33 */
34 private array $initialData = [];
35
36 /**
37 * Binds the room booking record data properties and index.
38 *
39 * @param array $data The room booking record properties to bind.
40 * @param ?int $index Optional room booking index within the booking itself.
41 */
42 public function __construct(array $data, ?int $index = null)
43 {
44 // bind record data
45 $this->bindData($data);
46
47 // set room-booking index
48 $this->index = $index;
49 }
50
51 /**
52 * Returns the room booking record check-in timestamp.
53 *
54 * @return int
55 */
56 public function getCheckin()
57 {
58 return $this->data['checkin'];
59 }
60
61 /**
62 * Returns the room booking record check-out timestamp.
63 *
64 * @return int
65 */
66 public function getCheckout()
67 {
68 return ($this->data['realback'] ?? 0) ?: $this->data['checkout'];
69 }
70
71 /**
72 * Calculates the timestamp for the last night of stay.
73 *
74 * @return int
75 */
76 public function calculateLastNight()
77 {
78 return strtotime('23:59:59', strtotime('-1 day', $this->getCheckout()));
79 }
80
81 /**
82 * Returns the room booking record last night of stay timestamp.
83 *
84 * @return int
85 */
86 public function getLastNight()
87 {
88 return ($this->data['_last_night_ts'] ?? 0) ?: $this->calculateLastNight();
89 }
90
91 /**
92 * Returns the booking ID.
93 *
94 * @return int
95 */
96 public function getBookingID()
97 {
98 return $this->data['idorder'];
99 }
100
101 /**
102 * Returns the room booking record ID.
103 *
104 * @return int
105 */
106 public function getRoomBookingID()
107 {
108 return $this->data['room_booking_id'];
109 }
110
111 /**
112 * Returns the room booking unit index assigned, if any.
113 *
114 * @return ?int
115 */
116 public function getRoomUnitIndex()
117 {
118 return $this->data['roomindex'] ?? null;
119 }
120
121 /**
122 * Sets the room booking unit index assigned, if any.
123 *
124 * @param ?int $index Room booking unit index assigned, or null.
125 * @param bool $setInitial Whether to store the initial data value.
126 *
127 * @return ?int The previous room booking index assigned.
128 */
129 public function setRoomUnitIndex(?int $index, bool $setInitial = true)
130 {
131 if ($index === 0) {
132 // index 0 stands for no actual room unit index modification
133 $index = $this->getRoomUnitIndex();
134 }
135
136 return $this->setDataValue('roomindex', $index, $setInitial);
137 }
138
139 /**
140 * Returns the room booking initial unit index assigned, if any.
141 *
142 * @return ?int
143 */
144 public function getInitialRoomUnitIndex()
145 {
146 return $this->getDataValue('roomindex', null, $initial = true);
147 }
148
149 /**
150 * Tells if the room booking record is being relocated.
151 *
152 * @return bool
153 */
154 public function isRelocating()
155 {
156 return (bool) ($this->data['relocate'] ?? null);
157 }
158
159 /**
160 * Tells if the room booking record is a booking closure.
161 *
162 * @return bool
163 */
164 public function isClosure()
165 {
166 return !empty($this->data['closure']);
167 }
168
169 /**
170 * Gets the value for a given data key.
171 *
172 * @param string $key The data key to get.
173 * @param mixed $default The default value to return.
174 * @param bool $initial Whether to fetch the initial data value.
175 */
176 public function getDataValue(string $key, $default = null, bool $initial = false)
177 {
178 if ($initial) {
179 // attempt to return the initial data value
180 return $this->initialData[$key] ?? $default;
181 }
182
183 // attempt to return the current data value
184 return $this->data[$key] ?? $default;
185 }
186
187 /**
188 * Sets a given value for a data key.
189 *
190 * @param string $key The data key to update.
191 * @param mixed $value The data value to set.
192 * @param bool $setInitial Whether to store the initial data value.
193 *
194 * @return mixed The previous data value.
195 */
196 public function setDataValue(string $key, $value, bool $setInitial = true)
197 {
198 // access the current data value
199 $currentValue = $this->data[$key] ?? null;
200
201 // set the new data value
202 $this->data[$key] = $value;
203
204 if ($setInitial && !isset($this->initialData[$key])) {
205 // store the initial value
206 $this->initialData[$key] = $currentValue;
207 }
208
209 return $currentValue;
210 }
211
212 /**
213 * Resets a given data property key to its initial value.
214 *
215 * @param string $key The data property ket to reset.
216 * @param mixed $value Optional initial value to set.
217 *
218 * @return void
219 */
220 public function resetInitialValue(string $key, $value = null)
221 {
222 if ($value === null) {
223 // attempt to fetch the initial value
224 $value = $this->initialData[$key] ?? null;
225 }
226
227 $this->data[$key] = $value;
228 }
229
230 /**
231 * Resets all initial values that were previously updated.
232 *
233 * @return void
234 */
235 public function resetInitialValues()
236 {
237 foreach ($this->initialData as $key => $value) {
238 // reset the previously updated key
239 $this->resetInitialValue($key, $value);
240 }
241
242 // reset the initial data elements
243 $this->initialData = [];
244 }
245
246 /**
247 * Binds and normalizes the room booking record data properties.
248 *
249 * @param array $data The room booking record properties to bind.
250 *
251 * @return void
252 *
253 * @throws InvalidArgumentException
254 */
255 public function bindData(array $data)
256 {
257 // check data integrity
258 $this->checkIntegrity($data);
259
260 // assign data properties
261 $this->data = $data;
262
263 // calculate and set the last night of stay timestamp
264 $this->setDataValue('_last_night_ts', $this->calculateLastNight(), $setInitial = false);
265 }
266
267 /**
268 * Returns the current record data properties.
269 *
270 * @return array
271 */
272 public function getData()
273 {
274 return $this->data;
275 }
276
277 /**
278 * Returns the room booking index within the booking itself.
279 *
280 * @return ?int
281 */
282 public function getRecordIndex()
283 {
284 return $this->index;
285 }
286
287 /**
288 * Ensures the given room booking record data properties are valid.
289 *
290 * @param array $data The room booking record properties to check.
291 *
292 * @return void
293 *
294 * @throws InvalidArgumentException
295 */
296 private function checkIntegrity(array $data)
297 {
298 // determine the data validity
299 $valid = true;
300
301 if (empty($data['checkin'])) {
302 $valid = false;
303 }
304
305 if (empty($data['checkout']) && empty($data['realback'])) {
306 $valid = false;
307 }
308
309 if (empty($data['idorder']) && empty($data['room_booking_id'])) {
310 $valid = false;
311 }
312
313 if (!$valid) {
314 throw new InvalidArgumentException('Invalid room booking record data properties.', 400);
315 }
316 }
317 }
318