From b0f06b1d7dc3a790d6d8babe5f0855c08c5f7878 Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 20 Oct 2026 21:49:17 +0200 Subject: =?UTF-8?q?mise=20en=20place=20de=20l'h=C3=A9ritage=202,=20nombreu?= =?UTF-8?q?ses=20simplifications=20et=20factorisations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/model/entities/Prestation.php | 139 +++++++++++++++----------------------- 1 file changed, 53 insertions(+), 86 deletions(-) (limited to 'src/model/entities/Prestation.php') diff --git a/src/model/entities/Prestation.php b/src/model/entities/Prestation.php index fe18b70..6fef8b6 100644 --- a/src/model/entities/Prestation.php +++ b/src/model/entities/Prestation.php @@ -28,19 +28,19 @@ abstract class Prestation #[ORM\ManyToOne(targetEntity: Client::class, cascade: ['persist'])] #[ORM\JoinColumn(name: 'id_client', referencedColumnName: 'id')] - private ?Client $client = null; // type int en SQL et dans la base + private Client $client; // type int en SQL et dans la base #[ORM\Column] - private string $code_presta; + protected string $code_presta; #[ORM\Column(type: 'string', length: 10)] // format 'aaaa-mm-jj' - private string $date; + 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; + public static EntityManager $entityManager; // inséré dans main.php - public function __construct(?Client $client = null){ + public function __construct(Client $client){ $this->setClient($client); } @@ -57,10 +57,17 @@ abstract class Prestation { return $this->date; } - /*public function getTypePresta(): string + public function getTypePresta(): string { - return $this->type_presta; - }*/ + 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; @@ -75,7 +82,7 @@ abstract class Prestation return get_object_vars($this); } - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne 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() @@ -85,31 +92,7 @@ abstract class Prestation "Commentaires:" => $this->commentaires]; } - // une interface pour ça c'est bien aussi non? - public function getFacture(): ?Facture - { - return $this->facture; - } - public function getDevis(): ?Devis - { - return $this->devis; - } - public function getCesu(): ?CESU - { - return $this->cesu; - } - public function getLocation(): ?Location - { - return $this->location; - } - // setters - //~ public function setCodePresta(string $value) - //~ { - //~ $this->code_presta = $value; - //~ return $this; - //~ } - //public function setDate(int $value) public function setDate(string $date): Prestation { $this->date = $date; @@ -125,36 +108,24 @@ abstract class Prestation }*/ } - /*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) + public function setModePaiement(string $value): Prestation { $this->mode_paiement = $value; return $this; } - public function setCommentaires(string $input) + public function setCommentaires(string $input): void { $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? + private function setClient(Client $client): void // private? { - $this->client = $input; - //$input->typeToClient(); // on modifie $client ici (merci les références PHP) + $this->client = $client; } - /*public function set(string $entry, string $input): void + public function set(string $entry, string $input): void { $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide @@ -167,8 +138,7 @@ abstract class Prestation 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); + //~ case "Type de Presta:": // choix impossible pour le moment et pas anodin //~ break; case "Mode de paiement:": $this->setModePaiement($input); @@ -177,12 +147,13 @@ abstract class Prestation $this->setCommentaires($input); break; } - }*/ + } // 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'); // prestas de tout type d'un client + $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); @@ -192,7 +163,7 @@ abstract class Prestation $date_array[1], // mois $date_array[2], // jour $this->client->getCodeClient(), // mise à jour éventuelle - //$this->type_presta, + $this->getTypePresta(), $how_many_times ]; $this->code_presta = implode('-', $array_code); @@ -208,37 +179,27 @@ abstract class Prestation //~ return $this; //~ } - public static function getServices(Client $Customer, string $type = '') + 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 = :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;*/ + /*$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('Prestation'); - $find_by = ['client' => $Customer->getId()]; - if($type != '') - { - //$find_by['type_presta'] = 'devis'; - } - $PrestaList = $repository->findBy($find_by); + $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) - { - $id = $Presta->getId(); - $PrestaListIdKeys[$id] = $Presta; + foreach($PrestaList as $Presta){ + $PrestaListIdKeys[$Presta->getId()] = $Presta; } // fenêtre $entrees = []; - foreach($PrestaListIdKeys as $Presta) - { + foreach($PrestaListIdKeys as $Presta){ $id = $Presta->getId(); $entrees[$id][] = $id; $entrees[$id][] = $Presta->getDate(); @@ -252,22 +213,28 @@ abstract class Prestation // choix de l'utilisateur $input = exec($ResultatsRecherchePresta->get()); // $input est l'ID de la prestation - //echo "input = " . $input . "\n"; - if($input == '') - { + if($input == ''){ echo "debug: recherche annulée ou saisie vide\n"; - return 0; + return null; } - else - { + else{ return $PrestaListIdKeys[$input]; } } - - // à mettre plus tard dans une classe mère - private function cleanSpecialChars(string $data): string + + // nettoyage caractères spéciaux: " ` + public function cleanSpecialChars(string $answers): string { - $search = ['"']; - return str_replace($search, '', $data); + $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); } } -- cgit v1.2.3