|
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 = array( |
|
|
|
|
|
|
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 string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getDiscountLabel() |
|
103
|
|
|
{ |
|
104
|
|
|
$type = $this->Discount()->Type; |
|
105
|
|
|
if ($type == 'Percent') { |
|
106
|
|
|
return "{$this->Percentage}%"; |
|
|
|
|
|
|
107
|
|
|
} elseif ($type == 'Amount') { |
|
108
|
|
|
return $this->dbObject('Amount')->Nice(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|