| @@ -1,0 +1,268 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage core | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2022 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 | + * Taxonomy summary class. | |
| 16 | + * | |
| 17 | + * @since 1.16.0 (J) - 1.6.0 (WP) | |
| 18 | + */ | |
| 19 | +class VBOTaxonomySummary | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The tax summary associative array. | |
| 23 | + * | |
| 24 | + * @var array | |
| 25 | + */ | |
| 26 | + protected static $summary = []; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The tax names associative array. | |
| 30 | + * | |
| 31 | + * @var array | |
| 32 | + */ | |
| 33 | + protected static $tax_names = []; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Starts a new summary array. | |
| 37 | + * | |
| 38 | + * @return array | |
| 39 | + */ | |
| 40 | + public static function start() | |
| 41 | + { | |
| 42 | + static::$summary = []; | |
| 43 | + | |
| 44 | + return []; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Adds a tax amount to a tax rate. | |
| 49 | + * | |
| 50 | + * @param float $rate the tax aliquot (rate). | |
| 51 | + * @param float|int $amount the amount of taxes. | |
| 52 | + * @param string $name the name of the tax. | |
| 53 | + * | |
| 54 | + * @return void | |
| 55 | + */ | |
| 56 | + public static function addTax($rate, $amount = 0, $name = null) | |
| 57 | + { | |
| 58 | + $rate = (float)$rate; | |
| 59 | + | |
| 60 | + if (!isset(static::$summary[$rate])) { | |
| 61 | + static::$summary[$rate] = 0; | |
| 62 | + } | |
| 63 | + | |
| 64 | + static::$summary[$rate] += $amount; | |
| 65 | + | |
| 66 | + if (!empty($name)) { | |
| 67 | + static::$tax_names[$rate] = $name; | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Adds a tax amount to a tax rate from a given rate plan ID. | |
| 73 | + * | |
| 74 | + * @param int $ratep_id the rate plan ID. | |
| 75 | + * @param float|int $amount the amount of taxes. | |
| 76 | + * | |
| 77 | + * @return float|int the tax rate applied. | |
| 78 | + */ | |
| 79 | + public static function addRatePlanTax($ratep_id, $amount = 0) | |
| 80 | + { | |
| 81 | + list($rate, $name) = static::getTaxRatePlan($ratep_id); | |
| 82 | + | |
| 83 | + static::addTax($rate, $amount, $name); | |
| 84 | + | |
| 85 | + return $rate; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Adds a tax amount to a tax rate from a given tax ID. | |
| 90 | + * | |
| 91 | + * @param int $tax_id the tax ID. | |
| 92 | + * @param float|int $amount the amount of taxes. | |
| 93 | + * | |
| 94 | + * @return float|int the tax rate applied. | |
| 95 | + */ | |
| 96 | + public static function addOptionTax($tax_id, $amount = 0) | |
| 97 | + { | |
| 98 | + list($rate, $name) = static::getTaxRateFromId($tax_id); | |
| 99 | + | |
| 100 | + static::addTax($rate, $amount, $name); | |
| 101 | + | |
| 102 | + return $rate; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Gets the tax rate assigned to the given rate plan ID. | |
| 107 | + * | |
| 108 | + * @param int $ratep_id the rate plan ID. | |
| 109 | + * | |
| 110 | + * @return array float|int (tax rate) and string (tax name). | |
| 111 | + */ | |
| 112 | + public static function getTaxRatePlan($ratep_id) | |
| 113 | + { | |
| 114 | + if (empty($ratep_id)) { | |
| 115 | + return [0, null]; | |
| 116 | + } | |
| 117 | + | |
| 118 | + $dbo = JFactory::getDbo(); | |
| 119 | + | |
| 120 | + $q = "SELECT `p`.`idiva`, `t`.`name`, `t`.`aliq` FROM `#__vikbooking_prices` AS `p` LEFT JOIN `#__vikbooking_iva` AS `t` ON `p`.`idiva`=`t`.`id` WHERE `p`.`id`=" . (int)$ratep_id; | |
| 121 | + $dbo->setQuery($q, 0, 1); | |
| 122 | + $record = $dbo->loadAssoc(); | |
| 123 | + if (!$record) { | |
| 124 | + return [0, null]; | |
| 125 | + } | |
| 126 | + | |
| 127 | + return [(float)$record['aliq'], $record['name']]; | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Gets the tax record data assigned to the given rate plan ID. | |
| 132 | + * | |
| 133 | + * @param int $ratep_id The rate plan ID. | |
| 134 | + * | |
| 135 | + * @return array Empty array or tax record found. | |
| 136 | + * | |
| 137 | + * @since 1.18.8 (J) - 1.8.8 (WP) | |
| 138 | + */ | |
| 139 | + public static function getTaxRecordFromRatePlan(int $ratep_id) | |
| 140 | + { | |
| 141 | + $dbo = JFactory::getDbo(); | |
| 142 | + | |
| 143 | + $dbo->setQuery( | |
| 144 | + $dbo->getQuery(true) | |
| 145 | + ->select([ | |
| 146 | + $dbo->qn('p.id'), | |
| 147 | + $dbo->qn('p.idiva'), | |
| 148 | + $dbo->qn('t.name'), | |
| 149 | + $dbo->qn('t.aliq'), | |
| 150 | + $dbo->qn('t.taxcap'), | |
| 151 | + ]) | |
| 152 | + ->from($dbo->qn('#__vikbooking_prices', 'p')) | |
| 153 | + ->leftJoin($dbo->qn('#__vikbooking_iva', 't') . ' ON ' . $dbo->qn('p.idiva') . ' = ' . $dbo->qn('t.id')) | |
| 154 | + ->where($dbo->qn('p.id') . ' = ' . $ratep_id) | |
| 155 | + ); | |
| 156 | + | |
| 157 | + return $dbo->loadAssoc() ?: []; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Gets the tax rate assigned to the given tax ID. | |
| 162 | + * | |
| 163 | + * @param int $tax_id the tax ID. | |
| 164 | + * | |
| 165 | + * @return array float|int (tax rate) and string (tax name). | |
| 166 | + */ | |
| 167 | + public static function getTaxRateFromId($tax_id) | |
| 168 | + { | |
| 169 | + if (empty($tax_id)) { | |
| 170 | + return [0, null]; | |
| 171 | + } | |
| 172 | + | |
| 173 | + $dbo = JFactory::getDbo(); | |
| 174 | + | |
| 175 | + $q = "SELECT `t`.`name`, `t`.`aliq` FROM `#__vikbooking_iva` AS `t` WHERE `t`.`id`=" . (int)$tax_id; | |
| 176 | + $dbo->setQuery($q, 0, 1); | |
| 177 | + $record = $dbo->loadAssoc(); | |
| 178 | + if (!$record) { | |
| 179 | + return [0, null]; | |
| 180 | + } | |
| 181 | + | |
| 182 | + return [(float)$record['aliq'], $record['name']]; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Returns the whole tax rate record from the given ID. | |
| 187 | + * | |
| 188 | + * @param ?int $tax_id The tax record ID. | |
| 189 | + * | |
| 190 | + * @return array | |
| 191 | + * | |
| 192 | + * @since 1.18.8 (J) - 1.8.8 (WP) argument is nullable. | |
| 193 | + */ | |
| 194 | + public static function getTaxRateRecord(?int $tax_id) | |
| 195 | + { | |
| 196 | + static $tax_map = []; | |
| 197 | + | |
| 198 | + $tax_id = (int) $tax_id; | |
| 199 | + | |
| 200 | + if (isset($tax_map[$tax_id])) { | |
| 201 | + return $tax_map[$tax_id]; | |
| 202 | + } | |
| 203 | + | |
| 204 | + if (empty($tax_id)) { | |
| 205 | + return []; | |
| 206 | + } | |
| 207 | + | |
| 208 | + $dbo = JFactory::getDbo(); | |
| 209 | + | |
| 210 | + $q = "SELECT * FROM `#__vikbooking_iva` WHERE `id`=" . $tax_id; | |
| 211 | + $dbo->setQuery($q, 0, 1); | |
| 212 | + | |
| 213 | + // cache value and return it | |
| 214 | + $tax_map[$tax_id] = (array) $dbo->loadAssoc(); | |
| 215 | + | |
| 216 | + return $tax_map[$tax_id]; | |
| 217 | + } | |
| 218 | + | |
| 219 | + /** | |
| 220 | + * Gets the tax summary associative array. | |
| 221 | + * | |
| 222 | + * @param bool $sort if true, the array will be sorted by key desc. | |
| 223 | + * | |
| 224 | + * @return array | |
| 225 | + */ | |
| 226 | + public static function get($sort = true) | |
| 227 | + { | |
| 228 | + if ($sort) { | |
| 229 | + krsort(static::$summary); | |
| 230 | + } | |
| 231 | + | |
| 232 | + return static::$summary; | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Gets the tax names associative array. | |
| 237 | + * | |
| 238 | + * @return array | |
| 239 | + */ | |
| 240 | + public static function getNames() | |
| 241 | + { | |
| 242 | + return static::$tax_names; | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Sets the tax summary associative array. | |
| 247 | + * | |
| 248 | + * @param array $summary | |
| 249 | + * | |
| 250 | + * @return void | |
| 251 | + */ | |
| 252 | + public static function set(array $summary = []) | |
| 253 | + { | |
| 254 | + static::$summary = $summary; | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Sets the tax names associative array. | |
| 259 | + * | |
| 260 | + * @param array $names | |
| 261 | + * | |
| 262 | + * @return void | |
| 263 | + */ | |
| 264 | + public static function setNames(array $names = []) | |
| 265 | + { | |
| 266 | + static::$tax_names = $names; | |
| 267 | + } | |
| 268 | +} | |