1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/AbstractValidator.php'; |
4
|
|
|
require_once __DIR__ . '/../class/bbcvols.class.php'; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Validates a flight |
8
|
|
|
* |
9
|
|
|
* @author Laurent De Coninck <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class FlightValidator extends AbstractValidator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Product |
15
|
|
|
*/ |
16
|
|
|
private $defaultService; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Translate $langs, DoliDB $db, $defaultServiceId) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($langs, $db); |
24
|
|
|
$this->fetchService($defaultServiceId); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function isValid($vol, $context = []) |
31
|
|
|
{ |
32
|
|
|
if (!($vol instanceof Bbcvols)) { |
33
|
|
|
throw new \InvalidArgumentException('Flight validator only accepts a flight'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->valid = true; |
37
|
|
|
|
38
|
|
|
$this->checkFlightInformation($vol) |
39
|
|
|
->checkPassengersInformation($vol) |
40
|
|
|
->checkInstructionInformation($vol, $context) |
41
|
|
|
->checkBillingInformation($vol, $context) |
42
|
|
|
->checkKilometers($vol) |
43
|
|
|
->checkOrderInformation($vol); |
44
|
|
|
|
45
|
|
|
return $this->valid; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $context |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
private function isGroupedFlight($context) |
54
|
|
|
{ |
55
|
|
|
return key_exists('grouped_flight', $context) && !empty($context['grouped_flight']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $hour |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
private function isHourValid($hour) |
64
|
|
|
{ |
65
|
|
|
$patern = '(([0-9]{4})|([0-9]{2}:[0-9]{2}(:[0-9]{2})?))'; |
66
|
|
|
return !(preg_match($patern, $hour) == 0 || (strlen($hour) != 4 && strlen($hour) != 8 && strlen($hour) != 5)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $defaultServiceId |
71
|
|
|
*/ |
72
|
|
|
private function fetchService($defaultServiceId) |
73
|
|
|
{ |
74
|
|
|
$this->defaultService = new Product($this->db); |
75
|
|
|
$this->defaultService->fetch($defaultServiceId); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the minimum price. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
private function getMinPrice() |
84
|
|
|
{ |
85
|
|
|
if ($this->defaultService->price_base_type == 'TTC') { |
86
|
|
|
return $this->defaultService->price_min; |
87
|
|
|
} |
88
|
|
|
return $this->defaultService->price_min_ttc; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Bbcvols $vol |
93
|
|
|
* @param array $context |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
private function checkBillingInformation($vol, $context) |
98
|
|
|
{ |
99
|
|
|
if ($vol->isLinkedToOrder() || $vol->isBilled()) { |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ((!$this->isGroupedFlight($context) || $vol->getPilotId() === $vol->getFkReceiver()) && $vol->getFlightType()->isBillingRequired() && $vol->isFree()) { |
104
|
|
|
$this->addError('cost', 'Erreur ce type de vol doit être payant.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($vol->getFlightType()->isBillingRequired() && !$vol->hasReceiver()) { |
108
|
|
|
$this->addError('cost', |
109
|
|
|
'Erreur ce type de vol doit être payant, mais personne n\'a été signalé comme recepteur d\'argent.'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!$this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && ($vol->getAmountPerPassenger()) < $this->getMinPrice()) { |
113
|
|
|
$this->addError('cost', |
114
|
|
|
sprintf('Le montant demandé pour ce vol n\'est pas suffisant un minimum de %s euros est demandé', |
115
|
|
|
$this->getMinPrice())); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Bbcvols $vol |
123
|
|
|
* @param array $context |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
private function checkInstructionInformation($vol, $context) |
128
|
|
|
{ |
129
|
|
|
if ($vol->isInstruction()) { |
130
|
|
|
if ($vol->getPilotId() === $vol->getOrganisatorId()) { |
131
|
|
|
$this->addError('organisator', |
132
|
|
|
'l\'organisateur d\'un vol d\'instruction doit être l\'instructeur et non le pilote'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($this->isGroupedFlight($context)) { |
136
|
|
|
$this->addError('alone', "Le vol d'instruction est un vol à un seul ballon."); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Bbcvols $vol |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
private function checkPassengersInformation($vol) |
149
|
|
|
{ |
150
|
|
|
if (!is_numeric($vol->nbrPax) || (int) $vol->nbrPax < 0) { |
151
|
|
|
$this->addError('nbrPax', 'Erreur le nombre de passager est un nombre négatif.'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if (!$vol->mustHavePax()) { |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!$vol->hasPax()) { |
159
|
|
|
$this->addError('nbrPax', 'Erreur ce type de vol doit etre fait avec des passagers.'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (empty(trim($vol->getPassengerNames()))) { |
163
|
|
|
$this->addError('passenger_names', 'Le nom des passagers est obligatoire.'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$passengers = explode(';', $vol->getPassengerNames()); |
167
|
|
|
if (count($passengers) !== $vol->getNumberOfPassengers()) { |
168
|
|
|
$this->addError('passenger_names', |
169
|
|
|
'Le nombre de noms des passagers doit être égale au nombre de passagers.'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param Bbcvols $vol |
177
|
|
|
* |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
|
|
private function checkKilometers($vol) |
181
|
|
|
{ |
182
|
|
|
if ($vol->hasKilometers() && !$vol->getKilometers() > 0) { |
183
|
|
|
$this->addError('kilometers', |
184
|
|
|
'Les kilometres doivent être un nombre positif'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($vol->hasKilometers() && !$vol->hasKilometersDescription()) { |
188
|
|
|
$this->addError('justif_kilometers', |
189
|
|
|
'Vous essayez d\'encoder des kilometres sans justificatif.'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param Bbcvols $vol |
197
|
|
|
* |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
private function checkFlightInformation($vol) |
201
|
|
|
{ |
202
|
|
|
if (!$this->isHourValid($vol->heureD)) { |
203
|
|
|
$this->addError('heureD', "L'heure depart n'est pas correcte'"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (!$this->isHourValid($vol->heureA)) { |
207
|
|
|
$this->addError('heureA', 'L\'heure d\'arrivee n\'est pas correcte'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (empty($this->errors) && ($vol->heureA - $vol->heureD) <= 0) { |
211
|
|
|
$this->addError('heures', 'L\'heure de depart est plus grande que l\'heure d\'arrivee'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (empty($vol->lieuD)) { |
215
|
|
|
$this->addError('lieuD', 'Le lieu de départ est vide'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if (empty($vol->lieuA)) { |
219
|
|
|
$this->addError('lieuA', 'Le lieu d\'arrivée est vide'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param Bbcvols $vol |
227
|
|
|
* @return $this|void |
228
|
|
|
*/ |
229
|
|
|
private function checkOrderInformation(Bbcvols $vol) |
230
|
|
|
{ |
231
|
|
|
if(!$vol->isLinkedToOrder()){ |
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$totalPassenegrs = 0; |
236
|
|
|
foreach($vol->getOrderIds() as $orderId => $nbrPassengers){ |
237
|
|
|
$totalPassenegrs+=(int)$nbrPassengers; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if($totalPassenegrs !== (int)$vol->getNumberOfPassengers()){ |
241
|
|
|
$this->addError('nbrPax', 'Le nombre de passagers ne correspond pas au nombre entré sur les commandes'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
} |