1 | <?php |
||
37 | class Discount extends PriceModifier |
||
38 | { |
||
39 | const PRICE = 'PRICE'; |
||
40 | const PERCENTAGE = 'PERCENTAGE'; |
||
41 | const APPLIES_EACH_TICKET = 'EACH_TICKET'; |
||
42 | |||
43 | private static $singular_name = 'Discount'; |
||
|
|||
44 | |||
45 | private static $db = array( |
||
46 | 'Description' => 'Text', |
||
47 | 'Amount' => 'Decimal', |
||
48 | 'Uses' => 'Int', |
||
49 | 'DiscountType' => 'Enum("PRICE,PERCENTAGE","PRICE")', |
||
50 | 'Code' => 'Varchar(255)', |
||
51 | 'ValidFrom' => 'SS_Datetime', |
||
52 | 'ValidTill' => 'SS_Datetime', |
||
53 | 'AppliesTo' => 'Enum("CART,EACH_TICKET","CART")' |
||
54 | ); |
||
55 | |||
56 | private static $default_sort = "ValidFrom DESC"; |
||
57 | |||
58 | private static $many_many = array( |
||
59 | 'Groups' => 'Group', |
||
60 | 'Events' => 'CalendarEvent' |
||
61 | ); |
||
62 | |||
63 | private static $indexes = array( |
||
64 | 'Code' => 'unique("Code")' |
||
65 | ); |
||
66 | |||
67 | private static $summary_fields = array( |
||
68 | 'Code' => 'Code', |
||
69 | 'Description' => 'Description', |
||
70 | 'ValidFrom.Nice' => 'Valid from', |
||
71 | 'ValidTill.Nice' => 'Valid till', |
||
72 | 'Reservations.Count' => 'Uses' |
||
73 | ); |
||
74 | |||
75 | private static $defaults = array( |
||
76 | 'Uses' => 1 |
||
77 | ); |
||
78 | |||
79 | /** |
||
80 | * Create the needed cms fields |
||
81 | * |
||
82 | * @return \FieldList |
||
83 | */ |
||
84 | public function getCMSFields() |
||
85 | { |
||
86 | $fields = parent::getCMSFields(); |
||
87 | |||
88 | $types = $this->dbObject('DiscountType')->enumValues(); |
||
89 | $appliesTo = $this->dbObject('AppliesTo')->enumValues(); |
||
90 | |||
91 | $fields->addFieldsToTab('Root.Main', array( |
||
92 | $code = TextField::create('Code', 'Code'), |
||
93 | TextareaField::create('Description', 'Description')->setDescription('The description is only visible in the cms'), |
||
94 | DropdownField::create('DiscountType', _t('Discount.TYPE', 'Type of discount'), $types), |
||
95 | DropdownField::create('AppliesTo', _t('Discount.AppliesTo', 'Discount applies to'), $appliesTo), |
||
96 | NumericField::create('Amount', _t('Discount.AMOUNT', 'Amount')), |
||
97 | NumericField::create('Uses', _t('Discount.USES', 'Maximum number of uses')), |
||
98 | $validFrom = DateField::create('ValidFrom', _t('Discount.VALID_FROM', 'Valid from')), |
||
99 | $validTill = DateField::create('ValidTill', _t('Discount.VALID_TILL', 'Valid till')), |
||
100 | TagField::create('Groups', _t('Discount.GROUPS', 'Constrain to groups'), Group::get()) |
||
101 | ->setShouldLazyLoad(true), |
||
102 | TagField::create('Events', _t('Discount.EVENTS', 'Constrain to events'), CalendarEvent::get()) |
||
103 | ->setShouldLazyLoad(true) |
||
104 | )); |
||
105 | |||
106 | $code->setDescription( |
||
107 | _t('Discount.CODE_HELP', 'The code is generated after saving') |
||
108 | ); |
||
109 | |||
110 | $validFrom |
||
111 | ->setConfig('showcalendar', true) |
||
112 | ->setDescription(_t('Discount.VALID_FROM_HELP', 'If no date is set the current date is used')); |
||
113 | $validTill |
||
114 | ->setConfig('showcalendar', true) |
||
115 | ->setDescription(_t('Discount.VALID_TILL_HELP', 'If no date is set the current date + 1 year is used')); |
||
116 | |||
117 | $fields->removeByName(array('Title')); |
||
118 | return $fields; |
||
119 | } |
||
120 | |||
121 | public function onBeforeWrite() |
||
144 | |||
145 | /** |
||
146 | * Return the table title |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getTableTitle() |
||
154 | |||
155 | /** |
||
156 | * Check if the discount exceeded the maximum uses |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function validateUses() |
||
164 | |||
165 | /** |
||
166 | * Calculate the discount |
||
167 | * |
||
168 | * @param float $total |
||
169 | * @param Reservation $reservation |
||
170 | */ |
||
171 | public function updateTotal(&$total, Reservation $reservation) |
||
192 | |||
193 | /** |
||
194 | * Check if the from and till dates are in the past and future |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function validateDate() |
||
207 | |||
208 | /** |
||
209 | * Validate the given member with the allowed groups |
||
210 | * |
||
211 | * @param Member $member |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function validateGroups(Member $member = null) |
||
230 | |||
231 | /** |
||
232 | * Validate if the given event is in the group of allowed events |
||
233 | * |
||
234 | * @param CalendarEvent $event |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | public function validateEvents(CalendarEvent $event) |
||
252 | |||
253 | /** |
||
254 | * Generate a unique coupon code |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function generateCode() |
||
262 | } |
||
263 |