PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / models / coupon.php
vikappointments / admin / models Last commit date
apiban.php 4 years ago apilog.php 2 years ago apiplugin.php 4 years ago apiuser.php 2 years ago apiuseroptions.php 2 years ago backup.php 5 months ago caldays.php 1 month ago calendar.php 1 month ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 4 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 5 months ago conversion.php 4 years ago country.php 2 years ago coupon.php 2 years ago couponemployee.php 4 years ago coupongroup.php 2 years ago couponservice.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 2 years ago customfservice.php 4 years ago customizer.php 4 years ago empgroup.php 2 years ago employee.php 2 years ago empsettings.php 4 years ago file.php 4 years ago findreservation.php 1 month ago group.php 2 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langempgroup.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 2 years ago langoptiongroup.php 4 years ago langoptionvar.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 2 years ago langtaxrule.php 4 years ago location.php 2 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 2 years ago multiorder.php 1 month ago option.php 1 year ago optiongroup.php 2 years ago optionvar.php 1 year ago orderstatus.php 2 years ago package.php 2 years ago packageservice.php 4 years ago packgroup.php 2 years ago packorder.php 2 years ago packorderitem.php 1 month ago payment.php 2 years ago rate.php 2 years ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 1 month ago resoptassoc.php 2 years ago restriction.php 2 years ago review.php 3 years ago serempassoc.php 1 year ago seroptassoc.php 4 years ago serrateassoc.php 4 years ago serrestrassoc.php 4 years ago service.php 2 years ago state.php 2 years ago statswidget.php 2 years ago statuscode.php 2 years ago subscription.php 1 month ago subscrorder.php 1 month ago tag.php 4 years ago tax.php 2 years ago taxrule.php 2 years ago updateprogram.php 4 years ago usernote.php 2 years ago waitinglist.php 1 month ago webhook.php 1 year ago worktime.php 1 month ago
coupon.php
273 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 coupon model.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsModelCoupon extends JModelVAP
22 {
23 /**
24 * Basic item loading implementation.
25 *
26 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
27 * If not set the instance property value is used.
28 * @param boolean $new True to return an empty object if missing.
29 *
30 * @return mixed The record object on success, null otherwise.
31 */
32 public function getItem($pk, $new = false)
33 {
34 // load item through parent
35 $item = parent::getItem($pk, $new);
36
37 if ($item && !$item->id)
38 {
39 // use random code
40 $item->code = VikAppointments::generateSerialCode(12, 'coupon');
41 }
42
43 return $item;
44 }
45
46 /**
47 * Basic save implementation.
48 *
49 * @param mixed $data Either an array or an object of data to save.
50 *
51 * @return mixed The ID of the record on success, false otherwise.
52 */
53 public function save($data)
54 {
55 $data = (array) $data;
56
57 // attempt to save the relation
58 $id = parent::save($data);
59
60 if (!$id)
61 {
62 // an error occurred, do not go ahead
63 return false;
64 }
65
66 if (isset($data['services']))
67 {
68 // get coupon-service model
69 $model = JModelVAP::getInstance('couponservice');
70 // define relations
71 $model->setRelation($id, $data['services']);
72 }
73
74 if (isset($data['employees']))
75 {
76 // get coupon-employee model
77 $model = JModelVAP::getInstance('couponemployee');
78 // define relations
79 $model->setRelation($id, $data['employees']);
80 }
81
82 return $id;
83 }
84
85 /**
86 * Extend delete implementation to delete any related records
87 * stored within a separated table.
88 *
89 * @param mixed $ids Either the record ID or a list of records.
90 *
91 * @return boolean True on success, false otherwise.
92 */
93 public function delete($ids)
94 {
95 // only int values are accepted
96 $ids = array_map('intval', (array) $ids);
97
98 // invoke parent first
99 if (!parent::delete($ids))
100 {
101 // nothing to delete
102 return false;
103 }
104
105 $dbo = JFactory::getDbo();
106
107 // load any coupon-service relation
108 $q = $dbo->getQuery(true)
109 ->select($dbo->qn('id'))
110 ->from($dbo->qn('#__vikappointments_coupon_service_assoc'))
111 ->where($dbo->qn('id_coupon') . ' IN (' . implode(',', $ids) . ')' );
112
113 $dbo->setQuery($q);
114
115 if ($assoc_ids = $dbo->loadColumn())
116 {
117 // get coupon-service model
118 $model = JModelVAP::getInstance('couponservice');
119 // delete relations
120 $model->delete($assoc_ids);
121 }
122
123 // load any coupon-employee relation
124 $q = $dbo->getQuery(true)
125 ->select($dbo->qn('id'))
126 ->from($dbo->qn('#__vikappointments_coupon_employee_assoc'))
127 ->where($dbo->qn('id_coupon') . ' IN (' . implode(',', $ids) . ')' );
128
129 $dbo->setQuery($q);
130
131 if ($assoc_ids = $dbo->loadColumn())
132 {
133 // get coupon-employee model
134 $model = JModelVAP::getInstance('couponemployee');
135 // delete relations
136 $model->delete($assoc_ids);
137 }
138
139 return true;
140 }
141
142 /**
143 * Returns the details of the specified coupon code.
144 *
145 * @param string $code The coupon code.
146 *
147 * @return mixed The coupon details on success, null otherwise.
148 */
149 public function getCoupon($code)
150 {
151 return $this->getItem(array('code' => $code));
152 }
153
154 /**
155 * Returns a list of services assigned to the specified coupon code.
156 *
157 * @param integer $id The coupon id.
158 *
159 * @return array
160 */
161 public function getServices($id)
162 {
163 if ($id)
164 {
165 $dbo = JFactory::getDbo();
166
167 // load any coupon-service relation
168 $q = $dbo->getQuery(true)
169 ->select($dbo->qn('id_service'))
170 ->from($dbo->qn('#__vikappointments_coupon_service_assoc'))
171 ->where($dbo->qn('id_coupon') . ' = ' . (int) $id);
172
173 $dbo->setQuery($q);
174 return $dbo->loadColumn();
175 }
176
177 return [];
178 }
179
180 /**
181 * Marks the specified coupon as used.
182 * In addition, removes the coupon if it should be deleted once
183 * the maximum number of usages is reached.
184 *
185 * @param mixed $coupon Either a coupon code or an array/object.
186 *
187 * @return boolean True on success, false otherwise.
188 */
189 public function redeem($coupon)
190 {
191 if (is_string($coupon))
192 {
193 // coupon code given, recover details
194 $coupon = $this->getCoupon($coupon);
195 }
196 else
197 {
198 // cast to object
199 $coupon = (object) $coupon;
200 }
201
202 if (!$coupon || empty($coupon->id))
203 {
204 // invalid coupon
205 return false;
206 }
207
208 // increase total usages
209 $coupon->used_quantity++;
210
211 // check whether we reached the maximum number of usages, the coupon
212 // is a GIFT and it should be removed from the system
213 if ($coupon->max_quantity - $coupon->used_quantity <= 0 && $coupon->remove_gift && $coupon->type == 2)
214 {
215 // delete coupon ID
216 $result = $this->delete($coupon->id);
217 }
218 else
219 {
220 // prepare save data
221 $data = array(
222 'id' => $coupon->id,
223 'used_quantity' => $coupon->used_quantity,
224 );
225
226 // commit changes
227 $result = (bool) $this->save($data);
228 }
229
230 return $result;
231 }
232
233 /**
234 * Restores the number of usages by one.
235 *
236 * @param mixed $coupon Either a coupon code or an array/object.
237 *
238 * @return boolean True on success, false otherwise.
239 */
240 public function unredeem($coupon)
241 {
242 if (is_string($coupon))
243 {
244 // coupon code given, recover details
245 $coupon = $this->getCoupon($coupon);
246 }
247 else
248 {
249 // cast to object
250 $coupon = (object) $coupon;
251 }
252
253 if (!$coupon || empty($coupon->id))
254 {
255 // invalid coupon
256 return false;
257 }
258
259 // decrease total usages
260 $coupon->used_quantity--;
261
262
263 // prepare save data
264 $data = array(
265 'id' => $coupon->id,
266 'used_quantity' => max(array(0, $coupon->used_quantity)),
267 );
268
269 // commit changes
270 return (bool) $this->save($data);
271 }
272 }
273