Passed
Push — 1.0 ( 538dd9...e2c8be )
by
unknown
01:52
created

Discount::getCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 13
rs 10
cc 2
nc 2
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 $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
41
        "Title",
42
        "Code",
43
        "Starts",
44
        "Expires"
45
    ];
46
47
    public function getCMSFields()
48
    {
49
        $fields = parent::getCMSFields();
50
51
        $min = $fields->dataFieldByName("MinOrder");
52
53
        if ($min) {
0 ignored issues
show
introduced by
$min is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
54
            $min->setDescription(
55
                _t(self::class.".MinOrderPreTax","This is the SubTotal of an order EXCLUDING vat and tax")
56
            );
57
        }
58
59
        return $fields;
60
    }
61
62
    /**
63
     * calculate the price reduction for this discount
64
     *
65
     * @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...
66
     * @return int
67
     */
68
    public function calculateAmount(Estimate $estimate)
69
    {
70
        return 0;
71
    }
72
73
    /**
74
     * calculate the value of a discount using an AppliedDiscount item.
75
     *
76
     * @param  AppliedDiscount $item
77
     * @return float
78
     */
79
    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

79
    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...
80
    {
81
        return 0;        
82
    }
83
84
    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

84
    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...
85
    {
86
        $applied = AppliedDiscount::create();
87
        $applied->Code = $this->Code;
88
        $applied->Title = $this->Title;
89
        $applied->Value = $this->calculateAmount($estimate);
90
        $applied->EstimateID = $estimate->ID;
91
92
        $applied->write();
93
94
        $estimate->Discounts()->add($applied);
95
    }
96
97
    /**
98
     * Generate a random string that we can use for the code by default
99
     *
100
     * @return string
101
     */
102
    protected static function generateRandomString($length = 10)
103
    {
104
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
105
        $string = '';
106
107
        for ($i = 0; $i < $length; $i++) {
108
            $string .= $characters[rand(0, strlen($characters) - 1)];
109
        }
110
111
        return $string;
112
    }
113
114
    /**
115
     * Set more complex default data
116
     */
117
    public function populateDefaults()
118
    {
119
        $this->setField('Code', self::generateRandomString());
120
    }
121
122
    public function onBeforeWrite()
123
    {
124
        parent::onBeforeWrite();
125
126
        // Ensure that the code is URL safe
127
        $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...
128
    }
129
130
    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

130
    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...
131
    {
132
        $extended = $this->extendedCan('canView', $member);
133
        if ($extended !== null) {
134
            return $extended;
135
        }
136
        
137
        return true;
138
    }
139
    
140
    public function canCreate($member = null, $context = [])
141
    {
142
        $extended = $this->extendedCan('canCreate', $member);
143
        if ($extended !== null) {
144
            return $extended;
145
        }
146
147
        $permissions = ["ADMIN", "DISCOUNTS_CREATE"];
148
149
        if (!$member) {
150
            $member = Security::getCurrentUser();
151
        }
152
        
153
        if ($member && Permission::checkMember($member->ID, $permissions)) {
154
            return true;
155
        }
156
157
        return false;
158
    }
159
160
    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

160
    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...
161
    {
162
        $extended = $this->extendedCan('canEdit', $member);
163
        if ($extended !== null) {
164
            return $extended;
165
        }
166
167
        $permissions = ["ADMIN", "DISCOUNTS_EDIT"];
168
169
        if (!$member) {
170
            $member = Security::getCurrentUser();
171
        }
172
        
173
        if ($member && Permission::checkMember($member->ID, $permissions)) {
174
            return true;
175
        }
176
177
        return false;
178
    }
179
180
    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

180
    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...
181
    {
182
        $extended = $this->extendedCan('canDelete', $member);
183
        if ($extended !== null) {
184
            return $extended;
185
        }
186
187
        $permissions = ["ADMIN", "DISCOUNTS_DELETE"];
188
189
        if (!$member) {
190
            $member = Security::getCurrentUser();
191
        }
192
        
193
        if ($member && Permission::checkMember($member->ID, $permissions)) {
194
            return true;
195
        }
196
197
        return false;
198
    }
199
200
201
202
    public function providePermissions()
203
    {
204
        return [
205
            "DISCOUNTS_CREATE" => [
206
                'name' => 'Create Discounts',
207
                'help' => 'Allow user to create discounts',
208
                'category' => 'Discounts',
209
                'sort' => 88
210
            ],
211
            "DISCOUNTS_EDIT" => [
212
                'name' => 'Edit Discounts',
213
                'help' => 'Allow user to edit discounts',
214
                'category' => 'Discounts',
215
                'sort' => 87
216
            ],
217
            "DISCOUNTS_DELETE" => [
218
                'name' => 'Delete Discounts',
219
                'help' => 'Allow user to delete discounts',
220
                'category' => 'Discounts',
221
                'sort' => 86
222
            ]
223
        ];
224
    }
225
}
226