PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / controllers / cart.php
vikappointments / site / controllers Last commit date
calendarweek.php 3 years ago cart.php 1 month ago confirmapp.php 2 years ago empattachser.php 4 years ago empeditcoupon.php 4 years ago empeditcustfield.php 4 years ago empeditlocation.php 4 years ago empeditpay.php 4 years ago empeditprofile.php 5 months ago empeditservice.php 4 years ago empeditwdays.php 4 years ago emplocwdays.php 4 years ago emplogin.php 2 years ago employeesearch.php 2 years ago employeeslist.php 4 years ago empmakerecur.php 1 month ago empmanres.php 1 month ago empsettings.php 2 years ago empsubscr.php 4 years ago empsubscrorder.php 1 year ago index.html 6 years ago modules.php 1 year ago order.php 5 months ago packages.php 4 years ago packagesconfirm.php 4 years ago packagesorder.php 1 year ago servicesearch.php 2 years ago subscriptions.php 4 years ago subscrpayment.php 1 year ago userprofile.php 5 months ago waitinglist.php 4 years ago
cart.php
409 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 cart controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerCart extends VAPControllerAdmin
22 {
23 /**
24 * Method used to add an item into the cart via AJAX.
25 *
26 * @return void
27 */
28 public function additem()
29 {
30 $app = JFactory::getApplication();
31 $input = $app->input;
32
33 $args = array();
34 $args['id_service'] = $input->getUint('id_ser', 0);
35 $args['id_employee'] = $input->getInt('id_emp', 0);
36 $args['date'] = $input->getString('date', 0);
37 $args['hour'] = $input->getUint('hour', 0);
38 $args['min'] = $input->getUint('min', 0);
39 $args['people'] = $input->getUint('people', 1);
40 $args['factor'] = $input->getUint('duration_factor', 1);
41 $args['options'] = $input->get('options', array(), 'array');
42
43 // get cart model
44 $model = $this->getModel();
45
46 // push item into the cart
47 $item = $model->addItem($args);
48
49 if (!$item)
50 {
51 // get error from model
52 $error = $model->getError($index = null, $string = true);
53 UIErrorFactory::raiseError(500, $error);
54 }
55
56 // get cart handler
57 $cart = $model->getCart();
58
59 // get the sum of total cost for each service (of this kind) in the cart
60 $group_cost = VAPCartUtils::getServiceTotalCost($cart->getItemsList(), $args['id_service']);
61
62 $result = new stdClass;
63 $result->item = $item->toArray();
64 $result->total = $cart->getTotalCost();
65 $result->totalNet = $cart->getTotalNet();
66 $result->totalTax = $cart->getTotalTax();
67 $result->totalGross = $cart->getTotalGross();
68 $result->groupCost = $group_cost;
69 $result->date = $item->getCheckinDate(JText::translate('DATE_FORMAT_LC2'), 'customer');
70 $result->totalsHtml = $this->getCartTotalsHtml($cart);
71
72 // send response to caller
73 $this->sendJSON($result);
74 }
75
76 /**
77 * Method used to add an item (with recurrence) into the cart via AJAX.
78 *
79 * @return void
80 */
81 public function addrecuritem()
82 {
83 $app = JFactory::getApplication();
84 $input = $app->input;
85
86 $args = array();
87 $args['id_service'] = $input->getUint('id_ser', 0);
88 $args['id_employee'] = $input->getInt('id_emp', 0);
89 $args['date'] = $input->getString('date', 0);
90 $args['hour'] = $input->getUint('hour', 0);
91 $args['min'] = $input->getUint('min', 0);
92 $args['people'] = $input->getUint('people', 1);
93 $args['factor'] = $input->getUint('duration_factor', 1);
94 $args['options'] = $input->get('options', array(), 'array');
95
96 // get recurrence roles
97 $rules = $input->getString('recurrence', '');
98 list($recurrence['by'], $recurrence['for'], $recurrence['amount']) = explode(',', $rules);
99
100 // get cart model
101 $model = $this->getModel();
102
103 // push item into the cart
104 $items = $model->addRecurringItem($args, $recurrence);
105
106 if (!$items)
107 {
108 // get error from model
109 $error = $model->getError($index = null, $string = true);
110 UIErrorFactory::raiseError(500, $error);
111 }
112
113 // get cart handler
114 $cart = $model->getCart();
115
116 // get the sum of total cost for each service (of this kind) in the cart
117 $group_cost = VAPCartUtils::getServiceTotalCost($cart->getItemsList(), $args['id_service']);
118
119 $count = 0;
120
121 // count the number of added items
122 foreach ($items as $item)
123 {
124 if ($item['status'])
125 {
126 $count++;
127 }
128 }
129
130 $result = new stdClass;
131 $result->total = $cart->getTotalCost();
132 $result->totalNet = $cart->getTotalNet();
133 $result->totalTax = $cart->getTotalTax();
134 $result->totalGross = $cart->getTotalGross();
135 $result->groupCost = $group_cost;
136 $result->items = $items;
137 $result->count = $count;
138 $result->totalsHtml = $this->getCartTotalsHtml($cart);
139
140 // send response to caller
141 $this->sendJSON($result);
142 }
143
144 /**
145 * Method used to remove an item from the cart via AJAX.
146 *
147 * @return void
148 */
149 public function removeitem()
150 {
151 $app = JFactory::getApplication();
152 $input = $app->input;
153
154 $args = array();
155 $args['id_service'] = $input->getUint('id_ser', 0);
156 $args['id_employee'] = $input->getInt('id_emp', 0);
157 $args['checkin'] = $input->getString('checkin', '');
158
159 // get cart model
160 $model = $this->getModel();
161
162 // remove item from the cart
163 $item = $model->removeItem($args);
164
165 if (!$item)
166 {
167 // get error from model
168 $error = $model->getError($index = null, $string = true);
169 UIErrorFactory::raiseError(500, $error);
170 }
171
172 // get cart handler
173 $cart = $model->getCart();
174
175 // get the sum of total cost for each service (of this kind) in the cart
176 $group_cost = VAPCartUtils::getServiceTotalCost($cart->getItemsList(), $args['id_service']);
177
178 $result = new stdClass;
179 $result->total = $cart->getTotalCost();
180 $result->totalNet = $cart->getTotalNet();
181 $result->totalTax = $cart->getTotalTax();
182 $result->totalGross = $cart->getTotalGross();
183 $result->groupCost = $group_cost;
184 $result->totalsHtml = $this->getCartTotalsHtml($cart);
185
186 if ($cart->isEmpty())
187 {
188 $url = 'index.php?option=com_vikappointments&view=servicesearch&id_service=' . $args['id_service'];
189
190 if ($args['id_employee'] > 0)
191 {
192 $url .= '&id_employee=' . $args['id_employee'];
193 }
194
195 $url .= '&date=' . JFactory::getDate($args['checkin'])->format('Y-m-d');
196
197 $itemid = $input->getUint('Itemid');
198
199 if ($itemid)
200 {
201 $url .= '&Itemid=' . $itemid;
202 }
203
204 // register URL to let javascript redirect the user to the
205 // details page of the last deleted service, since the cart
206 // is now empty and there's no need to stay in confirmapp
207 $result->redirect = JRoute::rewrite($url, false);
208 }
209
210 // send response to caller
211 $this->sendJSON($result);
212 }
213
214 /**
215 * Method used to add an item option into the cart via AJAX.
216 *
217 * @return void
218 */
219 public function addoption()
220 {
221 $app = JFactory::getApplication();
222 $input = $app->input;
223
224 $args = array();
225 $args['id_option'] = $input->getUint('id_opt', 0);
226 $args['id_service'] = $input->getUint('id_ser', 0);
227 $args['id_employee'] = $input->getInt('id_emp', 0);
228 $args['checkin'] = $input->getString('checkin', '');
229 $args['units'] = $input->getString('units', 1);
230
231 // get cart model
232 $model = $this->getModel();
233
234 // add option into the cart
235 $res = $model->addOption($args);
236
237 if (!$res)
238 {
239 // get error from model
240 $error = $model->getError($index = null, $string = true);
241 UIErrorFactory::raiseError(500, $error);
242 }
243
244 // get cart handler
245 $cart = $model->getCart();
246
247 // get the sum of total cost for each service (of this kind) in the cart
248 $group_cost = VAPCartUtils::getServiceTotalCost($cart->getItemsList(), $args['id_service']);
249
250 // get item details from cart
251 $index = $cart->indexOf($args['id_service'], $args['id_employee'], $args['checkin']);
252 $item = $cart->getItemAt($index);
253
254 // get option
255 $index = $item->indexOf($args['id_option']);
256 $option = $item->getOptionAt($index);
257
258 $result = new stdClass;
259 $result->itemTotal = $item->getTotalCost();
260 $result->total = $cart->getTotalCost();
261 $result->totalNet = $cart->getTotalNet();
262 $result->totalTax = $cart->getTotalTax();
263 $result->totalGross = $cart->getTotalGross();
264 $result->groupCost = $group_cost;
265 $result->quantity = $option->getQuantity();
266 $result->totalsHtml = $this->getCartTotalsHtml($cart);
267
268 // send response to caller
269 $this->sendJSON($result);
270 }
271
272 /**
273 * Method used to remove an item option from the cart via AJAX.
274 *
275 * @return void
276 */
277 public function removeoption()
278 {
279 $app = JFactory::getApplication();
280 $input = $app->input;
281
282 $args = array();
283 $args['id_option'] = $input->getUint('id_opt', 0);
284 $args['id_service'] = $input->getUint('id_ser', 0);
285 $args['id_employee'] = $input->getInt('id_emp', 0);
286 $args['checkin'] = $input->getString('checkin', '');
287 $args['units'] = $input->getString('units', 1);
288
289 // get cart model
290 $model = $this->getModel();
291
292 // remove option from the cart
293 $res = $model->removeOption($args);
294
295 if (!$res)
296 {
297 // get error from model
298 $error = $model->getError($index = null, $string = true);
299 UIErrorFactory::raiseError(500, $error);
300 }
301
302 // get cart handler
303 $cart = $model->getCart();
304
305 // get the sum of total cost for each service (of this kind) in the cart
306 $group_cost = VAPCartUtils::getServiceTotalCost($cart->getItemsList(), $args['id_service']);
307
308 // get item details from cart
309 $index = $cart->indexOf($args['id_service'], $args['id_employee'], $args['checkin']);
310 $item = $cart->getItemAt($index);
311
312 if (($index = $item->indexOf($args['id_option'])) == -1)
313 {
314 // option not found
315 $qty = 0;
316 }
317 else
318 {
319 // get remaining quantity
320 $qty = $item->getOptionAt($index)->getQuantity();
321 }
322
323 $result = new stdClass;
324 $result->itemTotal = $item->getTotalCost();
325 $result->total = $cart->getTotalCost();
326 $result->totalNet = $cart->getTotalNet();
327 $result->totalTax = $cart->getTotalTax();
328 $result->totalGross = $cart->getTotalGross();
329 $result->groupCost = $group_cost;
330 $result->quantity = $qty;
331 $result->totalsHtml = $this->getCartTotalsHtml($cart);
332
333 // send response to caller
334 $this->sendJSON($result);
335 }
336
337 /**
338 * AJAX task to empty the appointments cart.
339 *
340 * @return void
341 */
342 public function flush()
343 {
344 // flush the cart
345 $this->getModel()->emptyCart();
346
347 // send response to caller
348 $this->sendJSON(1);
349 }
350
351 /**
352 * End-point used to redeem the coupon code.
353 *
354 * @return void
355 *
356 * @since 1.7.3
357 */
358 public function redeemcoupon()
359 {
360 $app = JFactory::getApplication();
361 $input = $app->input;
362
363 // validate form token to prevent brute force attacks
364 if (!JSession::checkToken())
365 {
366 // direct access attempt
367 UIErrorFactory::raiseError(500, JText::translate('JINVALID_TOKEN'));
368 }
369
370 // get coupon key from POST
371 $coupon = $input->post->getString('coupon', '');
372
373 // get cart model
374 $model = $this->getModel();
375
376 // try to redeem the coupon code
377 $res = $model->redeemCoupon($coupon);
378
379 if (!$res)
380 {
381 // get last error registered by the model
382 $error = $model->getError($index = null, $string = true);
383 // propagate error or use the default one
384 UIErrorFactory::raiseError(500, $error ? $error : JText::translate('VAPCOUPONNOTVALID'));
385 }
386
387 $cart = $model->getCart();
388
389 $result = new stdClass;
390 $result->cart = $cart;
391 $result->totalsHtml = $this->getCartTotalsHtml($cart);
392
393 // send response to caller
394 $this->sendJSON($result);
395 }
396
397 /**
398 * Fetches the HTML block displaying the cart totals.
399 *
400 * @param VAPCart $cart
401 *
402 * @return string
403 */
404 protected function getCartTotalsHtml($cart)
405 {
406 return JLayoutHelper::render('blocks.carttotals', array('cart' => $cart));
407 }
408 }
409