1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
<?php
// src/entities/Prestation.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
#[ORM\Entity]
#[ORM\Table(name: 'prestations')]
class Prestation
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\ManyToOne(targetEntity: Client::class, cascade: ['persist'])]
#[ORM\JoinColumn(name: 'id_client', referencedColumnName: 'id')]
private Client|null $client = null; // type int en SQL et dans la base
#[ORM\Column]
private string $code_presta;
#[ORM\Column]
private int $date; // timestamp unix
#[ORM\Column]
private string $type_presta;
#[ORM\Column]
private string $mode_paiement = ''; // non renseigné quand on fait un devis
#[ORM\Column]
private string $commentaires;
public static EntityManager $entityManager;
public function __construct(Client $client = null)
{
if($client != null)
{
$this->setClient($client);
}
}
// getters
public function getId(): int
{
return $this->id;
}
public function getCodePresta(): string
{
return $this->code_presta;
}
public function getDate(): int
{
return $this->date;
}
public function getTypePresta(): string
{
return $this->type_presta;
}
public function getClient(): Client
{
return $this->client;
}
public function getAll(): array
{
// n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés
return get_object_vars($this);
}
public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this)
{
$code_presta_tableau = explode('-', $this->code_presta);
$Date = new Dates($this->date);
return [
//"Numéro prestation:" => end($code_presta_tableau), // crée des conflits, calcul automatique par setCodePresta()
"Date:" => $Date->getDate(),
//"Type de Presta:" => $this->type_presta, // choix impossible pour le moment
"Mode de paiement:" => $this->mode_paiement, // non pertinent pour un devis
"Commentaires:" => $this->commentaires];
}
// setters
//~ public function setCodePresta(string $value)
//~ {
//~ $this->code_presta = $value;
//~ return $this;
//~ }
//public function setDate(int $value)
public function setDate(Dates $Date)
{
//$this->date = (int)$value; // attend un timestamp
$this->date = $Date->getTimestamp();
return $this;
/*if($set_code_presta)
{
$code_presta_tableau = explode('-', $this->code_presta);
$Date = new Dates($value);
$code_presta_tableau[0] = $Date->getYear();
$code_presta_tableau[1] = $Date->getMonth();
$code_presta_tableau[2] = $Date->getDay();
$this->code_presta = implode('-', $code_presta_tableau);
}*/
}
public function setTypePresta(string $value) // appelée dans la section 2 uniquement, pour la 3 on verra plus tard
{
$this->type_presta = $value;
return $this;
}
public function setModePaiement(string $value)
{
$this->mode_paiement = $value;
return $this;
}
public function setCommentaires(string $input)
{
$this->commentaires = $input;
}
/*public function setAll(array $input)
{
$this->code_presta = $input[0];
$this->date = $input[1];
$this->type_presta = $input[2];
$this->mode_paiement = $input[3];
$this->commentaires = $input[4];
}*/
private function setClient(Client $input) // private?
{
$this->client = $input;
//$input->typeToClient(); // on modifie $client ici (merci les références PHP)
}
public function set(string $entry, $input) // $input = chaîne ou entier
{
if(gettype($input) === 'string')
{
$input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide
}
switch($entry)
{
// pas de cas "code presta" qui généré par setCodePresta, on peut modifier la date (ici) et le code client (ailleurs)
//~ case "Numéro prestation:":
//~ $this->setNumeroPresta($input);
//~ // modifier le code presta en conséquence
//~ break;
case "Date:": // inutile, setDate() est appelé directement après choix fenêtre calendrier
$this->setDate($input);
break;
//~ case "Type de Presta:": // choix impossible pour le moment
//~ $this->setTypePresta($input);
//~ break;
case "Mode de paiement:":
$this->setModePaiement($input);
break;
case "Commentaires:":
$this->setCommentaires($input);
break;
}
}
// code presta = année-mois-jour-codeclient-typedepresta-combientièmefois
public function setCodePresta(int $increment) // 0 pour modif, 1 pour nouvelle presta
{
$Date = new Dates($this->date);
$repository = self::$entityManager->getRepository('Prestation'); // prestas de tout type d'un client
// attention: il peut y avoir plusieurs prestations avec le même numéro au compteur, à améliorer
$combientieme_fois = count($repository->findBy(['client' => $this->client->getId()])) + $increment;
//var_dump($combientieme_fois);
$array_code = [$Date->getYear(), $Date->getMonth(), $Date->getDay(), $this->client->getCodeClient(), $this->type_presta, $combientieme_fois];
//~ $array_code[0] = $Date->getYear();
//~ $array_code[1] = $Date->getMonth();
//~ $array_code[2] = $Date->getDay();
//~ $array_code[3] = $this->client->getCodeClient(); // mise à jour éventuelle
//~ $array_code[4] = $this->type_presta;
$this->code_presta = implode('-', $array_code);
return $this;
}
// combientième fois au choix (ne fonctionne pas avec setCodePresta qui recalculera ce numéro automatiquement à la prochaine modification)
//~ public function setNumeroPresta($value)
//~ {
//~ // modifier le code presta, on pourrait aussi utiliser une regex
//~ $code_presta_tableau = explode('-', $this->code_presta);
//~ $code_presta_tableau[count($code_presta_tableau) - 1] = (int) $value; // dernière case du tableau
//~ $this->code_presta = implode('-', $code_presta_tableau);
//~ return $this;
//~ }
public static function getServices(Client $Customer, string $type = '')
{
echo "debug: recherche d'une prestation\n";
// requête DQL qui fait la même chose que le findBy plus bas
/*$query = self::$entityManager->createQuery('SELECT u FROM Prestation u WHERE u.client = :id_client AND u.type_presta = :type_presta');
$query->setParameter('id_client', $Customer->getId());
$query->setParameter('type_presta', $type);
$query_result = $query->getResult();
var_dump($query_result);die;*/
$repository = self::$entityManager->getRepository('Prestation');
$find_by = ['client' => $Customer->getId()];
if($type != '')
{
$find_by['type_presta'] = 'devis';
}
$PrestaList = $repository->findBy($find_by);
// le même tableau avec pour clés les id des objets
$PrestaListIdKeys = [];
foreach($PrestaList as $Presta)
{
$id = $Presta->getId();
$PrestaListIdKeys[$id] = $Presta;
}
// fenêtre
$entrees = [];
foreach($PrestaListIdKeys as $Presta)
{
$id = $Presta->getId();
$entrees[$id][] = $id;
$Date = new Dates((int)$Presta->getDate()); // envoi du timestamp, (int) est là par sécurité
$entrees[$id][] = $Date->getDate();
$entrees[$id][] = $Presta->getTypePresta();
$entrees[$id][] = $Presta->getCodePresta();
}
// pas à la bonne place, couper la fonction en deux et mettre ça ailleurs!
$ResultatsRecherchePresta = new ZenityList(ZenitySetup::$resultats_recherche_presta['text'], []);
$ResultatsRecherchePresta->setListRows($entrees, 4);
// choix de l'utilisateur
$input = exec($ResultatsRecherchePresta->get()); // $input est l'ID de la prestation
//echo "input = " . $input . "\n";
if($input == '')
{
echo "debug: recherche annulée ou saisie vide\n";
return 0;
}
else
{
return $PrestaListIdKeys[$input];
}
}
// à mettre plus tard dans une classe mère
private function cleanSpecialChars(string $data): string
{
$search = ['"'];
return str_replace($search, '', $data);
}
}
|