|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Products\Page\Product; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
|
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\Security\Permission; |
|
11
|
|
|
use SilverStripe\Versioned\GridFieldArchiveAction; |
|
12
|
|
|
use SilverStripe\Versioned\Versioned; |
|
13
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
|
14
|
|
|
|
|
15
|
|
|
class Discount extends DataObject |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $singular_name = 'Discount'; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $plural_name = 'Discounts'; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $db = array( |
|
|
|
|
|
|
31
|
|
|
'Title' => 'Varchar(255)', |
|
32
|
|
|
'Quantity' => 'Int', |
|
33
|
|
|
'Percentage' => 'Int', |
|
34
|
|
|
'StartTime' => 'DBDatetime', |
|
35
|
|
|
'EndTime' => 'DBDatetime', |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $many_many = [ |
|
|
|
|
|
|
42
|
|
|
'Products' => Product::class, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
49
|
|
|
'Title', |
|
50
|
|
|
'DiscountPercentage' => [ |
|
51
|
|
|
'title' => 'Discount', |
|
52
|
|
|
], |
|
53
|
|
|
'StartTime.Nice' => 'Starts', |
|
54
|
|
|
'EndTime.Nice' => 'Ends', |
|
55
|
|
|
'IsActive' => 'Active', |
|
56
|
|
|
'IsGlobal' => 'Global', |
|
57
|
|
|
'Products.count' => 'Products', |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
private static $defaults = [ |
|
|
|
|
|
|
64
|
|
|
'Quantity' => 1, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
private static $casting = [ |
|
|
|
|
|
|
71
|
|
|
'IsActive' => 'Boolean', |
|
72
|
|
|
'IsGlobal' => 'Boolean', |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
|
|
private static $extensions = [ |
|
|
|
|
|
|
79
|
|
|
Versioned::class, |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
private static $table_name = 'FoxyDiscount'; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return FieldList |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getCMSFields() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
|
93
|
|
|
$fields->removeByName([ |
|
94
|
|
|
'Quantity', |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
//$quantity = $fields->dataFieldByName('Quantity'); |
|
98
|
|
|
//$quantity->setTitle('Quantity to trigger discount'); |
|
99
|
|
|
|
|
100
|
|
|
$percentage = $fields->dataFieldByName('Percentage'); |
|
101
|
|
|
$percentage->setTitle('Percent discount'); |
|
102
|
|
|
|
|
103
|
|
|
if ($this->ID) { |
|
104
|
|
|
$field = $fields->dataFieldByName('Products'); |
|
105
|
|
|
$config = $field->getConfig(); |
|
106
|
|
|
$config |
|
107
|
|
|
->removeComponentsByType([ |
|
108
|
|
|
GridFieldAddExistingAutocompleter::class, |
|
109
|
|
|
GridFieldAddNewButton::class, |
|
110
|
|
|
GridFieldArchiveAction::class, |
|
111
|
|
|
]) |
|
112
|
|
|
->addComponents([ |
|
113
|
|
|
new GridFieldAddExistingSearchButton(), |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
}); |
|
117
|
|
|
|
|
118
|
|
|
return parent::getCMSFields(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getDiscountPercentage() |
|
125
|
|
|
{ |
|
126
|
|
|
return "{$this->Percentage}%"; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getIsActive() |
|
133
|
|
|
{ |
|
134
|
|
|
$date = date('Y-m-d H:i:s', strtotime('now')); |
|
135
|
|
|
return ($this->owner->StartTime <= $date && $this->owner->EndTime >= $date) && $this->owner->isPublished(); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getIsGlobal() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->Products()->count() === 0; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
148
|
|
|
* |
|
149
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
150
|
|
|
* @return boolean |
|
151
|
|
|
*/ |
|
152
|
|
|
public function canView($member = null) |
|
153
|
|
|
{ |
|
154
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
155
|
|
|
if ($extended !== null) { |
|
156
|
|
|
return $extended; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
164
|
|
|
* |
|
165
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
166
|
|
|
* |
|
167
|
|
|
* @return boolean |
|
168
|
|
|
*/ |
|
169
|
|
|
public function canEdit($member = null) |
|
170
|
|
|
{ |
|
171
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
172
|
|
|
if ($extended !== null) { |
|
173
|
|
|
return $extended; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
181
|
|
|
* |
|
182
|
|
|
* Uses archive not delete so that current stage is respected i.e if a |
|
183
|
|
|
* element is not published, then it can be deleted by someone who doesn't |
|
184
|
|
|
* have publishing permissions. |
|
185
|
|
|
* |
|
186
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
187
|
|
|
* |
|
188
|
|
|
* @return boolean |
|
189
|
|
|
*/ |
|
190
|
|
|
public function canDelete($member = null) |
|
191
|
|
|
{ |
|
192
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
193
|
|
|
if ($extended !== null) { |
|
194
|
|
|
return $extended; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Basic permissions, defaults to page perms where possible. |
|
202
|
|
|
* |
|
203
|
|
|
* @param \SilverStripe\Security\Member|null $member |
|
204
|
|
|
* @param array $context |
|
205
|
|
|
* |
|
206
|
|
|
* @return boolean |
|
207
|
|
|
*/ |
|
208
|
|
|
public function canCreate($member = null, $context = array()) |
|
209
|
|
|
{ |
|
210
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
|
211
|
|
|
if ($extended !== null) { |
|
212
|
|
|
return $extended; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return Permission::check('CMS_ACCESS', 'any', $member); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|