Completed
Push — master ( 0b1d68...e7164a )
by Roberto
12:40 queued 09:51
created

src/NFe/Danfe.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NFePHP\DA\NFe;
4
5
use InvalidArgumentException;
6
use NFePHP\DA\Legacy\Dom;
7
use NFePHP\DA\Legacy\Pdf;
8
use NFePHP\DA\Legacy\Common;
9
10
class Danfe extends Common
11
{
12
13
    /**
14
     * alinhamento padrão do logo (C-Center)
15
     *
16
     * @var string
17
     */
18
    protected $logoAlign = 'C';
19
    /**
20
     * Posição
21
     * @var float
22
     */
23
    protected $yDados = 0;
24
    /**
25
     * Numero DPEC
26
     *
27
     * @var string
28
     */
29
    protected $numero_registro_dpec = '';
30
     /**
31
     * Parâmetro para exibir ou ocultar os valores do PIS/COFINS.
32
     * @var boolean
33
     */
34
    protected $exibirPIS = true;
35
    /**
36
     * Parâmetro para exibir ou ocultar os valores do ICMS Interestadual e Valor Total dos Impostos.
37
     * @var boolean
38
     */
39
    protected $exibirIcmsInterestadual = true;
40
    /**
41
     * Parâmetro para exibir ou ocultar o texto sobre valor aproximado dos tributos.
42
     * @var boolean
43
     */
44
    protected $exibirValorTributos = true;
45
    /**
46
     * Parâmetro para exibir ou ocultar o texto adicional sobre a forma de pagamento
47
     * e as informações de fatura/duplicata.
48
     * @var boolean
49
     */
50
    protected $exibirTextoFatura = false;
51
    /**
52
     * Parâmetro do controle se deve concatenar automaticamente informações complementares
53
     * na descrição do produto, como por exemplo, informações sobre impostos.
54
     * @var boolean
55
     */
56
    protected $descProdInfoComplemento = true;
57
    /**
58
     * Parâmetro do controle se deve gerar quebras de linha com "\n" a partir de ";" na descrição do produto.
59
     * @var boolean
60
     */
61
    protected $descProdQuebraLinha = true;
62
    /**
63
     * objeto fpdf()
64
     * @var object
65
     */
66
    protected $pdf;
67
    /**
68
     * XML NFe
69
     * @var string
70
     */
71
    protected $xml;
72
    /**
73
     * path para logomarca em jpg
74
     * @var string
75
     */
76
    protected $logomarca = '';
77
    /**
78
     * mesagens de erro
79
     * @var string
80
     */
81
    protected $errMsg = '';
82
    /**
83
     * status de erro true um erro ocorreu false sem erros
84
     * @var boolean
85
     */
86
    protected $errStatus = false;
87
    /**
88
     * orientação da DANFE
89
     * P-Retrato ou L-Paisagem
90
     * @var string
91
     */
92
    protected $orientacao = 'P';
93
    /**
94
     * formato do papel
95
     * @var string
96
     */
97
    protected $papel = 'A4';
98
    /**
99
     * Nome da Fonte para gerar o DANFE
100
     * @var string
101
     */
102
    protected $fontePadrao = 'Times';
103
    /**
104
     * Texto
105
     * @var string
106
     */
107
    protected $textoAdic = '';
108
    /**
109
     * Largura
110
     * @var float
111
     */
112
    protected $wAdic = 0;
113
    /**
114
     * largura imprimivel, em milímetros
115
     * @var float
116
     */
117
    protected $wPrint;
118
    /**
119
     * Comprimento (altura) imprimivel, em milímetros
120
     * @var float
121
     */
122
    protected $hPrint;
123
    protected $maxH;
124
    protected $maxW;
125
    /**
126
     * largura do canhoto (25mm) apenas para a formatação paisagem
127
     * @var float
128
     */
129
    protected $wCanhoto = 25;
130
    /**
131
     * Formato chave
132
     * @var string
133
     */
134
    protected $formatoChave = "#### #### #### #### #### #### #### #### #### #### ####";
135
    /**
136
     * quantidade de itens já processados na montagem do DANFE
137
     * @var integer
138
     */
139
    protected $qtdeItensProc;
140
    /**
141
     * Document
142
     * @var DOMDocument
143
     */
144
    protected $dom;
145
    /**
146
     * Node
147
     * @var DOMNode
148
     */
149
    protected $infNFe;
150
    /**
151
     * Node
152
     * @var DOMNode
153
     */
154
    protected $ide;
155
    /**
156
     * Node
157
     * @var DOMNode
158
     */
159
    protected $entrega;
160
    /**
161
     * Node
162
     * @var DOMNode
163
     */
164
    protected $retirada;
165
    /**
166
     * Node
167
     * @var DOMNode
168
     */
169
    protected $emit;
170
    /**
171
     * Node
172
     * @var DOMNode
173
     */
174
    protected $dest;
175
    /**
176
     * Node
177
     * @var DOMNode
178
     */
179
    protected $enderEmit;
180
    /**
181
     * Node
182
     * @var DOMNode
183
     */
184
    protected $enderDest;
185
    /**
186
     * Node
187
     * @var DOMNode
188
     */
189
    protected $det;
190
    /**
191
     * Node
192
     * @var DOMNode
193
     */
194
    protected $cobr;
195
    /**
196
     * Node
197
     * @var DOMNode
198
     */
199
    protected $dup;
200
    /**
201
     * Node
202
     * @var DOMNode
203
     */
204
    protected $ICMSTot;
205
    /**
206
     * Node
207
     * @var DOMNode
208
     */
209
    protected $ISSQNtot;
210
    /**
211
     * Node
212
     * @var DOMNode
213
     */
214
    protected $transp;
215
    /**
216
     * Node
217
     * @var DOMNode
218
     */
219
    protected $transporta;
220
    /**
221
     * Node
222
     * @var DOMNode
223
     */
224
    protected $veicTransp;
225
    /**
226
     * Node reboque
227
     * @var DOMNode
228
     */
229
    protected $reboque;
230
    /**
231
     * Node infAdic
232
     * @var DOMNode
233
     */
234
    protected $infAdic;
235
    /**
236
     * Tipo de emissão
237
     * @var integer
238
     */
239
    protected $tpEmis;
240
    /**
241
     * Node infProt
242
     * @var DOMNode
243
     */
244
    protected $infProt;
245
    /**
246
     * 1-Retrato/ 2-Paisagem
247
     * @var integer
248
     */
249
    protected $tpImp;
250
    /**
251
     * Node compra
252
     * @var DOMNode
253
     */
254
    protected $compra;
255
    /**
256
     * ativa ou desativa o modo de debug
257
     * @var integer
258
     */
259
    protected $debugmode = false;
260
    /**
261
     * Creditos para integrador
262
     * @var string
263
     */
264
    protected $creditos = '';
265
266
    /**
267
     * __construct
268
     *
269
     * @name  __construct
270
     * @param string  $docXML      Conteúdo XML da NF-e (com ou sem a tag nfeProc)
271
     * @param string  $logoimage   Caminho para o arquivo do logo
272
     */
273
    public function __construct(
274
        $xml
275
    ) {
276
        $this->debugMode();
277
        $this->loadDoc($xml);
278
    }
279
    
280
    /**
281
     * Ativa ou desativa o modo debug
282
     * @param bool $activate
283
     * @return bool
284
     */
285
    public function debugMode($activate = null)
286
    {
287
        if (isset($activate) && is_bool($activate)) {
288
            $this->debugmode = $activate;
289
        }
290
        if ($this->debugmode) {
291
            //ativar modo debug
292
            error_reporting(E_ALL);
293
            ini_set('display_errors', 'On');
294
        } else {
295
            //desativar modo debug
296
            error_reporting(0);
297
            ini_set('display_errors', 'Off');
298
        }
299
        return $this->debugmode;
300
    }
301
302
    /**
303
     * Add the credits to the integrator in the footer message
304
     * @param string $message
305
     */
306
    public function creditsIntegratorFooter($message = '')
307
    {
308
        $this->creditos = trim($message);
309
    }
310
    
311
    /**
312
     * Dados brutos do PDF
313
     * @return string
314
     */
315
    public function render()
316
    {
317
        if (empty($this->pdf)) {
318
            $this->monta();
319
        }
320
        return $this->pdf->getPdf();
321
    }
322
323
    /**
324
     * monta
325
     * Monta a DANFE conforme as informações fornecidas para a classe durante sua
326
     * construção. Constroi DANFEs com até 3 páginas podendo conter até 56 itens.
327
     * A definição de margens e posições iniciais para a impressão são estabelecidas
328
     * pelo conteúdo da funçao e podem ser modificados.
329
     *
330
     * @param  string $orientacao (Opcional) Estabelece a orientação da impressão
331
     *  (ex. P-retrato), se nada for fornecido será usado o padrão da NFe
332
     * @param  string $papel      (Opcional) Estabelece o tamanho do papel (ex. A4)
333
     * @return string O ID da NFe numero de 44 digitos extraido do arquivo XML
334
     */
335
    public function monta(
336
        $logo = '',
337
        $orientacao = '',
338
        $papel = 'A4',
339
        $logoAlign = 'C',
340
        $depecNumReg = '',
341
        $margSup = 2,
342
        $margEsq = 2,
343
        $margInf = 2
344
    ) {
345
        $this->pdf = '';
346
        $this->logomarca = $logo;
347
        //se a orientação estiver em branco utilizar o padrão estabelecido na NF
348
        if ($orientacao == '') {
349
            if ($this->tpImp == '1') {
350
                $orientacao = 'P';
351
            } else {
352
                $orientacao = 'L';
353
            }
354
        }
355
        $this->orientacao = $orientacao;
356
        $this->papel = $papel;
357
        $this->logoAlign = $logoAlign;
358
        $this->numero_registro_dpec = $depecNumReg;
359
        //instancia a classe pdf
360
        $this->pdf = new Pdf($this->orientacao, 'mm', $this->papel);
361
        //margens do PDF, em milímetros. Obs.: a margem direita é sempre igual à
362
        //margem esquerda. A margem inferior *não* existe na FPDF, é definida aqui
363
        //apenas para controle se necessário ser maior do que a margem superior
364
        // posição inicial do conteúdo, a partir do canto superior esquerdo da página
365
        $xInic = $margEsq;
366
        $yInic = $margSup;
367
        if ($this->orientacao == 'P') {
368
            if ($papel == 'A4') {
369
                $this->maxW = 210;
370
                $this->maxH = 297;
371
            }
372
        } else {
373
            if ($papel == 'A4') {
374
                $this->maxH = 210;
375
                $this->maxW = 297;
376
                //se paisagem multiplica a largura do canhoto pela quantidade de canhotos
377
                $this->wCanhoto *= $this->qCanhoto;
378
            }
379
        }
380
        //total inicial de paginas
381
        $totPag = 1;
382
        //largura imprimivel em mm: largura da folha menos as margens esq/direita
383
        $this->wPrint = $this->maxW-($margEsq * 2);
384
        //comprimento (altura) imprimivel em mm: altura da folha menos as margens
385
        //superior e inferior
386
        $this->hPrint = $this->maxH-$margSup-$margInf;
387
        // estabelece contagem de paginas
388
        $this->pdf->aliasNbPages();
389
        // fixa as margens
390
        $this->pdf->setMargins($margEsq, $margSup);
391
        $this->pdf->setDrawColor(0, 0, 0);
392
        $this->pdf->setFillColor(255, 255, 255);
393
        // inicia o documento
394
        $this->pdf->open();
395
        // adiciona a primeira página
396
        $this->pdf->addPage($this->orientacao, $this->papel);
397
        $this->pdf->setLineWidth(0.1);
398
        $this->pdf->setTextColor(0, 0, 0);
399
400
        //##################################################################
401
        // CALCULO DO NUMERO DE PAGINAS A SEREM IMPRESSAS
402
        //##################################################################
403
        //Verificando quantas linhas serão usadas para impressão das duplicatas
404
        $linhasDup = 0;
405
        if (isset($this->dup) && $this->dup->length > 0) {
406
            $qtdPag = $this->dup->length;
407
        } elseif (isset($this->detPag) && $this->detPag->length > 0) {
408
           $qtdPag = $this->detPag->length;
409
        }
410
        if (($qtdPag > 0) && ($qtdPag <= 7)) {
411
            $linhasDup = 1;
412
        } elseif (($qtdPag > 7) && ($qtdPag <= 14)) {
413
            $linhasDup = 2;
414
        } elseif (($qtdPag > 14) && ($qtdPag <= 21)) {
415
            $linhasDup = 3;
416
        } elseif ($qtdPag > 21) {
417
            // chinnonsantos 11/05/2016: Limite máximo de impressão de duplicatas na NFe,
418
            // só vai ser exibito as 21 primeiras duplicatas (parcelas de pagamento),
419
            // se não oculpa espaço d+, cada linha comporta até 7 duplicatas.
420
            $linhasDup = 3;
421
        }
422
        //verifica se será impressa a linha dos serviços ISSQN
423
        $linhaISSQN = 0;
424
        if ((isset($this->ISSQNtot)) && ($this->getTagValue($this->ISSQNtot, 'vServ') > 0)) {
425
            $linhaISSQN = 1;
426
        }
427
        //calcular a altura necessária para os dados adicionais
428
        if ($this->orientacao == 'P') {
429
            $this->wAdic = round($this->wPrint*0.66, 0);
430
        } else {
431
            $this->wAdic = round(($this->wPrint-$this->wCanhoto)*0.5, 0);
432
        }
433
        $fontProduto = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
434
        $this->textoAdic = '';
435
        if (isset($this->retirada)) {
436
            $txRetCNPJ = $this->getTagValue($this->retirada, "CNPJ");
437
            $txRetxLgr = $this->getTagValue($this->retirada, "xLgr");
438
            $txRetnro = $this->getTagValue($this->retirada, "nro");
439
            $txRetxCpl = $this->getTagValue($this->retirada, "xCpl", " - ");
440
            $txRetxBairro = $this->getTagValue($this->retirada, "xBairro");
441
            $txRetxMun = $this->getTagValue($this->retirada, "xMun");
442
            $txRetUF = $this->getTagValue($this->retirada, "UF");
443
            $this->textoAdic .= "LOCAL DE RETIRADA : ".
444
                    $txRetCNPJ.
445
                    '-' .
446
                    $txRetxLgr .
447
                    ', ' .
448
                    $txRetnro .
449
                    ' ' .
450
                    $txRetxCpl .
451
                    ' - ' .
452
                    $txRetxBairro .
453
                    ' ' .
454
                    $txRetxMun .
455
                    ' - ' .
456
                    $txRetUF .
457
                    "\r\n";
458
        }
459
        //dados do local de entrega da mercadoria
460
        if (isset($this->entrega)) {
461
            $txRetCNPJ = $this->getTagValue($this->entrega, "CNPJ");
462
            $txRetxLgr = $this->getTagValue($this->entrega, "xLgr");
463
            $txRetnro = $this->getTagValue($this->entrega, "nro");
464
            $txRetxCpl = $this->getTagValue($this->entrega, "xCpl", " - ");
465
            $txRetxBairro = $this->getTagValue($this->entrega, "xBairro");
466
            $txRetxMun = $this->getTagValue($this->entrega, "xMun");
467
            $txRetUF = $this->getTagValue($this->entrega, "UF");
468
            if ($this->textoAdic != '') {
469
                $this->textoAdic .= ". \r\n";
470
            }
471
            $this->textoAdic .= "LOCAL DE ENTREGA : ".$txRetCNPJ.'-'.$txRetxLgr.', '.$txRetnro.' '.$txRetxCpl.
472
               ' - '.$txRetxBairro.' '.$txRetxMun.' - '.$txRetUF."\r\n";
473
        }
474
        //informações adicionais
475
        $this->textoAdic .= $this->geraInformacoesDasNotasReferenciadas();
476
        if (isset($this->infAdic)) {
477
            $i = 0;
478
            if ($this->textoAdic != '') {
479
                $this->textoAdic .= ". \r\n";
480
            }
481
            $this->textoAdic .= ! empty($this->getTagValue($this->infAdic, "infCpl")) ?
482
                'Inf. Contribuinte: ' .
483
                $this->anfaveaDANFE($this->getTagValue($this->infAdic, "infCpl")) : '';
484
            $infPedido = $this->geraInformacoesDaTagCompra();
485
            if ($infPedido != "") {
486
                $this->textoAdic .= $infPedido;
487
            }
488
            $this->textoAdic .= $this->getTagValue($this->dest, "email", ' Email do Destinatário: ');
489
            $this->textoAdic .= ! empty($this->getTagValue($this->infAdic, "infAdFisco")) ?
490
                "\r\n Inf. fisco: " .
491
                $this->getTagValue($this->infAdic, "infAdFisco") : '';
492
            $obsCont = $this->infAdic->getElementsByTagName("obsCont");
493
            if (isset($obsCont)) {
494
                foreach ($obsCont as $obs) {
495
                    $campo =  $obsCont->item($i)->getAttribute("xCampo");
496
                    $xTexto = ! empty($obsCont->item($i)->getElementsByTagName("xTexto")->item(0)->nodeValue) ?
497
                        $obsCont->item($i)->getElementsByTagName("xTexto")->item(0)->nodeValue : '';
498
                    $this->textoAdic .= "\r\n" . $campo . ':  ' . trim($xTexto);
499
                    $i++;
500
                }
501
            }
502
        }
503
        //INCLUSO pela NT 2013.003 Lei da Transparência
504
        //verificar se a informação sobre o valor aproximado dos tributos
505
        //já se encontra no campo de informações adicionais
506
        if ($this->exibirValorTributos) {
507
            $flagVTT = strpos(strtolower(trim($this->textoAdic)), 'valor');
508
            $flagVTT = $flagVTT || strpos(strtolower(trim($this->textoAdic)), 'vl');
509
            $flagVTT = $flagVTT && strpos(strtolower(trim($this->textoAdic)), 'aprox');
510
            $flagVTT = $flagVTT && (strpos(strtolower(trim($this->textoAdic)), 'trib') ||
511
                    strpos(strtolower(trim($this->textoAdic)), 'imp'));
512
            $vTotTrib = $this->getTagValue($this->ICMSTot, 'vTotTrib');
513
            if ($vTotTrib != '' && !$flagVTT) {
514
                $this->textoAdic .= "\n Valor Aproximado dos Tributos : R$ " . number_format($vTotTrib, 2, ",", ".");
515
            }
516
        }
517
        //fim da alteração NT 2013.003 Lei da Transparência
518
        $this->textoAdic = str_replace(";", "\n", $this->textoAdic);
519
        $alinhas = explode("\n", $this->textoAdic);
520
        $numlinhasdados = 0;
521
        foreach ($alinhas as $linha) {
522
            $numlinhasdados += $this->pdf->getNumLines($linha, $this->wAdic, $fontProduto);
523
        }
524
        $hdadosadic = round(($numlinhasdados+3) * $this->pdf->fontSize, 0);
525
        if ($hdadosadic < 10) {
526
            $hdadosadic = 10;
527
        }
528
        //altura disponivel para os campos da DANFE
529
        $hcabecalho = 47;//para cabeçalho
530
        $hdestinatario = 25;//para destinatario
531
        $hduplicatas = 12;//para cada grupo de 7 duplicatas
532
        $himposto = 18;// para imposto
533
        $htransporte = 25;// para transporte
534
        $hissqn = 11;// para issqn
535
        $hfooter = 5;// para rodape
536
        $hCabecItens = 4;//cabeçalho dos itens
537
        //alturas disponiveis para os dados
538
        $hDispo1 = $this->hPrint - 10 - ($hcabecalho +
539
            $hdestinatario + ($linhasDup * $hduplicatas) + $himposto + $htransporte +
540
            ($linhaISSQN * $hissqn) + $hdadosadic + $hfooter + $hCabecItens +
541
            $this->sizeExtraTextoFatura());
542
        if ($this->orientacao == 'P') {
543
            $hDispo1 -= 24 * $this->qCanhoto;//para canhoto
544
            $w = $this->wPrint;
545
        } else {
546
            $hcanhoto = $this->hPrint;//para canhoto
547
            $w = $this->wPrint - $this->wCanhoto;
548
        }
549
        $hDispo2 = $this->hPrint - 10 - ($hcabecalho + $hfooter + $hCabecItens)-4;
550
        //Contagem da altura ocupada para impressão dos itens
551
        $fontProduto = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
552
        $numlinhas = 0;
553
        $hUsado = $hCabecItens;
554
        $w2 = round($w*0.28, 0);
555
        $hDispo = $hDispo1;
556
        $totPag = 1;
557
        $i = 0;
558
        while ($i < $this->det->length) {
559
            $texto = $this->descricaoProduto($this->det->item($i));
560
            $numlinhas = $this->pdf->getNumLines($texto, $w2, $fontProduto);
561
            $hUsado += round(($numlinhas * $this->pdf->fontSize) + ($numlinhas * 0.5), 2);
562
            if ($hUsado > $hDispo) {
563
                $totPag++;
564
                $hDispo = $hDispo2;
565
                $hUsado = $hCabecItens;
566
                // Remove canhoto para páginas secundárias em modo paisagem ('L')
567
                $w2 = round($this->wPrint*0.28, 0);
568
                $i--; // decrementa para readicionar o item que não coube nessa pagina na outra.
569
            }
570
            $i++;
571
        } //fim da soma das areas de itens usadas
572
        $qtdeItens = $i; //controle da quantidade de itens no DANFE
573
        //montagem da primeira página
574
        $pag = 1;
575
        $x = $xInic;
576
        $y = $yInic;
577
        //coloca o(s) canhoto(s) da NFe
578
        if ($this->orientacao == 'P') {
579
            for ($i = 1; $i <= $this->qCanhoto; $i++) {
580
                $y = $this->canhoto($x, $y);
581
            }
582
        } else {
583
            for ($i = 1; $i <= $this->qCanhoto; $i++) {
584
                $this->canhoto($x, $y);
585
                $x = 25 * $i;
586
            }
587
        }
588
        //coloca o cabeçalho
589
        $y = $this->header($x, $y, $pag, $totPag);
590
        //coloca os dados do destinatário
591
        $y = $this->destinatarioDANFE($x, $y+1);
592
        
593
        
594
        //Verifica as formas de pagamento da nota fiscal
595
        $formaPag = [];
596
        if (isset($this->detPag) && $this->detPag->length > 0) {
597
            foreach ($this->detPag as $k => $d) {
598
                $fPag = !empty($this->detPag->item($k)->getElementsByTagName('tPag')->item(0)->nodeValue) ?
599
                    $this->detPag->item($k)->getElementsByTagName('tPag')->item(0)->nodeValue : '0';
600
                $formaPag[$fPag] = $fPag;
601
            }
602
        }
603
        //caso tenha boleto imprimir fatura
604
        if ($this->dup->length > 0) {
605
            $y = $this->fatura($x, $y+1);
606
        } else {
607
            //Se somente tiver a forma de pagamento sem pagamento ou outros não imprimir nada
608
            if (count($formaPag)=='1' && (isset($formaPag[90]) || isset($formaPag[99]))) {
609
                $y = $y;
610
            } else {
611
                //caso tenha mais de uma forma de pagamento ou seja diferente de boleto exibe a
612
                //forma de pagamento e o valor
613
                $y = $this->pagamento($x, $y+1);
614
            }
615
        }
616
        //coloca os dados dos impostos e totais da NFe
617
        $y = $this->imposto($x, $y+1);
618
        //coloca os dados do trasnporte
619
        $y = $this->transporte($x, $y+1);
620
        //itens da DANFE
621
        $nInicial = 0;
622
623
        $y = $this->itens($x, $y+1, $nInicial, $hDispo1, $pag, $totPag, $hCabecItens);
624
625
        //coloca os dados do ISSQN
626
        if ($linhaISSQN == 1) {
627
            $y = $this->issqn($x, $y+4);
628
        } else {
629
            $y += 4;
630
        }
631
        //coloca os dados adicionais da NFe
632
        $y = $this->dadosAdicionais($x, $y, $hdadosadic);
633
        //coloca o rodapé da página
634
        if ($this->orientacao == 'P') {
635
            $this->rodape($xInic);
636
        } else {
637
            $this->rodape($xInic);
638
        }
639
640
        //loop para páginas seguintes
641
        for ($n = 2; $n <= $totPag; $n++) {
642
            // fixa as margens
643
            $this->pdf->setMargins($margEsq, $margSup);
644
            //adiciona nova página
645
            $this->pdf->addPage($this->orientacao, $this->papel);
646
            //ajusta espessura das linhas
647
            $this->pdf->setLineWidth(0.1);
648
            //seta a cor do texto para petro
649
            $this->pdf->setTextColor(0, 0, 0);
650
            // posição inicial do relatorio
651
            $x = $xInic;
652
            $y = $yInic;
653
            //coloca o cabeçalho na página adicional
654
            $y = $this->header($x, $y, $n, $totPag);
655
            //coloca os itens na página adicional
656
            $y = $this->itens($x, $y+1, $nInicial, $hDispo2, $n, $totPag, $hCabecItens);
657
            //coloca o rodapé da página
658
            if ($this->orientacao == 'P') {
659
                $this->rodape($xInic);
660
            } else {
661
                $this->rodape($xInic);
662
            }
663
            //se estiver na última página e ainda restar itens para inserir, adiciona mais uma página
664
            if ($n == $totPag && $this->qtdeItensProc < $qtdeItens) {
665
                $totPag++;
666
            }
667
        }
668
    }
669
670
    /**
671
     * anfavea
672
     * Função para transformar o campo cdata do padrão ANFAVEA para
673
     * texto imprimível
674
     *
675
     * @param  string $cdata campo CDATA
676
     * @return string conteúdo do campo CDATA como string
677
     */
678
    protected function anfaveaDANFE($cdata = '')
679
    {
680
        if ($cdata == '') {
681
            return '';
682
        }
683
        //remove qualquer texto antes ou depois da tag CDATA
684
        $cdata = str_replace('<![CDATA[', '<CDATA>', $cdata);
685
        $cdata = str_replace(']]>', '</CDATA>', $cdata);
686
        $cdata = preg_replace('/\s\s+/', ' ', $cdata);
687
        $cdata = str_replace("> <", "><", $cdata);
688
        $len = strlen($cdata);
689
        $startPos = strpos($cdata, '<');
690
        if ($startPos === false) {
691
            return $cdata;
692
        }
693
        for ($x=$len; $x>0; $x--) {
694
            if (substr($cdata, $x, 1) == '>') {
695
                $endPos = $x;
696
                break;
697
            }
698
        }
699
        if ($startPos > 0) {
700
            $parte1 = substr($cdata, 0, $startPos);
701
        } else {
702
            $parte1 = '';
703
        }
704
        $parte2 = substr($cdata, $startPos, $endPos-$startPos+1);
705
        if ($endPos < $len) {
706
            $parte3 = substr($cdata, $endPos + 1, $len - $endPos - 1);
707
        } else {
708
            $parte3 = '';
709
        }
710
        $texto = trim($parte1).' '.trim($parte3);
711
        if (strpos($parte2, '<CDATA>') === false) {
712
            $cdata = '<CDATA>'.$parte2.'</CDATA>';
713
        } else {
714
            $cdata = $parte2;
715
        }
716
        //Retira a tag <FONTE IBPT> (caso existir) pois não é uma estrutura válida XML
717
        $cdata = str_replace('<FONTE IBPT>', '', $cdata);
718
        //carrega o xml CDATA em um objeto DOM
719
        $dom = new Dom();
720
        $dom->loadXML($cdata, LIBXML_NOBLANKS | LIBXML_NOEMPTYTAG);
721
        //$xml = $dom->saveXML();
722
        //grupo CDATA infADprod
723
        $id = $dom->getElementsByTagName('id')->item(0);
724
        $div = $dom->getElementsByTagName('div')->item(0);
725
        $entg = $dom->getElementsByTagName('entg')->item(0);
726
        $dest = $dom->getElementsByTagName('dest')->item(0);
727
        $ctl = $dom->getElementsByTagName('ctl')->item(0);
728
        $ref = $dom->getElementsByTagName('ref')->item(0);
729
        if (isset($id)) {
730
            if ($id->hasAttributes()) {
731
                foreach ($id->attributes as $attr) {
732
                    $name = $attr->nodeName;
733
                    $value = $attr->nodeValue;
734
                    $texto .= " $name : $value";
735
                }
736
            }
737
        }
738
        if (isset($div)) {
739
            if ($div->hasAttributes()) {
740
                foreach ($div->attributes as $attr) {
741
                    $name = $attr->nodeName;
742
                    $value = $attr->nodeValue;
743
                    $texto .= " $name : $value";
744
                }
745
            }
746
        }
747
        if (isset($entg)) {
748
            if ($entg->hasAttributes()) {
749
                foreach ($entg->attributes as $attr) {
750
                    $name = $attr->nodeName;
751
                    $value = $attr->nodeValue;
752
                    $texto .= " $name : $value";
753
                }
754
            }
755
        }
756
        if (isset($dest)) {
757
            if ($dest->hasAttributes()) {
758
                foreach ($dest->attributes as $attr) {
759
                    $name = $attr->nodeName;
760
                    $value = $attr->nodeValue;
761
                    $texto .= " $name : $value";
762
                }
763
            }
764
        }
765
        if (isset($ctl)) {
766
            if ($ctl->hasAttributes()) {
767
                foreach ($ctl->attributes as $attr) {
768
                    $name = $attr->nodeName;
769
                    $value = $attr->nodeValue;
770
                    $texto .= " $name : $value";
771
                }
772
            }
773
        }
774
        if (isset($ref)) {
775
            if ($ref->hasAttributes()) {
776
                foreach ($ref->attributes as $attr) {
777
                    $name = $attr->nodeName;
778
                    $value = $attr->nodeValue;
779
                    $texto .= " $name : $value";
780
                }
781
            }
782
        }
783
        //grupo CADATA infCpl
784
        $t = $dom->getElementsByTagName('transmissor')->item(0);
785
        $r = $dom->getElementsByTagName('receptor')->item(0);
786
        $versao = ! empty($dom->getElementsByTagName('versao')->item(0)->nodeValue) ?
787
            'Versao:'.$dom->getElementsByTagName('versao')->item(0)->nodeValue.' ' : '';
788
        $especieNF = ! empty($dom->getElementsByTagName('especieNF')->item(0)->nodeValue) ?
789
            'Especie:'.$dom->getElementsByTagName('especieNF')->item(0)->nodeValue.' ' : '';
790
        $fabEntrega = ! empty($dom->getElementsByTagName('fabEntrega')->item(0)->nodeValue) ?
791
            'Entrega:'.$dom->getElementsByTagName('fabEntrega')->item(0)->nodeValue.' ' : '';
792
        $dca = ! empty($dom->getElementsByTagName('dca')->item(0)->nodeValue) ?
793
            'dca:'.$dom->getElementsByTagName('dca')->item(0)->nodeValue.' ' : '';
794
        $texto .= "".$versao.$especieNF.$fabEntrega.$dca;
795
        if (isset($t)) {
796
            if ($t->hasAttributes()) {
797
                $texto .= " Transmissor ";
798
                foreach ($t->attributes as $attr) {
799
                    $name = $attr->nodeName;
800
                    $value = $attr->nodeValue;
801
                    $texto .= " $name : $value";
802
                }
803
            }
804
        }
805
        if (isset($r)) {
806
            if ($r->hasAttributes()) {
807
                $texto .= " Receptor ";
808
                foreach ($r->attributes as $attr) {
809
                    $name = $attr->nodeName;
810
                    $value = $attr->nodeValue;
811
                    $texto .= " $name : $value";
812
                }
813
            }
814
        }
815
        return $texto;
816
    }
817
818
819
    /**
820
     * Verifica o status da NFe
821
     *
822
     * @return array
823
     */
824
    protected function statusNFe()
825
    {
826
        if (!isset($this->nfeProc)) {
827
            return ['status' => false, 'message' => 'NFe NÃO PROTOCOLADA'];
828
        }
829
        if ($this->getTagValue($this->ide, "tpAmb") == '2') {
830
            return ['status' => false, 'message' => 'NFe EMITIDA EM HOMOLOGAÇÃO'];
831
        }
832
        $cStat = $this->getTagValue($this->nfeProc, "cStat");
833
        if ($cStat == '101'
834
            || $cStat == '151'
835
            || $cStat == '135'
836
            || $cStat == '155'
837
        ) {
838
            return ['status' => false, 'message' => 'NFe CANCELADA'];
839
        }
840
        
841
        if ($cStat == '110' ||
842
               $cStat == '301' ||
843
               $cStat == '302'
844
               
845
        ) {
846
            return ['status' => false, 'message' => 'NFe DENEGADA'];
847
        }
848
        return ['status' => true, 'message' => ''];
849
    }
850
851
    protected function notaDPEC()
852
    {
853
        return $this->numero_registro_dpec != '';
854
    }
855
856
857
858
    /**
859
     *header
860
     * Monta o cabelhalho da DANFE (retrato e paisagem)
861
     *
862
     * @param  number $x      Posição horizontal inicial, canto esquerdo
863
     * @param  number $y      Posição vertical inicial, canto superior
864
     * @param  number $pag    Número da Página
865
     * @param  number $totPag Total de páginas
866
     * @return number Posição vertical final
867
     */
868
    protected function header($x = 0, $y = 0, $pag = '1', $totPag = '1')
869
    {
870
        $oldX = $x;
871
        $oldY = $y;
872
        if ($this->orientacao == 'P') {
873
              $maxW = $this->wPrint;
874
        } else {
875
            if ($pag == 1) { // primeira página
876
                $maxW = $this->wPrint - $this->wCanhoto;
877
            } else { // páginas seguintes
878
                $maxW = $this->wPrint;
879
            }
880
        }
881
        //####################################################################################
882
        //coluna esquerda identificação do emitente
883
        $w = round($maxW*0.41, 0);
884
        if ($this->orientacao == 'P') {
885
            $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>'I'];
886
        } else {
887
            $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>'B'];
888
        }
