diff options
Diffstat (limited to 'src/model/entities/Prestation.php')
| -rw-r--r-- | src/model/entities/Prestation.php | 139 |
1 files changed, 53 insertions, 86 deletions
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 | |||
| 28 | 28 | ||
| 29 | #[ORM\ManyToOne(targetEntity: Client::class, cascade: ['persist'])] | 29 | #[ORM\ManyToOne(targetEntity: Client::class, cascade: ['persist'])] |
| 30 | #[ORM\JoinColumn(name: 'id_client', referencedColumnName: 'id')] | 30 | #[ORM\JoinColumn(name: 'id_client', referencedColumnName: 'id')] |
| 31 | private ?Client $client = null; // type int en SQL et dans la base | 31 | private Client $client; // type int en SQL et dans la base |
| 32 | #[ORM\Column] | 32 | #[ORM\Column] |
| 33 | private string $code_presta; | 33 | protected string $code_presta; |
| 34 | #[ORM\Column(type: 'string', length: 10)] // format 'aaaa-mm-jj' | 34 | #[ORM\Column(type: 'string', length: 10)] // format 'aaaa-mm-jj' |
| 35 | private string $date; | 35 | protected string $date; |
| 36 | #[ORM\Column] | 36 | #[ORM\Column] |
| 37 | private string $mode_paiement = ''; // non renseigné quand on fait un devis | 37 | private string $mode_paiement = ''; // non renseigné quand on fait un devis |
| 38 | #[ORM\Column] | 38 | #[ORM\Column] |
| 39 | private string $commentaires; | 39 | private string $commentaires; |
| 40 | 40 | ||
| 41 | public static EntityManager $entityManager; | 41 | public static EntityManager $entityManager; // inséré dans main.php |
| 42 | 42 | ||
| 43 | public function __construct(?Client $client = null){ | 43 | public function __construct(Client $client){ |
| 44 | $this->setClient($client); | 44 | $this->setClient($client); |
| 45 | } | 45 | } |
| 46 | 46 | ||
| @@ -57,10 +57,17 @@ abstract class Prestation | |||
| 57 | { | 57 | { |
| 58 | return $this->date; | 58 | return $this->date; |
| 59 | } | 59 | } |
| 60 | /*public function getTypePresta(): string | 60 | public function getTypePresta(): string |
| 61 | { | 61 | { |
| 62 | return $this->type_presta; | 62 | return match (true) { |
| 63 | }*/ | 63 | $this instanceof Devis => 'devis', |
| 64 | $this instanceof Facture => 'facture', | ||
| 65 | $this instanceof CESU => 'cesu', | ||
| 66 | $this instanceof Location => 'location', | ||
| 67 | $this instanceof NonVendue => 'non_vendue', | ||
| 68 | //default => throw new \LogicException('Type de prestation inconnu'), // impossible | ||
| 69 | }; | ||
| 70 | } | ||
| 64 | public function getModePaiement(): string | 71 | public function getModePaiement(): string |
| 65 | { | 72 | { |
| 66 | return $this->mode_paiement; | 73 | return $this->mode_paiement; |
| @@ -75,7 +82,7 @@ abstract class Prestation | |||
| 75 | return get_object_vars($this); | 82 | return get_object_vars($this); |
| 76 | } | 83 | } |
| 77 | 84 | ||
| 78 | public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) | 85 | public function getAllWithWindowFields(): array // différent de getAll() qui retourne get_object_vars($this) |
| 79 | { | 86 | { |
| 80 | return [ | 87 | return [ |
| 81 | //"Numéro prestation:" => end($code_presta_tableau), // crée des conflits, calcul automatique par setCodePresta() | 88 | //"Numéro prestation:" => end($code_presta_tableau), // crée des conflits, calcul automatique par setCodePresta() |
| @@ -85,31 +92,7 @@ abstract class Prestation | |||
| 85 | "Commentaires:" => $this->commentaires]; | 92 | "Commentaires:" => $this->commentaires]; |
| 86 | } | 93 | } |
| 87 | 94 | ||
| 88 | // une interface pour ça c'est bien aussi non? | ||
| 89 | public function getFacture(): ?Facture | ||
| 90 | { | ||
| 91 | return $this->facture; | ||
| 92 | } | ||
| 93 | public function getDevis(): ?Devis | ||
| 94 | { | ||
| 95 | return $this->devis; | ||
| 96 | } | ||
| 97 | public function getCesu(): ?CESU | ||
| 98 | { | ||
| 99 | return $this->cesu; | ||
| 100 | } | ||
| 101 | public function getLocation(): ?Location | ||
| 102 | { | ||
| 103 | return $this->location; | ||
| 104 | } | ||
| 105 | |||
| 106 | // setters | 95 | // setters |
| 107 | //~ public function setCodePresta(string $value) | ||
| 108 | //~ { | ||
| 109 | //~ $this->code_presta = $value; | ||
| 110 | //~ return $this; | ||
| 111 | //~ } | ||
| 112 | //public function setDate(int $value) | ||
| 113 | public function setDate(string $date): Prestation | 96 | public function setDate(string $date): Prestation |
| 114 | { | 97 | { |
| 115 | $this->date = $date; | 98 | $this->date = $date; |
| @@ -125,36 +108,24 @@ abstract class Prestation | |||
| 125 | }*/ | 108 | }*/ |
| 126 | } | 109 | } |
| 127 | 110 | ||
| 128 | /*public function setTypePresta(string $value) // appelée dans la section 2 uniquement, pour la 3 on verra plus tard | 111 | public function setModePaiement(string $value): Prestation |
| 129 | { | ||
| 130 | $this->type_presta = $value; | ||
| 131 | return $this; | ||
| 132 | }*/ | ||
| 133 | public function setModePaiement(string $value) | ||
| 134 | { | 112 | { |
| 135 | $this->mode_paiement = $value; | 113 | $this->mode_paiement = $value; |
| 136 | return $this; | 114 | return $this; |
| 137 | } | 115 | } |
| 138 | public function setCommentaires(string $input) | 116 | public function setCommentaires(string $input): void |
| 139 | { | 117 | { |
| 140 | $this->commentaires = $input; | 118 | $this->commentaires = $input; |
| 141 | } | 119 | } |
| 142 | /*public function setAll(array $input) | 120 | /*public function setAll(array $input) |
| 143 | { | 121 | {}*/ |
| 144 | $this->code_presta = $input[0]; | ||
| 145 | $this->date = $input[1]; | ||
| 146 | $this->type_presta = $input[2]; | ||
| 147 | $this->mode_paiement = $input[3]; | ||
| 148 | $this->commentaires = $input[4]; | ||
| 149 | }*/ | ||
| 150 | 122 | ||
| 151 | private function setClient(Client $input) // private? | 123 | private function setClient(Client $client): void // private? |
| 152 | { | 124 | { |
| 153 | $this->client = $input; | 125 | $this->client = $client; |
| 154 | //$input->typeToClient(); // on modifie $client ici (merci les références PHP) | ||
| 155 | } | 126 | } |
| 156 | 127 | ||
| 157 | /*public function set(string $entry, string $input): void | 128 | public function set(string $entry, string $input): void |
| 158 | { | 129 | { |
| 159 | $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide | 130 | $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide |
| 160 | 131 | ||
| @@ -167,8 +138,7 @@ abstract class Prestation | |||
| 167 | case "Date:": // inutile, setDate() est appelé directement après choix fenêtre calendrier | 138 | case "Date:": // inutile, setDate() est appelé directement après choix fenêtre calendrier |
| 168 | $this->setDate($input); | 139 | $this->setDate($input); |
| 169 | break; | 140 | break; |
| 170 | //~ case "Type de Presta:": // choix impossible pour le moment | 141 | //~ case "Type de Presta:": // choix impossible pour le moment et pas anodin |
| 171 | //~ $this->setTypePresta($input); | ||
| 172 | //~ break; | 142 | //~ break; |
| 173 | case "Mode de paiement:": | 143 | case "Mode de paiement:": |
| 174 | $this->setModePaiement($input); | 144 | $this->setModePaiement($input); |
| @@ -177,12 +147,13 @@ abstract class Prestation | |||
| 177 | $this->setCommentaires($input); | 147 | $this->setCommentaires($input); |
| 178 | break; | 148 | break; |
| 179 | } | 149 | } |
| 180 | }*/ | 150 | } |
| 181 | 151 | ||
| 182 | // code presta = année-mois-jour-codeclient-typedepresta-combientièmefois | 152 | // code presta = année-mois-jour-codeclient-typedepresta-combientièmefois |
| 183 | public function setCodePresta(int $increment): Prestation // 0 pour modif, 1 pour nouvelle presta | 153 | public function setCodePresta(int $increment): Prestation // 0 pour modif, 1 pour nouvelle presta |
| 184 | { | 154 | { |
| 185 | $repository = self::$entityManager->getRepository('Prestation'); // prestas de tout type d'un client | 155 | $repository = self::$entityManager->getRepository('Prestation'); |
| 156 | |||
| 186 | // attention: il peut y avoir plusieurs prestations avec le même numéro au compteur, à améliorer | 157 | // attention: il peut y avoir plusieurs prestations avec le même numéro au compteur, à améliorer |
| 187 | $how_many_times = count($repository->findBy(['client' => $this->client->getId()])) + $increment; | 158 | $how_many_times = count($repository->findBy(['client' => $this->client->getId()])) + $increment; |
| 188 | //var_dump($how_many_times); | 159 | //var_dump($how_many_times); |
| @@ -192,7 +163,7 @@ abstract class Prestation | |||
| 192 | $date_array[1], // mois | 163 | $date_array[1], // mois |
| 193 | $date_array[2], // jour | 164 | $date_array[2], // jour |
| 194 | $this->client->getCodeClient(), // mise à jour éventuelle | 165 | $this->client->getCodeClient(), // mise à jour éventuelle |
| 195 | //$this->type_presta, | 166 | $this->getTypePresta(), |
| 196 | $how_many_times | 167 | $how_many_times |
| 197 | ]; | 168 | ]; |
| 198 | $this->code_presta = implode('-', $array_code); | 169 | $this->code_presta = implode('-', $array_code); |
| @@ -208,37 +179,27 @@ abstract class Prestation | |||
| 208 | //~ return $this; | 179 | //~ return $this; |
| 209 | //~ } | 180 | //~ } |
| 210 | 181 | ||
| 211 | public static function getServices(Client $Customer, string $type = '') | 182 | public static function getServices(Client $Customer, string $type_class = Prestation::class): ?Prestation // param par défaut = recherche tout type |
| 212 | { | 183 | { |
| 213 | echo "debug: recherche d'une prestation\n"; | 184 | echo "debug: recherche d'une prestation\n"; |
| 214 | 185 | ||
| 215 | // requête DQL qui fait la même chose que le findBy plus bas | 186 | // requête DQL qui fait la même chose que le findBy plus bas |
| 216 | /*$query = self::$entityManager->createQuery('SELECT u FROM Prestation u WHERE u.client = :id_client AND u.type_presta = :type_presta'); | 187 | /*$query = self::$entityManager->createQuery('SELECT u FROM Prestation u WHERE u.client = :client AND u INSTANCE OF ' . $type_class); |
| 217 | $query->setParameter('id_client', $Customer->getId()); | 188 | $query->setParameter('client', $Customer); |
| 218 | $query->setParameter('type_presta', $type); | 189 | $PrestaList = $query->getResult();*/ |
| 219 | $query_result = $query->getResult(); | ||
| 220 | var_dump($query_result);die;*/ | ||
| 221 | 190 | ||
| 222 | $repository = self::$entityManager->getRepository('Prestation'); | 191 | $repository = self::$entityManager->getRepository($type_class); |
| 223 | $find_by = ['client' => $Customer->getId()]; | 192 | $PrestaList = $repository->findBy(['client' => $Customer]); |
| 224 | if($type != '') | ||
| 225 | { | ||
| 226 | //$find_by['type_presta'] = 'devis'; | ||
| 227 | } | ||
| 228 | $PrestaList = $repository->findBy($find_by); | ||
| 229 | 193 | ||
| 230 | // le même tableau avec pour clés les id des objets | 194 | // le même tableau avec pour clés les id des objets |
| 231 | $PrestaListIdKeys = []; | 195 | $PrestaListIdKeys = []; |
| 232 | foreach($PrestaList as $Presta) | 196 | foreach($PrestaList as $Presta){ |
| 233 | { | 197 | $PrestaListIdKeys[$Presta->getId()] = $Presta; |
| 234 | $id = $Presta->getId(); | ||
| 235 | $PrestaListIdKeys[$id] = $Presta; | ||
| 236 | } | 198 | } |
| 237 | 199 | ||
| 238 | // fenêtre | 200 | // fenêtre |
| 239 | $entrees = []; | 201 | $entrees = []; |
| 240 | foreach($PrestaListIdKeys as $Presta) | 202 | foreach($PrestaListIdKeys as $Presta){ |
| 241 | { | ||
| 242 | $id = $Presta->getId(); | 203 | $id = $Presta->getId(); |
| 243 | $entrees[$id][] = $id; | 204 | $entrees[$id][] = $id; |
| 244 | $entrees[$id][] = $Presta->getDate(); | 205 | $entrees[$id][] = $Presta->getDate(); |
| @@ -252,22 +213,28 @@ abstract class Prestation | |||
| 252 | 213 | ||
| 253 | // choix de l'utilisateur | 214 | // choix de l'utilisateur |
| 254 | $input = exec($ResultatsRecherchePresta->get()); // $input est l'ID de la prestation | 215 | $input = exec($ResultatsRecherchePresta->get()); // $input est l'ID de la prestation |
| 255 | //echo "input = " . $input . "\n"; | 216 | if($input == ''){ |
| 256 | if($input == '') | ||
| 257 | { | ||
| 258 | echo "debug: recherche annulée ou saisie vide\n"; | 217 | echo "debug: recherche annulée ou saisie vide\n"; |
| 259 | return 0; | 218 | return null; |
| 260 | } | 219 | } |
| 261 | else | 220 | else{ |
| 262 | { | ||
| 263 | return $PrestaListIdKeys[$input]; | 221 | return $PrestaListIdKeys[$input]; |
| 264 | } | 222 | } |
| 265 | } | 223 | } |
| 266 | 224 | ||
| 267 | // à mettre plus tard dans une classe mère | 225 | // nettoyage caractères spéciaux: " ` |
| 268 | private function cleanSpecialChars(string $data): string | 226 | public function cleanSpecialChars(string $answers): string |
| 269 | { | 227 | { |
| 270 | $search = ['"']; | 228 | $targets = ['"', '`']; |
| 271 | return str_replace($search, '', $data); | 229 | return str_replace($targets, '', $answers); // possibilité que des input deviennent vide |
| 230 | } | ||
| 231 | |||
| 232 | public function answersToArray(string $answers): array|false | ||
| 233 | { | ||
| 234 | if($answers === ''){ | ||
| 235 | echo "erreur: chaîne vide dans answersToArray()\n"; | ||
| 236 | return false; | ||
| 237 | } | ||
| 238 | return explode('|', $answers); | ||
| 272 | } | 239 | } |
| 273 | } | 240 | } |
