|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\TaxAdmin\Model; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DB; |
|
6
|
|
|
use SilverStripe\i18n\i18n; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\Security\Permission; |
|
10
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
11
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
12
|
|
|
use SilverStripe\Forms\CheckboxSetField; |
|
13
|
|
|
use SilverStripe\Security\PermissionProvider; |
|
14
|
|
|
use SilverStripe\Forms\MultiSelectField; |
|
15
|
|
|
use SilverStripe\Forms\ListboxField; |
|
16
|
|
|
use SilverCommerce\GeoZones\Model\Zone; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A tax rate can be added to a product and allows you to map a product |
|
20
|
|
|
* to a percentage of tax. |
|
21
|
|
|
* |
|
22
|
|
|
* If added to a product, the tax will then be added to the price |
|
23
|
|
|
* automatically. |
|
24
|
|
|
* |
|
25
|
|
|
* @author i-lateral (http://www.i-lateral.com) |
|
26
|
|
|
* @package catalogue |
|
27
|
|
|
*/ |
|
28
|
|
|
class TaxRate extends DataObject implements PermissionProvider |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
private static $table_name = 'TaxRate'; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private static $db = [ |
|
|
|
|
|
|
34
|
|
|
"Title" => "Varchar", |
|
35
|
|
|
"Rate" => "Decimal", |
|
36
|
|
|
'Global' => 'Boolean' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
40
|
|
|
"Site" => SiteConfig::class |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
44
|
|
|
"Zones" => Zone::class |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
private static $casting = [ |
|
|
|
|
|
|
48
|
|
|
"ZonesList" => "Varchar" |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
private static $summary_fields = [ |
|
|
|
|
|
|
52
|
|
|
"Title", |
|
53
|
|
|
"Rate", |
|
54
|
|
|
"ZonesList" |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
private static $searchable_fields = [ |
|
|
|
|
|
|
58
|
|
|
"Title", |
|
59
|
|
|
"Rate" |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
public function getZonesList() |
|
63
|
|
|
{ |
|
64
|
|
|
return implode(", ", $this->Zones()->column("Name")); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getCMSValidator() |
|
68
|
|
|
{ |
|
69
|
|
|
return RequiredFields::create([ |
|
70
|
|
|
"Title", |
|
71
|
|
|
"Rate" |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function requireDefaultRecords() |
|
76
|
|
|
{ |
|
77
|
|
|
// If no tax rates, setup some defaults |
|
78
|
|
|
if (!TaxRate::get()->exists()) { |
|
79
|
|
|
$config = SiteConfig::current_site_config(); |
|
80
|
|
|
$category = $config->TaxCategories()->first(); |
|
81
|
|
|
|
|
82
|
|
|
$vat = TaxRate::create(); |
|
83
|
|
|
$vat->Title = "VAT"; |
|
84
|
|
|
$vat->Rate = 20; |
|
85
|
|
|
$vat->SiteID = $config->ID; |
|
86
|
|
|
$vat->write(); |
|
87
|
|
|
DB::alteration_message( |
|
88
|
|
|
'VAT tax rate created.', |
|
89
|
|
|
'created' |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
$reduced = TaxRate::create(); |
|
93
|
|
|
$reduced->Title = "Reduced rate"; |
|
94
|
|
|
$reduced->Rate = 5; |
|
95
|
|
|
$reduced->SiteID = $config->ID; |
|
96
|
|
|
$reduced->write(); |
|
97
|
|
|
DB::alteration_message( |
|
98
|
|
|
'Reduced tax rate created.', |
|
99
|
|
|
'created' |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$zero = TaxRate::create(); |
|
103
|
|
|
$zero->Title = "Zero rate"; |
|
104
|
|
|
$zero->Rate = 0; |
|
105
|
|
|
$zero->SiteID = $config->ID; |
|
106
|
|
|
$zero->write(); |
|
107
|
|
|
DB::alteration_message( |
|
108
|
|
|
'Zero tax rate created.', |
|
109
|
|
|
'created' |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
if ($category) { |
|
113
|
|
|
$category->Rates()->add($vat); |
|
114
|
|
|
DB::alteration_message( |
|
115
|
|
|
'Added VAT to category', |
|
116
|
|
|
'created' |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
parent::requireDefaultRecords(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function providePermissions() |
|
125
|
|
|
{ |
|
126
|
|
|
return [ |
|
127
|
|
|
"TAXADMIN_MANAGE_RATE" => [ |
|
128
|
|
|
'name' => 'Manage Tax Rates', |
|
129
|
|
|
'help' => 'Allow user to create, edit and delete tax rates', |
|
130
|
|
|
'category' => 'Tax', |
|
131
|
|
|
'sort' => 0 |
|
132
|
|
|
] |
|
133
|
|
|
]; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Anyone can view tax categories |
|
138
|
|
|
* |
|
139
|
|
|
* @param Member $member |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
|
|
public function canView($member = null) |
|
143
|
|
|
{ |
|
144
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
145
|
|
|
|
|
146
|
|
|
if ($extended !== null) { |
|
147
|
|
|
return $extended; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Anyone can create orders, even guest users |
|
155
|
|
|
* |
|
156
|
|
|
* @param Member $member |
|
157
|
|
|
* @return boolean |
|
158
|
|
|
*/ |
|
159
|
|
|
public function canCreate($member = null, $context = []) |
|
160
|
|
|
{ |
|
161
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member, $context); |
|
162
|
|
|
|
|
163
|
|
|
if ($extended !== null) { |
|
164
|
|
|
return $extended; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (!$member) { |
|
168
|
|
|
$member = Member::currentUser(); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_RATE"])) { |
|
172
|
|
|
return true; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Only users with correct rights can edit |
|
180
|
|
|
* |
|
181
|
|
|
* @param Member $member |
|
182
|
|
|
* @return boolean |
|
183
|
|
|
*/ |
|
184
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
187
|
|
|
|
|
188
|
|
|
if ($extended !== null) { |
|
189
|
|
|
return $extended; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if (!$member) { |
|
193
|
|
|
$member = Member::currentUser(); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_RATE"])) { |
|
197
|
|
|
return true; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return false; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* No one should be able to delete an order once it has been created |
|
205
|
|
|
* |
|
206
|
|
|
* @param Member $member |
|
207
|
|
|
* @return boolean |
|
208
|
|
|
*/ |
|
209
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
212
|
|
|
|
|
213
|
|
|
if ($extended !== null) { |
|
214
|
|
|
return $extended; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (!$member) { |
|
218
|
|
|
$member = Member::currentUser(); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_RATE"])) { |
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|