889
        $w1 = $w;
890
        $h=32;
891
        $oldY += $h;
892
        $this->pdf->textBox($x, $y, $w, $h);
893
        $texto = 'IDENTIFICAÇÃO DO EMITENTE';
894
        $this->pdf->textBox($x, $y, $w, 5, $texto, $aFont, 'T', 'C', 0, '');
895
        //estabelecer o alinhamento
896
        //pode ser left L, center C, right R, full logo L
897
        //se for left separar 1/3 da largura para o tamanho da imagem
898
        //os outros 2/3 serão usados para os dados do emitente
899
        //se for center separar 1/2 da altura para o logo e 1/2 para os dados
900
        //se for right separa 2/3 para os dados e o terço seguinte para o logo
901
        //se não houver logo centraliza dos dados do emitente
902
        // coloca o logo
903
        if (!empty($this->logomarca)) {
904
            $logoInfo = getimagesize($this->logomarca);
905
            $type = strtolower(explode('/', $logoInfo['mime'])[1]);
906
            if ($type == 'png') {
907
                $this->logomarca = $this->imagePNGtoJPG($this->logomarca);
908
                $type == 'jpg';
909
            }
910
            //largura da imagem em mm
911
            $logoWmm = ($logoInfo[0]/72)*25.4;
912
            //altura da imagem em mm
913
            $logoHmm = ($logoInfo[1]/72)*25.4;
914
            if ($this->logoAlign=='L') {
915
                $nImgW = round($w/3, 0);
916
                $nImgH = round($logoHmm * ($nImgW/$logoWmm), 0);
917
                $xImg = $x+1;
918
                $yImg = round(($h-$nImgH)/2, 0)+$y;
919
                //estabelecer posições do texto
920
                $x1 = round($xImg + $nImgW +1, 0);
921
                $y1 = round($h/3+$y, 0);
922
                $tw = round(2*$w/3, 0);
923
            } elseif ($this->logoAlign=='C') {
924
                $nImgH = round($h/3, 0);
925
                $nImgW = round($logoWmm * ($nImgH/$logoHmm), 0);
926
                $xImg = round(($w-$nImgW)/2+$x, 0);
927
                $yImg = $y+3;
928
                $x1 = $x;
929
                $y1 = round($yImg + $nImgH + 1, 0);
930
                $tw = $w;
931
            } elseif ($this->logoAlign=='R') {
932
                $nImgW = round($w/3, 0);
933
                $nImgH = round($logoHmm * ($nImgW/$logoWmm), 0);
934
                $xImg = round($x+($w-(1+$nImgW)), 0);
935
                $yImg = round(($h-$nImgH)/2, 0)+$y;
936
                $x1 = $x;
937
                $y1 = round($h/3+$y, 0);
938
                $tw = round(2*$w/3, 0);
939
            } elseif ($this->logoAlign=='F') {
940
                $nImgH = round($h-5, 0);
941
                $nImgW = round($logoWmm * ($nImgH/$logoHmm), 0);
942
                $xImg = round(($w-$nImgW)/2+$x, 0);
943
                $yImg = $y+3;
944
                $x1 = $x;
945
                $y1 = round($yImg + $nImgH + 1, 0);
946
                $tw = $w;
947
            }
948
            $type = (substr($this->logomarca, 0, 7) === 'data://') ? 'jpg' : null;
949
            $this->pdf->Image($this->logomarca, $xImg, $yImg, $nImgW, $nImgH, $type);
950
        } else {
951
            $x1 = $x;
952
            $y1 = round($h/3+$y, 0);
953
            $tw = $w;
954
        }
955
        // monta as informações apenas se diferente de full logo
956
        if ($this->logoAlign !== 'F') {
957
            //Nome emitente
958
            $aFont = ['font'=>$this->fontePadrao, 'size'=>12, 'style'=>'B'];
959
            $texto = $this->emit->getElementsByTagName("xNome")->item(0)->nodeValue;
960
            $this->pdf->textBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
961
            //endereço
962
            $y1 = $y1+5;
963
            $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>''];
964
            $fone = ! empty($this->enderEmit->getElementsByTagName("fone")->item(0)->nodeValue)
965
                 ? $this->enderEmit->getElementsByTagName("fone")->item(0)->nodeValue
966
                 : '';
