PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.6
VikBooking Hotel Booking Engine & PMS v1.6.6
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / src / taxonomy / summary.php

summary.php in VikBooking Hotel Booking Engine & PMS 1.6.6, at admin/helpers/src/taxonomy/summary.php

240 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 rate assigned to the given tax ID.
132 *
133 * @param int $tax_id the tax ID.
134 *
135 * @return array float|int (tax rate) and string (tax name).
136 */
137 public static function getTaxRateFromId($tax_id)
138 {
139 if (empty($tax_id)) {
140 return [0, null];
141 }
142
143 $dbo = JFactory::getDbo();
144
145 $q = "SELECT `t`.`name`, `t`.`aliq` FROM `#__vikbooking_iva` AS `t` WHERE `t`.`id`=" . (int)$tax_id;
146 $dbo->setQuery($q, 0, 1);
147 $record = $dbo->loadAssoc();
148 if (!$record) {
149 return [0, null];
150 }
151
152 return [(float)$record['aliq'], $record['name']];
153 }
154
155 /**
156 * Returns the whole tax rate record from the given ID.
157 *
158 * @param int $tax_id the tax record ID.
159 *
160 * @return array
161 *
162 * @since 1.16.1 (J) - 1.6.1 (WP)
163 */
164 public static function getTaxRateRecord($tax_id)
165 {
166 static $tax_map = [];
167
168 $tax_id = (int)$tax_id;
169
170 if (isset($tax_map[$tax_id])) {
171 return $tax_map[$tax_id];
172 }
173
174 $dbo = JFactory::getDbo();
175
176 $q = "SELECT * FROM `#__vikbooking_iva` WHERE `id`=" . $tax_id;
177 $dbo->setQuery($q, 0, 1);
178 $record = $dbo->loadAssoc();
179
180 if (!$record) {
181 $record = [];
182 }
183
184 // cache value and return it
185 $tax_map[$tax_id] = $record;
186
187 return $record;
188 }
189
190 /**
191 * Gets the tax summary associative array.
192 *
193 * @param bool $sort if true, the array will be sorted by key desc.
194 *
195 * @return array
196 */
197 public static function get($sort = true)
198 {
199 if ($sort) {
200 krsort(static::$summary);
201 }
202
203 return static::$summary;
204 }
205
206 /**
207 * Gets the tax names associative array.
208 *
209 * @return array
210 */
211 public static function getNames()
212 {
213 return static::$tax_names;
214 }
215
216 /**
217 * Sets the tax summary associative array.
218 *
219 * @param array $summary
220 *
221 * @return void
222 */
223 public static function set(array $summary = [])
224 {
225 static::$summary = $summary;
226 }
227
228 /**
229 * Sets the tax names associative array.
230 *
231 * @param array $names
232 *
233 * @return void
234 */
235 public static function setNames(array $names = [])
236 {
237 static::$tax_names = $names;
238 }
239 }
240