RepDominicana::setHeader()   F
last analyzed

Complexity

Conditions 18
Paths 760

Size

Total Lines 89
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 18
eloc 49
c 2
b 1
f 0
nc 760
nop 3
dl 0
loc 89
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
        $extensionVar = new static();
59
        $extensionVar->pipe('setHeaderBefore', $model, $printer);
60
61
        if ($printer->print_stored_logo) {
62
            static::$escpos->setJustification(Printer::JUSTIFY_CENTER);
63
            // imprimimos el logotipo almacenado en la impresora
64
            static::$connector->write("\x1Cp\x01\x00\x00");
65
            static::$escpos->feed();
66
        }
67
68
        // obtenemos los datos de la empresa
69
        $company = $model->getCompany();
70
71
        // establecemos el tamaño de la fuente
72
        static::$escpos->setTextSize($printer->title_font_size, $printer->title_font_size);
73
74
        // imprimimos el nombre corto de la empresa
75
        if ($printer->print_comp_shortname) {
76
            static::$escpos->text(static::sanitize($company->nombrecorto) . "\n");
77
            static::$escpos->setTextSize($printer->head_font_size, $printer->head_font_size);
78
79
            // imprimimos el nombre de la empresa
80
            static::$escpos->text(static::sanitize($company->nombre) . "\n");
81
        } else {
82
            // imprimimos el nombre de la empresa
83
            static::$escpos->text(static::sanitize($company->nombre) . "\n");
84
            static::$escpos->setTextSize($printer->head_font_size, $printer->head_font_size);
85
        }
86
87
        static::$escpos->setJustification();
88
89
        // imprimimos la dirección de la empresa
90
        static::$escpos->text(static::sanitize($company->direccion) . "\n");
91
        static::$escpos->text(static::sanitize("CP: " . $company->codpostal . ', ' . $company->ciudad) . "\n");
92
        static::$escpos->text(static::sanitize($company->tipoidfiscal . ': ' . $company->cifnif) . "\n\n");
93
94
        if ($printer->print_comp_tlf) {
95
            if (false === empty($company->telefono1) && false === empty($company->telefono2)) {
96
                static::$escpos->text(static::sanitize($company->telefono1 . ' / ' . $company->telefono2) . "\n");
97
            } elseif (false === empty($company->telefono1)) {
98
                static::$escpos->text(static::sanitize($company->telefono1) . "\n");
99
            } elseif (false === empty($company->telefono2)) {
100
                static::$escpos->text(static::sanitize($company->telefono2) . "\n");
101
            }
102
        }
103
104
        // imprimimos el título del documento
105
        static::$escpos->text(static::sanitize($title) . "\n");
106
107
        static::setHeaderTPV($model, $printer);
108
109
        // si es un documento de venta
110
        // imprimimos la fecha y el cliente
111
        if (in_array($model->modelClassName(), ['PresupuestoCliente', 'PedidoCliente', 'AlbaranCliente', 'FacturaCliente'])) {
112
            static::$escpos->text(static::sanitize(static::$i18n->trans('date') . ': ' . $model->fecha . ' ' . $model->hora) . "\n");
113
            static::$escpos->text(static::sanitize(static::$i18n->trans('customer') . ': ' . $model->nombrecliente) . "\n");
114
            if(strlen($model->cifnif) == 9) {
115
                static::$escpos->text(static::sanitize(static::$i18n->trans('title-cifnif-rnc') . ': ' . $model->cifnif) . "\n\n");
116
            } else {
117
                static::$escpos->text(static::sanitize(static::$i18n->trans('title-cifnif-ci') . ': ' . $model->cifnif) . "\n\n");
118
            }
119
120
            if ($model->modelClassName() === 'FacturaCliente') {
121
                if ($model->tipocomprobante !== null && $model->tipocomprobante !== '') {
122
                    static::$escpos->text(static::sanitize(static::$i18n->trans('tipo_comprobante') . ': ' .$model->descripcionTipoComprobante()). "\n");
123
                }
124
125
                if ($model->numeroncf !== null && $model->numeroncf !== '') {
126
                    static::$escpos->text(static::sanitize(static::$i18n->trans('ncf-number') . ': ' . $model->numeroncf). "\n");
127
                }
128
129
                if ($model->ncffechavencimiento!== null && $model->ncffechavencimiento!== '') {
130
                    static::$escpos->text(static::sanitize(static::$i18n->trans('due-date') . ': ' . $model->ncffechavencimiento). "\n\n");
131
                } else {
132
                    static::$escpos->text("\n\n");
133
                }
134
            }
135
        }
136
137
        // añadimos la cabecera
138
        if ($printer->head) {
139
            static::$escpos->setJustification(Printer::JUSTIFY_CENTER);
140
            static::$escpos->text(static::sanitize($printer->head) . "\n\n");
141
            static::$escpos->setJustification();
142
        }
143
144
        $extensionVar->pipe('setHeaderAfter', $model, $printer);
145
    }
146
147
    protected static function getTipoComprobanteRD(string $numero): string
148
    {
149
        switch ($numero) {
150
            case '01':
151
                return static::$i18n->trans('desc-ncf-type-01');
152
153
            case '02':
154
                return static::$i18n->trans('desc-ncf-type-02');
155
156
            case '03':
157
                return static::$i18n->trans('desc-ncf-type-03');
158
159
            case '04':
160
                return static::$i18n->trans('desc-ncf-type-04');
161
162
            case '11':
163
                return static::$i18n->trans('desc-ncf-type-11');
164
165
            case '12':
166
                return static::$i18n->trans('desc-ncf-type-12');
167
168
            case '13':
169
                return static::$i18n->trans('desc-ncf-type-13');
170
171
            case '14':
172
                return static::$i18n->trans('desc-ncf-type-14');
173
174
            case '15':
175
                return static::$i18n->trans('desc-ncf-type-15');
176
177
            case '16':
178
                return static::$i18n->trans('desc-ncf-type-16');
179
180
            case '17':
181
                return static::$i18n->trans('desc-ncf-type-17');
182
183
            default:
184
                return '';
185
        }
186
    }
187
}