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/CESU.php | 71 ++++++------------- src/model/entities/Devis.php | 98 +++++++-------------------- src/model/entities/Facture.php | 113 ++++++++++++------------------- src/model/entities/Location.php | 63 +++++------------ src/model/entities/NonVendue.php | 32 +++++++++ src/model/entities/Prestation.php | 139 +++++++++++++++----------------------- 6 files changed, 193 insertions(+), 323 deletions(-) (limited to 'src/model') diff --git a/src/model/entities/CESU.php b/src/model/entities/CESU.php index b840ac9..3e7ab10 100644 --- a/src/model/entities/CESU.php +++ b/src/model/entities/CESU.php @@ -2,7 +2,6 @@ // src/entities/CESU.php use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\EntityManager; #[ORM\Entity] #[ORM\Table(name: 'cesu')] @@ -18,17 +17,7 @@ class CESU extends Prestation /*#[ORM\Column]*/ // pour plus tard private ?string $date_bulletin_paie = null; // format Y-m-d conservé tout du long: formulaire, BDD, affichage - public static EntityManager $entityManager; - - public function __construct(?Prestation $presta = null){ - $this->setPresta($presta); - } - // getters - public function getPresta(): ?Prestation - { - return $this->presta; - } public function getSalaire(): float { return $this->salaire; @@ -41,14 +30,15 @@ class CESU extends Prestation public function getAllWithWindowFields(): array { - return [ + $common_data = parent::getAllWithWindowFields(); + return array_merge($common_data, [ "Tâche effectuée:" => $this->taches, "Durée du travail:" => $this->duree_travail, - "Salaire:" => $this->salaire]; + "Salaire:" => $this->salaire]); } // setters - public function set(string $entry, string $input) + public function set(string $entry, string $input): void { $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaîne vide switch($entry) @@ -62,11 +52,9 @@ class CESU extends Prestation case "Salaire:": $this->setSalaire($input); break; - } - } - private function setPresta(Prestation $input): void - { - $this->presta = $input; + default: // gérer les champ dans Prestation + parent::set($entry, $input); + } } public function setTaches(string $value): CESU { @@ -78,7 +66,7 @@ class CESU extends Prestation $this->duree_travail = $value; return($this); } - public function setSalaire($value): CESU + public function setSalaire(string $value): CESU { $value = str_replace(',', '.', $value); $this->salaire = (float) $value; @@ -97,38 +85,23 @@ class CESU extends Prestation $this->salaire = (float)$input[2]; } - // à mettre plus tard dans une classe mère - protected function cleanSpecialChars(string $data): string - { - $search = ['"']; - return str_replace($search, '', $data); - } - // à mettre plus tard dans une classe mère - public function hydrate(string $answers): void + public function validateForm(array $data_array): bool { - $answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide - if($answers == '') - { - echo "erreur de CESU::hydrate(), la chaine \$answers est vide.\n"; - return; - } - $data_array = explode('|', $answers); // array - - if(count($data_array) === 4) // facture normale - { - $this->getPresta()->setModePaiement($data_array[3]); - //array_pop($data_array); // supprime la dernière case - unset($data_array[3]); - $this->setAll($data_array); + if(count($data_array) === 4){ + return true; } - else - { - echo "erreur de CESU::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n"; - return; + else{ + echo "erreur de CESU::validateForm(), le tableau \$data_array n'a pas la taille attendue.\n"; + return false; } + } - //self::$entityManager->persist('Prestation'); - self::$entityManager->persist($this); // $Presta avec en cascade! - self::$entityManager->flush(); + // à mettre plus tard dans une classe mère + public function hydrate(array $data_array): void + { + $this->setModePaiement($data_array[3]); + //array_pop($data_array); // supprime la dernière case + unset($data_array[3]); + $this->setAll($data_array); } } diff --git a/src/model/entities/Devis.php b/src/model/entities/Devis.php index f559243..3f051c2 100644 --- a/src/model/entities/Devis.php +++ b/src/model/entities/Devis.php @@ -2,7 +2,6 @@ // src/entities/Devis.php use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\EntityManager; #[ORM\Entity] #[ORM\Table(name: 'devis')] @@ -29,17 +28,7 @@ class Devis extends Prestation #[ORM\Column (options: ['default' => 'non'])] // défaut dans le cas d'un requête SQL ignorant ce champ private string $signature_devis = 'non'; - public static EntityManager $entityManager; - - public function __construct(?Prestation $presta = null){ - $this->setPresta($presta); - } - // getters - public function getPresta(): Prestation - { - return $this->presta; - } public function getAll(): array { // n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés @@ -48,19 +37,8 @@ class Devis extends Prestation public function getAllWithWindowFields(): array { - //~ $taches = ["Tâches:" => $this->taches]; - //~ $champs_communs = [ - //~ "Total Main d'oeuvre:" => $this->total_main_d_oeuvre, - //~ "Pièces:" => $this->pieces, - //~ "Total des pièces:" => $this->total_pieces, - //~ "Déplacement:" => $this->deplacement, - //~ "Total HT:" => $this->total_HT]; - //~ $champs_devis = [ - //~ "Delai de livraison:" => $this->delai_livraison, - //~ "Durée de validité:" => $this->validite_devis, - //~ "Devis signé:" => $this->signature_devis]; - //~ return $champs_communs + $champs_devis; - return [ + $common_data = parent::getAllWithWindowFields(); + return array_merge($common_data, [ "Tâches:" => $this->taches, // JUSTE AJOUTÉ "Total Main d'oeuvre:" => $this->total_main_d_oeuvre, "Pièces:" => $this->pieces, @@ -70,11 +48,11 @@ class Devis extends Prestation "Total HT:" => $this->total_HT, "Delai de livraison:" => $this->delai_livraison, "Durée de validité:" => $this->validite_devis, - "Devis signé:" => $this->signature_devis]; + "Devis signé:" => $this->signature_devis]); } // setters - public function set(string $entry, string $input) // trouve la bonne méthode + public function set(string $entry, string $input): void // trouve la bonne méthode { $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaîne vide switch($entry) @@ -109,41 +87,39 @@ class Devis extends Prestation case "Devis signé:": $this->setSignatureDevis($input); break; + default: // gérer les champ dans Prestation + parent::set($entry, $input); } } - private function setPresta(Prestation $input) // private? - { - $this->presta = $input; - } - public function setTaches(string $value) + public function setTaches(string $value): Devis { $this->taches = $value; return($this); } - public function setTotalMainDOeuvre($value) + public function setTotalMainDOeuvre(string $value): Devis { $value = str_replace(',', '.', $value); $this->total_main_d_oeuvre = (float) $value; // float "nettoie" tous les caractères après le dernier chiffre trouvé (ex: 50€ => 50, abc => 0) return($this); } - public function setPieces(string $value) + public function setPieces(string $value): Devis { $this->pieces = $value; return($this); } - public function setTotalPieces($value) + public function setTotalPieces(string $value): Devis { $value = str_replace(',', '.', $value); $this->total_pieces = (float) $value; return($this); } - public function setDeplacement($value) + public function setDeplacement(string $value): Devis { $value = str_replace(',', '.', $value); $this->deplacement = (float) $value; return($this); } - public function setTotalHT($value) + public function setTotalHT(string $value): Devis { $value = str_replace(',', '.', $value); $this->total_HT = (float) $value; @@ -155,23 +131,23 @@ class Devis extends Prestation $this->prix_devis = (float) $value; return($this); } - public function setDelaiLivraison(string $value) + public function setDelaiLivraison(string $value): Devis { $this->delai_livraison = $value; return($this); } - public function setValiditedevis(string $value) + public function setValiditedevis(string $value): Devis { $this->validite_devis = $value; return($this); } - public function setSignatureDevis(string $value) + public function setSignatureDevis(string $value): Devis { $this->signature_devis = $value; return($this); } - private function setAll(array $input) // private? + private function setAll(array $input): void // private? { $this->taches = $input[0]; $this->total_main_d_oeuvre = (float)$input[1]; @@ -186,43 +162,21 @@ class Devis extends Prestation // $signature_devis est renseigné plus tard en modifiant le devis } - // à mettre plus tard dans une classe mère - protected function cleanSpecialChars(string $data): string - { - $search = ['"']; - return str_replace($search, '', $data); - } - // à mettre plus tard dans une classe mère - public function hydrate(string $answers): void + public function validateForm(array $data_array): bool { - $answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide - if($answers == '') - { - echo "erreur de Devis::hydrate(), la chaine \$answers est vide.\n"; - return; + if(count($data_array) === 9){ + return true; } - $data_array = explode('|', $answers); // array - - if(count($data_array) === 9) // facture normale, "=== 9" = merdique - { - $this->getPresta()->setModePaiement(''); // sécurité, doctrine plante si "null" - $this->setAll($data_array); + else{ + echo "erreur de Devis::validateForm(), le tableau \$data_array n'a pas la taille attendue.\n"; + return false; } - else - { - echo "erreur de Devis::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n"; - return; - } - - //self::$entityManager->persist('Prestation'); - self::$entityManager->persist($this); // $Presta avec en cascade! - self::$entityManager->flush(); } - // création d'une facture à partir d'un devis - public static function getQuotation(Prestation $Quotation) + // à mettre plus tard dans une classe mère + public function hydrate(array $data_array): void { - $repository = self::$entityManager->getRepository('Devis'); - return $repository->findOneBy(['presta' => $Quotation->getId()]); // ne peut y en avoir deux + //$this->setModePaiement(''); // vaut déjà '' + $this->setAll($data_array); } } diff --git a/src/model/entities/Facture.php b/src/model/entities/Facture.php index fe04508..abe37fb 100644 --- a/src/model/entities/Facture.php +++ b/src/model/entities/Facture.php @@ -2,7 +2,6 @@ // src/entities/Facture.php use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\EntityManager; #[ORM\Entity] #[ORM\Table(name: 'factures')] @@ -29,17 +28,14 @@ class Facture extends Prestation #[ORM\Column] private float $total_HT; - public static EntityManager $entityManager; + private bool $from_quotation; - public function __construct(?Prestation $presta = null){ - $this->setPresta($presta); + public function __construct(Client $client, bool $from_quotation){ + parent::__construct($client); + $this->from_quotation = $from_quotation; } // getters - public function getPresta(): Prestation - { - return $this->presta; - } public function getAll(): array { // n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés @@ -60,20 +56,8 @@ class Facture extends Prestation public function getAllWithWindowFields(): array { - //~ $taches = ["Tâches:" => $this->taches]; - //~ $champs_facture = [ - //~ "PC:" => $this->machine, - //~ "OS:" => $this->OS, - //~ "Données:" => $this->donnees, - //~ "Clés de licences:" => $this->cles_licences]; - //~ $champs_communs = [ - //~ "Total Main d'oeuvre:" => $this->total_main_d_oeuvre, - //~ "Pièces:" => $this->pieces, - //~ "Total des pièces:" => $this->total_pieces, - //~ "Déplacement:" => $this->deplacement, - //~ "Total HT:" => $this->total_HT]; - //return $taches + $champs_facture + $champs_communs; - return [ + $common_data = parent::getAllWithWindowFields(); + return array_merge($common_data, [ "Tâches:" => $this->taches, "PC:" => $this->machine, "OS:" => $this->OS, @@ -83,11 +67,11 @@ class Facture extends Prestation "Pièces:" => $this->pieces, "Total des pièces:" => $this->total_pieces, "Déplacement:" => $this->deplacement, - "Total HT:" => $this->total_HT]; + "Total HT:" => $this->total_HT]); } // setters - public function set(string $entry, string $input) // trouve la bonne méthode + public function set(string $entry, string $input): void // trouve la bonne méthode { $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaîne vide switch($entry) @@ -122,68 +106,66 @@ class Facture extends Prestation case "Total HT:": $this->setTotalHT($input); break; + default: // gérer les champ dans Prestation + parent::set($entry, $input); } } - public function setPresta(Prestation $input) // private? - { - $this->presta = $input; - } - public function setTaches(string $value) + public function setTaches(string $value): Facture { $this->taches = $value; return($this); } - public function setMachine(string $value) + public function setMachine(string $value): Facture { $this->machine = $value; return($this); } - public function setOS(string $value) + public function setOS(string $value): Facture { $this->OS = $value; return($this); } - public function setDonnees(string $value) + public function setDonnees(string $value): Facture { $this->donnees = $value; return($this); } - public function setClesLicences(string $value) + public function setClesLicences(string $value): Facture { $this->cles_licences = $value; return($this); } - public function setTotalMainDOeuvre($value) + public function setTotalMainDOeuvre(string $value): Facture { $value = str_replace(',', '.', $value); $this->total_main_d_oeuvre = (float) $value; // float "nettoie" tous les caractères après le dernier chiffre trouvé (ex: 50€ => 50, abc => 0) return($this); } - public function setPieces(string $value) + public function setPieces(string $value): Facture { $this->pieces = $value; return($this); } - public function setTotalPieces($value) + public function setTotalPieces(string $value): Facture { $value = str_replace(',', '.', $value); $this->total_pieces = (float) $value; return($this); } - public function setDeplacement($value) + public function setDeplacement(string $value): Facture { $value = str_replace(',', '.', $value); $this->deplacement = (float) $value; return($this); } - public function setTotalHT($value) + public function setTotalHT(string $value): Facture { $value = str_replace(',', '.', $value); $this->total_HT = (float) $value; return($this); } - private function setAll(array $input) // private? + private function setService(array $input): void // private? { $this->taches = $input[0]; $this->machine = $input[1]; @@ -196,7 +178,7 @@ class Facture extends Prestation $this->deplacement = (float)$input[8]; $this->total_HT = (float)$input[9]; } - private function setServiceFromQuotation(array $input) // private? + private function setServiceFromQuotation(array $input): void // private? { $this->machine = $input[0]; $this->OS = $input[1]; @@ -204,50 +186,39 @@ class Facture extends Prestation $this->cles_licences = $input[3]; } - // à mettre plus tard dans une classe mère - protected function cleanSpecialChars(string $data): string + public function validateForm(array $data_array): bool { - $search = ['"']; - return str_replace($search, '', $data); + if(!$this->from_quotation && count($data_array) === 11){ // facture normale + return true; + } + elseif(count($data_array) === 5){ // facture à partir d'un devis + return true; + } + else{ + echo "erreur de Facture::validateForm(), le tableau \$data_array n'a pas la taille attendue.\n"; + return false; + } } + // à mettre plus tard dans une classe mère - public function hydrate(string $answers): void + public function hydrate(array $data_array): void { - $answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide - if($answers == '') - { - echo "erreur de Facture::hydrate(), la chaine \$answers est vide.\n"; - return; - } - $data_array = explode('|', $answers); // array - - if(count($data_array) === 11) // facture normale - { - $this->getPresta()->setModePaiement($data_array[10]); + if(!$this->from_quotation){ // facture normale + $this->setModePaiement($data_array[10]); //array_pop($data_array); // supprime la dernière case unset($data_array[10]); - $this->setAll($data_array); + $this->setService($data_array); } - elseif(count($data_array) === 5) // facture à partir d'un devis - { - $this->getPresta()->setModePaiement($data_array[4]); + else{ // facture à partir d'un devis + $this->setModePaiement($data_array[4]); unset($data_array[4]); $this->setServiceFromQuotation($data_array); } - else - { - echo "erreur de Facture::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n"; - return; - } - - //self::$entityManager->persist('Prestation'); - self::$entityManager->persist($this); // $Presta avec en cascade! - self::$entityManager->flush(); } - public function hydrateWithQuotation(Devis $QuotationDetails) + public function hydrateWithQuotation(Devis $Quotation): void { - $data = $QuotationDetails->getAll(); + $data = $Quotation->getAll(); //var_dump($data);die; $this->taches = $data['taches']; $this->total_main_d_oeuvre = $data['total_main_d_oeuvre']; diff --git a/src/model/entities/Location.php b/src/model/entities/Location.php index 3574272..c18c40a 100644 --- a/src/model/entities/Location.php +++ b/src/model/entities/Location.php @@ -2,7 +2,6 @@ // src/entities/Location.php use Doctrine\ORM\Mapping as ORM; -use Doctrine\ORM\EntityManager; #[ORM\Entity] #[ORM\Table(name: 'locations')] @@ -27,17 +26,7 @@ class Location extends Prestation #[ORM\Column] protected float $caution; - public static EntityManager $entityManager; - - public function __construct(?Prestation $presta = null){ - $this->setPresta($presta); - } - // getters - public function getPresta(): Prestation - { - return $this->presta; - } public function getLoyer(): float { return $this->loyer_hebdo; @@ -54,7 +43,8 @@ class Location extends Prestation public function getAllWithWindowFields(): array { - return [ + $common_data = parent::getAllWithWindowFields(); + return array_merge($common_data, [ "Désignation:" => $this->designation, "Description du modèle:" => $this->modele_description, "Valeur:" => $this->valeur, @@ -63,7 +53,7 @@ class Location extends Prestation "Durée de la location:" => $this->duree_location, "Loyer Hebdomadaire:" => $this->loyer_hebdo, "Loyers Payés:" => $this->loyers_payes, - "Caution:" => $this->caution]; + "Caution:" => $this->caution]); } // setters @@ -99,11 +89,9 @@ class Location extends Prestation case "Caution:": $this->setCaution($input); break; - } - } - private function setPresta(Prestation $input): void // private? - { - $this->presta = $input; + default: // gérer les champ dans Prestation + parent::set($entry, $input); + } } public function setDesignation(string $value): Location { @@ -168,36 +156,21 @@ class Location extends Prestation $this->caution = (int)$input[7]; } - // à mettre plus tard dans une classe mère - protected function cleanSpecialChars(string $data): string - { - $search = ['"']; - return str_replace($search, '', $data); - } - // à mettre plus tard dans une classe mère - public function hydrate(string $answers): void + public function validateForm(array $data_array): bool { - $answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide - if($answers == '') - { - echo "erreur de Location::hydrate(), la chaine \$answers est vide.\n"; - return; - } - $data_array = explode('|', $answers); // array - - if(count($data_array) === 8) - { - $this->getPresta()->setModePaiement(''); // sécurité, doctrine plante si "null" - $this->setAll($data_array); + if(count($data_array) === 8){ + return true; } - else - { - echo "erreur de Location::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n"; - return; + else{ + echo "erreur de Location::validateForm(), le tableau \$data_array n'a pas la taille attendue.\n"; + return false; } + } - //self::$entityManager->persist('Prestation'); - self::$entityManager->persist($this); // $Presta avec en cascade! - self::$entityManager->flush(); + // à mettre plus tard dans une classe mère + public function hydrate(array $data_array): void + { + //$this->setModePaiement(''); // vaut déjà '' + $this->setAll($data_array); } } diff --git a/src/model/entities/NonVendue.php b/src/model/entities/NonVendue.php index b6b2612..5a8879e 100644 --- a/src/model/entities/NonVendue.php +++ b/src/model/entities/NonVendue.php @@ -9,4 +9,36 @@ class NonVendue extends Prestation { #[ORM\Column] private string $taches; + + public function getTaches(): string + { + return $this->taches; + } + public function getAllWithWindowFields(): array + { + $common_data = parent::getAllWithWindowFields(); + return array_merge($common_data, ["Tâches:" => $this->taches,]); + } + public function setTaches(string $value): NonVendue + { + $this->taches = $value; + return $this; + } + + //public function set(string $entry, string $input): void // pas nécessaire ici + + public function validateForm(array $data_array): bool + { + if(count($data_array) === 1){ + return true; + } + else{ + echo "erreur de NonVendue::validateForm(), le tableau \$data_array n'a pas la taille attendue.\n"; + return false; + } + } + public function hydrate(array $data_array): void + { + $this->setTaches($data_array[0]); + } } \ No newline at end of file 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