From 22b941b3526dd3aaf6976eb4ed30aa2ecc30f921 Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 22 Jun 2023 15:22:23 +0200 Subject: factorisation 1_new_service.php vers functions.php --- src/functions.php | 172 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 121 insertions(+), 51 deletions(-) (limited to 'src/functions.php') diff --git a/src/functions.php b/src/functions.php index bd91ddf..0b075b5 100644 --- a/src/functions.php +++ b/src/functions.php @@ -22,6 +22,72 @@ function windowAppCommand(string $app, string $path = ''): string return $command; } +function makeObjectCLient() +{ + // fenêtres + $QuestionNouveauClient = new ZenityQuestion(ZenitySetup::$question_nouveau_client_text); + $RechercheClient = new zenityEntry(ZenitySetup::$recherche_client_text); + $ResultatsRechercheClient = new ZenityList(ZenitySetup::$resultats_recherche_client_text, []); + $NouveauClient = new ZenityForms(ZenitySetup::$nouveau_client_text, ZenitySetup::$nouveau_client_entrees); + + $Client = new Clients; + + // est ce que le client est déjà dans la base? + $client_inconnu = true; + if(exec($QuestionNouveauClient->get()) == '0') // $? = 0 signifie oui, double égal == pour le transtypage + { + echo "choix: recherche d'une client\n"; + $input = exec($RechercheClient->get()); + if($input == '') + { + echo "debug: recherche annulée ou saisie vide\n"; + return 0; + } + + echo "debug: recherche effectuée\n"; + $ResultatsRechercheClient->setListRows(rechercheClient($input, $Client), $Client->getTable()); // recherche silencieuse + + // sélection parmi les résultats + $input = exec($ResultatsRechercheClient->get()); // renvoie l'ID de la table 'clients' + $ResultatsRechercheClient->cleanCommand(); + + if($input == '') + { + echo "debug: client pas trouvé ou pas sélectionné\n"; + return 0; + } + + echo "debug: client sélectionné\n"; + $Client->hydrate($Client->findById($input)); + $client_inconnu = false; + } + else + { + echo "choix: nouveau client\n"; + } + + // on n'a pas cherché OU on n'a pas trouvé + if($client_inconnu) + { + $input = exec($NouveauClient->get()); + if($input == '') + { + echo "debug: annulation lors de l'enregistrement d'un nouveau client\n"; + return 0; + } + if(!$Client->hydrateFromForm($input)) + { + // messages d'erreur dans hydrateFromForm() + return 0; + } + unset($input); + $Client->create(); + $Client->setID(); // sans paramètre, exécute un $this->db->lastInsertId() + } + + return $Client; +} + // NOTE 1: en PHP les objets sont passés aux fonctions par référence par défaut, toutefois ce n'est pas entièrement vrai // NOTE 2: PHP n'a pas de pointeur mais des références, une référence est un alias qui ne contient pas l'objet lui-même // NOTE 3: la variable créée lors d'un "new" est elle-même une référence contenant un identifiant (= le pointeur?) @@ -30,78 +96,65 @@ function windowAppCommand(string $app, string $path = ''): string function rechercheClient(string $input, Clients $Client): array { $input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre - $result = $Client->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées return($result); } -function makeFolder(string $path) -{ - if(!file_exists($path)) - { - mkdir($path); - chmod($path, 0755); // droits en octal - } -} - -function makeLatexAndPdfDocument($documentType, Clients $Client = null, Prestations $Presta = null, $Details = null) // $Details peut être de type DevisFactures ou Locations +function makeLatexAndPdfDocument(Clients $Client = null, Prestations $Presta = null, $PrestaDetails = null) { $latex = ''; $year = ''; $data = []; - $file_name = $documentType . '.tex'; $latex_path = Config::$latex_path; $pdf_path = Config::$pdf_path; - // tableau associatif avec des données des différents objets - // note: la propriété 'commentaires' de $Presta écrase celle de $Client (pas grave, les clients n'ont pas besoin d'en avoir connaissance) - // encore une fois, ce programme est destiné à un usage privé et hors ligne, sans déclaration CNIL + // verso d'une enveloppe + $latex = makeLatex('enveloppe_verso'); // pas de données transmises, elles sont dans la classe Config + $file_name = 'enveloppe_verso.tex'; + makeFiles($latex_path, $pdf_path, $file_name, $latex); - if($Client == null) // verso d'une enveloppe - { - $latex = makeLatex($documentType); // pas de données transmises, elles sont dans la classe Config - $file_name = 'enveloppe_verso'; - } - else + if($Client !== null) { $data = $Client->getAll(); - if($Presta == null) // recto d'une enveloppe - { - $latex_path .= 'enveloppes_recto/'; - $pdf_path .= 'enveloppes_recto/'; - $data['code_postal'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 - $latex = makeLatex($documentType, $data); // injection des variables - $file_name = $Client->getCodeClient() . '.tex'; - } - else // facture, devis, location + // recto d'une enveloppe + $latex_recto_path = $latex_path . 'enveloppes_recto/'; + $pdf_verso_path = $pdf_path . 'enveloppes_recto/'; + $data['code_postal'] = 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'; + makeFiles($latex_recto_path, $pdf_verso_path, $file_name, $latex); + + // facture, devis, location + if($Presta !== null && $PrestaDetails !== null) { - $data = array_merge($data, $Presta->getAll()); - $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!) - $year = $Date->getYear(); - - // créer un sous-dossier pour la nouvelle année - $latex_path .= $year . '/'; - $pdf_path .= $year . '/'; - - if($Details != null) + $type = $Presta->getTypePresta(); + $file_name = $type . '.tex'; + if($type === 'facture' || $type === 'devis' || $type === 'location') { - $data = array_merge($data, $Details->getAll()); + $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'; + makeFiles($latex_year_path, $pdf_year_path, $file_name, $latex); + } + elseif($type === 'cesu' || $type === 'non_vendue') + {} // pas de document + else + { + echo "debug: erreur génération latex, type de prestation \n"; + return 0; } - - $latex = makeLatex($documentType, $data, $Date); // injection des variables - $file_name = $Presta->getCodePresta() . '.tex'; } } - - // création des fichiers - makeFolder($latex_path); - makeFolder($pdf_path); - file_put_contents($latex_path. $file_name, $latex); // écriture du fichier - latexToPdf($latex_path, $file_name, $pdf_path); } -function makeLatex(string $documentType, array $data = [], Dates $Date = null) + +function makeLatex(string $type, array $data = [], Dates $Date = null) { $date = ''; if($Date != null) @@ -113,13 +166,30 @@ function makeLatex(string $documentType, array $data = [], Dates $Date = null) // 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/' . $documentType . '.php'); + 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); } +function makeFiles($latex_path, $pdf_path, $file_name, $latex) +{ + makeFolder($latex_path); + makeFolder($pdf_path); + file_put_contents($latex_path. $file_name, $latex); // écriture du fichier + latexToPdf($latex_path, $file_name, $pdf_path); +} + +function makeFolder(string $path) +{ + if(!file_exists($path)) + { + mkdir($path); + chmod($path, 0755); // droits en octal + } +} + // compilation à partir d'un fichier .tex function latexToPdf(string $latexPath, string $fileName, string $pdfPath) { -- cgit v1.2.3