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