rules
6 days ago
factory.php
6 days ago
index.html
6 days ago
loader.php
6 days ago
rule.php
6 days ago
tax.php
6 days ago
tax.php
125 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.rule'); |
| 15 | |
| 16 | /** |
| 17 | * Encapsulates the details and the rules of a tax. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VAPTax extends JObject |
| 22 | { |
| 23 | /** |
| 24 | * Internal property used to handle the rules, |
| 25 | * which cannot be altered from outside. |
| 26 | * |
| 27 | * @var array |
| 28 | */ |
| 29 | protected $_rules = array(); |
| 30 | |
| 31 | /** |
| 32 | * Class constructor. |
| 33 | * |
| 34 | * @param mixed $data Either and associative array or another |
| 35 | * object to set the initial properties of the object. |
| 36 | */ |
| 37 | public function __construct($data = array()) |
| 38 | { |
| 39 | // invoke parent to construct object |
| 40 | parent::__construct($data); |
| 41 | |
| 42 | // get loaded rules, if any |
| 43 | $rules = $this->get('rules', null); |
| 44 | |
| 45 | if ($rules) |
| 46 | { |
| 47 | // properly import them with dedicated method |
| 48 | foreach ($rules as $rule) |
| 49 | { |
| 50 | // attach new rule |
| 51 | $this->attachRule($rule); |
| 52 | } |
| 53 | |
| 54 | // clear public property |
| 55 | $this->set('rules', null); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Proxy to access the internal rules. |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function getRules() |
| 65 | { |
| 66 | return $this->_rules; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Attaches a calculation rule to this object. |
| 71 | * |
| 72 | * @param mixed $rule Either a rule instance or an array/object. |
| 73 | * |
| 74 | * @return self This object to support chaining. |
| 75 | */ |
| 76 | public function attachRule($rule) |
| 77 | { |
| 78 | if (!$rule instanceof VAPTaxRule) |
| 79 | { |
| 80 | // create new instance |
| 81 | $rule = VAPTaxRule::getInstance($rule); |
| 82 | } |
| 83 | |
| 84 | // register rule |
| 85 | $this->_rules[] = $rule; |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Calculates the taxes for the specified amount. |
| 92 | * |
| 93 | * @param float $total The total amount to use. |
| 94 | * @param array $options An array of options. |
| 95 | * |
| 96 | * @return object An object containing the resulting taxes. |
| 97 | */ |
| 98 | public function calculate($total, array $options = array()) |
| 99 | { |
| 100 | // prepare object to return |
| 101 | $result = new stdClass; |
| 102 | $result->net = (float) $total; |
| 103 | $result->tax = 0; |
| 104 | $result->gross = $result->net; |
| 105 | $result->breakdown = array(); |
| 106 | |
| 107 | // calculate costs only in case the given amount |
| 108 | // is higher than 0, because it doesn't make sense |
| 109 | // to calculate the taxes on a discount or for a |
| 110 | // free item |
| 111 | if ($result->net > 0) |
| 112 | { |
| 113 | // iterate supported rules |
| 114 | foreach ($this->getRules() as $rule) |
| 115 | { |
| 116 | // let the rule manipulate the properties |
| 117 | // of the resulting object |
| 118 | $rule->calculate($total, $result, $options); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $result; |
| 123 | } |
| 124 | } |
| 125 |