1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Coupons\Model\Coupon; |
|
|
|
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DiscountTier |
11
|
|
|
* @package Dynamic\Foxy\Discounts\Model |
12
|
|
|
*/ |
13
|
|
|
class DiscountTier extends DataObject |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private static $db = [ |
19
|
|
|
'Quantity' => 'Int', |
20
|
|
|
'Percentage' => 'Int', |
21
|
|
|
'Amount' => 'Currency', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $has_one = [ |
28
|
|
|
'Discount' => Discount::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $defaults = [ |
35
|
|
|
'Quantity' => 1, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $summary_fields = [ |
42
|
|
|
'DiscountLabel' => [ |
43
|
|
|
'title' => 'Discount', |
44
|
|
|
], |
45
|
|
|
'Quantity', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private static $table_name = 'FoxyDiscountTier'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private static $default_sort = [ |
57
|
|
|
'Quantity', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return FieldList|void |
62
|
|
|
*/ |
63
|
|
|
public function getCMSFields() |
64
|
|
|
{ |
65
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
66
|
|
|
$fields->removeByName([ |
67
|
|
|
'DiscountID', |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$quantity = $fields->dataFieldByName('Quantity'); |
71
|
|
|
$quantity->setTitle('Quantity to trigger discount'); |
72
|
|
|
|
73
|
|
|
/** @var Discount|Coupon $type */ |
74
|
|
|
if ($parent = $this->getParent()) { |
75
|
|
|
$type = $parent->Type; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$percentage = $fields->dataFieldByName('Percentage') |
78
|
|
|
->setTitle('Percent discount'); |
79
|
|
|
$amount = $fields->dataFieldByName('Amount') |
80
|
|
|
->setTitle('Amount to discount'); |
81
|
|
|
|
82
|
|
|
$fields->removeByName([ |
83
|
|
|
'Percentage', |
84
|
|
|
'Amount', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
if ($type == 'Percent') { |
88
|
|
|
$fields->addFieldToTab( |
89
|
|
|
'Root.Main', |
90
|
|
|
$percentage |
91
|
|
|
); |
92
|
|
|
} elseif ($type == 'Amount') { |
93
|
|
|
$fields->addFieldToTab( |
94
|
|
|
'Root.Main', |
95
|
|
|
$amount |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
return parent::getCMSFields(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function getParent() |
108
|
|
|
{ |
109
|
|
|
foreach ($this->hasOne() as $relationName => $className) { |
110
|
|
|
$field = "{$relationName}ID"; |
111
|
|
|
|
112
|
|
|
if ($this->{$field} > 0) { |
113
|
|
|
return $className::get()->byID($this->{$field}); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \SilverStripe\ORM\ValidationResult|void |
122
|
|
|
*/ |
123
|
|
|
public function validate() |
124
|
|
|
{ |
125
|
|
|
$response = parent::validate(); |
126
|
|
|
|
127
|
|
|
if ($this->exists()) { |
128
|
|
|
$exclude['ID'] = $this->ID; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** @var Discount $discount */ |
132
|
|
|
if ($discount = Discount::get()->byID($this->DiscountID)) { |
|
|
|
|
133
|
|
|
$existing = $discount->DiscountTiers()->filter('Quantity', $this->Quantity); |
|
|
|
|
134
|
|
|
if (isset($exclude)) { |
135
|
|
|
$existing = $existing->exclude($exclude); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($existing->count() > 0) { |
139
|
|
|
$response->addError("A discount tier already has the quantity {$this->Quantity} set"); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $response; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getDiscountLabel() |
150
|
|
|
{ |
151
|
|
|
$type = $this->Discount()->Type; |
|
|
|
|
152
|
|
|
$label = ''; |
153
|
|
|
|
154
|
|
|
if ($type == 'Percent') { |
155
|
|
|
$label = "{$this->Percentage}%"; |
|
|
|
|
156
|
|
|
} elseif ($type == 'Amount') { |
157
|
|
|
$label = $this->dbObject('Amount')->Nice(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->extend('updateDiscountLabel', $label); |
161
|
|
|
|
162
|
|
|
return $label; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
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