Passed
Push — 1.0 ( e2c8be...7d5088 )
by Morven
15:44
created

Discount::getI18nType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverCommerce\Discounts\Model;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Security\Group;
8
use SilverStripe\Security\Security;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\SiteConfig\SiteConfig;
13
use SilverStripe\Security\PermissionProvider;
14
use SilverCommerce\OrdersAdmin\Model\Estimate;
15
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
16
use SilverCommerce\Discounts\Model\AppliedDiscount;
17
use SilverCommerce\CatalogueAdmin\Model\CatalogueCategory;
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Catalogue...Model\CatalogueCategory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class Discount extends DataObject implements PermissionProvider
20
{
21
    private static $table_name = 'Discount';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
22
23
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        "Title"     => "Varchar",
25
        "Code"      => "Varchar(99)",
26
        "MinOrder"  => "Decimal",
27
        "Starts"    => "Date",
28
        "Expires"   => "Date"
29
    ];
30
31
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
32
        "Site"      => SiteConfig::class
33
    ];
34
35
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
36
        "Groups"    => Group::class,
37
        "Categories" => CatalogueCategory::class
38
    ];
39
40
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
41
        'I18nType'
42
    ];
43
44
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
45
        'I18nType',
46
        "Title",
47
        "Code",
48
        "Starts",
49
        "Expires"
50
    ];
51
52
    private static $field_labels = [
0 ignored issues
show
introduced by
The private property $field_labels is not used, and could be removed.
Loading history...
53
        'I18nType' => 'Type'
54
    ];
55
56
    public function getCMSFields()
57
    {
58
        $self = $this;
59
60
        $this->beforeUpdateCMSFields(function ($fields) use ($self) {
0 ignored issues
show
Unused Code introduced by
The import $self is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
61
            // Add i18n type field to field list
62
            $fields->addFieldToTab(
63
                'Root.Main',
64
                ReadonlyField::create('I18nType', $this->fieldLabel('I18nType')),
65
                'Title'
66
            );
67
68
            $min = $fields->dataFieldByName("MinOrder");
69
70
            if ($min) {
71
                $min->setDescription(
72
                    _t(self::class.".MinOrderPreTax","This is the SubTotal of an order EXCLUDING vat and tax")
73
                );
74
            }
75
        });
76
77
        return parent::getCMSFields();
78
    }
79
80
    /**
81
     * Generate a translated type of this discount (based on it's classname)
82
     *
83
     * @return string
84
     */
85
    public function getI18nType()
86
    {
87
        return $this->i18n_singular_name();
88
    }
89
90
    /**
91
     * calculate the price reduction for this discount
92
     *
93
     * @param  Currency $value - the total/sub-total of the items this discount applies to.
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Discounts\Model\Currency was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     * @return int
95
     */
96
    public function calculateAmount(Estimate $estimate)
97
    {
98
        return 0;
99
    }
100
101
    /**
102
     * calculate the value of a discount using an AppliedDiscount item.
103
     *
104
     * @param  AppliedDiscount $item
105
     * @return float
106
     */
107
    public function appliedAmount(AppliedDiscount $item)
0 ignored issues
show
Unused Code introduced by
The parameter $item 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

107
    public function appliedAmount(/** @scrutinizer ignore-unused */ AppliedDiscount $item)

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...
108
    {
109
        return 0;        
110
    }
111
112
    public function applyDiscount($estimate, $code = null)
0 ignored issues
show
Unused Code introduced by
The parameter $code 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

112
    public function applyDiscount($estimate, /** @scrutinizer ignore-unused */ $code = null)

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...
113
    {
114
        $applied = AppliedDiscount::create();
115
        $applied->Code = $this->Code;
116
        $applied->Title = $this->Title;
117
        $applied->Value = $this->calculateAmount($estimate);
118
        $applied->EstimateID = $estimate->ID;
119
120
        $applied->write();
121
122
        $estimate->Discounts()->add($applied);
123
    }
124
125
    /**
126
     * Generate a random string that we can use for the code by default
127
     *
128
     * @return string
129
     */
130
    protected static function generateRandomString($length = 10)
131
    {
132
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
133
        $string = '';
134
135
        for ($i = 0; $i < $length; $i++) {
136
            $string .= $characters[rand(0, strlen($characters) - 1)];
137
        }
138
139
        return $string;
140
    }
141
142
    /**
143
     * Set more complex default data
144
     */
145
    public function populateDefaults()
146
    {
147
        $this->setField('Code', self::generateRandomString());
148
    }
149
150
    public function onBeforeWrite()
151
    {
152
        parent::onBeforeWrite();
153
154
        // Ensure that the code is URL safe
155
        $this->Code = Convert::raw2url($this->Code);
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
156
    }
157
158
    public function canView($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

158
    public function canView($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...
159
    {
160
        $extended = $this->extendedCan('canView', $member);
161
        if ($extended !== null) {
162
            return $extended;
163
        }
164
        
165
        return true;
166
    }
167
    
168
    public function canCreate($member = null, $context = [])
169
    {
170
        $extended = $this->extendedCan('canCreate', $member);
171
        if ($extended !== null) {
172
            return $extended;
173
        }
174
175
        $permissions = ["ADMIN", "DISCOUNTS_CREATE"];
176
177
        if (!$member) {
178
            $member = Security::getCurrentUser();
179
        }
180
        
181
        if ($member && Permission::checkMember($member->ID, $permissions)) {
182
            return true;
183
        }
184
185
        return false;
186
    }
187
188
    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

188
    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...
189
    {
190
        $extended = $this->extendedCan('canEdit', $member);
191
        if ($extended !== null) {
192
            return $extended;
193
        }
194
195
        $permissions = ["ADMIN", "DISCOUNTS_EDIT"];
196
197
        if (!$member) {
198
            $member = Security::getCurrentUser();
199
        }
200
        
201
        if ($member && Permission::checkMember($member->ID, $permissions)) {
202
            return true;
203
        }
204
205
        return false;
206
    }
207
208
    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

208
    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...
209
    {
210
        $extended = $this->extendedCan('canDelete', $member);
211
        if ($extended !== null) {
212
            return $extended;
213
        }
214
215
        $permissions = ["ADMIN", "DISCOUNTS_DELETE"];
216
217
        if (!$member) {
218
            $member = Security::getCurrentUser();
219
        }
220
        
221
        if ($member && Permission::checkMember($member->ID, $permissions)) {
222
            return true;
223
        }
224
225
        return false;
226
    }
227
228
229
230
    public function providePermissions()
231
    {
232
        return [
233
            "DISCOUNTS_CREATE" => [
234
                'name' => 'Create Discounts',
235
                'help' => 'Allow user to create discounts',
236
                'category' => 'Discounts',
237
                'sort' => 88
238
            ],
239
            "DISCOUNTS_EDIT" => [
240
                'name' => 'Edit Discounts',
241
                'help' => 'Allow user to edit discounts',
242
                'category' => 'Discounts',
243
                'sort' => 87
244
            ],
245
            "DISCOUNTS_DELETE" => [
246
                'name' => 'Delete Discounts',
247
                'help' => 'Allow user to delete discounts',
248
                'category' => 'Discounts',
249
                'sort' => 86
250
            ]
251
        ];
252
    }
253
}
254