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
|
<?php
// src/entities/Prestation.php
// MAJ champs id
// 1/ supprimer les champ id des filles (retirer contrainte clé primaire avant)
// 2/ renommer les champs id_presta des filles en id (même nom que l'id de la mère)
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
#[ORM\Entity]
#[ORM\Table(name: 'prestations')]
#[ORM\InheritanceType('JOINED')]
#[ORM\DiscriminatorColumn(name: 'type_presta', type: 'string')]
#[ORM\DiscriminatorMap([
'devis' => Devis::class,
'facture' => Facture::class,
'cesu' => CESU::class,
'location' => Location::class,
'non_vendue' => NonVendue::class
])]
abstract class Prestation
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Client::class, cascade: ['persist'])]
#[ORM\JoinColumn(name: 'id_client', referencedColumnName: 'id')]
private Client $client; // type int en SQL et dans la base
#[ORM\Column]
protected string $code_presta;
#[ORM\Column(type: 'string', length: 10)] // format 'aaaa-mm-jj'
protected string $date;
#[ORM\Column]
private string $mode_paiement = ''; // non renseigné quand on fait un devis
#[ORM\Column]
private string $commentaires;
public static EntityManager $entityManager; // inséré dans main.php
public function __construct(Client $client){
$this->setClient($client);
}
// getters
public function getId(): int
{
return $this->id;
}
public function getCodePresta(): string
{
return $this->code_presta;
}
public function getDate(): string
{
return $this->date;
}
public function getTypePresta(): string
{
return match (true) {
$this instanceof Devis => 'devis',
$this instanceof Facture => 'facture',
$this instanceof CESU => 'cesu',
$this instanceof Location => 'location',
$this instanceof NonVendue => 'non_vendue',
//default => throw new \LogicException('Type de prestation inconnu'), // impossible
};
}
public function getModePaiement(): string
{
return $this->mode_paiement;
}
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 getAll() qui retourne get_object_vars($this)
{
return [
//"Numéro prestation:" => end($code_presta_tableau), // crée des conflits, calcul automatique par setCodePresta()
"Date:" => $this->date,
//"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 setDate(string $date): Prestation
{
$this->date = $date;
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 setModePaiement(string $value): Prestation
{
$this->mode_paiement = $value;
return $this;
}
public function setCommentaires(string $input): void
{
$this->commentaires = $input;
}
/*public function setAll(array $input)
{}*/
private function setClient(Client $client): void // private?
{
$this->client = $client;
}
public function set(string $entry, string $input): void
{
$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 et pas anodin
//~ break;
case "Mode de paiement:":
$this->setModePaiement($input);
break;
case "Commentaires:":
$this->setCommentaires($input);
break;
default:
echo "erreur: entrée non reconnu dans Prestation::set\n";
}
}
// code presta = année-mois-jour-codeclient-typedepresta-combientièmefois
public function setCodePresta(int $increment): Prestation // 0 pour modif, 1 pour nouvelle presta
{
$repository = self::$entityManager->getRepository('Prestation');
// attention: il peut y avoir plusieurs prestations avec le même numéro au compteur, à améliorer
$how_many_times = count($repository->findBy(['client' => $this->client->getId()])) + $increment;
//var_dump($how_many_times);
$date_array = explode('-', $this->date);
$array_code = [
$date_array[0], // année
$date_array[1], // mois
$date_array[2], // jour
$this->client->getCodeClient(), // mise à jour éventuelle
$this->getTypePresta(),
$how_many_times
];
$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_class = Prestation::class): ?Prestation // param par défaut = recherche tout 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 = :client AND u INSTANCE OF ' . $type_class);
$query->setParameter('client', $Customer);
$PrestaList = $query->getResult();*/
$repository = self::$entityManager->getRepository($type_class);
$PrestaList = $repository->findBy(['client' => $Customer]);
// le même tableau avec pour clés les id des objets
$PrestaListIdKeys = [];
foreach($PrestaList as $Presta){
$PrestaListIdKeys[$Presta->getId()] = $Presta;
}
// fenêtre
$entrees = [];
foreach($PrestaListIdKeys as $Presta){
$id = $Presta->getId();
$entrees[$id][] = $id;
$entrees[$id][] = $Presta->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
if($input == ''){
echo "debug: recherche annulée ou saisie vide\n";
return null;
}
else{
return $PrestaListIdKeys[$input];
}
}
// nettoyage caractères spéciaux: " `
public function cleanSpecialChars(string $answers): string
{
$targets = ['"', '`'];
return str_replace($targets, '', $answers); // possibilité que des input deviennent vide
}
public function answersToArray(string $answers): array|false
{
if($answers === ''){
echo "erreur: chaîne vide dans answersToArray()\n";
return false;
}
return explode('|', $answers);
}
}
|