1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Nicolaas [at] sunnysideup.co.nz |
5
|
|
|
* @package: ecommerce |
6
|
|
|
* @sub-package: ecommerce_delivery |
7
|
|
|
* @description: allows you to add a modifier at checkout where the customer is prompted to |
8
|
|
|
* add a donation rounding up to the next round number. |
9
|
|
|
* |
10
|
|
|
* The trick here is to work out the value of the donation |
11
|
|
|
* from the total without the donation and then add it to the donation |
12
|
|
|
* to get to a round numer. |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class AnyPriceRoundUpDonationModifier extends OrderModifier |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
// ######################################## *** model defining static variables (e.g. $db, $has_one) |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* add extra fields as you need them. |
22
|
|
|
* |
23
|
|
|
**/ |
24
|
|
|
private static $db = array( |
|
|
|
|
25
|
|
|
"ModifierTotalExcludingDonation" => "Currency", |
26
|
|
|
"SubTotal" => "Currency", |
27
|
|
|
"OtherValue" => "Currency", |
28
|
|
|
"AddDonation" => "Boolean" |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
// ######################################## *** cms variables + functions (e.g. getCMSFields, $searchableFields) |
32
|
|
|
|
33
|
|
|
public function getCMSFields() |
34
|
|
|
{ |
35
|
|
|
$fields = parent::getCMSFields(); |
36
|
|
|
return $fields; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private static $singular_name = "Round Up Donation"; |
|
|
|
|
40
|
|
|
public function i18n_singular_name() |
41
|
|
|
{ |
42
|
|
|
return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATION", "Round Up Donation"); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private static $plural_name = "Round Up Donations"; |
|
|
|
|
46
|
|
|
public function i18n_plural_name() |
47
|
|
|
{ |
48
|
|
|
return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATIONS", "Round Up Donations"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Maximum Round Up |
56
|
|
|
* to which the donation should round. |
57
|
|
|
* +1 = nearest 10, e.g. 73.45 rounds to 80 |
58
|
|
|
* 0 = nearest rounded integer - e.g. 73.45 rounds to 74 |
59
|
|
|
* -1 = nearest 10 cents - e.g. 73.45 rounds to 73.50 |
60
|
|
|
* |
61
|
|
|
* @var Int |
62
|
|
|
*/ |
63
|
|
|
private static $precision = 1; |
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Maximum Round Up - modifier will ensure that the round up is no more |
67
|
|
|
* than the number specified here. |
68
|
|
|
* @var Int |
69
|
|
|
*/ |
70
|
|
|
private static $maximum_round_up = 5; |
|
|
|
|
71
|
|
|
|
72
|
|
|
private static $round_up_even_if_there_is_nothing_to_round = true; |
|
|
|
|
73
|
|
|
|
74
|
|
|
private static $include_form_in_order_table = true; |
75
|
|
|
|
76
|
|
|
// ######################################## *** CRUD functions (e.g. canEdit) |
77
|
|
|
// ######################################## *** init and update functions |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* For all modifers with their own database fields, we need to include this... |
81
|
|
|
* It will update each of the fields. |
82
|
|
|
* With this, we also need to create the methods |
83
|
|
|
* Live{functionName} |
84
|
|
|
* e.g LiveMyField() and LiveMyReduction() in this case... |
85
|
|
|
* @param Bool $force - run it, even if it has run already |
86
|
|
|
*/ |
87
|
|
|
public function runUpdate($force = false) |
88
|
|
|
{ |
89
|
|
|
$this->checkField("AddDonation"); |
90
|
|
|
$this->checkField("OtherValue"); |
91
|
|
|
$this->checkField("SubTotal"); |
92
|
|
|
$this->checkField("ModifierTotalExcludingDonation"); |
93
|
|
|
parent::runUpdate($force); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* allows you to save a new value AddDonation |
98
|
|
|
* @param Boolean $b |
99
|
|
|
*/ |
100
|
|
|
public function updateAddDonation($b) |
101
|
|
|
{ |
102
|
|
|
$this->AddDonation = $b ? 1 : 0; |
|
|
|
|
103
|
|
|
$this->write(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* allows you to save a new value OtherValue |
108
|
|
|
* @param float |
109
|
|
|
*/ |
110
|
|
|
public function updateOtherValue($f) |
111
|
|
|
{ |
112
|
|
|
$this->OtherValue = $f; |
|
|
|
|
113
|
|
|
$this->write(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
// ######################################## *** form functions (e. g. Showform and getform) |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* standard OrderModifier Method |
121
|
|
|
* Should we show a form in the checkout page for this modifier? |
122
|
|
|
*/ |
123
|
|
|
public function ShowForm() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
/*$ajaxObject = $this->AJAXDefinitions(); |
|
|
|
|
126
|
|
|
//TableValue is a database value |
127
|
|
|
$tableID = $ajaxObject->TableID(); |
128
|
|
|
if(!$this->hasDonation()) { |
129
|
|
|
Requirements::customScript("jQuery(document).ready(function() {jQuery(\"#".$tableID."\").hide();});", "hide$tableID"); |
130
|
|
|
}*/ |
131
|
|
|
return $this->Order()->Items(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Should the form be included in the editable form |
136
|
|
|
* on the checkout page? |
137
|
|
|
* @return Boolean |
138
|
|
|
*/ |
139
|
|
|
public function ShowFormInEditableOrderTable() |
140
|
|
|
{ |
141
|
|
|
return ($this->ShowForm() && self::$include_form_in_order_table) ? true : false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* standard OrderModifier Method |
146
|
|
|
* This method returns the form for the checkout page. |
147
|
|
|
* @param Object $controller = Controller object for form |
|
|
|
|
148
|
|
|
* @return Object - AnyPriceRoundUpDonationModifier |
149
|
|
|
*/ |
150
|
|
|
public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null) |
151
|
|
|
{ |
152
|
|
|
$fields = new FieldList(); |
153
|
|
|
$fields->push($this->headingField()); |
154
|
|
|
$fields->push($this->descriptionField()); |
155
|
|
|
$maxRoundUpObject = DBField::create_field('Currency', Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up')); |
156
|
|
|
$checkFieldTitle = sprintf( |
157
|
|
|
_t("AnyPriceRoundUpDonationModifier.ADDDONATION", "Add round up donation (maximum added %s)?"), |
158
|
|
|
$maxRoundUpObject->Nice() |
159
|
|
|
); |
160
|
|
|
$checkField = new DropdownField( |
161
|
|
|
'AddDonation', |
162
|
|
|
$checkFieldTitle, |
163
|
|
|
array( |
164
|
|
|
_t("AnyPriceRoundUpDonationModifier.NO", 'No'), |
165
|
|
|
_t("AnyPriceRoundUpDonationModifier.YES", 'Yes') |
166
|
|
|
), |
167
|
|
|
$this->AddDonation |
|
|
|
|
168
|
|
|
); |
169
|
|
|
$fields->push($checkField); |
170
|
|
|
$fields->push(new NumericFIeld('OtherValue', _t("AnyPriceRoundUpDonationModifier.OTHERVALUE", "Other Value"), $this->OtherValue)); |
|
|
|
|
171
|
|
|
$actions = new FieldList( |
172
|
|
|
new FormAction('submit', 'Update Order') |
173
|
|
|
); |
174
|
|
|
return new AnyPriceRoundUpDonationModifier_Form($optionalController, 'AnyPriceRoundUpDonationModifier', $fields, $actions, $optionalValidator); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* This has to be set to true, because it can be added by form using AJAX. |
182
|
|
|
* @return Boolean |
183
|
|
|
*/ |
184
|
|
|
public function ShowInTable() |
185
|
|
|
{ |
186
|
|
|
if ($this->Order()->IsSubmitted()) { |
187
|
|
|
return $this->TableValue > 0; |
|
|
|
|
188
|
|
|
} |
189
|
|
|
return true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Removed via form instead. |
194
|
|
|
* @return Boolean |
195
|
|
|
*/ |
196
|
|
|
public function CanBeRemoved() |
197
|
|
|
{ |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// ######################################## *** inner calculations.... USES CALCULATED VALUES |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Works out if there is a donation at all. |
206
|
|
|
* |
207
|
|
|
*@return Boolean |
208
|
|
|
*/ |
209
|
|
|
protected function hasDonation() |
210
|
|
|
{ |
211
|
|
|
if (($this->LiveAddDonation() && Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up') > 0) || $this->OtherValue > 0) { |
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
/** |
217
|
|
|
* Works out the total round up amount, using both the |
218
|
|
|
* sub-total and the modifier total. |
219
|
|
|
* |
220
|
|
|
*@return Float |
221
|
|
|
*/ |
222
|
|
|
protected function workOutRoundUpAmount() |
223
|
|
|
{ |
224
|
|
|
if ($this->hasDonation()) { |
225
|
|
|
if ($this->OtherValue > 0) { |
|
|
|
|
226
|
|
|
$actualAdditionToTotal = $this->OtherValue; |
|
|
|
|
227
|
|
|
} else { |
228
|
|
|
$totalExcludingDonation = $this->LiveSubTotal() + $this->LiveModifierTotalExcludingDonation(); |
229
|
|
|
$precisionMultiplier = pow(10, Config::inst()->get('AnyPriceRoundUpDonationModifier', 'precision')); |
230
|
|
|
$totalMultipliedByPrecision = $totalExcludingDonation / $precisionMultiplier; |
231
|
|
|
$roundedTotalMultipliedByPrecision = ceil($totalMultipliedByPrecision); |
232
|
|
|
$actualAdditionToTotal = ($roundedTotalMultipliedByPrecision * $precisionMultiplier) - $totalExcludingDonation; |
233
|
|
|
while ($actualAdditionToTotal > Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up') && $actualAdditionToTotal > 0) { |
234
|
|
|
$actualAdditionToTotal = $actualAdditionToTotal - Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up'); |
235
|
|
|
} |
236
|
|
|
if (Config::inst()->get('AnyPriceRoundUpDonationModifier', 'round_up_even_if_there_is_nothing_to_round') && $actualAdditionToTotal == 0) { |
237
|
|
|
$actualAdditionToTotal = Config::inst()->get("AnyPriceRoundUpDonationModifier", 'maximum_round_up'); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} else { |
241
|
|
|
$actualAdditionToTotal = 0; |
242
|
|
|
} |
243
|
|
|
return $actualAdditionToTotal; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
// ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* if we want to change the default value for the Name field |
251
|
|
|
* (defined in the OrderModifer class) then we can do this |
252
|
|
|
* as shown in the method below. |
253
|
|
|
* You may choose to return an empty string or just a standard message. |
254
|
|
|
* |
255
|
|
|
* |
256
|
|
|
**/ |
257
|
|
|
protected function LiveName() |
258
|
|
|
{ |
259
|
|
|
if ($this->OtherValue > 0) { |
|
|
|
|
260
|
|
|
return _t("AnyPriceRoundUpDonationModifier.DONATION", "Donation"); |
261
|
|
|
} elseif ($this->hasDonation()) { |
262
|
|
|
return _t("AnyPriceRoundUpDonationModifier.ROUNDUPDONATION", "Round up donation"); |
263
|
|
|
} else { |
264
|
|
|
return _t("AnyPriceRoundUpDonationModifier.NOROUNDUPDONATION", "No round up donation added"); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* |
270
|
|
|
* @return Boolean |
271
|
|
|
**/ |
272
|
|
|
protected function LiveAddDonation() |
273
|
|
|
{ |
274
|
|
|
return $this->AddDonation; |
|
|
|
|
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* |
279
|
|
|
* @return Float |
280
|
|
|
**/ |
281
|
|
|
protected function LiveOtherValue() |
282
|
|
|
{ |
283
|
|
|
return $this->OtherValue; |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Work out sub total amount for order |
288
|
|
|
* @return float |
289
|
|
|
**/ |
290
|
|
|
protected function LiveSubTotal() |
291
|
|
|
{ |
292
|
|
|
if ($this->hasDonation()) { |
293
|
|
|
$order = $this->Order(); |
294
|
|
|
return $order->SubTotal(); |
295
|
|
|
} else { |
296
|
|
|
return 0; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Work out modifier total excluding donation |
302
|
|
|
* @return float |
|
|
|
|
303
|
|
|
**/ |
304
|
|
|
protected function LiveModifierTotalExcludingDonation() |
305
|
|
|
{ |
306
|
|
|
if ($this->hasDonation()) { |
307
|
|
|
$modifiersTotal = 0; |
308
|
|
|
$order = $this->Order(); |
309
|
|
|
if ($order) { |
310
|
|
|
if ($modifiers = $order->Modifiers()) { |
311
|
|
|
foreach ($modifiers as $modifier) { |
312
|
|
|
if (!$modifier->IsRemoved()) { //we just doubledouble-check this... |
313
|
|
|
if ($modifier instanceof $this->ClassName) { |
314
|
|
|
$totalForModifier = 0; |
315
|
|
|
} else { |
316
|
|
|
$totalForModifier = $modifier->CalculationTotal(); |
317
|
|
|
} |
318
|
|
|
$modifiersTotal += floatval($totalForModifier); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
return $modifiersTotal; |
324
|
|
|
} else { |
325
|
|
|
return 0; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
protected function LiveCalculatedTotal() |
330
|
|
|
{ |
331
|
|
|
if ($this->hasDonation()) { |
332
|
|
|
return $this->workOutRoundUpAmount(); |
333
|
|
|
} else { |
334
|
|
|
return 0; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function LiveTableValue() |
339
|
|
|
{ |
340
|
|
|
return $this->LiveCalculatedTotal(); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
|
344
|
|
|
// ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) |
345
|
|
|
|
346
|
|
|
private static $table_sub_title; |
|
|
|
|
347
|
|
|
|
348
|
|
|
public function getTableSubTitle() |
349
|
|
|
{ |
350
|
|
|
return _t('AnyPriceRoundUpDonationModifier.TABLESUBTITLE', $this->stat('table_sub_title')); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) |
354
|
|
|
|
355
|
|
|
public function onBeforeWrite() |
356
|
|
|
{ |
357
|
|
|
parent::onBeforeWrite(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
// ######################################## *** AJAX related functions |
362
|
|
|
/** |
363
|
|
|
* some modifiers can be hidden after an ajax update (e.g. if someone enters a discount coupon and it does not exist). |
364
|
|
|
* There might be instances where ShowInTable (the starting point) is TRUE and HideInAjaxUpdate return false. |
365
|
|
|
*@return Boolean |
366
|
|
|
**/ |
367
|
|
|
public function HideInAjaxUpdate() |
368
|
|
|
{ |
369
|
|
|
//we check if the parent wants to hide it... |
370
|
|
|
//we need to do this first in case it is being removed. |
371
|
|
|
if (parent::HideInAjaxUpdate()) { |
372
|
|
|
return true; |
373
|
|
|
} |
374
|
|
|
// we do NOT hide it if values have been entered |
375
|
|
|
if ($this->hasDonation() || $this->ShowFormInEditableOrderTable()) { |
|
|
|
|
376
|
|
|
return false; |
377
|
|
|
} |
378
|
|
|
return true; |
379
|
|
|
} |
380
|
|
|
// ######################################## *** debug functions |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
class AnyPriceRoundUpDonationModifier_Form extends OrderModifierForm |
|
|
|
|
384
|
|
|
{ |
385
|
|
|
public function __construct($optionalController = null, $name, $fields, $actions, $optionalValidator = null) |
|
|
|
|
386
|
|
|
{ |
387
|
|
|
parent::__construct($optionalController, $name, $fields, $actions, $optionalValidator); |
388
|
|
|
Requirements::javascript("ecommerce_anypriceproduct/javascript/AnyPriceRoundUpDonationModifier.js"); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function submit(array $data, Form $form, $message = "order updated", $status = "good") |
|
|
|
|
392
|
|
|
{ |
393
|
|
|
$order = ShoppingCart::current_order(); |
394
|
|
|
if ($order) { |
395
|
|
|
if ($modifiers = $order->Modifiers("AnyPriceRoundUpDonationModifier")) { |
396
|
|
|
$msg = ""; |
397
|
|
|
foreach ($modifiers as $modifier) { |
398
|
|
|
if (isset($data['AddDonation']) && $data['AddDonation']) { |
399
|
|
|
$modifier->updateAddDonation(true); |
400
|
|
|
$msg .= _t("AnyPriceRoundUpDonationModifier.UPDATED", "Round up donation added - THANK YOU."); |
401
|
|
|
} else { |
402
|
|
|
$modifier->updateAddDonation(false); |
403
|
|
|
$msg .= _t("AnyPriceRoundUpDonationModifier.REMOVED", "Round up donation removed."); |
404
|
|
|
} |
405
|
|
View Code Duplication |
if (isset($data['OtherValue'])) { |
|
|
|
|
406
|
|
|
$modifier->updateOtherValue(floatval($data['OtherValue'])); |
407
|
|
|
if (floatval($data['OtherValue']) > 0) { |
408
|
|
|
//here we replace the message! |
409
|
|
|
$msg = _t("AnyPriceRoundUpDonationModifier.UPDATED_OTHER", "Added donation - THANK YOU."); |
410
|
|
|
} |
411
|
|
|
} else { |
412
|
|
|
$modifier->updateOtherValue(0); |
413
|
|
|
} |
414
|
|
|
$modifier->write(); |
415
|
|
|
} |
416
|
|
|
return ShoppingCart::singleton()->setMessageAndReturn($msg, "good"); |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
return ShoppingCart::singleton()->setMessageAndReturn(_t("AnyPriceRoundUpDonationModifier.NOTUPDATED", "Could not update the round up donation status.", "bad")); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.