PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.7
VikBooking Hotel Booking Engine & PMS v1.6.7
1.8.16 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 All 37 releases
vikbooking / admin / views / ratesoverv / view.html.php

view.html.php in VikBooking Hotel Booking Engine & PMS 1.6.7, at admin/views/ratesoverv/view.html.php

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