TaxCategory   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 113
c 5
b 0
f 0
dl 0
loc 274
rs 9.28
wmc 39

11 Methods

Rating   Name   Duplication   Size   Complexity  
B ValidTax() 0 33 7
A providePermissions() 0 8 1
A canDelete() 0 17 5
A getRatesList() 0 3 1
A getCMSValidator() 0 4 1
A canCreate() 0 17 5
A canEdit() 0 17 5
A getCMSFields() 0 32 4
A requireDefaultRecords() 0 20 2
A onAfterWrite() 0 10 6
A canView() 0 9 2
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';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
42
43
    /**
44
     * What location will this rate be applied based on?
45
     *
46
     * @var array
47
     * @config
48
     */
49
    private static $rate_locations = [
0 ignored issues
show
introduced by
The private property $rate_locations is not used, and could be removed.
Loading history...
50
        'Shipping Address',
51
        'Billing Address',
52
        'Store Address'
53
    ];
54
55
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
56
        "Title" => "Varchar",
57
        "Default" => "Boolean"
58
    ];
59
    
60
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
61
        "Site" => SiteConfig::class
62
    ];
63
64
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
65
        "Rates" => TaxRate::class
66
    ];
67
68
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
69
        "Rates" => [
70
            "Location" => "Int"
71
        ]
72
    ];
73
74
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
75
        "Title",
76
        "RatesList",
77
        "Default"
78
    ];
79
80
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
81
        "RatesList" => "Varchar(255)"
82
    ];
83
    
84
    public function getRatesList()
85
    {
86
        return implode(", ", $this->Rates()->column("Title"));
0 ignored issues
show
Bug introduced by
The method Rates() does not exist on SilverCommerce\TaxAdmin\Model\TaxCategory. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        return implode(", ", $this->/** @scrutinizer ignore-call */ Rates()->column("Title"));
Loading history...
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) {
0 ignored issues
show
introduced by
$grid is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
148
            $grid->setTitle("");
149
            $config = $grid->getConfig();
150
151
            $add_field = new GridFieldAddExistingAutocompleter('buttons-before-right');
152
153
            if ($add_field && $site) {
0 ignored issues
show
introduced by
$site is of type SilverStripe\SiteConfig\SiteConfig, thus it always evaluated to true.
Loading history...
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()) {
0 ignored issues
show
Bug introduced by
The method Site() does not exist on SilverCommerce\TaxAdmin\Model\TaxCategory. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
        if ($this->Default && $this->/** @scrutinizer ignore-call */ Site()->exists()) {
Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

254
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

270
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
271
    {
272
        $extended = $this->extendedCan(__FUNCTION__, $member);
273
        
274
        if ($extended !== null) {
275
            return $extended;
276
        }
277
        
278
        if (!$member) {
279
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

279
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

295
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
296
    {
297
        $extended = $this->extendedCan(__FUNCTION__, $member);
298
        
299
        if ($extended !== null) {
300
            return $extended;
301
        }
302
        
303
        if (!$member) {
304
            $member = Member::currentUser();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

304
            $member = /** @scrutinizer ignore-deprecated */ Member::currentUser();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
305
        }
306
307
        if ($member && Permission::checkMember($member->ID, ["ADMIN", "TAXADMIN_MANAGE_CATEGORY"])) {
308
            return true;
309
        }
310
311
        return false;
312
    }
313
}
314