| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
jimport('joomla.application.component.view'); |
| 14 |
|
| 15 |
class VikbookingViewShowprc extends JViewVikBooking |
| 16 |
{ |
| 17 |
function display($tpl = null) |
| 18 |
{ |
| 19 |
$dbo = JFactory::getDbo(); |
| 20 |
$app = JFactory::getApplication(); |
| 21 |
$session = JFactory::getSession(); |
| 22 |
$vbo_tn = VikBooking::getTranslator(); |
| 23 |
|
| 24 |
$proomopt = VikRequest::getVar('roomopt', array()); |
| 25 |
$proomindex = VikRequest::getVar('roomindex', array()); |
| 26 |
$pdays = VikRequest::getInt('days', 0, 'request'); |
| 27 |
$pcheckin = VikRequest::getInt('checkin', '', 'request'); |
| 28 |
$pcheckout = VikRequest::getInt('checkout', '', 'request'); |
| 29 |
$padults = VikRequest::getVar('adults', array()); |
| 30 |
$pchildren = VikRequest::getVar('children', array()); |
| 31 |
$proomsnum = VikRequest::getInt('roomsnum', 0, 'request'); |
| 32 |
$ppkg_id = VikRequest::getInt('pkg_id', 0, 'request'); |
| 33 |
$split_stay = VikRequest::getVar('split_stay', array()); |
| 34 |
$pitemid = VikRequest::getInt('Itemid', 0, 'request'); |
| 35 |
|
| 36 |
$nowdf = VikBooking::getDateFormat(); |
| 37 |
if ($nowdf == "%d/%m/%Y") { |
| 38 |
$df = 'd/m/Y'; |
| 39 |
} elseif ($nowdf == "%m/%d/%Y") { |
| 40 |
$df = 'm/d/Y'; |
| 41 |
} else { |
| 42 |
$df = 'Y/m/d'; |
| 43 |
} |
| 44 |
|
| 45 |
$rooms = []; |
| 46 |
$arrpeople = []; |
| 47 |
for ($ir = 1; $ir <= $proomsnum; $ir++) { |
| 48 |
$ind = $ir - 1; |
| 49 |
if (!empty($proomopt[$ind])) { |
| 50 |
$q = "SELECT * FROM `#__vikbooking_rooms` WHERE `id`=" . (int)$proomopt[$ind] . " AND `avail`='1';"; |
| 51 |
$dbo->setQuery($q); |
| 52 |
$get_room = $dbo->loadAssoc(); |
| 53 |
if ($get_room) { |
| 54 |
$rooms[$ir] = $get_room; |
| 55 |
} |
| 56 |
} |
| 57 |
if (!empty($padults[$ind])) { |
| 58 |
$arrpeople[$ir]['adults'] = intval($padults[$ind]); |
| 59 |
} else { |
| 60 |
$arrpeople[$ir]['adults'] = 0; |
| 61 |
} |
| 62 |
if (!empty($pchildren[$ind])) { |
| 63 |
$arrpeople[$ir]['children'] = intval($pchildren[$ind]); |
| 64 |
} else { |
| 65 |
$arrpeople[$ir]['children'] = 0; |
| 66 |
} |
| 67 |
} |
| 68 |
if (empty($rooms) || count($rooms) != $proomsnum) { |
| 69 |
VikError::raiseWarning('', JText::translate('VBERRSELECTINGROOMS')); |
| 70 |
$app->redirect(JRoute::rewrite('index.php?option=com_vikbooking'.(!empty($pitemid) ? '&Itemid='.$pitemid : ''))); |
| 71 |
exit; |
| 72 |
} |
| 73 |
$vbo_tn->translateContents($rooms, '#__vikbooking_rooms'); |
| 74 |
|
| 75 |
// collect all room IDs involved |
| 76 |
$rooms_involved = []; |
| 77 |
foreach ($rooms as $room_booked) { |
| 78 |
if (!in_array($room_booked['id'], $rooms_involved)) { |
| 79 |
$rooms_involved[] = $room_booked['id']; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// calculate total nights of stay |
| 84 |
$secdiff = $pcheckout - $pcheckin; |
| 85 |
$daysdiff = $secdiff / 86400; |
| 86 |
if (is_int($daysdiff)) { |
| 87 |
if ($daysdiff < 1) { |
| 88 |
$daysdiff = 1; |
| 89 |
} |
| 90 |
} else { |
| 91 |
if ($daysdiff < 1) { |
| 92 |
$daysdiff = 1; |
| 93 |
} else { |
| 94 |
$sum = floor($daysdiff) * 86400; |
| 95 |
$newdiff = $secdiff - $sum; |
| 96 |
$maxhmore = VikBooking::getHoursMoreRb() * 3600; |
| 97 |
if ($maxhmore >= $newdiff) { |
| 98 |
$daysdiff = floor($daysdiff); |
| 99 |
} else { |
| 100 |
$daysdiff = ceil($daysdiff); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ($pdays != $daysdiff) { |
| 106 |
showSelectVb(JText::translate('VBERRCALCTAR')); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// get check-in and check-out dates information |
| 111 |
$checkin_info = getdate($pcheckin); |
| 112 |
$checkout_info = getdate($pcheckout); |
| 113 |
|
| 114 |
/** |
| 115 |
* Check split stay information. |
| 116 |
* |
| 117 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 118 |
*/ |
| 119 |
if (!empty($split_stay) && count($split_stay) == count($proomopt) && count($split_stay) == $proomsnum && $proomsnum > 1) { |
| 120 |
// valid split stay request vars received |
| 121 |
$split_stay_checkins = []; |
| 122 |
$split_stay_checkouts = []; |
| 123 |
foreach ($split_stay as $sps_k => $split_room) { |
| 124 |
// calculate and set the exact check-in and check-out timestamps for this split-room |
| 125 |
$new_room_checkin = VikBooking::getDateTimestamp($split_room['checkin'], $checkin_info['hours'], $checkin_info['minutes'], $checkin_info['seconds']); |
| 126 |
$new_room_checkout = VikBooking::getDateTimestamp($split_room['checkout'], $checkout_info['hours'], $checkout_info['minutes'], $checkout_info['seconds']); |
| 127 |
$split_stay_checkins[] = $new_room_checkin; |
| 128 |
$split_stay_checkouts[] = $new_room_checkout; |
| 129 |
// update split stay information |
| 130 |
$split_room['checkin_ts'] = $new_room_checkin; |
| 131 |
$split_room['checkout_ts'] = $new_room_checkout; |
| 132 |
$split_stay[$sps_k] = $split_room; |
| 133 |
} |
| 134 |
// validate minimum and maximum stay dates for the split stay |
| 135 |
if (empty($split_stay_checkins) || empty($split_stay_checkouts)) { |
| 136 |
// error, unset any value for the split stay |
| 137 |
$split_stay = []; |
| 138 |
} elseif (min($split_stay_checkins) != $pcheckin) { |
| 139 |
// error, unset any value for the split stay |
| 140 |
$split_stay = []; |
| 141 |
} elseif (max($split_stay_checkouts) != $pcheckout) { |
| 142 |
// error, unset any value for the split stay |
| 143 |
$split_stay = []; |
| 144 |
} |
| 145 |
} else { |
| 146 |
// unset any possible value as it's invalid |
| 147 |
$split_stay = []; |
| 148 |
} |
| 149 |
|
| 150 |
// modify booking |
| 151 |
$mod_booking = []; |
| 152 |
$skip_busy_ids = []; |
| 153 |
$only_non_ref = false; |
| 154 |
$cur_mod = $session->get('vboModBooking', ''); |
| 155 |
if (is_array($cur_mod) && count($cur_mod)) { |
| 156 |
$mod_booking = $cur_mod; |
| 157 |
$skip_busy_ids = VikBooking::loadBookingBusyIds($mod_booking['id']); |
| 158 |
/** |
| 159 |
* Booking modification should check if a non refundable rate plan was |
| 160 |
* previously selected so that moving to a refundable rate will be denied. |
| 161 |
* |
| 162 |
* @since 1.15.3 (J) - 1.5.5 (WP) |
| 163 |
*/ |
| 164 |
$mod_booking_rooms = VikBooking::loadOrdersRoomsData($mod_booking['id']); |
| 165 |
$mod_booking_rplans = []; |
| 166 |
$mod_booking_tars = []; |
| 167 |
foreach ($mod_booking_rooms as $mod_booking_room) { |
| 168 |
if (!empty($mod_booking_room['idtar']) && !in_array($mod_booking_room['idtar'], $mod_booking_tars)) { |
| 169 |
$mod_booking_tars[] = $mod_booking_room['idtar']; |
| 170 |
} |
| 171 |
} |
| 172 |
if (count($mod_booking_tars)) { |
| 173 |
$q = "SELECT `idprice` FROM `#__vikbooking_dispcost` WHERE `id` IN (" . implode(', ', $mod_booking_tars) . ") GROUP BY `idprice`;"; |
| 174 |
$dbo->setQuery($q); |
| 175 |
$mod_booking_rplans = $dbo->loadAssocList(); |
| 176 |
} |
| 177 |
$only_non_ref = VikBooking::findNonRefundableRates($mod_booking_rplans); |
| 178 |
} |
| 179 |
|
| 180 |
// check that room(s) are available |
| 181 |
$groupdays = VikBooking::getGroupDays($pcheckin, $pcheckout, $daysdiff); |
| 182 |
$morehst = VikBooking::getHoursRoomAvail() * 3600; |
| 183 |
$goonunits = true; |
| 184 |
$rooms_counts = []; |
| 185 |
$all_busy = VikBooking::loadBusyRecords($rooms_involved, time(), $pcheckout); |
| 186 |
foreach ($rooms as $num => $r) { |
| 187 |
// room busy records |
| 188 |
$busy = isset($all_busy[$r['id']]) ? $all_busy[$r['id']] : []; |
| 189 |
// determine the days to consider for the count of the availability |
| 190 |
$use_groupdays = $groupdays; |
| 191 |
if (!empty($split_stay) && !empty($split_stay[($num - 1)]) && $split_stay[($num - 1)]['idroom'] == $r['id']) { |
| 192 |
$split_room = $split_stay[($num - 1)]; |
| 193 |
$use_groupdays = VikBooking::getGroupDays($split_room['checkin_ts'], $split_room['checkout_ts'], (int)$split_room['nights']); |
| 194 |
} |
| 195 |
foreach ($use_groupdays as $gday) { |
| 196 |
$bfound = 0; |
| 197 |
foreach ($busy as $bu) { |
| 198 |
if (in_array($bu['id'], $skip_busy_ids)) { |
| 199 |
// booking modification |
| 200 |
continue; |
| 201 |
} |
| 202 |
if ($gday >= $bu['checkin'] && $gday <= ($morehst + $bu['checkout'])) { |
| 203 |
$bfound++; |
| 204 |
} |
| 205 |
} |
| 206 |
if ($bfound >= $r['units']) { |
| 207 |
$goonunits = false; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
if (!isset($rooms_counts[$r['id']])) { |
| 212 |
$rooms_counts[$r['id']] = [ |
| 213 |
'name' => $r['name'], |
| 214 |
'units' => $r['units'], |
| 215 |
'count' => 0, |
| 216 |
]; |
| 217 |
} |
| 218 |
// increase counter for this room ID in case multiple units of this same room are asked |
| 219 |
$rooms_counts[$r['id']]['count']++; |
| 220 |
} |
| 221 |
|
| 222 |
if (!$goonunits) { |
| 223 |
showSelectVb(JText::translate('VBROOMNOTRIT') . " " . date($df . ' H:i', $pcheckin) . " " . JText::translate('VBROOMNOTCONSTO') . " " . date($df . ' H:i', $pcheckout)); |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
// validate multiple units of the same room type ID |
| 228 |
foreach ($rooms_counts as $idr => $unitused) { |
| 229 |
if ($unitused['count'] > $unitused['units']) { |
| 230 |
VikError::raiseWarning('', JText::sprintf('VBERRROOMUNITSNOTAVAIL', $unitused['count'], $unitused['name'])); |
| 231 |
$app->redirect(JRoute::rewrite('index.php?option=com_vikbooking'.(!empty($pitemid) ? '&Itemid='.$pitemid : ''))); |
| 232 |
$goonunits = false; |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// collect closed rate plans information |
| 238 |
$all_rooms = []; |
| 239 |
foreach ($rooms as $num => $r) { |
| 240 |
if (!in_array($r['id'], $all_rooms)) { |
| 241 |
$all_rooms[] = $r['id']; |
| 242 |
} |
| 243 |
} |
| 244 |
$roomrpclosed = VikBooking::getRoomRplansClosedInDates($all_rooms, $pcheckin, $daysdiff); |
| 245 |
|
| 246 |
// prepare data |
| 247 |
$tars = []; |
| 248 |
$hoursdiff = VikBooking::countHoursToArrival($pcheckin); |
| 249 |
$aretherefares = true; |
| 250 |
|
| 251 |
// prepare tariffs available |
| 252 |
foreach ($rooms as $num => $r) { |
| 253 |
// determine the number of nights of stay and dates to consider |
| 254 |
$use_los = (int)$daysdiff; |
| 255 |
$room_checkin = $pcheckin; |
| 256 |
$room_checkout = $pcheckout; |
| 257 |
if (!empty($split_stay) && !empty($split_stay[($num - 1)]) && $split_stay[($num - 1)]['idroom'] == $r['id']) { |
| 258 |
$use_los = (int)$split_stay[($num - 1)]['nights']; |
| 259 |
$room_checkin = $split_stay[($num - 1)]['checkin_ts']; |
| 260 |
$room_checkout = $split_stay[($num - 1)]['checkout_ts']; |
| 261 |
} |
| 262 |
|
| 263 |
// fetch rates |
| 264 |
$q = "SELECT `d`.*,`p`.`minlos`,`p`.`minhadv` FROM `#__vikbooking_dispcost` AS `d` LEFT JOIN `#__vikbooking_prices` AS `p` ON `p`.`id`=`d`.`idprice` WHERE `d`.`days`=" . $use_los . " AND `d`.`idroom`=" . (int)$r['id'] . ($only_non_ref === true ? ' AND `p`.`free_cancellation`=0' : '') . " ORDER BY `d`.`cost` ASC;"; |
| 265 |
$dbo->setQuery($q); |
| 266 |
$tar = $dbo->loadAssocList(); |
| 267 |
if (!$tar) { |
| 268 |
$aretherefares = false; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
// closed rate plans on these dates |
| 273 |
if (count($roomrpclosed) && array_key_exists($r['id'], $roomrpclosed)) { |
| 274 |
foreach ($tar as $kk => $tt) { |
| 275 |
if (array_key_exists('idprice', $tt) && array_key_exists($tt['idprice'], $roomrpclosed[$r['id']])) { |
| 276 |
unset($tar[$kk]); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// rate plans with a minlos, or with a min hours in advance |
| 282 |
foreach ($tar as $kk => $tt) { |
| 283 |
if (!empty($tt['minlos']) && $tt['minlos'] > $use_los) { |
| 284 |
unset($tar[$kk]); |
| 285 |
} elseif ($hoursdiff < $tt['minhadv']) { |
| 286 |
unset($tar[$kk]); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (!count($tar)) { |
| 291 |
$aretherefares = false; |
| 292 |
break; |
| 293 |
} |
| 294 |
|
| 295 |
// reset values |
| 296 |
$tar = array_values($tar); |
| 297 |
|
| 298 |
// apply seasonal rates |
| 299 |
$tar = VikBooking::applySeasonsRoom($tar, $room_checkin, $room_checkout); |
| 300 |
|
| 301 |
// apply OBP rules |
| 302 |
$tar = VBORoomHelper::getInstance()->applyOBPRules($tar, $r, $arrpeople[$num]['adults']); |
| 303 |
|
| 304 |
$tars[$num] = $tar; |
| 305 |
} |
| 306 |
|
| 307 |
if ($aretherefares !== true) { |
| 308 |
showSelectVb(JText::translate('VBNOTARFNDSELO')); |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
$pkg = []; |
| 313 |
if (!empty($ppkg_id)) { |
| 314 |
$pkg = VikBooking::validateRoomPackage($ppkg_id, $rooms, $daysdiff, $pcheckin, $pcheckout); |
| 315 |
if (!is_array($pkg) || empty($pkg)) { |
| 316 |
if (is_string($pkg)) { |
| 317 |
VikError::raiseWarning('', $pkg); |
| 318 |
} |
| 319 |
$app->redirect(JRoute::rewrite("index.php?option=com_vikbooking&view=packagedetails&pkgid=".$ppkg_id.(!empty($pitemid) ? "&Itemid=".$pitemid : ""), false)); |
| 320 |
exit; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
$this->tars = $tars; |
| 325 |
$this->rooms = $rooms; |
| 326 |
$this->roomsnum = $proomsnum; |
| 327 |
$this->arrpeople = $arrpeople; |
| 328 |
$this->checkin = $pcheckin; |
| 329 |
$this->checkout = $pcheckout; |
| 330 |
$this->days = $daysdiff; |
| 331 |
$this->pkg = $pkg; |
| 332 |
$this->mod_booking = $mod_booking; |
| 333 |
$this->split_stay = $split_stay; |
| 334 |
$this->vbo_tn = $vbo_tn; |
| 335 |
//theme |
| 336 |
$theme = VikBooking::getTheme(); |
| 337 |
if ($theme != 'default') { |
| 338 |
$thdir = VBO_SITE_PATH.DS.'themes'.DS.$theme.DS.'showprc'; |
| 339 |
if (is_dir($thdir)) { |
| 340 |
$this->_setPath('template', $thdir.DS); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* We append to the booking process the rooms indexes booked through the interactive map, if any. |
| 346 |
* |
| 347 |
* @since 1.14 (J) - 1.4.0 (WP) |
| 348 |
*/ |
| 349 |
if (count($proomindex) == count($rooms)) { |
| 350 |
$only_empty_indexes = true; |
| 351 |
foreach ($proomindex as $rindex) { |
| 352 |
if ((int)$rindex > 0) { |
| 353 |
// a true room index was selected |
| 354 |
$only_empty_indexes = false; |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
if ($only_empty_indexes) { |
| 359 |
// we don't need to pass along the room indexes |
| 360 |
$proomindex = []; |
| 361 |
} |
| 362 |
} else { |
| 363 |
$proomindex = []; |
| 364 |
} |
| 365 |
$this->roomindex = $proomindex; |
| 366 |
|
| 367 |
// VBO 1.11 - push data to tracker |
| 368 |
$rooms_ids = []; |
| 369 |
foreach ($rooms as $ir => $r) { |
| 370 |
$rooms_ids[$ir] = $r['id']; |
| 371 |
} |
| 372 |
VikBooking::getTracker()->pushDates($pcheckin, $pcheckout, $daysdiff)->pushParty($arrpeople)->pushRooms($rooms_ids, array(), $proomindex)->closeTrack(); |
| 373 |
|
| 374 |
parent::display($tpl); |
| 375 |
} |
| 376 |
} |
| 377 |
|