1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This object is attached to any Buyable |
4
|
|
|
* using the ComplexPriceBuyableDecorator |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class ComplexPriceObject extends DataObject |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $db = array( |
|
|
|
|
12
|
|
|
'NewPrice' => 'Currency', |
13
|
|
|
'Percentage' => 'Double', |
14
|
|
|
'Reduction' => 'Currency', |
15
|
|
|
'From' => 'SS_Datetime', |
16
|
|
|
'Until' => 'SS_Datetime', |
17
|
|
|
'NoLongerValid' => 'Boolean' |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
private static $many_many = array( |
|
|
|
|
21
|
|
|
'Groups' => 'Group', |
22
|
|
|
'EcommerceCountries' => 'EcommerceCountry' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $searchable_fields = array( |
|
|
|
|
26
|
|
|
"NoLongerValid" => true, |
27
|
|
|
"From" => true, |
28
|
|
|
"Until" => true |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $field_labels = array( |
|
|
|
|
32
|
|
|
'From' => 'Valid From', |
33
|
|
|
'Until' => 'Valid Until', |
34
|
|
|
'NoLongerValidNice' => 'Valid?' |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
private static $summary_fields = array( |
|
|
|
|
38
|
|
|
'From' => 'Valid From', |
39
|
|
|
'Until' => 'Valid Until', |
40
|
|
|
'AppliesTo' => 'Applies To', |
41
|
|
|
'CalculatedPrice' => 'New Price', |
42
|
|
|
'NoLongerValidNice' => 'Valid' |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
private static $casting = array( |
|
|
|
|
46
|
|
|
'NoLongerValidNice' => 'Varchar', |
47
|
|
|
'Buyable' => 'DataOject', |
48
|
|
|
'Name' => 'Varchar', |
49
|
|
|
'CalculatedPrice' => 'Currency', |
50
|
|
|
'AppliesTo' => 'Text' |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
private static $singular_name = "Price"; |
|
|
|
|
54
|
|
|
|
55
|
|
|
private static $plural_name = "Prices"; |
|
|
|
|
56
|
|
|
|
57
|
|
|
//defaults |
58
|
|
|
private static $default_sort = "\"Until\" DESC"; |
|
|
|
|
59
|
|
|
|
60
|
|
|
public function getCMSFields() |
61
|
|
|
{ |
62
|
|
|
$fields = parent::getCMSFields(); |
63
|
|
|
|
64
|
|
|
$fields->replaceField("From", $from = new DateField("From", "Valid From - add any date and time")); |
65
|
|
|
$fields->replaceField("Until", $until = new DateField("Until", "Valid Until - add any date and time")); |
66
|
|
|
$fields->replaceField("NewPrice", new CurrencyField("NewPrice", "PRICE (OPTION 1 / 3) - only enter if there is a set new price independent of the 'standard' price.")); |
67
|
|
|
$fields->replaceField("Percentage", new NumericField("Percentage", "PERCENTAGE (OPTIONAL 2/ 3) discount from 0 (0% discount) to 100 (100% discount).")); |
68
|
|
|
$fields->replaceField("Reduction", new CurrencyField("Reduction", "REDUCTION (OPTION 3 /3 ) - e.g. if you enter 2.00 then the new price will be the standard product price minus 2.")); |
69
|
|
|
if (!$this->ID) { |
70
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("SaveFirst", "<p>Please save first - and then select security groups / countries</p>")); |
71
|
|
|
$fields->removeByName("NoLongerValid"); |
72
|
|
|
} |
73
|
|
|
if ($groups = Group::get()->count()) { |
|
|
|
|
74
|
|
|
$groups = Group::get(); |
75
|
|
|
$fields->replaceField("Groups", new CheckboxSetField("Groups", "Who", $groups->map()->toArray())); |
76
|
|
|
} else { |
77
|
|
|
$fields->removeByName("Groups"); |
78
|
|
|
} |
79
|
|
View Code Duplication |
if ($ecommerceCountries = EcommerceCountry::get()) { |
|
|
|
|
80
|
|
|
$fields->replaceField("EcommerceCountries", new CheckboxSetField("EcommerceCountries", "Where", $ecommerceCountries->map()->toArray())); |
81
|
|
|
} else { |
82
|
|
|
$fields->removeByName("EcommerceCountries"); |
83
|
|
|
} |
84
|
|
View Code Duplication |
if (DiscountCouponOption::get()->count()) { |
|
|
|
|
85
|
|
|
$fields->replaceField("DiscountCouponOptions", new CheckboxSetField("DiscountCouponOptions", "Discount Coupons", DiscountCouponOption::get()->map()->toArray())); |
86
|
|
|
} else { |
87
|
|
|
$fields->removeByName("DiscountCouponOptions"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$from->setConfig('showcalendar', true); |
91
|
|
|
$until->setConfig('showcalendar', true); |
92
|
|
|
return $fields; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function Buyable() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
return $this->getBuyable(); |
98
|
|
|
} |
99
|
|
|
public function getBuyable() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$array = $this->stat("has_one"); |
102
|
|
|
if ($array && is_array($array) && count($array)) { |
103
|
|
|
foreach ($array as $className) { |
104
|
|
|
$fieldName = $className."ID"; |
105
|
|
|
if (isset($this->$fieldName) && $this->$fieldName > 0) { |
106
|
|
|
return $className::get()->byID($this->$fieldName); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
* works out any price reductions |
115
|
|
|
*/ |
116
|
|
|
public function CalculatedPrice() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
return $this->getCalculatedPrice(); |
119
|
|
|
} |
120
|
|
|
public function getCalculatedPrice() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$buyable = $this->getBuyable(); |
123
|
|
|
if ($this->NewPrice && $this->NewPrice > 0) { |
|
|
|
|
124
|
|
|
$newPrice = $this->NewPrice; |
|
|
|
|
125
|
|
|
} else { |
126
|
|
|
$newPrice = $buyable->Price; |
127
|
|
|
if ($this->Percentage) { |
|
|
|
|
128
|
|
|
$newPrice = $newPrice - ($newPrice * ($this->Percentage / 100)); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
if ($this->Reduction) { |
|
|
|
|
131
|
|
|
$newPrice = $newPrice - $this->Reduction; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
return $newPrice; |
134
|
|
|
} |
135
|
|
|
if ($newPrice < 0) { |
136
|
|
|
$newPrice = 0; |
137
|
|
|
} |
138
|
|
|
return $newPrice; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function AppliesTo() |
142
|
|
|
{ |
143
|
|
|
return $this->getAppliesTo(); |
144
|
|
|
} |
145
|
|
|
public function getAppliesTo() |
146
|
|
|
{ |
147
|
|
|
$appliesTo = array(); //; |
148
|
|
|
if ($this->Groups()) { |
|
|
|
|
149
|
|
|
foreach ($this->Groups() as $group) { |
|
|
|
|
150
|
|
|
$appliesTo[] = $group->getTitle(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
if ($this->EcommerceCountries()) { |
|
|
|
|
154
|
|
|
foreach ($this->EcommerceCountries() as $ecommerceCountries) { |
|
|
|
|
155
|
|
|
$appliesTo[] = $ecommerceCountries->getTitle(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
if (!count($appliesTo)) { |
159
|
|
|
$appliesTo[] = "Everyone"; |
160
|
|
|
} |
161
|
|
|
return implode(", ", $appliesTo) . "."; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function NoLongerValidNice() |
165
|
|
|
{ |
166
|
|
|
return $this->getNoLongerValidNice(); |
167
|
|
|
} |
168
|
|
|
public function getNoLongerValidNice() |
169
|
|
|
{ |
170
|
|
|
$nowTS = strtotime("now"); |
171
|
|
|
$untilTS = strtotime($this->Until); |
|
|
|
|
172
|
|
|
if ($this->NoLongerValid || $untilTS < $nowTS) { |
|
|
|
|
173
|
|
|
if ($untilTS < $nowTS) { |
174
|
|
|
$this->NoLongerValid; |
|
|
|
|
175
|
|
|
$this->write(); |
176
|
|
|
return "expired"; |
177
|
|
|
} |
178
|
|
|
return "no longer valid"; |
179
|
|
|
} else { |
180
|
|
|
return "current"; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function validate() |
185
|
|
|
{ |
186
|
|
|
$errors = array(); |
187
|
|
|
if (strtotime($this->From) < strtotime("1 jan 2000")) { |
|
|
|
|
188
|
|
|
$errors[] = "The FROM field needs to be after 1 Jan 2000"; |
189
|
|
|
} |
190
|
|
|
if (strtotime($this->Until) < strtotime("1 jan 2000")) { |
|
|
|
|
191
|
|
|
$errors[] = "The UNTIL field needs to be after 1 Jan 2000"; |
192
|
|
|
} |
193
|
|
|
if (strtotime($this->Until) < strtotime($this->From)) { |
|
|
|
|
194
|
|
|
$errors[] = "The UNTIL field needs to be after the FROM field"; |
195
|
|
|
} |
196
|
|
|
if ($this->Percentage < 0 || $this->Percentage > 100) { |
|
|
|
|
197
|
|
|
$errors[] = "The PERCENTAGE field needs to be between 0 and 100"; |
198
|
|
|
} |
199
|
|
|
if (count($errors)== 0) { |
200
|
|
|
return new ValidationResult(); |
201
|
|
|
} else { |
202
|
|
|
return new ValidationResult(false, "Please check: ".implode("; ", $errors)."."); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function Name() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
return $this->getName(); |
209
|
|
|
} |
210
|
|
|
public function getName() |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
if ($buyable = $this->getBuyable()) { |
213
|
|
|
return $buyable->getTitle(); |
214
|
|
|
} |
215
|
|
|
return "no name"; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function onBeforeWrite() |
219
|
|
|
{ |
220
|
|
|
parent::onBeforeWrite(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
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.