-
Notifications
You must be signed in to change notification settings - Fork 170
/
Discount.php
371 lines (313 loc) · 8.2 KB
/
Discount.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\commerce\models;
use Craft;
use craft\commerce\base\Model;
use craft\commerce\Plugin;
use craft\commerce\records\Discount as DiscountRecord;
use craft\helpers\UrlHelper;
use craft\validators\UniqueValidator;
/**
* Discount model
*
* @property string|false $cpEditUrl
* @property-read string $percentDiscountAsPercent
* @property array $categoryIds
* @property array $purchasableIds
* @property array $userGroupIds
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 2.0
*/
class Discount extends Model
{
// Properties
// =========================================================================
/**
* @var int ID
*/
public $id;
/**
* @var string Name of the discount
*/
public $name;
/**
* @var string The description of this discount
*/
public $description;
/**
* @var string Coupon Code
*/
public $code;
/**
* @var int Per user coupon use limit
*/
public $perUserLimit = 0;
/**
* @var int Per email coupon use limit
*/
public $perEmailLimit = 0;
/**
* @var int Total use limit by guests or users
*/
public $totalUseLimit = 0;
/**
* @var int Total use counter;
*/
public $totalUses = 0;
/**
* @var \DateTime|null Date the discount is valid from
*/
public $dateFrom;
/**
* @var \DateTime|null Date the discount is valid to
*/
public $dateTo;
/**
* @var float Total minimum spend on matching items
*/
public $purchaseTotal = 0;
/**
* @var int Total minimum qty of matching items
*/
public $purchaseQty = 0;
/**
* @var int Total maximum spend on matching items
*/
public $maxPurchaseQty = 0;
/**
* @var float Base amount of discount
*/
public $baseDiscount = 0;
/**
* @var float Amount of discount per item
*/
public $perItemDiscount;
/**
* @var float Percentage of amount discount per item
*/
public $percentDiscount;
/**
* @var string Whether the discount is off the original price, or the already discount price.
*/
public $percentageOffSubject;
/**
* @var bool Exclude on sale purchasables
*/
public $excludeOnSale;
/**
* @var bool Matching products have free shipping.
*/
public $hasFreeShippingForMatchingItems;
/**
* @var bool The whole order has free shipping.
*/
public $hasFreeShippingForOrder;
/**
* @var bool Match all user groups.
*/
public $allGroups;
/**
* @var bool Match all products
*/
public $allPurchasables;
/**
* @var bool Match all product types
*/
public $allCategories;
/**
* @var bool Discount enabled?
*/
public $enabled = true;
/**
* @var bool stopProcessing
*/
public $stopProcessing;
/**
* @var int sortOrder
*/
public $sortOrder;
/**
* @var \DateTime|null
*/
public $dateCreated;
/**
* @var \DateTime|null
*/
public $dateUpdated;
/**
* @var int[] Product Ids
*/
private $_purchasableIds;
/**
* @var int[] Product Type IDs
*/
private $_categoryIds;
/**
* @var int[] Group IDs
*/
private $_userGroupIds;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function datetimeAttributes(): array
{
$attributes = parent::datetimeAttributes();
$attributes[] = 'dateFrom';
$attributes[] = 'dateTo';
return $attributes;
}
/**
* @return string|false
*/
public function getCpEditUrl()
{
return UrlHelper::cpUrl('commerce/promotions/discounts/' . $this->id);
}
/**
* @return array
*/
public function getCategoryIds(): array
{
if (null === $this->_categoryIds) {
$this->_loadRelations();
}
return $this->_categoryIds;
}
/**
* @return array
*/
public function getPurchasableIds(): array
{
if (null === $this->_purchasableIds) {
$this->_loadRelations();
}
return $this->_purchasableIds;
}
/**
* @return array
*/
public function getUserGroupIds(): array
{
if (null === $this->_userGroupIds) {
$this->_loadRelations();
}
return $this->_userGroupIds;
}
/**
* Sets the related product type ids
*
* @param array $categoryIds
*/
public function setCategoryIds(array $categoryIds)
{
$this->_categoryIds = array_unique($categoryIds);
}
/**
* Sets the related product ids
*
* @param array $purchasableIds
*/
public function setPurchasableIds(array $purchasableIds)
{
$this->_purchasableIds = array_unique($purchasableIds);
}
/**
* Sets the related user group ids
*
* @param array $userGroupIds
*/
public function setUserGroupIds(array $userGroupIds)
{
$this->_userGroupIds = array_unique($userGroupIds);
}
/**
* @param bool $value
* @deprecated in 2.1
*/
public function setFreeShipping($value)
{
Craft::$app->getDeprecator()->log('Discount::setFreeShipping()', 'Discount::setFreeShipping() has been deprecated. Use Discount::setHasFreeShippingForMatchingItems() instead');
$this->setHasFreeShippingForMatchingItems($value);
}
/**
* @return bool
* @deprecated in 2.1
*/
public function getFreeShipping(): bool
{
Craft::$app->getDeprecator()->log('Discount::getFreeShipping()', 'Discount::getFreeShipping() or discount.freeShipping has been deprecated. Use Discount::getHasFreeShippingForMatchingItems() or discount.hasFreeShippingForMatchingItems instead');
return $this->getHasFreeShippingForMatchingItems();
}
/**
* @param bool $value
*/
public function setHasFreeShippingForMatchingItems($value)
{
$this->hasFreeShippingForMatchingItems = (bool)$value;
}
/**
* @return bool
*/
public function getHasFreeShippingForMatchingItems(): bool
{
return (bool)$this->hasFreeShippingForMatchingItems;
}
/**
* @return string
*/
public function getPercentDiscountAsPercent(): string
{
if ($this->percentDiscount !== 0) {
$string = (string)$this->percentDiscount;
$number = rtrim($string, '0');
$diff = strlen($string) - strlen($number);
return Craft::$app->formatter->asPercent(-$this->percentDiscount, 2 - $diff);
}
return Craft::$app->formatter->asPercent(0);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[
[
'purchaseTotal',
'perUserLimit',
'perEmailLimit',
'totalUseLimit',
'totalUses',
'purchaseTotal',
'purchaseQty',
'maxPurchaseQty',
'baseDiscount',
'perItemDiscount',
'percentDiscount'
], 'number', 'skipOnEmpty' => false
],
['hasFreeShippingForOrder', function ($attribute, $params, $validator) {
if ($this->hasFreeShippingForMatchingItems && $this->hasFreeShippingForOrder) {
$this->addError($attribute, 'Free shipping can only be for whole order or matching items, not both.');
}
}],
[['code'], UniqueValidator::class, 'targetClass' => DiscountRecord::class, 'targetAttribute' => ['code']],
];
}
// Private Methods
// =========================================================================
/**
* Loads the sale relations
*/
private function _loadRelations()
{
Plugin::getInstance()->getDiscounts()->populateDiscountRelations($this);
}
}