967
            $lgr = $this->getTagValue($this->enderEmit, "xLgr");
968
            $nro = $this->getTagValue($this->enderEmit, "nro");
969
            $cpl = $this->getTagValue($this->enderEmit, "xCpl", " - ");
970
            $bairro = $this->getTagValue($this->enderEmit, "xBairro");
971
            $CEP = $this->getTagValue($this->enderEmit, "CEP");
972
            $CEP = $this->formatField($CEP, "#####-###");
973
            $mun = $this->getTagValue($this->enderEmit, "xMun");
974
            $UF = $this->getTagValue($this->enderEmit, "UF");
975
            $texto = $lgr . ", " . $nro . $cpl . "\n" . $bairro . " - "
976
                    . $CEP . "\n" . $mun . " - " . $UF . " "
977
                    . "Fone/Fax: " . $fone;
978
            $this->pdf->textBox($x1, $y1, $tw, 8, $texto, $aFont, 'T', 'C', 0, '');
979
        }
980
981
        //####################################################################################
982
        //coluna central Danfe
983
        $x += $w;
984
        $w=round($maxW * 0.17, 0);//35;
985
        $w2 = $w;
986
        $h = 32;
987
        $this->pdf->textBox($x, $y, $w, $h);
988
  
989
        $texto = "DANFE";
990
        $aFont = ['font'=>$this->fontePadrao, 'size'=>14, 'style'=>'B'];
991
        $this->pdf->textBox($x, $y+1, $w, $h, $texto, $aFont, 'T', 'C', 0, '');
992
        $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>''];
993
        $texto = 'Documento Auxiliar da Nota Fiscal Eletrônica';
994
        $h = 20;
995
        $this->pdf->textBox($x, $y+6, $w, $h, $texto, $aFont, 'T', 'C', 0, '', false);
996
        
997
998
        $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>''];
999
        $texto = '0 - ENTRADA';
1000
        $y1 = $y + 14;
1001
        $h = 8;
1002
        $this->pdf->textBox($x+2, $y1, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1003
        $texto = '1 - SAÍDA';
1004
        $y1 = $y + 17;
1005
        $this->pdf->textBox($x+2, $y1, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1006
        //tipo de nF
1007
        $aFont = ['font'=>$this->fontePadrao, 'size'=>12, 'style'=>'B'];
1008
        $y1 = $y + 13;
1009
        $h = 7;
1010
        $texto = $this->ide->getElementsByTagName('tpNF')->item(0)->nodeValue;
1011
        $this->pdf->textBox($x+27, $y1, 5, $h, $texto, $aFont, 'C', 'C', 1, '');
1012
        //numero da NF
1013
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1014
        $y1 = $y + 20;
1015
        $numNF = str_pad($this->ide->getElementsByTagName('nNF')->item(0)->nodeValue, 9, "0", STR_PAD_LEFT);
1016
        $numNF = $this->formatField($numNF, "###.###.###");
1017
        $texto = "Nº. " . $numNF;
1018
        $this->pdf->textBox($x, $y1, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1019
        //Série
1020
        $y1 = $y + 23;
1021
        $serie = str_pad($this->ide->getElementsByTagName('serie')->item(0)->nodeValue, 3, "0", STR_PAD_LEFT);
1022
        $texto = "Série " . $serie;
1023
        $this->pdf->textBox($x, $y1, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1024
        //numero paginas
1025
        $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>'I'];
1026
        $y1 = $y + 26;
1027
        $texto = "Folha " . $pag . "/" . $totPag;
1028
        $this->pdf->textBox($x, $y1, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1029
1030
        //####################################################################################
1031
        //coluna codigo de barras
1032
        $x += $w;
1033
        $w = ($maxW-$w1-$w2);//85;
1034
        $w3 = $w;
1035
        $h = 32;
1036
        $this->pdf->textBox($x, $y, $w, $h);
1037
        $this->pdf->setFillColor(0, 0, 0);
1038
        $chave_acesso = str_replace('NFe', '', $this->infNFe->getAttribute("Id"));
1039
        $bW = 75;
1040
        $bH = 12;
1041
        //codigo de barras
1042
        $this->pdf->code128($x+(($w-$bW)/2), $y+2, $chave_acesso, $bW, $bH);
1043
        //linhas divisorias
1044
        $this->pdf->line($x, $y+4+$bH, $x+$w, $y+4+$bH);
1045
        $this->pdf->line($x, $y+12+$bH, $x+$w, $y+12+$bH);
1046
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1047
        $y1 = $y+4+$bH;
1048
        $h = 7;
1049
        $texto = 'CHAVE DE ACESSO';
1050
        $this->pdf->textBox($x, $y1, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1051
        $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>'B'];
1052
        $y1 = $y+8+$bH;
1053
        $texto = $this->formatField($chave_acesso, $this->formatoChave);
1054
        $this->pdf->textBox($x+2, $y1, $w-2, $h, $texto, $aFont, 'T', 'C', 0, '');
1055
        $y1 = $y+12+$bH;
1056
        $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>''];
1057
        $chaveContingencia="";
1058
        if ($this->notaDpec()) {
1059
            $cabecalhoProtoAutorizacao = 'NÚMERO DE REGISTRO DPEC';
1060
        } else {
1061
            $cabecalhoProtoAutorizacao = 'PROTOCOLO DE AUTORIZAÇÃO DE USO';
1062
        }
1063
        if (($this->tpEmis == 2 || $this->tpEmis == 5) && !$this->notaDpec()) {
1064
            $cabecalhoProtoAutorizacao = "DADOS DA NF-E";
1065
            $chaveContingencia = $this->geraChaveAdicionalDeContingencia();
1066
            $this->pdf->setFillColor(0, 0, 0);
1067
            //codigo de barras
1068
            $this->pdf->code128($x+11, $y1+1, $chaveContingencia, $bW*.9, $bH/2);
1069
        } else {
1070
            $texto = 'Consulta de autenticidade no portal nacional da NF-e';
1071
            $this->pdf->textBox($x+2, $y1, $w-2, $h, $texto, $aFont, 'T', 'C', 0, '');
1072
            $y1 = $y+16+$bH;
1073
            $texto = 'www.nfe.fazenda.gov.br/portal ou no site da Sefaz Autorizadora';
1074
            $this->pdf->textBox(
1075
                $x+2,
1076
                $y1,
1077
                $w-2,
1078
                $h,
1079
                $texto,
1080
                $aFont,
1081
                'T',
1082
                'C',
1083
                0,
1084
                'http://www.nfe.fazenda.gov.br/portal ou no site da Sefaz Autorizadora'
1085
            );
1086
        }
1087
1088
        //####################################################################################
1089
        //Dados da NF do cabeçalho
1090
        //natureza da operação
1091
        $texto = 'NATUREZA DA OPERAÇÃO';
1092
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1093
        $w = $w1+$w2;
1094
        $y = $oldY;
1095
        $oldY += $h;
1096
        $x = $oldX;
1097
        $h = 7;
1098
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1099
        $texto = $this->ide->getElementsByTagName("natOp")->item(0)->nodeValue;
1100
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1101
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1102
        $x += $w;
1103
        $w = $w3;
1104
        //PROTOCOLO DE AUTORIZAÇÃO DE USO ou DADOS da NF-E
1105
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1106
        $this->pdf->textBox($x, $y, $w, $h, $cabecalhoProtoAutorizacao, $aFont, 'T', 'L', 1, '');
1107
        // algumas NFe podem estar sem o protocolo de uso portanto sua existencia deve ser
1108
        // testada antes de tentar obter a informação.
1109
        // NOTA : DANFE sem protocolo deve existir somente no caso de contingência !!!
1110
        // Além disso, existem várias NFes em contingência que eu recebo com protocolo de autorização.
1111
        // Na minha opinião, deveríamos mostra-lo, mas o  manual  da NFe v4.01 diz outra coisa...
1112
        if (($this->tpEmis == 2 || $this->tpEmis == 5) && !$this->notaDpec()) {
1113
            $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>'B'];
1114
            $texto = $this->formatField($chaveContingencia, "#### #### #### #### #### #### #### #### ####");
1115
            $cStat = '';
1116
        } else {
1117
            $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1118
            if ($this->notaDpec()) {
1119
                $texto = $this->numero_registro_dpec;
1120
                $cStat = '';
1121
            } else {
1122
                if (isset($this->nfeProc)) {
1123
                    $texto = ! empty($this->nfeProc->getElementsByTagName("nProt")->item(0)->nodeValue) ?
1124
                            $this->nfeProc->getElementsByTagName("nProt")->item(0)->nodeValue : '';
1125
                    $tsHora = $this->convertTime($this->nfeProc->getElementsByTagName("dhRecbto")->item(0)->nodeValue);
1126
                    if ($texto != '') {
1127
                        $texto .= "  -  " . date('d/m/Y H:i:s', $tsHora);
1128
                    }
1129
                    $cStat = $this->nfeProc->getElementsByTagName("cStat")->item(0)->nodeValue;
1130
                } else {
1131
                    $texto = '';
1132
                    $cStat = '';
1133
                }
1134
            }
1135
        }
1136
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1137
        //####################################################################################
1138
        //INSCRIÇÃO ESTADUAL
1139
        $w = round($maxW * 0.333, 0);
1140
        $y += $h;
1141
        $oldY += $h;
1142
        $x = $oldX;
1143
        $texto = 'INSCRIÇÃO ESTADUAL';
1144
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1145
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1146
        $texto = $this->getTagValue($this->emit, "IE");
1147
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1148
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1149
        //INSCRIÇÃO ESTADUAL DO SUBST. TRIBUT.
1150
        $x += $w;
1151
        $texto = 'INSCRIÇÃO ESTADUAL DO SUBST. TRIBUT.';
1152
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1153
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1154
        $texto = ! empty($this->emit->getElementsByTagName("IEST")->item(0)->nodeValue)
1155
             ? $this->emit->getElementsByTagName("IEST")->item(0)->nodeValue
1156
             : '';
1157
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1158
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1159
        //CNPJ
1160
        $x += $w;
1161
        $w = ($maxW-(2*$w));
1162
        $texto = 'CNPJ / CPF';
1163
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1164
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1165
        //Pegando valor do CPF/CNPJ
1166
        if (! empty($this->emit->getElementsByTagName("CNPJ")->item(0)->nodeValue)) {
1167
            $texto = $this->formatField(
1168
                $this->emit->getElementsByTagName("CNPJ")->item(0)->nodeValue,
1169
                "###.###.###/####-##"
1170
            );
1171
        } else {
1172
            $texto = ! empty($this->emit->getElementsByTagName("CPF")->item(0)->nodeValue) ?
1173
                $this->formatField(
1174
                    $this->emit->getElementsByTagName("CPF")->item(0)->nodeValue,
1175
                    "###.###.###-##"
1176
                ) : '';
1177
        }
1178
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1179
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1180
1181
        //####################################################################################
1182
        //Indicação de NF Homologação, cancelamento e falta de protocolo
1183
        $tpAmb = $this->ide->getElementsByTagName('tpAmb')->item(0)->nodeValue;
1184
        //indicar cancelamento
1185
        $resp = $this->statusNFe();
1186
        if (!$resp['status']) {
1187
            $x = 10;
1188
            $y = $this->hPrint-130;
1189
            $h = 25;
1190
            $w = $maxW-(2*$x);
1191
            $this->pdf->setTextColor(90, 90, 90);
1192
            $texto = $resp['message'];
1193
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1194
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1195
            $y += $h;
1196
            $h = 5;
1197
            $w = $maxW-(2*$x);
1198
            if (isset($this->infProt) && $resp['status']) {
1199
                $xMotivo = $this->infProt->getElementsByTagName("xMotivo")->item(0)->nodeValue;
1200
            } else {
1201
                $xMotivo = '';
1202
            }
1203
            $texto = "SEM VALOR FISCAL\n".$xMotivo;
1204
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1205
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1206
            $this->pdf->setTextColor(0, 0, 0);
1207
        }
1208
        
1209
        /*
1210
        if ($this->pNotaCancelada()) {
1211
            //101 Cancelamento
1212
            $x = 10;
1213
            $y = $this->hPrint-130;
1214
            $h = 25;
1215
            $w = $maxW-(2*$x);
1216
            $this->pdf->SetTextColor(90, 90, 90);
1217
            $texto = "NFe CANCELADA";
1218
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1219
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1220
            $this->pdf->SetTextColor(0, 0, 0);
1221
        }*/
1222
1223
        if ($this->notaDpec() || $this->tpEmis == 4) {
1224
            //DPEC
1225
            $x = 10;
1226
            $y = $this->hPrint-130;
1227
            $h = 25;
1228
            $w = $maxW-(2*$x);
1229
            $this->pdf->SetTextColor(200, 200, 200);
1230
            $texto = "DANFE impresso em contingência -\n".
1231
                     "DPEC regularmente recebido pela Receita\n".
1232
                     "Federal do Brasil";
1233
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1234
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1235
            $this->pdf->SetTextColor(0, 0, 0);
1236
        }
1237
        /*
1238
        if ($this->pNotaDenegada()) {
1239
            //110 301 302 Denegada
1240
            $x = 10;
1241
            $y = $this->hPrint-130;
1242
            $h = 25;
1243
            $w = $maxW-(2*$x);
1244
            $this->pdf->SetTextColor(90, 90, 90);
1245
            $texto = "NFe USO DENEGADO";
1246
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1247
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1248
            $y += $h;
1249
            $h = 5;
1250
            $w = $maxW-(2*$x);
1251
            if (isset($this->infProt)) {
1252
                $xMotivo = $this->infProt->getElementsByTagName("xMotivo")->item(0)->nodeValue;
1253
            } else {
1254
                $xMotivo = '';
1255
            }
1256
            $texto = "SEM VALOR FISCAL\n".$xMotivo;
1257
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1258
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1259
            $this->pdf->SetTextColor(0, 0, 0);
1260
        }
1261
         *
1262
         */
1263
        //indicar sem valor
1264
        /*
1265
        if ($tpAmb != 1) {
1266
            $x = 10;
1267
            if ($this->orientacao == 'P') {
1268
                $y = round($this->hPrint*2/3, 0);
1269
            } else {
1270
                $y = round($this->hPrint/2, 0);
1271
            }
1272
            $h = 5;
1273
            $w = $maxW-(2*$x);
1274
            $this->pdf->SetTextColor(90, 90, 90);
1275
            $texto = "SEM VALOR FISCAL";
1276
            $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1277
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1278
            $aFont = ['font'=>$this->fontePadrao, 'size'=>30, 'style'=>'B'];
1279
            $texto = "AMBIENTE DE HOMOLOGAÇÃO";
1280
            $this->pdf->textBox($x, $y+14, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1281
            $this->pdf->SetTextColor(0, 0, 0);
1282
        } else {
1283
            $x = 10;
1284
            if ($this->orientacao == 'P') {
1285
                $y = round($this->hPrint*2/3, 0);
1286
            } else {
1287
                $y = round($this->hPrint/2, 0);
1288
            }//fim orientacao
1289
            $h = 5;
1290
            $w = $maxW-(2*$x);
1291
            $this->pdf->SetTextColor(90, 90, 90);
1292
            //indicar FALTA DO PROTOCOLO se NFe não for em contingência
1293
            if (($this->tpEmis == 2 || $this->tpEmis == 5) && !$this->notaDpec()) {
1294
                //Contingência
1295
                $texto = "DANFE Emitido em Contingência";
1296
                $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1297
                $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1298
                $aFont = ['font'=>$this->fontePadrao, 'size'=>30, 'style'=>'B'];
1299
                $texto = "devido à problemas técnicos";
1300
                $this->pdf->textBox($x, $y+12, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1301
            } else {
1302
                if (!isset($this->nfeProc)) {
1303
                    if (!$this->notaDpec()) {
1304
                        $texto = "SEM VALOR FISCAL";
1305
                        $aFont = ['font'=>$this->fontePadrao, 'size'=>48, 'style'=>'B'];
1306
                        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1307
                    }
1308
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>30, 'style'=>'B'];
1309
                    $texto = "FALTA PROTOCOLO DE APROVAÇÃO DA SEFAZ";
1310
                    if (!$this->notaDpec()) {
1311
                        $this->pdf->textBox($x, $y+12, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1312
                    } else {
1313
                        $this->pdf->textBox($x, $y+25, $w, $h, $texto, $aFont, 'C', 'C', 0, '');
1314
                    }
1315
                }//fim nefProc
1316
            }//fim tpEmis
1317
            $this->pdf->SetTextColor(0, 0, 0);
1318
        }
1319
         *
1320
         */
1321
        return $oldY;
1322
    } //fim header
1323
1324
    /**
1325
     * destinatarioDANFE
1326
     * Monta o campo com os dados do destinatário na DANFE. (retrato e paisagem)
1327
     *
1328
     * @name   destinatarioDANFE
1329
     * @param  number $x Posição horizontal canto esquerdo
1330
     * @param  number $y Posição vertical canto superior
1331
     * @return number Posição vertical final
1332
     */
1333
    protected function destinatarioDANFE($x = 0, $y = 0)
1334
    {
1335
        //####################################################################################
1336
        //DESTINATÁRIO / REMETENTE
1337
        $oldX = $x;
1338
        $oldY = $y;
0 ignored issues
show
$oldY 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...
1339
        if ($this->orientacao == 'P') {
1340
            $maxW = $this->wPrint;
1341
        } else {
1342
            $maxW = $this->wPrint - $this->wCanhoto;
1343
        }
1344
        $w = $maxW;
1345
        $h = 7;
1346
        $texto = 'DESTINATÁRIO / REMETENTE';
1347
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1348
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1349
        //NOME / RAZÃO SOCIAL
1350
        $w = round($maxW*0.61, 0);
1351
        $w1 = $w;
1352
        $y += 3;
1353
        $texto = 'NOME / RAZÃO SOCIAL';
1354
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1355
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1356
        $texto = $this->dest->getElementsByTagName("xNome")->item(0)->nodeValue;
1357
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1358
        if ($this->orientacao == 'P') {
1359
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'L', 0, '');
1360
        } else {
1361
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'L', 1, '');
1362
        }
1363
        //CNPJ / CPF
1364
        $x += $w;
1365
        $w = round($maxW*0.23, 0);
1366
        $w2 = $w;
1367
        $texto = 'CNPJ / CPF';
1368
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1369
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1370
        //Pegando valor do CPF/CNPJ
1371
        if (! empty($this->dest->getElementsByTagName("CNPJ")->item(0)->nodeValue)) {
1372
            $texto = $this->formatField(
1373
                $this->dest->getElementsByTagName("CNPJ")->item(0)->nodeValue,
1374
                "###.###.###/####-##"
1375
            );
1376
        } else {
1377
            $texto = ! empty($this->dest->getElementsByTagName("CPF")->item(0)->nodeValue) ?
1378
                    $this->formatField(
1379
                        $this->dest->getElementsByTagName("CPF")->item(0)->nodeValue,
1380
                        "###.###.###-##"
1381
                    ) : '';
1382
        }
1383
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1384
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1385
        //DATA DA EMISSÃO
1386
        $x += $w;
1387
        $w = $maxW-($w1+$w2);
1388
        $wx = $w;
1389
        $texto = 'DATA DA EMISSÃO';
