|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Holds prices for individual countries for |
|
5
|
|
|
* Buyables. |
|
6
|
|
|
* |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
class CountryPrice extends DataObject |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
// CURRENCY LIST AND STATIC FUNCTIONS |
|
14
|
|
|
|
|
15
|
|
|
private static $db = array( |
|
|
|
|
|
|
16
|
|
|
'Price' => 'Currency', |
|
17
|
|
|
'Country' => 'Varchar(2)', |
|
18
|
|
|
'Currency' => 'Varchar(3)', |
|
19
|
|
|
'ObjectClass' => 'Varchar', |
|
20
|
|
|
'ObjectID' => 'Int' |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
private static $field_labels = array( |
|
|
|
|
|
|
24
|
|
|
'Currency' => 'Currency Code', |
|
25
|
|
|
'Country' => 'Country Code', |
|
26
|
|
|
'ObjectClass' => 'Buyable Name', |
|
27
|
|
|
'ObjectID' => 'Buyable ID' |
|
28
|
|
|
); |
|
29
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
30
|
|
|
'BuyableName' => 'Buyable', |
|
31
|
|
|
'CountryName' => 'Country', |
|
32
|
|
|
'FullPrice' => 'Price' |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
private static $casting = array( |
|
|
|
|
|
|
36
|
|
|
'BuyableName' => 'Varchar', |
|
37
|
|
|
'Title' => 'Varchar', |
|
38
|
|
|
'CountryName' => 'Varchar', |
|
39
|
|
|
'FullPrice' => 'Varchar' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
private static $indexes = array( |
|
|
|
|
|
|
43
|
|
|
'Unique' => array( |
|
44
|
|
|
'type' => 'unique', |
|
45
|
|
|
'value' => 'Country,ObjectClass,ObjectID' |
|
46
|
|
|
), |
|
47
|
|
|
'Currency' => true |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
private static $searchable_fields = array( |
|
|
|
|
|
|
51
|
|
|
'Price' => 'PartialMatchFilter', |
|
52
|
|
|
'Country' => 'PartialMatchFilter', |
|
53
|
|
|
'Currency' => 'PartialMatchFilter' |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* caching only variable |
|
58
|
|
|
* @var EcommerceCountry | null |
|
59
|
|
|
*/ |
|
60
|
|
|
private $_myBuyable = null; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* the buyable we relate to |
|
64
|
|
|
* return DataObject | null |
|
65
|
|
|
*/ |
|
66
|
|
|
public function Buyable() |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
if (! $this->_myBuyable) { |
|
69
|
|
|
$className = $this->ObjectClass; |
|
|
|
|
|
|
70
|
|
|
if (class_exists($this->ObjectClass)) { |
|
|
|
|
|
|
71
|
|
|
$this->_myBuyable = $className::get()->byID($this->ObjectID); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->_myBuyable; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* caching only variable |
|
80
|
|
|
* @var EcommerceCountry | null |
|
81
|
|
|
*/ |
|
82
|
|
|
private $_myCountryObject = null; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* |
|
86
|
|
|
* return EcommerceCountry | null |
|
87
|
|
|
*/ |
|
88
|
|
|
public function CountryObject() |
|
89
|
|
|
{ |
|
90
|
|
|
if (! $this->_myCountryObject) { |
|
91
|
|
|
if ($this->Country) { |
|
|
|
|
|
|
92
|
|
|
$this->_myCountryObject = EcommerceCountry::get() |
|
|
|
|
|
|
93
|
|
|
->filter(array("Code" => $this->Country)) |
|
|
|
|
|
|
94
|
|
|
->First(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->_myCountryObject; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* return EcommerceCountry | null |
|
104
|
|
|
*/ |
|
105
|
|
|
public function CurrencyObject() |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->Country) { |
|
|
|
|
|
|
108
|
|
|
return EcommerceCurrency::get()->filter(array("Code" => $this->Currency))->First(); |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* casted variable |
|
114
|
|
|
* @return String |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getBuyableName() |
|
117
|
|
|
{ |
|
118
|
|
|
if ($obj = $this->Buyable()) { |
|
119
|
|
|
return $obj->Title; |
|
120
|
|
|
} |
|
121
|
|
|
return "ERROR: Object not found"; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* casted variable |
|
126
|
|
|
* @return String |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getTitle() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->getBuyableName()." // ".$this->getCountryName()."// ".$this->getFullPrice(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* casted variable |
|
135
|
|
|
* @return String |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getCountryName() |
|
138
|
|
|
{ |
|
139
|
|
|
return EcommerceCountry::find_title($this->Country); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* casted variable |
|
144
|
|
|
* returns nicely formatted price.. |
|
145
|
|
|
* @return String |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getFullPrice() |
|
148
|
|
|
{ |
|
149
|
|
|
return "$this->Price $this->Currency" . ($this->isObsolete() ? ' (obsolete!)' : ''); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getCMSFields() |
|
153
|
|
|
{ |
|
154
|
|
|
$fields = parent::getCMSFields(); |
|
155
|
|
|
// This works only because only NZ uses NZD |
|
156
|
|
|
$countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map('Code', 'Name')->toArray(); |
|
157
|
|
|
unset($countries[EcommerceConfig::get('EcommerceCountry', 'default_country_code')]); |
|
158
|
|
|
$field = DropdownField::create('Country', 'Country', $countries); |
|
159
|
|
|
$fields->replaceField('Country', $field); |
|
160
|
|
|
|
|
161
|
|
|
if ($this->ID) { |
|
162
|
|
|
$fields->makeFieldReadonly('Country'); |
|
163
|
|
|
$list = EcommerceCurrency::ecommerce_currency_list()->exclude(array("Code" => $this->Currency)); |
|
|
|
|
|
|
164
|
|
|
if ($list->count()) { |
|
165
|
|
|
$listArray = array($this->Currency => $this->Currency) + $list->map("Code", "Name")->toArray(); |
|
|
|
|
|
|
166
|
|
|
} else { |
|
167
|
|
|
$listArray = array($this->Currency => $this->Currency); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
$fields->replaceField( |
|
170
|
|
|
'Currency', |
|
171
|
|
|
DropdownField::create( |
|
172
|
|
|
'Currency', |
|
173
|
|
|
'Currency', |
|
174
|
|
|
$listArray |
|
175
|
|
|
) |
|
176
|
|
|
); |
|
177
|
|
|
} else { |
|
178
|
|
|
$fields->removeByName('Currency'); |
|
179
|
|
|
} |
|
180
|
|
|
$fields->removeByName('ObjectClass'); |
|
181
|
|
|
$fields->removeByName('ObjectID'); |
|
182
|
|
|
if (self::$cms_object) { |
|
183
|
|
|
$fields->addFieldToTab("Root.Main", new HiddenField('MyObjectClass', '', self::$cms_object->ClassName)); |
|
184
|
|
|
$fields->addFieldToTab("Root.Main", new HiddenField('MyObjectID', '', self::$cms_object->ID)); |
|
185
|
|
|
} else { |
|
186
|
|
|
//to do BuyableSelectField |
|
187
|
|
|
} |
|
188
|
|
|
$buyable = $this->Buyable(); |
|
189
|
|
|
if ($buyable && $buyable->exists()) { |
|
190
|
|
|
$fields->addFieldToTab( |
|
191
|
|
|
'Root.Main', |
|
192
|
|
|
$buyableLink = ReadonlyField::create( |
|
193
|
|
|
'ProductOrService', |
|
194
|
|
|
'Product or Service', |
|
195
|
|
|
'<a href="'.$buyable->CMSEditLink().'">'.$this->getBuyableName().'</a>' |
|
196
|
|
|
) |
|
197
|
|
|
); |
|
198
|
|
|
$buyableLink->dontEscape = true; |
|
199
|
|
|
} |
|
200
|
|
|
$fields->addFieldsToTab( |
|
201
|
|
|
'Root.Debug', |
|
202
|
|
|
array( |
|
203
|
|
|
ReadonlyField::create('ObjectClass', 'ObjectClass'), |
|
204
|
|
|
ReadonlyField::create('ObjectID', 'ObjectID') |
|
205
|
|
|
) |
|
206
|
|
|
); |
|
207
|
|
|
return $fields; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
private static $cms_object = null; |
|
211
|
|
|
|
|
212
|
|
|
//MUST KEEP |
|
213
|
|
|
public static function set_cms_object($o) |
|
214
|
|
|
{ |
|
215
|
|
|
self::$cms_object = $o; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
public function canEdit($member = null) |
|
219
|
|
|
{ |
|
220
|
|
|
$canEdit = parent::canEdit(); |
|
221
|
|
|
if (! $canEdit) { |
|
222
|
|
|
$member = Member::currentUser(); |
|
223
|
|
|
$distributor = $member->Distributor(); |
|
224
|
|
|
if ($distributor->exists()) { |
|
225
|
|
|
return $distributor->getComponents('Countries', "\"Code\" = '$this->Country'")->Count() > 0; |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
return $canEdit; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* We use validate as an onBeforeWrite as well because in this case it makes sense |
|
233
|
|
|
* as in the validation process we add stuff... |
|
234
|
|
|
* @return ValidationResult |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function validate() |
|
237
|
|
|
{ |
|
238
|
|
|
$result = parent::validate(); |
|
239
|
|
|
if (! $this->ObjectClass && isset($_REQUEST["MyObjectClass"])) { |
|
|
|
|
|
|
240
|
|
|
if (class_exists($_REQUEST["MyObjectClass"])) { |
|
241
|
|
|
$this->ObjectClass = Convert::raw2sql($_REQUEST["MyObjectClass"]); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
if (! $this->ObjectID && isset($_REQUEST["MyObjectID"])) { |
|
|
|
|
|
|
245
|
|
|
$this->ObjectID = intval($_REQUEST["MyObjectID"]); |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
//check for duplicates in case it has not been created yet... |
|
248
|
|
|
if (! $this->ObjectClass || ! $this->ObjectID) { |
|
|
|
|
|
|
249
|
|
|
$result->error('Object could not be created. Please contact your developer.'); |
|
250
|
|
|
return $result; |
|
251
|
|
|
} |
|
252
|
|
|
$currencyPerCountry = CountryPrice_EcommerceCurrency::get_currency_per_country(); |
|
253
|
|
|
if (!isset($currencyPerCountry[$this->Country])) { |
|
|
|
|
|
|
254
|
|
|
$result->error("Can not find currency for this country '".$this->Country."'"); |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
$this->Currency = $currencyPerCountry[$this->Country]; |
|
|
|
|
|
|
257
|
|
|
$duplicates = CountryPrice::get() |
|
258
|
|
|
->exclude(array("ID" => $this->ID - 0)) |
|
259
|
|
|
->filter(array("ObjectClass" => $this->ObjectClass, "ObjectID" => $this->ObjectID, "Country" => $this->Country)); |
|
|
|
|
|
|
260
|
|
|
if ($duplicates->count()) { |
|
261
|
|
|
$result->error('You can not add this price for this country because a price for this country already exists.'); |
|
262
|
|
|
} |
|
263
|
|
|
return $result; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Returns if the currency is an old currency not used anymore. |
|
268
|
|
|
* @return Boolean |
|
|
|
|
|
|
269
|
|
|
*/ |
|
270
|
|
|
public function isObsolete() |
|
271
|
|
|
{ |
|
272
|
|
|
$currencyPerCountry = CountryPrice_EcommerceCurrency::get_currency_per_country(); |
|
273
|
|
|
if (isset($currencyPerCountry[$this->Country])) { |
|
|
|
|
|
|
274
|
|
|
return $currencyPerCountry[$this->Country] != $this->Currency; |
|
|
|
|
|
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* name of session variable used to set Country |
|
280
|
|
|
* @var String |
|
281
|
|
|
*/ |
|
282
|
|
|
private static $location_param = 'Location'; |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* country for user |
|
287
|
|
|
* @var String |
|
288
|
|
|
*/ |
|
289
|
|
|
private static $location_country; |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* returns Country code |
|
293
|
|
|
* @return string |
|
294
|
|
|
*/ |
|
295
|
|
|
public static function get_location_country() |
|
296
|
|
|
{ |
|
297
|
|
|
return self::$location_country; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|