From 945af9fda5146405ab9903d4d268bcb2fe95da25 Mon Sep 17 00:00:00 2001 From: polo Date: Tue, 10 Jan 2023 13:08:54 +0100 Subject: enregistrement section 1 --- src/model/Clients.php | 65 ++++++++++++--------------- src/model/DevisFactures.php | 102 ++++++++++++++++++++++++------------------- src/model/Model.php | 30 ++++++++----- src/model/Prestations.php | 73 +++++++++++++------------------ src/model/StructTablesDB.php | 3 +- src/model/traits.php | 83 ++++++++++++++++++++++++++++++++++- 6 files changed, 217 insertions(+), 139 deletions(-) (limited to 'src/model') diff --git a/src/model/Clients.php b/src/model/Clients.php index aeb39c1..9661562 100644 --- a/src/model/Clients.php +++ b/src/model/Clients.php @@ -5,18 +5,18 @@ class Clients extends Model { // lecture des données ou hydratation protected $ID; // auto-incrémentée - protected $prenom_nom = ''; - protected $code_client = ''; - protected $adresse = ''; - protected $telephone = ''; - protected $courriel = ''; - protected $commentaires = ''; + protected $prenom_nom; + protected $code_client; + protected $adresse; + protected $telephone; + protected $courriel; + protected $commentaires; - use ModelChildren; // renseigne parent::table + use ModelChildren; // pour hydrateFromForm() public function __construct() { - $this->table = 'clients'; + $this->table = 'clients'; // à mettre dans ModelChildren } //~ public function set(string $variable, $value) @@ -36,59 +36,48 @@ class Clients extends Model } // setters - public function setID(int $value) // inutile? il s'autoincrémente - { - $this->ID = $value; - return($this); - } - public function setPrenom_nom(string $value) + //~ public function setID() -> dans le trait ModelChildren + + public function setPrenomNom($value) { - $this->prenom_nom = $value; + $this->prenom_nom = (string) $value; return($this); } - public function setCode_client(string $value) + public function setCodeClient($value) { - $this->code_client = $value; + $this->code_client = (string) $value; return($this); } - public function setAdresse(string $value) + public function setAdresse($value) { - $this->adresse = $value; + $this->adresse = (string) $value; return($this); } - public function setTelephone(string $value) // chaine parce que zenity renvoie une chaine et parce qu'on garde le 0 au début + public function setTelephone($value) // chaine parce que zenity renvoie une chaine et parce qu'on garde le 0 au début { if(is_numeric($value)) { - $this->telephone = $value; + $this->telephone = (string) $value; + } + else + { + $this->telephone = ''; + echo 'debug: le champ "telephone" ne doit comporter que des nombres, aucun numéro ne sera utilisé' . "\n"; } return($this); } - public function setCourriel(string $value) + public function setCourriel($value) { - $this->courriel = $value; + $this->courriel = (string) $value; return($this); } - public function setCommentaires(string $value) + public function setCommentaires($value) { - $this->commentaires = $value; + $this->commentaires = (string) $value; return($this); } - public function newRow(array $input) - { - $this->hydrate(['prenom_nom' => $input[0], 'code_client' => $input[1], 'adresse' => $input[2], 'telephone' => $input[3], 'courriel' => $input[4], 'commentaires' => $input[5]]); - $this->create(); - // ID obtenu par auto-incrémentation - $this->ID = $this->db->lastInsertId(); // méthode de PDO - } - //~ public function setIdFromLastInsertID() // à faire juste après l'écriture d'une nouvelle entrée - //~ { - //~ $this->db = parent::getInstance(); // $db est créée dans Model::execQuery() - //~ $this->ID = $this->db->lastInsertId(); // méthode de PDO - //~ } - public function findByKeywords(array $keywords, string $field): array // n'hydrate pas les variables, on doit choisir un client et hydrater ensuite { $result = []; diff --git a/src/model/DevisFactures.php b/src/model/DevisFactures.php index 1ed3a5c..ff80960 100644 --- a/src/model/DevisFactures.php +++ b/src/model/DevisFactures.php @@ -3,49 +3,42 @@ class DevisFactures extends Model { + //~ public $type = ''; // deux tables séparées devis ou factures + // lecture des données ou hydratation - private $ID_devis_facture; // auto-incrémentée - private $ID_presta = 0; - private $validite_devis; - private $signature_devis; - private $taches; - private $machine; - private $OS; - private $donnees; - private $cles_licences; - private $total_main_d_oeuvre; - private $pieces; - private $total_pieces; - private $deplacement; - private $total_HT; + protected $ID; // auto-incrémentée + protected $ID_presta; + protected $taches; + protected $machine; + protected $OS; + protected $donnees; + protected $cles_licences; + protected $temps; + protected $total_main_d_oeuvre; + protected $pieces; + protected $total_pieces; + protected $deplacement; + protected $total_HT; + protected $validite_devis; + protected $signature_devis; - //~ use ModelChildren; + use ModelChildren; // pour hydrateFromForm() - public function __construct(int $ID_presta) + public function __construct(int $ID_presta, string $table) { - $this->table = 'devisfactures'; $this->ID_presta = $ID_presta; + $this->table = $table; // deux tables séparées devis et factures } // setters - public function setIDDevisFacture(int $value = 0) - { - if($value === 0) - { - $this->ID_devis_facture = $this->db->lastInsertId(); // méthode de PDO - } - else - { - $this->ID_devis_facture = $value; - } - return($this); - } + //~ public function setID() -> dans le trait ModelChildren + public function setIDPresta(int $value) { $this->ID_presta = $value; return($this); } - public function setValiditeDdevis(string $value) + public function setValiditedevis(string $value) { $this->validite_devis = $value; return($this); @@ -80,9 +73,15 @@ class DevisFactures extends Model $this->cles_licences = $value; return($this); } - public function setTotalMainDOeuvre(float $value) + public function setTemps(string $value) { - $this->total_main_d_oeuvre = $value; + $this->temps = $value; + return($this); + } + public function setTotalMainDOeuvre($value) + { + $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) @@ -90,28 +89,39 @@ class DevisFactures extends Model $this->pieces = $value; return($this); } - public function setTotalPieces(float $value) + public function setTotalPieces($value) { - $this->total_pieces = $value; + $value = str_replace(',', '.', $value); + $this->total_pieces = (float) $value; return($this); } - public function setDeplacement(float $value) + public function setDeplacement($value) { - $this->deplacement = $value; + $value = str_replace(',', '.', $value); + $this->deplacement = (float) $value; return($this); } - public function setTotalHT(float $value) + public function setTotalHT($value) { - $this->total_HT = $value; + $value = str_replace(',', '.', $value); + $this->total_HT = (float) $value; return($this); } - public function newRow(array $input, array $quotations_input = []) - { - if(!empty($quotations_input)) // cas d'un devis - { - $this->hydrate(['validite_devis' => $quotations_input[0], 'signature_devis' => $quotations_input[1]]); - } - $this->hydrate([]); - } + //~ public function newRow(array $input) + //~ { + //~ if($this->table === 'devis') // comme la table 'factures' avec deux champs en plus + //~ { + //~ $this->hydrate(['ID' => $input[0], 'ID_presta' => $input[1], 'taches' => $input[2], 'machine' => $input[3], 'OS' => $input[4], 'donnees' => $input[5], 'cles_licences' => $input[6], 'total_main_d_oeuvre' => $input[7], 'pieces' => $input[8], 'total_pieces' => $input[9], 'deplacement' => $input[10], 'total_HT' => $input[11]], + //~ ['validite_devis' => $input[12], 'signature_devis' => $input[13]]); + //~ $this->hydrate(['validite_devis' => $quotations_input[0], 'signature_devis' => $quotations_input[1]]); + //~ } + //~ elseif($this->table === 'factures') + //~ { + //~ $this->hydrate(['ID' => $input[0], 'ID_presta' => $input[1], 'taches' => $input[2], 'machine' => $input[3], 'OS' => $input[4], 'donnees' => $input[5], 'cles_licences' => $input[6], 'total_main_d_oeuvre' => $input[7], 'pieces' => $input[8], 'total_pieces' => $input[9], 'deplacement' => $input[10], 'total_HT' => $input[11]]); + //~ } + //~ $this->hydrate(['ID' => $input[0], 'ID_presta' => $input[1], 'taches' => $input[2], 'machine' => $input[3], 'OS' => $input[4], 'donnees' => $input[5], 'cles_licences' => $input[6], 'total_main_d_oeuvre' => $input[7], 'pieces' => $input[8], 'total_pieces' => $input[9], 'deplacement' => $input[10], 'total_HT' => $input[11]]); + //~ $this->create(); + //~ $this->ID_devis_facture = $this->db->lastInsertId(); // méthode de PDO + //~ } } diff --git a/src/model/Model.php b/src/model/Model.php index 938e3dd..4c1fb4d 100644 --- a/src/model/Model.php +++ b/src/model/Model.php @@ -19,22 +19,26 @@ class Model extends DB return($this->table); } - // setters (plusieurs en même temps) - public function hydrate(array $data) // $data = tableau associatif en entrée: nom_du_champ => valeur + // setters + public function hydrate(array $data): bool // $data = tableau associatif en entrée: nom_du_champ => valeur { foreach($data as $key => $value) { - // nom d'un setter, forme "setMachin()" - $setterName = 'set' . ucfirst($key); // ucfirst met la première lettre en majuscule - // détection - if(method_exists($this, $setterName) && $value != NULL) // on trouve aussi la méthode is_callable() + // nom du setter + // nom_du_champ devient setNomDuChamp + // on sépare les mots par des espaces, ucwords met la première lettre de chaque mot en majuscule, puis on supprime les espaces + $setter_name = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); // ucwords: première lettre de chaque mot en majuscule + if(method_exists($this, $setter_name)) { - //~ var_dump($value); - // on renseigne les propriétés des l'instance - $this->$setterName($value); // nom d'une méthode dans une variable + $this->$setter_name($value); } + else + { + echo "debug: la méthode $setter_name n'existe pas\n"; + return false; + } } - return($this); + return true; } @@ -47,6 +51,8 @@ class Model extends DB if($attributes !== null) // requête préparée { + var_dump($sql); + var_dump($attributes); $query = $this->db->prepare($sql); $query->execute($attributes); return $query; @@ -66,10 +72,12 @@ class Model extends DB $fields = []; $question_marks = []; // ? $values = []; + //~ var_dump($this); foreach($this as $field => $value) { + var_dump($field); var_dump($value); // champs non renseignées et variables de l'objet qui ne sont pas des champs - // note: avec le !== (au lieu de !=) une valeur 0 passe le filtre + // note: avec le !== (au lieu de !=) une valeur 0 est différente de null if($value !== null && $field != 'db' && $field != 'table') { $fields[] = $field; // push diff --git a/src/model/Prestations.php b/src/model/Prestations.php index cbe8e6c..2f84daa 100644 --- a/src/model/Prestations.php +++ b/src/model/Prestations.php @@ -4,66 +4,59 @@ class Prestations extends Model { // lecture des données ou hydratation - protected $ID_presta; // auto-incrémentée + protected $ID; // auto-incrémentée protected $ID_client = 0; - protected $combientieme_fois = 0; + //~ protected $combientieme_fois = 0; protected $code_presta = ''; protected $date = 0; // timestamp unix protected $type_presta = ''; protected $mode_paiement = ''; protected $commentaires = ''; - //~ use ModelChildren; + use ModelChildren; public function __construct(int $ID_client) { $this->table = 'prestations'; $this->ID_client = $ID_client; - $this->combientiemeFois(); + //~ $this->combientiemeFois(); } // getters - public function getIDPresta(): int + public function getID(): int { - return $this->ID_presta; + return $this->ID; } public function getIDClient(): int { return $this->ID_client; } - public function getCombientiemeFois(): int - { - return $this->combientieme_fois; - } + //~ public function getCombientiemeFois(): int + //~ { + //~ return $this->combientieme_fois; + //~ } public function getDate(): int { return $this->date; } - - // setters - public function setIDPresta(int $value = 0) + public function getTypePresta(): string { - if($value === 0) - { - $this->ID_presta = $this->db->lastInsertId(); // méthode de PDO - } - else - { - $this->ID_presta = $value; - } - return($this); + return $this->type_presta; } + + // setters + //~ public function setID() -> dans le trait ModelChildren public function setIDClient(int $value) { $this->ID_client = $value; return($this); } - public function setCombientiemeFois(int $value) - { - $this->combientieme_fois = $value; - return($this); - } + //~ public function setCombientiemeFois(int $value) + //~ { + //~ $this->combientieme_fois = $value; + //~ return($this); + //~ } public function setCodePresta(string $value) { $this->code_presta = $value; @@ -90,25 +83,21 @@ class Prestations extends Model return($this); } - protected function combientiemeFois() - { - // on récupère un tableau contenant toutes les prestations d'un client tous types confondus (devis, facture, cesu, location, enregistrement sans vente) - $array = $this->find(['ID_client' => $this->ID_client]); - $this->combientieme_fois = count($array) + 1; - } + //~ protected function combientiemeFois() + //~ { + //~ // on récupère un tableau contenant toutes les prestations d'un client tous types confondus (devis, facture, cesu, location, enregistrement sans vente) + //~ $array = $this->find(['ID_client' => $this->ID_client]); + //~ //$this->combientieme_fois = count($array) + 1; + //~ return count($array) + 1; + //~ } // code client = année-mois-jour-codeclient-combientièmefois public function makeCodePresta(Dates $Date, string $code_client) { - $array_code = [$Date->getYear(), $Date->getMonth(), $Date->getDay(), $code_client, $this->combientieme_fois]; + // on récupère un tableau contenant toutes les prestations d'un client tous types confondus (devis, facture, cesu, location, enregistrement sans vente) + $combientieme_fois = count($this->find(['ID_client' => $this->ID_client])) + 1; + + $array_code = [$Date->getYear(), $Date->getMonth(), $Date->getDay(), $code_client, $combientieme_fois]; $this->code_presta = implode('-', $array_code); } - - //~ public function newRow(array $input) - //~ { - //~ $this->hydrate(['ID_client' => $input[0], 'code_presta' => $input[1], 'date' => $input[2], 'type_presta' => $input[3], 'mode_paiement' => $input[4], 'commentaires' => $input[5]]); - //~ $this->create(); - //~ // ID obtenu par auto-incrémentation - //~ $this->ID_presta = $this->db->lastInsertId(); // méthode de PDO - //~ } } diff --git a/src/model/StructTablesDB.php b/src/model/StructTablesDB.php index 0f13b80..00c5321 100644 --- a/src/model/StructTablesDB.php +++ b/src/model/StructTablesDB.php @@ -11,7 +11,8 @@ class StructTablesDB // les tables devis_factures, cesu et locations sont liées à la table prestations 'clients' => ['ID' => 'INTEGER', 'prenom_nom' => 'TEXT', 'code_client' => 'TEXT', 'adresse' => 'TEXT', 'telephone' => 'TEXT', 'courriel' => 'TEXT', 'commentaires' => 'TEXT'], 'prestations' => ['ID' => 'INTEGER', 'ID_client' => 'INTEGER', 'code_presta' => 'TEXT', 'date' => 'INTEGER', 'type_presta' => 'TEXT', 'mode_paiement' => 'TEXT', 'commentaires' => 'TEXT'], - 'devisfactures' => ['ID' => 'INTEGER', 'ID_presta' => 'INTEGER', 'validite_devis' => 'TEXT', 'signature_devis' => 'TEXT', 'taches' => 'TEXT', 'machine' => 'TEXT', 'OS' => 'TEXT', 'donnees' => 'TEXT', 'cles_licences' => 'TEXT', 'total_main_d_oeuvre' => 'REAL', 'pieces' => 'TEXT', 'total_pieces' => 'REAL', 'deplacement' => 'REAL', 'total_HT' => 'REAL'], + 'devis' => ['ID' => 'INTEGER', 'ID_presta' => 'INTEGER', 'taches' => 'TEXT', 'machine' => 'TEXT', 'OS' => 'TEXT', 'donnees' => 'TEXT', 'cles_licences' => 'TEXT', 'temps' => 'TEXT', 'total_main_d_oeuvre' => 'REAL', 'pieces' => 'TEXT', 'total_pieces' => 'REAL', 'deplacement' => 'REAL', 'total_HT' => 'REAL', 'validite_devis' => 'TEXT', 'signature_devis' => 'TEXT'], + 'factures' => ['ID' => 'INTEGER', 'ID_presta' => 'INTEGER', 'taches' => 'TEXT', 'machine' => 'TEXT', 'OS' => 'TEXT', 'donnees' => 'TEXT', 'cles_licences' => 'TEXT', 'temps' => 'TEXT', 'total_main_d_oeuvre' => 'REAL', 'pieces' => 'TEXT', 'total_pieces' => 'REAL', 'deplacement' => 'REAL', 'total_HT' => 'REAL'], 'cesu' => ['ID' => 'INTEGER', 'ID_presta' => 'INTEGER', 'taches' => 'TEXT', 'duree_travail' => 'TEXT', 'salaire' => 'REAL'], 'locations' => ['ID' => 'INTEGER', 'ID_presta' => 'INTEGER', 'designation' => 'TEXT', 'modele_description' => 'TEXT', 'valeur' => 'REAL', 'etat_des_lieux_debut' => 'TEXT', 'etat_des_lieux_fin' => 'TEXT', 'duree_location' => 'INTEGER', 'loyer_mensuel' => 'REAL', 'loyers_payes' => 'INTEGER', 'caution' => 'INTEGER'] ]; diff --git a/src/model/traits.php b/src/model/traits.php index 3d446c1..41f1c35 100644 --- a/src/model/traits.php +++ b/src/model/traits.php @@ -1,7 +1,9 @@ db = parent::getInstance(); //~ $this->ID = $this->db->lastInsertId(); // méthode de PDO //~ } + + public function setID(int $value = 0) + { + if($value === 0) + { + $this->ID = $this->db->lastInsertId(); // méthode de PDO + } + else + { + $this->ID = $value; + } + return($this); + } + + function hydrateFromForm(string $data_string, Object $Presta = NULL): bool // quand l'objet est $DetailsPresta, on hydrate aussi $Presta + { + //~ $tableSize = count(StructTablesDB::$structureOfTables[$this->getTable()]); // int + + if($data_string !== '') + { + $data_array = explode('|', $data_string); // array + //~ var_dump($data_array); + //~ if(count($data_array) === $tableSize - 1) // nombre de champs sauf ID qui est auto-incrémenté automatiquement + //~ { + //~ var_dump($this->getTable()); + //~ var_dump($data_array); + $check = false; + switch($this->getTable()) + { + case 'clients'; + if($data_array[0] == '') + { + echo "debug: données insuffisantes, le nom du client doit au minimum être renseigné\n"; + return false; + } + else + { + $check = $this->hydrate(['prenom_nom' => $data_array[0], 'code_client' => $data_array[1], 'adresse' => $data_array[2], 'telephone' => $data_array[3], 'courriel' => $data_array[4], 'commentaires' => $data_array[5]]); + } + break; + case 'prestations'; // inutilisé + break; + case 'devis'; + $check = $Presta->hydrate(['mode_paiement' => $data_array[11]]); + if($check) + { + $check = $this->hydrate(['taches' => $data_array[0], 'machine' => $data_array[1], 'OS' => $data_array[2], 'donnees' => $data_array[3], 'cles_licences' => $data_array[4], 'temps' => $data_array[5], 'total_main_d_oeuvre' => $data_array[6], 'pieces' => $data_array[7], 'total_pieces' => $data_array[8], 'deplacement' => $data_array[9], 'total_HT' => $data_array[10], + 'validite_devis' => $data_array[12], 'signature_devis' => $data_array[13]]); + } + break; + case 'factures'; + $check = $Presta->hydrate(['mode_paiement' => $data_array[11]]); + if($check) + { + $check = $this->hydrate(['taches' => $data_array[0], 'machine' => $data_array[1], 'OS' => $data_array[2], 'donnees' => $data_array[3], 'cles_licences' => $data_array[4], 'temps' => $data_array[5], 'total_main_d_oeuvre' => $data_array[6], 'pieces' => $data_array[7], 'total_pieces' => $data_array[8], 'deplacement' => $data_array[9], 'total_HT' => $data_array[10]]); + } + break; + case 'cesu'; + break; + case 'locations'; + break; + default: // inutilisé + echo "debug: table inconnue hydrateFromForm()"; + return false; + } + return $check; + //~ } + //~ else + //~ { + //~ echo "debug: mauvais tableau, il doit avoir " . $tableSize - 1 . " cases\n"; // ou -3 pour les factures + //~ return false; + //~ } + } + else + { + echo "debug: annulation lors du formulaire\n"; + return false; + } + } } -- cgit v1.2.3