PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.17
VikAppointments Services Booking Calendar v1.2.17
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / tax.php
vikappointments / admin / controllers Last commit date
analytics.php 5 months ago apiban.php 5 months ago apilog.php 5 months ago apiplugin.php 5 months ago apiuser.php 5 months ago backup.php 5 months ago calendar.php 5 months ago city.php 5 months ago closure.php 5 months ago configapp.php 5 months ago configcldays.php 5 months ago configcron.php 5 months ago configemp.php 5 months ago configsmsapi.php 5 months ago configuration.php 5 months ago conversion.php 5 months ago country.php 5 months ago coupon.php 5 months ago coupongroup.php 5 months ago cronjob.php 5 months ago cronjoblog.php 5 months ago customer.php 5 months ago customf.php 5 months ago dashboard.php 5 months ago emplocwdays.php 5 months ago employee.php 5 months ago emprates.php 5 months ago export.php 5 months ago exportres.php 5 months ago file.php 5 months ago findreservation.php 5 months ago group.php 5 months ago import.php 5 months ago index.html 5 months ago invoice.php 5 months ago langcustomf.php 5 months ago langemployee.php 5 months ago langgroup.php 5 months ago langmedia.php 5 months ago langoption.php 5 months ago langoptiongroup.php 5 months ago langpackage.php 5 months ago langpackgroup.php 5 months ago langpayment.php 5 months ago langservice.php 5 months ago langstatuscode.php 5 months ago langsubscr.php 5 months ago langtax.php 5 months ago location.php 5 months ago mailtext.php 5 months ago makerecurrence.php 5 months ago media.php 5 months ago multiorder.php 5 months ago option.php 5 months ago optiongroup.php 5 months ago package.php 5 months ago packgroup.php 5 months ago packorder.php 5 months ago payment.php 5 months ago rate.php 5 months ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 5 months ago restriction.php 5 months ago review.php 5 months ago service.php 5 months ago serworkday.php 5 months ago state.php 5 months ago statuscode.php 5 months ago subscription.php 5 months ago subscrorder.php 5 months ago tag.php 5 months ago tax.php 5 months ago usernote.php 5 months ago waitinglist.php 5 months ago webhook.php 5 months ago wizard.php 5 months ago
tax.php
381 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.mvc.controllers.admin');
15
16 /**
17 * VikAppointments tax controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerTax extends VAPControllerAdmin
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 */
28 public function add()
29 {
30 $app = JFactory::getApplication();
31 $user = JFactory::getUser();
32
33 // unset user state for being recovered again
34 $app->setUserState('vap.tax.data', array());
35
36 // check user permissions
37 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.taxes', 'com_vikappointments'))
38 {
39 // back to main list, not authorised to create records
40 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
41 $this->cancel();
42
43 return false;
44 }
45
46 $this->setRedirect('index.php?option=com_vikappointments&view=managetax');
47
48 return true;
49 }
50
51 /**
52 * Task used to access the management page of an existing record.
53 *
54 * @return boolean
55 */
56 public function edit()
57 {
58 $app = JFactory::getApplication();
59 $user = JFactory::getUser();
60
61 // unset user state for being recovered again
62 $app->setUserState('vap.tax.data', array());
63
64 // check user permissions
65 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.taxes', 'com_vikappointments'))
66 {
67 // back to main list, not authorised to edit records
68 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
69 $this->cancel();
70
71 return false;
72 }
73
74 $cid = $app->input->getUint('cid', array(0));
75
76 $this->setRedirect('index.php?option=com_vikappointments&view=managetax&cid[]=' . $cid[0]);
77
78 return true;
79 }
80
81 /**
82 * Task used to save the record data set in the request.
83 * After saving, the user is redirected to the main list.
84 *
85 * @return void
86 */
87 public function saveclose()
88 {
89 if ($this->save())
90 {
91 $this->cancel();
92 }
93 }
94
95 /**
96 * Task used to save the record data set in the request.
97 * After saving, the user is redirected to the creation
98 * page of a new record.
99 *
100 * @return void
101 */
102 public function savenew()
103 {
104 if ($this->save())
105 {
106 $this->setRedirect('index.php?option=com_vikappointments&task=tax.add');
107 }
108 }
109
110 /**
111 * Task used to save the record data set in the request.
112 * After saving, the user is redirected to the management
113 * page of the record that has been saved.
114 *
115 * @return boolean
116 */
117 public function save()
118 {
119 $app = JFactory::getApplication();
120 $input = $app->input;
121 $user = JFactory::getUser();
122
123 /**
124 * Added token validation.
125 *
126 * @since 1.7
127 */
128 if (!JSession::checkToken())
129 {
130 // back to main list, missing CSRF-proof token
131 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
132 $this->cancel();
133
134 return false;
135 }
136
137 $args = array();
138 $args['name'] = $input->getString('name', '');
139 $args['description'] = $input->getString('description');
140 $args['id'] = $input->getUint('id', 0);
141
142 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
143
144 // check user permissions
145 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.taxes', 'com_vikappointments'))
146 {
147 // back to main list, not authorised to create/edit records
148 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
149 $this->cancel();
150
151 return false;
152 }
153
154 // get tax model
155 $tax = $this->getModel();
156
157 // try to save arguments
158 $id = $tax->save($args);
159
160 if (!$id)
161 {
162 // get string error
163 $error = $tax->getError(null, true);
164
165 // display error message
166 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
167
168 $url = 'index.php?option=com_vikappointments&view=managetax';
169
170 if ($args['id'])
171 {
172 $url .= '&cid[]=' . $args['id'];
173 }
174
175 // redirect to new/edit page
176 $this->setRedirect($url);
177
178 return false;
179 }
180
181 // get tax rule model
182 $taxrule = $this->getModel('taxrule');
183
184 // load deleted rules
185 $rule_deleted = $input->get('rule_deleted', array(), 'uint');
186
187 // delete rules before save the other ones
188 $taxrule->delete($rule_deleted);
189
190 // load rules details
191 $rule_json = $input->get('rule_json', array(), 'array');
192
193 foreach ($rule_json as $i => $json)
194 {
195 // decode the rule data
196 $src = json_decode($json, true);
197
198 // always specify the tax ID
199 $src['id_tax'] = $id;
200 // set up the ordering
201 $src['ordering'] = $i + 1;
202
203 // attempt to save the rule
204 $taxrule->save($src);
205 }
206
207 // display generic successful message
208 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
209
210 // redirect to edit page
211 $this->setRedirect('index.php?option=com_vikappointments&task=tax.edit&cid[]=' . $id);
212
213 return true;
214 }
215
216 /**
217 * Duplicates a list of records set in the request.
218 *
219 * @return boolean
220 */
221 public function duplicate()
222 {
223 $app = JFactory::getApplication();
224 $user = JFactory::getUser();
225
226 /**
227 * Added token validation.
228 * Both GET and POST are supported.
229 *
230 * @since 1.7
231 */
232 if (!JSession::checkToken() && !JSession::checkToken('get'))
233 {
234 // back to main list, missing CSRF-proof token
235 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
236 $this->cancel();
237
238 return false;
239 }
240
241 $cid = $app->input->get('cid', array(), 'uint');
242
243 // check user permissions
244 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.taxes', 'com_vikappointments'))
245 {
246 // back to main list, not authorised to delete records
247 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
248 $this->cancel();
249
250 return false;
251 }
252
253 // duplicate selected records
254 $result = $this->getModel()->duplicate($cid);
255
256 /**
257 * @todo should we display how many records have been created?
258 */
259
260 // back to main list
261 $this->cancel();
262
263 return true;
264 }
265
266 /**
267 * Deletes a list of records set in the request.
268 *
269 * @return boolean
270 */
271 public function delete()
272 {
273 $app = JFactory::getApplication();
274 $user = JFactory::getUser();
275
276 /**
277 * Added token validation.
278 * Both GET and POST are supported.
279 *
280 * @since 1.7
281 */
282 if (!JSession::checkToken() && !JSession::checkToken('get'))
283 {
284 // back to main list, missing CSRF-proof token
285 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
286 $this->cancel();
287
288 return false;
289 }
290
291 $cid = $app->input->get('cid', array(), 'uint');
292
293 // check user permissions
294 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.taxes', 'com_vikappointments'))
295 {
296 // back to main list, not authorised to delete records
297 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
298 $this->cancel();
299
300 return false;
301 }
302
303 // delete selected records
304 $this->getModel()->delete($cid);
305
306 // back to main list
307 $this->cancel();
308
309 return true;
310 }
311
312 /**
313 * Redirects the users to the main records list.
314 *
315 * @return void
316 */
317 public function cancel()
318 {
319 $this->setRedirect('index.php?option=com_vikappointments&view=taxes');
320 }
321
322 /**
323 * AJAX end-point used to test how the taxes are applied.
324 * The task expects the following arguments to be set in request.
325 *
326 * @param integer id_tax The tax ID.
327 * @param float amount The base amount.
328 *
329 * @return void
330 */
331 function testajax()
332 {
333 $app = JFactory::getApplication();
334 $input = $app->input;
335
336 /**
337 * Added token validation.
338 *
339 * @since 1.7
340 */
341 if (!JSession::checkToken())
342 {
343 // missing CSRF-proof token
344 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
345 }
346
347 $id_tax = $input->getUint('id_tax', 0);
348 $amount = $input->getFloat('amount', 0);
349 $id_user = $input->getUint('id_user', 0);
350 $id_emp = $input->getUint('id_employee', 0);
351 $langtag = $input->getString('langtag', null);
352 $subject = $input->getString('subject', null);
353
354 // store the last search in the user state
355 $app->setUserState('vaptaxestest.id_tax', $id_tax);
356 $app->setUserState('vaptaxestest.amount', $amount);
357 $app->setUserState('vaptaxestest.langtag', $langtag);
358
359 VAPLoader::import('libraries.tax.factory');
360
361 $options = array();
362 $options['lang'] = $langtag;
363 $options['subject'] = $subject;
364
365 if ($id_user)
366 {
367 $options['id_user'] = $id_user;
368 }
369 else if ($id_emp)
370 {
371 $options['id_employee'] = $id_emp;
372 }
373
374 // calculate taxes
375 $result = VAPTaxFactory::calculate($id_tax, $amount, $options);
376
377 // send result to caller
378 $this->sendJSON($result);
379 }
380 }
381