Completed
Push — master ( 35135d...676ec5 )
by Esteban De La Fuente
02:22
created

LibroCompraVenta::agregar()   F

Complexity

Conditions 58
Paths 18

Size

Total Lines 180

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 180
rs 3.3333
c 0
b 0
f 0
cc 58
nc 18
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * LibreDTE
5
 * Copyright (C) SASCO SpA (https://sasco.cl)
6
 *
7
 * Este programa es software libre: usted puede redistribuirlo y/o
8
 * modificarlo bajo los términos de la Licencia Pública General Affero de GNU
9
 * publicada por la Fundación para el Software Libre, ya sea la versión
10
 * 3 de la Licencia, o (a su elección) cualquier versión posterior de la
11
 * misma.
12
 *
13
 * Este programa se distribuye con la esperanza de que sea útil, pero
14
 * SIN GARANTÍA ALGUNA; ni siquiera la garantía implícita
15
 * MERCANTIL o de APTITUD PARA UN PROPÓSITO DETERMINADO.
16
 * Consulte los detalles de la Licencia Pública General Affero de GNU para
17
 * obtener una información más detallada.
18
 *
19
 * Debería haber recibido una copia de la Licencia Pública General Affero de GNU
20
 * junto a este programa.
21
 * En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>.
22
 */
23
24
namespace sasco\LibreDTE\Sii\Dte\PDF;
25
26
/**
27
 * Clase para generar el PDF de la IECV
28
 * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
29
 * @version 2017-07-26
30
 */
31
class LibroCompraVenta extends \sasco\LibreDTE\PDF
32
{
33
34
    private $dte_tipo_operacion = [
35
        'suma' => [30, 32, 33, 34, 35, 38, 39, 41, 48, 55, 56, 110, 111, 914],
36
        'resta' => [45, 46, 60, 61, 112],
37
    ]; ///< Tipos de operaciones si suman o restan los DTE al vender
38
39
    /**
40
     * Constructor de la clase
41
     * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
42
     * @version 2016-03-09
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct('L');
47
        $this->SetTitle('IECV');
48
    }
49
50
    /**
51
     * Método que agrega un libro al PDF
52
     * @param libro
53
     * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
54
     * @version 2016-10-06
55
     */
56
    public function agregar(array $libro)
57
    {
58
        $this->startPageGroup();
59
        $this->AddPage();
60
        if (isset($libro['LibroCompraVenta']))
61
            $libro = $libro['LibroCompraVenta'];
62
        // título del libro
63
        $this->SetFont('helvetica', 'B', 16);
64
        $this->Texto('Libro de '.ucfirst(strtolower($libro['EnvioLibro']['Caratula']['TipoOperacion'])), null, null, 'C');
65
        $this->Ln();
66
        $this->Ln();
67
        // carátula
68
        $this->SetFont('helvetica', 'B', 12);
69
        $this->Texto('I.- Carátula');
70
        $this->Ln();
71
        $this->Ln();
72
        $this->SetFont('helvetica', '', 9);
73
        $titulos = ['Emisor', 'Firma', 'Período', 'Resolución', 'N°', 'Operación', 'Tipo de libro', 'Tipo de envio', 'Aut. rectific.'];
74
        $this->addTable($titulos, [$libro['EnvioLibro']['Caratula']]);
75
        $this->Ln();
76
        // resumenes
77
        $this->SetFont('helvetica', 'B', 12);
78
        $this->Texto('II.- Resumen');
79
        $this->Ln();
80
        $this->Ln();
81
        $this->SetFont('helvetica', '', 9);
82
        if (isset($libro['EnvioLibro']['ResumenPeriodo']['TotalesPeriodo'])) {
83
            // agregar resumen
84
            if (!isset($libro['EnvioLibro']['ResumenPeriodo']['TotalesPeriodo'][0])) {
85
                $libro['EnvioLibro']['ResumenPeriodo']['TotalesPeriodo'] = [$libro['EnvioLibro']['ResumenPeriodo']['TotalesPeriodo']];
86
            }
87
            $resumen = [];
88
            $total_resumen = [
89
                    'TotDoc' => 0,
90
                    'TotMntExe' => 0,
91
                    'TotMntNeto' => 0,
92
                    'TotMntIVA' => 0,
93
                    'CodImp' => 0,
94
                    'TotMntImp' => 0,
95
                    'TotIVARetParcial' => 0,
96
                    'TotIVARetTotal' => 0,
97
                    'TotIVANoRetenido' => 0,
98
                    'TotMntTotal' => 0,
99
            ];
100
            foreach ($libro['EnvioLibro']['ResumenPeriodo']['TotalesPeriodo'] as $total) {
101
                // contabilizar totales del resumen
102
                $total_resumen['TotDoc'] += $total['TotDoc'];
103
                if (in_array($total['TpoDoc'], $this->dte_tipo_operacion['suma'])) {
104
                    $total_resumen['TotMntExe'] += !empty($total['TotMntExe']) ? $total['TotMntExe'] : 0;
105
                    $total_resumen['TotMntNeto'] += !empty($total['TotMntNeto']) ? $total['TotMntNeto'] : 0;
106
                    $total_resumen['TotMntIVA'] += !empty($total['TotMntIVA']) ? $total['TotMntIVA'] : 0;
107
                    $total_resumen['TotMntImp'] += !empty($total['TotOtrosImp']['TotMntImp']) ? $total['TotOtrosImp']['TotMntImp'] : 0;
108
                    $total_resumen['TotIVARetParcial'] += !empty($total['TotIVARetParcial']) ? $total['TotIVARetParcial'] : 0;
109
                    $total_resumen['TotIVARetTotal'] += !empty($total['TotIVARetTotal']) ? $total['TotIVARetTotal'] : 0;
110
                    $total_resumen['TotIVANoRetenido'] += !empty($total['TotIVANoRetenido']) ? $total['TotIVANoRetenido'] : 0;
111
                    $total_resumen['TotMntTotal'] += $total['TotMntTotal'];
112
                }
113
                else if (in_array($total['TpoDoc'], $this->dte_tipo_operacion['resta'])) {
114
                    $total_resumen['TotMntExe'] -= !empty($total['TotMntExe']) ? $total['TotMntExe'] : 0;
115
                    $total_resumen['TotMntNeto'] -= !empty($total['TotMntNeto']) ? $total['TotMntNeto'] : 0;
116
                    $total_resumen['TotMntIVA'] -= !empty($total['TotMntIVA']) ? $total['TotMntIVA'] : 0;
117
                    $total_resumen['TotMntImp'] -= !empty($total['TotOtrosImp']['TotMntImp']) ? $total['TotOtrosImp']['TotMntImp'] : 0;
118
                    $total_resumen['TotIVARetParcial'] -= !empty($total['TotIVARetParcial']) ? $total['TotIVARetParcial'] : 0;
119
                    $total_resumen['TotIVARetTotal'] -= !empty($total['TotIVARetTotal']) ? $total['TotIVARetTotal'] : 0;
120
                    $total_resumen['TotIVANoRetenido'] -= !empty($total['TotIVANoRetenido']) ? $total['TotIVANoRetenido'] : 0;
121
                    $total_resumen['TotMntTotal'] -= $total['TotMntTotal'];
122
                }
123
                // agregar al resumen
124
                $resumen[] = [
125
                    $total['TpoDoc'],
126
                    num($total['TotDoc']),
127
                    !empty($total['TotMntExe']) ? num($total['TotMntExe']) : '',
128
                    !empty($total['TotMntNeto']) ? num($total['TotMntNeto']) : '',
129
                    !empty($total['TotMntIVA']) ? num($total['TotMntIVA']) : '',
130
                    !empty($total['TotOtrosImp']['CodImp']) ? $total['TotOtrosImp']['CodImp'] : '',
131
                    !empty($total['TotOtrosImp']['TotMntImp']) ? num($total['TotOtrosImp']['TotMntImp']) : '',
132
                    !empty($total['TotIVARetParcial']) ? num($total['TotIVARetParcial']) : '',
133
                    !empty($total['TotIVARetTotal']) ? num($total['TotIVARetTotal']) : '',
134
                    !empty($total['TotIVANoRetenido']) ? num($total['TotIVANoRetenido']) : '',
135
                    num($total['TotMntTotal']),
136
                ];
137
            }
138
            // agregar totales
139
            $resumen[] = [
140
                '',
141
                num($total_resumen['TotDoc']),
142
                !empty($total_resumen['TotMntExe']) ? num($total_resumen['TotMntExe']) : '',
143
                !empty($total_resumen['TotMntNeto']) ? num($total_resumen['TotMntNeto']) : '',
144
                !empty($total_resumen['TotMntIVA']) ? num($total_resumen['TotMntIVA']) : '',
145
                '',
146
                !empty($total_resumen['TotMntImp']) ? num($total_resumen['TotMntImp']) : '',
147
                !empty($total_resumen['TotIVARetParcial']) ? num($total_resumen['TotIVARetParcial']) : '',
148
                !empty($total_resumen['TotIVARetTotal']) ? num($total_resumen['TotIVARetTotal']) : '',
149
                !empty($total_resumen['TotIVANoRetenido']) ? num($total_resumen['TotIVANoRetenido']) : '',
150
                num($total_resumen['TotMntTotal']),
151
            ];
152
            // agregar tabla
153
            $titulos = ['DTE', 'Total', 'Exento', 'Neto', 'IVA', 'Imp', 'Monto', 'Ret parc.', 'Ret tot.', 'No reten.', 'Total'];
154
            $this->addTable($titulos, $resumen, ['width'=>[10, 19, 26, 27, 26, 10, 26, 26, 26, 26, 27]]);
155
        } else {
156
            $this->Texto('Sin movimientos');
157
            $this->Ln();
158
            $this->Ln();
159
        }
160
        // detalle
161
        $this->SetFont('helvetica', 'B', 12);
162
        $this->Texto('III.- Detalle');
163
        $this->Ln();
164
        $this->Ln();
165
        $this->SetFont('helvetica', '', 9);
166
        if (isset($libro['EnvioLibro']['Detalle'])) {
167
            if (!isset($libro['EnvioLibro']['Detalle'][0])) {
168
                $libro['EnvioLibro']['Detalle'] = [$libro['EnvioLibro']['Detalle']];
169
            }
170
            $detalle = [];
171
            foreach ($libro['EnvioLibro']['Detalle'] as $d) {
172
                // impuesto adicional
173
                if (!empty($d['OtrosImp'])) {
174
                    if (!isset($d['OtrosImp'][0]))
175
                        $d['OtrosImp'] = [$d['OtrosImp']];
176
                    $n_OtrosImp = count($d['OtrosImp']);
177
                } else $n_OtrosImp = 0;
178
                // se agrega detalle
179
                $detalle[] = [
180
                    $d['TpoDoc'],
181
                    $d['NroDoc'],
182
                    !empty($d['FchDoc']) ? $d['FchDoc'] : ((!empty($d['Anulado']) && $d['Anulado']=='A') ? 'ANULADO' : ''),
183
                    !empty($d['RUTDoc']) ? $d['RUTDoc'] : '',
184
                    !empty($d['MntExe']) ? num($d['MntExe']) : '',
185
                    !empty($d['MntNeto']) ? num($d['MntNeto']) : '',
186
                    !empty($d['MntIVA']) ? num($d['MntIVA']) : '',
187
                    $n_OtrosImp ? $d['OtrosImp'][0]['CodImp'] : '',
188
                    $n_OtrosImp ? $d['OtrosImp'][0]['TasaImp'] : '',
189
                    $n_OtrosImp ? num($d['OtrosImp'][0]['MntImp']) : '',
190
                    !empty($d['IVARetParcial']) ? num($d['IVARetParcial']) : '',
191
                    !empty($d['IVARetTotal']) ? num($d['IVARetTotal']) : '',
192
                    !empty($d['IVANoRetenido']) ? num($d['IVANoRetenido']) : '',
193
                    isset($d['MntTotal']) ? num($d['MntTotal']) : '',
194
                ];
195
                // agregar otros impuestos adicionales
196
                if ($n_OtrosImp>1) {
197
                    for ($i=1; $i<$n_OtrosImp; $i++) {
198
                        $detalle[] = [
199
                            '',
200
                            '',
201
                            '',
202
                            '',
203
                            '',
204
                            '',
205
                            '',
206
                            $d['OtrosImp'][$i]['CodImp'],
207
                            $d['OtrosImp'][$i]['TasaImp'],
208
                            num($d['OtrosImp'][$i]['MntImp']),
209
                            '',
210
                            '',
211
                            '',
212
                            '',
213
                        ];
214
                    }
215
                }
216
            }
217
            $titulos = ['DTE', 'Folio', 'Emisión', 'RUT', 'Exento', 'Neto', 'IVA', 'Imp', 'Tasa', 'Monto', 'Ret parc.', 'Ret tot.', 'No reten.', 'Total'];
218
            $this->addTable($titulos, $detalle, ['fontsize'=>9, 'width'=>[10, 19, 20, 20, 20, 20, 20, 10, 10, 20, 20, 20, 20, 20]], false);
219
        } else {
220
            $this->Texto('No hay detalle de documentos');
221
            $this->Ln();
222
            $this->Ln();
223
        }
224
        // firma
225
        $this->SetFont('helvetica', 'B', 12);
226
        $this->Texto('IV.- Firma electrónica');
227
        $this->Ln();
228
        $this->Ln();
229
        $this->SetFont('helvetica', '', 9);
230
        $titulos = ['Fecha y hora', 'Digest'];
231
        $this->addTable($titulos, [[
232
            str_replace('T', ' ', $libro['EnvioLibro']['TmstFirma']),
233
            isset($libro['Signature']) ? $libro['Signature']['SignedInfo']['Reference']['DigestValue'] : 'Sin firma',
234
        ]]);
235
    }
236
237
}
238