item.php
451 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.tax.factory'); |
| 15 | |
| 16 | /** |
| 17 | * Class used to handle the packages that can be stored within a cart. |
| 18 | * |
| 19 | * @since 1.6 |
| 20 | */ |
| 21 | class VAPCartPackagesItem implements JsonSerializable |
| 22 | { |
| 23 | /** |
| 24 | * The package identifier. |
| 25 | * |
| 26 | * @var integer |
| 27 | */ |
| 28 | private $id; |
| 29 | |
| 30 | /** |
| 31 | * The package name. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | private $name; |
| 36 | |
| 37 | /** |
| 38 | * The package cost. |
| 39 | * |
| 40 | * @var float |
| 41 | */ |
| 42 | private $price; |
| 43 | |
| 44 | /** |
| 45 | * The number of appointments that can be redeemed with |
| 46 | * a single unit of this package. |
| 47 | * |
| 48 | * @var integer |
| 49 | */ |
| 50 | private $numapp; |
| 51 | |
| 52 | /** |
| 53 | * The number of selected units. |
| 54 | * |
| 55 | * @var integer |
| 56 | */ |
| 57 | private $quantity; |
| 58 | |
| 59 | /** |
| 60 | * An object holding the item totals. |
| 61 | * |
| 62 | * @var object |
| 63 | * @since 1.7 |
| 64 | */ |
| 65 | private $totals; |
| 66 | |
| 67 | /** |
| 68 | * Class constructor. |
| 69 | * |
| 70 | * @param integer $id The package ID. |
| 71 | * @param string $name The package name. |
| 72 | * @param float $price The package cost. |
| 73 | * @param integer $numapp The appointments to redeem. |
| 74 | * @param integer $quantity The number of units. |
| 75 | */ |
| 76 | public function __construct($id, $name, $price, $numapp, $quantity = 1) |
| 77 | { |
| 78 | $this->id = $id; |
| 79 | $this->name = $name; |
| 80 | $this->numapp = $numapp; |
| 81 | $this->quantity = $quantity; |
| 82 | |
| 83 | // calculate totals |
| 84 | $this->setPrice($price); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the package identifier. |
| 89 | * |
| 90 | * @return integer |
| 91 | */ |
| 92 | public function getID() |
| 93 | { |
| 94 | return $this->id; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the package name. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function getName() |
| 103 | { |
| 104 | /** |
| 105 | * Translate package name at runtime. |
| 106 | * |
| 107 | * @since 1.7 |
| 108 | */ |
| 109 | $translator = VAPFactory::getTranslator(); |
| 110 | // translate the specified package |
| 111 | $tx = $translator->translate('package', $this->getID()); |
| 112 | |
| 113 | if ($tx) |
| 114 | { |
| 115 | // use the specified translation |
| 116 | $name = $tx->name; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | // use the default service name |
| 121 | $name = $this->name; |
| 122 | } |
| 123 | |
| 124 | return $name; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the package base price. |
| 129 | * |
| 130 | * @return float |
| 131 | */ |
| 132 | public function getPrice() |
| 133 | { |
| 134 | return (float) $this->price; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns the resulting price after applying the discounts. |
| 139 | * |
| 140 | * @param mixed $cart When specified, the system will try to apply |
| 141 | * the discounts to the base price. |
| 142 | * @param array &$lookup A lookup used to track the applied discounts. |
| 143 | * |
| 144 | * @return float |
| 145 | * |
| 146 | * @since 1.7 |
| 147 | */ |
| 148 | public function getDiscountedPrice($cart = null, &$lookup = array()) |
| 149 | { |
| 150 | $price = $this->getTotalCost(); |
| 151 | |
| 152 | // in case the cart was specified, check whether there are some |
| 153 | // discounts to apply |
| 154 | if (!$cart || !$cart->getDiscounts()) |
| 155 | { |
| 156 | // nope, use default price |
| 157 | return $price; |
| 158 | } |
| 159 | |
| 160 | $base = $price; |
| 161 | |
| 162 | foreach ($cart->getDiscounts() as $discount) |
| 163 | { |
| 164 | $old = $price; |
| 165 | |
| 166 | // apply discount on cascade |
| 167 | $price = $discount->apply($price, $base, $this); |
| 168 | |
| 169 | if (!isset($lookup[$discount->getID()])) |
| 170 | { |
| 171 | // create discount repository |
| 172 | $lookup[$discount->getID()] = 0; |
| 173 | } |
| 174 | |
| 175 | // increase repo by subtracting the price after the discount |
| 176 | // from the price before the discount |
| 177 | $lookup[$discount->getID()] += $old - $price; |
| 178 | } |
| 179 | |
| 180 | // make sure the price is not lower than 0 |
| 181 | return max(array(0, $price)); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Sets the package base price. |
| 186 | * |
| 187 | * @param float $price The package base cost. |
| 188 | * |
| 189 | * @return self This object to support chaining. |
| 190 | * |
| 191 | * @since 1.6.6 |
| 192 | */ |
| 193 | public function setPrice($price) |
| 194 | { |
| 195 | $this->price = (float) $price; |
| 196 | |
| 197 | $options = array(); |
| 198 | $options['subject'] = 'package'; |
| 199 | // $options['id_user'] = JFactory::getUser()->id; |
| 200 | |
| 201 | // calculate taxes |
| 202 | $this->totals = VAPTaxFactory::calculate($this->getID(), $this->getTotalCost(), $options); |
| 203 | |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns the package total cost. |
| 209 | * |
| 210 | * @return integer |
| 211 | * |
| 212 | * @uses getPrice() |
| 213 | * @uses getQuantity() |
| 214 | */ |
| 215 | public function getTotalCost() |
| 216 | { |
| 217 | return $this->getPrice() * $this->getQuantity(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Returns the total net of the package. |
| 222 | * |
| 223 | * @param mixed $cart When specified, the system will try to apply |
| 224 | * the discounts to the base price. |
| 225 | * |
| 226 | * @return float |
| 227 | * |
| 228 | * @since 1.7 |
| 229 | */ |
| 230 | public function getTotalNet($cart = null) |
| 231 | { |
| 232 | // get discounted price |
| 233 | $price = $this->getDiscountedPrice($cart); |
| 234 | |
| 235 | if ($price == $this->getTotalCost()) |
| 236 | { |
| 237 | // no discount, use default total net |
| 238 | return $this->totals->net; |
| 239 | } |
| 240 | |
| 241 | $options = array(); |
| 242 | $options['subject'] = 'package'; |
| 243 | // $options['id_user'] = JFactory::getUser()->id; |
| 244 | |
| 245 | // re-calculate net of discounted item |
| 246 | return VAPTaxFactory::calculate($this->getID(), $price, $options)->net; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Returns the total taxes of the package. |
| 251 | * |
| 252 | * @param mixed $cart When specified, the system will try to apply |
| 253 | * the discounts to the base price. |
| 254 | * |
| 255 | * @return float |
| 256 | * |
| 257 | * @since 1.7 |
| 258 | */ |
| 259 | public function getTotalTax($cart = null) |
| 260 | { |
| 261 | // get discounted price |
| 262 | $price = $this->getDiscountedPrice($cart); |
| 263 | |
| 264 | if ($price == $this->getTotalCost()) |
| 265 | { |
| 266 | // no discount, use default total tax |
| 267 | return $this->totals->tax; |
| 268 | } |
| 269 | |
| 270 | $options = array(); |
| 271 | $options['subject'] = 'package'; |
| 272 | // $options['id_user'] = JFactory::getUser()->id; |
| 273 | |
| 274 | // re-calculate taxes of discounted item |
| 275 | return VAPTaxFactory::calculate($this->getID(), $price, $options)->tax; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns the total gross of the package. |
| 280 | * |
| 281 | * @param mixed $cart When specified, the system will try to apply |
| 282 | * the discounts to the base price. |
| 283 | * |
| 284 | * @return float |
| 285 | * |
| 286 | * @since 1.7 |
| 287 | */ |
| 288 | public function getTotalGross($cart = null) |
| 289 | { |
| 290 | // get discounted price |
| 291 | $price = $this->getDiscountedPrice($cart); |
| 292 | |
| 293 | if ($price == $this->getTotalCost()) |
| 294 | { |
| 295 | // no discount, use default total gross |
| 296 | return $this->totals->gross; |
| 297 | } |
| 298 | |
| 299 | $options = array(); |
| 300 | $options['subject'] = 'package'; |
| 301 | // $options['id_user'] = JFactory::getUser()->id; |
| 302 | |
| 303 | // re-calculate gross of discounted item |
| 304 | return VAPTaxFactory::calculate($this->getID(), $price, $options)->gross; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Returns the number of appointments that can be redeemed |
| 309 | * with a single unit of this package. |
| 310 | * |
| 311 | * @return integer |
| 312 | */ |
| 313 | public function getNumberAppointments() |
| 314 | { |
| 315 | return $this->numapp; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Returns the number of selected units. |
| 320 | * |
| 321 | * @return integer |
| 322 | */ |
| 323 | public function getQuantity() |
| 324 | { |
| 325 | return $this->quantity; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Checks if this item is active or not. |
| 330 | * |
| 331 | * @return boolean |
| 332 | * |
| 333 | * @deprecated 1.8 Without replacement. |
| 334 | */ |
| 335 | public function isActive() |
| 336 | { |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Marks this item as active. |
| 342 | * |
| 343 | * @return self This object to support chaining. |
| 344 | * |
| 345 | * @deprecated 1.8 Without replacement. |
| 346 | */ |
| 347 | public function active() |
| 348 | { |
| 349 | return $this; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Marks this item as unactive. |
| 354 | * |
| 355 | * @return self This object to support chaining. |
| 356 | * |
| 357 | * @deprecated 1.8 Without replacement. |
| 358 | */ |
| 359 | public function remove() |
| 360 | { |
| 361 | return $this; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Increases the quantity of this package by the specified units. |
| 366 | * |
| 367 | * @param integer $unit The units to add. |
| 368 | * |
| 369 | * @return self This object to support chaining. |
| 370 | */ |
| 371 | public function addQuantity($unit = 1) |
| 372 | { |
| 373 | // increase quantity by the specified units |
| 374 | $this->quantity += abs($unit); |
| 375 | |
| 376 | // refresh taxes |
| 377 | $this->setPrice($this->getPrice()); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Decreases the quantity of this package by the specified units. |
| 382 | * |
| 383 | * @param integer $unit The units to remove. |
| 384 | * |
| 385 | * @return self This object to support chaining. |
| 386 | */ |
| 387 | public function removeQuantity($unit = 1) |
| 388 | { |
| 389 | // decrease units |
| 390 | $this->quantity -= abs($unit); |
| 391 | |
| 392 | if ($this->quantity <= 0) |
| 393 | { |
| 394 | // permanently removed |
| 395 | $this->quantity = 0; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | // refresh taxes |
| 400 | $this->setPrice($this->getPrice()); |
| 401 | } |
| 402 | |
| 403 | return $this->quantity; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Magic method used to return a string representation of this instance. |
| 408 | * |
| 409 | * @return string |
| 410 | */ |
| 411 | public function __tostring() |
| 412 | { |
| 413 | return '<pre>' . print_r($this, true) . '</pre>'; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Returns an array containing the details of this instance. |
| 418 | * |
| 419 | * @return array |
| 420 | */ |
| 421 | public function toArray() |
| 422 | { |
| 423 | $arr = array( |
| 424 | 'id' => $this->getID(), |
| 425 | 'name' => $this->getName(), |
| 426 | 'price' => $this->getPrice(), |
| 427 | 'total' => $this->getTotalCost(), |
| 428 | 'numapp' => $this->getNumberAppointments(), |
| 429 | 'quantity' => $this->getQuantity(), |
| 430 | ); |
| 431 | |
| 432 | return $arr; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Creates a standard object, containing all the supported properties, |
| 437 | * to be used when this class is passed to "json_encode()". |
| 438 | * |
| 439 | * @return object |
| 440 | * |
| 441 | * @since 1.7 |
| 442 | * |
| 443 | * @see JsonSerializable |
| 444 | */ |
| 445 | #[ReturnTypeWillChange] |
| 446 | public function jsonSerialize() |
| 447 | { |
| 448 | return $this->toArray(); |
| 449 | } |
| 450 | } |
| 451 |