PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / tax / loader.php
vikappointments / site / helpers / libraries / tax Last commit date
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
loader.php
220 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.tax');
15
16 /**
17 * Helper class used to pre-load the details of the supported taxes.
18 *
19 * @since 1.7
20 */
21 abstract class VAPTaxLoader
22 {
23 /**
24 * Cache the loaded taxes to avoid loading
25 * them more than once.
26 *
27 * @var array
28 */
29 protected static $taxes = array();
30
31 /**
32 * Loads the details of the specified tax by checking
33 * whether the same details have been already loaded.
34 *
35 * @param integer $id The tax identifier.
36 * @param string $tag The language tag to use for translations.
37 *
38 * @return object The tax details.
39 *
40 * @throws Exception
41 */
42 public static function load($id, $tag = null)
43 {
44 if (!$tag)
45 {
46 // get current language tag
47 $tag = JFactory::getLanguage()->getTag();
48 }
49
50 if (!isset(static::$taxes[$id]))
51 {
52 // create initial pool
53 static::$taxes[$id] = array();
54 }
55
56 // make sure the requested tax is not yet
57 // contained within the cache pool in the
58 // given language
59 if (!isset(static::$taxes[$id][$tag]))
60 {
61 // load and cache result
62 static::$taxes[$id][$tag] = static::_load($id, $tag);
63 }
64
65 return static::$taxes[$id][$tag];
66 }
67
68 /**
69 * Loads the details of the specified tax.
70 *
71 * @param integer $id The tax identifier.
72 * @param string $tag The language tag to use for translations.
73 *
74 * @return object The tax details.
75 *
76 * @throws Exception
77 */
78 protected static function _load($id, $tag)
79 {
80 $dbo = JFactory::getDbo();
81
82 $q = $dbo->getQuery(true);
83
84 // load tax details
85 $q->select($dbo->qn('t.id'));
86 $q->select($dbo->qn('t.name'));
87 $q->select($dbo->qn('t.description'));
88 $q->from($dbo->qn('#__vikappointments_tax', 't'));
89
90 // load linked tax rules
91 $q->select($dbo->qn('r.id', 'rule_id'));
92 $q->select($dbo->qn('r.name', 'rule_name'));
93 $q->select($dbo->qn('r.operator', 'rule_operator'));
94 $q->select($dbo->qn('r.amount', 'rule_amount'));
95 $q->select($dbo->qn('r.cap', 'rule_cap'));
96 $q->select($dbo->qn('r.apply', 'rule_apply'));
97 $q->select($dbo->qn('r.breakdown', 'rule_breakdown'));
98 $q->from($dbo->qn('#__vikappointments_tax_rule', 'r'));
99
100 // filter by given tax
101 $q->where($dbo->qn('t.id') . ' = ' . (int) $id);
102 // apply strict relation because taxes must specify at least
103 // a rule, otherwise there wouldn't be anything to calculate
104 $q->where($dbo->qn('t.id') . ' = ' . $dbo->qn('r.id_tax'));
105
106 // sort rules by the specified ordering to properly
107 // calculate the resulting taxes
108 $q->order($dbo->qn('r.ordering') . ' ASC');
109
110 /**
111 * Trigger hook to allow external plugins to manipulate the query used
112 * to load the tax details through this helper class.
113 *
114 * TIP: any column with an alias that starts with "rule_" will be
115 * automatically injected within the rule instance.
116 *
117 * @param mixed &$query A query builder object.
118 *
119 * @return void
120 *
121 * @since 1.7
122 */
123 VAPFactory::getEventDispatcher()->trigger('onBeforeQueryTax', array(&$q));
124
125 $dbo->setQuery($q);
126 $rows = $dbo->loadObjectList();
127
128 if (!$rows)
129 {
130 // no matching tax
131 throw new Exception(sprintf('Tax [%d] not found', (int) $id), 404);
132 }
133
134 $tax = array();
135
136 // iterate properties of the first record to check
137 // what should be injected within the tax object
138 foreach (get_object_vars($rows[0]) as $k => $v)
139 {
140 // inject any property that DOES NOT start with "rule_"
141 if (!preg_match("/^rule_/", $k))
142 {
143 $tax[$k] = $v;
144 }
145 }
146
147 // get translator
148 $translator = VAPFactory::getTranslator();
149
150 // create tax instance
151 $tax = new VAPTax($tax);
152
153 // translate tax details
154 $taxLang = $translator->translate('tax', $tax->get('id'), $tag);
155
156 if ($taxLang)
157 {
158 // update name with translation found
159 $tax->set('name', $taxLang->name);
160 }
161
162 $rule_ids = array();
163
164 // iterate rules
165 foreach ($rows as $row)
166 {
167 $rule = array();
168
169 // iterate properties of the current record to check
170 // what should be injected within the rule object
171 foreach (get_object_vars($row) as $k => $v)
172 {
173 // inject any property that STARTS with "rule_"
174 if (preg_match("/^rule_(.+?)$/", $k, $match))
175 {
176 // use property without "rule_"
177 $rule[end($match)] = $v;
178 }
179 }
180
181 // translate tax rule details
182 $taxRuleLang = $translator->translate('taxrule', $rule['id'], $tag);
183
184 if ($taxRuleLang)
185 {
186 // update name with translation found
187 $rule['name'] = $taxRuleLang->name;
188
189 // JSON decode breakdown translation
190 $taxRuleLang->breakdown = $taxRuleLang->breakdown ? json_decode($taxRuleLang->breakdown, true) : array();
191
192 if ($rule['breakdown'] && $taxRuleLang->breakdown)
193 {
194 // decode original breakdown
195 $rule['breakdown'] = json_decode($rule['breakdown']);
196
197 foreach ($rule['breakdown'] as $i => $bd)
198 {
199 // replace name with translation only if not empty and we didn't receive
200 // an array, which would mean that we have the default BD
201 if (!empty($taxRuleLang->breakdown[$bd->id]) && is_string($taxRuleLang->breakdown[$bd->id]))
202 {
203 $bd->name = $taxRuleLang->breakdown[$bd->id];
204 }
205
206 // update array
207 $rule['breakdown'][$i] = $bd;
208 }
209 }
210 }
211
212 // attach rule to parent tax
213 $tax->attachRule($rule);
214 }
215
216 // return created tax
217 return $tax;
218 }
219 }
220