1
|
|
|
<?php |
2
|
|
|
require_once __DIR__ . '/AbstractValidator.php'; |
3
|
|
|
require_once __DIR__ . '/../class/bbcvols.class.php'; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Validate a simple order. |
8
|
|
|
* |
9
|
|
|
* @author Laurent De Coninck <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class SimpleOrderValidator 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
|
|
|
* @param $defaultServiceId |
29
|
|
|
*/ |
30
|
|
|
private function fetchService($defaultServiceId) |
31
|
|
|
{ |
32
|
|
|
$this->defaultService = new Product($this->db); |
33
|
|
|
$this->defaultService->fetch($defaultServiceId); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a value as input and validates it. If an error occurs, it returns error messages. |
38
|
|
|
* |
39
|
|
|
* @param stdClass $value |
40
|
|
|
* @param array $context |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function isValid($value, $context = []) |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
$this->valid = true; |
48
|
|
|
|
49
|
|
|
if (empty($value->name)) { |
50
|
|
|
$this->addError('name', 'Le nom est requis pour créer une commande'); |
51
|
|
|
} |
52
|
|
|
if (empty($value->email) && empty($value->phone)) { |
53
|
|
|
$this->addWarning('Soit l\'e-mail soit le téléphone n\'a pas été complété'); |
54
|
|
|
} |
55
|
|
|
if (empty($value->nbrPax) || (int) $value->nbrPax <= 0) { |
56
|
|
|
$this->addError('nbrPax', 'Le nombre de passagers doit être plus grand que 0.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ((int) $value->nbrPax > 0) { |
60
|
|
|
$costPerPax = $value->cost / (int) $value->nbrPax; |
61
|
|
|
if ($costPerPax < $this->getMinPrice()) { |
62
|
|
|
$this->addError('cost', |
63
|
|
|
sprintf('Le prix demandé par passagé est trop peu élevé. Un minimum de %s est demandé', |
64
|
|
|
$this->getMinPrice())); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->valid; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the minimum price. |
73
|
|
|
* |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
private function getMinPrice() |
77
|
|
|
{ |
78
|
|
|
if ($this->defaultService->price_base_type == 'TTC') { |
79
|
|
|
return $this->defaultService->price_min; |
80
|
|
|
} |
81
|
|
|
return $this->defaultService->price_min_ttc; |
82
|
|
|
} |
83
|
|
|
} |