1390
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1391
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1392
        $dEmi = ! empty($this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue) ?
1393
                $this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue : '';
1394
        if ($dEmi == '') {
1395
            $dEmi = ! empty($this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue) ?
1396
                    $this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue : '';
1397
            $aDemi = explode('T', $dEmi);
1398
            $dEmi = $aDemi[0];
1399
        }
1400
        $texto = $this->ymdTodmy($dEmi);
1401
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1402
        if ($this->orientacao == 'P') {
1403
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1404
        } else {
1405
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 1, '');
1406
        }
1407
        //ENDEREÇO
1408
        $w = round($maxW*0.47, 0);
1409
        $w1 = $w;
1410
        $y += $h;
1411
        $x = $oldX;
1412
        $texto = 'ENDEREÇO';
1413
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1414
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1415
        $texto = $this->dest->getElementsByTagName("xLgr")->item(0)->nodeValue;
1416
        $texto .= ', ' . $this->dest->getElementsByTagName("nro")->item(0)->nodeValue;
1417
        $texto .= $this->getTagValue($this->dest, "xCpl", " - ");
1418
1419
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1420
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'L', 0, '', true);
1421
        //BAIRRO / DISTRITO
1422
        $x += $w;
1423
        $w = round($maxW*0.21, 0);
1424
        $w2 = $w;
1425
        $texto = 'BAIRRO / DISTRITO';
1426
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1427
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1428
        $texto = $this->dest->getElementsByTagName("xBairro")->item(0)->nodeValue;
1429
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1430
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1431
        //CEP
1432
        $x += $w;
1433
        $w = $maxW-$w1-$w2-$wx;
1434
        $w2 = $w;
1435
        $texto = 'CEP';
1436
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1437
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1438
        $texto = ! empty($this->dest->getElementsByTagName("CEP")->item(0)->nodeValue) ?
1439
                $this->dest->getElementsByTagName("CEP")->item(0)->nodeValue : '';
1440
        $texto = $this->formatField($texto, "#####-###");
1441
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1442
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1443
        //DATA DA SAÍDA
1444
        $x += $w;
1445
        $w = $wx;
1446
        $texto = 'DATA DA SAÍDA/ENTRADA';
1447
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1448
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1449
        $dSaiEnt = ! empty($this->ide->getElementsByTagName("dSaiEnt")->item(0)->nodeValue) ?
1450
                $this->ide->getElementsByTagName("dSaiEnt")->item(0)->nodeValue : '';
1451
        if ($dSaiEnt == '') {
1452
            $dSaiEnt = ! empty($this->ide->getElementsByTagName("dhSaiEnt")->item(0)->nodeValue) ?
1453
                    $this->ide->getElementsByTagName("dhSaiEnt")->item(0)->nodeValue : '';
1454
            $aDsaient = explode('T', $dSaiEnt);
1455
            $dSaiEnt = $aDsaient[0];
1456
        }
1457
        $texto = $this->ymdTodmy($dSaiEnt);
1458
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1459
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1460
        //MUNICÍPIO
1461
        $w = $w1;
1462
        $y += $h;
1463
        $x = $oldX;
1464
        $texto = 'MUNICÍPIO';
1465
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1466
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1467
        $texto = $this->dest->getElementsByTagName("xMun")->item(0)->nodeValue;
1468
        if (strtoupper(trim($texto)) == "EXTERIOR" && $this->dest->getElementsByTagName("xPais")->length > 0) {
1469
            $texto .= " - " .  $this->dest->getElementsByTagName("xPais")->item(0)->nodeValue;
1470
        }
1471
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1472
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'L', 0, '');
1473
        //UF
1474
        $x += $w;
1475
        $w = 8;
1476
        $texto = 'UF';
1477
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1478
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1479
        $texto = $this->dest->getElementsByTagName("UF")->item(0)->nodeValue;
1480
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1481
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1482
        //FONE / FAX
1483
        $x += $w;
1484
        $w = round(($maxW -$w1-$wx-8)/2, 0);
1485
        $w3 = $w;
1486
        $texto = 'FONE / FAX';
1487
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1488
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1489
        $texto = ! empty($this->dest->getElementsByTagName("fone")->item(0)->nodeValue) ?
1490
                $this->dest->getElementsByTagName("fone")->item(0)->nodeValue : '';
1491
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1492
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1493
        //INSCRIÇÃO ESTADUAL
1494
        $x += $w;
1495
        $w = $maxW -$w1-$wx-8-$w3;
1496
        $texto = 'INSCRIÇÃO ESTADUAL';
1497
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1498
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1499
        $IE = $this->dest->getElementsByTagName("IE");
1500
        $texto = ($IE && $IE->length > 0) ? $IE->item(0)->nodeValue : '';
1501
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1502
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1503
        //HORA DA SAÍDA
1504
        $x += $w;
1505
        $w = $wx;
1506
        $texto = 'HORA DA SAÍDA/ENTRADA';
1507
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1508
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
1509
        $hSaiEnt = ! empty($this->ide->getElementsByTagName("hSaiEnt")->item(0)->nodeValue) ?
1510
                $this->ide->getElementsByTagName("hSaiEnt")->item(0)->nodeValue : '';
1511
        if ($hSaiEnt == '') {
1512
            $dhSaiEnt = ! empty($this->ide->getElementsByTagName("dhSaiEnt")->item(0)->nodeValue) ?
1513
                    $this->ide->getElementsByTagName("dhSaiEnt")->item(0)->nodeValue : '';
1514
            $tsDhSaiEnt = $this->convertTime($dhSaiEnt);
1515
            if ($tsDhSaiEnt != '') {
1516
                $hSaiEnt = date('H:i:s', $tsDhSaiEnt);
1517
            }
1518
        }
1519
        $texto = $hSaiEnt;
1520
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1521
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
1522
        return ($y + $h);
1523
    } //fim da função destinatarioDANFE
1524
1525
     /**
1526
     * getTextoFatura
1527
     * Gera a String do Texto da Fatura
1528
      *
1529
     * @name   getTextoFatura
1530
     * @return uma String com o texto ou "";
1531
     */
1532
    protected function getTextoFatura()
1533
    {
1534
        if (isset($this->cobr)) {
1535
            $fat = $this->cobr->getElementsByTagName("fat")->item(0);
1536
            if (isset($fat)) {
1537
                if (!empty($this->getTagValue($this->ide, "indPag"))) {
1538
                    $textoIndPag = "";
1539
                    $indPag = $this->getTagValue($this->ide, "indPag");
1540
                    if ($indPag === "0") {
1541
                        $textoIndPag = "Pagamento à Vista - ";
1542
                    } elseif ($indPag === "1") {
1543
                        $textoIndPag = "Pagamento à Prazo - ";
1544
                    }
1545
                    $nFat = $this->getTagValue($fat, "nFat", "Fatura: ");
1546
                    $vOrig = $this->getTagValue($fat, "vOrig", " Valor Original: ");
1547
                    $vDesc = $this->getTagValue($fat, "vDesc", " Desconto: ");
1548
                    $vLiq = $this->getTagValue($fat, "vLiq", " Valor Líquido: ");
1549
                    $texto = $textoIndPag . $nFat . $vOrig . $vDesc . $vLiq;
1550
                    return $texto;
1551
                } else {
1552
                    $pag = $this->dom->getElementsByTagName("pag");
1553
                    if ($tPag = $this->getTagValue($pag->item(0), "tPag")) {
1554
                        return $this->tipoPag($tPag);
1555
                    }
1556
                }
1557
            }
1558
        }
1559
        return "";
1560
    }
1561
1562
     /**
1563
     * sizeExtraTextoFatura
1564
     * Calcula o espaço ocupado pelo texto da fatura. Este espaço só é utilizado quando não houver duplicata.
1565
      *
1566
     * @name   sizeExtraTextoFatura
1567
     * @return integer
1568
     */
1569
    protected function sizeExtraTextoFatura()
1570
    {
1571
        $textoFatura = $this->getTextoFatura();
1572
        //verificar se existem duplicatas
1573
        if ($this->dup->length == 0 && $textoFatura !== "") {
1574
            return 10;
1575
        }
1576
        return 0;
1577
    }
1578
1579
    /**
1580
     * fatura
1581
     * Monta o campo de duplicatas da DANFE (retrato e paisagem)
1582
     *
1583
     * @name   fatura
1584
     * @param  number $x Posição horizontal canto esquerdo
1585
     * @param  number $y Posição vertical canto superior
1586
     * @return number Posição vertical final
1587
     */
1588
    protected function fatura($x, $y)
1589
    {
1590
        $linha = 1;
1591
        $h = 8+3;
1592
        $oldx = $x;
1593
        $textoFatura = $this->getTextoFatura();
1594
        //verificar se existem duplicatas
1595
        if ($this->dup->length > 0 || $textoFatura !== "") {
1596
            //#####################################################################
1597
            //FATURA / DUPLICATA
1598
            $texto = "FATURA / DUPLICATA";
1599
            if ($this->orientacao == 'P') {
1600
                $w = $this->wPrint;
1601
            } else {
1602
                $w = 271;
1603
            }
1604
            $h = 8;
1605
            $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1606
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1607
            $y += 3;
1608
            $dups = "";
1609
            $dupcont = 0;
1610
            $nFat = $this->dup->length;
1611
            if ($nFat > 7) {
1612
                $myH = 6;
1613
                $myW = $this->wPrint;
1614
                if ($this->orientacao == 'L') {
1615
                    $myW -= $this->wCanhoto;
1616
                }
1617
                $aFont = ['font' => $this->fontePadrao, 'size' => 9, 'style' => ''];
1618
                $texto = "Existem mais de 7 duplicatas registradas, portanto não "
1619
                    . "serão exibidas, confira diretamente pelo XML.";
1620
                $this->pdf->textBox($x, $y, $myW, $myH, $texto, $aFont, 'C', 'C', 1, '');
1621
                return ($y + $h - 3);
1622
            }
1623
            if ($textoFatura !== "" && $this->exibirTextoFatura) {
1624
                $myH=6;
1625
                $myW = $this->wPrint;
1626
                if ($this->orientacao == 'L') {
1627
                    $myW -= $this->wCanhoto;
1628
                }
1629
                $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>''];
1630
                $this->pdf->textBox($x, $y, $myW, $myH, $textoFatura, $aFont, 'C', 'L', 1, '');
1631
                $y+=$myH+1;
1632
            }
1633
            if ($this->orientacao == 'P') {
1634
                $w = round($this->wPrint/7.018, 0)-1;
1635
            } else {
1636
                $w = 28;
1637
            }
1638
            $increm = 1;
1639
            foreach ($this->dup as $k => $d) {
1640
                $nDup = ! empty($this->dup->item($k)->getElementsByTagName('nDup')->item(0)->nodeValue) ?
1641
                        $this->dup->item($k)->getElementsByTagName('nDup')->item(0)->nodeValue : '';
1642
                $dDup = ! empty($this->dup->item($k)->getElementsByTagName('dVenc')->item(0)->nodeValue) ?
1643
                        $this->ymdTodmy($this->dup->item($k)->getElementsByTagName('dVenc')->item(0)->nodeValue) : '';
1644
                $vDup = ! empty($this->dup->item($k)->getElementsByTagName('vDup')->item(0)->nodeValue) ?
1645
                        'R$ ' . number_format(
1646
                            $this->dup->item($k)->getElementsByTagName('vDup')->item(0)->nodeValue,
1647
                            2,
1648
                            ",",
1649
                            "."
1650
                        ) : '';
1651
                $h = 8;
1652
                $texto = '';
1653
                if ($nDup!='0' && $nDup!='') {
1654
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1655
                    $this->pdf->textBox($x, $y, $w, $h, 'Num.', $aFont, 'T', 'L', 1, '');
1656
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1657
                    $this->pdf->textBox($x, $y, $w, $h, $nDup, $aFont, 'T', 'R', 0, '');
1658
                } else {
1659
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1660
                    $this->pdf->textBox($x, $y, $w, $h, ($dupcont+1)."", $aFont, 'T', 'L', 1, '');
1661
                }
1662
                $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1663
                $this->pdf->textBox($x, $y, $w, $h, 'Venc.', $aFont, 'C', 'L', 0, '');
1664
                $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1665
                $this->pdf->textBox($x, $y, $w, $h, $dDup, $aFont, 'C', 'R', 0, '');
1666
                $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1667
                $this->pdf->textBox($x, $y, $w, $h, 'Valor', $aFont, 'B', 'L', 0, '');
1668
                $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1669
                $this->pdf->textBox($x, $y, $w, $h, $vDup, $aFont, 'B', 'R', 0, '');
1670
                $x += $w+$increm;
1671
                $dupcont += 1;
1672
                if ($this->orientacao == 'P') {
1673
                    $maxDupCont = 6;
1674
                } else {
1675
                    $maxDupCont = 8;
1676
                }
1677
                if ($dupcont > $maxDupCont) {
1678
                    $y += 9;
1679
                    $x = $oldx;
1680
                    $dupcont = 0;
1681
                    $linha += 1;
1682
                }
1683
                if ($linha == 5) {
1684
                    $linha = 4;
1685
                    break;
1686
                }
1687
            }
1688
            if ($dupcont == 0) {
1689
                $y -= 9;
1690
                $linha--;
1691
            }
1692
            return ($y+$h);
1693
        } else {
1694
            $linha = 0;
1695
            return ($y-2);
1696
        }
1697
    }
1698
1699
    /**
1700
     * pagamento
1701
     * Monta o campo de pagamentos da DANFE (retrato e paisagem) (foi baseada na fatura)
1702
     *
1703
     * @name   pagamento
1704
     * @param  number $x Posição horizontal canto esquerdo
1705
     * @param  number $y Posição vertical canto superior
1706
     * @return number Posição vertical final
1707
     */
1708
    protected function pagamento($x, $y)
1709
    {
1710
        $linha = 1;
1711
        $h = 8+3;
1712
        $oldx = $x;
1713
        //verificar se existem cobranças definidas
1714
        if (isset($this->detPag) && $this->detPag->length > 0) {
1715
            //#####################################################################
1716
            //Tipo de pagamento
1717
            $texto = "PAGAMENTO";
1718
            if ($this->orientacao == 'P') {
1719
                $w = $this->wPrint;
1720
            } else {
1721
                $w = 271;
1722
            }
1723
            $h = 8;
1724
            $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1725
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1726
            $y += 3;
1727
            $dups = "";
1728
            $dupcont = 0;
1729
            if ($this->orientacao == 'P') {
1730
                $w = round($this->wPrint/7.018, 0)-1;
1731
            } else {
1732
                $w = 28;
1733
            }
1734
            if ($this->orientacao == 'P') {
1735
                $maxDupCont = 6;
1736
            } else {
1737
                $maxDupCont = 8;
1738
            }
1739
            $increm = 1;
1740
            $formaPagamento = ['01'=>'Dinheiro','02'=>'Cheque','03'=>'Cartão de Crédito',
1741
                                    '04'=>'Cartão de Débito','05'=>'Crédito Loja','10'=>'Vale Alimentação',
1742
                                    '11'=>'Vale Refeição','12'=>'Vale Presente','13'=>'Vale Combustível',
1743
                                    '14'=>'Duplicata Mercantil','15'=>'Boleto','90'=>'Sem pagamento','99'=>'Outros'];
1744
            $bandeira = ['01'=>'Visa','02'=>'Mastercard','03'=>'American','04'=>'Sorocred','05'=>'Diners',
1745
                              '06'=>'Elo','07'=>'Hipercard','08'=>'Aura','09'=>'Cabal','99'=>'Outros'];
1746
            foreach ($this->detPag as $k => $d) {
1747
                $fPag = !empty($this->detPag->item($k)->getElementsByTagName('tPag')->item(0)->nodeValue)
1748
                    ? $this->detPag->item($k)->getElementsByTagName('tPag')->item(0)->nodeValue : '0';
1749
                $vPag = ! empty($this->detPag->item($k)->getElementsByTagName('vPag')->item(0)->nodeValue)
1750
                    ? 'R$ ' . number_format(
1751
                        $this->detPag->item($k)->getElementsByTagName('vPag')->item(0)->nodeValue,
1752
                        2,
1753
                        ",",
1754
                        "."
1755
                    ) : '';
1756
                $h = 6;
1757
                $texto = '';
1758
                if (isset($formaPagamento[$fPag])) {
1759
                    /*Exibir Item sem pagamento ou outros?*/
1760
                    if ($fPag=='90' || $fPag=='99') {
1761
                        continue;
1762
                    }
1763
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1764
                    $this->pdf->textBox($x, $y, $w, $h, 'Forma', $aFont, 'T', 'L', 1, '');
1765
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1766
                    $this->pdf->textBox($x, $y, $w, $h, $formaPagamento[$fPag], $aFont, 'T', 'R', 0, '');
1767
                } else {
1768
                    $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
1769
                    $this->pdf->textBox($x, $y, $w, $h, "Forma ".$fPag." não encontrado", $aFont, 'T', 'L', 1, '');
1770
                }
1771
                $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1772
                $this->pdf->textBox($x, $y, $w, $h, 'Valor', $aFont, 'B', 'L', 0, '');
1773
                $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1774
                $this->pdf->textBox($x, $y, $w, $h, $vPag, $aFont, 'B', 'R', 0, '');
1775
                $x += $w+$increm;
1776
                $dupcont += 1;
1777
1778
                if ($dupcont>$maxDupCont) {
1779
                    $y += 9;
1780
                    $x = $oldx;
1781
                    $dupcont = 0;
1782
                    $linha += 1;
1783
                }
1784
                if ($linha == 5) {
1785
                    $linha = 4;
1786
                    break;
1787
                }
1788
            }
1789
            if ($dupcont == 0) {
1790
                $y -= 9;
1791
                $linha--;
1792
            }
1793
            return ($y+$h);
1794
        } else {
1795
            $linha = 0;
1796
            return ($y-2);
1797
        }
1798
    } //fim da função pagamento
1799
    
1800
    /**
1801
     * impostoHelper
1802
     * Auxilia a montagem dos campos de impostos e totais da DANFE
1803
     *
1804
     * @name   impostoHelper
1805
     * @param  float $x Posição horizontal canto esquerdo
1806
     * @param  float $y Posição vertical canto superior
1807
     * @param  float $w Largura do campo
1808
     * @param  float $h Altura do campo
1809
     * @param  float $h Título do campo
1810
     * @param  float $h Valor do imposto
1811
     * @return float Sugestão do $x do próximo imposto
1812
     */
1813
    protected function impostoHelper($x, $y, $w, $h, $titulo, $campoImposto)
1814
    {
1815
        $valorImposto = '0, 00';
1816
        $the_field = $this->ICMSTot->getElementsByTagName($campoImposto)->item(0);
1817
        if (isset($the_field)) {
1818
            $the_value = $the_field->nodeValue;
1819
            if (!empty($the_value)) {
1820
                $valorImposto = number_format($the_value, 2, ",", ".");
1821
            }
1822
        }
1823
1824
        $fontTitulo = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1825
        $fontValor = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1826
        $this->pdf->textBox($x, $y, $w, $h, $titulo, $fontTitulo, 'T', 'L', 1, '');
1827
        $this->pdf->textBox($x, $y, $w, $h, $valorImposto, $fontValor, 'B', 'R', 0, '');
1828
1829
        $next_x = $x + $w;
1830
        return $next_x;
1831
    }
