| 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 |
// import Joomla view library |
| 14 |
jimport('joomla.application.component.view'); |
| 15 |
|
| 16 |
class VikBookingViewRatesoverv extends JViewVikBooking |
| 17 |
{ |
| 18 |
public function display($tpl = null) |
| 19 |
{ |
| 20 |
// Set the toolbar |
| 21 |
$this->addToolBar(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Check the next festivities periodically |
| 25 |
* |
| 26 |
* @since 1.12.0 (J) - 1.2.0 (WP) |
| 27 |
*/ |
| 28 |
$fests = VikBooking::getFestivitiesInstance(); |
| 29 |
if ($fests->shouldCheckFestivities()) { |
| 30 |
$fests->storeNextFestivities(); |
| 31 |
} |
| 32 |
$festivities = $fests->loadFestDates(); |
| 33 |
|
| 34 |
$dbo = JFactory::getDbo(); |
| 35 |
$mainframe = JFactory::getApplication(); |
| 36 |
$session = JFactory::getSession(); |
| 37 |
|
| 38 |
$cid = VikRequest::getVar('cid', array(0)); |
| 39 |
$sesscids = $session->get('vbRatesOviewCids', array()); |
| 40 |
if (empty($cid[0]) && is_array($sesscids) && count($sesscids)) { |
| 41 |
// load rooms from session only if no room IDs requested |
| 42 |
$cid = $sesscids; |
| 43 |
} |
| 44 |
|
| 45 |
// first room ID |
| 46 |
$roomid = (int)$cid[0]; |
| 47 |
|
| 48 |
if (empty($roomid)) { |
| 49 |
$q = "SELECT `id` FROM `#__vikbooking_rooms` WHERE `avail`=1 ORDER BY `#__vikbooking_rooms`.`name` ASC LIMIT 1"; |
| 50 |
$dbo->setQuery($q); |
| 51 |
$dbo->execute(); |
| 52 |
if ($dbo->getNumRows()) { |
| 53 |
$roomid = $dbo->loadResult(); |
| 54 |
} else { |
| 55 |
$q = "SELECT `id` FROM `#__vikbooking_rooms` ORDER BY `#__vikbooking_rooms`.`name` ASC LIMIT 1"; |
| 56 |
$dbo->setQuery($q); |
| 57 |
$dbo->execute(); |
| 58 |
if ($dbo->getNumRows()) { |
| 59 |
$roomid = $dbo->loadResult(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
if (empty($roomid)) { |
| 64 |
$mainframe->redirect("index.php?option=com_vikbooking&task=rooms"); |
| 65 |
exit; |
| 66 |
} |
| 67 |
// make sure to set at least the first index of cid[] |
| 68 |
$cid[0] = $roomid; |
| 69 |
// |
| 70 |
$q = "SELECT `id`,`name` FROM `#__vikbooking_rooms` ORDER BY `#__vikbooking_rooms`.`name` ASC;"; |
| 71 |
$dbo->setQuery($q); |
| 72 |
$dbo->execute(); |
| 73 |
$all_rooms = $dbo->getNumRows() ? $dbo->loadAssocList() : []; |
| 74 |
|
| 75 |
/** |
| 76 |
* Load categories to be used for group filter. |
| 77 |
* |
| 78 |
* @since 1.13 (J) - 1.3.0 (WP) |
| 79 |
*/ |
| 80 |
$q = "SELECT `id`,`name` FROM `#__vikbooking_categories` ORDER BY `#__vikbooking_categories`.`name` ASC;"; |
| 81 |
$dbo->setQuery($q); |
| 82 |
$dbo->execute(); |
| 83 |
$categories = $dbo->getNumRows() ? $dbo->loadAssocList() : array(); |
| 84 |
|
| 85 |
// load rooms rows for all requested rooms |
| 86 |
$roomrows = []; |
| 87 |
$reqids = []; |
| 88 |
$reqcats = []; |
| 89 |
foreach ($cid as $rid) { |
| 90 |
if (empty($rid)) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
$rid = (int)$rid; |
| 94 |
if ($rid < 0) { |
| 95 |
// category |
| 96 |
array_push($reqcats, ($rid + (abs($rid) * 2))); |
| 97 |
} else { |
| 98 |
// room |
| 99 |
array_push($reqids, $rid); |
| 100 |
} |
| 101 |
} |
| 102 |
if (!count($reqcats) && !count($reqids)) { |
| 103 |
$mainframe->redirect("index.php?option=com_vikbooking&task=rooms"); |
| 104 |
exit; |
| 105 |
} |
| 106 |
|
| 107 |
if (!count($reqcats) && empty($sesscids) && count($reqids) === 1 && count($all_rooms) > 1) { |
| 108 |
// push the first 5 rooms by default |
| 109 |
$max_def_rooms = 5; |
| 110 |
$def_rooms_count = 0; |
| 111 |
$reqids = []; |
| 112 |
foreach ($all_rooms as $aroom) { |
| 113 |
$reqids[] = $aroom['id']; |
| 114 |
$def_rooms_count++; |
| 115 |
if ($def_rooms_count >= $max_def_rooms) { |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$clauses = []; |
| 122 |
if (count($reqids)) { |
| 123 |
array_push($clauses, "`id` IN (" . implode(', ', $reqids) . ")"); |
| 124 |
} |
| 125 |
foreach ($reqcats as $cat_id) { |
| 126 |
array_push($clauses, "(`idcat`='" . $cat_id . ";' OR `idcat` LIKE '" . $cat_id . ";%' OR `idcat` LIKE '%;" . $cat_id . ";%' OR `idcat` LIKE '%;" . $cat_id . ";')"); |
| 127 |
} |
| 128 |
$q = "SELECT * FROM `#__vikbooking_rooms` WHERE ".implode(' OR ', $clauses)." ORDER BY `name` ASC;"; |
| 129 |
$dbo->setQuery($q); |
| 130 |
$dbo->execute(); |
| 131 |
if ($dbo->getNumRows()) { |
| 132 |
$rows = $dbo->loadAssocList(); |
| 133 |
foreach ($rows as $row) { |
| 134 |
$roomrows[$row['id']] = $row; |
| 135 |
} |
| 136 |
} |
| 137 |
if (!count($roomrows)) { |
| 138 |
$mainframe->redirect("index.php?option=com_vikbooking&task=rooms"); |
| 139 |
exit; |
| 140 |
} |
| 141 |
if (!count($reqids)) { |
| 142 |
// in case of just a category filter we need to restore the requested room IDs for later actions |
| 143 |
$reqids = array_keys($roomrows); |
| 144 |
} |
| 145 |
// get all requested and valid room IDs |
| 146 |
$req_room_ids = array_keys($roomrows); |
| 147 |
$session->set('vbRatesOviewCids', $req_room_ids); |
| 148 |
// Restrictions |
| 149 |
$all_restrictions = []; |
| 150 |
foreach ($req_room_ids as $rid) { |
| 151 |
$all_restrictions[(int)$rid] = VikBooking::loadRestrictions(true, array($rid)); |
| 152 |
} |
| 153 |
// length of stay pricing overview (only if one single room requested) |
| 154 |
$first_roomrestr = isset($all_restrictions[(int)$roomid]) ? $all_restrictions[(int)$roomid] : array(); |
| 155 |
$pnights_cal = VikRequest::getVar('nights_cal', array()); |
| 156 |
$pnights_cal = VikBooking::filterNightsSeasonsCal($pnights_cal); |
| 157 |
$room_nights_cal = explode(',', VikBooking::getRoomParam('seasoncal_nights', $roomrows[(int)$roomid]['params'])); |
| 158 |
$room_nights_cal = VikBooking::filterNightsSeasonsCal($room_nights_cal); |
| 159 |
$seasons_cal = []; |
| 160 |
$seasons_cal_nights = []; |
| 161 |
if (count($pnights_cal) > 0) { |
| 162 |
$seasons_cal_nights = $pnights_cal; |
| 163 |
} elseif (count($room_nights_cal) > 0) { |
| 164 |
$seasons_cal_nights = $room_nights_cal; |
| 165 |
} else { |
| 166 |
$q = "SELECT `days` FROM `#__vikbooking_dispcost` WHERE `idroom`=".intval($roomid)." ORDER BY `#__vikbooking_dispcost`.`days` ASC LIMIT 7;"; |
| 167 |
$dbo->setQuery($q); |
| 168 |
$dbo->execute(); |
| 169 |
if ($dbo->getNumRows() > 0) { |
| 170 |
$nights_vals = $dbo->loadAssocList(); |
| 171 |
$nights_got = []; |
| 172 |
foreach ($nights_vals as $night) { |
| 173 |
$nights_got[] = $night['days']; |
| 174 |
} |
| 175 |
$seasons_cal_nights = VikBooking::filterNightsSeasonsCal($nights_got); |
| 176 |
} |
| 177 |
} |
| 178 |
if (count($req_room_ids) > 1) { |
| 179 |
// it's useless to spend server resources to calculate the seasons calendar nights (LOS Pricing Overview) since it won't be displayed when more than 1 room |
| 180 |
$seasons_cal_nights = []; |
| 181 |
} |
| 182 |
if (count($seasons_cal_nights) > 0) { |
| 183 |
$q = "SELECT `p`.*,`tp`.`name`,`tp`.`attr`,`tp`.`idiva`,`tp`.`breakfast_included`,`tp`.`free_cancellation`,`tp`.`canc_deadline` FROM `#__vikbooking_dispcost` AS `p` LEFT JOIN `#__vikbooking_prices` `tp` ON `p`.`idprice`=`tp`.`id` WHERE `p`.`days` IN (".implode(',', $seasons_cal_nights).") AND `p`.`idroom`=".(int)$roomid." ORDER BY `p`.`days` ASC, `p`.`cost` ASC;"; |
| 184 |
$dbo->setQuery($q); |
| 185 |
$dbo->execute(); |
| 186 |
if ($dbo->getNumRows() > 0) { |
| 187 |
$tars = $dbo->loadAssocList(); |
| 188 |
$arrtar = []; |
| 189 |
foreach ($tars as $tar) { |
| 190 |
$arrtar[$tar['days']][] = $tar; |
| 191 |
} |
| 192 |
$seasons_cal['nights'] = $seasons_cal_nights; |
| 193 |
$seasons_cal['offseason'] = $arrtar; |
| 194 |
$q = "SELECT * FROM `#__vikbooking_seasons` WHERE `idrooms` LIKE '%-".$roomid."-%';"; |
| 195 |
$dbo->setQuery($q); |
| 196 |
$dbo->execute(); |
| 197 |
if ($dbo->getNumRows() > 0) { |
| 198 |
$seasons = $dbo->loadAssocList(); |
| 199 |
//Restrictions |
| 200 |
$all_seasons = []; |
| 201 |
$curtime = time(); |
| 202 |
foreach ($seasons as $sk => $s) { |
| 203 |
if (empty($s['from']) && empty($s['to'])) { |
| 204 |
continue; |
| 205 |
} |
| 206 |
$now_year = !empty($s['year']) ? $s['year'] : date('Y'); |
| 207 |
list($sfrom, $sto) = VikBooking::getSeasonRangeTs($s['from'], $s['to'], $now_year); |
| 208 |
if ($sto < $curtime && empty($s['year'])) { |
| 209 |
$now_year += 1; |
| 210 |
list($sfrom, $sto) = VikBooking::getSeasonRangeTs($s['from'], $s['to'], $now_year); |
| 211 |
} |
| 212 |
if ($sto >= $curtime) { |
| 213 |
$s['from_ts'] = $sfrom; |
| 214 |
$s['to_ts'] = $sto; |
| 215 |
$all_seasons[] = $s; |
| 216 |
} |
| 217 |
} |
| 218 |
if (count($all_seasons) > 0) { |
| 219 |
$vbo_df = VikBooking::getDateFormat(); |
| 220 |
$vbo_df = $vbo_df == "%d/%m/%Y" ? 'd/m/Y' : ($vbo_df == "%m/%d/%Y" ? 'm/d/Y' : 'Y/m/d'); |
| 221 |
$hcheckin = 0; |
| 222 |
$mcheckin = 0; |
| 223 |
$hcheckout = 0; |
| 224 |
$mcheckout = 0; |
| 225 |
$timeopst = VikBooking::getTimeOpenStore(); |
| 226 |
if (is_array($timeopst)) { |
| 227 |
$opent = VikBooking::getHoursMinutes($timeopst[0]); |
| 228 |
$closet = VikBooking::getHoursMinutes($timeopst[1]); |
| 229 |
$hcheckin = $opent[0]; |
| 230 |
$mcheckin = $opent[1]; |
| 231 |
$hcheckout = $closet[0]; |
| 232 |
$mcheckout = $closet[1]; |
| 233 |
} |
| 234 |
$all_seasons = VikBooking::sortSeasonsRangeTs($all_seasons); |
| 235 |
$seasons_cal['seasons'] = $all_seasons; |
| 236 |
$seasons_cal['season_prices'] = []; |
| 237 |
$seasons_cal['restrictions'] = []; |
| 238 |
//calc price changes for each season and for each num-night |
| 239 |
foreach ($all_seasons as $sk => $s) { |
| 240 |
$checkin_base_ts = $s['from_ts']; |
| 241 |
$is_dst = date('I', $checkin_base_ts); |
| 242 |
foreach ($arrtar as $numnights => $tar) { |
| 243 |
$checkout_base_ts = $s['to_ts']; |
| 244 |
for($i = 1; $i <= $numnights; $i++) { |
| 245 |
$checkout_base_ts += 86400; |
| 246 |
$is_now_dst = date('I', $checkout_base_ts); |
| 247 |
if ($is_dst != $is_now_dst) { |
| 248 |
if ((int)$is_dst == 1) { |
| 249 |
$checkout_base_ts += 3600; |
| 250 |
} else { |
| 251 |
$checkout_base_ts -= 3600; |
| 252 |
} |
| 253 |
$is_dst = $is_now_dst; |
| 254 |
} |
| 255 |
} |
| 256 |
//calc check-in and check-out ts for the two dates |
| 257 |
$first = VikBooking::getDateTimestamp(date($vbo_df, $checkin_base_ts), $hcheckin, $mcheckin); |
| 258 |
$second = VikBooking::getDateTimestamp(date($vbo_df, $checkout_base_ts), $hcheckout, $mcheckout); |
| 259 |
$tar = VikBooking::applySeasonsRoom($tar, $first, $second, $s); |
| 260 |
$seasons_cal['season_prices'][$sk][$numnights] = $tar; |
| 261 |
//Restrictions |
| 262 |
if (count($first_roomrestr)) { |
| 263 |
$season_restr = VikBooking::parseSeasonRestrictions($first, $second, $numnights, $first_roomrestr); |
| 264 |
if (count($season_restr) > 0) { |
| 265 |
$seasons_cal['restrictions'][$sk][$numnights] = $season_restr; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
//calendar rates |
| 275 |
$todayd = getdate(); |
| 276 |
$tsstart = mktime(0, 0, 0, $todayd['mon'], $todayd['mday'], $todayd['year']); |
| 277 |
$startdate = VikRequest::getString('startdate', '', 'request'); |
| 278 |
if (!empty($startdate)) { |
| 279 |
$startts = VikBooking::getDateTimestamp($startdate, 0, 0); |
| 280 |
if (!empty($startts)) { |
| 281 |
$session->set('vbRatesOviewTs', $startts); |
| 282 |
$tsstart = $startts; |
| 283 |
} |
| 284 |
} else { |
| 285 |
$prevts = $session->get('vbRatesOviewTs', ''); |
| 286 |
if (!empty($prevts)) { |
| 287 |
$tsstart = $prevts; |
| 288 |
} |
| 289 |
} |
| 290 |
$roomrates = []; |
| 291 |
// read the rates for the lowest number of nights for each room requested |
| 292 |
foreach ($req_room_ids as $rid) { |
| 293 |
$nowroomrates = []; |
| 294 |
/** |
| 295 |
* Some types of price may not have a cost for 1 or 2 nights, |
| 296 |
* so joining by MIN(`days`) may exclude certain types of price. |
| 297 |
* We need to manually get via PHP all types of price. |
| 298 |
* Old query below is no longer in use, even though it was |
| 299 |
* compatible with the SQL strict mode (only_full_group_by). |
| 300 |
* $q = "SELECT `r`.`id`,`r`.`idroom`,`r`.`days`,`r`.`idprice`,`r`.`cost`,`p`.`name` FROM `#__vikbooking_dispcost` AS `r` INNER JOIN (SELECT MIN(`days`) AS `min_days` FROM `#__vikbooking_dispcost` WHERE `idroom`=".(int)$rid." GROUP BY `idroom`) AS `r2` ON `r`.`days`=`r2`.`min_days` LEFT JOIN `#__vikbooking_prices` `p` ON `p`.`id`=`r`.`idprice` WHERE `r`.`idroom`=".(int)$rid." GROUP BY `r`.`id`,`r`.`idroom`,`r`.`days`,`r`.`idprice`,`r`.`cost`,`p`.`name` ORDER BY `r`.`days` ASC, `r`.`cost` ASC;"; |
| 301 |
* |
| 302 |
* @since 1.10 (J) - 1.0.14 (WP) |
| 303 |
*/ |
| 304 |
$q = "SELECT `r`.`id`,`r`.`idroom`,`r`.`days`,`r`.`idprice`,`r`.`cost`,`p`.`name`,`p`.`minlos` FROM `#__vikbooking_dispcost` AS `r` LEFT JOIN `#__vikbooking_prices` `p` ON `p`.`id`=`r`.`idprice` WHERE `r`.`idroom`=".(int)$rid." ORDER BY `r`.`days` ASC, `r`.`cost` ASC LIMIT 50;"; |
| 305 |
$dbo->setQuery($q); |
| 306 |
$dbo->execute(); |
| 307 |
if ($dbo->getNumRows() > 0) { |
| 308 |
$nowroomrates = $dbo->loadAssocList(); |
| 309 |
$parsed_room_prices = []; |
| 310 |
foreach ($nowroomrates as $rrk => $rrv) { |
| 311 |
if (isset($parsed_room_prices[$rrv['idprice']])) { |
| 312 |
unset($nowroomrates[$rrk]); |
| 313 |
continue; |
| 314 |
} |
| 315 |
$nowroomrates[$rrk]['cost'] = round(($rrv['cost'] / $rrv['days']), 2); |
| 316 |
$nowroomrates[$rrk]['days'] = 1; |
| 317 |
$parsed_room_prices[$rrv['idprice']] = 1; |
| 318 |
} |
| 319 |
} |
| 320 |
$nowroomrates = array_values($nowroomrates); |
| 321 |
// push rates for this room |
| 322 |
$roomrates[(int)$rid] = $nowroomrates; |
| 323 |
} |
| 324 |
|
| 325 |
// sort room rates by the best rate plan name |
| 326 |
foreach ($roomrates as $rid => $roomrplans) { |
| 327 |
$roomrates[$rid] = VikBooking::sortRatePlans($roomrplans); |
| 328 |
} |
| 329 |
|
| 330 |
// read all the bookings between these dates for all rooms |
| 331 |
$booked_dates = []; |
| 332 |
$MAX_DAYS = 60; |
| 333 |
$info_start = getdate($tsstart); |
| 334 |
$endts = mktime(23, 59, 59, $info_start['mon'], ($info_start['mday'] + $MAX_DAYS), $info_start['year']); |
| 335 |
$q = "SELECT `b`.*,`ob`.`idorder` FROM `#__vikbooking_busy` AS `b`,`#__vikbooking_ordersbusy` AS `ob` WHERE `b`.`idroom` IN (".implode(', ', $reqids).") AND `b`.`id`=`ob`.`idbusy` AND (`b`.`checkin`>=".$tsstart." OR `b`.`checkout`>=".$tsstart.") AND (`b`.`checkin`<=".$endts." OR `b`.`checkout`<=".$tsstart.");"; |
| 336 |
$dbo->setQuery($q); |
| 337 |
$dbo->execute(); |
| 338 |
$rbusy = $dbo->getNumRows() > 0 ? $dbo->loadAssocList() : array(); |
| 339 |
$ridbusy = []; |
| 340 |
foreach ($rbusy as $rb) { |
| 341 |
if (!isset($ridbusy[$rb['idroom']])) { |
| 342 |
$ridbusy[$rb['idroom']] = []; |
| 343 |
} |
| 344 |
array_push($ridbusy[$rb['idroom']], $rb); |
| 345 |
} |
| 346 |
foreach ($req_room_ids as $rid) { |
| 347 |
$booked_dates[(int)$rid] = isset($ridbusy[(int)$rid]) ? $ridbusy[(int)$rid] : []; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Load room day notes for the requested dates |
| 352 |
* |
| 353 |
* @since 1.13.5 |
| 354 |
*/ |
| 355 |
$rdaynotes = VikBooking::getCriticalDatesInstance()->loadRoomDayNotes(date('Y-m-d', $tsstart), date('Y-m-d', $endts)); |
| 356 |
// |
| 357 |
|
| 358 |
$this->all_rooms = $all_rooms; |
| 359 |
$this->categories = $categories; |
| 360 |
$this->reqcats = $reqcats; |
| 361 |
$this->all_restrictions = $all_restrictions; |
| 362 |
$this->roomrows = $roomrows; |
| 363 |
$this->seasons_cal_nights = $seasons_cal_nights; |
| 364 |
$this->seasons_cal = $seasons_cal; |
| 365 |
$this->tsstart = $tsstart; |
| 366 |
$this->roomrates = $roomrates; |
| 367 |
$this->booked_dates = $booked_dates; |
| 368 |
$this->req_room_ids = $req_room_ids; |
| 369 |
$this->firstroom = $roomid; |
| 370 |
$this->festivities = $festivities; |
| 371 |
$this->rdaynotes = $rdaynotes; |
| 372 |
|
| 373 |
// Display the template |
| 374 |
parent::display($tpl); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Sets the toolbar |
| 379 |
*/ |
| 380 |
protected function addToolBar() |
| 381 |
{ |
| 382 |
JToolBarHelper::title(JText::translate('VBMAINRATESOVERVIEWTITLE'), 'vikbooking'); |
| 383 |
if (JFactory::getUser()->authorise('core.create', 'com_vikbooking')) { |
| 384 |
JToolBarHelper::addNew('newseason', JText::translate('VBMAINSEASONSNEW')); |
| 385 |
JToolBarHelper::spacer(); |
| 386 |
JToolBarHelper::addNew('newrestriction', JText::translate('VBMAINRESTRICTIONNEW')); |
| 387 |
JToolBarHelper::spacer(); |
| 388 |
} |
| 389 |
JToolBarHelper::cancel( 'cancel', JText::translate('VBBACK')); |
| 390 |
JToolBarHelper::spacer(); |
| 391 |
} |
| 392 |
} |
| 393 |
|