summaryrefslogtreecommitdiff
path: root/src/functions.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/functions.php')
-rw-r--r--src/functions.php172
1 files changed, 121 insertions, 51 deletions
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
22 return $command; 22 return $command;
23} 23}
24 24
25function makeObjectCLient()
26{
27 // fenêtres
28 $QuestionNouveauClient = new ZenityQuestion(ZenitySetup::$question_nouveau_client_text);
29 $RechercheClient = new zenityEntry(ZenitySetup::$recherche_client_text);
30 $ResultatsRechercheClient = new ZenityList(ZenitySetup::$resultats_recherche_client_text, []);
31 $NouveauClient = new ZenityForms(ZenitySetup::$nouveau_client_text, ZenitySetup::$nouveau_client_entrees);
32
33 $Client = new Clients;
34
35 // est ce que le client est déjà dans la base?
36 $client_inconnu = true;
37 if(exec($QuestionNouveauClient->get()) == '0') // $? = 0 signifie oui, double égal == pour le transtypage
38 {
39 echo "choix: recherche d'une client\n";
40 $input = exec($RechercheClient->get());
41 if($input == '')
42 {
43 echo "debug: recherche annulée ou saisie vide\n";
44 return 0;
45 }
46
47 echo "debug: recherche effectuée\n";
48 $ResultatsRechercheClient->setListRows(rechercheClient($input, $Client), $Client->getTable()); // recherche silencieuse
49
50 // sélection parmi les résultats
51 $input = exec($ResultatsRechercheClient->get()); // renvoie l'ID de la table 'clients'
52 $ResultatsRechercheClient->cleanCommand();
53
54 if($input == '')
55 {
56 echo "debug: client pas trouvé ou pas sélectionné\n";
57 return 0;
58 }
59
60 echo "debug: client sélectionné\n";
61 $Client->hydrate($Client->findById($input));
62 $client_inconnu = false;
63 }
64 else
65 {
66 echo "choix: nouveau client\n";
67 }
68
69 // on n'a pas cherché OU on n'a pas trouvé
70 if($client_inconnu)
71 {
72 $input = exec($NouveauClient->get());
73 if($input == '')
74 {
75 echo "debug: annulation lors de l'enregistrement d'un nouveau client\n";
76 return 0;
77 }
78 if(!$Client->hydrateFromForm($input))
79 {
80 // messages d'erreur dans hydrateFromForm()
81 return 0;
82 }
83 unset($input);
84 $Client->create();
85 $Client->setID(); // sans paramètre, exécute un $this->db->lastInsertId()
86 }
87
88 return $Client;
89}
90
25// 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 91// 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
26// 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 92// 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
27// NOTE 3: la variable créée lors d'un "new" est elle-même une référence contenant un identifiant (= le pointeur?) 93// 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
30function rechercheClient(string $input, Clients $Client): array 96function rechercheClient(string $input, Clients $Client): array
31{ 97{
32 $input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre 98 $input_array = explode(' ', $input); // si plusieurs mot, on les recherche tous l'un après l'autre
33
34 $result = $Client->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées 99 $result = $Client->findByKeywords($input_array, 'prenom_nom'); // on obtient un tableau à deux dimensions avec les entrées trouvées
35 return($result); 100 return($result);
36} 101}
37 102
38function makeFolder(string $path) 103function makeLatexAndPdfDocument(Clients $Client = null, Prestations $Presta = null, $PrestaDetails = null)
39{
40 if(!file_exists($path))
41 {
42 mkdir($path);
43 chmod($path, 0755); // droits en octal
44 }
45}
46
47function makeLatexAndPdfDocument($documentType, Clients $Client = null, Prestations $Presta = null, $Details = null) // $Details peut être de type DevisFactures ou Locations
48{ 104{
49 $latex = ''; 105 $latex = '';
50 $year = ''; 106 $year = '';
51 $data = []; 107 $data = [];
52 $file_name = $documentType . '.tex';
53 $latex_path = Config::$latex_path; 108 $latex_path = Config::$latex_path;
54 $pdf_path = Config::$pdf_path; 109 $pdf_path = Config::$pdf_path;
55 110
56 // tableau associatif avec des données des différents objets 111 // verso d'une enveloppe
57 // note: la propriété 'commentaires' de $Presta écrase celle de $Client (pas grave, les clients n'ont pas besoin d'en avoir connaissance) 112 $latex = makeLatex('enveloppe_verso'); // pas de données transmises, elles sont dans la classe Config
58 // encore une fois, ce programme est destiné à un usage privé et hors ligne, sans déclaration CNIL 113 $file_name = 'enveloppe_verso.tex';
114 makeFiles($latex_path, $pdf_path, $file_name, $latex);
59 115
60 if($Client == null) // verso d'une enveloppe 116 if($Client !== null)
61 {
62 $latex = makeLatex($documentType); // pas de données transmises, elles sont dans la classe Config
63 $file_name = 'enveloppe_verso';
64 }
65 else
66 { 117 {
67 $data = $Client->getAll(); 118 $data = $Client->getAll();
68 119
69 if($Presta == null) // recto d'une enveloppe 120 // recto d'une enveloppe
70 { 121 $latex_recto_path = $latex_path . 'enveloppes_recto/';
71 $latex_path .= 'enveloppes_recto/'; 122 $pdf_verso_path = $pdf_path . 'enveloppes_recto/';
72 $pdf_path .= 'enveloppes_recto/'; 123 $data['code_postal'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0
73 $data['code_postal'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0 124 $latex = makeLatex('enveloppe_recto', $data); // injection des variables
74 $latex = makeLatex($documentType, $data); // injection des variables 125 $file_name = $Client->getCodeClient() . '.tex';
75 $file_name = $Client->getCodeClient() . '.tex'; 126 makeFiles($latex_recto_path, $pdf_verso_path, $file_name, $latex);
76 } 127
77 else // facture, devis, location 128 // facture, devis, location
129 if($Presta !== null && $PrestaDetails !== null)
78 { 130 {
79 $data = array_merge($data, $Presta->getAll()); 131 $type = $Presta->getTypePresta();
80 $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!) 132 $file_name = $type . '.tex';
81 $year = $Date->getYear(); 133 if($type === 'facture' || $type === 'devis' || $type === 'location')
82
83 // créer un sous-dossier pour la nouvelle année
84 $latex_path .= $year . '/';
85 $pdf_path .= $year . '/';
86
87 if($Details != null)
88 { 134 {
89 $data = array_merge($data, $Details->getAll()); 135 $data = array_merge($data, $Presta->getAll());
136 $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!)
137 $year = $Date->getYear();
138 $latex_year_path = $latex_path . $year . '/'; // un sous-dossier par année
139 $pdf_year_path = $pdf_path . $year . '/';
140 $data = array_merge($data, $PrestaDetails->getAll());
141 $latex = makeLatex($type, $data, $Date); // injection des variables
142 $file_name = $Presta->getCodePresta() . '.tex';
143 makeFiles($latex_year_path, $pdf_year_path, $file_name, $latex);
144 }
145 elseif($type === 'cesu' || $type === 'non_vendue')
146 {} // pas de document
147 else
148 {
149 echo "debug: erreur génération latex, type de prestation \n";
150 return 0;
90 } 151 }
91
92 $latex = makeLatex($documentType, $data, $Date); // injection des variables
93 $file_name = $Presta->getCodePresta() . '.tex';
94 } 152 }
95 } 153 }
96
97 // création des fichiers
98 makeFolder($latex_path);
99 makeFolder($pdf_path);
100 file_put_contents($latex_path. $file_name, $latex); // écriture du fichier
101 latexToPdf($latex_path, $file_name, $pdf_path);
102} 154}
103 155
104function makeLatex(string $documentType, array $data = [], Dates $Date = null) 156
157function makeLatex(string $type, array $data = [], Dates $Date = null)
105{ 158{
106 $date = ''; 159 $date = '';
107 if($Date != null) 160 if($Date != null)
@@ -113,13 +166,30 @@ function makeLatex(string $documentType, array $data = [], Dates $Date = null)
113 // on obtient la variable $latex avec ob_get_clean() 166 // on obtient la variable $latex avec ob_get_clean()
114 // le include() ici, c'est du génie ou c'est moche ou les deux? 167 // le include() ici, c'est du génie ou c'est moche ou les deux?
115 // un nouveau fichier php est inséré à chaque itération 168 // un nouveau fichier php est inséré à chaque itération
116 include('latex_templates/' . $documentType . '.php'); 169 include('latex_templates/' . $type . '.php');
117 170
118 // on retourne le buffer 171 // on retourne le buffer
119 // normallement le code PHP inséré avec include est nettoyé en quittant la fonction 172 // normallement le code PHP inséré avec include est nettoyé en quittant la fonction
120 return($latex); 173 return($latex);
121} 174}
122 175
176function makeFiles($latex_path, $pdf_path, $file_name, $latex)
177{
178 makeFolder($latex_path);
179 makeFolder($pdf_path);
180 file_put_contents($latex_path. $file_name, $latex); // écriture du fichier
181 latexToPdf($latex_path, $file_name, $pdf_path);
182}
183
184function makeFolder(string $path)
185{
186 if(!file_exists($path))
187 {
188 mkdir($path);
189 chmod($path, 0755); // droits en octal
190 }
191}
192
123// compilation à partir d'un fichier .tex 193// compilation à partir d'un fichier .tex
124function latexToPdf(string $latexPath, string $fileName, string $pdfPath) 194function latexToPdf(string $latexPath, string $fileName, string $pdfPath)
125{ 195{