1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DiscountForm.php |
4
|
|
|
* |
5
|
|
|
* @author Bram de Leeuw |
6
|
|
|
* Date: 10/03/17 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
10
|
|
|
|
11
|
|
|
use FieldList; |
12
|
|
|
use FormAction; |
13
|
|
|
use TextField; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class DiscountForm |
17
|
|
|
* |
18
|
|
|
* @package Broarm\EventTickets |
19
|
|
|
*/ |
20
|
|
|
class DiscountField extends TextField |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var DiscountForm |
24
|
|
|
*/ |
25
|
|
|
protected $form; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Validate the discount if it is set by... |
29
|
|
|
* Checking if it exists |
30
|
|
|
* Checking if it has uses left |
31
|
|
|
* Checking if it has a valid date |
32
|
|
|
* Checking if the event is valid |
33
|
|
|
* Checking if the discount is valid on one of the registered members |
34
|
|
|
* TODO: move all these checks to the discount itself? make a method that returns a error message |
35
|
|
|
* |
36
|
|
|
* @param \Validator $validator |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function validate($validator) |
41
|
|
|
{ |
42
|
|
|
// If no discount is set continue doing default validation |
43
|
|
|
if (!isset($this->value) || empty($this->value)) { |
44
|
|
|
return parent::validate($validator); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var Discount $discount */ |
48
|
|
|
// Check if the discount exists |
49
|
|
|
if (!$discount = Discount::get()->find('Code', $this->value)) { |
50
|
|
|
$validator->validationError($this->name, _t( |
51
|
|
|
'DiscountField.VALIDATION_NOT_FOUND', |
52
|
|
|
'The entered coupon is not found' |
53
|
|
|
), 'validation'); |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Check if the discount is already used |
59
|
|
|
if ($discount->validateUses()) { |
60
|
|
|
$validator->validationError($this->name, _t( |
61
|
|
|
'DiscountField.VALIDATION_USED_CHECK', |
62
|
|
|
'The entered coupon is already used' |
63
|
|
|
), 'validation'); |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @var DiscountForm form */ |
69
|
|
|
//$this->form; |
70
|
|
|
|
71
|
|
|
// Check if the coupon is expired |
72
|
|
|
if (!$checkDate = $discount->validateDate()) { |
73
|
|
|
$validator->validationError($this->name, _t( |
74
|
|
|
'DiscountField.VALIDATION_DATE_CHECK', |
75
|
|
|
'The coupon is expired' |
76
|
|
|
), 'validation'); |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Check if the coupon is allowed on this event |
82
|
|
|
if (!$checkEvent = $discount->validateEvents($this->form->getReservation()->Event())) { |
83
|
|
|
$validator->validationError($this->name, _t( |
84
|
|
|
'DiscountField.VALIDATION_EVENT_CHECK', |
85
|
|
|
'The coupon is not allowed on this event' |
86
|
|
|
), 'validation'); |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// If groups are required check if one of the attendees is in the required group |
92
|
|
|
if (!$checkMember = $discount->validateGroups()) { |
93
|
|
|
foreach ($this->form->getReservation()->Attendees() as $attendee) { |
94
|
|
|
/** @var Attendee $attendee */ |
95
|
|
|
if ($attendee->Member()->exists() && $member = $attendee->Member()) { |
96
|
|
|
if ($checkMember = $discount->validateGroups($member)) { |
97
|
|
|
// If one of the member is part of the group validate the discount |
98
|
|
|
break; |
99
|
|
|
} else { |
100
|
|
|
$checkMember = false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$checkMember) { |
107
|
|
|
$validator->validationError($this->name, _t( |
108
|
|
|
'DiscountField.VALIDATION_MEMBER_CHECK', |
109
|
|
|
'None of the attendees is allowed to use this coupon' |
110
|
|
|
), 'validation'); |
111
|
|
|
|
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$discount->write(); |
116
|
|
|
$this->form->getReservation()->PriceModifiers()->add($discount); |
117
|
|
|
$this->form->getReservation()->calculateTotal(); |
118
|
|
|
$this->form->getReservation()->write(); |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
} |