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; |
|
|
|
|
18
|
|
|
|
19
|
|
|
class Discount extends DataObject implements PermissionProvider |
20
|
|
|
{ |
21
|
|
|
private static $table_name = 'Discount'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
private static $db = [ |
|
|
|
|
24
|
|
|
"Title" => "Varchar", |
25
|
|
|
"Code" => "Varchar(99)", |
26
|
|
|
"MinOrder" => "Decimal", |
27
|
|
|
"Starts" => "Date", |
28
|
|
|
"Expires" => "Date" |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private static $has_one = [ |
|
|
|
|
32
|
|
|
"Site" => SiteConfig::class |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
private static $many_many = [ |
|
|
|
|
36
|
|
|
"Groups" => Group::class, |
37
|
|
|
"Categories" => CatalogueCategory::class |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
private static $summary_fields = [ |
|
|
|
|
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) { |
|
|
|
|
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. |
|
|
|
|
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) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return 0; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function applyDiscount($estimate, $code = null) |
|
|
|
|
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); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function canView($member = null, $context = []) |
|
|
|
|
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 = []) |
|
|
|
|
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 = []) |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths