analytics.php
4 years ago
apiban.php
4 years ago
apilog.php
4 years ago
apiplugin.php
4 years ago
apiuser.php
4 years ago
backup.php
4 years ago
calendar.php
4 years ago
city.php
4 years ago
closure.php
1 month ago
configapp.php
4 years ago
configcldays.php
2 years ago
configcron.php
4 years ago
configemp.php
4 years ago
configsmsapi.php
4 years ago
configuration.php
1 month ago
conversion.php
1 year ago
country.php
4 years ago
coupon.php
4 years ago
coupongroup.php
4 years ago
cronjob.php
2 years ago
cronjoblog.php
4 years ago
customer.php
4 months ago
customf.php
1 year ago
dashboard.php
4 years ago
emplocwdays.php
4 years ago
employee.php
1 year ago
emprates.php
4 years ago
export.php
4 years ago
exportres.php
4 years ago
file.php
4 months ago
findreservation.php
1 month ago
group.php
4 years ago
import.php
4 years ago
index.html
4 years ago
invoice.php
1 month ago
langcustomf.php
4 years ago
langemployee.php
4 years ago
langgroup.php
4 years ago
langmedia.php
4 years ago
langoption.php
4 years ago
langoptiongroup.php
4 years ago
langpackage.php
4 years ago
langpackgroup.php
4 years ago
langpayment.php
4 years ago
langservice.php
4 years ago
langstatuscode.php
4 years ago
langsubscr.php
4 years ago
langtax.php
4 years ago
location.php
4 years ago
mailtext.php
2 years ago
makerecurrence.php
1 month ago
media.php
4 years ago
multiorder.php
4 years ago
option.php
4 months ago
optiongroup.php
4 years ago
package.php
2 years ago
packgroup.php
4 years ago
packorder.php
1 year ago
payment.php
4 years ago
rate.php
4 years ago
reportsemp.php
4 years ago
reportsser.php
4 years ago
reservation.php
1 month ago
restriction.php
4 years ago
review.php
4 years ago
service.php
1 year ago
serworkday.php
4 months ago
state.php
4 years ago
statuscode.php
4 years ago
subscription.php
4 years ago
subscrorder.php
4 years ago
tag.php
4 years ago
tax.php
4 years ago
usernote.php
4 years ago
waitinglist.php
4 years ago
webhook.php
4 years ago
wizard.php
1 year ago
rate.php
413 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | VAPLoader::import('libraries.mvc.controllers.admin'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments special rate controller. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsControllerRate extends VAPControllerAdmin |
| 22 | { |
| 23 | /** |
| 24 | * Task used to access the creation page of a new record. |
| 25 | * |
| 26 | * @return boolean |
| 27 | */ |
| 28 | public function add() |
| 29 | { |
| 30 | $app = JFactory::getApplication(); |
| 31 | $user = JFactory::getUser(); |
| 32 | |
| 33 | // unset user state for being recovered again |
| 34 | $app->setUserState('vap.rate.data', array()); |
| 35 | |
| 36 | // check user permissions |
| 37 | if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.services', 'com_vikappointments')) |
| 38 | { |
| 39 | // back to main list, not authorised to create records |
| 40 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 41 | $this->cancel(); |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | $this->setRedirect('index.php?option=com_vikappointments&view=managerate'); |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Task used to access the management page of an existing record. |
| 53 | * |
| 54 | * @return boolean |
| 55 | */ |
| 56 | public function edit() |
| 57 | { |
| 58 | $app = JFactory::getApplication(); |
| 59 | $user = JFactory::getUser(); |
| 60 | |
| 61 | // unset user state for being recovered again |
| 62 | $app->setUserState('vap.rate.data', array()); |
| 63 | |
| 64 | // check user permissions |
| 65 | if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.services', 'com_vikappointments')) |
| 66 | { |
| 67 | // back to main list, not authorised to edit records |
| 68 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 69 | $this->cancel(); |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $cid = $app->input->getUint('cid', array(0)); |
| 75 | |
| 76 | $this->setRedirect('index.php?option=com_vikappointments&view=managerate&cid[]=' . $cid[0]); |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Task used to save the record data set in the request. |
| 83 | * After saving, the user is redirected to the main list. |
| 84 | * |
| 85 | * @return void |
| 86 | */ |
| 87 | public function saveclose() |
| 88 | { |
| 89 | if ($this->save()) |
| 90 | { |
| 91 | $this->cancel(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Task used to save the record data set in the request. |
| 97 | * After saving, the user is redirected to the creation |
| 98 | * page of a new record. |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function savenew() |
| 103 | { |
| 104 | if ($this->save()) |
| 105 | { |
| 106 | $this->setRedirect('index.php?option=com_vikappointments&task=rate.add'); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Task used to save the record data as a copy of the current item. |
| 112 | * After saving, the user is redirected to the management |
| 113 | * page of the record that has been saved. |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | public function savecopy() |
| 118 | { |
| 119 | $this->save(true); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Task used to save the record data set in the request. |
| 124 | * After saving, the user is redirected to the management |
| 125 | * page of the record that has been saved. |
| 126 | * |
| 127 | * @param boolean $copy True to save the record as a copy. |
| 128 | * |
| 129 | * @return boolean |
| 130 | */ |
| 131 | public function save($copy = false) |
| 132 | { |
| 133 | $app = JFactory::getApplication(); |
| 134 | $input = $app->input; |
| 135 | $user = JFactory::getUser(); |
| 136 | |
| 137 | /** |
| 138 | * Added token validation. |
| 139 | * |
| 140 | * @since 1.7 |
| 141 | */ |
| 142 | if (!JSession::checkToken()) |
| 143 | { |
| 144 | // back to main list, missing CSRF-proof token |
| 145 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 146 | $this->cancel(); |
| 147 | |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | $args = array(); |
| 152 | $args['name'] = $input->getString('name'); |
| 153 | $args['description'] = JComponentHelper::filterText($input->getRaw('description')); |
| 154 | $args['charge'] = abs($input->getFloat('charge')) * $input->getInt('factor', 1); |
| 155 | $args['people'] = $input->getUint('people', 0); |
| 156 | $args['published'] = $input->getUint('published', 0); |
| 157 | $args['weekdays'] = $input->getUint('weekdays', array()); |
| 158 | $args['usergroups'] = $input->getString('usergroups', array()); |
| 159 | $args['fromdate'] = $input->getString('fromdate'); |
| 160 | $args['todate'] = $input->getString('todate'); |
| 161 | $args['fromtime'] = $input->getUint('fromtime', 0); |
| 162 | $args['totime'] = $input->getUint('totime', 0); |
| 163 | $args['params'] = $input->get('params', array(), 'array'); |
| 164 | $args['services'] = $input->getUint('services', array()); |
| 165 | $args['id'] = $input->getUint('id', 0); |
| 166 | |
| 167 | if ($copy) |
| 168 | { |
| 169 | // unset ID to create a copy |
| 170 | $args['id'] = 0; |
| 171 | } |
| 172 | |
| 173 | // unset people if it should be ignored |
| 174 | if (!$input->getUint('enablepeople', 0)) |
| 175 | { |
| 176 | $args['people'] = 0; |
| 177 | } |
| 178 | |
| 179 | // unset range if the time shouldn't be used |
| 180 | if (!$input->getUint('usetime', 0) || $args['fromtime'] >= $args['totime']) |
| 181 | { |
| 182 | $args['fromtime'] = $args['totime'] = 0; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Convert timestamp from local timezone to UTC. |
| 187 | * |
| 188 | * @since 1.7 |
| 189 | */ |
| 190 | $args['fromdate'] = VAPDateHelper::getSqlDateLocale($args['fromdate']); |
| 191 | $args['todate'] = VAPDateHelper::getSqlDateLocale($args['todate']); |
| 192 | |
| 193 | $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create'); |
| 194 | |
| 195 | // check user permissions |
| 196 | if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.services', 'com_vikappointments')) |
| 197 | { |
| 198 | // back to main list, not authorised to create/edit records |
| 199 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 200 | $this->cancel(); |
| 201 | |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // get rate model |
| 206 | $rate = $this->getModel(); |
| 207 | |
| 208 | // try to save arguments |
| 209 | $id = $rate->save($args); |
| 210 | |
| 211 | if (!$id) |
| 212 | { |
| 213 | // get string error |
| 214 | $error = $rate->getError(null, true); |
| 215 | |
| 216 | // display error message |
| 217 | $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error'); |
| 218 | |
| 219 | $url = 'index.php?option=com_vikappointments&view=managerate'; |
| 220 | |
| 221 | if ($args['id']) |
| 222 | { |
| 223 | $url .= '&cid[]=' . $args['id']; |
| 224 | } |
| 225 | |
| 226 | // redirect to new/edit page |
| 227 | $this->setRedirect($url); |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | // display generic successful message |
| 233 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 234 | |
| 235 | // redirect to edit page |
| 236 | $this->setRedirect('index.php?option=com_vikappointments&task=rate.edit&cid[]=' . $id); |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Deletes a list of records set in the request. |
| 243 | * |
| 244 | * @return boolean |
| 245 | */ |
| 246 | public function delete() |
| 247 | { |
| 248 | $app = JFactory::getApplication(); |
| 249 | $user = JFactory::getUser(); |
| 250 | |
| 251 | /** |
| 252 | * Added token validation. |
| 253 | * Both GET and POST are supported. |
| 254 | * |
| 255 | * @since 1.7 |
| 256 | */ |
| 257 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 258 | { |
| 259 | // back to main list, missing CSRF-proof token |
| 260 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 261 | $this->cancel(); |
| 262 | |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | $cid = $app->input->get('cid', array(), 'uint'); |
| 267 | |
| 268 | // check user permissions |
| 269 | if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.services', 'com_vikappointments')) |
| 270 | { |
| 271 | // back to main list, not authorised to delete records |
| 272 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 273 | $this->cancel(); |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | // delete selected records |
| 279 | $this->getModel()->delete($cid); |
| 280 | |
| 281 | // back to main list |
| 282 | $this->cancel(); |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Publishes the selected records. |
| 289 | * |
| 290 | * @return boolean |
| 291 | */ |
| 292 | public function publish() |
| 293 | { |
| 294 | $app = JFactory::getApplication(); |
| 295 | $user = JFactory::getUser(); |
| 296 | |
| 297 | /** |
| 298 | * Added token validation. |
| 299 | * Both GET and POST are supported. |
| 300 | * |
| 301 | * @since 1.7 |
| 302 | */ |
| 303 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 304 | { |
| 305 | // back to main list, missing CSRF-proof token |
| 306 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 307 | $this->cancel(); |
| 308 | |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | $cid = $app->input->get('cid', array(), 'uint'); |
| 313 | $task = $app->input->get('task', null); |
| 314 | |
| 315 | $state = $task == 'unpublish' ? 0 : 1; |
| 316 | |
| 317 | // check user permissions |
| 318 | if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.services', 'com_vikappointments')) |
| 319 | { |
| 320 | // back to main list, not authorised to edit records |
| 321 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 322 | $this->cancel(); |
| 323 | |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | // change state of selected records |
| 328 | $this->getModel()->publish($cid, $state); |
| 329 | |
| 330 | // back to main list |
| 331 | $this->cancel(); |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Redirects the users to the main records list. |
| 338 | * |
| 339 | * @return void |
| 340 | */ |
| 341 | public function cancel() |
| 342 | { |
| 343 | $this->setRedirect('index.php?option=com_vikappointments&view=rates'); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * AJAX end-point used to test how the special rates are applied. |
| 348 | * The task expects the following arguments to be set in request. |
| 349 | * |
| 350 | * @param integer id_service The service ID. |
| 351 | * @param integer id_employee The employee ID (optional). |
| 352 | * @param string checkin The checkin date and time. |
| 353 | * @param integer people The number of people (optional). |
| 354 | * |
| 355 | * @return void |
| 356 | */ |
| 357 | function testajax() |
| 358 | { |
| 359 | $app = JFactory::getApplication(); |
| 360 | $input = $app->input; |
| 361 | |
| 362 | /** |
| 363 | * Added token validation. |
| 364 | * |
| 365 | * @since 1.7 |
| 366 | */ |
| 367 | if (!JSession::checkToken()) |
| 368 | { |
| 369 | // missing CSRF-proof token |
| 370 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 371 | } |
| 372 | |
| 373 | $id_service = $input->getUint('id_service', 0); |
| 374 | $id_employee = $input->getUint('id_employee', 0); |
| 375 | $usergroup = $input->getString('usergroup', 0); |
| 376 | $checkin = $input->getString('checkin'); |
| 377 | $people = $input->getUint('people', 1); |
| 378 | $is_debug = $input->getBool('debug', false); |
| 379 | |
| 380 | // store the last search in the user state |
| 381 | $app->setUserState('ratestest.id_service', $id_service); |
| 382 | $app->setUserState('ratestest.id_employee', $id_employee); |
| 383 | $app->setUserState('ratestest.usergroup', $usergroup); |
| 384 | $app->setUserState('ratestest.checkin', $checkin); |
| 385 | $app->setUserState('ratestest.people', $people); |
| 386 | $app->setUserState('ratestest.debug', $is_debug); |
| 387 | |
| 388 | // create checkin timestamp |
| 389 | $checkin = VAPDateHelper::getDate($checkin)->format('Y-m-d H:i:s'); |
| 390 | |
| 391 | // var used to trace the rates calculation |
| 392 | $trace = array(); |
| 393 | |
| 394 | if ($usergroup) |
| 395 | { |
| 396 | // inject property to force usergroup |
| 397 | $trace['usergroup'] = $usergroup; |
| 398 | } |
| 399 | |
| 400 | if ($is_debug) |
| 401 | { |
| 402 | // inject property to force debugging |
| 403 | $trace['debug'] = array(); |
| 404 | } |
| 405 | |
| 406 | // calculate rate |
| 407 | $rate = VAPSpecialRates::getRate($id_service, $id_employee, $checkin, $people, $trace); |
| 408 | |
| 409 | // send result to caller |
| 410 | $this->sendJSON(array($rate, $trace)); |
| 411 | } |
| 412 | } |
| 413 |