|
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
|
|
|
if (!$this->isHourValid($vol->heureD)) { |
|
39
|
|
|
$this->addError('heureD', "L'heure depart n'est pas correcte'"); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!$this->isHourValid($vol->heureA)) { |
|
43
|
|
|
$this->addError('heureA', 'L\'heure d\'arrivee n\'est pas correcte'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (empty($this->errors) && ($vol->heureA - $vol->heureD) <= 0) { |
|
47
|
|
|
$this->addError('heures', 'L\'heure de depart est plus grande que l\'heure d\'arrivee'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (empty($vol->lieuD)) { |
|
51
|
|
|
$this->addError('lieuD', 'Le lieu de départ est vide'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (empty($vol->lieuA)) { |
|
55
|
|
|
$this->addError('lieuA', 'Le lieu d\'arrivée est vide'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// PAX |
|
59
|
|
|
if (!is_numeric($vol->nbrPax) || (int) $vol->nbrPax < 0) { |
|
60
|
|
|
$this->addError('nbrPax', 'Erreur le nombre de passager est un nombre négatif.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if($vol->mustHavePax()){ |
|
64
|
|
|
if (!$vol->hasPax()) { |
|
65
|
|
|
$this->addError('nbrPax', 'Erreur ce type de vol doit etre fait avec des passagers.'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if(empty(trim($vol->getPassengerNames()))){ |
|
69
|
|
|
$this->addError('passenger_names', 'Le nom des passagers est obligatoire.'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if(empty(trim($vol->getPassengerNames()))){ |
|
73
|
|
|
$this->addError('passenger_names', 'Le nom des passagers est obligatoire.'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$passengers = explode(';', $vol->getPassengerNames()); |
|
77
|
|
|
if(count($passengers) !== $vol->getNumberOfPassengers()){ |
|
78
|
|
|
$this->addError('passenger_names', 'Le nombre de noms des passagers doit être égale au nombre de passagers.'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if($vol->isInstruction()){ |
|
83
|
|
|
if($vol->getPilotId() === $vol->getOrganisatorId()){ |
|
84
|
|
|
$this->addError('organisator', 'l\'organisateur d\'un vol d\'instruction doit être l\'instructeur et non le pilote'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if($this->isGroupedFlight($context)){ |
|
88
|
|
|
$this->addError('alone', "Le vol d'instruction est un vol à un seul ballon."); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// verification billing |
|
93
|
|
|
if ($this->isGroupedFlight($context) && $vol->getFlightType()->isBillingRequired() && $vol->isFree()) { |
|
94
|
|
|
$this->addError('cost', 'Erreur ce type de vol doit être payant.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ($vol->getFlightType()->isBillingRequired() && !$vol->hasReceiver()) { |
|
98
|
|
|
$this->addError('cost', |
|
99
|
|
|
'Erreur ce type de vol doit être payant, mais personne n\'a été signalé comme recepteur d\'argent.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($vol->getFlightType()->isBillingRequired() && ($vol->getAmountPerPassenger()) < $this->getMinPrice()) { |
|
103
|
|
|
$this->addError('cost', |
|
104
|
|
|
sprintf('Le montant demandé pour ce vol n\'est pas suffisant un minimum de %s euros est demandé', |
|
105
|
|
|
$this->getMinPrice())); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
//Kilometers |
|
109
|
|
|
if ($vol->hasKilometers() && !$vol->getKilometers() > 0) { |
|
110
|
|
|
$this->addError('kilometers', |
|
111
|
|
|
'Les kilometres doivent être un nombre positif'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($vol->hasKilometers() && !$vol->hasKilometersDescription()) { |
|
115
|
|
|
$this->addError('justif_kilometers', |
|
116
|
|
|
'Vous essayez d\'encoder des kilometres sans justificatif.'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->valid; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $context |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
private function isGroupedFlight($context) |
|
128
|
|
|
{ |
|
129
|
|
|
return key_exists('grouped_flight', $context) && !empty($context['grouped_flight']); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $hour |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
|
|
private function isHourValid($hour) |
|
138
|
|
|
{ |
|
139
|
|
|
$patern = '#[0-9]{4}#'; |
|
140
|
|
|
return !(preg_match($patern, $hour) == 0 || strlen($hour) != 4); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param $defaultServiceId |
|
145
|
|
|
*/ |
|
146
|
|
|
private function fetchService($defaultServiceId) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->defaultService = new Product($this->db); |
|
149
|
|
|
$this->defaultService->fetch($defaultServiceId); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns the minimum price. |
|
154
|
|
|
* |
|
155
|
|
|
* @return int |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getMinPrice() |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->defaultService->price_base_type == 'TTC') { |
|
160
|
|
|
return $this->defaultService->price_min; |
|
161
|
|
|
} |
|
162
|
|
|
return $this->defaultService->price_min_ttc; |
|
163
|
|
|
} |
|
164
|
|
|
} |