1832
1833
    /**
1834
     * imposto
1835
     * Monta o campo de impostos e totais da DANFE (retrato e paisagem)
1836
     *
1837
     * @param  number $x Posição horizontal canto esquerdo
1838
     * @param  number $y Posição vertical canto superior
1839
     * @return number Posição vertical final
1840
     */
1841
    protected function imposto($x, $y)
1842
    {
1843
        $x_inicial = $x;
1844
        //#####################################################################
1845
1846
1847
        $campos_por_linha = 9;
1848
        if (!$this->exibirPIS) {
1849
            $campos_por_linha--;
1850
        }
1851
        if (!$this->exibirIcmsInterestadual) {
1852
            $campos_por_linha -= 2;
1853
        }
1854
1855
        if ($this->orientacao == 'P') {
1856
            $maxW = $this->wPrint;
1857
            $title_size = 31;
1858
        } else {
1859
            $maxW = $this->wPrint - $this->wCanhoto;
1860
            $title_size = 40;
1861
        }
1862
        $w = $maxW / $campos_por_linha;
1863
1864
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1865
        $texto = "CÁLCULO DO IMPOSTO";
1866
        $this->pdf->textBox($x, $y, $title_size, 8, $texto, $aFont, 'T', 'L', 0, '');
1867
        $y += 3;
1868
        $h = 7;
1869
1870
        $x = $this->impostoHelper($x, $y, $w, $h, "BASE DE CÁLC. DO ICMS", "vBC");
1871
        $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DO ICMS", "vICMS");
1872
        $x = $this->impostoHelper($x, $y, $w, $h, "BASE DE CÁLC. ICMS S.T.", "vBCST");
1873
        $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DO ICMS SUBST.", "vST");
1874
        $x = $this->impostoHelper($x, $y, $w, $h, "V. IMP. IMPORTAÇÃO", "vII");
1875
1876
        if ($this->exibirIcmsInterestadual) {
1877
            $x = $this->impostoHelper($x, $y, $w, $h, "V. ICMS UF REMET.", "vICMSUFRemet");
1878
            $x = $this->impostoHelper($x, $y, $w, $h, "V. FCP UF DEST.", "vFCPUFDest");
1879
        }
1880
1881
        if ($this->exibirPIS) {
1882
            $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DO PIS", "vPIS");
1883
        }
1884
1885
        $x = $this->impostoHelper($x, $y, $w, $h, "V. TOTAL PRODUTOS", "vProd");
1886
1887
        //
1888
1889
        $y += $h;
1890
        $x = $x_inicial;
1891
1892
        $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DO FRETE", "vFrete");
1893
        $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DO SEGURO", "vSeg");
1894
        $x = $this->impostoHelper($x, $y, $w, $h, "DESCONTO", "vDesc");
1895
        $x = $this->impostoHelper($x, $y, $w, $h, "OUTRAS DESPESAS", "vOutro");
1896
        $x = $this->impostoHelper($x, $y, $w, $h, "VALOR TOTAL IPI", "vIPI");
1897
1898
        if ($this->exibirIcmsInterestadual) {
1899
            $x = $this->impostoHelper($x, $y, $w, $h, "V. ICMS UF DEST.", "vICMSUFDest");
1900
            $x = $this->impostoHelper($x, $y, $w, $h, "V. TOT. TRIB.", "vTotTrib");
1901
        }
1902
1903
        if ($this->exibirPIS) {
1904
            $x = $this->impostoHelper($x, $y, $w, $h, "VALOR DA COFINS", "vCOFINS");
1905
        }
1906
        $x = $this->impostoHelper($x, $y, $w, $h, "V. TOTAL DA NOTA", "vNF");
1907
1908
        return ($y+$h);
1909
    } //fim imposto
1910
1911
    /**
1912
     * transporte
1913
     * Monta o campo de transportes da DANFE (retrato e paisagem)
1914
     *
1915
     * @name   transporte
1916
     * @param  float $x Posição horizontal canto esquerdo
1917
     * @param  float $y Posição vertical canto superior
1918
     * @return float Posição vertical final
1919
     */
1920
    protected function transporte($x, $y)
1921
    {
1922
        $oldX = $x;
1923
        if ($this->orientacao == 'P') {
1924
            $maxW = $this->wPrint;
1925
        } else {
1926
            $maxW = $this->wPrint - $this->wCanhoto;
1927
        }
1928
        //#####################################################################
1929
        //TRANSPORTADOR / VOLUMES TRANSPORTADOS
1930
        $texto = "TRANSPORTADOR / VOLUMES TRANSPORTADOS";
1931
        $w = $maxW;
1932
        $h = 7;
1933
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
1934
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
1935
        //NOME / RAZÃO SOCIAL
1936
        $w1 = $maxW*0.29;
1937
        $y += 3;
1938
        $texto = 'NOME / RAZÃO SOCIAL';
1939
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1940
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'T', 'L', 1, '');
1941
        if (isset($this->transporta)) {
1942
            $texto = ! empty($this->transporta->getElementsByTagName("xNome")->item(0)->nodeValue) ?
1943
                    $this->transporta->getElementsByTagName("xNome")->item(0)->nodeValue : '';
1944
        } else {
1945
            $texto = '';
1946
        }
1947
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1948
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'B', 'L', 0, '');
1949
        //FRETE POR CONTA
1950
        $x += $w1;
1951
        $w2 = $maxW*0.15;
1952
        $texto = 'FRETE';
1953
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1954
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
1955
        $tipoFrete = ! empty($this->transp->getElementsByTagName("modFrete")->item(0)->nodeValue) ?
1956
                $this->transp->getElementsByTagName("modFrete")->item(0)->nodeValue : '0';
1957
        switch ($tipoFrete) {
1958
            case 0:
1959
                $texto = "0-Por conta do Rem";
1960
                break;
1961
            case 1:
1962
                $texto = "1-Por conta do Dest";
1963
                break;
1964
            case 2:
1965
                $texto = "2-Por conta de Terceiros";
1966
                break;
1967
            case 3:
1968
                $texto = "3-Próprio por conta do Rem";
1969
                break;
1970
            case 4:
1971
                $texto = "4-Próprio por conta do Dest";
1972
                break;
1973
            case 9:
1974
                $texto = "9-Sem Transporte";
1975
                break;
1976
        }
1977
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1978
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'C', 'C', 1, '');
1979
        //CÓDIGO ANTT
1980
        $x += $w2;
1981
        $texto = 'CÓDIGO ANTT';
1982
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1983
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
1984
        if (isset($this->veicTransp)) {
1985
            $texto = ! empty($this->veicTransp->getElementsByTagName("RNTC")->item(0)->nodeValue) ?
1986
                    $this->veicTransp->getElementsByTagName("RNTC")->item(0)->nodeValue : '';
1987
        } else {
1988
            $texto = '';
1989
        }
1990
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
1991
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
1992
        //PLACA DO VEÍC
1993
        $x += $w2;
1994
        $texto = 'PLACA DO VEÍCULO';
1995
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
1996
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
1997
        if (isset($this->veicTransp)) {
1998
            $texto = ! empty($this->veicTransp->getElementsByTagName("placa")->item(0)->nodeValue) ?
1999
                    $this->veicTransp->getElementsByTagName("placa")->item(0)->nodeValue : '';
2000
        } elseif (isset($this->reboque)) {
2001
            $texto = ! empty($this->reboque->getElementsByTagName("placa")->item(0)->nodeValue) ?
2002
                    $this->reboque->getElementsByTagName("placa")->item(0)->nodeValue : '';
2003
        } else {
2004
            $texto = '';
2005
        }
2006
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2007
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
2008
        //UF
2009
        $x += $w2;
2010
        $w3 = round($maxW*0.04, 0);
2011
        $texto = 'UF';
2012
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2013
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'T', 'L', 1, '');
2014
        if (isset($this->veicTransp)) {
2015
            $texto = ! empty($this->veicTransp->getElementsByTagName("UF")->item(0)->nodeValue) ?
2016
                    $this->veicTransp->getElementsByTagName("UF")->item(0)->nodeValue : '';
2017
        } elseif (isset($this->reboque)) {
2018
            $texto = ! empty($this->reboque->getElementsByTagName("UF")->item(0)->nodeValue) ?
2019
                    $this->reboque->getElementsByTagName("UF")->item(0)->nodeValue : '';
2020
        } else {
2021
            $texto = '';
2022
        }
2023
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2024
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'B', 'C', 0, '');
2025
        //CNPJ / CPF
2026
        $x += $w3;
2027
        $w = $maxW-($w1+3*$w2+$w3);
2028
        $texto = 'CNPJ / CPF';
2029
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2030
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2031
        if (isset($this->transporta)) {
2032
            $texto = ! empty($this->transporta->getElementsByTagName("CNPJ")->item(0)->nodeValue) ?
2033
                    $this->formatField(
2034
                        $this->transporta->getElementsByTagName("CNPJ")->item(0)->nodeValue,
2035
                        "##.###.###/####-##"
2036
                    ) : '';
2037
            if ($texto == '') {
2038
                $texto = ! empty($this->transporta->getElementsByTagName("CPF")->item(0)->nodeValue) ?
2039
                        $this->formatField(
2040
                            $this->transporta->getElementsByTagName("CPF")->item(0)->nodeValue,
2041
                            "###.###.###-##"
2042
                        ) : '';
2043
            }
2044
        } else {
2045
            $texto = '';
2046
        }
2047
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2048
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
2049
        //#####################################################################
2050
        //ENDEREÇO
2051
        $y += $h;
2052
        $x = $oldX;
2053
        $h = 7;
2054
        $w1 = $maxW*0.44;
2055
        $texto = 'ENDEREÇO';
2056
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2057
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'T', 'L', 1, '');
2058
        if (isset($this->transporta)) {
2059
            $texto = ! empty($this->transporta->getElementsByTagName("xEnder")->item(0)->nodeValue) ?
2060
                    $this->transporta->getElementsByTagName("xEnder")->item(0)->nodeValue : '';
2061
        } else {
2062
            $texto = '';
2063
        }
2064
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2065
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'B', 'L', 0, '');
2066
        //MUNICÍPIO
2067
        $x += $w1;
2068
        $w2 = round($maxW*0.30, 0);
2069
        $texto = 'MUNICÍPIO';
2070
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2071
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
2072
        if (isset($this->transporta)) {
2073
            $texto = ! empty($this->transporta->getElementsByTagName("xMun")->item(0)->nodeValue) ?
2074
                    $this->transporta->getElementsByTagName("xMun")->item(0)->nodeValue : '';
2075
        } else {
2076
            $texto = '';
2077
        }
2078
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2079
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
2080
        //UF
2081
        $x += $w2;
2082
        $w3 = round($maxW*0.04, 0);
2083
        $texto = 'UF';
2084
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2085
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'T', 'L', 1, '');
2086
        if (isset($this->transporta)) {
2087
            $texto = ! empty($this->transporta->getElementsByTagName("UF")->item(0)->nodeValue) ?
2088
                    $this->transporta->getElementsByTagName("UF")->item(0)->nodeValue : '';
2089
        } else {
2090
            $texto = '';
2091
        }
2092
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2093
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'B', 'C', 0, '');
2094
        //INSCRIÇÃO ESTADUAL
2095
        $x += $w3;
2096
        $w = $maxW-($w1+$w2+$w3);
2097
        $texto = 'INSCRIÇÃO ESTADUAL';
2098
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2099
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2100
        $texto = '';
2101
        if (isset($this->transporta)) {
2102
            if (! empty($this->transporta->getElementsByTagName("IE")->item(0)->nodeValue)) {
2103
                $texto = $this->transporta->getElementsByTagName("IE")->item(0)->nodeValue;
2104
            }
2105
        }
2106
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2107
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'C', 0, '');
2108
        //Tratar Multiplos volumes
2109
        $volumes = $this->transp->getElementsByTagName('vol');
2110
        $quantidade = 0;
2111
        $especie = '';
2112
        $marca = '';
2113
        $numero = '';
2114
        $texto = '';
2115
        $pesoBruto=0;
2116
        $pesoLiquido=0;
2117
        foreach ($volumes as $volume) {
2118
            $quantidade += ! empty($volume->getElementsByTagName("qVol")->item(0)->nodeValue) ?
2119
                    $volume->getElementsByTagName("qVol")->item(0)->nodeValue : 0;
2120
            $pesoBruto += ! empty($volume->getElementsByTagName("pesoB")->item(0)->nodeValue) ?
2121
                    $volume->getElementsByTagName("pesoB")->item(0)->nodeValue : 0;
2122
            $pesoLiquido += ! empty($volume->getElementsByTagName("pesoL")->item(0)->nodeValue) ?
2123
                    $volume->getElementsByTagName("pesoL")->item(0)->nodeValue : 0;
2124
            $texto = ! empty($this->transp->getElementsByTagName("esp")->item(0)->nodeValue) ?
2125
                    $this->transp->getElementsByTagName("esp")->item(0)->nodeValue : '';
2126
            if ($texto != $especie && $especie != '') {
2127
                //tem várias especies
2128
                $especie = 'VARIAS';
2129
            } else {
2130
                $especie = $texto;
2131
            }
2132
            $texto = ! empty($this->transp->getElementsByTagName("marca")->item(0)->nodeValue) ?
2133
                    $this->transp->getElementsByTagName("marca")->item(0)->nodeValue : '';
2134
            if ($texto != $marca && $marca != '') {
2135
                //tem várias especies
2136
                $marca = 'VARIAS';
2137
            } else {
2138
                $marca = $texto;
2139
            }
2140
            $texto = ! empty($this->transp->getElementsByTagName("nVol")->item(0)->nodeValue) ?
2141
                    $this->transp->getElementsByTagName("nVol")->item(0)->nodeValue : '';
2142
            if ($texto != $numero && $numero != '') {
2143
                //tem várias especies
2144
                $numero = 'VARIOS';
2145
            } else {
2146
                $numero = $texto;
2147
            }
2148
        }
2149
2150
        //#####################################################################
2151
        //QUANTIDADE
2152
        $y += $h;
2153
        $x = $oldX;
2154
        $h = 7;
2155
        $w1 = round($maxW*0.10, 0);
2156
        $texto = 'QUANTIDADE';
2157
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2158
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'T', 'L', 1, '');
2159
        if (!empty($quantidade)) {
2160
            $texto = $quantidade;
2161
            $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2162
            $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'B', 'C', 0, '');
2163
        }
2164
        //ESPÉCIE
2165
        $x += $w1;
2166
        $w2 = round($maxW*0.17, 0);
2167
        $texto = 'ESPÉCIE';
2168
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2169
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
2170
        $texto = $especie;
2171
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2172
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
2173
        //MARCA
2174
        $x += $w2;
2175
        $texto = 'MARCA';
2176
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2177
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
2178
        $texto = ! empty($this->transp->getElementsByTagName("marca")->item(0)->nodeValue) ?
2179
                $this->transp->getElementsByTagName("marca")->item(0)->nodeValue : '';
2180
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2181
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
2182
        //NUMERAÇÃO
2183
        $x += $w2;
2184
        $texto = 'NUMERAÇÃO';
2185
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2186
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'T', 'L', 1, '');
2187
        $texto = $numero;
2188
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2189
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'B', 'C', 0, '');
2190
        //PESO BRUTO
2191
        $x += $w2;
2192
        $w3 = round($maxW*0.20, 0);
2193
        $texto = 'PESO BRUTO';
2194
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2195
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'T', 'L', 1, '');
2196
        if (is_numeric($pesoBruto) && $pesoBruto > 0) {
2197
            $texto = number_format($pesoBruto, 3, ",", ".");
2198
        } else {
2199
            $texto = '';
2200
        }
2201
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2202
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'B', 'R', 0, '');
2203
        //PESO LÍQUIDO
2204
        $x += $w3;
2205
        $w = $maxW -($w1+3*$w2+$w3);
2206
        $texto = 'PESO LÍQUIDO';
2207
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2208
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2209
        if (is_numeric($pesoLiquido) && $pesoLiquido > 0) {
2210
            $texto = number_format($pesoLiquido, 3, ",", ".");
2211
        } else {
2212
            $texto = '';
2213
        }
2214
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2215
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'R', 0, '');
2216
        return ($y+$h);
2217
    } //fim transporte
2218
2219
2220
2221
    protected function descricaoProdutoHelper($origem, $campo, $formato)
2222
    {
2223
        $valor_original = $origem->getElementsByTagName($campo)->item(0);
2224
        if (!isset($valor_original)) {
2225
            return "";
2226
        }
2227
        $valor_original = $valor_original->nodeValue;
2228
        $valor = ! empty($valor_original) ? number_format($valor_original, 2, ",", ".") : '';
2229
2230
        if ($valor != "") {
2231
            return sprintf($formato, $valor);
2232
        }
2233
        return "";
2234
    }
2235
2236
    /**
2237
     * descricaoProduto
2238
     * Monta a string de descrição de cada Produto
2239
     *
2240
     * @name   descricaoProduto
2241
     * @param  DOMNode itemProd
2242
     * @return string descricao do produto
2243
     */
2244
    protected function descricaoProduto($itemProd)
2245
    {
2246
        $prod = $itemProd->getElementsByTagName('prod')->item(0);
2247
        $ICMS = $itemProd->getElementsByTagName("ICMS")->item(0);
2248
        $ICMSUFDest = $itemProd->getElementsByTagName("ICMSUFDest")->item(0);
2249
        $impostos = '';
2250
2251
        if (!empty($ICMS)) {
2252
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vBCFCP", " BcFcp=%s");
2253
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pFCP", " pFcp=%s%%");
2254
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vFCP", " vFcp=%s");
2255
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pRedBC", " pRedBC=%s%%");
2256
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pMVAST", " IVA/MVA=%s%%");
2257
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pICMSST", " pIcmsSt=%s%%");
2258
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vBCST", " BcIcmsSt=%s");
2259
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vICMSST", " vIcmsSt=%s");
2260
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vBCFCPST", " BcFcpSt=%s");
2261
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pFCPST", " pFcpSt=%s%%");
2262
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vFCPST", " vFcpSt=%s");
2263
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vBCSTRet", " vBcStRet=%s");
2264
            $impostos .= $this->descricaoProdutoHelper($ICMS, "pST", " pSt=%s");
2265
            $impostos .= $this->descricaoProdutoHelper($ICMS, "vICMSSTRet", " vIcmsStRet=%s");
2266
        }
2267
        if (!empty($ICMSUFDest)) {
2268
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "pFCPUFDest", " pFCPUFDest=%s%%");
2269
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "pICMSUFDest", " pICMSUFDest=%s%%");
2270
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "pICMSInterPart", " pICMSInterPart=%s%%");
2271
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "vFCPUFDest", " vFCPUFDest=%s");
2272
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "vICMSUFDest", " vICMSUFDest=%s");
2273
            $impostos .= $this->descricaoProdutoHelper($ICMSUFDest, "vICMSUFRemet", " vICMSUFRemet=%s");
2274
        }
