summaryrefslogtreecommitdiff
path: root/src/Latex.php
blob: 871a84597435ec3cb01c9b86e6610a10cba6e348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
// src/Latex.php
//
// génération du code LaTeX

abstract class Latex
{
    protected string $type;
    protected string $file_name;
    protected string $latex_path;
    protected string $pdf_path;
    protected array $data = []; // données à insérer dans le template
    protected string $latex; // latex pur
    
    static function makeInstance(string $type): ?Latex
    {
        switch($type){
            // documents pour les clients
            case 'devis':
                return new DevisLatex;
            case 'facture':
                return new FactureLatex;
            case 'location':
                return new LocationLatex;
            case 'enveloppe_recto':
                return new EnveloppeRectoLatex;
            case 'enveloppe_verso':
                return new EnveloppeVersoLatex;
             // pas de document
            case 'cesu':
                return null;
            case 'non_vendue':
                return null;
            
            // documents pour la compta
            
            // erreur
            default:
                echo "erreur, la sous-classe latex à initialiser n'existe pas\n";
                return null;
        }
    }
    
    //~ public function __construct()
    //~ {}
    
    public function makeLatex()
    {
        $data = $this->data; // tableau à inséré dans du code latex
        include('src/latex_templates/' . $this->type . '.php');
        $this->latex = $latex; // buffer récupéré par ob_get_clean()
        
        // QUESTION:
        // cette utilisation de include(), c'est du génie ou c'est moche ou les deux?
        // un nouveau fichier est inséré à chaque appel de makeLatex()
        // puis la mémoire est libérée à la fin de la fonction
        // le PHP inséré fait donc partie de cette "mémoire"
    }

    public function makeTexAndPdf(): void // document polymorphe
    {
        if(get_class($this) !== 'EnveloppeVersoLatex')
        {
            File::makeFolder($this->latex_path);
            File::makeFolder($this->pdf_path);
        }
        File::makeFile($this->latex_path, $this->file_name, $this->latex); // fichier .tex
        $this->latexToPdf();
    }

    // commande système pdflatex
    private function latexToPdf(): void
    {
        // paramètre optionnel
        $output_dir = '';
        if($this->pdf_path !== '')
        {
            $output_dir = '-output-directory=' . $this->pdf_path;
        }
        
        // compilation
        exec('pdflatex ' . $output_dir . ' ' . $this->latex_path . $this->file_name);
        
        // nettoyage
        $basename = basename($this->file_name, '.tex');
        unlink($this->pdf_path . $basename . '.aux');
        unlink($this->pdf_path . $basename . '.log');
    }
    
    // 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): Latex
    {
        $this->data = array_merge($this->data, $Object->getAll()); // nécessite des tableaux associatifs
        return $this; // pour chainer les méthodes
    }
}


abstract class PrestaLatex extends Latex
{
    protected array $date_array; // [année, mois, jour]
    
    public function setData(Object $Object): Latex // polymorphisme
    {
        $this->data = array_merge($this->data, $Object->getAll()); // nécessite des tableaux associatifs
        if(isset($this->data['date'])){
            $this->data['date'] = str_replace('-', '/', $this->data['date']); // 17/10/2026, avec des / parce que j'aime bien
        }

        if(in_array(get_class($Object), ['Facture', 'Devis', 'Location'])){
            $this->file_name = $Object->getCodePresta() . '.tex';
            $this->date_array = explode('-', $Object->getdate());
            $this->latex_path = Config::$latex_path . $this->date_array[0] . DIRECTORY_SEPARATOR;
            $this->pdf_path = Config::$pdf_path . $this->date_array[0] . DIRECTORY_SEPARATOR;
        }
        
        return $this; // pour chainer les méthodes
    }
    
    // forme = code-presta.tex
    //~ protected function nameTheFile(string $quoi, string $codePresta)
    //~ {
        //~ $this->fileName = $quoi . '-' . $codePresta . '.tex';
    //~ }
    
    public function makeLatex(): void
    {
        $data = $this->data;
        //$date = preg_replace('#\D#', '/', $this->date_array[2] . '/' . $this->date_array[1] . '/' . $this->date_array[0]);
        
        // 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('src/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 DevisLatex extends PrestaLatex // extends Latex
{
    protected string $type = 'devis';
}
class FactureLatex extends PrestaLatex // extends Latex
{
    protected string $type = 'facture';
}
class LocationLatex extends PrestaLatex // extends Latex
{
    protected string $type = 'location';
}

class EnveloppeRectoLatex extends Latex
{
    protected string $type = 'enveloppe_recto';
    public function __construct(){
        $this->latex_path = Config::$latex_path . 'enveloppes_recto/';
        $this->pdf_path = Config::$pdf_path . 'enveloppes_recto/';
    }
    
    public function setData(Object $Client): Latex // ici de type Clients
    {
        $this->file_name = $Client->getCodeClient() . '.tex';
        $this->data = array_merge($this->data, $Client->getAll()); // nécessite des tableaux associatifs
        $this->spacesInPostCode(); // nécessite $this->data

        return $this; // pour chainer les méthodes
    }
    
    // code postal avec 2 espaces entre chaque chiffre: 2 \ 9 \ 0 \ 0 \ 0
    protected function spacesInPostCode()
    {
        $this->data['code_postal_espaces'] = implode(' \ ', str_split($this->data['code_postal']));
    }
}
class EnveloppeVersoLatex extends Latex
{
    protected string $type = 'enveloppe_verso';
    protected string $file_name = 'enveloppe_verso.tex';
    public function __construct(){
        $this->latex_path = Config::$latex_path;
        $this->pdf_path = Config::$pdf_path;
    }
}



abstract class ComptaLatex extends Latex
{
    //~ 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';
    //~ }
}

class LivreRecettesLatex extends ComptaLatex
{}

class RegistreAchatsLatex extends ComptaLatex
{}

class BilanLatex extends ComptaLatex
{}