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