2275
        $infAdProd = ! empty($itemProd->getElementsByTagName('infAdProd')->item(0)->nodeValue)
2276
            ? substr(
2277
                $this->anfaveaDANFE($itemProd->getElementsByTagName('infAdProd')->item(0)->nodeValue),
2278
                0,
2279
                500
2280
            )
2281
            : '';
2282
        if (! empty($infAdProd)) {
2283
            $infAdProd = trim($infAdProd);
2284
            $infAdProd .= ' ';
2285
        }
2286
        $loteTxt ='';
2287
        $rastro = $prod->getElementsByTagName("med");
2288
        if (!empty($prod->getElementsByTagName("rastro"))) {
2289
            $rastro = $prod->getElementsByTagName("rastro");
2290
            $i = 0;
2291
            while ($i < $rastro->length) {
2292
                $loteTxt .= $this->getTagValue($rastro->item($i), 'nLote', ' Lote: ');
2293
                $loteTxt .= $this->getTagValue($rastro->item($i), 'qLote', ' Quant: ');
2294
                $loteTxt .= $this->getTagDate($rastro->item($i), 'dFab', ' Fab: ');
2295
                $loteTxt .= $this->getTagDate($rastro->item($i), 'dVal', ' Val: ');
2296
                $loteTxt .= $this->getTagValue($rastro->item($i), 'vPMC', ' PMC: ');
2297
                $i++;
2298
            }
2299
            if ($loteTxt != '') {
2300
                $loteTxt.= ' ';
2301
            }
2302
        }
2303
        //NT2013.006 FCI
2304
        $nFCI = (! empty($itemProd->getElementsByTagName('nFCI')->item(0)->nodeValue)) ?
2305
                ' FCI:'.$itemProd->getElementsByTagName('nFCI')->item(0)->nodeValue : '';
2306
        $tmp_ad=$infAdProd . ($this->descProdInfoComplemento ? $loteTxt . $impostos . $nFCI : '');
2307
        $texto = $prod->getElementsByTagName("xProd")->item(0)->nodeValue . (strlen($tmp_ad)!=0?"\n    ".$tmp_ad:'');
2308
        //decodifica os caracteres html no xml
2309
        $texto = html_entity_decode($texto);
2310
        if ($this->descProdQuebraLinha) {
2311
            $texto = str_replace(";", "\n", $texto);
2312
        }
2313
        return $texto;
2314
    }
2315
2316
    /**
2317
     * itens
2318
     * Monta o campo de itens da DANFE (retrato e paisagem)
2319
     *
2320
     * @name   itens
2321
     * @param  float $x       Posição horizontal canto esquerdo
2322
     * @param  float $y       Posição vertical canto superior
2323
     * @param  float $nInicio Número do item inicial
2324
     * @param  float $max     Número do item final
2325
     * @param  float $hmax    Altura máxima do campo de itens em mm
2326
     * @return float Posição vertical final
2327
     */
2328
    protected function itens($x, $y, &$nInicio, $hmax, $pag = 0, $totpag = 0, $hCabecItens = 7)
