PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / packorder.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 1 year ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 5 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 5 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 5 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
packorder.php
382 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 package order controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerPackorder 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.packorder.data', array());
35
36 // check user permissions
37 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
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=managepackorder');
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.packorder.data', array());
63
64 // check user permissions
65 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
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=managepackorder&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=packorder.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['id_user'] = $input->getUint('id_user', 0);
139 $args['purchaser_nominative'] = $input->getString('purchaser_nominative', '');
140 $args['purchaser_mail'] = $input->getString('purchaser_mail', '');
141 $args['purchaser_phone'] = $input->getString('purchaser_phone', '');
142 $args['purchaser_prefix'] = $input->getString('purchaser_prefix', '');
143 $args['purchaser_country'] = $input->getString('purchaser_country', '');
144 $args['id_payment'] = $input->getUint('id_payment', 0);
145 $args['total_cost'] = $input->getFloat('total_cost', 0);
146 $args['total_net'] = $input->getFloat('total_net', 0);
147 $args['total_tax'] = $input->getFloat('total_tax', 0);
148 $args['payment_charge'] = $input->getFloat('payment_charge', 0);
149 $args['payment_tax'] = $input->getFloat('payment_tax', 0);
150 $args['status'] = $input->getString('status', '');
151 $args['status_comment'] = $input->getString('comment', '');
152 $args['notify'] = $input->getBool('notifycust', false);
153 $args['add_discount'] = $input->getString('add_discount', '');
154 $args['remove_discount'] = $input->getBool('remove_discount', false);
155 $args['id'] = $input->getUint('id', 0);
156
157 if ($args['add_discount'] === 'manual')
158 {
159 // fetch manual discount from request
160 $args['add_discount'] = $input->get('manual_discount', [], 'array');
161 }
162
163 // import custom fields requestor and loader (as dependency)
164 VAPLoader::import('libraries.customfields.requestor');
165
166 // get relevant custom fields only
167 $_cf = VAPCustomFieldsLoader::getInstance()
168 ->noRequiredCheckbox()
169 ->fetch();
170
171 // load custom fields from request
172 $args['custom_f'] = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = false);
173
174 // inject uploads in custom fields array
175 foreach ($tmp['uploads'] as $k => $v)
176 {
177 $args['custom_f'][$k] = $v;
178 }
179
180 // register data fetched by the custom fields so that the package
181 // order is able to use them for saving purposes
182 $args['fields_data'] = $tmp;
183
184 // get selected packages
185 $args['items'] = $input->get('pack_json', array(), 'array');
186 // load deleted packages
187 $args['deletedItems'] = $input->get('pack_deleted', array(), 'uint');
188
189 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
190
191 // check user permissions
192 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
193 {
194 // back to main list, not authorised to create/edit records
195 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
196 $this->cancel();
197
198 return false;
199 }
200
201 // get db model
202 $order = $this->getModel();
203
204 // try to save arguments
205 $id = $order->save($args);
206
207 if (!$id)
208 {
209 // get string error
210 $error = $order->getError(null, true);
211
212 // display error message
213 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
214
215 $url = 'index.php?option=com_vikappointments&view=managepackorder';
216
217 if ($args['id'])
218 {
219 $url .= '&cid[]=' . $args['id'];
220 }
221
222 // redirect to new/edit page
223 $this->setRedirect($url);
224
225 return false;
226 }
227
228 // display generic successful message
229 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
230
231 // redirect to edit page
232 $this->setRedirect('index.php?option=com_vikappointments&task=packorder.edit&cid[]=' . $id);
233
234 return true;
235 }
236
237 /**
238 * Deletes a list of records set in the request.
239 *
240 * @return boolean
241 */
242 public function delete()
243 {
244 $app = JFactory::getApplication();
245 $user = JFactory::getUser();
246
247 /**
248 * Added token validation.
249 * Both GET and POST are supported.
250 *
251 * @since 1.7
252 */
253 if (!JSession::checkToken() && !JSession::checkToken('get'))
254 {
255 // back to main list, missing CSRF-proof token
256 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
257 $this->cancel();
258
259 return false;
260 }
261
262 $cid = $app->input->get('cid', array(), 'uint');
263
264 // check user permissions
265 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
266 {
267 // back to main list, not authorised to delete records
268 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
269 $this->cancel();
270
271 return false;
272 }
273
274 // delete selected records
275 $this->getModel()->delete($cid);
276
277 // back to main list
278 $this->cancel();
279
280 return true;
281 }
282
283 /**
284 * Redirects the users to the main records list.
285 *
286 * @return void
287 */
288 public function cancel()
289 {
290 if (VikAppointments::isPackagesEnabled())
291 {
292 $this->setRedirect('index.php?option=com_vikappointments&view=packorders');
293 }
294 else
295 {
296 $this->setRedirect('index.php?option=com_vikappointments');
297 }
298 }
299
300 /**
301 * AJAX end-point used to change the status code.
302 * The task expects the following arguments to be set in request.
303 *
304 * @param integer id The package order ID.
305 * @param string status The new status code.
306 * @param string layout The layout to use (for return).
307 *
308 * @return void
309 */
310 public function changestatusajax()
311 {
312 $input = JFactory::getApplication()->input;
313 $user = JFactory::getUser();
314
315 /**
316 * Added token validation.
317 *
318 * @since 1.7
319 */
320 if (!JSession::checkToken())
321 {
322 // missing CSRF-proof token
323 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
324 }
325
326 $data = array();
327 $data['id'] = $input->getUint('id');
328 $data['status'] = $input->getString('status');
329
330 // check user permissions
331 if (!$data['id'] || !$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.packages', 'com_vikappointments') || !VikAppointments::isPackagesEnabled())
332 {
333 // not authorised to edit records
334 UIErrorFactory::raiseError(403, JText::translate('JERROR_ALERTNOAUTHOR'));
335 }
336
337 // get status code details
338 $code = JHtml::fetch('vaphtml.status.find', '*', array('code' => $data['status']), $limit = true);
339
340 if (!$code)
341 {
342 // code not found
343 UIErrorFactory::raiseError(404, JText::translate('JGLOBAL_NO_MATCHING_RESULTS'));
344 }
345
346 // register comment for status change
347 $data['status_comment'] = 'VAP_STATUS_CHANGED_FROM_LIST';
348
349 $model = $this->getModel();
350
351 // update status
352 $model->save($data);
353
354 /**
355 * Automatically send a notification to the customer according to the mailing preferences.
356 *
357 * @since 1.7.7
358 */
359 if (VAPFactory::getConfig()->getBool('notifyonstatuschange'))
360 {
361 try
362 {
363 // send e-mail notification to the customer
364 $model->sendEmailNotification($data['id'], [
365 // validate e-mail rules before sending
366 'check' => true,
367 ]);
368 }
369 catch (Exception $error)
370 {
371 // go ahead silently
372 }
373 }
374
375 // render HTML
376 $code->html = JHtml::fetch('vaphtml.status.display', $code, $input->getString('layout'));
377
378 // send code to caller
379 $this->sendJSON($code);
380 }
381 }
382