summaryrefslogtreecommitdiff
path: root/src/functions.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/functions.php')
-rw-r--r--src/functions.php82
1 files changed, 71 insertions, 11 deletions
diff --git a/src/functions.php b/src/functions.php
index b8779e8..89bfbc9 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -1,7 +1,7 @@
1<?php 1<?php
2// src/functions.php 2// src/functions.php
3// 3//
4// gros fourre-tout, à ranger plus tard 4// utilisées par main.php et les fonctions de la boucle principale
5 5
6 6
7// commande pour lancer une application graphique en ouvrant un fichier 7// commande pour lancer une application graphique en ouvrant un fichier
@@ -40,25 +40,85 @@ function makeFolder(string $path)
40 if(!file_exists($path)) 40 if(!file_exists($path))
41 { 41 {
42 mkdir($path); 42 mkdir($path);
43 chmod($path, 0755); 43 chmod($path, 0755); // droits en octal
44 } 44 }
45} 45}
46 46
47function makeLatexAndPdfDocument($file_name, $template, Clients $Client, Prestations $Presta = null, $Details = null) // $Details peut être de type DevisFactures ou Locations 47function makeLatexAndPdfDocument($documentType, Clients $Client = null, Prestations $Presta = null, $Details = null) // $Details peut être de type DevisFactures ou Locations
48{ 48{
49 //~ $data = ; // tableau associatif avec des données des différents objets 49 $latex = '';
50 $latex = getLatexFromTemplate($template, $data); 50 $year = '';
51 file_put_contents($latex_path . $file_name, $latex); // injection des variables & écriture du fichier 51 $data = [];
52 $file_name = $documentType . '.tex';
53 $latex_path = Config::$latex_path;
54 $pdf_path = Config::$pdf_path;
55
56 // tableau associatif avec des données des différents objets
57 // note: la propriété 'commentaires' de $Presta écrase celle de $Client (pas grave, les clients n'ont pas besoin d'en avoir connaissance)
58 // encore une fois, ce programme est destiné à un usage privé et hors ligne, sans déclaration CNIL
59
60 if($Client == null) // verso d'une enveloppe
61 {
62 $data[0] = Config::$business_guy;
63 $data[1] = Config::$business_address;
64 $data[2] = implode(str_split(Config::$business_postcode));
65 $data[3] = Config::$business_city;
66
67 $latex = makeLatex($documentType, $data); // injection des variables
68 $file_name = 'enveloppe_verso';
69 }
70 else
71 {
72 $data = $Client->getAll();
73
74 if($Presta == null) // recto d'une enveloppe
75 {
76 $latex_path .= 'enveloppes_recto/';
77 $pdf_path .= 'enveloppes_recto/';
78 $data['code_postal'] = implode(' \ ', str_split($data['code_postal'])); // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0
79 $latex = makeLatex($documentType, $data); // injection des variables
80 $file_name = $Client->getCodeClient() . '.tex';
81 }
82 else // facture, devis, location
83 {
84 $data = array_merge($data, $Presta->getAll());
85 $Date = new Dates($Presta->getDate()); // entrée = timestamp (doit être un "int"!!)
86 $year = $Date->getYear();
87
88 // créer un sous-dossier pour la nouvelle année
89 $latex_path .= $year . '/';
90 $pdf_path .= $year . '/';
91
92 if($Details != null)
93 {
94 $data = array_merge($data, $Details->getAll());
95 }
96
97 $latex = makeLatex($documentType, $data, $Date); // injection des variables
98 $file_name = $Presta->getCodePresta() . '.tex';
99 }
100 }
101
102 // création des fichiers
103 makeFolder($latex_path);
104 makeFolder($pdf_path);
105 file_put_contents($latex_path. $file_name, $latex); // écriture du fichier
52 latexToPdf($latex_path, $file_name, $pdf_path); 106 latexToPdf($latex_path, $file_name, $pdf_path);
53} 107}
54 108
55function getLatexFromTemplate(string $template, $data) 109function makeLatex(string $documentType, array $data, Dates $Date = null)
56{ 110{
57 // variables à injecter 111 $date = '';
58 $nom_client = "M. Duchmol"; 112 if($Date != null)
113 {
114 $date = $Date->getDate();
115 $date = preg_replace('#\D#', '/', $date); // date avec des slashs / parce que j'aime bien
116 }
59 117
60 // on obtient la variable $latex avec ob_get_clean() 118 // on obtient la variable $latex avec ob_get_clean()
61 include('latex_templates/' . $template . '.php'); 119 // le include() ici, c'est du génie ou c'est moche ou les deux?
120 // un nouveau fichier php est inséré à chaque itération
121 include('latex_templates/' . $documentType . '.php');
62 122
63 // on retourne le buffer 123 // on retourne le buffer
64 // normallement le code PHP inséré avec include est nettoyé en quittant la fonction 124 // normallement le code PHP inséré avec include est nettoyé en quittant la fonction
@@ -76,7 +136,7 @@ function latexToPdf(string $latexPath, string $fileName, string $pdfPath)
76 136
77 // compilation 137 // compilation
78 exec('pdflatex ' . $outputDir . $latexPath . $fileName); 138 exec('pdflatex ' . $outputDir . $latexPath . $fileName);
79 139
80 // nettoyage 140 // nettoyage
81 $basename = basename($fileName, '.tex'); 141 $basename = basename($fileName, '.tex');
82 unlink($pdfPath . $basename . '.aux'); 142 unlink($pdfPath . $basename . '.aux');