1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeClient; |
6
|
|
|
use SilverStripe\Forms\DropdownField; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\HeaderField; |
9
|
|
|
use SilverStripe\Forms\OptionsetField; |
10
|
|
|
use SilverStripe\Forms\ReadonlyField; |
11
|
|
|
use SilverStripe\ORM\DataObject; |
12
|
|
|
use SilverStripe\Security\Permission; |
13
|
|
|
|
14
|
|
|
class ProductCategory extends DataObject |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $db = array( |
|
|
|
|
20
|
|
|
'Title' => 'Varchar(255)', |
21
|
|
|
'Code' => 'Varchar(50)', |
22
|
|
|
'DeliveryType' => 'Varchar(50)', |
23
|
|
|
'MaxDownloads' => 'Int', |
24
|
|
|
'MaxDownloadsTime' => 'Int', |
25
|
|
|
'DefaultWeight' => 'Float', |
26
|
|
|
'DefaultWeightUnit' => 'Enum("LBS, KBS", "LBS")', |
27
|
|
|
'DefaultLengthUnit' => 'Enum("in, cm", "in")', |
28
|
|
|
'ShippingFlatRate' => 'Currency', |
29
|
|
|
'ShippingFlatRateType' => 'Varchar(50)', |
30
|
|
|
'HandlingFeeType' => 'Varchar(50)', |
31
|
|
|
'HandlingFee' => 'Currency', |
32
|
|
|
'HandlingFeePercentage' => 'Decimal', |
33
|
|
|
'HandlingFeeMinimum' => 'Currency', |
34
|
|
|
'DiscountType' => 'Varchar(50)', |
35
|
|
|
'DiscountName' => 'Varchar(50)', |
36
|
|
|
'DiscountDetails' => 'Varchar(200)', |
37
|
|
|
'CustomsValue' => 'Currency', |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private static $singular_name = 'FoxyCart Category'; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private static $plural_name = 'FoxyCart Categories'; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private static $description = 'Set the FoxyCart Category on a Product'; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private static $summary_fields = array( |
|
|
|
|
59
|
|
|
'Title' => 'Name', |
60
|
|
|
'Code' => 'Code' |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private static $indexes = array( |
|
|
|
|
67
|
|
|
'Code' => true |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private static $table_name = 'FS_ProductCategory'; |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return FieldList |
77
|
|
|
*/ |
78
|
|
|
public function getCMSFields() |
79
|
|
|
{ |
80
|
|
|
$fields = parent::getCMSFields(); |
81
|
|
|
|
82
|
|
|
if ($this->ID) { |
83
|
|
|
if ($this->Title == 'Default') { |
84
|
|
|
$fields->replaceField( |
85
|
|
|
'Title', |
86
|
|
|
ReadonlyField::create('Title') |
|
|
|
|
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$fields->replaceField( |
91
|
|
|
'Code', |
92
|
|
|
ReadonlyField::create('Code') |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), 'DeliveryType'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$fields->replaceField( |
99
|
|
|
'DeliveryType', |
100
|
|
|
OptionsetField::create('DeliveryType', 'Delivery Type', $this->getShippingOptions()) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$fields->dataFieldByName('MaxDownloads') |
104
|
|
|
->displayIf('DeliveryType')->isEqualTo('downloaded'); |
105
|
|
|
|
106
|
|
|
$fields->dataFieldByName('MaxDownloadsTime') |
107
|
|
|
->displayIf('DeliveryType')->isEqualTo('downloaded'); |
108
|
|
|
|
109
|
|
|
$fields->dataFieldByName('DefaultWeight') |
110
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
111
|
|
|
|
112
|
|
|
$fields->dataFieldByName('DefaultWeightUnit') |
113
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
114
|
|
|
|
115
|
|
|
$fields->dataFieldByName('DefaultLengthUnit') |
116
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
117
|
|
|
|
118
|
|
|
$fields->dataFieldByName('ShippingFlatRate') |
119
|
|
|
->displayIf('DeliveryType')->isEqualTo('flat_rate'); |
120
|
|
|
|
121
|
|
|
$fields->replaceField( |
122
|
|
|
'ShippingFlatRateType', |
123
|
|
|
DropdownField::create('ShippingFlatRateType', 'Flat Rate Type', $this->getShippingFlatRateTypes()) |
124
|
|
|
->setEmptyString('') |
125
|
|
|
->displayIf('DeliveryType')->isEqualTo('flat_rate')->end() |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$fields->insertBefore(HeaderField::create('HandlingHD', 'Handling Fees and Discounts', 3), 'HandlingFeeType'); |
129
|
|
|
|
130
|
|
|
$fields->replaceField( |
131
|
|
|
'HandlingFeeType', |
132
|
|
|
DropdownField::create('HandlingFeeType', 'Handling Fee Type', $this->getHandlingFeeTypes()) |
133
|
|
|
->setEmptyString('') |
134
|
|
|
->setDescription('This determines what type of Handling Fee you would like to use.') |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$fields->dataFieldByName('HandlingFee') |
138
|
|
|
->displayIf('HandlingFeeType')->isNotEqualTo(''); |
139
|
|
|
|
140
|
|
|
$fields->dataFieldByName('HandlingFeeMinimum') |
141
|
|
|
->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum'); |
142
|
|
|
|
143
|
|
|
$fields->dataFieldByName('HandlingFeePercentage') |
144
|
|
|
->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum') |
145
|
|
|
->orIf('HandlingFeeType')->isEqualTo('flat_percent'); |
146
|
|
|
|
147
|
|
|
$fields->replaceField( |
148
|
|
|
'DiscountType', |
149
|
|
|
DropdownField::create('DiscountType', 'Discount Type', $this->getDiscountTypes()) |
150
|
|
|
->setEmptyString('') |
151
|
|
|
->setDescription('This determines what type of per category discount you would like to use, if any.') |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$fields->dataFieldByName('DiscountName') |
155
|
|
|
->displayIf('DiscountType')->isNotEqualTo(''); |
156
|
|
|
|
157
|
|
|
$fields->dataFieldByName('DiscountDetails') |
158
|
|
|
->displayIf('DiscountType')->isNotEqualTo(''); |
159
|
|
|
|
160
|
|
|
$fields->dataFieldByName('CustomsValue') |
161
|
|
|
->setDescription('Enter a dollar amount here for the declared customs value for international shipments. If you leave this blank, the sale price of the item will be used.'); |
162
|
|
|
|
163
|
|
|
/* |
|
|
|
|
164
|
|
|
$fields = FieldList::create( |
165
|
|
|
LiteralField::create( |
166
|
|
|
'PCIntro', |
167
|
|
|
_t( |
168
|
|
|
'ProductCategory.PCIntro', |
169
|
|
|
'<p>Categories must be created in your |
170
|
|
|
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank"> |
171
|
|
|
FoxyCart Product Categories |
172
|
|
|
</a>, and also manually created in FoxyStripe. |
173
|
|
|
</p>' |
174
|
|
|
) |
175
|
|
|
), |
176
|
|
|
TextField::create('Code') |
177
|
|
|
->setTitle(_t('ProductCategory.Code', 'Category Code')) |
178
|
|
|
->setDescription(_t('ProductCategory.CodeDescription', 'copy/paste from FoxyCart')), |
179
|
|
|
TextField::create('Title') |
180
|
|
|
->setTitle(_t('ProductCategory.Title', 'Category Title')) |
181
|
|
|
->setDescription(_t('ProductCategory.TitleDescription', 'copy/paste from FoxyCart')), |
182
|
|
|
DropdownField::create( |
183
|
|
|
'DeliveryType', |
184
|
|
|
'Delivery Type', |
185
|
|
|
singleton('ProductCategory')->dbObject('DeliveryType')->enumValues() |
186
|
|
|
)->setEmptyString('') |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$this->extend('updateCMSFields', $fields); |
190
|
|
|
*/ |
191
|
|
|
|
192
|
|
|
return $fields; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
197
|
|
|
*/ |
198
|
|
|
public function requireDefaultRecords() |
199
|
|
|
{ |
200
|
|
|
parent::requireDefaultRecords(); |
201
|
|
|
$allCats = ProductCategory::get(); |
202
|
|
|
if (!$allCats->count()) { |
203
|
|
|
$cat = new ProductCategory(); |
204
|
|
|
$cat->Title = 'Default'; |
|
|
|
|
205
|
|
|
$cat->Code = 'DEFAULT'; |
|
|
|
|
206
|
|
|
$cat->write(); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param bool $member |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public function canView($member = false) |
215
|
|
|
{ |
216
|
|
|
return true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param null $member |
|
|
|
|
221
|
|
|
* @return bool|int |
222
|
|
|
*/ |
223
|
|
|
public function canEdit($member = null) |
224
|
|
|
{ |
225
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param null $member |
|
|
|
|
230
|
|
|
* @return bool|int |
231
|
|
|
*/ |
232
|
|
|
public function canDelete($member = null) |
233
|
|
|
{ |
234
|
|
|
|
235
|
|
|
//don't allow deletion of DEFAULT category |
236
|
|
|
return ($this->Code == 'DEFAULT') ? false : Permission::check('Product_CANCRUD', 'any', $member); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param null $member |
|
|
|
|
241
|
|
|
* @return bool|int |
242
|
|
|
*/ |
243
|
|
|
public function canCreate($member = null, $context = []) |
244
|
|
|
{ |
245
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getShippingOptions() |
252
|
|
|
{ |
253
|
|
|
return [ |
254
|
|
|
'shipped' => 'Shipped using live shipping rates', |
255
|
|
|
'downloaded' => 'Downloaded by the customer', |
256
|
|
|
'flat_rate' => 'Shipped using a flat rate fee', |
257
|
|
|
'pickup' => 'Picked up by the customer', |
258
|
|
|
'notshipped' => 'No Shipping', |
259
|
|
|
]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function getShippingFlatRateTypes() |
266
|
|
|
{ |
267
|
|
|
return [ |
268
|
|
|
'per_order' => 'Charge per order', |
269
|
|
|
'per_item' => 'Charge per item', |
270
|
|
|
]; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public function getHandlingFeeTypes() |
277
|
|
|
{ |
278
|
|
|
return [ |
279
|
|
|
'flat_per_order' => 'Flat fee per order with products in this category', |
280
|
|
|
'flat_per_item' => 'Flat fee per product in this category', |
281
|
|
|
'flat_percent' => 'Flat fee per shipment + % of price for products in this category', |
282
|
|
|
'flat_percent_with_minimum' => 'Flat fee per shipment OR % of order total with products in this category. Whichever is greater.', |
283
|
|
|
]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function getDiscountTypes() |
290
|
|
|
{ |
291
|
|
|
return [ |
292
|
|
|
'quantity_amount' => 'Discount by an amount based on the quantity', |
293
|
|
|
'quantity_percentage' => 'Discount by a percentage based on the quantity', |
294
|
|
|
'price_amount' => 'Discount by an amount based on the price in this category', |
295
|
|
|
'price_percentage' => 'Discount by a percentage based on the price in this category', |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return array |
301
|
|
|
*/ |
302
|
|
|
public function getDataMap() |
303
|
|
|
{ |
304
|
|
|
return [ |
305
|
|
|
'name' => $this->Title, |
306
|
|
|
'code' => $this->Code, |
307
|
|
|
'item_delivery_type' => $this->DeliveryType, |
|
|
|
|
308
|
|
|
'max_downloads_per_customer' => $this->MaxDownloads, |
|
|
|
|
309
|
|
|
'max_downloads_time_period' => $this->MaxDownloadsTime, |
|
|
|
|
310
|
|
|
'customs_value' => $this->CustomsValue, |
|
|
|
|
311
|
|
|
'default_weight' => $this->DefaultWeight, |
|
|
|
|
312
|
|
|
'default_weight_unit' => $this->DefaultWeightUnit, |
|
|
|
|
313
|
|
|
'default_length_unit' => $this->DefautlLengthUnit, |
|
|
|
|
314
|
|
|
'shipping_flat_rate' => $this->ShippingFlatRate, |
|
|
|
|
315
|
|
|
'shipping_flat_rate_type' => $this->ShippingFlatRateType, |
|
|
|
|
316
|
|
|
'handling_fee_type' => $this->HandlingFeeType, |
|
|
|
|
317
|
|
|
'handling_fee' => $this->HandlingFee, |
|
|
|
|
318
|
|
|
'handling_fee_minimum' => $this->HandlingFeeMinimum, |
|
|
|
|
319
|
|
|
'handling_fee_percentage' => $this->HandlingFeePercentage, |
|
|
|
|
320
|
|
|
'discount_type' => $this->DiscountType, |
|
|
|
|
321
|
|
|
'discount_name' => $this->DiscountName, |
|
|
|
|
322
|
|
|
'discount_details' => $this->DiscountDetails, |
|
|
|
|
323
|
|
|
]; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
328
|
|
|
*/ |
329
|
|
|
public function onAfterWrite() |
330
|
|
|
{ |
331
|
|
|
parent::onAfterWrite(); |
332
|
|
|
|
333
|
|
|
if ($this->isChanged()) { |
334
|
|
|
if ($fc = new FoxyStripeClient()) { |
335
|
|
|
$fc->putCategory($this->getDataMap()); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
342
|
|
|
*/ |
343
|
|
|
public function onAfterDelete() |
344
|
|
|
{ |
345
|
|
|
parent::onAfterDelete(); |
346
|
|
|
|
347
|
|
|
if ($fc = new FoxyStripeClient()) { |
348
|
|
|
$fc->deleteCategory($this->getDataMap()); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|