apiban.php
4 years ago
apilog.php
2 years ago
apiplugin.php
4 years ago
apiuser.php
2 years ago
apiuseroptions.php
2 years ago
backup.php
4 months ago
caldays.php
1 month ago
calendar.php
1 month ago
city.php
4 years ago
closure.php
1 month ago
configapp.php
4 years ago
configcldays.php
4 years ago
configcron.php
4 years ago
configemp.php
4 years ago
configsmsapi.php
4 years ago
configuration.php
4 months ago
conversion.php
4 years ago
country.php
2 years ago
coupon.php
2 years ago
couponemployee.php
4 years ago
coupongroup.php
2 years ago
couponservice.php
4 years ago
cronjob.php
2 years ago
cronjoblog.php
4 years ago
customer.php
4 months ago
customf.php
2 years ago
customfservice.php
4 years ago
customizer.php
4 years ago
empgroup.php
2 years ago
employee.php
2 years ago
empsettings.php
4 years ago
file.php
4 years ago
findreservation.php
1 month ago
group.php
2 years ago
import.php
4 years ago
index.html
4 years ago
invoice.php
1 month ago
langcustomf.php
4 years ago
langempgroup.php
4 years ago
langemployee.php
4 years ago
langgroup.php
4 years ago
langmedia.php
4 years ago
langoption.php
2 years ago
langoptiongroup.php
4 years ago
langoptionvar.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
2 years ago
langtaxrule.php
4 years ago
location.php
2 years ago
mailtext.php
2 years ago
makerecurrence.php
1 month ago
media.php
2 years ago
multiorder.php
1 month ago
option.php
1 year ago
optiongroup.php
2 years ago
optionvar.php
1 year ago
orderstatus.php
2 years ago
package.php
2 years ago
packageservice.php
4 years ago
packgroup.php
2 years ago
packorder.php
2 years ago
packorderitem.php
1 month ago
payment.php
2 years ago
rate.php
2 years ago
reportsemp.php
4 months ago
reportsser.php
4 months ago
reservation.php
1 month ago
resoptassoc.php
2 years ago
restriction.php
2 years ago
review.php
3 years ago
serempassoc.php
1 year ago
seroptassoc.php
4 years ago
serrateassoc.php
4 years ago
serrestrassoc.php
4 years ago
service.php
2 years ago
state.php
2 years ago
statswidget.php
2 years ago
statuscode.php
2 years ago
subscription.php
1 month ago
subscrorder.php
1 month ago
tag.php
4 years ago
tax.php
2 years ago
taxrule.php
2 years ago
updateprogram.php
4 years ago
usernote.php
2 years ago
waitinglist.php
1 month ago
webhook.php
1 year ago
worktime.php
1 month ago
service.php
335 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.model'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments service model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelService extends JModelVAP |
| 22 | { |
| 23 | /** |
| 24 | * Cache of has own calendars. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected static $privCal = array(); |
| 29 | |
| 30 | /** |
| 31 | * Cache of zip restrictions. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected static $zipRestr = array(); |
| 36 | |
| 37 | /** |
| 38 | * Cache of assigned employees. |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | protected static $employees = array(); |
| 43 | |
| 44 | /** |
| 45 | * Basic item loading implementation. |
| 46 | * |
| 47 | * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match. |
| 48 | * If not set the instance property value is used. |
| 49 | * @param boolean $new True to return an empty object if missing. |
| 50 | * |
| 51 | * @return mixed The record object on success, null otherwise. |
| 52 | */ |
| 53 | public function getItem($pk, $new = false) |
| 54 | { |
| 55 | // load item through parent |
| 56 | $item = parent::getItem($pk, $new); |
| 57 | |
| 58 | if ($item && !$item->id) |
| 59 | { |
| 60 | // use default duration |
| 61 | $item->duration = 60; |
| 62 | } |
| 63 | |
| 64 | return $item; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Extend delete implementation to delete any related records |
| 69 | * stored within a separated table. |
| 70 | * |
| 71 | * @param mixed $ids Either the record ID or a list of records. |
| 72 | * |
| 73 | * @return boolean True on success, false otherwise. |
| 74 | */ |
| 75 | public function delete($ids) |
| 76 | { |
| 77 | // only int values are accepted |
| 78 | $ids = array_map('intval', (array) $ids); |
| 79 | |
| 80 | // invoke parent first |
| 81 | if (!parent::delete($ids)) |
| 82 | { |
| 83 | // nothing to delete |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $dbo = JFactory::getDbo(); |
| 88 | |
| 89 | // load any assigned translation |
| 90 | $q = $dbo->getQuery(true) |
| 91 | ->select($dbo->qn('id')) |
| 92 | ->from($dbo->qn('#__vikappointments_lang_service')) |
| 93 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 94 | |
| 95 | $dbo->setQuery($q); |
| 96 | |
| 97 | if ($lang_ids = $dbo->loadColumn()) |
| 98 | { |
| 99 | // get translation model |
| 100 | $model = JModelVAP::getInstance('langservice'); |
| 101 | // delete assigned translations |
| 102 | $model->delete($lang_ids); |
| 103 | } |
| 104 | |
| 105 | // load any service-employee relation |
| 106 | $q = $dbo->getQuery(true) |
| 107 | ->select($dbo->qn('id')) |
| 108 | ->from($dbo->qn('#__vikappointments_ser_emp_assoc')) |
| 109 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 110 | |
| 111 | $dbo->setQuery($q); |
| 112 | |
| 113 | if ($assoc_ids = $dbo->loadColumn()) |
| 114 | { |
| 115 | // get model |
| 116 | $model = JModelVAP::getInstance('serempassoc'); |
| 117 | // delete relations and attached WORKING DAYS |
| 118 | $model->delete($assoc_ids); |
| 119 | } |
| 120 | |
| 121 | // load any service-option relation |
| 122 | $q = $dbo->getQuery(true) |
| 123 | ->select($dbo->qn('id')) |
| 124 | ->from($dbo->qn('#__vikappointments_ser_opt_assoc')) |
| 125 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 126 | |
| 127 | $dbo->setQuery($q); |
| 128 | |
| 129 | if ($assoc_ids = $dbo->loadColumn()) |
| 130 | { |
| 131 | // get model |
| 132 | $model = JModelVAP::getInstance('seroptassoc'); |
| 133 | // delete relations |
| 134 | $model->delete($assoc_ids); |
| 135 | } |
| 136 | |
| 137 | // load any service-rate relation |
| 138 | $q = $dbo->getQuery(true) |
| 139 | ->select($dbo->qn('id')) |
| 140 | ->from($dbo->qn('#__vikappointments_ser_rates_assoc')) |
| 141 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 142 | |
| 143 | $dbo->setQuery($q); |
| 144 | |
| 145 | if ($assoc_ids = $dbo->loadColumn()) |
| 146 | { |
| 147 | // get model |
| 148 | $model = JModelVAP::getInstance('serrateassoc'); |
| 149 | // delete relations |
| 150 | $model->delete($assoc_ids); |
| 151 | } |
| 152 | |
| 153 | // load any service-restriction relation |
| 154 | $q = $dbo->getQuery(true) |
| 155 | ->select($dbo->qn('id')) |
| 156 | ->from($dbo->qn('#__vikappointments_ser_restr_assoc')) |
| 157 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 158 | |
| 159 | $dbo->setQuery($q); |
| 160 | |
| 161 | if ($assoc_ids = $dbo->loadColumn()) |
| 162 | { |
| 163 | // get model |
| 164 | $model = JModelVAP::getInstance('serrestrassoc'); |
| 165 | // delete relations |
| 166 | $model->delete($assoc_ids); |
| 167 | } |
| 168 | |
| 169 | // load any service-coupon relation |
| 170 | $q = $dbo->getQuery(true) |
| 171 | ->select($dbo->qn('id')) |
| 172 | ->from($dbo->qn('#__vikappointments_coupon_service_assoc')) |
| 173 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 174 | |
| 175 | $dbo->setQuery($q); |
| 176 | |
| 177 | if ($assoc_ids = $dbo->loadColumn()) |
| 178 | { |
| 179 | // get model |
| 180 | $model = JModelVAP::getInstance('couponservice'); |
| 181 | // delete relations |
| 182 | $model->delete($assoc_ids); |
| 183 | } |
| 184 | |
| 185 | // load any service-custom field relation |
| 186 | $q = $dbo->getQuery(true) |
| 187 | ->select($dbo->qn('id')) |
| 188 | ->from($dbo->qn('#__vikappointments_cf_service_assoc')) |
| 189 | ->where($dbo->qn('id_service') . ' IN (' . implode(',', $ids) . ')' ); |
| 190 | |
| 191 | $dbo->setQuery($q); |
| 192 | |
| 193 | if ($assoc_ids = $dbo->loadColumn()) |
| 194 | { |
| 195 | // get model |
| 196 | $model = JModelVAP::getInstance('customfservice'); |
| 197 | // delete relations |
| 198 | $model->delete($assoc_ids); |
| 199 | } |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns a list of employees assigned to the specified service. |
| 206 | * |
| 207 | * @param integer $id The service ID. |
| 208 | * @param boolean $strict True to return all the employees. |
| 209 | * False to obtain only the employees listed in the front-end. |
| 210 | * |
| 211 | * @return array A list of employees. |
| 212 | */ |
| 213 | public function getEmployees($id, $strict = false) |
| 214 | { |
| 215 | // check whether the employees of the specified service |
| 216 | // have been already registered within the internal cache |
| 217 | if (!isset(static::$employees[$id])) |
| 218 | { |
| 219 | static::$employees[$id] = []; |
| 220 | |
| 221 | $dbo = JFactory::getDbo(); |
| 222 | |
| 223 | $q = $dbo->getQuery(true) |
| 224 | ->select('a.*') |
| 225 | ->select($dbo->qn('e.id', 'id_employee')) |
| 226 | ->select($dbo->qn('e.nickname')) |
| 227 | ->from($dbo->qn('#__vikappointments_employee', 'e')) |
| 228 | ->leftjoin($dbo->qn('#__vikappointments_ser_emp_assoc', 'a') . ' ON ' . $dbo->qn('a.id_employee') . ' = ' . $dbo->qn('e.id')) |
| 229 | ->where($dbo->qn('a.id_service') . ' = ' . (int) $id) |
| 230 | ->order($dbo->qn('a.ordering') . ' ASC'); |
| 231 | |
| 232 | if ($strict) |
| 233 | { |
| 234 | $q->where($dbo->qn('e.listable') . ' = 1'); |
| 235 | } |
| 236 | |
| 237 | $dbo->setQuery($q); |
| 238 | |
| 239 | foreach ($dbo->loadObjectList() as $employee) |
| 240 | { |
| 241 | // use a different name for assoc ID |
| 242 | $employee->id_assoc = $employee->id; |
| 243 | // switch employee ID to standard name |
| 244 | $employee->id = $employee->id_employee; |
| 245 | unset($employee->id_employee); |
| 246 | |
| 247 | static::$employees[$id][] = $employee; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return static::$employees[$id]; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Calculates the average price of the specified services. |
| 256 | * |
| 257 | * @param array $ids A list of services to fetch. Leave empty |
| 258 | * to load all the services. |
| 259 | * |
| 260 | * @return float The average price. |
| 261 | */ |
| 262 | public function getAveragePrice(array $ids = array()) |
| 263 | { |
| 264 | $dbo = JFactory::getDbo(); |
| 265 | |
| 266 | $q = $dbo->getQuery(true) |
| 267 | ->select('AVG(' . $dbo->qn('price') . ')') |
| 268 | ->from($dbo->qn('#__vikappointments_service')); |
| 269 | |
| 270 | if ($ids) |
| 271 | { |
| 272 | $q->where($dbo->qn('id') . ' IN (' . implode(',', array_map('intval', $ids)) . ')'); |
| 273 | } |
| 274 | |
| 275 | $dbo->setQuery($q); |
| 276 | return (float) $dbo->loadResult(); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Checks if the given service owns a private calendar |
| 281 | * that cannot be shared with other services. |
| 282 | * |
| 283 | * @param integer $id The service ID. |
| 284 | * |
| 285 | * @return boolean True if own calendar, false otherwise. |
| 286 | */ |
| 287 | public function hasOwnCalendar($id) |
| 288 | { |
| 289 | if (!isset(static::$privCal[$id])) |
| 290 | { |
| 291 | static::$privCal[$id] = false; |
| 292 | |
| 293 | $dbo = JFactory::getDbo(); |
| 294 | |
| 295 | $q = $dbo->getQuery(true) |
| 296 | ->select($dbo->qn('has_own_cal')) |
| 297 | ->from($dbo->qn('#__vikappointments_service')) |
| 298 | ->where($dbo->qn('id') . ' = ' . (int) $id); |
| 299 | |
| 300 | $dbo->setQuery($q, 0, 1); |
| 301 | static::$privCal[$id] = (bool) $dbo->loadResult(); |
| 302 | } |
| 303 | |
| 304 | return static::$privCal[$id]; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Checks if the given service owns a private calendar |
| 309 | * that cannot be shared with other services. |
| 310 | * |
| 311 | * @param integer $id The service ID. |
| 312 | * |
| 313 | * @return boolean True if own calendar, false otherwise. |
| 314 | */ |
| 315 | public function hasZipRestriction($id) |
| 316 | { |
| 317 | if (!isset(static::$zipRestr[$id])) |
| 318 | { |
| 319 | static::$zipRestr[$id] = false; |
| 320 | |
| 321 | $dbo = JFactory::getDbo(); |
| 322 | |
| 323 | $q = $dbo->getQuery(true) |
| 324 | ->select($dbo->qn('enablezip')) |
| 325 | ->from($dbo->qn('#__vikappointments_service')) |
| 326 | ->where($dbo->qn('id') . ' = ' . (int) $id); |
| 327 | |
| 328 | $dbo->setQuery($q, 0, 1); |
| 329 | static::$zipRestr[$id] = (bool) $dbo->loadResult(); |
| 330 | } |
| 331 | |
| 332 | return static::$zipRestr[$id]; |
| 333 | } |
| 334 | } |
| 335 |