| 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 VikBookingViewCalendar 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.availability', 'com_vikbooking') && !JFactory::getUser()->authorise('core.vbo.bookings', 'com_vikbooking')) { |
| 24 |
VBOHttpDocument::getInstance()->close(403, JText::translate('JERROR_ALERTNOAUTHOR')); |
| 25 |
} |
| 26 |
|
| 27 |
$cid = VikRequest::getVar('cid', array(0)); |
| 28 |
$aid = $cid[0]; |
| 29 |
|
| 30 |
$app = JFactory::getApplication(); |
| 31 |
$dbo = JFactory::getDbo(); |
| 32 |
$session = JFactory::getSession(); |
| 33 |
|
| 34 |
$rid = $session->get('vbCalRid', ''); |
| 35 |
$aid = !empty($rid) && empty($aid) ? $rid : $aid; |
| 36 |
if (empty($aid)) { |
| 37 |
$q = "SELECT `id` FROM `#__vikbooking_rooms` ORDER BY `#__vikbooking_rooms`.`name` ASC"; |
| 38 |
$dbo->setQuery($q, 0, 1); |
| 39 |
$aid = $dbo->loadResult(); |
| 40 |
} |
| 41 |
if (empty($aid)) { |
| 42 |
VikError::raiseWarning('', 'No Rooms.'); |
| 43 |
$app->redirect("index.php?option=com_vikbooking&task=rooms"); |
| 44 |
$app->close(); |
| 45 |
} |
| 46 |
|
| 47 |
$session->set('vbCalRid', $aid); |
| 48 |
$pvmode = VikRequest::getString('vmode', '', 'request'); |
| 49 |
$cur_vmode = $session->get('vikbookingvmode', ""); |
| 50 |
if (!empty($pvmode) && ctype_digit($pvmode)) { |
| 51 |
$session->set('vikbookingvmode', $pvmode); |
| 52 |
} elseif (empty($cur_vmode)) { |
| 53 |
$session->set('vikbookingvmode', "12"); |
| 54 |
} |
| 55 |
$vmode = (int)$session->get('vikbookingvmode', "12"); |
| 56 |
|
| 57 |
// new reservation ID default status |
| 58 |
$new_res_id = 0; |
| 59 |
|
| 60 |
$q = "SELECT * FROM `#__vikbooking_rooms` WHERE `id`=".$dbo->quote($aid); |
| 61 |
$dbo->setQuery($q, 0, 1); |
| 62 |
$room = $dbo->loadAssoc(); |
| 63 |
if (!$room) { |
| 64 |
$session->set('vbCalRid', ''); |
| 65 |
VikError::raiseWarning('', 'No Rooms.'); |
| 66 |
$app->redirect("index.php?option=com_vikbooking&task=rooms"); |
| 67 |
$app->close(); |
| 68 |
} |
| 69 |
|
| 70 |
$q = "SELECT `id`,`name`,`img`,`units` FROM `#__vikbooking_rooms` ORDER BY `#__vikbooking_rooms`.`name` ASC;"; |
| 71 |
$dbo->setQuery($q); |
| 72 |
$allc = $dbo->loadAssocList(); |
| 73 |
|
| 74 |
$q = "SELECT `id`,`name` FROM `#__vikbooking_gpayments` ORDER BY `#__vikbooking_gpayments`.`name` ASC;"; |
| 75 |
$dbo->setQuery($q); |
| 76 |
$payments = $dbo->loadAssocList(); |
| 77 |
|
| 78 |
/** |
| 79 |
* Split stay data for booking. |
| 80 |
* |
| 81 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 82 |
*/ |
| 83 |
$split_stay_data = VikRequest::getVar('split_stay_data', array()); |
| 84 |
|
| 85 |
$actnow = time(); |
| 86 |
$forced_reason = ''; |
| 87 |
$forcebooking = VikRequest::getInt('forcebooking', 0, 'request'); |
| 88 |
$pcheckindate = VikRequest::getString('checkindate', '', 'request'); |
| 89 |
$pcheckoutdate = VikRequest::getString('checkoutdate', '', 'request'); |
| 90 |
$pcheckinh = VikRequest::getString('checkinh', '', 'request'); |
| 91 |
$pcheckinm = VikRequest::getString('checkinm', '', 'request'); |
| 92 |
$pcheckouth = VikRequest::getString('checkouth', '', 'request'); |
| 93 |
$pcheckoutm = VikRequest::getString('checkoutm', '', 'request'); |
| 94 |
$pcustdata = VikRequest::getString('custdata', '', 'request'); |
| 95 |
$pcustmail = VikRequest::getString('custmail', '', 'request'); |
| 96 |
$padults = VikRequest::getInt('adults', 0, 'request'); |
| 97 |
$pchildren = VikRequest::getInt('children', 0, 'request'); |
| 98 |
$psetclosed = VikRequest::getInt('setclosed', 0, 'request'); |
| 99 |
$psetunitsclosed = VikRequest::getInt('setunitsclosed', 0, 'request'); |
| 100 |
$num_rooms = VikRequest::getInt('num_rooms', 0, 'request'); |
| 101 |
$num_rooms = empty($num_rooms) || $num_rooms <= 0 ? 1 : $num_rooms; |
| 102 |
$pordstatus = VikRequest::getString('newstatus', '', 'request'); |
| 103 |
$pordstatus = (empty($pordstatus) || !in_array($pordstatus, array('confirmed', 'standby')) ? 'confirmed' : $pordstatus); |
| 104 |
$pordstatus = intval($psetclosed) > 0 ? 'confirmed' : $pordstatus; |
| 105 |
$pcountrycode = VikRequest::getString('countrycode', '', 'request'); |
| 106 |
$pstate = VikRequest::getString('state', '', 'request'); |
| 107 |
$pt_first_name = VikRequest::getString('t_first_name', '', 'request'); |
| 108 |
$pt_last_name = VikRequest::getString('t_last_name', '', 'request'); |
| 109 |
$pphone = VikRequest::getString('phone', '', 'request'); |
| 110 |
$pcustomer_id = VikRequest::getInt('customer_id', 0, 'request'); |
| 111 |
$ppaymentid = VikRequest::getString('payment', '', 'request'); |
| 112 |
$pcust_cost = VikRequest::getFloat('cust_cost', 0, 'request'); |
| 113 |
$proomcost = VikRequest::getFloat('roomcost', 0, 'request'); |
| 114 |
$pidprice = VikRequest::getInt('idprice', 0, 'request'); |
| 115 |
$id_tariff = 0; |
| 116 |
$ptotalpnight = VikRequest::getString('totalpnight', 'total', 'request'); |
| 117 |
$ptaxid = VikRequest::getInt('taxid', 0, 'request'); |
| 118 |
$pcloseothers = VikRequest::getVar('closeothers', array()); |
| 119 |
|
| 120 |
/** |
| 121 |
* Mark the units as closed in case of a regular reservation for no customers. |
| 122 |
* |
| 123 |
* @since 1.16.6 (J) - 1.6.6 (WP) |
| 124 |
*/ |
| 125 |
if ($psetunitsclosed && ($psetclosed || $pordstatus != 'confirmed' || $pcustomer_id)) { |
| 126 |
$psetunitsclosed = 0; |
| 127 |
} |
| 128 |
|
| 129 |
$paymentmeth = ''; |
| 130 |
if (!empty($ppaymentid) && $payments) { |
| 131 |
foreach ($payments as $pay) { |
| 132 |
if (intval($pay['id']) == intval($ppaymentid)) { |
| 133 |
$paymentmeth = $pay['id'].'='.$pay['name']; |
| 134 |
break; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// check if a new booking should be created |
| 140 |
if (!empty($pcheckindate) && !empty($pcheckoutdate)) { |
| 141 |
// validate basic information submitted |
| 142 |
$can_create_res = true; |
| 143 |
if (!VikBooking::dateIsValid($pcheckindate) || !VikBooking::dateIsValid($pcheckoutdate)) { |
| 144 |
$can_create_res = false; |
| 145 |
VikError::raiseWarning('', JText::translate('ERRINVDATESEASON')); |
| 146 |
} |
| 147 |
|
| 148 |
// get stay timestamps |
| 149 |
$first = VikBooking::getDateTimestamp($pcheckindate, $pcheckinh, $pcheckinm); |
| 150 |
$second = VikBooking::getDateTimestamp($pcheckoutdate, $pcheckouth, $pcheckoutm); |
| 151 |
|
| 152 |
if ($first >= $second) { |
| 153 |
$can_create_res = false; |
| 154 |
VikError::raiseWarning('', 'Invalid Dates: current server time is '.date('Y-m-d H:i', $actnow).'. Reservation requested from '.date('Y-m-d H:i', $first).' to '.date('Y-m-d H:i', $second)); |
| 155 |
} |
| 156 |
|
| 157 |
// count nights of stay |
| 158 |
$secdiff = $second - $first; |
| 159 |
$daysdiff = $secdiff / 86400; |
| 160 |
if (is_int($daysdiff)) { |
| 161 |
if ($daysdiff < 1) { |
| 162 |
$daysdiff = 1; |
| 163 |
} |
| 164 |
} else { |
| 165 |
if ($daysdiff < 1) { |
| 166 |
$daysdiff = 1; |
| 167 |
} else { |
| 168 |
$sum = floor($daysdiff) * 86400; |
| 169 |
$newdiff = $secdiff - $sum; |
| 170 |
$maxhmore = VikBooking::getHoursMoreRb() * 3600; |
| 171 |
if ($maxhmore >= $newdiff) { |
| 172 |
$daysdiff = floor($daysdiff); |
| 173 |
} else { |
| 174 |
$daysdiff = ceil($daysdiff); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ($can_create_res) { |
| 180 |
/** |
| 181 |
* Get an instance of the reservation model by injecting all values. |
| 182 |
* |
| 183 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 184 |
*/ |
| 185 |
$model_res = VBOModelReservation::getInstance([ |
| 186 |
'force_booking' => $forcebooking, |
| 187 |
'split_stay' => $split_stay_data, |
| 188 |
'set_closed' => $psetclosed, |
| 189 |
'units_closed' => $psetunitsclosed, |
| 190 |
'close_others' => $pcloseothers, |
| 191 |
'status' => $pordstatus, |
| 192 |
'checkin' => $first, |
| 193 |
'checkout' => $second, |
| 194 |
'checkin_h' => $pcheckinh, |
| 195 |
'checkin_m' => $pcheckinm, |
| 196 |
'checkout_h' => $pcheckouth, |
| 197 |
'checkout_m' => $pcheckoutm, |
| 198 |
'nights' => $daysdiff, |
| 199 |
'num_rooms' => $num_rooms, |
| 200 |
'adults' => $padults, |
| 201 |
'children' => $pchildren, |
| 202 |
'id_payment' => $paymentmeth, |
| 203 |
])->setCustomer([ |
| 204 |
'id' => $pcustomer_id, |
| 205 |
'first_name' => $pt_first_name, |
| 206 |
'last_name' => $pt_last_name, |
| 207 |
'data' => $pcustdata, |
| 208 |
'email' => $pcustmail, |
| 209 |
'country' => $pcountrycode, |
| 210 |
'state' => $pstate, |
| 211 |
'phone' => $pphone, |
| 212 |
])->setRoom([ |
| 213 |
'id' => $room['id'], |
| 214 |
'cust_cost' => $pcust_cost, |
| 215 |
'total_or_pnight' => $ptotalpnight, |
| 216 |
'room_cost' => $proomcost, |
| 217 |
'id_price' => $pidprice, |
| 218 |
'id_tax' => $ptaxid, |
| 219 |
]); |
| 220 |
|
| 221 |
// store the reservation |
| 222 |
$model_res->create(); |
| 223 |
|
| 224 |
// get the new booking ID |
| 225 |
$res_id = $model_res->getNewBookingID(); |
| 226 |
if (!$res_id) { |
| 227 |
VikError::raiseWarning('', $model_res->getError()); |
| 228 |
$app->redirect("index.php?option=com_vikbooking&task=calendar"); |
| 229 |
$app->close(); |
| 230 |
} |
| 231 |
|
| 232 |
// check if any action for the Channel Manager should be displayed |
| 233 |
$vcm_action = $model_res->getChannelManagerAction(); |
| 234 |
if ($vcm_action) { |
| 235 |
VikError::raiseNotice('', $vcm_action); |
| 236 |
} |
| 237 |
|
| 238 |
if ($pordstatus == 'standby') { |
| 239 |
// redirect only if the booking status is pending |
| 240 |
$app->enqueueMessage(JText::translate('VBQUICKRESWARNSTANDBY')); |
| 241 |
$app->redirect("index.php?option=com_vikbooking&task=editbusy&cid[]=" . $res_id); |
| 242 |
$app->close(); |
| 243 |
} |
| 244 |
|
| 245 |
// set new reservation ID |
| 246 |
$new_res_id = $res_id; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
$mints = mktime(0, 0, 0, date('m'), 1, date('Y')); |
| 251 |
$q = "SELECT `b`.*,`ob`.`idorder`,`o`.`closure` FROM `#__vikbooking_busy` AS `b` LEFT JOIN `#__vikbooking_ordersbusy` `ob` ON `ob`.`idbusy`=`b`.`id` LEFT JOIN `#__vikbooking_orders` `o` ON `o`.`id`=`ob`.`idorder` WHERE `b`.`idroom`=".(int)$room['id']." AND (`b`.`checkin`>=".$mints." OR `b`.`checkout`>=".$mints.") AND `ob`.`idorder` IS NOT NULL;"; |
| 252 |
$dbo->setQuery($q); |
| 253 |
$busy = $dbo->loadAssocList(); |
| 254 |
|
| 255 |
$this->room = $room; |
| 256 |
$this->new_res_id = $new_res_id; |
| 257 |
$this->allc = $allc; |
| 258 |
$this->payments = $payments; |
| 259 |
$this->busy = $busy; |
| 260 |
$this->vmode = $vmode; |
| 261 |
|
| 262 |
// Display the template |
| 263 |
parent::display($tpl); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Sets the toolbar |
| 268 |
*/ |
| 269 |
protected function addToolBar() |
| 270 |
{ |
| 271 |
JToolBarHelper::title(JText::translate('VBMAINCALTITLE'), 'vikbooking'); |
| 272 |
JToolBarHelper::cancel( 'canceledorder', JText::translate('VBBACK')); |
| 273 |
JToolBarHelper::spacer(); |
| 274 |
} |
| 275 |
} |
| 276 |
|