From 9bdfb5196a2ee1cbfc403702e8d2ef88076d366f Mon Sep 17 00:00:00 2001 From: polo Date: Wed, 5 Jul 2023 01:39:29 +0200 Subject: classe Latex "fonctionnelle" --- data/dev.sqlite | Bin 36864 -> 36864 bytes src/Latex.php | 177 +++++++++++++++++++++++--------- src/files.php | 14 +++ src/functions.php | 143 +++++++++++++------------- src/latex_templates/enveloppe_recto.php | 2 - src/main.php | 2 +- src/model/Clients.php | 18 ++-- src/model/Model.php | 4 +- src/model/Prestations.php | 22 ++-- src/sections/1_new_service.php | 54 +++++++++- 10 files changed, 283 insertions(+), 153 deletions(-) diff --git a/data/dev.sqlite b/data/dev.sqlite index 7802e1e..841d187 100644 Binary files a/data/dev.sqlite and b/data/dev.sqlite differ diff --git a/src/Latex.php b/src/Latex.php index a766fd6..051c48b 100644 --- a/src/Latex.php +++ b/src/Latex.php @@ -5,9 +5,12 @@ abstract class Latex { - protected $fileName = ''; - protected $latexPath = ''; - protected $pdfPath = ''; + protected $type = ''; + protected $file_name = ''; + protected $latex_path = ''; + protected $pdf_path = ''; + protected $data = []; // données à insérer dans le template + protected $latex = ''; // latex pur static function makeLatexSubClass(string $type): Object { @@ -15,15 +18,15 @@ abstract class Latex { // documents pour les clients case 'devis': - return new DevisLatex(); + return new DevisLatex($type); case 'facture': - return new FactireLatex(); + return new FactureLatex($type); case 'location': - return new LocationLatex(); + return new LocationLatex($type); case 'enveloppe_recto': - return new EnveloppeRectoLatex(); + return new EnveloppeRectoLatex($type); case 'enveloppe_verso': - return new EnveloppeVersoLatex(); + return new EnveloppeVersoLatex($type); // documents pour la compta @@ -33,72 +36,152 @@ abstract class Latex } } - protected function createFile(string $latex, string $fileName, string $latexPath) + // tester en private? + public function __construct(string $type) { - // nom du fichier créé = nom.tex - // pour les devis, factures et enveloppes, le nom est le code la prestation - // pour les livre de recettes et registres des achats mensuels: - // le nom du fichier suit cet exemple: "Recettes-2022-06-Juin.tex" - // pour le livre de recette ou le registre des achats annuel, même principe: "Achats-2022.tex" - // pour le bilan comptable annuel, ça donne: "Bilan-2022.tex" - $fichier = fopen($latexPath . $fileName, "w+"); - fputs($fichier, $latex); - fclose($fichier); + $this->type = $type; + } + + public function makeLatex() + { + $data = $this->data; + + // on obtient la variable $latex avec ob_get_clean() + // le include() ici, c'est du génie ou c'est moche ou les deux? + // un nouveau fichier php est inséré à chaque itération + include('latex_templates/' . $this->type . '.php'); + + // on retourne le buffer + // normallement le code PHP inséré avec include est nettoyé en quittant la fonction + $this->latex = $latex; + //return($latex); + } + + // getters + public function getFileName(): string + { + return $this->file_name; + } + public function getLatexPath(): string + { + return $this->latex_path; + } + public function getPdfPath(): string + { + return $this->pdf_path; + } + + public function getLatex(): string + { + return $this->latex; + } + + // setters + public function setFileName(string $file_name) + { + $this->file_name = $file_name; + } + public function setLatexPath(string $latex_path) + { + $this->latex_path = $latex_path; + } + public function setPdfPath(string $pdf_path) + { + $this->pdf_path = $pdf_path; + } + + public function setData(Object $Object) + { + $this->data = array_merge($this->data, $Object->getAll()); // nécessite des tableaux associatifs + //return $Object; // pour chainer les méthodes } } - abstract class PrestaLatex extends Latex { - public function __construct(string $quoi, string $codePresta) + protected $Date; // sera un type "Dates" + protected $year = ''; + + //~ public function __construct(string $type) // surcharge Latex::__construct() + //~ { + //~ $this->type = $type; + //~ $this->Date = new Dates(); + //~ $this->year = $this->Date->getYear(); + //~ } + + public function getYear(): string // parce que Dates::getDate() retourne une chaine { - nameTheFile($quoi, $codePresta); + return $this->year; + } + + public function makeDateInstance(int $date) + { + $this->Date = new Dates($date); + $this->year = $this->Date->getYear(); } // forme = code-presta.tex - protected function nameTheFile(string $quoi, string $codePresta) + //~ protected function nameTheFile(string $quoi, string $codePresta) + //~ { + //~ $this->fileName = $quoi . '-' . $codePresta . '.tex'; + //~ } + + public function makeLatex() { - $this->fileName = $quoi . '-' . $codePresta . '.tex'; + $data = $this->data; + $date = preg_replace('#\D#', '/', $this->Date->getDate()); // date avec des slashs / parce que j'aime bien + + // on obtient la variable $latex avec ob_get_clean() + // le include() ici, c'est du génie ou c'est moche ou les deux? + // un nouveau fichier php est inséré à chaque itération + include('latex_templates/' . $this->type . '.php'); + + // on retourne le buffer + // normallement le code PHP inséré avec include est nettoyé en quittant la fonction + $this->latex = $latex; + //return($latex); } } -//~ class PrestaClassFactory extends PrestaLatex -//~ {} - -class DevisLatex extends PrestaLatex +class DevisLatex extends PrestaLatex // extends Latex {} -class FactureLatex extends PrestaLatex +class FactureLatex extends PrestaLatex // extends Latex {} -class LocationLatex extends PrestaLatex -{} - -class EnveloppeRectoLatex// extends PrestaLatex +class LocationLatex extends PrestaLatex // extends Latex {} -class EnveloppeVersoLatex// extends PrestaLatex +class EnveloppeRectoLatex extends Latex +{ + // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 + public function spacesInPostCode() + { + $this->data['code_postal_espaces'] = implode(' \ ', str_split($this->data['code_postal'])); + } +} +class EnveloppeVersoLatex extends Latex {} abstract class ComptaLatex extends Latex { - public function __construct(string $quoi, string $annee, int $numeroMois = 0) - { - nameTheFile($quoi, $annee, $numeroMois); - } + //~ public function __construct(string $quoi, string $annee, int $numeroMois = 0) + //~ { + //~ nameTheFile($quoi, $annee, $numeroMois); + //~ } // forme = Recettes-2022-06-Juin.tex ou Recettes-2022.tex // type de 'annee'? - protected function nameTheFile(string $quoi, string $annee, int $numeroMois = 0) - { - $this->fileName = $quoi . '-' . $annee; - $mois = ['', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']; - if($numeroMois > 0 && $numeroMois <= 12) - { - $this->fileName .= '-' . $numeroMois . '-' . $mois[$numeroMois]; - } - $this->fileName .= '.tex'; - } + //~ protected function nameTheFile(string $quoi, string $annee, int $numeroMois = 0) + //~ { + //~ $this->fileName = $quoi . '-' . $annee; + //~ $mois = ['', 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']; + //~ if($numeroMois > 0 && $numeroMois <= 12) + //~ { + //~ $this->fileName .= '-' . $numeroMois . '-' . $mois[$numeroMois]; + //~ } + //~ $this->fileName .= '.tex'; + //~ } } class LivreRecettesLatex extends ComptaLatex diff --git a/src/files.php b/src/files.php index 9e1a3f5..2d12280 100644 --- a/src/files.php +++ b/src/files.php @@ -7,6 +7,19 @@ function makeFile($path, $file_name, $data) { file_put_contents($path. $file_name, $data); chmod($path . $file_name, 0644); // droits en octal + + //~ protected function createFile(string $latex, string $file_name, string $latexPath) + //~ { + //~ // nom du fichier créé = nom.tex + //~ // pour les devis, factures et enveloppes, le nom est le code la prestation + //~ // pour les livre de recettes et registres des achats mensuels: + //~ // le nom du fichier suit cet exemple: "Recettes-2022-06-Juin.tex" + //~ // pour le livre de recette ou le registre des achats annuel, même principe: "Achats-2022.tex" + //~ // pour le bilan comptable annuel, ça donne: "Bilan-2022.tex" + //~ $fichier = fopen($latexPath . $file_name, "w+"); + //~ fputs($fichier, $latex); + //~ fclose($fichier); + //~ } } function makeFolder(string $path) @@ -21,6 +34,7 @@ function makeFolder(string $path) // commande système pdflatex function latexToPdf(string $latex_path, string $file_name, string $pdf_path) { + // paramètre optionnel $output_dir = ''; if($pdf_path !== '') { diff --git a/src/functions.php b/src/functions.php index 1bbb40c..aaacb36 100644 --- a/src/functions.php +++ b/src/functions.php @@ -101,88 +101,87 @@ function rechercheClient(string $input, Clients $Client): array } -// remplacer par une classe Latex -function makeLatexAndPdfDocuments(Clients $Client = null, Prestations $Presta = null, $PrestaDetails = null) -{ - $latex = ''; - $year = ''; - $data = []; - $latex_path = Config::$latex_path; - $pdf_path = Config::$pdf_path; +//~ function makeLatexAndPdfDocuments(Clients $Client = null, Prestations $Presta = null, $PrestaDetails = null) +//~ { + //~ $latex = ''; + //~ $year = ''; + //~ $data = []; + //~ $latex_path = Config::$latex_path; + //~ $pdf_path = Config::$pdf_path; - // verso d'une enveloppe - $latex = makeLatex('enveloppe_verso'); // pas de données transmises, elles sont dans la classe Config - $file_name = 'enveloppe_verso.tex'; - makeFile($latex_path, $file_name, $latex); + //~ // verso d'une enveloppe + //~ $latex = makeLatex('enveloppe_verso'); // pas de données transmises, elles sont dans la classe Config + //~ $file_name = 'enveloppe_verso.tex'; + //~ makeFile($latex_path, $file_name, $latex); - latexToPdf($latex_path, $file_name, $pdf_path); + //~ latexToPdf($latex_path, $file_name, $pdf_path); - if($Client !== null) - { - $data = $Client->getAll(); + //~ if($Client !== null) + //~ { + //~ $data = $Client->getAll(); - // recto d'une enveloppe - $latex_recto_path = $latex_path . 'enveloppes_recto/'; - $pdf_recto_path = $pdf_path . 'enveloppes_recto/'; - $data['code_postal_espaces'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 + //~ // recto d'une enveloppe + //~ $latex_recto_path = $latex_path . 'enveloppes_recto/'; + //~ $pdf_recto_path = $pdf_path . 'enveloppes_recto/'; + //~ $data['code_postal_espaces'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 - $latex = makeLatex('enveloppe_recto', $data); // injection des variables - $file_name = $Client->getCodeClient() . '.tex'; - makeFolder($latex_recto_path); - makeFile($latex_recto_path, $file_name, $latex); + //~ $latex = makeLatex('enveloppe_recto', $data); // injection des variables + //~ $file_name = $Client->getCodeClient() . '.tex'; + //~ makeFolder($latex_recto_path); + //~ makeFile($latex_recto_path, $file_name, $latex); - makeFolder($pdf_recto_path); - latexToPdf($latex_recto_path, $file_name, $pdf_recto_path); + //~ makeFolder($pdf_recto_path); + //~ latexToPdf($latex_recto_path, $file_name, $pdf_recto_path); - // facture, devis, location - if($Presta !== null && $PrestaDetails !== null) - { - $type = $Presta->getTypePresta(); - $file_name = $type . '.tex'; - if($type === 'facture' || $type === 'devis' || $type === 'location') - { - $data = array_merge($data, $Presta->getAll()); - $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!) - $year = $Date->getYear(); - $latex_year_path = $latex_path . $year . '/'; // un sous-dossier par année - $pdf_year_path = $pdf_path . $year . '/'; - $data = array_merge($data, $PrestaDetails->getAll()); + //~ // facture, devis, location + //~ if($Presta !== null && $PrestaDetails !== null) + //~ { + //~ $type = $Presta->getTypePresta(); + //~ $file_name = $type . '.tex'; + //~ if($type === 'facture' || $type === 'devis' || $type === 'location') + //~ { + //~ $data = array_merge($data, $Presta->getAll()); + //~ $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!) + //~ $year = $Date->getYear(); + //~ $latex_year_path = $latex_path . $year . '/'; // un sous-dossier par année + //~ $pdf_year_path = $pdf_path . $year . '/'; + //~ $data = array_merge($data, $PrestaDetails->getAll()); - $latex = makeLatex($type, $data, $Date); // injection des variables - $file_name = $Presta->getCodePresta() . '.tex'; - makeFolder($latex_year_path); - makeFile($latex_year_path, $file_name, $latex); + //~ $latex = makeLatex($type, $data, $Date); // injection des variables + //~ $file_name = $Presta->getCodePresta() . '.tex'; + //~ makeFolder($latex_year_path); + //~ makeFile($latex_year_path, $file_name, $latex); - makeFolder($pdf_year_path); - latexToPdf($latex_year_path, $file_name, $pdf_year_path); - } - elseif($type === 'cesu' || $type === 'non_vendue') - {} // pas de document - else - { - echo "debug: erreur génération latex, type de prestation \n"; - return 0; - } - } - } -} + //~ makeFolder($pdf_year_path); + //~ latexToPdf($latex_year_path, $file_name, $pdf_year_path); + //~ } + //~ elseif($type === 'cesu' || $type === 'non_vendue') + //~ {} // pas de document + //~ else + //~ { + //~ echo "debug: erreur génération latex, type de prestation \n"; + //~ return 0; + //~ } + //~ } + //~ } +//~ } -function makeLatex(string $type, array $data = [], Dates $Date = null) -{ - $date = ''; - if($Date != null) - { - $date = $Date->getDate(); - $date = preg_replace('#\D#', '/', $date); // date avec des slashs / parce que j'aime bien - } +//~ function makeLatex(string $type, array $data = [], Dates $Date = null) +//~ { + //~ $date = ''; + //~ if($Date != null) + //~ { + //~ $date = $Date->getDate(); + //~ $date = preg_replace('#\D#', '/', $date); // date avec des slashs / parce que j'aime bien + //~ } - // on obtient la variable $latex avec ob_get_clean() - // le include() ici, c'est du génie ou c'est moche ou les deux? - // un nouveau fichier php est inséré à chaque itération - include('latex_templates/' . $type . '.php'); + //~ // on obtient la variable $latex avec ob_get_clean() + //~ // le include() ici, c'est du génie ou c'est moche ou les deux? + //~ // un nouveau fichier php est inséré à chaque itération + //~ include('latex_templates/' . $type . '.php'); - // on retourne le buffer - // normallement le code PHP inséré avec include est nettoyé en quittant la fonction - return($latex); -} + //~ // on retourne le buffer + //~ // normallement le code PHP inséré avec include est nettoyé en quittant la fonction + //~ return($latex); +//~ } diff --git a/src/latex_templates/enveloppe_recto.php b/src/latex_templates/enveloppe_recto.php index 6da6a08..294e589 100644 --- a/src/latex_templates/enveloppe_recto.php +++ b/src/latex_templates/enveloppe_recto.php @@ -1,8 +1,6 @@ \documentclass[]{report} diff --git a/src/main.php b/src/main.php index 86509dd..7bf8470 100755 --- a/src/main.php +++ b/src/main.php @@ -47,7 +47,7 @@ require('model/Locations.php'); require('view/Zenity.php'); // commande système zenity require('view/ZenitySetup.php'); // texte dans les fenêtres ET instanciation (un objet = une commande) -//require('Latex.php'); // générer le code LaTeX +require('Latex.php'); // générer le code LaTeX require('sections/1_new_service.php'); require('sections/2_quotations.php'); diff --git a/src/model/Clients.php b/src/model/Clients.php index 8f460c6..d9dd28d 100644 --- a/src/model/Clients.php +++ b/src/model/Clients.php @@ -35,27 +35,27 @@ class Clients extends Model public function setPrenomNom($value) { $this->prenom_nom = (string) $value; - return($this); + return $this; } public function setCodeClient($value) { $this->code_client = (string) $value; - return($this); + return $this; } public function setAdresse($value) { $this->adresse = (string) $value; - return($this); + return $this; } public function setCodePostal($value) { $this->code_postal = (string) $value; - return($this); + return $this; } public function setVille($value) { $this->ville = (string) $value; - return($this); + return $this; } public function setTelephone($value) // chaine parce que zenity renvoie une chaine et parce qu'on garde le 0 au début @@ -69,17 +69,17 @@ class Clients extends Model $this->telephone = ''; echo 'debug: le champ "telephone" ne doit comporter que des nombres, aucun numéro ne sera utilisé' . "\n"; } - return($this); + return $this; } public function setCourriel($value) { $this->courriel = (string) $value; - return($this); + return $this; } public function setApropos($value) { $this->apropos = (string) $value; - return($this); + return $this; } @@ -106,6 +106,6 @@ class Clients extends Model } } } - return($result); + return $result; } } diff --git a/src/model/Model.php b/src/model/Model.php index 40a9fcb..17d1292 100644 --- a/src/model/Model.php +++ b/src/model/Model.php @@ -16,7 +16,7 @@ abstract class Model extends DB // getters public function getTable(): string { - return($this->table); + return $this->table; } public function getAll(): array @@ -35,7 +35,7 @@ abstract class Model extends DB { $this->ID = $value; } - return($this); + return $this; } public function hydrate(array $data): bool // $data = tableau associatif en entrée: nom_du_champ => valeur diff --git a/src/model/Prestations.php b/src/model/Prestations.php index 94dd805..22865df 100644 --- a/src/model/Prestations.php +++ b/src/model/Prestations.php @@ -33,7 +33,7 @@ class Prestations extends Model { return $this->code_presta; } - public function getDate(): int + public function getDate(): int // timestamp unix { return $this->date; } @@ -48,7 +48,7 @@ class Prestations extends Model public function setIDClient(int $value) { $this->ID_client = $value; - return($this); + return $this; } //~ public function setCombientiemeFois(int $value) //~ { @@ -58,37 +58,29 @@ class Prestations extends Model public function setCodePresta(string $value) { $this->code_presta = $value; - return($this); + return $this; } public function setDate(int $value) { $this->date = $value; - return($this); + return $this; } public function setTypePresta(string $value) { $this->type_presta = $value; - return($this); + return $this; } public function setModePaiement(string $value) { $this->mode_paiement = $value; - return($this); + return $this; } public function setCommentaires(string $value) { $this->commentaires = $value; - return($this); + 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; - //~ return count($array) + 1; - //~ } - // code client = année-mois-jour-codeclient-combientièmefois public function makeCodePresta(Dates $Date, string $code_client) { diff --git a/src/sections/1_new_service.php b/src/sections/1_new_service.php index e7e3564..6f5a72c 100644 --- a/src/sections/1_new_service.php +++ b/src/sections/1_new_service.php @@ -130,13 +130,57 @@ function newService(): int // code de retour, si 0 retour menu principal, si 2 a // -- partie 3: LaTeX -- - makeLatexAndPdfDocuments($Client, $Presta, $PrestaDetails); + //makeLatexAndPdfDocuments($Client, $Presta, $PrestaDetails); + // factoriser tout ça + /* plusieurs parties: + * - une fonction ou on crée et manipule les objets (une classe par type de document) + * - manipulation des données + * - chemins et noms de fichiers + * - insertion des variables + * - écriture du fichier (+ dossier si nécessaire) + */ + // et pour bien faire ajouter aussi une interface // fabrique d'objets (sans connaître les noms des classes) - - //~ $EnveloppeRecto = Latex::makeLatexSubClass('enveloppe_recto'); - //~ $EnveloppeVerso = Latex::makeLatexSubClass('enveloppe_verso'); - //~ $DocumentPresta = Latex::makeLatexSubClass($Presta->getTypePresta()); // $type = facture, devis, location + $EnveloppeRecto = Latex::makeLatexSubClass('enveloppe_recto'); + $EnveloppeVerso = Latex::makeLatexSubClass('enveloppe_verso'); + $DocumentPresta = Latex::makeLatexSubClass($Presta->getTypePresta()); // $type = facture, devis, location + + // génération du latex + $EnveloppeRecto->setData($Client); + $EnveloppeRecto->setFileName($Client->getCodeClient() . '.tex'); + $EnveloppeRecto->setLatexPath(Config::$latex_path . 'enveloppes_recto/'); + $EnveloppeRecto->setPdfPath(Config::$pdf_path . 'enveloppes_recto/'); + $EnveloppeRecto->spacesInPostCode(); + $EnveloppeRecto->makeLatex(); + + $EnveloppeVerso->setFileName('enveloppe_verso.tex'); + $EnveloppeVerso->setLatexPath(Config::$latex_path); + $EnveloppeVerso->setPdfPath(Config::$pdf_path); + $EnveloppeVerso->makeLatex(); + + $DocumentPresta->setData($Client); + $DocumentPresta->setData($Presta); + $DocumentPresta->setData($PrestaDetails); + $DocumentPresta->makeDateInstance($Presta->getDate()); // paramètre = int + $DocumentPresta->setFileName($Presta->getCodePresta() . '.tex'); + $DocumentPresta->setLatexPath(Config::$latex_path . $DocumentPresta->getYear() . '/'); + $DocumentPresta->setPdfPath(Config::$pdf_path . $DocumentPresta->getYear() . '/'); + $DocumentPresta->makeLatex(); + + // création des fichiers + makeFolder($EnveloppeRecto->getLatexPath()); + makeFolder($EnveloppeRecto->getPdfPath()); + makeFile($EnveloppeRecto->getLatexPath(), $EnveloppeRecto->getFileName(), $EnveloppeRecto->getLatex()); + latexToPdf($EnveloppeRecto->getLatexPath(), $EnveloppeRecto->getFileName(), $EnveloppeRecto->getPdfPath()); + + makeFile($EnveloppeVerso->getLatexPath(), $EnveloppeVerso->getFileName(), $EnveloppeVerso->getLatex()); + latexToPdf($EnveloppeVerso->getLatexPath(), $EnveloppeVerso->getFileName(), $EnveloppeVerso->getPdfPath()); + + makeFolder($DocumentPresta->getLatexPath()); + makeFolder($DocumentPresta->getPdfPath()); + makeFile($DocumentPresta->getLatexPath(), $DocumentPresta->getFileName(), $DocumentPresta->getLatex()); + latexToPdf($DocumentPresta->getLatexPath(), $DocumentPresta->getFileName(), $DocumentPresta->getPdfPath()); // -- partie 4: récapitulatif -- -- cgit v1.2.3