|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Nicolaas [at] sunnysideup.co.nz |
|
5
|
|
|
* @author Romain [at] sunnysideup.co.nz |
|
6
|
|
|
* @package: ecommerce |
|
7
|
|
|
* @sub-package: ecommerce_delivery |
|
8
|
|
|
* @description: Shipping calculation scheme based on SimpleShippingModifier. |
|
9
|
|
|
* It lets you set fixed shipping costs, or a fixed |
|
10
|
|
|
* cost for each region you're delivering to. |
|
11
|
|
|
*/ |
|
12
|
|
|
class DiscountCouponModifier extends OrderModifier |
|
13
|
|
|
{ |
|
14
|
|
|
// ######################################## *** model defining static variables (e.g. $db, $has_one) |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* standard SS Variable |
|
18
|
|
|
* @var Array |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $db = array( |
|
|
|
|
|
|
21
|
|
|
'DebugString' => 'HTMLText', |
|
22
|
|
|
'SubTotalAmount' => 'Currency', |
|
23
|
|
|
'CouponCodeEntered' => 'Varchar(25)' |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* standard SS Variable |
|
28
|
|
|
* @var Array |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
31
|
|
|
"DiscountCouponOption" => "DiscountCouponOption" |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Should the discount be worked out over the the sub-total or |
|
36
|
|
|
* the Total Total? |
|
37
|
|
|
* @var Boolean |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $include_modifiers_in_subtotal = false; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* If this method is present in the Buyable, the related order item will be excluded |
|
43
|
|
|
* @var Boolean |
|
44
|
|
|
*/ |
|
45
|
|
|
private static $exclude_buyable_method = 'ExcludeInDiscountCalculation'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Standard SS Variable |
|
49
|
|
|
* @var String |
|
50
|
|
|
*/ |
|
51
|
|
|
private static $singular_name = "Discount Coupon Entry"; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
public function i18n_singular_name() |
|
54
|
|
|
{ |
|
55
|
|
|
return _t("DiscountCouponModifier.SINGULAR_NAME", "Discount Coupon Entry"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Standard SS Variable |
|
60
|
|
|
* @var String |
|
61
|
|
|
*/ |
|
62
|
|
|
private static $plural_name = "Discount Coupon Entries"; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public function i18n_plural_name() |
|
65
|
|
|
{ |
|
66
|
|
|
return _t("DiscountCouponModifier.PLURAL_NAME", "Discount Coupon Entries"); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields) |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Standard SS Method |
|
73
|
|
|
* @return FieldList |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getCMSFields() |
|
76
|
|
|
{ |
|
77
|
|
|
$fields = parent::getCMSFields(); |
|
78
|
|
|
$fields->removeByName("DebugString"); |
|
79
|
|
|
$fields->removeByName("SubTotalAmount"); |
|
80
|
|
|
$fields->removeByName("OrderCoupon"); |
|
81
|
|
|
$fields->addFieldToTab( |
|
82
|
|
|
"Root.Debug", |
|
83
|
|
|
new ReadonlyField( |
|
84
|
|
|
"SubTotalAmountShown", |
|
85
|
|
|
_t("DiscountCouponModifier.SUB_TOTAL_AMOUNT", "sub-total amount"), |
|
86
|
|
|
$this->SubTotalAmount |
|
|
|
|
|
|
87
|
|
|
) |
|
88
|
|
|
); |
|
89
|
|
|
$fields->addFieldToTab( |
|
90
|
|
|
"Root.Debug", |
|
91
|
|
|
new ReadonlyField( |
|
92
|
|
|
"DebugStringShown", |
|
93
|
|
|
_t("DiscountCouponModifier.DEBUG_STRING", "debug string"), |
|
94
|
|
|
$this->DebugString |
|
|
|
|
|
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
return $fields; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order) |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Used in calculations to work out how much we need. |
|
104
|
|
|
* @var Double | Null |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $_actualDeductions = null; |
|
107
|
|
|
|
|
108
|
|
|
// ######################################## *** CRUD functions (e.g. canEdit) |
|
109
|
|
|
// ######################################## *** init and update functions |
|
110
|
|
|
/** |
|
111
|
|
|
* updates all database fields |
|
112
|
|
|
* |
|
113
|
|
|
* @param Bool $force - run it, even if it has run already |
|
114
|
|
|
*/ |
|
115
|
|
|
public function runUpdate($force = false) |
|
116
|
|
|
{ |
|
117
|
|
|
if (!$this->IsRemoved()) { |
|
118
|
|
|
$this->checkField("SubTotalAmount"); |
|
119
|
|
|
$this->checkField("CouponCodeEntered"); |
|
120
|
|
|
$this->checkField("DiscountCouponOptionID"); |
|
121
|
|
|
} |
|
122
|
|
|
parent::runUpdate($force); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// ######################################## *** form functions (e. g. showform and getform) |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Show the form? |
|
129
|
|
|
* We always show it when there are items in the cart. |
|
130
|
|
|
* @return Boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
public function ShowForm() |
|
133
|
|
|
{ |
|
134
|
|
|
$items = $this->Order()->Items(); |
|
135
|
|
|
if ($items) { |
|
136
|
|
|
//-- START HACK |
|
137
|
|
|
return true; |
|
138
|
|
|
//-- END HACK |
|
139
|
|
|
if (singleton('DiscountCouponOption')->hasExtension('DiscountCouponSiteTreeDOD')) { |
|
|
|
|
|
|
140
|
|
|
foreach ($items as $item) { |
|
141
|
|
|
//here we need to add foreach valid coupon |
|
142
|
|
|
//for each item->Buyable |
|
143
|
|
|
//check if the coupon |
|
144
|
|
|
//can be applied to the buyable |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
return DiscountCouponOption::get()->exclude(array("NumberOfTimesCouponCanBeUsed" => 0))->count(); |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Controller $optionalController |
|
|
|
|
|
|
156
|
|
|
* @param Validator $optionalValidator |
|
|
|
|
|
|
157
|
|
|
* @return DiscountCouponModifier_Form |
|
158
|
|
|
*/ |
|
159
|
|
|
public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null) |
|
160
|
|
|
{ |
|
161
|
|
|
$fields = new FieldList( |
|
162
|
|
|
$this->headingField(), |
|
163
|
|
|
$this->descriptionField(), |
|
164
|
|
|
new TextField( |
|
165
|
|
|
'DiscountCouponCode', |
|
166
|
|
|
_t("DiscountCouponModifier.COUPON", 'Coupon'), |
|
167
|
|
|
$this->LiveCouponCodeEntered() |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
$actions = new FieldList( |
|
171
|
|
|
new FormAction( |
|
172
|
|
|
'submit', |
|
173
|
|
|
_t("DiscountCouponModifier.APPLY", 'Apply Coupon') |
|
174
|
|
|
) |
|
175
|
|
|
); |
|
176
|
|
|
$form = new DiscountCouponModifier_Form( |
|
177
|
|
|
$optionalController, |
|
178
|
|
|
'DiscountCouponModifier', |
|
179
|
|
|
$fields, |
|
180
|
|
|
$actions, |
|
181
|
|
|
$optionalValidator |
|
182
|
|
|
); |
|
183
|
|
|
$fields->fieldByName("DiscountCouponCode")->setValue($this->CouponCodeEntered); |
|
|
|
|
|
|
184
|
|
|
return $form; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param String $code - code that has been entered |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
* */ |
|
192
|
|
|
public function updateCouponCodeEntered($code) |
|
193
|
|
|
{ |
|
194
|
|
|
//set to new value .... |
|
195
|
|
|
$this->CouponCodeEntered = $code; |
|
|
|
|
|
|
196
|
|
|
$coupon = DiscountCouponOption::get() |
|
197
|
|
|
->filter(array("Code" => $code))->first(); |
|
198
|
|
|
//apply valid discount coupong |
|
199
|
|
|
if ($coupon) { |
|
200
|
|
|
if ($coupon->IsValid() && $this->isValidAdditional($coupon)) { |
|
|
|
|
|
|
201
|
|
|
$this->setCoupon($coupon); |
|
|
|
|
|
|
202
|
|
|
$messages = array(_t('DiscountCouponModifier.APPLIED', 'Coupon applied'), 'good'); |
|
203
|
|
|
} else { |
|
204
|
|
|
$messages = array(_t('DiscountCouponModifier.NOT_VALID', 'Coupon is no longer available'), 'bad'); |
|
205
|
|
|
$this->DiscountCouponOptionID = 0; |
|
|
|
|
|
|
206
|
|
|
} |
|
207
|
|
|
} elseif ($code) { |
|
208
|
|
|
$messages = array(_t('DiscountCouponModifier.NOTFOUND', 'Coupon could not be found'), 'bad'); |
|
209
|
|
|
if ($this->DiscountCouponOptionID) { |
|
|
|
|
|
|
210
|
|
|
$this->DiscountCouponOptionID = 0; |
|
|
|
|
|
|
211
|
|
|
$messages = array(_t('DiscountCouponModifier.REMOVED', 'Existing coupon removed'), 'good'); |
|
212
|
|
|
} |
|
213
|
|
|
} else { |
|
214
|
|
|
//to do: do we need to remove it again? |
|
215
|
|
|
$messages = array(_t('DiscountCouponModifier.NOT_ENTERED', 'No coupon was entered'), 'bad'); |
|
216
|
|
|
} |
|
217
|
|
|
$this->write(); |
|
218
|
|
|
|
|
219
|
|
|
return $messages; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @param DiscountCouponOption $coupon |
|
224
|
|
|
*/ |
|
225
|
|
|
public function setCoupon($coupon) |
|
226
|
|
|
{ |
|
227
|
|
|
$this->DiscountCouponOptionID = $coupon->ID; |
|
|
|
|
|
|
228
|
|
|
$this->write(); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param int $couponID |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setCouponByID($couponID) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->DiscountCouponOptionID = $couponID; |
|
|
|
|
|
|
237
|
|
|
$this->write(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
// ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @see self::HideInAjaxUpdate |
|
244
|
|
|
* |
|
245
|
|
|
* @return boolean |
|
246
|
|
|
*/ |
|
247
|
|
|
public function ShowInTable() |
|
248
|
|
|
{ |
|
249
|
|
|
if ($this->DiscountCouponOptionID) { |
|
|
|
|
|
|
250
|
|
|
return true; |
|
251
|
|
|
} elseif ($this->Order()->IsSubmitted()) { |
|
252
|
|
|
return false; |
|
253
|
|
|
} else { |
|
254
|
|
|
//we hide it with ajax if needed |
|
255
|
|
|
return true; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return boolean |
|
261
|
|
|
*/ |
|
262
|
|
|
public function CanRemove() |
|
263
|
|
|
{ |
|
264
|
|
|
return false; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return float |
|
269
|
|
|
*/ |
|
270
|
|
|
public function CartValue() |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->getCartValue(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
public function getCartValue() |
|
|
|
|
|
|
276
|
|
|
{ |
|
277
|
|
|
return $this->TableValue; |
|
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
// ######################################## *** inner calculations.... USES CALCULATED VALUES |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Checks for extensions to make sure it is valid... |
|
284
|
|
|
* @param DiscountCouponOption $coupon |
|
285
|
|
|
* @return bool returns true if the coupon is valid |
|
286
|
|
|
*/ |
|
287
|
|
|
protected function isValidAdditional($coupon) |
|
288
|
|
|
{ |
|
289
|
|
|
$exclusions = $this->extend('checkForExclusions', $coupon); |
|
290
|
|
|
if (is_array($exclusions) && count($exclusions)) { |
|
291
|
|
|
foreach ($exclusions as $exclusion) { |
|
292
|
|
|
if ($exclusion === true) { |
|
293
|
|
|
return false; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
return true; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* returns the discount coupon, if any ... |
|
302
|
|
|
* |
|
303
|
|
|
* @return DiscountCouponOption | null |
|
|
|
|
|
|
304
|
|
|
*/ |
|
305
|
|
|
protected function myDiscountCouponOption() |
|
306
|
|
|
{ |
|
307
|
|
|
$coupon = null; |
|
|
|
|
|
|
308
|
|
|
if ($id = $this->LiveDiscountCouponOptionID()) { |
|
309
|
|
|
$coupon = DiscountCouponOption::get()->byID($id); |
|
310
|
|
|
if ($coupon) { |
|
311
|
|
|
if ($coupon->ApplyPercentageToApplicableProducts) { |
|
312
|
|
|
$arrayOfOrderItemsToWhichThisCouponApplies = $this->applicableProductsArray($coupon); |
|
313
|
|
|
if (count($arrayOfOrderItemsToWhichThisCouponApplies)) { |
|
314
|
|
|
return $coupon; |
|
315
|
|
|
} |
|
316
|
|
|
} else { |
|
317
|
|
|
return $coupon; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
return null; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
private static $_applicable_products_array = null; |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* returns an Array of OrderItem IDs |
|
328
|
|
|
* to which the coupon applies |
|
329
|
|
|
* @param DiscountCouponOption |
|
330
|
|
|
* @return Array |
|
331
|
|
|
*/ |
|
332
|
|
|
protected function applicableProductsArray($coupon) |
|
333
|
|
|
{ |
|
334
|
|
|
if (self::$_applicable_products_array === null) { |
|
335
|
|
|
self::$_applicable_products_array = array(); |
|
336
|
|
|
$finalArray = array(); |
|
337
|
|
|
$order = $this->Order(); |
|
338
|
|
|
if ($order) { |
|
339
|
|
|
$items = $order->Items(); |
|
340
|
|
|
if ($items && $items->count()) { |
|
341
|
|
|
//get a list of all the products in the cart |
|
342
|
|
|
$arrayOfProductsInOrder = array(); |
|
343
|
|
|
foreach ($items as $item) { |
|
344
|
|
|
$buyable = $item->Buyable(); |
|
345
|
|
|
if ($buyable instanceof ProductVaration) { |
|
|
|
|
|
|
346
|
|
|
$buyable = $buyable->Product(); |
|
347
|
|
|
} |
|
348
|
|
|
$arrayOfProductsInOrder[$item->ID] = $buyable->ID; |
|
349
|
|
|
} |
|
350
|
|
|
//if no products / product groups are specified then |
|
351
|
|
|
//it applies |
|
352
|
|
|
//get a list of all the products to which the coupon applies |
|
353
|
|
|
$productsArray = $coupon->Products()->map("ID", "ID")->toArray(); |
|
354
|
|
|
if (count($productsArray)) { |
|
355
|
|
|
$matches = array_intersect($productsArray, $arrayOfProductsInOrder); |
|
356
|
|
|
foreach ($matches as $buyableID) { |
|
357
|
|
|
foreach ($arrayOfProductsInOrder as $itemID => $innerBuyableID) { |
|
358
|
|
|
if ($buyableID == $innerBuyableID) { |
|
359
|
|
|
$finalArray[$itemID] = $itemID; |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
} else { |
|
364
|
|
|
foreach ($arrayOfProductsInOrder as $itemID => $buyableID) { |
|
365
|
|
|
$finalArray[$itemID] = $itemID; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
self::$_applicable_products_array = $finalArray; |
|
371
|
|
|
} |
|
372
|
|
|
return self::$_applicable_products_array; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
// ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* @return int |
|
|
|
|
|
|
379
|
|
|
* */ |
|
380
|
|
|
protected function LiveName() |
|
381
|
|
|
{ |
|
382
|
|
|
$code = $this->LiveCouponCodeEntered(); |
|
383
|
|
|
$coupon = $this->myDiscountCouponOption(); |
|
384
|
|
|
if ($coupon) { |
|
385
|
|
|
return _t("DiscountCouponModifier.COUPON", "Coupon") . " '" . $code . "' " . _t("DiscountCouponModifier.APPLIED", "applied."); |
|
386
|
|
|
} elseif ($code) { |
|
387
|
|
|
return _t("DiscountCouponModifier.COUPON", "Coupon") . " '" . $code . "' " . _t("DiscountCouponModifier.COULDNOTBEAPPLIED", "could not be applied."); |
|
388
|
|
|
} |
|
389
|
|
|
return _t("DiscountCouponModifier.NOCOUPONENTERED", "No (valid) coupon entered") . $code; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
private static $subtotal = 0; |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* @return float |
|
396
|
|
|
* */ |
|
397
|
|
|
protected function LiveSubTotalAmount() |
|
398
|
|
|
{ |
|
399
|
|
|
if (!self::$subtotal) { |
|
400
|
|
|
$order = $this->Order(); |
|
401
|
|
|
$items = $order->Items(); |
|
402
|
|
|
$coupon = $this->myDiscountCouponOption(); |
|
403
|
|
|
if ($coupon && $coupon->ApplyPercentageToApplicableProducts) { |
|
|
|
|
|
|
404
|
|
|
$array = $this->applicableProductsArray($coupon); |
|
405
|
|
|
$subTotal = 0; |
|
406
|
|
|
if (count($array)) { |
|
407
|
|
|
if ($items) { |
|
408
|
|
|
$itemCount = 0; |
|
|
|
|
|
|
409
|
|
|
foreach ($items as $item) { |
|
410
|
|
|
if (in_array($item->ID, $array)) { |
|
411
|
|
|
$subTotal += $item->Total(); |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
} else { |
|
417
|
|
|
$subTotal = $order->SubTotal(); |
|
418
|
|
|
$function = $this->Config()->get("exclude_buyable_method"); |
|
419
|
|
|
if ($items) { |
|
420
|
|
|
foreach ($items as $item) { |
|
421
|
|
|
$buyable = $item->Buyable(); |
|
422
|
|
|
if ($buyable && $buyable->hasMethod($function) && $buyable->$function($this)) { |
|
423
|
|
|
$subTotal -= $item->Total(); |
|
424
|
|
|
} |
|
425
|
|
|
} |
|
426
|
|
|
} |
|
427
|
|
|
if ($this->Config()->get("include_modifiers_in_subtotal")) { |
|
428
|
|
|
$subTotal += $order->ModifiersSubTotal(array(get_class($this))); |
|
429
|
|
|
} |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
self::$subtotal = $subTotal; |
|
433
|
|
|
} |
|
434
|
|
|
return self::$subtotal; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
protected $_calculatedTotal = null; |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* @return float |
|
441
|
|
|
* */ |
|
442
|
|
|
protected function LiveCalculatedTotal() |
|
443
|
|
|
{ |
|
444
|
|
|
if ($this->_calculatedTotal === null) { |
|
445
|
|
|
$this->_calculatedTotal = 0; |
|
446
|
|
|
$this->_actualDeductions = 0; |
|
|
|
|
|
|
447
|
|
|
$this->DebugString = ""; |
|
|
|
|
|
|
448
|
|
|
$subTotal = $this->LiveSubTotalAmount(); |
|
449
|
|
|
$coupon = $this->myDiscountCouponOption(); |
|
450
|
|
|
if ($coupon && $this->isValidAdditional($coupon)) { |
|
451
|
|
|
if ($coupon->MinimumOrderSubTotalValue > 0 && $subTotal < $coupon->MinimumOrderSubTotalValue) { |
|
|
|
|
|
|
452
|
|
|
$this->_actualDeductions = 0; |
|
453
|
|
|
$this->DebugString .= "<hr />sub-total is too low to offer any discount: " . $this->_actualDeductions; |
|
|
|
|
|
|
454
|
|
|
} else { |
|
455
|
|
|
if ($coupon->DiscountAbsolute > 0) { |
|
|
|
|
|
|
456
|
|
|
$this->_actualDeductions += $coupon->DiscountAbsolute; |
|
|
|
|
|
|
457
|
|
|
$this->DebugString .= "<hr />using absolutes for coupon discount: " . $this->_actualDeductions; |
|
|
|
|
|
|
458
|
|
|
} |
|
459
|
|
|
if ($coupon->DiscountPercentage > 0) { |
|
|
|
|
|
|
460
|
|
|
$this->_actualDeductions += ($coupon->DiscountPercentage / 100) * $subTotal; |
|
|
|
|
|
|
461
|
|
|
$this->DebugString .= "<hr />using percentages for coupon discount: " . $this->_actualDeductions; |
|
|
|
|
|
|
462
|
|
|
} |
|
463
|
|
|
} |
|
464
|
|
|
if ($coupon->MaximumDiscount > 0) { |
|
|
|
|
|
|
465
|
|
|
if ($this->_actualDeductions > $coupon->MaximumDiscount) { |
|
|
|
|
|
|
466
|
|
|
$this->DebugString .= "<hr />actual deductions (" . $this->_actualDeductions . ") are greater than maximum discount (" . $coupon->MaximumDiscount . "): "; |
|
|
|
|
|
|
467
|
|
|
$this->_actualDeductions = $coupon->MaximumDiscount; |
|
|
|
|
|
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
} |
|
471
|
|
|
if ($subTotal < $this->_actualDeductions) { |
|
472
|
|
|
$this->_actualDeductions = $subTotal; |
|
473
|
|
|
} |
|
474
|
|
|
$this->DebugString .= "<hr />final score: " . $this->_actualDeductions; |
|
|
|
|
|
|
475
|
|
|
$this->_actualDeductions = -1 * $this->_actualDeductions; |
|
|
|
|
|
|
476
|
|
|
|
|
477
|
|
|
$this->_calculatedTotal = $this->_actualDeductions; |
|
478
|
|
|
} |
|
479
|
|
|
return $this->_calculatedTotal; |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* @return float |
|
484
|
|
|
* */ |
|
485
|
|
|
public function LiveTableValue() |
|
486
|
|
|
{ |
|
487
|
|
|
return $this->LiveCalculatedTotal(); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
/** |
|
491
|
|
|
* @return String |
|
492
|
|
|
* */ |
|
493
|
|
|
protected function LiveDebugString() |
|
494
|
|
|
{ |
|
495
|
|
|
return $this->DebugString; |
|
|
|
|
|
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
/** |
|
499
|
|
|
* @return String |
|
500
|
|
|
* */ |
|
501
|
|
|
protected function LiveCouponCodeEntered() |
|
502
|
|
|
{ |
|
503
|
|
|
return $this->CouponCodeEntered; |
|
|
|
|
|
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* @return int |
|
508
|
|
|
* */ |
|
509
|
|
|
protected function LiveDiscountCouponOptionID() |
|
510
|
|
|
{ |
|
511
|
|
|
return $this->DiscountCouponOptionID; |
|
|
|
|
|
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
// ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) |
|
515
|
|
|
|
|
516
|
|
|
/** |
|
517
|
|
|
* @return Boolean |
|
518
|
|
|
* */ |
|
519
|
|
|
public function IsDeductable() |
|
520
|
|
|
{ |
|
521
|
|
|
return true; |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
// ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) |
|
525
|
|
|
// ######################################## *** AJAX related functions |
|
526
|
|
|
/** |
|
527
|
|
|
* some modifiers can be hidden after an ajax update (e.g. if someone enters a discount coupon and it does not exist). |
|
528
|
|
|
* There might be instances where ShowInTable (the starting point) is TRUE and HideInAjaxUpdate return false. |
|
529
|
|
|
* @return Boolean |
|
530
|
|
|
* */ |
|
531
|
|
|
public function HideInAjaxUpdate() |
|
532
|
|
|
{ |
|
533
|
|
|
//we check if the parent wants to hide it... |
|
534
|
|
|
//we need to do this first in case it is being removed. |
|
535
|
|
|
if (parent::HideInAjaxUpdate()) { |
|
536
|
|
|
return true; |
|
537
|
|
|
} |
|
538
|
|
|
// we do NOT hide it if values have been entered |
|
539
|
|
|
if ($this->CouponCodeEntered) { |
|
|
|
|
|
|
540
|
|
|
return false; |
|
541
|
|
|
} |
|
542
|
|
|
return true; |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
// ######################################## *** debug functions |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
class DiscountCouponModifier_Form extends OrderModifierForm |
|
549
|
|
|
{ |
|
550
|
|
|
|
|
551
|
|
|
/** |
|
552
|
|
|
* @var Array |
|
553
|
|
|
* |
|
554
|
|
|
*/ |
|
555
|
|
|
private static $custom_javascript_files = array( |
|
556
|
|
|
"ecommerce_discount_coupon/javascript/DiscountCouponModifier.js" |
|
557
|
|
|
); |
|
558
|
|
|
|
|
559
|
|
|
public static function get_custom_javascript_files() |
|
560
|
|
|
{ |
|
561
|
|
|
$jsFiles = $this->Config()->get("custom_javascript_files"); |
|
|
|
|
|
|
562
|
|
|
if (is_array($jsFiles) && count($jsFiles)) { |
|
563
|
|
|
return $jsFiles; |
|
564
|
|
|
} |
|
565
|
|
|
return null; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
public function __construct($optionalController = null, $name, FieldList $fields, FieldList $actions, $optionalValidator = null) |
|
569
|
|
|
{ |
|
570
|
|
|
parent::__construct($optionalController, $name, $fields, $actions, $optionalValidator); |
|
571
|
|
|
Requirements::themedCSS("DiscountCouponModifier", "ecommerce_discount_coupon"); |
|
572
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/jquery/jquery.js"); |
|
573
|
|
|
Requirements::javascript(THIRDPARTY_DIR . "/jquery-form/jquery.form.js"); |
|
574
|
|
|
//Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js"); |
|
575
|
|
|
//Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"); |
|
576
|
|
|
if ($jsRequirements = $this->Config()->get("custom_javascript_files")) { |
|
577
|
|
|
foreach ($jsRequirements as $js) { |
|
578
|
|
|
Requirements::javascript($js); |
|
579
|
|
|
} |
|
580
|
|
|
} |
|
581
|
|
|
} |
|
582
|
|
|
|
|
583
|
|
|
public function submit(array $data, Form $form, $message = "Order updated", $status = "good") |
|
|
|
|
|
|
584
|
|
|
{ |
|
585
|
|
|
if (isset($data['DiscountCouponCode'])) { |
|
586
|
|
|
$order = ShoppingCart::current_order(); |
|
587
|
|
|
if ($order) { |
|
588
|
|
|
$modifiers = $order->Modifiers('DiscountCouponModifier'); |
|
589
|
|
|
$modifier = $modifiers->First(); |
|
590
|
|
|
if ($modifier) { |
|
591
|
|
|
list($message, $type) = $modifier->updateCouponCodeEntered(Convert::raw2sql($data['DiscountCouponCode'])); |
|
592
|
|
|
$form->addErrorMessage("DiscountCouponCode", $message, $type); |
|
593
|
|
|
return ShoppingCart::singleton()->setMessageAndReturn($message, $type); |
|
594
|
|
|
} |
|
595
|
|
|
} |
|
596
|
|
|
} |
|
597
|
|
|
return ShoppingCart::singleton()->setMessageAndReturn(_t("DiscountCouponModifier.NOTAPPLIED", "Coupon could not be found.", "bad")); |
|
598
|
|
|
} |
|
599
|
|
|
} |
|
600
|
|
|
|