1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\TaxAdmin\Model; |
4
|
|
|
|
5
|
|
|
use Locale; |
6
|
|
|
use SilverStripe\ORM\DB; |
7
|
|
|
use SilverStripe\i18n\i18n; |
8
|
|
|
use SilverStripe\ORM\DataList; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
use SilverStripe\Security\Member; |
11
|
|
|
use SilverStripe\Security\Security; |
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
use SilverStripe\Forms\DropdownField; |
14
|
|
|
use SilverStripe\Security\Permission; |
15
|
|
|
use SilverStripe\Forms\RequiredFields; |
16
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
17
|
|
|
use SilverCommerce\TaxAdmin\Model\TaxRate; |
18
|
|
|
use SilverStripe\Security\PermissionProvider; |
19
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
20
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
21
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
22
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
23
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
24
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
25
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
26
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A tax rate can be added to a product and allows you to map a product |
30
|
|
|
* to a percentage of tax. |
31
|
|
|
* |
32
|
|
|
* If added to a product, the tax will then be added to the price |
33
|
|
|
* automatically. |
34
|
|
|
* |
35
|
|
|
* @author i-lateral (http://www.i-lateral.com) |
36
|
|
|
* @package catalogue |
37
|
|
|
*/ |
38
|
|
|
class TaxCategory extends DataObject implements PermissionProvider |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
private static $table_name = 'TaxCategory'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* What location will this rate be applied based on? |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
* @config |
48
|
|
|
*/ |
49
|
|
|
private static $rate_locations = [ |
|
|
|
|
50
|
|
|
'Shipping Address', |
51
|
|
|
'Billing Address', |
52
|
|
|
'Store Address' |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
private static $db = [ |
|
|
|
|
56
|
|
|
"Title" => "Varchar", |
57
|
|
|
"Default" => "Boolean" |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
private static $has_one = [ |
|
|
|
|
61
|
|
|
"Site" => SiteConfig::class |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
private static $many_many = [ |
|
|
|
|
65
|
|
|
"Rates" => TaxRate::class |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
private static $many_many_extraFields = [ |
|
|
|
|
69
|
|
|
"Rates" => [ |
70
|
|
|
"Location" => "Int" |
71
|
|
|
] |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
private static $summary_fields = [ |
|
|
|
|
75
|
|
|
"Title", |
76
|
|
|
"RatesList", |
77
|
|
|
"Default" |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
private static $casting = [ |
|
|
|
|
81
|
|
|
"RatesList" => "Varchar(255)" |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
public function getRatesList() |
85
|
|
|
{ |
86
|
|
|
return implode(", ", $this->Rates()->column("Title")); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Attempt to determine the relevent current tax |
91
|
|
|
* from the users location (or default store location) |
92
|
|
|
* |
93
|
|
|
* @param string $country The current ISO-3166 2 character country code |
94
|
|
|
* @param string $region A 3 character ISO-3166-2 subdivision code |
95
|
|
|
* @return TaxRate|null |
96
|
|
|
*/ |
97
|
|
|
public function ValidTax($country = null, $region = null) |
98
|
|
|
{ |
99
|
|
|
if (empty($country)) { |
100
|
|
|
// First try and get the locale from the member |
101
|
|
|
$member = Security::getCurrentUser(); |
102
|
|
|
|
103
|
|
|
if ($member && $member->getLocale()) { |
104
|
|
|
$country = $member->getLocale(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (empty($country)) { |
109
|
|
|
$country = i18n::get_locale(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (strlen($country) > 2) { |
113
|
|
|
$country = Locale::getRegion($country); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$filter = [ |
117
|
|
|
'Global' => 1, |
118
|
|
|
"Zones.Regions.CountryCode" => $country |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
$rates = $this |
122
|
|
|
->Rates() |
123
|
|
|
->filterAny($filter); |
124
|
|
|
|
125
|
|
|
if (isset($region)) { |
126
|
|
|
$rates = $rates->filter("Zones.Regions.Code", $region); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $rates->first(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getCMSValidator() |
133
|
|
|
{ |
134
|
|
|
return RequiredFields::create([ |
135
|
|
|
"Title" |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getCMSFields() |
140
|
|
|
{ |
141
|
|
|
$site = SiteConfig::current_site_config(); |
142
|
|
|
|
143
|
|
|
$fields = parent::getCMSFields(); |
144
|
|
|
|
145
|
|
|
$grid = $fields->dataFieldByName("Rates"); |
146
|
|
|
|
147
|
|
|
if ($grid) { |
|
|
|
|
148
|
|
|
$grid->setTitle(""); |
149
|
|
|
$config = $grid->getConfig(); |
150
|
|
|
|
151
|
|
|
$add_field = new GridFieldAddExistingAutocompleter('buttons-before-right'); |
152
|
|
|
|
153
|
|
|
if ($add_field && $site) { |
|
|
|
|
154
|
|
|
$dataClass = $grid->getModelClass(); |
155
|
|
|
$add_field->setSearchList( |
156
|
|
|
DataList::create($dataClass)->filter('Site.ID', $site->ID) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$config |
161
|
|
|
->removeComponentsByType(GridFieldAddNewButton::class) |
162
|
|
|
->removeComponentsByType(GridFieldEditButton::class) |
163
|
|
|
->removeComponentsByType(GridFieldDeleteAction::class) |
164
|
|
|
->removeComponentsByType(GridFieldDetailForm::class) |
165
|
|
|
->removeComponentsByType(GridFieldAddExistingAutocompleter::class) |
166
|
|
|
->addComponent($add_field) |
167
|
|
|
->addComponent(new GridFieldDeleteAction(true)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $fields; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function requireDefaultRecords() |
174
|
|
|
{ |
175
|
|
|
// If no tax rates, setup some defaults |
176
|
|
|
if (!TaxCategory::get()->exists()) { |
177
|
|
|
$config = SiteConfig::current_site_config(); |
178
|
|
|
|
179
|
|
|
$cat = TaxCategory::create([ |
180
|
|
|
"Title" => "Standard Goods", |
181
|
|
|
"Default" => 1 |
182
|
|
|
]); |
183
|
|
|
$cat->SiteID = $config->ID; |
184
|
|
|
$cat->write(); |
185
|
|
|
|
186
|
|
|
DB::alteration_message( |
187
|
|
|
'Standard Goods tax category created', |
188
|
|
|
'created' |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
parent::requireDefaultRecords(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function onAfterWrite() |
196
|
|
|
{ |
197
|
|
|
parent::onAfterWrite(); |
198
|
|
|
|
199
|
|
|
// If this is set to default, reset all other categories |
200
|
|
|
if ($this->Default && $this->Site()->exists()) { |
|
|
|
|
201
|
|
|
foreach ($this->Site()->TaxCategories() as $cat) { |
202
|
|
|
if ($cat->ID != $this->ID && $cat->Default) { |
203
|
|
|
$cat->Default = false; |
204
|
|
|
$cat->write(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function providePermissions() |
211
|
|
|
{ |
212
|
|
|
return [ |
213
|
|
|
"TAXADMIN_MANAGE_CATEGORY" => [ |
214
|
|
|
'name' => 'Manage Tax Categories', |
215
|
|
|
'help' => 'Allow user to create, edit and delete tax categoires', |
216
|
|
|
'category' => 'Tax', |
217
|
|
|
'sort' => 10 |
218
|
|
|
] |
219
|
|
|
]; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Anyone can view tax categories |
224
|
|
|
* |
225
|
|
|
* @param Member $member |
226
|
|
|
* @return boolean |
227
|
|
|
*/ |
228
|
|
|
public function canView($member = null) |
229
|
|
|
{ |
230
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
231
|
|
|
|
232
|
|
|
if ($extended !== null) { |
233
|
|
|
return $extended; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Anyone can create orders, even guest users |
241
|
|
|
* |
242
|
|
|
* @param Member $member |
243
|
|
|
* @return boolean |
244
|
|
|
*/ |
245
|
|
|
public function canCreate($member = null, $context = []) |
246
|
|
|
{ |
247
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member, $context); |
248
|
|
|
|
249
|
|
|
if ($extended !== null) { |
250
|
|
|
return $extended; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if (!$member) { |
254
|
|
|
$member = Member::currentUser(); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_CATEGORY"])) { |
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Only users with correct rights can edit |
266
|
|
|
* |
267
|
|
|
* @param Member $member |
268
|
|
|
* @return boolean |
269
|
|
|
*/ |
270
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
273
|
|
|
|
274
|
|
|
if ($extended !== null) { |
275
|
|
|
return $extended; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
if (!$member) { |
279
|
|
|
$member = Member::currentUser(); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_CATEGORY"])) { |
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* No one should be able to delete an order once it has been created |
291
|
|
|
* |
292
|
|
|
* @param Member $member |
293
|
|
|
* @return boolean |
294
|
|
|
*/ |
295
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
298
|
|
|
|
299
|
|
|
if ($extended !== null) { |
300
|
|
|
return $extended; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (!$member) { |
304
|
|
|
$member = Member::currentUser(); |
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_CATEGORY"])) { |
308
|
|
|
return true; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return false; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|