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
option.php
339 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 option model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelOption extends JModelVAP |
| 22 | { |
| 23 | /** |
| 24 | * Checks whether there's a relation between the specified |
| 25 | * option and service. |
| 26 | * |
| 27 | * @param integer $id_option The option ID. |
| 28 | * @param integer $id_service The service ID. |
| 29 | * |
| 30 | * @return booelan True if published, false otherwise. |
| 31 | */ |
| 32 | public function exists($id_option, $id_service) |
| 33 | { |
| 34 | // create search query |
| 35 | $pk = array( |
| 36 | 'id_service' => (int) $id_service, |
| 37 | 'id_option' => (int) $id_option, |
| 38 | ); |
| 39 | |
| 40 | // get existing relation, if any |
| 41 | return (bool) JModelVAP::getInstance('seroptassoc')->getItem($pk); |
| 42 | } |
| 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 | if (is_array($pk) && array_key_exists('id_variation', $pk)) |
| 56 | { |
| 57 | // extract variation filter from query |
| 58 | $id_var = (int) $pk['id_variation']; |
| 59 | unset($pk['id_variation']); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | $id_var = 0; |
| 64 | } |
| 65 | |
| 66 | // get option from parent |
| 67 | $option = parent::getItem($pk, $new); |
| 68 | |
| 69 | if (!$option) |
| 70 | { |
| 71 | // option not found |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | // register empty variations |
| 76 | $option->variations = array(); |
| 77 | |
| 78 | // do not need to load variations in case of new item |
| 79 | if ($option->id) |
| 80 | { |
| 81 | if ($id_var > 0) |
| 82 | { |
| 83 | // load only the specified variation |
| 84 | $var = JModelVAP::getInstance('optionvar')->getItem($id_var); |
| 85 | |
| 86 | if (!$var) |
| 87 | { |
| 88 | // relation not found |
| 89 | return false; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | // register only this variation |
| 94 | $option->variations[] = $var; |
| 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | $dbo = JFactory::getDbo(); |
| 100 | |
| 101 | // load all the assigned variations |
| 102 | $q = $dbo->getQuery(true) |
| 103 | ->select('v.*') |
| 104 | ->from($dbo->qn('#__vikappointments_option_value', 'v')) |
| 105 | ->where($dbo->qn('v.id_option') . ' = ' . $option->id) |
| 106 | ->order($dbo->qn('v.ordering') . ' ASC'); |
| 107 | |
| 108 | $dbo->setQuery($q); |
| 109 | $option->variations = $dbo->loadObjectList(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $option; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Basic save implementation. |
| 118 | * |
| 119 | * @param mixed $data Either an array or an object of data to save. |
| 120 | * |
| 121 | * @return mixed The ID of the record on success, false otherwise. |
| 122 | */ |
| 123 | public function save($data) |
| 124 | { |
| 125 | $data = (array) $data; |
| 126 | |
| 127 | /** |
| 128 | * In case we are saving the number of units remaining in stock, |
| 129 | * check whether the new value is higher than the previous one. |
| 130 | * If this is the case, unset the flag used to check whether a |
| 131 | * notification was already sent to the administrator. |
| 132 | * |
| 133 | * @since 1.7.7 |
| 134 | */ |
| 135 | if (!empty($data['id']) && !empty($data['units'])) |
| 136 | { |
| 137 | $table = $this->getTable(); |
| 138 | $table->load($data['id']); |
| 139 | |
| 140 | if ($table->units < $data['units']) |
| 141 | { |
| 142 | // the product can be notified again |
| 143 | $data['stock_notified'] = 0; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return parent::save($data); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Extend delete implementation to delete any related records |
| 152 | * stored within a separated table. |
| 153 | * |
| 154 | * @param mixed $ids Either the record ID or a list of records. |
| 155 | * |
| 156 | * @return boolean True on success, false otherwise. |
| 157 | */ |
| 158 | public function delete($ids) |
| 159 | { |
| 160 | // only int values are accepted |
| 161 | $ids = array_map('intval', (array) $ids); |
| 162 | |
| 163 | // invoke parent first |
| 164 | if (!parent::delete($ids)) |
| 165 | { |
| 166 | // nothing to delete |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | $dbo = JFactory::getDbo(); |
| 171 | |
| 172 | // load any assigned translation |
| 173 | $q = $dbo->getQuery(true) |
| 174 | ->select($dbo->qn('id')) |
| 175 | ->from($dbo->qn('#__vikappointments_lang_option')) |
| 176 | ->where($dbo->qn('id_option') . ' IN (' . implode(',', $ids) . ')' ); |
| 177 | |
| 178 | $dbo->setQuery($q); |
| 179 | |
| 180 | if ($lang_ids = $dbo->loadColumn()) |
| 181 | { |
| 182 | // get translation model |
| 183 | $model = JModelVAP::getInstance('langoption'); |
| 184 | // delete assigned translations |
| 185 | $model->delete($lang_ids); |
| 186 | } |
| 187 | |
| 188 | // load any option-service relation |
| 189 | $q = $dbo->getQuery(true) |
| 190 | ->select($dbo->qn('id')) |
| 191 | ->from($dbo->qn('#__vikappointments_ser_opt_assoc')) |
| 192 | ->where($dbo->qn('id_option') . ' IN (' . implode(',', $ids) . ')' ); |
| 193 | |
| 194 | $dbo->setQuery($q); |
| 195 | |
| 196 | if ($assoc_ids = $dbo->loadColumn()) |
| 197 | { |
| 198 | // get service model |
| 199 | $model = JModelVAP::getInstance('seroptassoc'); |
| 200 | // delete relations |
| 201 | $model->delete($assoc_ids); |
| 202 | } |
| 203 | |
| 204 | // load any children variations |
| 205 | $q = $dbo->getQuery(true) |
| 206 | ->select($dbo->qn('id')) |
| 207 | ->from($dbo->qn('#__vikappointments_option_value')) |
| 208 | ->where($dbo->qn('id_option') . ' IN (' . implode(',', $ids) . ')' ); |
| 209 | |
| 210 | $dbo->setQuery($q); |
| 211 | |
| 212 | if ($var_ids = $dbo->loadColumn()) |
| 213 | { |
| 214 | // get variation model |
| 215 | $model = JModelVAP::getInstance('optionvar'); |
| 216 | // delete children |
| 217 | $model->delete($var_ids); |
| 218 | } |
| 219 | |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Calculates the remaining units of a specific option. |
| 225 | * |
| 226 | * @param int $idOption The option to look for. |
| 227 | * @param int $idVariation The variation to look for, if any. |
| 228 | * @param int $idIndex The reservation item to exlcude, if any. |
| 229 | * |
| 230 | * @return int The remaining units. |
| 231 | * |
| 232 | * @since 1.7.7 |
| 233 | */ |
| 234 | public function getStock($idOption, $idVariation = null, $idIndex = null) |
| 235 | { |
| 236 | $item = null; |
| 237 | |
| 238 | if ($idVariation > 0) |
| 239 | { |
| 240 | $variation = JModelVAP::getInstance('optionvar')->getItem((int) $idVariation, $blank = true); |
| 241 | |
| 242 | if ($variation->stock) |
| 243 | { |
| 244 | // the variation of the option uses its own stock |
| 245 | $item = $variation; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // the variation inherits the stock from the parent |
| 250 | $idVariation = null; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (!$item) |
| 255 | { |
| 256 | // use the option details |
| 257 | $item = JModelVAP::getInstance('option')->getItem((int) $idOption, $blank = true); |
| 258 | } |
| 259 | |
| 260 | if (!$item->stock) |
| 261 | { |
| 262 | // stock disabled for this item |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | // count the actual number of sold units |
| 267 | $soldUnits = JModelVAP::getInstance('reservation')->countSoldOptions($idOption, $idVariation, $idIndex); |
| 268 | |
| 269 | // calculate the remaining units |
| 270 | return $item->units - $soldUnits; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Sends an e-mail notification to the administrator(s) about products with |
| 275 | * low remaining units. |
| 276 | * |
| 277 | * @param array $options An array of options. |
| 278 | * |
| 279 | * @return bool True on success, false otherwise. |
| 280 | * |
| 281 | * @since 1.7.7 |
| 282 | */ |
| 283 | public function sendEmailNotification(array $options = []) |
| 284 | { |
| 285 | VAPLoader::import('libraries.mail.factory'); |
| 286 | |
| 287 | $sent = false; |
| 288 | |
| 289 | try |
| 290 | { |
| 291 | // instantiate mail |
| 292 | $mail = VAPMailFactory::getInstance('stock', $options); |
| 293 | |
| 294 | if (!$mail->shouldSend()) |
| 295 | { |
| 296 | // configured to avoid receiving this kind of e-mails |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | // send notification |
| 301 | $sent = $mail->send(); |
| 302 | } |
| 303 | catch (Exception $e) |
| 304 | { |
| 305 | // probably order not found, register error message |
| 306 | $this->setError($e->getMessage()); |
| 307 | |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | if ($sent) |
| 312 | { |
| 313 | // e-mail sent successfully, now we should flag the processed items as notified to prevent duplicate e-mails |
| 314 | foreach ($mail->getItems() as $item) |
| 315 | { |
| 316 | // do not notify again until the administrator refills this product |
| 317 | if ($item->variationStock) |
| 318 | { |
| 319 | // the variation uses a self stock |
| 320 | JModelVAP::getInstance('optionvar')->save([ |
| 321 | 'id' => $item->variationId, |
| 322 | 'stock_notified' => 1, |
| 323 | ]); |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | // update the option |
| 328 | $this->save([ |
| 329 | 'id' => $item->optionId, |
| 330 | 'stock_notified' => 1, |
| 331 | ]); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | return $sent; |
| 337 | } |
| 338 | } |
| 339 |