2329
    {
2330
        $oldX = $x;
2331
        $oldY = $y;
2332
        $totItens = $this->det->length;
2333
        //#####################################################################
2334
        //DADOS DOS PRODUTOS / SERVIÇOS
2335
        $texto = "DADOS DOS PRODUTOS / SERVIÇOS ";
2336
        if ($this->orientacao == 'P') {
2337
            $w = $this->wPrint;
2338
        } else {
2339
            if ($nInicio < 2) { // primeira página
2340
                $w = $this->wPrint - $this->wCanhoto;
2341
            } else { // páginas seguintes
2342
                $w = $this->wPrint;
2343
            }
2344
        }
2345
        $h = 4;
2346
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
2347
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
2348
        $y += 3;
2349
        //desenha a caixa dos dados dos itens da NF
2350
        $hmax += 1;
2351
        $texto = '';
2352
        $this->pdf->textBox($x, $y, $w, $hmax);
2353
        //##################################################################################
2354
        // cabecalho LOOP COM OS DADOS DOS PRODUTOS
2355
        //CÓDIGO PRODUTO
2356
        $texto = "CÓDIGO PRODUTO";
2357
        $w1 = round($w*0.09, 0);
2358
        $h = 4;
2359
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2360
        $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2361
        $this->pdf->line($x+$w1, $y, $x+$w1, $y+$hmax);
2362
        //DESCRIÇÃO DO PRODUTO / SERVIÇO
2363
        $x += $w1;
2364
        $w2 = round($w*0.28, 0);
2365
        $texto = 'DESCRIÇÃO DO PRODUTO / SERVIÇO';
2366
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2367
        $this->pdf->textBox($x, $y, $w2, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2368
        $this->pdf->line($x+$w2, $y, $x+$w2, $y+$hmax);
2369
        //NCM/SH
2370
        $x += $w2;
2371
        $w3 = round($w*0.06, 0);
2372
        $texto = 'NCM/SH';
2373
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2374
        $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2375
        $this->pdf->line($x+$w3, $y, $x+$w3, $y+$hmax);
2376
        //O/CST ou O/CSOSN
2377
        $x += $w3;
2378
        $w4 = round($w*0.05, 0);
2379
        $texto = 'O/CSOSN';//Regime do Simples CRT = 1 ou CRT = 2
2380
        if ($this->getTagValue($this->emit, 'CRT') == '3') {
2381
             $texto = 'O/CST';//Regime Normal
2382
        }
2383
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2384
        $this->pdf->textBox($x, $y, $w4, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2385
        $this->pdf->line($x+$w4, $y, $x+$w4, $y+$hmax);
2386
        //CFOP
2387
        $x += $w4;
2388
        $w5 = round($w*0.04, 0);
2389
        $texto = 'CFOP';
2390
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2391
        $this->pdf->textBox($x, $y, $w5, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2392
        $this->pdf->line($x+$w5, $y, $x+$w5, $y+$hmax);
2393
        //UN
2394
        $x += $w5;
2395
        $w6 = round($w*0.03, 0);
2396
        $texto = 'UN';
2397
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2398
        $this->pdf->textBox($x, $y, $w6, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2399
        $this->pdf->line($x+$w6, $y, $x+$w6, $y+$hmax);
2400
        //QUANT
2401
        $x += $w6;
2402
        $w7 = round($w*0.07, 0);
2403
        $texto = 'QUANT';
2404
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2405
        $this->pdf->textBox($x, $y, $w7, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2406
        $this->pdf->line($x+$w7, $y, $x+$w7, $y+$hmax);
2407
        //VALOR UNIT
2408
        $x += $w7;
2409
        $w8 = round($w*0.06, 0);
2410
        $texto = 'VALOR UNIT';
2411
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2412
        $this->pdf->textBox($x, $y, $w8, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2413
        $this->pdf->line($x+$w8, $y, $x+$w8, $y+$hmax);
2414
        //VALOR TOTAL
2415
        $x += $w8;
2416
        $w9 = round($w*0.06, 0);
2417
        $texto = 'VALOR TOTAL';
2418
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2419
        $this->pdf->textBox($x, $y, $w9, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2420
        $this->pdf->line($x+$w9, $y, $x+$w9, $y+$hmax);
2421
        //B.CÁLC ICMS
2422
        $x += $w9;
2423
        $w10 = round($w*0.06, 0);
2424
        $texto = 'B.CÁLC ICMS';
2425
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2426
        $this->pdf->textBox($x, $y, $w10, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2427
        $this->pdf->line($x+$w10, $y, $x+$w10, $y+$hmax);
2428
        //VALOR ICMS
2429
        $x += $w10;
2430
        $w11 = round($w*0.06, 0);
2431
        $texto = 'VALOR ICMS';
2432
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2433
        $this->pdf->textBox($x, $y, $w11, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2434
        $this->pdf->line($x+$w11, $y, $x+$w11, $y+$hmax);
2435
        //VALOR IPI
2436
        $x += $w11;
2437
        $w12 = round($w*0.05, 0);
2438
        $texto = 'VALOR IPI';
2439
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2440
        $this->pdf->textBox($x, $y, $w12, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2441
        $this->pdf->line($x+$w12, $y, $x+$w12, $y+$hmax);
2442
        //ALÍQ. ICMS
2443
        $x += $w12;
2444
        $w13 = round($w*0.035, 0);
2445
        $texto = 'ALÍQ. ICMS';
2446
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2447
        $this->pdf->textBox($x, $y, $w13, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2448
        $this->pdf->line($x+$w13, $y, $x+$w13, $y+$hmax);
2449
        //ALÍQ. IPI
2450
        $x += $w13;
2451
        $w14 = $w-($w1+$w2+$w3+$w4+$w5+$w6+$w7+$w8+$w9+$w10+$w11+$w12+$w13);
2452
        $texto = 'ALÍQ. IPI';
2453
        $this->pdf->textBox($x, $y, $w14, $h, $texto, $aFont, 'C', 'C', 0, '', false);
2454
        $this->pdf->line($oldX, $y+$h+1, $oldX + $w, $y+$h+1);
2455
        $y += 5;
2456
        //##################################################################################
2457
        // LOOP COM OS DADOS DOS PRODUTOS
2458
        $i = 0;
2459
        $hUsado = $hCabecItens;
2460
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
2461
        foreach ($this->det as $d) {
2462
            if ($i >= $nInicio) {
2463
                $thisItem = $this->det->item($i);
2464
                //carrega as tags do item
2465
                $prod = $thisItem->getElementsByTagName("prod")->item(0);
2466
                $imposto = $this->det->item($i)->getElementsByTagName("imposto")->item(0);
2467
                $ICMS = $imposto->getElementsByTagName("ICMS")->item(0);
2468
                $IPI  = $imposto->getElementsByTagName("IPI")->item(0);
2469
                $textoProduto = trim($this->descricaoProduto($thisItem));
2470
2471
                $linhaDescr = $this->pdf->getNumLines($textoProduto, $w2, $aFont);
2472
                $h = round(($linhaDescr * $this->pdf->fontSize)+ ($linhaDescr * 0.5), 2);
2473
                $hUsado += $h;
2474
2475
                $diffH = $hmax - $hUsado;
2476
2477
                if ($pag != $totpag) {
2478
                    if (1 > $diffH && $i < $totItens) {
2479
                        //ultrapassa a capacidade para uma única página
2480
                        //o restante dos dados serão usados nas proximas paginas
2481
                        $nInicio = $i;
2482
                        break;
2483
                    }
2484
                }
2485
                $y_linha=$y+$h;
2486
                // linha entre itens
2487
                $this->pdf->dashedHLine($oldX, $y_linha, $w, 0.1, 120);
2488
                //corrige o x
2489
                $x=$oldX;
2490
                //codigo do produto
2491
                $texto = $prod->getElementsByTagName("cProd")->item(0)->nodeValue;
2492
                $this->pdf->textBox($x, $y, $w1, $h, $texto, $aFont, 'T', 'C', 0, '');
2493
                $x += $w1;
2494
                //DESCRIÇÃO
2495
                if ($this->orientacao == 'P') {
2496
                    $this->pdf->textBox($x, $y, $w2, $h, $textoProduto, $aFont, 'T', 'L', 0, '', false);
2497
                } else {
2498
                    $this->pdf->textBox($x, $y, $w2, $h, $textoProduto, $aFont, 'T', 'L', 0, '', false);
2499
                }
2500
                $x += $w2;
2501
                //NCM
2502
                $texto = ! empty($prod->getElementsByTagName("NCM")->item(0)->nodeValue) ?
2503
                        $prod->getElementsByTagName("NCM")->item(0)->nodeValue : '';
2504
                $this->pdf->textBox($x, $y, $w3, $h, $texto, $aFont, 'T', 'C', 0, '');
2505
                $x += $w3;
2506
                //CST
2507
                if (isset($ICMS)) {
2508
                    $origem =  $this->getTagValue($ICMS, "orig");
2509
                    $cst =  $this->getTagValue($ICMS, "CST");
2510
                    $csosn =  $this->getTagValue($ICMS, "CSOSN");
2511
                    $texto = $origem.$cst.$csosn;
2512
                    $this->pdf->textBox($x, $y, $w4, $h, $texto, $aFont, 'T', 'C', 0, '');
2513
                }
2514
                //CFOP
2515
                $x += $w4;
2516
                $texto = $prod->getElementsByTagName("CFOP")->item(0)->nodeValue;
2517
                $this->pdf->textBox($x, $y, $w5, $h, $texto, $aFont, 'T', 'C', 0, '');
2518
                //Unidade
2519
                $x += $w5;
2520
                $texto = $prod->getElementsByTagName("uCom")->item(0)->nodeValue;
2521
                $this->pdf->textBox($x, $y, $w6, $h, $texto, $aFont, 'T', 'C', 0, '');
2522
                $x += $w6;
2523
                if ($this->orientacao == 'P') {
2524
                    $alinhamento = 'R';
2525
                } else {
2526
                    $alinhamento = 'R';
2527
                }
2528
                // QTDADE
2529
                $texto = number_format($prod->getElementsByTagName("qCom")->item(0)->nodeValue, 4, ",", ".");
2530
                $this->pdf->textBox($x, $y, $w7, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2531
                $x += $w7;
2532
                // Valor Unitário
2533
                $texto = number_format($prod->getElementsByTagName("vUnCom")->item(0)->nodeValue, 4, ",", ".");
2534
                $this->pdf->textBox($x, $y, $w8, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2535
                $x += $w8;
2536
                // Valor do Produto
2537
                $texto = "";
2538
                if (is_numeric($prod->getElementsByTagName("vProd")->item(0)->nodeValue)) {
2539
                    $texto = number_format($prod->getElementsByTagName("vProd")->item(0)->nodeValue, 2, ",", ".");
2540
                }
2541
                $this->pdf->textBox($x, $y, $w9, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2542
                //Valor da Base de calculo
2543
                $x += $w9;
2544
                if (isset($ICMS)) {
2545
                    $texto = ! empty($ICMS->getElementsByTagName("vBC")->item(0)->nodeValue) ?
2546
                            number_format(
2547
                                $ICMS->getElementsByTagName("vBC")->item(0)->nodeValue,
2548
                                2,
2549
                                ",",
2550
                                "."
2551
                            ) : '0, 00';
2552
                    $this->pdf->textBox($x, $y, $w10, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2553
                }
2554
                //Valor do ICMS
2555
                $x += $w10;
2556
                if (isset($ICMS)) {
2557
                    $texto = ! empty($ICMS->getElementsByTagName("vICMS")->item(0)->nodeValue) ?
2558
                            number_format(
2559
                                $ICMS->getElementsByTagName("vICMS")->item(0)->nodeValue,
2560
                                2,
2561
                                ",",
2562
                                "."
2563
                            ) : '0, 00';
2564
                    $this->pdf->textBox($x, $y, $w11, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2565
                }
2566
                //Valor do IPI
2567
                $x += $w11;
2568
                if (isset($IPI)) {
2569
                    $texto = ! empty($IPI->getElementsByTagName("vIPI")->item(0)->nodeValue) ?
2570
                            number_format($IPI->getElementsByTagName("vIPI")->item(0)->nodeValue, 2, ",", ".") :'';
2571
                } else {
2572
                    $texto = '';
2573
                }
2574
                $this->pdf->textBox($x, $y, $w12, $h, $texto, $aFont, 'T', $alinhamento, 0, '');
2575
                // %ICMS
2576
                $x += $w12;
2577
                if (isset($ICMS)) {
2578
                    $texto = ! empty($ICMS->getElementsByTagName("pICMS")->item(0)->nodeValue) ?
2579
                            number_format(
2580
                                $ICMS->getElementsByTagName("pICMS")->item(0)->nodeValue,
2581
                                2,
2582
                                ",",
2583
                                "."
2584
                            ) : '0, 00';
2585
                    $this->pdf->textBox($x, $y, $w13, $h, $texto, $aFont, 'T', 'C', 0, '');
2586
                }
2587
                //%IPI
2588
                $x += $w13;
2589
                if (isset($IPI)) {
2590
                    $texto = ! empty($IPI->getElementsByTagName("pIPI")->item(0)->nodeValue) ?
2591
                            number_format($IPI->getElementsByTagName("pIPI")->item(0)->nodeValue, 2, ",", ".") : '';
2592
                } else {
2593
                    $texto = '';
2594
                }
2595
                $this->pdf->textBox($x, $y, $w14, $h, $texto, $aFont, 'T', 'C', 0, '');
2596
2597
2598
                // Dados do Veiculo Somente para veiculo 0 Km
2599
                $veicProd = $prod->getElementsByTagName("veicProd")->item(0);
2600
                // Tag somente é gerada para veiculo 0k, e só é permitido um veiculo por NF-e por conta do detran
2601
                // Verifica se a Tag existe
2602
                if (!empty($veicProd)) {
2603
                    $this->dadosItenVeiculoDANFE($oldX, $y, $nInicio, $h, $prod);
2604
                }
2605
2606
2607
                $y += $h;
2608
                $i++;
2609
                //incrementa o controle dos itens processados.
2610
                $this->qtdeItensProc++;
2611
            } else {
2612
                $i++;
2613
            }
2614
        }
2615
        return $oldY+$hmax;
2616
    }
2617
2618
2619
    /**
2620
     * dadosItenVeiculoDANFE
2621
     * Coloca os dados do veiculo abaixo do item da NFe. (retrato e paisagem)
2622
     *
2623
     * @param float  $x    Posição horizontal
2624
     *                     canto esquerdo
2625
     * @param float  $y    Posição vertical
2626
     *                     canto superior
2627
     * @param        $nInicio
2628
     * @param float  $h    altura do campo
2629
     * @param object $prod Contendo todos os dados do item
2630
     */
2631
2632
    protected function dadosItenVeiculoDANFE($x, $y, &$nInicio, $h, $prod)
2633
    {
2634
        $oldX = $x;
2635
        $oldY = $y;
2636
2637
        if ($this->orientacao == 'P') {
2638
            $w = $this->wPrint;
2639
        } else {
2640
            if ($nInicio < 2) { // primeira página
2641
                $w = $this->wPrint - $this->wCanhoto;
2642
            } else { // páginas seguintes
2643
                $w = $this->wPrint;
2644
            }
2645
        }
2646
2647
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
2648
2649
        $w1 = round($w*0.09, 0);
2650
2651
        // Tabela Renavam Combustivel
2652
        $renavamCombustivel = [
2653
            1=>'ALCOOL',
2654
            2=>'GASOLINA',
2655
            3=>'DIESEL',
2656
            4=>'GASOGENIO',
2657
            5=>'GAS METANO',
2658
            6=>'ELETRICO/FONTE INTERNA',
2659
            7=>'ELETRICO/FONTE EXTERNA',
2660
            8=>'GASOL/GAS NATURAL COMBUSTIVEL',
2661
            9=>'ALCOOL/GAS NATURAL COMBUSTIVEL',
2662
            10=>'DIESEL/GAS NATURAL COMBUSTIVEL',
2663
            11=>'VIDE/CAMPO/OBSERVACAO',
2664
            12=>'ALCOOL/GAS NATURAL VEICULAR',
2665
            13=>'GASOLINA/GAS NATURAL VEICULAR',
2666
            14=>'DIESEL/GAS NATURAL VEICULAR',
2667
            15=>'GAS NATURAL VEICULAR',
2668
            16=>'ALCOOL/GASOLINA',
2669
            17=>'GASOLINA/ALCOOL/GAS NATURAL',
2670
            18=>'GASOLINA/ELETRICO'
2671
        ];
2672
2673
        $renavamEspecie = [
2674
            1=>'PASSAGEIRO',
2675
            2=>'CARGA',
2676
            3=>'MISTO',
2677
            4=>'CORRIDA',
2678
            5=>'TRACAO',
2679
            6=>'ESPECIAL',
2680
            7=>'COLECAO'
2681
        ];
2682
2683
        $renavamTiposVeiculos = [
2684
            1=>'BICICLETA',
2685
            2=>'CICLOMOTOR',
2686
            3=>'MOTONETA',
2687
            4=>'MOTOCICLETA',
2688
            5=>'TRICICLO',
2689
            6=>'AUTOMOVEL',
2690
            7=>'MICROONIBUS',
2691
            8=>'ONIBUS',
2692
            9=>'BONDE',
2693
            10=>'REBOQUE',
2694
            11=>'SEMI-REBOQUE',
2695
            12=>'CHARRETE',
2696
            13=>'CAMIONETA',
2697
            14=>'CAMINHAO',
2698
            15=>'CARROCA',
2699
            16=>'CARRO DE MAO',
2700
            17=>'CAMINHAO TRATOR',
2701
            18=>'TRATOR DE RODAS',
2702
            19=>'TRATOR DE ESTEIRAS',
2703
            20=>'TRATOR MISTO',
2704
            21=>'QUADRICICLO',
2705
            22=>'CHASSI/PLATAFORMA',
2706
            23=>'CAMINHONETE',
2707
            24=>'SIDE-CAR',
2708
            25=>'UTILITARIO',
2709
            26=>'MOTOR-CASA'
2710
        ];
2711
2712
        $renavamTipoPintura = [
2713
            'F'=>'FOSCA',
2714
            'S'=>'SÓLIDA',
2715
            'P'=>'PEROLIZADA',
2716
            'M'=>'METALICA',
2717
        ];
2718
2719
        $veicProd = $prod->getElementsByTagName("veicProd")->item(0);
2720
2721
        $veiculoChassi = $veicProd->getElementsByTagName("chassi")->item(0)->nodeValue;
2722
        $veiculoCor = $veicProd->getElementsByTagName("xCor")->item(0)->nodeValue;
2723
        $veiculoCilindrada = $veicProd->getElementsByTagName("cilin")->item(0)->nodeValue;
2724
        $veiculoCmkg = $veicProd->getElementsByTagName("CMT")->item(0)->nodeValue;
2725
        $veiculoTipo = $veicProd->getElementsByTagName("tpVeic")->item(0)->nodeValue;
2726
2727
        $veiculoMotor = $veicProd->getElementsByTagName("nMotor")->item(0)->nodeValue;
2728
        $veiculoRenavam = $veicProd->getElementsByTagName("cMod")->item(0)->nodeValue;
2729
        $veiculoHp = $veicProd->getElementsByTagName("pot")->item(0)->nodeValue;
2730
        $veiculoPlaca = ''; //$veiculo->getElementsByTagName("CMT")->item(0)->nodeValue;
2731
        $veiculoTipoPintura = $veicProd->getElementsByTagName("tpPint")->item(0)->nodeValue;
2732
        $veiculoMarcaModelo = $prod->getElementsByTagName("xProd")->item(0)->nodeValue;
2733
        $veiculoEspecie = $veicProd->getElementsByTagName("espVeic")->item(0)->nodeValue;
2734
        $veiculoCombustivel = $veicProd->getElementsByTagName("tpComb")->item(0)->nodeValue;
2735
        $veiculoSerial = $veicProd->getElementsByTagName("nSerie")->item(0)->nodeValue;
2736
        $veiculoFabricacao = $veicProd->getElementsByTagName("anoFab")->item(0)->nodeValue;
2737
        $veiculoModelo = $veicProd->getElementsByTagName("anoMod")->item(0)->nodeValue;
2738
        $veiculoDistancia = $veicProd->getElementsByTagName("dist")->item(0)->nodeValue;
2739
2740
        $x = $oldX;
2741
2742
        $yVeic = $y + $h;
2743
        $texto = 'Chassi: ............: ' . $veiculoChassi;
2744
        $this->pdf->textBox($x, $yVeic, $w1+40, $h, $texto, $aFont, 'T', 'L', 0, '');
2745
        $yVeic += $h;
2746
        $texto = 'Cor...................: ' . $veiculoCor;
2747
        $this->pdf->textBox($x, $yVeic, $w1+40, $h, $texto, $aFont, 'T', 'L', 0, '');
2748
        $yVeic += $h;
2749
        $texto = 'Cilindrada........: ' . $veiculoCilindrada;
2750
        $this->pdf->textBox($x, $yVeic, $w1+40, $h, $texto, $aFont, 'T', 'L', 0, '');
2751
        $yVeic += $h;
2752
        $texto = 'Cmkg...............: ' . $veiculoCmkg;
2753
        $this->pdf->textBox($x, $yVeic, $w1+40, $h, $texto, $aFont, 'T', 'L', 0, '');
2754
        $yVeic += $h;
2755
        $texto = 'Tipo.................: ' . ($renavamTiposVeiculos[intval($veiculoTipo)] ?? $veiculoTipo);
2756
         $this->pdf->textBox($x, $yVeic, $w1+40, $h, $texto, $aFont, 'T', 'L', 0, '');
2757
        $yVeic = $y + $h;
2758
        $xVeic = $x + 65;
2759
        $texto = 'Nº Motor: .........: ' . $veiculoMotor;
2760
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2761
        $yVeic += $h;
2762
        $texto = 'Renavam...........: ' . $veiculoRenavam;
2763
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2764
        $yVeic += $h;
2765
        $texto = 'HP.....................: ' . $veiculoHp;
2766
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2767
        $yVeic += $h;
2768
        $texto = 'Placa.................: ' . $veiculoPlaca;
2769
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2770
        $yVeic += $h;
2771
        $texto = 'Tipo Pintura......: ' . ($renavamEspecie[intval($veiculoTipoPintura)] ?? $veiculoTipoPintura);
2772
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2773
        $yVeic = $y + $h;
2774
        $xVeic = $xVeic + 55;
2775
        $texto = 'Marca / Modelo.....: ' . $veiculoMarcaModelo;
2776
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2777
        $yVeic += $h;
2778
        $texto = 'Especie..................: ' . ($renavamEspecie[intval($veiculoEspecie)] ?? $veiculoEspecie);
2779
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2780
        $yVeic += $h;
2781
        $texto = 'Combustivel..........: ' . ($renavamCombustivel[intval($veiculoCombustivel)] ?? $veiculoCombustivel);
2782
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2783
        $yVeic += $h;
2784
        $texto = 'Serial.....................: ' . $veiculoSerial;
2785
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2786
        $yVeic += $h;
2787
        $texto = 'Ano Fab/Mod........: '. $veiculoFabricacao . '/' . $veiculoModelo;
2788
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2789
        $yVeic += $h;
2790
        $texto = 'Distancia Entre Eixos(mm)..: '. $veiculoDistancia;
2791
        $this->pdf->textBox($xVeic, $yVeic, $w1+50, $h, $texto, $aFont, 'T', 'L', 0, '');
2792
    }
2793
2794
    /**
2795
     * issqn
2796
     * Monta o campo de serviços do DANFE
2797
     *
2798
     * @name   issqn (retrato e paisagem)
2799
     * @param  float $x Posição horizontal canto esquerdo
2800
     * @param  float $y Posição vertical canto superior
2801
     * @return float Posição vertical final
2802
     */
2803
    protected function issqn($x, $y)
2804
    {
2805
        $oldX = $x;
2806
        //#####################################################################
2807
        //CÁLCULO DO ISSQN
2808
        $texto = "CÁLCULO DO ISSQN";
2809
        $w = $this->wPrint;
2810
        $h = 7;
2811
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
2812
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 0, '');
2813
        //INSCRIÇÃO MUNICIPAL
2814
        $y += 3;
2815
        $w = round($this->wPrint*0.23, 0);
2816
        $texto = 'INSCRIÇÃO MUNICIPAL';
2817
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2818
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2819
        //inscrição municipal
2820
        $texto = ! empty($this->emit->getElementsByTagName("IM")->item(0)->nodeValue) ?
2821
                $this->emit->getElementsByTagName("IM")->item(0)->nodeValue : '';
2822
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2823
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'L', 0, '');
2824
        //VALOR TOTAL DOS SERVIÇOS
2825
        $x += $w;
2826
        $texto = 'VALOR TOTAL DOS SERVIÇOS';
2827
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2828
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2829
        if (isset($this->ISSQNtot)) {
2830
            $texto = ! empty($this->ISSQNtot->getElementsByTagName("vServ")->item(0)->nodeValue) ?
2831
                    $this->ISSQNtot->getElementsByTagName("vServ")->item(0)->nodeValue : '';
2832
            $texto = number_format($texto, 2, ",", ".");
2833
        } else {
2834
            $texto = '';
2835
        }
2836
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2837
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'R', 0, '');
2838
        //BASE DE CÁLCULO DO ISSQN
2839
        $x += $w;
2840
        $texto = 'BASE DE CÁLCULO DO ISSQN';
2841
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2842
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2843
        if (isset($this->ISSQNtot)) {
2844
            $texto = ! empty($this->ISSQNtot->getElementsByTagName("vBC")->item(0)->nodeValue) ?
2845
                    $this->ISSQNtot->getElementsByTagName("vBC")->item(0)->nodeValue : '';
2846
            $texto = ! empty($texto) ? number_format($texto, 2, ",", ".") : '';
2847
        } else {
2848
            $texto = '';
2849
        }
2850
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2851
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'R', 0, '');
2852
        //VALOR TOTAL DO ISSQN
2853
        $x += $w;
2854
        if ($this->orientacao == 'P') {
2855
            $w = $this->wPrint - (3 * $w);
2856
        } else {
2857
            $w = $this->wPrint - (3 * $w)-$this->wCanhoto;
2858
        }
2859
        $texto = 'VALOR TOTAL DO ISSQN';
2860
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
2861
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2862
        if (isset($this->ISSQNtot)) {
2863
            $texto = ! empty($this->ISSQNtot->getElementsByTagName("vISS")->item(0)->nodeValue) ?
2864
                    $this->ISSQNtot->getElementsByTagName("vISS")->item(0)->nodeValue : '';
2865
            $texto = ! empty($texto) ? number_format($texto, 2, ",", ".") : '';
2866
        } else {
2867
            $texto = '';
2868
        }
2869
        $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
2870
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'B', 'R', 0, '');
2871
        return ($y+$h+1);
2872
    }
2873
2874
    /**
2875
     *dadosAdicionais
2876
     * Coloca o grupo de dados adicionais da NFe. (retrato e paisagem)
2877
     *
2878
     * @name   dadosAdicionais
2879
     * @param  float $x Posição horizontal canto esquerdo
2880
     * @param  float $y Posição vertical canto superior
2881
     * @param  float $h altura do campo
2882
     * @return float Posição vertical final (eixo Y)
2883
     */
2884
    protected function dadosAdicionais($x, $y, $h)
2885
    {
2886
        //##################################################################################
2887
        //DADOS ADICIONAIS
2888
        $texto = "DADOS ADICIONAIS";
2889
        if ($this->orientacao == 'P') {
2890
              $w = $this->wPrint;
2891
        } else {
2892
              $w = $this->wPrint-$this->wCanhoto;
2893
        }
2894
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>'B'];
2895
        $this->pdf->textBox($x, $y, $w, 8, $texto, $aFont, 'T', 'L', 0, '');
2896
        //INFORMAÇÕES COMPLEMENTARES
2897
        $texto = "INFORMAÇÕES COMPLEMENTARES";
2898
        $y += 3;
2899
        $w = $this->wAdic;
2900
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>'B'];
2901
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2902
        //o texto com os dados adicionais foi obtido na função montaDANFE
2903
        //e carregado em uma propriedade privada da classe
2904
        //$this->wAdic com a largura do campo
2905
        //$this->textoAdic com o texto completo do campo
2906
        $y += 1;
2907
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
2908
        $this->pdf->textBox($x, $y+2, $w-2, $h-3, $this->textoAdic, $aFont, 'T', 'L', 0, '', false);
2909
        //RESERVADO AO FISCO
2910
        $texto = "RESERVADO AO FISCO";
2911
        $x += $w;
2912
        $y -= 1;
2913
        if ($this->orientacao == 'P') {
2914
            $w = $this->wPrint-$w;
2915
        } else {
2916
            $w = $this->wPrint-$w-$this->wCanhoto;
2917
        }
2918
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>'B'];
2919
        $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'T', 'L', 1, '');
2920
        //inserir texto informando caso de contingência
2921
        // 1 - Normal - emissão normal;
2922
        // 2 - Contingência FS - emissão em contingência com impressão do DANFE em Formulário de Segurança;
2923
        // 3 - Contingência SCAN - emissão em contingência no Sistema de Contingência do Ambiente Nacional;
2924
        // 4 - Contingência DPEC - emissão em contingência com envio da Declaração
2925
        //     Prévia de Emissão em Contingência;
2926
        // 5 - Contingência FS-DA - emissão em contingência com impressão do DANFE em Formulário de
2927
        //     Segurança para Impressão de Documento Auxiliar de Documento Fiscal Eletrônico (FS-DA);
2928
        // 6 - Contingência SVC-AN
2929
        // 7 - Contingência SVC-RS
2930
        $xJust = $this->getTagValue($this->ide, 'xJust', 'Justificativa: ');
2931
        $dhCont = $this->getTagValue($this->ide, 'dhCont', ' Entrada em contingência : ');
2932
        $texto = '';
2933
        switch ($this->tpEmis) {
2934
            case 2:
2935
                $texto = 'CONTINGÊNCIA FS' . $dhCont . $xJust;
2936
                break;
2937
            case 3:
2938
                $texto = 'CONTINGÊNCIA SCAN' . $dhCont . $xJust;
2939
                break;
2940
            case 4:
2941
                $texto = 'CONTINGÊNCIA DPEC' . $dhCont . $xJust;
2942
                break;
2943
            case 5:
2944
                $texto = 'CONTINGÊNCIA FSDA' . $dhCont . $xJust;
2945
                break;
2946
            case 6:
2947
                $texto = 'CONTINGÊNCIA SVC-AN' . $dhCont . $xJust;
2948
                break;
2949
            case 7:
2950
                $texto = 'CONTINGÊNCIA SVC-RS' . $dhCont . $xJust;
2951
                break;
2952
        }
2953
        $y += 2;
2954
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
2955
        $this->pdf->textBox($x, $y, $w-2, $h-3, $texto, $aFont, 'T', 'L', 0, '', false);
2956
        return $y+$h;
2957
    }
2958
2959
    /**
2960
     * rodape
2961
     * Monta o rodapé no final da DANFE com a data/hora de impressão e informações
2962
     * sobre a API NfePHP
2963
     *
2964
     * @param  float $x  Posição horizontal canto esquerdo
2965
     *
2966
     * @return void
2967
     */
2968
    protected function rodape($x)
2969
    {
2970
        
2971
        $y = $this->maxH - 4;
2972
        if ($this->orientacao == 'P') {
2973
              $w = $this->wPrint;
2974
        } else {
2975
              $w = $this->wPrint-$this->wCanhoto;
2976
              $x = $this->wCanhoto;
2977
        }
2978
        $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>'I'];
2979
        $texto = "Impresso em ". date('d/m/Y') . " as " . date('H:i:s');
2980
        $this->pdf->textBox($x, $y, $w, 0, $texto, $aFont, 'T', 'L', false);
2981
        $texto = $this->creditos .  "  Powered by NFePHP®";
2982
        $this->pdf->textBox($x, $y, $w, 0, $texto, $aFont, 'T', 'R', false, '');
2983
    }
2984
2985
    /**
2986
     * pCcanhotoDANFE
2987
     * Monta o canhoto da DANFE (retrato e paisagem)
2988
     *
2989
     * @name   canhotoDANFE
2990
     * @param  number $x Posição horizontal canto esquerdo
2991
     * @param  number $y Posição vertical canto superior
2992
     * @return number Posição vertical final
2993
     *
2994
     * TODO 21/07/14 fmertins: quando orientação L-paisagem, o canhoto está sendo gerado incorretamente
2995
     */
2996
    protected function canhoto($x, $y)
2997
    {
2998
        $oldX = $x;
2999
        $oldY = $y;
3000
        //#################################################################################
3001
        //canhoto
3002
        //identificação do tipo de nf entrada ou saida
3003
        $tpNF = $this->ide->getElementsByTagName('tpNF')->item(0)->nodeValue;
3004
        if ($tpNF == '0') {
3005
            //NFe de Entrada
3006
            $emitente = '';
3007
            $emitente .= $this->dest->getElementsByTagName("xNome")->item(0)->nodeValue . " - ";
3008
            $emitente .= $this->enderDest->getElementsByTagName("xLgr")->item(0)->nodeValue . ", ";
3009
            $emitente .= $this->enderDest->getElementsByTagName("nro")->item(0)->nodeValue . " - ";
3010
            $emitente .= $this->getTagValue($this->enderDest, "xCpl", " - ", " ");
3011
            $emitente .= $this->enderDest->getElementsByTagName("xBairro")->item(0)->nodeValue . " ";
3012
            $emitente .= $this->enderDest->getElementsByTagName("xMun")->item(0)->nodeValue . "-";
3013
            $emitente .= $this->enderDest->getElementsByTagName("UF")->item(0)->nodeValue . "";
3014
            $destinatario = $this->emit->getElementsByTagName("xNome")->item(0)->nodeValue . " ";
3015
        } else {
3016
            //NFe de Saída
3017
            $emitente = $this->emit->getElementsByTagName("xNome")->item(0)->nodeValue . " ";
3018
            $destinatario = '';
3019
            $destinatario .= $this->dest->getElementsByTagName("xNome")->item(0)->nodeValue . " - ";
3020
            $destinatario .= $this->enderDest->getElementsByTagName("xLgr")->item(0)->nodeValue . ", ";
3021
            $destinatario .= $this->enderDest->getElementsByTagName("nro")->item(0)->nodeValue . " ";
3022
            $destinatario .= $this->getTagValue($this->enderDest, "xCpl", " - ", " ");
3023
            $destinatario .= $this->enderDest->getElementsByTagName("xBairro")->item(0)->nodeValue . " ";
3024
            $destinatario .= $this->enderDest->getElementsByTagName("xMun")->item(0)->nodeValue . "-";
3025
            $destinatario .= $this->enderDest->getElementsByTagName("UF")->item(0)->nodeValue . " ";
3026
        }
3027
        //identificação do sistema emissor
3028
        //linha separadora do canhoto
3029
        if ($this->orientacao == 'P') {
3030
            $w = round($this->wPrint * 0.81, 0);
3031
        } else {
3032
            //linha separadora do canhoto - 238
3033
            //posicao altura
3034
            $y = $this->wPrint-85;
3035
            //altura
3036
            $w = $this->wPrint-85-24;
3037
        }
3038
        $h = 10;
3039
        //desenha caixa
3040
        $texto = '';
3041
        $aFont = ['font'=>$this->fontePadrao, 'size'=>7, 'style'=>''];
3042
        $aFontSmall = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
3043
        if ($this->orientacao == 'P') {
3044
            $this->pdf->textBox($x, $y, $w, $h, $texto, $aFont, 'C', 'L', 1, '', false);
3045
        } else {
3046
            $this->pdf->textBox90($x, $y, $w, $h, $texto, $aFont, 'C', 'L', 1, '', false);
3047
        }
3048
        $numNF = str_pad($this->ide->getElementsByTagName('nNF')->item(0)->nodeValue, 9, "0", STR_PAD_LEFT);
3049
        $serie = str_pad($this->ide->getElementsByTagName('serie')->item(0)->nodeValue, 3, "0", STR_PAD_LEFT);
3050
        $texto = "RECEBEMOS DE ";
3051
        $texto .= $emitente;
3052
        $texto .= " OS PRODUTOS E/OU SERVIÇOS CONSTANTES DA NOTA FISCAL ELETRÔNICA INDICADA ";
3053
        if ($this->orientacao == 'P') {
3054
            $texto .= "ABAIXO";
3055
        } else {
3056
            $texto .= "AO LADO";
3057
        }
3058
        $texto .= ". EMISSÃO: ";
3059
        $dEmi = ! empty($this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue) ?
3060
                $this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue : '';
3061
        if ($dEmi == '') {
3062
            $dEmi = ! empty($this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue) ?
3063
                    $this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue : '';
3064
            $aDemi = explode('T', $dEmi);
3065
            $dEmi = $aDemi[0];
3066
        }
3067
        $texto .= $this->ymdTodmy($dEmi) ." ";
3068
        $texto .= "VALOR TOTAL: R$ ";
3069
        $texto .= number_format($this->ICMSTot->getElementsByTagName("vNF")->item(0)->nodeValue, 2, ",", ".") . " ";
3070
        $texto .= "DESTINATÁRIO: ";
3071
        $texto .= $destinatario;
3072
        if ($this->orientacao == 'P') {
3073
            $this->pdf->textBox($x, $y, $w-1, $h, $texto, $aFont, 'C', 'L', 0, '', false);
3074
            $x1 = $x + $w;
3075
            $w1 = $this->wPrint - $w;
3076
            $texto = "NF-e";
3077
            $aFont = ['font'=>$this->fontePadrao, 'size'=>14, 'style'=>'B'];
3078
            $this->pdf->textBox($x1, $y, $w1, 18, $texto, $aFont, 'T', 'C', 0, '');
3079
            $texto = "Nº. " . $this->formatField($numNF, "###.###.###") . " \n";
3080
            $texto .= "Série $serie";
3081
            $aFont = ['font'=>$this->fontePadrao, 'size'=>10, 'style'=>'B'];
3082
            $this->pdf->textBox($x1, $y, $w1, 18, $texto, $aFont, 'C', 'C', 1, '');
3083
            //DATA DE RECEBIMENTO
3084
            $texto = "DATA DE RECEBIMENTO";
3085
            $y += $h;
3086
            $w2 = round($this->wPrint*0.17, 0); //35;
3087
            $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
3088
            $this->pdf->textBox($x, $y, $w2, 8, $texto, $aFont, 'T', 'L', 1, '');
3089
            //IDENTIFICAÇÃO E ASSINATURA DO RECEBEDOR
3090
            $x += $w2;
3091
            $w3 = $w-$w2;
3092
            $texto = "IDENTIFICAÇÃO E ASSINATURA DO RECEBEDOR";
3093
            $this->pdf->textBox($x, $y, $w3, 8, $texto, $aFont, 'T', 'L', 1, '');
3094
            $x = $oldX;
3095
            $y += 9;
3096
            $this->pdf->dashedHLine($x, $y, $this->wPrint, 0.1, 80);
3097
            $y += 2;
3098
            return $y;
3099
        } else {
3100
            $x--;
3101
            $x = $this->pdf->textBox90($x, $y, $w-1, $h, $texto, $aFontSmall, 'C', 'L', 0, '', false);
3102
            //NUMERO DA NOTA FISCAL LOGO NFE
3103
            $w1 = 18;
3104
            $x1 = $oldX;
3105
            $y = $oldY;
3106
            $texto = "NF-e";
3107
            $aFont = ['font'=>$this->fontePadrao, 'size'=>14, 'style'=>'B'];
3108
            $this->pdf->textBox($x1, $y, $w1, 18, $texto, $aFont, 'T', 'C', 0, '');
3109
            $texto = "Nº.\n" . $this->formatField($numNF, "###.###.###") . " \n";
3110
            $texto .= "Série $serie";
3111
            $aFont = ['font'=>$this->fontePadrao, 'size'=>8, 'style'=>'B'];
3112
            $this->pdf->textBox($x1, $y, $w1, 18, $texto, $aFont, 'C', 'C', 1, '');
3113
            //DATA DO RECEBIMENTO
3114
            $texto = "DATA DO RECEBIMENTO";
3115
            $y = $this->wPrint-85;
3116
            $x = 12;
3117
            $w2 = round($this->wPrint*0.17, 0); //35;
3118
            $aFont = ['font'=>$this->fontePadrao, 'size'=>6, 'style'=>''];
3119
            $this->pdf->textBox90($x, $y, $w2, 8, $texto, $aFont, 'T', 'L', 1, '');
3120
            //IDENTIFICAÇÃO E ASSINATURA DO RECEBEDOR
3121
            $y -= $w2;
3122
            $w3 = $w-$w2;
3123
            $texto = "IDENTIFICAÇÃO E ASSINATURA DO RECEBEDOR";
3124
            $aFont = ['font'=>$this->fontePadrao, 'size'=>5.7, 'style'=>''];
3125
            $x = $this->pdf->textBox90($x, $y, $w3, 8, $texto, $aFont, 'T', 'L', 1, '');
3126
            $this->pdf->DashedVLine(23, $oldY, 0.1, $this->wPrint-20, 67);
3127
            return $x;
3128
        }
3129
    }
3130
3131
    /**
3132
     * geraInformacoesDaTagCompra
3133
     * Devolve uma string contendo informação sobre as tag <compra><xNEmp>, <xPed> e <xCont> ou string vazia.
3134
     * Aviso: Esta função não leva em consideração dados na tag xPed do item.
3135
     *
3136
     * @name   pGeraInformacoesDaTagCompra
3137
     * @return string com as informacoes dos pedidos.
3138
     */
3139
    protected function geraInformacoesDaTagCompra()
3140
    {
3141
        $saida = "";
3142
        if (isset($this->compra)) {
3143
            if (! empty($this->compra->getElementsByTagName("xNEmp")->item(0)->nodeValue)) {
3144
                $saida .= " Nota de Empenho: " . $this->compra->getElementsByTagName("xNEmp")->item(0)->nodeValue;
3145
            }
3146
            if (! empty($this->compra->getElementsByTagName("xPed")->item(0)->nodeValue)) {
3147
                $saida .= " Pedido: " . $this->compra->getElementsByTagName("xPed")->item(0)->nodeValue;
3148
            }
3149
            if (! empty($this->compra->getElementsByTagName("xCont")->item(0)->nodeValue)) {
3150
                $saida .= " Contrato: " . $this->compra->getElementsByTagName("xCont")->item(0)->nodeValue;
3151
            }
3152
        }
3153
        return $saida;
3154
    }
3155
3156
    /**
3157
     * geraChaveAdicionalDeContingencia
3158
     *
3159
     * @name   geraChaveAdicionalDeContingencia
3160
     * @return string chave
3161
     */
3162
    protected function geraChaveAdicionalDeContingencia()
3163
    {
3164
        //cUF tpEmis CNPJ vNF ICMSp ICMSs DD  DV
3165
        // Quantidade de caracteres  02   01      14  14    01    01  02 01
3166
        $forma  = "%02d%d%s%014d%01d%01d%02d";
3167
        $cUF    = $this->ide->getElementsByTagName('cUF')->item(0)->nodeValue;
3168
        $CNPJ   = "00000000000000" . $this->emit->getElementsByTagName('CNPJ')->item(0)->nodeValue;
3169
        $CNPJ   = substr($CNPJ, -14);
3170
        $vNF    = $this->ICMSTot->getElementsByTagName("vNF")->item(0)->nodeValue * 100;
3171
        $vICMS  = $this->ICMSTot->getElementsByTagName("vICMS")->item(0)->nodeValue;
3172
        if ($vICMS > 0) {
3173
            $vICMS = 1;
3174
        }
3175
        $icmss  = $this->ICMSTot->getElementsByTagName("vBC")->item(0)->nodeValue;
3176
        if ($icmss > 0) {
3177
            $icmss = 1;
3178
        }
3179
        $dEmi = ! empty($this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue) ?
3180
                $this->ide->getElementsByTagName("dEmi")->item(0)->nodeValue : '';
3181
        if ($dEmi == '') {
3182
            $dEmi = ! empty($this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue) ?
3183
                    $this->ide->getElementsByTagName("dhEmi")->item(0)->nodeValue : '';
3184
            $aDemi = explode('T', $dEmi);
3185
            $dEmi = $aDemi[0];
3186
        }
3187
        $dd  = $dEmi;
3188
        $rpos = strrpos($dd, '-');
3189
        $dd  = substr($dd, $rpos +1);
3190
        $chave = sprintf($forma, $cUF, $this->tpEmis, $CNPJ, $vNF, $vICMS, $icmss, $dd);
3191
        $chave = $chave . $this->modulo11($chave);
3192
        return $chave;
3193
    }
3194
3195
    /**
3196
     *  geraInformacoesDasNotasReferenciadas
3197
     * Devolve uma string contendo informação sobre as notas referenciadas. Suporta N notas, eletrônicas ou não
3198
     * Exemplo: NFe Ref.: série: 01 número: 01 emit: 11.111.111/0001-01
3199
     * em 10/2010 [0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000]
3200
     *
3201
     * @return string Informacoes a serem adicionadas no rodapé sobre notas referenciadas.
3202
     */
3203
    protected function geraInformacoesDasNotasReferenciadas()
3204
    {
3205
        $formaNfeRef = "\r\nNFe Ref.: série:%d número:%d emit:%s em %s [%s]";
3206
        $formaCTeRef = "\r\nCTe Ref.: série:%d número:%d emit:%s em %s [%s]";
3207
        $formaNfRef = "\r\nNF  Ref.: série:%d numero:%d emit:%s em %s modelo: %d";
3208
        $formaECFRef = "\r\nECF Ref.: modelo: %s ECF:%d COO:%d";
3209
        $formaNfpRef = "\r\nNFP Ref.: série:%d número:%d emit:%s em %s modelo: %d IE:%s";
3210
        $saida='';
3211
        $nfRefs = $this->ide->getElementsByTagName('NFref');
3212
        if (0 === $nfRefs->length) {
3213
            return $saida;
3214
        }
3215
        if ($nfRefs->length > 2) {
3216
            return 'Existem mais de 2 NF/NFe/ECF/NFP/CTe referenciadas, não serão exibidas na DANFE.';
3217
        }
3218
        foreach ($nfRefs as $nfRef) {
3219
            if (empty($nfRef)) {
3220
                continue;
3221
            }
3222
            $refNFe = $nfRef->getElementsByTagName('refNFe');
3223
            foreach ($refNFe as $chave_acessoRef) {
3224
                $chave_acesso = $chave_acessoRef->nodeValue;
3225
                $chave_acessoF = $this->formatField($chave_acesso, $this->formatoChave);
3226
                $data = substr($chave_acesso, 4, 2)."/20".substr($chave_acesso, 2, 2);
3227
                $cnpj = $this->formatField(substr($chave_acesso, 6, 14), "##.###.###/####-##");
3228
                $serie  = substr($chave_acesso, 22, 3);
3229
                $numero = substr($chave_acesso, 25, 9);
3230
                $saida .= sprintf($formaNfeRef, $serie, $numero, $cnpj, $data, $chave_acessoF);
3231
            }
3232
            $refNF = $nfRef->getElementsByTagName('refNF');
3233
            foreach ($refNF as $umaRefNFe) {
3234
                $data = $umaRefNFe->getElementsByTagName('AAMM')->item(0)->nodeValue;
3235
                $cnpj = $umaRefNFe->getElementsByTagName('CNPJ')->item(0)->nodeValue;
3236
                $mod = $umaRefNFe->getElementsByTagName('mod')->item(0)->nodeValue;
3237
                $serie = $umaRefNFe->getElementsByTagName('serie')->item(0)->nodeValue;
3238
                $numero = $umaRefNFe->getElementsByTagName('nNF')->item(0)->nodeValue;
3239
                $data = substr($data, 2, 2) . "/20" . substr($data, 0, 2);
3240
                $cnpj = $this->formatField($cnpj, "##.###.###/####-##");
3241
                $saida .= sprintf($formaNfRef, $serie, $numero, $cnpj, $data, $mod);
3242
            }
3243
            $refCTe = $nfRef->getElementsByTagName('refCTe');
3244
            foreach ($refCTe as $chave_acessoRef) {
3245
                $chave_acesso = $chave_acessoRef->nodeValue;
3246
                $chave_acessoF = $this->formatField($chave_acesso, $this->formatoChave);
3247
                $data = substr($chave_acesso, 4, 2)."/20".substr($chave_acesso, 2, 2);
3248
                $cnpj = $this->formatField(substr($chave_acesso, 6, 14), "##.###.###/####-##");
3249
                $serie  = substr($chave_acesso, 22, 3);
3250
                $numero = substr($chave_acesso, 25, 9);
3251
                $saida .= sprintf($formaCTeRef, $serie, $numero, $cnpj, $data, $chave_acessoF);
3252
            }
3253
            $refECF = $nfRef->getElementsByTagName('refECF');
3254
            foreach ($refECF as $umaRefNFe) {
3255
                $mod    = $umaRefNFe->getElementsByTagName('mod')->item(0)->nodeValue;
3256
                $nECF   = $umaRefNFe->getElementsByTagName('nECF')->item(0)->nodeValue;
3257
                $nCOO   = $umaRefNFe->getElementsByTagName('nCOO')->item(0)->nodeValue;
3258
                $saida .= sprintf($formaECFRef, $mod, $nECF, $nCOO);
3259
            }
3260
            $refNFP = $nfRef->getElementsByTagName('refNFP');
3261
            foreach ($refNFP as $umaRefNFe) {
3262
                $data = $umaRefNFe->getElementsByTagName('AAMM')->item(0)->nodeValue;
3263
                $cnpj = ! empty($umaRefNFe->getElementsByTagName('CNPJ')->item(0)->nodeValue) ?
3264
                    $umaRefNFe->getElementsByTagName('CNPJ')->item(0)->nodeValue :
3265
                    '';
3266
                $cpf = ! empty($umaRefNFe->getElementsByTagName('CPF')->item(0)->nodeValue) ?
3267
                        $umaRefNFe->getElementsByTagName('CPF')->item(0)->nodeValue : '';
3268
                $mod = $umaRefNFe->getElementsByTagName('mod')->item(0)->nodeValue;
3269
                $serie = $umaRefNFe->getElementsByTagName('serie')->item(0)->nodeValue;
3270
                $numero = $umaRefNFe->getElementsByTagName('nNF')->item(0)->nodeValue;
3271
                $ie = $umaRefNFe->getElementsByTagName('IE')->item(0)->nodeValue;
3272
                $data = substr($data, 2, 2) . "/20" . substr($data, 0, 2);
3273
                if ($cnpj == '') {
3274
                    $cpf_cnpj = $this->formatField($cpf, "###.###.###-##");
3275
                } else {
3276
                    $cpf_cnpj = $this->formatField($cnpj, "##.###.###/####-##");
3277
                }
3278
                $saida .= sprintf($formaNfpRef, $serie, $numero, $cpf_cnpj, $data, $mod, $ie);
3279
            }
3280
        }
3281
        return $saida;
3282
    }
3283
    
3284
    private function loadDoc($xml)
3285
    {
3286
        $this->xml = $xml;
3287
        if (!empty($xml)) {
3288
            $this->dom = new Dom();
3289
            $this->dom->loadXML($this->xml);
3290
            if (empty($this->dom->getElementsByTagName("infNFe")->item(0))) {
3291
                throw new \Exception('Isso não é um NFe.');
3292
            }
3293
            $this->nfeProc = $this->dom->getElementsByTagName("nfeProc")->item(0);
3294
            $this->infNFe = $this->dom->getElementsByTagName("infNFe")->item(0);
3295
            $this->ide = $this->dom->getElementsByTagName("ide")->item(0);
3296
            if ($this->getTagValue($this->ide, "mod") != '55') {
3297
                throw new \Exception("O xml deve ser NF-e modelo 55.");
3298
            }
3299
            $this->entrega = $this->dom->getElementsByTagName("entrega")->item(0);
3300
            $this->retirada = $this->dom->getElementsByTagName("retirada")->item(0);
3301
            $this->emit = $this->dom->getElementsByTagName("emit")->item(0);
3302
            $this->dest = $this->dom->getElementsByTagName("dest")->item(0);
3303
            $this->enderEmit = $this->dom->getElementsByTagName("enderEmit")->item(0);
3304
            $this->enderDest = $this->dom->getElementsByTagName("enderDest")->item(0);
3305
            $this->det = $this->dom->getElementsByTagName("det");
3306
            $this->cobr = $this->dom->getElementsByTagName("cobr")->item(0);
3307
            $this->dup = $this->dom->getElementsByTagName('dup');
3308
            $this->ICMSTot = $this->dom->getElementsByTagName("ICMSTot")->item(0);
3309
            $this->ISSQNtot = $this->dom->getElementsByTagName("ISSQNtot")->item(0);
3310
            $this->transp = $this->dom->getElementsByTagName("transp")->item(0);
3311
            $this->transporta = $this->dom->getElementsByTagName("transporta")->item(0);
3312
            $this->veicTransp = $this->dom->getElementsByTagName("veicTransp")->item(0);
3313
            $this->detPag = $this->dom->getElementsByTagName("detPag");
3314
            $this->reboque = $this->dom->getElementsByTagName("reboque")->item(0);
3315
            $this->infAdic = $this->dom->getElementsByTagName("infAdic")->item(0);
3316
            $this->compra = $this->dom->getElementsByTagName("compra")->item(0);
3317
            $this->tpEmis = $this->getTagValue($this->ide, "tpEmis");
3318
            $this->tpImp = $this->getTagValue($this->ide, "tpImp");
3319
            $this->infProt = $this->dom->getElementsByTagName("infProt")->item(0);
3320
        }
3321
    }
3322
    
3323
    private function imagePNGtoJPG($original)
3324
    {
3325
        $image = imagecreatefrompng($original);
3326
        ob_start();
3327
        imagejpeg($image, null, 100);
3328
        imagedestroy($image);
3329
        $stringdata = ob_get_contents(); // read from buffer
3330
        ob_end_clean();
3331
        return 'data://text/plain;base64,'.base64_encode($stringdata);
3332
    }
3333
}
3334