rules
4 days ago
factory.php
4 days ago
index.html
4 days ago
loader.php
4 days ago
rule.php
4 days ago
tax.php
4 days ago
rule.php
136 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 | /** |
| 15 | * Tax rule calculator. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | class VAPTaxRule extends JObject |
| 20 | { |
| 21 | /** |
| 22 | * Creates an instance for the specified rule operator. |
| 23 | * |
| 24 | * @param mixed $data Either and associative array or another |
| 25 | * object to set the initial properties of the object. |
| 26 | */ |
| 27 | public static function getInstance($data) |
| 28 | { |
| 29 | $data = (array) $data; |
| 30 | |
| 31 | // make sure the operator is set |
| 32 | if (!isset($data['operator'])) |
| 33 | { |
| 34 | // raise an error because the tax rule is not applicable |
| 35 | throw new Exception('Tax rule did not specify an operator', 400); |
| 36 | } |
| 37 | |
| 38 | // try to load specific rule |
| 39 | VAPLoader::import('libraries.tax.rules.' . $data['operator']); |
| 40 | |
| 41 | // create class name |
| 42 | $classname = 'VAPTaxRule' . ucfirst($data['operator']); |
| 43 | |
| 44 | if (!class_exists($classname)) |
| 45 | { |
| 46 | // no specific rule found, use the default one to |
| 47 | // let external plugins implement their own operations |
| 48 | $classname = 'VAPTaxRule'; |
| 49 | } |
| 50 | |
| 51 | // create new rule instance |
| 52 | return new $classname($data); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Calculates the taxes for the specified amount. |
| 57 | * |
| 58 | * @param float $total The total amount to use. |
| 59 | * @param object $data An object containing the tax details. |
| 60 | * @param array $options An array of options. |
| 61 | * |
| 62 | * @return void |
| 63 | */ |
| 64 | public function calculate($total, $data, array $options = array()) |
| 65 | { |
| 66 | /** |
| 67 | * Default rule triggers a hook to let plugins apply the taxes |
| 68 | * by using custom rules. |
| 69 | * |
| 70 | * @param VAPTaxRule $rule The rule instance. |
| 71 | * @param float $total The total amount to use. |
| 72 | * @param object $data An object containing the tax details. |
| 73 | * @param array $options An array of options. |
| 74 | * |
| 75 | * @return void |
| 76 | * |
| 77 | * @since 1.7 |
| 78 | */ |
| 79 | VAPFactory::getEventDispatcher()->trigger('onCalculateTax', array($this, $total, $data, $options)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Split the taxes according to the configuration |
| 84 | * of the rule breakdowns. |
| 85 | * |
| 86 | * @param float $tax The resulting taxes. |
| 87 | * @param array &$list A list of breakdowns. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public function addBreakdown($tax, array &$list = array()) |
| 92 | { |
| 93 | // get breakdown configuration |
| 94 | $breakdown = $this->get('breakdown', null); |
| 95 | |
| 96 | if (is_string($breakdown)) |
| 97 | { |
| 98 | // attempt to decode breakdowns list |
| 99 | $breakdown = json_decode($breakdown); |
| 100 | } |
| 101 | |
| 102 | if ($breakdown) |
| 103 | { |
| 104 | $total_split = 0; |
| 105 | |
| 106 | // calculate the total sum defined by the breakdowns so |
| 107 | // that we can proportionally split the taxes according |
| 108 | // to their percentages |
| 109 | foreach ($breakdown as $bd) |
| 110 | { |
| 111 | $total_split += $bd->amount; |
| 112 | } |
| 113 | |
| 114 | // split taxes found |
| 115 | foreach ($breakdown as $bd) |
| 116 | { |
| 117 | $tmp = new stdClass; |
| 118 | // recalculate amount proportionally |
| 119 | $tmp->tax = round($tax * $bd->amount / $total_split, 2, PHP_ROUND_HALF_UP); |
| 120 | $tmp->name = $bd->name; |
| 121 | |
| 122 | $list[] = $tmp; |
| 123 | } |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | // use current rule as breakdown |
| 128 | $tmp = new stdClass; |
| 129 | $tmp->tax = $tax; |
| 130 | $tmp->name = $this->get('name', ''); |
| 131 | |
| 132 | $list[] = $tmp; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |