PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.8
VikBooking Hotel Booking Engine & PMS v1.6.8
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.8, at admin/views/ratesoverv/view.html.php

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