Passed
Push — master ( d67de9...7babde )
by Joe Nilson
02:15
created

residentes_pdf::generar_pdf_lineas_tablas()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 17
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 25
rs 9.7
1
<?php
2
/*
3
 * Copyright (C) 2018 Joe Nilson <joenilson at gmail.com>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
require_once 'plugins/facturacion_base/extras/fs_pdf.php';
19
require_once 'plugins/residentes/extras/residentes_controller.php';
20
21
/**
22
 * Description of residentes_pdf
23
 *
24
 * @author Joe Nilson <joenilson at gmail.com>
25
 */
26
class residentes_pdf extends fs_pdf
0 ignored issues
show
Bug introduced by
The type fs_pdf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
{
28
    const LOGO_X = 40;
29
    const LOGO_Y = 700;
30
31
    public $cliente_residente;
32
33
    public function calcular_tamanyo_logo()
34
    {
35
        $tamanyo = $size = getimagesize($this->logo);
36
        if ($size[0] > 200) {
37
            $tamanyo[0] = 200;
38
            $tamanyo[1] = $tamanyo[1] * $tamanyo[0] / $size[0];
39
            $size[0] = $tamanyo[0];
40
            $size[1] = $tamanyo[1];
41
        }
42
43
        if ($size[1] > 80) {
44
            $tamanyo[1] = 80;
45
            $tamanyo[0] = $tamanyo[0] * $tamanyo[1] / $size[1];
46
        }
47
48
        return $tamanyo;
49
    }
50
51
    public function generar_tipo_doc($tipo_documento)
52
    {
53
        return ucfirst(str_replace('_', ' ', $tipo_documento));
54
    }
55
56
    public function generar_pdf_cabecera(&$empresa, &$lppag)
57
    {
58
        /// ¿Añadimos el logo?
59
        if ($this->logo !== false) {
60
            if (function_exists('imagecreatefromstring')) {
61
                $lppag -= 2; /// si metemos el logo, caben menos líneas
62
63
                $tamanyo = $this->calcular_tamanyo_logo();
64
                if (strtolower(substr($this->logo, -4)) === '.png') {
65
                    $this->pdf->addPngFromFile($this->logo, self::LOGO_X, self::LOGO_Y, $tamanyo[0], $tamanyo[1]);
66
                } elseif (function_exists('imagepng')) {
67
                    /**
68
                     * La librería ezpdf tiene problemas al redimensionar jpegs,
69
                     * así que hacemos la conversión a png para evitar estos problemas.
70
                     */
71
                    if (imagepng(imagecreatefromstring(file_get_contents($this->logo)), FS_MYDOCS
1 ignored issue
show
Bug introduced by
The constant FS_MYDOCS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
72
                        . 'images/logo.png')) {
73
                        $this->pdf->addPngFromFile(
74
                            FS_MYDOCS . 'images/logo.png',
75
                            self::LOGO_X,
76
                            self::LOGO_Y,
77
                            $tamanyo[0],
78
                            $tamanyo[1]
79
                        );
80
                    } else {
81
                        $this->pdf->addJpegFromFile($this->logo, self::LOGO_X, self::LOGO_Y, $tamanyo[0], $tamanyo[1]);
82
                    }
83
                } else {
84
                    $this->pdf->addJpegFromFile($this->logo, self::LOGO_X, self::LOGO_Y, $tamanyo[0], $tamanyo[1]);
85
                }
86
87
                $this->pdf->ez['rightMargin'] = 40;
88
                $this->pdf->ezText(
89
                    "<b>" . fs_fix_html($empresa->nombre) . "</b>",
90
                    12,
91
                    array('justification' => 'right')
92
                );
93
                $this->pdf->ezText(FS_CIFNIF . ": " . $empresa->cifnif, 8, array('justification' => 'right'));
1 ignored issue
show
Bug introduced by
The constant FS_CIFNIF was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
95
                $direccion = $empresa->direccion . "\n";
96
                if ($empresa->apartado) {
97
                    $direccion .= ucfirst(FS_APARTADO) . ': ' . $empresa->apartado . ' - ';
1 ignored issue
show
Bug introduced by
The constant FS_APARTADO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
98
                }
99
100
                if ($empresa->codpostal) {
101
                    $direccion .= 'CP: ' . $empresa->codpostal . ' - ';
102
                }
103
104
                if ($empresa->ciudad) {
105
                    $direccion .= $empresa->ciudad . ' - ';
106
                }
107
108
                if ($empresa->provincia) {
109
                    $direccion .= '(' . $empresa->provincia . ')';
110
                }
111
112
                if ($empresa->telefono) {
113
                    $direccion .= "\nTeléfono: " . $empresa->telefono;
114
                }
115
116
                $this->pdf->ezText(fs_fix_html($direccion) . "\n", 8, array('justification' => 'right'));
117
                $this->set_y(self::LOGO_Y + 10);
118
            } else {
119
                die('ERROR: no se encuentra la función imagecreatefromstring(). '
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
120
                    . 'Y por tanto no se puede usar el logotipo en los documentos.');
121
            }
122
        } else {
123
            $this->pdf->ezText(
124
                "<b>" . fs_fix_html($empresa->nombre) . "</b>",
125
                12,
126
                array('justification' => 'center')
127
            );
128
            $this->pdf->ezText(FS_CIFNIF . ": " . $empresa->cifnif, 8, array('justification' => 'center'));
129
130
            $direccion = $empresa->direccion;
131
            if ($empresa->apartado) {
132
                $direccion .= ' - ' . ucfirst(FS_APARTADO) . ': ' . $empresa->apartado;
133
            }
134
135
            if ($empresa->codpostal) {
136
                $direccion .= ' - CP: ' . $empresa->codpostal;
137
            }
138
139
            if ($empresa->ciudad) {
140
                $direccion .= ' - ' . $empresa->ciudad;
141
            }
142
143
            if ($empresa->provincia) {
144
                $direccion .= ' (' . $empresa->provincia . ')';
145
            }
146
147
            if ($empresa->telefono) {
148
                $direccion .= ' - Teléfono: ' . $empresa->telefono;
149
            }
150
151
            $this->pdf->ezText(fs_fix_html($direccion), 8, array('justification' => 'center'));
152
        }
153
    }
154
155
    public function generar_datos_residente(&$pdf_doc, $tipo_documento, &$lppag, $table_width = 560)
156
    {
157
        $width_campo1 = 110;
158
        $tipo_doc = $this->generar_tipo_doc($tipo_documento);
159
        $tipo_residente = ($this->cliente_residente->informacion->propietario) ? 'Propietario' : 'Inquilino';
160
        /*
161
         * Esta es la tabla con los datos del cliente:
162
         * Informe Cobros           Fecha:
163
         * Cliente:        Tipo Residente:
164
         * Dirección:           Teléfonos:
165
         */
166
        $pdf_doc->new_table();
167
        $pdf_doc->add_table_row(
168
            array(
169
                'campo1' => "<b>" . $tipo_doc . "</b>",
170
                'dato1' => '',
171
                'campo2' => "<b>Fecha Impresión:</b> " . \date('d-m-Y H:i:s')
172
            )
173
        );
174
175
        $pdf_doc->add_table_row(
176
            array(
177
                'campo1' => "<b>Residente:</b>",
178
                'dato1' => fs_fix_html($this->cliente_residente->nombre),
179
                'campo2' => "<b>Tipo Residente:</b> " . $tipo_residente
180
            )
181
        );
182
183
        $row = array(
184
            'campo1' => "<b>Inmueble:</b>",
185
            'dato1' => fs_fix_html($this->cliente_residente->inmueble->codigo_externo() .
186
                ' - ' . $this->cliente_residente->inmueble->numero),
187
            'campo2' => ''
188
        );
189
190
        if (!$this->cliente_residente) {
191
            /// nada
192
        } elseif ($this->cliente_residente->telefono1) {
193
            $row['campo2'] = "<b>Teléfonos:</b> " . $this->cliente_residente->telefono1;
194
            if ($this->cliente_residente->telefono2) {
195
                $row['campo2'] .= "\n" . $this->cliente_residente->telefono2;
196
                $lppag -= 2;
197
            }
198
        } elseif ($this->cliente_residente->telefono2) {
199
            $row['campo2'] = "<b>Teléfonos:</b> " . $this->cliente_residente->telefono2;
200
        }
201
        $pdf_doc->add_table_row($row);
202
203
        $pdf_doc->save_table(
204
            array(
205
                'cols' => array(
206
                    'campo1' => array('width' => $width_campo1, 'justification' => 'right'),
207
                    'dato1' => array('justification' => 'left'),
208
                    'campo2' => array('justification' => 'right')
209
                ),
210
                'showLines' => 0,
211
                'width' => $table_width,
212
                'shaded' => 0,
213
                'fontSize' => 8
214
            )
215
        );
216
        $pdf_doc->pdf->ezText("\n", 10);
217
    }
218
219
    public function generar_pdf_lineas(&$pdf_doc, &$items, &$linea_actual, &$lppag, $tipo, $table_width = 540)
220
    {
221
        /// calculamos el número de páginas
222
        if (!isset($this->numpaginas)) {
223
            $this->numpaginas = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property numpaginas does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
224
            $lineas = 0;
225
            while ($lineas < count($items)) {
226
                $lppag2 = $lppag;
227
                $this->verificar_longitud_linea($items, $lineas, $lppag2);
228
                $lineas += $lppag2;
229
                $this->numpaginas++;
230
            }
231
232
            if ($this->numpaginas === 0) {
233
                $this->numpaginas = 1;
234
            }
235
        }
236
237
        /// leemos las líneas para ver si hay que mostrar mas información
238
        $this->verificar_longitud_linea($items, $linea_actual, $lppag);
239
240
        /*
241
         * Creamos la tabla con las lineas de pendientes
242
         */
243
        $pdf_doc->new_table();
244
        [$table_header, $array_cols] = $this->generar_pdf_lineas_tablas($tipo);
245
        $pdf_doc->add_table_header($table_header);
246
247
        for ($i = $linea_actual; (($linea_actual < ($lppag + $i)) && ( $linea_actual < count($items)));) {
248
            $fila = $this->generar_pdf_lineas_fila($tipo, $items, $linea_actual);
249
            $pdf_doc->add_table_row($fila);
250
            $linea_actual++;
251
        }
252
253
        $pdf_doc->save_table(
254
            array(
255
                'fontSize' => 8,
256
                'cols' => $array_cols,
257
                'width' => $table_width,
258
                'shaded' => 1,
259
                'shadeCol' => array(0.95, 0.95, 0.95),
260
                'lineCol' => array(0.3, 0.3, 0.3),
261
            )
262
        );
263
    }
264
265
    public function generar_pdf_lineas_tablas($tipo)
266
    {
267
        $table_header = array(
268
            'item' => '<b>Pendiente de Pago</b>', 'fecha' => '<b>Fecha</b>',
269
            'vencimiento' => '<b>Vencimiento</b>', 'importe' => '<b>Monto</b>',
270
            'descuento' => '<b>Descuento</b>', 'total' => '<b>Total</b>', 'atraso' => '<b>Atraso</b>',
271
        );
272
        $array_cols = array(
273
            'item' => array('justification' => 'left'), 'fecha' => array('justification' => 'center'),
274
            'vencimiento' => array('justification' => 'center'), 'importe' => array('justification' => 'right'),
275
            'descuento' => array('justification' => 'right'),
276
            'total' => array('justification' => 'right'), 'atraso' => array('justification' => 'center')
277
        );
278
        if ($tipo === 'pagado') {
279
            $table_header = array(
280
                'item' => '<b>Pagos Realizados</b>', 'fecha' => '<b>Fecha</b>',
281
                'importe' => '<b>Monto</b>', 'f_pago' => '<b>F. Pago</b>'
282
            );
283
            $array_cols = array(
284
                'item' => array('justification' => 'left'), 'fecha' => array('justification' => 'center'),
285
                'importe' => array('justification' => 'right'), 'f_pago' => array('justification' => 'center')
286
            );
287
        }
288
289
        return array($table_header,$array_cols);
290
    }
291
292
    public function generar_pdf_lineas_fila($tipo, $items, $linea_actual)
293
    {
294
        $residentes_controller = new residentes_controller();
295
        $descripcion = fs_fix_html($items[$linea_actual]->descripcion);
296
        $fila = array(
297
            'item' => $descripcion,
298
            'fecha' => $items[$linea_actual]->fecha,
299
            'vencimiento' => $items[$linea_actual]->vencimiento,
300
            'importe' => $residentes_controller->show_precio(
301
                $items[$linea_actual]->pvpsindto,
302
                $residentes_controller->empresa->coddivisa,
303
                true,
304
                FS_NF0
1 ignored issue
show
Bug introduced by
The constant FS_NF0 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
305
            ),
306
            'descuento' => $residentes_controller->show_numero($items[$linea_actual]->dtopor) . " %",
307
            'total' => $residentes_controller->show_precio(
308
                $items[$linea_actual]->pvptotal,
309
                $residentes_controller->empresa->coddivisa,
310
                true,
311
                FS_NF0
312
            ),
313
            'atraso' => $items[$linea_actual]->dias_atraso
314
        );
315
        if ($tipo === 'pagado') {
316
            $fila = array(
317
                'item' => $descripcion,
318
                'fecha' => $items[$linea_actual]->fecha,
319
                'importe' => $residentes_controller->show_precio(
320
                    $items[$linea_actual]->pvptotal,
321
                    $residentes_controller->empresa->coddivisa,
322
                    true,
323
                    FS_NF0
324
                ),
325
                'f_pago' => $items[$linea_actual]->f_pago
326
            );
327
        }
328
        return $fila;
329
    }
330
331
    public function verificar_longitud_linea($items, &$lineas, &$lppag2)
332
    {
333
        foreach ($items as $i => $lin) {
334
            if ($i >= $lineas && $i < $lineas + $lppag2) {
335
                $linea_size = 1;
336
                $len = mb_strlen($lin->descripcion);
337
                while ($len > 85) {
338
                    $len -= 85;
339
                    $linea_size += 0.5;
340
                }
341
                $aux = explode("\n", $lin->descripcion);
342
                if (count($aux) > 1) {
343
                    $linea_size += 0.5 * ( count($aux) - 1);
344
                }
345
                if ($linea_size > 1) {
346
                    $lppag2 -= $linea_size - 1;
347
                }
348
            }
349
        }
350
    }
351
}