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; } } // 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); } }