Completed
Push — master ( a0ced1...0c034e )
by Roberto
123:33 queued 74:20
created

Dacce::printDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NFePHP\DA\NFe;
4
5
/**
6
 * Esta classe gera a carta de correção em PDF
7
 * NOTA: Esse documento NÃO É NORMALIZADO, nem requerido pela SEFAZ
8
 *
9
 * @category  Library
10
 * @package   nfephp-org/sped-da
11
 * @name      Dacce.php
12
 * @copyright 2009-2016 NFePHP
13
 * @license   http://www.gnu.org/licenses/lgpl.html GNU/LGPL v.3
14
 * @link      http://github.com/nfephp-org/sped-da for the canonical source repository
15
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
16
 */
17
18
use Exception;
19
use NFePHP\DA\Legacy\Dom;
20
use NFePHP\DA\Legacy\Pdf;
21
use NFePHP\DA\Legacy\Common;
22
23
class Dacce extends Common
24
{
25
    public $chNFe;
26
    
27
    protected $logoAlign = 'C';
28
    protected $yDados = 0;
29
    protected $debugMode = 0;
30
    protected $aEnd = array();
31
    protected $pdf;
32
    protected $xml;
33
    protected $logomarca = '';
34
    protected $errMsg = '';
35
    protected $errStatus = false;
36
    protected $orientacao = 'P';
37
    protected $papel = 'A4';
38
    protected $destino = 'I';
39
    protected $pdfDir = '';
40
    protected $fontePadrao = 'Times';
41
    protected $version = '0.1.1';
42
    protected $wPrint;
43
    protected $hPrint;
44
    protected $wCanhoto;
45
    protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####";
46
    protected $id;
47
    protected $tpAmb;
48
    protected $cOrgao;
49
    protected $xCorrecao;
50
    protected $xCondUso;
51
    protected $dhEvento;
52
    protected $cStat;
53
    protected $xMotivo;
54
    protected $CNPJDest = '';
55
    protected $CPFDest = '';
56
    protected $dhRegEvento;
57
    protected $nProt;
58
    
59
    private $dom;
60
    private $procEventoNFe;
61
    private $evento;
62
    private $infEvento;
63
    private $retEvento;
64
    private $retInfEvento;
65
66
    /**
67
     * Construtor recebe parametro pra impressao
68
     * @param string $docXML      conteudo do Arquivo XML
69
     * @param string $sOrientacao (Opcional) Orientação da impressão P-retrato L-Paisagem
70
     * @param string $sPapel      Tamanho do papel (Ex. A4)
71
     * @param string $sPathLogo   Caminho para o arquivo do logo
72
     * @param string $sDestino    Destino do PDF I-browser D-download S-string F-salva
73
     * @param array  $aEnd        array com o endereço do emitente
74
     * @param string $sDirPDF     Caminho para o diretorio de armazenamento dos arquivos PDF
75
     * @param string $fonteDANFE  Nome da fonte alternativa do DAnfe
0 ignored issues
show
Bug introduced by
There is no parameter named $fonteDANFE. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @param number $mododebug   0-Não 1-Sim e 2-nada (2 default)
77
     */
78
    public function __construct(
79
        $docXML = '',
80
        $sOrientacao = '',
81
        $sPapel = '',
82
        $sPathLogo = '',
83
        $sDestino = 'I',
84
        $aEnd = '',
85
        $sDirPDF = '',
86
        $fontePDF = '',
87
        $mododebug = 0
88
    ) {
89
        if (is_numeric($mododebug)) {
90
            $this->debugMode = (int) $mododebug;
91
        }
92
        if ($this->debugMode === 1) {
93
            // ativar modo debug
94
            error_reporting(E_ALL);
95
            ini_set('display_errors', 'On');
96
        } elseif ($this->debugMode === 0) {
97
            // desativar modo debug
98
            error_reporting(0);
99
            ini_set('display_errors', 'Off');
100
        }
101
        if (is_array($aEnd)) {
102
            $this->aEnd = $aEnd;
103
        }
104
        $this->orientacao = $sOrientacao;
105
        $this->papel = $sPapel;
106
        $this->pdf = '';
107
        $this->xml = $docXML;
108
        $this->logomarca = $sPathLogo;
109
        $this->destino = $sDestino;
110
        $this->pdfDir = $sDirPDF;
111
        // verifica se foi passa a fonte a ser usada
112
        if (empty($fontePDF)) {
113
            $this->fontePadrao = 'Times';
114
        } else {
115
            $this->fontePadrao = $fontePDF;
116
        }
117
        // se for passado o xml
118
        if (!empty($this->xml)) {
119
            $this->dom = new Dom();
120
            $this->dom->loadXML($this->xml);
121
            $this->procEventoNFe = $this->dom->getElementsByTagName("procEventoNFe")->item(0);
122
            $this->evento = $this->procEventoNFe->getElementsByTagName("evento")->item(0);
123
            $this->retEvento = $this->procEventoNFe->getElementsByTagName("retEvento")->item(0);
124
            $this->infEvento = $this->evento->getElementsByTagName("infEvento")->item(0);
125
            $this->retInfEvento = $this->retEvento->getElementsByTagName("infEvento")->item(0);
126
            $tpEvento = $this->infEvento->getElementsByTagName("tpEvento")->item(0)->nodeValue;
127
            if ($tpEvento != '110110') {
128
                $this->errMsg = 'Um evento de CC-e deve ser passado.';
129
                $this->errStatus = true;
130
                throw new Exception($this->errMsg);
131
            }
132
            $this->id = str_replace('ID', '', $this->infEvento->getAttribute("Id"));
133
            $this->chNFe = $this->infEvento->getElementsByTagName("chNFe")->item(0)->nodeValue;
134
            $this->tpAmb = $this->infEvento->getElementsByTagName("tpAmb")->item(0)->nodeValue;
135
            $this->cOrgao = $this->infEvento->getElementsByTagName("cOrgao")->item(0)->nodeValue;
136
            $this->xCorrecao = $this->infEvento->getElementsByTagName("xCorrecao")->item(0)->nodeValue;
137
            $this->xCondUso = $this->infEvento->getElementsByTagName("xCondUso")->item(0)->nodeValue;
138
            $this->dhEvento = $this->infEvento->getElementsByTagName("dhEvento")->item(0)->nodeValue;
139
            $this->cStat = $this->retInfEvento->getElementsByTagName("cStat")->item(0)->nodeValue;
140
            $this->xMotivo = $this->retInfEvento->getElementsByTagName("xMotivo")->item(0)->nodeValue;
141
            $this->CNPJDest = !empty($this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue)
142
                ? $this->retInfEvento->getElementsByTagName("CNPJDest")->item(0)->nodeValue
143
                : '';
144
            $this->CPFDest = !empty($this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue)
145
                ? $this->retInfEvento->getElementsByTagName("CPFDest")->item(0)->nodeValue
146
                : '';
147
            $this->dhRegEvento = $this->retInfEvento->getElementsByTagName("dhRegEvento")->item(0)->nodeValue;
148
            $this->nProt = $this->retInfEvento->getElementsByTagName("nProt")->item(0)->nodeValue;
149
        }
150
    }
151
152
    /**
153
     * Monta o pdf
154
     * @param string $orientacao
155
     * @param string $papel
156
     * @param string $logoAlign
157
     */
158
    public function monta($orientacao = 'P', $papel = 'A4', $logoAlign = 'C')
159
    {
160
        $this->orientacao = $orientacao;
161
        $this->papel = $papel;
162
        $this->logoAlign = $logoAlign;
163
        $this->pBuildDACCE();
164
    }
165
166
    /**
167
     * Dados brutos do PDF
168
     * @return string
169
     */
170
    public function render()
171
    {
172
        return $this->printDACCE('', 'S');
173
    }
174
    
175
    /**
176
     * pBuildDACCE
177
     */
178
    private function pBuildDACCE()
179
    {
180
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
181
        if ($this->orientacao == 'P') {
182
            // margens do PDF
183
            $margSup = 2;
184
            $margEsq = 2;
185
            $margDir = 2;
186
            // posição inicial do relatorio
187
            $xInic = 1;
188
            $yInic = 1;
189
            if ($this->papel == 'A4') { // A4 210x297mm
190
                $maxW = 210;
191
                $maxH = 297;
192
            }
193
        } else {
194
            // margens do PDF
195
            $margSup = 3;
196
            $margEsq = 3;
197
            $margDir = 3;
198
            // posição inicial do relatorio
199
            $xInic = 5;
200
            $yInic = 5;
201
            if ($papel == 'A4') { // A4 210x297mm
0 ignored issues
show
Bug introduced by
The variable $papel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
202
                $maxH = 210;
203
                $maxW = 297;
204
            }
205
        } // orientação
206
207
        // largura imprimivel em mm
208
        $this->wPrint = $maxW - ($margEsq + $xInic);
0 ignored issues
show
Bug introduced by
The variable $maxW does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
209
        // comprimento imprimivel em mm
210
        $this->hPrint = $maxH - ($margSup + $yInic);
0 ignored issues
show
Bug introduced by
The variable $maxH does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
211
        // estabelece contagem de paginas
212
        $this->pdf->aliasNbPages();
213
        // fixa as margens
214
        $this->pdf->setMargins($margEsq, $margSup, $margDir);
215
        $this->pdf->setDrawColor(0, 0, 0);
216
        $this->pdf->setFillColor(255, 255, 255);
217
        // inicia o documento
218
        $this->pdf->open();
219
        // adiciona a primeira página
220
        $this->pdf->addPage($this->orientacao, $this->papel);
221
        $this->pdf->setLineWidth(0.1);
222
        $this->pdf->setTextColor(0, 0, 0);
223
        // montagem da página
224
        $pag = 1;
225
        $x = $xInic;
226
        $y = $yInic;
227
        // coloca o cabeçalho
228
        $y = $this->pHeader($x, $y, $pag);
229
        // coloca os dados da CCe
230
        $y = $this->pBody($x, $y + 15);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->pBody($x, $y + 15) (which targets NFePHP\DA\NFe\Dacce::pBody()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
231
        // coloca os dados da CCe
232
        $y = $this->pFooter($x, $y + $this->hPrint - 20);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $y is correct as $this->pFooter($x, $y + $this->hPrint - 20) (which targets NFePHP\DA\NFe\Dacce::pFooter()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$y is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
233
    }
234
235
    /**
236
     * Monta o cabeçalho
237
     * @param  number $x
238
     * @param  number $y
239
     * @param  number $pag
240
     * @return number
241
     */
242
    private function pHeader($x, $y, $pag)
0 ignored issues
show
Unused Code introduced by
The parameter $pag is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
243
    {
244
        $oldX = $x;
245
        $oldY = $y;
246
        $maxW = $this->wPrint;
247
        // ####################################################################################
248
        // coluna esquerda identificação do emitente
249
        $w = round($maxW * 0.41, 0); // 80;
250
        if ($this->orientacao == 'P') {
251
            $aFont = array(
252
                'font' => $this->fontePadrao,
253
                'size' => 6,
254
                'style' => 'I'
255
            );
256
        } else {
257
            $aFont = array(
258
                'font' => $this->fontePadrao,
259
                'size' => 8,
260
                'style' => 'B'
261
            );
262
        }
263
        $w1 = $w;
0 ignored issues
show
Unused Code introduced by
$w1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
264
        $h = 32;
265
        $oldY += $h;
266
        $this->pTextBox($x, $y, $w, $h);
267
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
268
        $this->pTextBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
269
        if (!empty($this->logomarca)) {
270
            $logoInfo = getimagesize($this->logomarca);
271
            $type = strtolower(explode('/', $logoInfo['mime'])[1]);
272
            if ($type == 'png') {
273
                $this->logomarca = $this->imagePNGtoJPG($this->logomarca);
274
                $type == 'jpg';
275
            }
276
            // largura da imagem em mm
277
            $logoWmm = ($logoInfo[0] / 72) * 25.4;
278
            // altura da imagem em mm
279
            $logoHmm = ($logoInfo[1] / 72) * 25.4;
280
            if ($this->logoAlign == 'L') {
281
                $nImgW = round($w / 3, 0);
282
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
283
                $xImg = $x + 1;
284
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
285
                // estabelecer posições do texto
286
                $x1 = round($xImg + $nImgW + 1, 0);
287
                $y1 = round($h / 3 + $y, 0);
288
                $tw = round(2 * $w / 3, 0);
289
            }
290
            if ($this->logoAlign == 'C') {
291
                $nImgH = round($h / 3, 0);
292
                $nImgW = round($logoWmm * ($nImgH / $logoHmm), 0);
293
                $xImg = round(($w - $nImgW) / 2 + $x, 0);
294
                $yImg = $y + 3;
295
                $x1 = $x;
296
                $y1 = round($yImg + $nImgH + 1, 0);
297
                $tw = $w;
298
            }
299
            if ($this->logoAlign == 'R') {
300
                $nImgW = round($w / 3, 0);
301
                $nImgH = round($logoHmm * ($nImgW / $logoWmm), 0);
302
                $xImg = round($x + ($w - (1 + $nImgW)), 0);
303
                $yImg = round(($h - $nImgH) / 2, 0) + $y;
304
                $x1 = $x;
305
                $y1 = round($h / 3 + $y, 0);
306
                $tw = round(2 * $w / 3, 0);
307
            }
308
            $type = (substr($this->logomarca, 0, 7) === 'data://') ? 'jpg' : null;
309
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH, $type);
0 ignored issues
show
Bug introduced by
The variable $xImg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $yImg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $nImgW does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $nImgH does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
310
        } else {
311
            $x1 = $x;
312
            $y1 = round($h / 3 + $y, 0);
313
            $tw = $w;
314
        }
315
        // Nome emitente
316
        $aFont = ['font' => $this->fontePadrao,'size' => 12,'style' => 'B'];
317
        $texto = $this->aEnd['razao'];
318
        $this->pTextBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
0 ignored issues
show
Bug introduced by
The variable $x1 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $y1 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $tw does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
319
        // endereço
320
        $y1 = $y1 + 6;
321
        $aFont = ['font' => $this->fontePadrao,'size' => 8,'style' => ''];
322
        $lgr = $this->aEnd['logradouro'];
323
        $nro = $this->aEnd['numero'];
324
        $cpl = $this->aEnd['complemento'];
325
        $bairro = $this->aEnd['bairro'];
326
        $CEP = $this->aEnd['CEP'];
327
        $CEP = $this->pFormat($CEP, "#####-###");
328
        $mun = $this->aEnd['municipio'];
329
        $UF = $this->aEnd['UF'];
330
        $fone = $this->aEnd['telefone'];
331
        $email = $this->aEnd['email'];
332
        $foneLen = strlen($fone);
333
        if ($foneLen > 0) {
334
            $fone2 = substr($fone, 0, $foneLen - 4);
335
            $fone1 = substr($fone, 0, $foneLen - 8);
336
            $fone = '(' . $fone1 . ') ' . substr($fone2, - 4) . '-' . substr($fone, - 4);
337
        } else {
338
            $fone = '';
339
        }
340
        if ($email != '') {
341
            $email = 'Email: ' . $email;
342
        }
343
        $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - " . $CEP . "\n"
344
            . $mun . " - " . $UF . " " . $fone . "\n" . $email;
345
        $this->pTextBox($x1, $y1 - 2, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
346
        // ##################################################
347
        $w2 = round($maxW - $w, 0);
348
        $x += $w;
349
        $this->pTextBox($x, $y, $w2, $h);
350
        $y1 = $y + $h;
351
        $aFont = ['font' => $this->fontePadrao,'size' => 16,'style' => 'B'];
352
        $this->pTextBox($x, $y + 2, $w2, 8, 'Representação Gráfica de CC-e', $aFont, 'T', 'C', 0, '');
353
        $aFont = array(
354
            'font' => $this->fontePadrao,
355
            'size' => 12,
356
            'style' => 'I'
357
        );
358
        $this->pTextBox($x, $y + 7, $w2, 8, '(Carta de Correção Eletrônica)', $aFont, 'T', 'C', 0, '');
359
        $texto = 'ID do Evento: ' . $this->id;
360
        $aFont = ['font' => $this->fontePadrao,'size' => 10,'style' => ''];
361
        $this->pTextBox($x, $y + 15, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
362
        $tsHora = $this->pConvertTime($this->dhEvento);
363
        $texto = 'Criado em : ' . date('d/m/Y   H:i:s', $tsHora);
364
        $this->pTextBox($x, $y + 20, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
365
        $tsHora = $this->pConvertTime($this->dhRegEvento);
366
        $texto = 'Prococolo: ' . $this->nProt . '  -  Registrado na SEFAZ em: ' . date('d/m/Y   H:i:s', $tsHora);
367
        $this->pTextBox($x, $y + 25, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
368
        // ####################################################
369
        $x = $oldX;
370
        $this->pTextBox($x, $y1, $maxW, 40);
371
        $sY = $y1 + 40;
372
        $texto = 'De acordo com as determinações legais vigentes, vimos por meio desta comunicar-lhe'.
373
            ' que a Nota Fiscal, abaixo referenciada, contêm irregularidades que estão destacadas e' .
374
            ' suas respectivas correções, solicitamos que sejam aplicadas essas correções ao executar'.
375
            ' seus lançamentos fiscais.';
376
        $aFont = array(
377
            'font' => $this->fontePadrao,
378
            'size' => 10,
379
            'style' => ''
380
        );
381
        $this->pTextBox($x + 5, $y1, $maxW - 5, 20, $texto, $aFont, 'T', 'L', 0, '', false);
382
        // ############################################
383
        $x = $oldX;
384
        $y = $y1;
385
        $texto = '';
386
        if ($this->CNPJDest != '') {
387
            $texto = 'CNPJ do Destinatário: ' . $this->pFormat($this->CNPJDest, "##.###.###/####-##");
388
        }
389
        if ($this->CPFDest != '') {
390
            $texto = 'CPF do Destinatário: ' . $this->pFormat($this->CPFDest, "###.###.###-##");
391
        }
392
        $aFont = array(
393
            'font' => $this->fontePadrao,
394
            'size' => 12,
395
            'style' => 'B'
396
        );
397
        $this->pTextBox($x + 2, $y + 13, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
398
        $numNF = substr($this->chNFe, 25, 9);
399
        $serie = substr($this->chNFe, 22, 3);
400
        $numNF = $this->pFormat($numNF, "###.###.###");
401
        $texto = "Nota Fiscal: " . $numNF . '  -   Série: ' . $serie;
402
        $this->pTextBox($x + 2, $y + 19, $w2, 8, $texto, $aFont, 'T', 'L', 0, '');
403
        $bW = 87;
404
        $bH = 15;
405
        $x = 55;
406
        $y = $y1 + 13;
407
        $w = $maxW;
408
        $this->pdf->setFillColor(0, 0, 0);
409
        $this->pdf->code128($x + (($w - $bW) / 2), $y + 2, $this->chNFe, $bW, $bH);
410
        $this->pdf->setFillColor(255, 255, 255);
411
        $y1 = $y + 2 + $bH;
412
        $aFont = array(
413
            'font' => $this->fontePadrao,
414
            'size' => 10,
415
            'style' => ''
416
        );
417
        $texto = $this->pFormat($this->chNFe, $this->formatoChave);
418
        $this->pTextBox($x, $y1, $w - 2, $h, $texto, $aFont, 'T', 'C', 0, '');
419
        $x = $oldX;
420
        $this->pTextBox($x, $sY, $maxW, 15);
421
        $texto = $this->xCondUso;
422
        $aFont = array(
423
            'font' => $this->fontePadrao,
424
            'size' => 8,
425
            'style' => 'I'
426
        );
427
        $this->pTextBox($x + 2, $sY + 2, $maxW - 2, 15, $texto, $aFont, 'T', 'L', 0, '', false);
428
        return $sY + 2;
429
    }
430
431
    /**
432
     * Monta o corpo da pagina
433
     * @param number $x
434
     * @param number $y
435
     */
436
    private function pBody($x, $y)
437
    {
438
        $maxW = $this->wPrint;
439
        $texto = 'CORREÇÕES A SEREM CONSIDERADAS';
440
        $aFont = array(
441
            'font' => $this->fontePadrao,
442
            'size' => 10,
443
            'style' => 'B'
444
        );
445
        $this->pTextBox($x, $y, $maxW, 5, $texto, $aFont, 'T', 'L', 0, '', false);
446
        $y += 5;
447
        $this->pTextBox($x, $y, $maxW, 190);
448
        $texto = str_replace(";", PHP_EOL, $this->xCorrecao);
449
        $aFont = array(
450
            'font' => $this->fontePadrao,
451
            'size' => 12,
452
            'style' => 'B'
453
        );
454
        $this->pTextBox($x + 2, $y + 2, $maxW - 2, 150, $texto, $aFont, 'T', 'L', 0, '', false);
455
        if ($this->tpAmb != 1) {
456
            $x = 10;
457
            if ($this->orientacao == 'P') {
458
                $y = round($this->hPrint * 2 / 3, 0);
459
            } else {
460
                $y = round($this->hPrint / 2, 0);
461
            }
462
            $h = 5;
463
            $w = $maxW - (2 * $x);
464
            $this->pdf->SetTextColor(90, 90, 90);
465
            $texto = "SEM VALOR FISCAL";
466
            $aFont = array(
467
                'font' => $this->fontePadrao,
468
                'size' => 48,
469
                'style' => 'B'
470
            );
471
            $this->pTextBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
472
            $aFont = array(
473
                'font' => $this->fontePadrao,
474
                'size' => 30,
475
                'style' => 'B'
476
            );
477
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
478
            $this->pTextBox($x, $y + 14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
479
            $this->pdf->SetTextColor(0, 0, 0);
480
        }
481
    }
482
483
    /**
484
     * Monta o rodapé
485
     * @param number $x
486
     * @param number $y
487
     */
488
    protected function pFooter($x, $y)
489
    {
490
        $w = $this->wPrint;
491
        $texto = "Este documento é uma representação gráfica da CC-e e foi impresso apenas para sua"
492
            . " informação e não possue validade fiscal.\n A CC-e deve ser recebida e mantida em"
493
            . " arquivo eletrônico XML e pode ser consultada através dos Portais das SEFAZ.";
494
        $aFont = array(
495
            'font' => $this->fontePadrao,
496
            'size' => 10,
497
            'style' => 'I'
498
        );
499
        $this->pTextBox($x, $y, $w, 20, $texto, $aFont, 'T', 'C', 0, '', false);
500
        $y = $this->hPrint - 4;
501
        $texto = "Impresso em  " . date('d/m/Y   H:i:s');
502
        $w = $this->wPrint - 4;
503
        $aFont = array(
504
            'font' => $this->fontePadrao,
505
            'size' => 6,
506
            'style' => 'I'
507
        );
508
        $this->pTextBox($x, $y, $w, 4, $texto, $aFont, 'T', 'L', 0, '');
509
        $texto = "Dacce ver. " . $this->version . "  Powered by NFePHP (GNU/GPLv3 GNU/LGPLv3) © www.nfephp.org";
510
        $aFont = array(
511
            'font' => $this->fontePadrao,
512
            'size' => 6,
513
            'style' => 'I'
514
        );
515
        $this->pTextBox($x, $y, $w, 4, $texto, $aFont, 'T', 'R', 0, 'http://www.nfephp.org');
516
    }
517
518
    /**
519
     * Gera a saida
520
     * @param  string $nome
521
     * @param  string $destino
522
     * @param  string $printer
523
     * @return mixed
524
     */
525
    public function printDocument($nome = '', $destino = 'I', $printer = '')
526
    {
527
        return $this->printDACCE($nome, $destino, $printer);
528
    }
529
530
    /**
531
     * Gera a saida
532
     * @param  string $nome
533
     * @param  string $destino
534
     * @param  string $printer
535
     * @return mixed
536
     */
537
    public function printDACCE($nome = '', $destino = 'I', $printer = '')
0 ignored issues
show
Unused Code introduced by
The parameter $printer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
538
    {
539
        if ($this->pdf == null) {
540
            $this->pBuildDACCE();
541
        }
542
        return $this->pdf->Output($nome, $destino);
543
    }
544
    
545
    private function imagePNGtoJPG($original)
546
    {
547
        $image = imagecreatefrompng($original);
548
        ob_start();
549
        imagejpeg($image, null, 100);
550
        imagedestroy($image);
551
        $stringdata = ob_get_contents(); // read from buffer
552
        ob_end_clean();
553
        return 'data://text/plain;base64,'.base64_encode($stringdata);
554
    }
555
}
556