RepDominicana::setHeader()   F
last analyzed

Complexity

Conditions 18
Paths 760

Size

Total Lines 109
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 18
eloc 66
c 2
b 1
f 0
nc 760
nop 3
dl 0
loc 109
rs 1.0333

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
 * Copyright (C) 2023-2024 Joe Nilson <[email protected]>
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
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (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
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Lib\Tickets;
19
20
use FacturaScripts\Core\Template\ExtensionsTrait;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Template\ExtensionsTrait 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...
21
use FacturaScripts\Core\Template\ModelClass;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Template\ModelClass 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...
22
use FacturaScripts\Core\Tools;
23
use FacturaScripts\Dinamic\Lib\Tickets\BaseTicket;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Lib\Tickets\BaseTicket 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...
24
use FacturaScripts\Dinamic\Model\Agente;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Agente 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...
25
use FacturaScripts\Dinamic\Model\Ticket;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Ticket 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...
26
use FacturaScripts\Dinamic\Model\TicketPrinter;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\TicketPrinter 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
use FacturaScripts\Dinamic\Model\User;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\User 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...
28
use Mike42\Escpos\Printer;
0 ignored issues
show
Bug introduced by
The type Mike42\Escpos\Printer 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...
29
30
class RepDominicana extends BaseTicket
31
{
32
    use ExtensionsTrait;
33
    public static function print(ModelClass $model, TicketPrinter $printer, User $user, Agente $agent = null): bool
34
    {
35
        static::init();
36
37
        $ticket = new Ticket();
38
        $ticket->idprinter = $printer->id;
39
        $ticket->nick = $user->nick;
40
        $ticket->title = $model->codigo;
41
42
        static::setHeader($model, $printer, $ticket->title);
43
        static::setBody($model, $printer);
44
        static::setFooter($model, $printer);
45
        $ticket->body = static::getBody();
46
        $ticket->base64 = true;
47
        $ticket->appversion = 1;
48
49
        if ($agent) {
50
            $ticket->codagente = $agent->codagente;
51
        }
52
53
        return $ticket->save();
54
    }
55
56
    protected static function setHeader(ModelClass $model, TicketPrinter $printer, string $title): void
57
    {
58
        if ($printer->print_stored_logo) {
59
            static::$escpos->setJustification(Printer::JUSTIFY_CENTER);
60
            // imprimimos el logotipo almacenado en la impresora
61
            static::$connector->write("\x1Cp\x01\x00\x00");
62
            static::$escpos->feed();
63
        }
64
65
        // obtenemos los datos de la empresa
66
        $company = $model->getCompany();
67
68
        // establecemos el tamaño de la fuente
69
        static::$escpos->setTextSize($printer->title_font_size, $printer->title_font_size);
70
71
        // imprimimos el nombre corto de la empresa
72
        if ($printer->print_comp_shortname) {
73
            static::$escpos->text(static::sanitize($company->nombrecorto) . "\n");
74
            static::$escpos->setTextSize($printer->head_font_size, $printer->head_font_size);
75
76
            // imprimimos el nombre de la empresa
77
            static::$escpos->text(static::sanitize($company->nombre) . "\n");
78
        } else {
79
            // imprimimos el nombre de la empresa
80
            static::$escpos->text(static::sanitize($company->nombre) . "\n");
81
            static::$escpos->setTextSize($printer->head_font_size, $printer->head_font_size);
82
        }
83
84
        static::$escpos->setJustification();
85
86
        // imprimimos la dirección de la empresa
87
        static::$escpos->text(static::sanitize($company->direccion) . "\n");
88
        static::$escpos->text(static::sanitize("CP: " . $company->codpostal . ', ' . $company->ciudad) . "\n");
89
        static::$escpos->text(static::sanitize($company->tipoidfiscal . ': ' . $company->cifnif) . "\n\n");
90
91
        if ($printer->print_comp_tlf) {
92
            if (false === empty($company->telefono1) && false === empty($company->telefono2)) {
93
                static::$escpos->text(static::sanitize($company->telefono1 . ' / ' . $company->telefono2) . "\n");
94
            } elseif (false === empty($company->telefono1)) {
95
                static::$escpos->text(static::sanitize($company->telefono1) . "\n");
96
            } elseif (false === empty($company->telefono2)) {
97
                static::$escpos->text(static::sanitize($company->telefono2) . "\n");
98
            }
99
        }
100
101
        // imprimimos el título del documento
102
        static::$escpos->text(static::sanitize($title) . "\n");
103
104
        static::setHeaderTPV($model, $printer);
105
106
        // si es un documento de venta
107
        // imprimimos la fecha y el cliente
108
        if (in_array(
109
                $model->modelClassName(),
110
                [
111
                'PresupuestoCliente',
112
                'PedidoCliente',
113
                'AlbaranCliente',
114
                'FacturaCliente'
115
                ]
116
            )
117
        ) {
118
            static::$escpos->text(static::sanitize(
119
                static::$i18n->trans('date') . ': ' . $model->fecha . ' ' . $model->hora
120
                ) . "\n");
121
            static::$escpos->text(static::sanitize(
122
                static::$i18n->trans('customer') . ': ' . $model->nombrecliente
123
                ) . "\n");
124
            if(strlen($model->cifnif) == 9) {
125
                static::$escpos->text(static::sanitize(
126
                    static::$i18n->trans('title-cifnif-rnc') . ': ' . $model->cifnif
127
                ) . "\n\n");
128
            } else {
129
                static::$escpos->text(static::sanitize(
130
                static::$i18n->trans('title-cifnif-ci') . ': ' . $model->cifnif
131
                ) . "\n\n");
132
            }
133
134
135
            if ($model->modelClassName() === 'FacturaCliente') {
136
137
                if (property_exists($model, 'tipocomprobante') && $model->tipocomprobante) {
138
                    static::$escpos->text(static::sanitize(
139
                            static::$i18n->trans('tipo_comprobante') . ': ' .
140
                            $model->descripcionTipoComprobante()
141
                        ). "\n");
142
                }
143
144
                if (property_exists($model, 'numeroncf') && $model->numeroncf) {
145
                    static::$escpos->text(static::sanitize(
146
                        static::$i18n->trans('ncf-number') . ': ' . $model->numeroncf
147
                    ). "\n");
148
                }
149
150
                if (property_exists($model, 'ncffechavencimiento') && $model->ncffechavencimiento) {
151
                    static::$escpos->text(static::sanitize(
152
                        static::$i18n->trans('due-date') . ': ' . $model->ncffechavencimiento
153
                    ). "\n\n");
154
                } else {
155
                    static::$escpos->text("\n\n");
156
                }
157
            }
158
        }
159
160
        // añadimos la cabecera
161
        if ($printer->head) {
162
            static::$escpos->setJustification(Printer::JUSTIFY_CENTER);
163
            static::$escpos->text(static::sanitize($printer->head) . "\n\n");
164
            static::$escpos->setJustification();
165
        }
166
    }
167
168
    protected static function getTipoComprobanteRD(string $numero): string
169
    {
170
        switch ($numero) {
171
            case '01':
172
                return static::$i18n->trans('desc-ncf-type-01');
173
174
            case '02':
175
                return static::$i18n->trans('desc-ncf-type-02');
176
177
            case '03':
178
                return static::$i18n->trans('desc-ncf-type-03');
179
180
            case '04':
181
                return static::$i18n->trans('desc-ncf-type-04');
182
183
            case '11':
184
                return static::$i18n->trans('desc-ncf-type-11');
185
186
            case '12':
187
                return static::$i18n->trans('desc-ncf-type-12');
188
189
            case '13':
190
                return static::$i18n->trans('desc-ncf-type-13');
191
192
            case '14':
193
                return static::$i18n->trans('desc-ncf-type-14');
194
195
            case '15':
196
                return static::$i18n->trans('desc-ncf-type-15');
197
198
            case '16':
199
                return static::$i18n->trans('desc-ncf-type-16');
200
201
            case '17':
202
                return static::$i18n->trans('desc-ncf-type-17');
203
204
            default:
205
                return '';
206
        }
207
    }
208
}