PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / models / empsubscrorder.php
vikappointments / site / models Last commit date
allorders.php 1 month ago calendarweek.php 1 month ago cart.php 1 month ago confirmapp.php 1 month ago empaccountstat.php 1 month ago empattachser.php 1 month ago empcoupons.php 1 month ago empcustfields.php 1 month ago empeditcoupon.php 1 month ago empeditcustfield.php 1 month ago empeditlocation.php 1 month ago empeditpay.php 1 month ago empeditprofile.php 1 month ago empeditservice.php 1 month ago empeditwdays.php 1 month ago emplocations.php 1 month ago emplocwdays.php 1 month ago emplogin.php 1 month ago employeesearch.php 1 month ago employeeslist.php 1 month ago empmanres.php 1 month ago emppaylist.php 1 month ago empserviceslist.php 1 month ago empsettingsman.php 1 month ago empsubscrcart.php 1 month ago empsubscrhistory.php 1 month ago empsubscrorder.php 1 month ago empwdays.php 1 month ago index.html 1 month ago packages.php 1 month ago packagescart.php 1 month ago packagesconfirm.php 1 month ago packorders.php 1 month ago servicesearch.php 1 month ago serviceslist.php 1 month ago subscrcart.php 1 month ago subscrhistory.php 1 month ago subscrpayment.php 1 month ago
empsubscrorder.php
311 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.model');
15
16 /**
17 * VikAppointments employee area subscription order view model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelEmpsubscrorder extends JModelVAP
22 {
23 /**
24 * Completes the booking process by saving the purchased subscription.
25 *
26 * @param array $data An array containing some booking options.
27 *
28 * @return mixed The landing page URL on success, false otherwise.
29 */
30 public function save($data)
31 {
32 $dispatcher = VAPFactory::getEventDispatcher();
33
34 // get cart model
35 $cart = JModelVAP::getInstance('empsubscrcart');
36
37 ////////////////////////////////////////////////////////////
38 ////////////////////// INITIALIZATION //////////////////////
39 ////////////////////////////////////////////////////////////
40
41 $auth = VAPEmployeeAuth::getInstance();
42
43 if (!$auth->isEmployee())
44 {
45 // the user is not an employee
46 $this->setError(JText::translate('JERROR_ALERTNOAUTHOR'));
47 return false;
48 }
49
50 try
51 {
52 /**
53 * Trigger event to manipulate the cart instance.
54 *
55 * @param mixed $cart The cart instance.
56 *
57 * @return void
58 *
59 * @since 1.7
60 * @since 1.7.8 It is now possible to throw exceptions to abort the saving process.
61 */
62 $dispatcher->trigger('onInitSaveSubscriptionOrder', [$cart]);
63 }
64 catch (Exception $e)
65 {
66 $this->setError($e->getMessage());
67 return false;
68 }
69
70 // prepare order array
71 $order = array();
72
73 ////////////////////////////////////////////////////////////
74 //////////////////// FETCH SUBSCRIPTION ////////////////////
75 ////////////////////////////////////////////////////////////
76
77 try
78 {
79 // get selected subscription
80 $subscr = $cart->getSubscription();
81 }
82 catch (Exception $e)
83 {
84 // an error occurred, register message and abort
85 $this->setError($e);
86 return false;
87 }
88
89 $order['id_subscr'] = $subscr['id'];
90
91 ////////////////////////////////////////////////////////////
92 //////////////////// FETCH CUSTOM FIELDS ///////////////////
93 ////////////////////////////////////////////////////////////
94
95 // register current language tag
96 $order['langtag'] = JFactory::getLanguage()->getTag();
97
98 // assign subscription to the current employee
99 $order['id_employee'] = $auth->id;
100
101 if (!empty($data['billing']))
102 {
103 $order['billing'] = array_map(function($elem)
104 {
105 // make input safe
106 return JComponentHelper::filterText($elem);
107 }, (array) $data['billing']);
108
109 $tmp = array();
110
111 /**
112 * Trigger event to manipulate the custom fields array and the
113 * billing information of the employee.
114 *
115 * @param array &$fields The custom fields values.
116 *
117 * @return void
118 *
119 * @since 1.7
120 */
121 $dispatcher->trigger('onPrepareFieldsSaveEmployeeSubscriptionOrder', array(&$order['billing']));
122
123 // get employee model
124 $empModel = JModelVAP::getInstance('employee');
125
126 // update employee billing details
127 $empModel->save([
128 'id' => $order['id_employee'],
129 'billing_json' => json_encode($order['billing']),
130 ]);
131 }
132
133 ////////////////////////////////////////////////////////////
134 ///////////////////// FETCH TOTAL COSTS ////////////////////
135 ////////////////////////////////////////////////////////////
136
137 $totals = $cart->getTotals();
138
139 // set up order totals
140 $order['total_cost'] = $totals->gross;
141 $order['total_net'] = $totals->net;
142 $order['total_tax'] = $totals->tax;
143 $order['discount'] = $totals->discount;
144
145 ////////////////////////////////////////////////////////////
146 ///////////////////// VALIDATE PAYMENT /////////////////////
147 ////////////////////////////////////////////////////////////
148
149 $payment = null;
150
151 $order['id_payment'] = 0;
152
153 if ($order['total_cost'] > 0)
154 {
155 // get selected payment
156 $payment = $cart->getPayment();
157
158 if ($payment)
159 {
160 if (!empty($totals->payment))
161 {
162 // include payment charge
163 $order['payment_charge'] = $totals->payment->net;
164 $order['payment_tax'] = $totals->payment->tax;
165 }
166
167 $order['id_payment'] = $payment['id'];
168
169 // auto-confirm orders according to the configuration of
170 // the payment, otherwise force PENDING status to let the
171 // employees be able to start a transaction
172 if ($payment['setconfirmed'])
173 {
174 // auto-confirm order
175 $order['status'] = JHtml::fetch('vaphtml.status.confirmed', 'subscriptions', 'code');
176 }
177 }
178 }
179
180 ////////////////////////////////////////////////////////////
181 /////////////////////// ORDER STATUS ///////////////////////
182 ////////////////////////////////////////////////////////////
183
184 if (empty($order['status']))
185 {
186 // auto-confirm in case of no cost
187 $status = $order['total_cost'] > 0 ? 'pending' : 'confirmed';
188
189 // status not yet specified, use the default one
190 $order['status'] = JHtml::fetch('vaphtml.status.' . $status, 'subscriptions', 'code');
191 }
192
193 $order['status_comment'] = null;
194
195 /**
196 * Trigger event to manipulate the order status at runtime.
197 *
198 * @param string &$status The currently fetched order status.
199 * @param string &$comment An optional status comment to be used.
200 *
201 * @return void
202 *
203 * @since 1.7
204 */
205 $dispatcher->trigger('onFetchStatusSaveSubscriptionOrder', array(&$order['status'], &$order['status_comment']));
206
207 // check whether the status has been immediately confirmed and we have an empty comment
208 if (empty($order['status_comment']) && JHtml::fetch('vaphtml.status.isconfirmed', 'subscriptions', $order['status']))
209 {
210 if ($order['total_cost'] == 0)
211 {
212 // no cost, automatically confirmed
213 $order['status_comment'] = 'VAP_STATUS_CONFIRMED_AS_NO_COST';
214 }
215 else if (!$payment)
216 {
217 // no configured payments
218 $order['status_comment'] = 'VAP_STATUS_CONFIRMED_AS_NO_PAYMENT';
219 }
220 else
221 {
222 // auto-approved through the configuration of the payment
223 $order['status_comment'] = 'VAP_STATUS_CONFIRMED_RESULT_OF_PAYMENT';
224 }
225 }
226
227 ////////////////////////////////////////////////////////////
228 ///////////////////// FETCH COUPON CODE ////////////////////
229 ////////////////////////////////////////////////////////////
230
231 // check whether the coupon code was set
232 $coupon = $cart->getDiscount('coupon');
233
234 if ($coupon)
235 {
236 // assign coupon code to the order
237 $order['coupon'] = (array) $coupon->get('couponData');
238 // redeem coupon code
239 VikAppointments::couponUsed($order['coupon']);
240 }
241
242 ////////////////////////////////////////////////////////////
243 //////////////////// SAVE ORDER DETAILS ////////////////////
244 ////////////////////////////////////////////////////////////
245
246 $ordnum = $ordkey = null;
247
248 // get subscription order model
249 $orderModel = JModelVAP::getInstance('subscrorder');
250
251 // save the order
252 if (!$orderModel->save($order))
253 {
254 // an error occurred while trying to save the order
255 $error = $orderModel->getError();
256 // propagate the error found or use a generic one
257 $this->setError($error ? $error : JText::translate('VAPSUBSCRINSERTERR'));
258 return false;
259 }
260
261 // get order saved data
262 $orderData = $orderModel->getData();
263
264 // use order number/key pair of saved order
265 $ordnum = $orderData['id'];
266
267 // empty cart on success
268 $cart->emptyCart();
269
270 ////////////////////////////////////////////////////////////
271 ////////////////////// NOTIFICATIONS ///////////////////////
272 ////////////////////////////////////////////////////////////
273
274 // $mailOptions = array();
275 // validate e-mail rules before sending
276 // $mailOptions['check'] = true;
277
278 // send e-mail notification to the employee
279 // $orderModel->sendEmailNotification($ordnum, $mailOptions);
280
281 // send e-mail notification to the administrator(s)
282 // $mailOptions['client'] = 'subscradmin';
283 // $orderModel->sendEmailNotification($ordnum, $mailOptions);
284
285 $redirect_url = 'index.php?option=com_vikappointments&view=empsubscrorder&id=' . $ordnum;
286
287 if (!empty($data['itemid']))
288 {
289 $redirect_url .= '&Itemid=' . $data['itemid'];
290 }
291
292 /**
293 * Trigger event to manipulate the redirect URL after completing
294 * the subscription purchase process.
295 *
296 * Use VAPOrderFactory::getEmployeeSubscription($ordnum) to access the order details.
297 *
298 * @param string &$url The redirect URL (plain).
299 * @param integer $order The order id.
300 *
301 * @return void
302 *
303 * @since 1.7
304 */
305 $dispatcher->trigger('onRedirectSubscriptionOrder', array(&$redirect_url, $ordnum));
306
307 // rewrite landing page
308 return JRoute::rewrite($redirect_url, false);
309 }
310 }
311