Completed
Push — master ( dbba02...2893fd )
by Roberto
03:33 queued 10s
created

Make::buildDet()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 153

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 1190

Importance

Changes 0
Metric Value
dl 0
loc 153
ccs 0
cts 98
cp 0
rs 0
c 0
b 0
f 0
cc 34
nc 4838401
nop 0
crap 1190

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace NFePHP\NFe;
4
5
/**
6
 * Classe a construção do xml da NFe modelo 55 e modelo 65
7
 * Esta classe basica está estruturada para montar XML da NFe para o
8
 * layout versão 4.00, os demais modelos serão derivados deste
9
 *
10
 * @category  API
11
 * @package   NFePHP\NFe\
12
 * @copyright Copyright (c) 2008-2019
13
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
14
 * @license   https://opensource.org/licenses/MIT MIT
15
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
16
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
17
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
18
 */
19
use NFePHP\Common\Keys;
20
use NFePHP\Common\DOMImproved as Dom;
21
use NFePHP\Common\Strings;
22
use NFePHP\NFe\Common\Gtin;
23
use stdClass;
24
use RuntimeException;
25
use InvalidArgumentException;
26
use DOMElement;
27
use DateTime;
28
29
class Make
30
{
31
32
    /**
33
     * @var array
34
     */
35
    public $errors = [];
36
    /**
37
     * @var string
38
     */
39
    public $chNFe;
40
    /**
41
     * @var string
42
     */
43
    public $xml;
44
    /**
45
     * @var string
46
     */
47
    protected $version;
48
    /**
49
     * @var integer
50
     */
51
    protected $mod = 55;
52
    /**
53
     * @var \NFePHP\Common\DOMImproved
54
     */
55
    public $dom;
56
    /**
57
     * @var integer
58
     */
59
    protected $tpAmb = 2;
60
    /**
61
     * @var DOMElement
62
     */
63
    protected $NFe;
64
    /**
65
     * @var DOMElement
66
     */
67
    protected $infNFe;
68
    /**
69
     * @var DOMElement
70
     */
71
    protected $ide;
72
    /**
73
     * @var DOMElement
74
     */
75
    protected $emit;
76
    /**
77
     * @var DOMElement
78
     */
79
    protected $enderEmit;
80
    /**
81
     * @var DOMElement
82
     */
83
    protected $dest;
84
    /**
85
     * @var DOMElement
86
     */
87
    protected $enderDest;
88
    /**
89
     * @var DOMElement
90
     */
91
    protected $retirada;
92
    /**
93
     * @var DOMElement
94
     */
95
    protected $entrega;
96
    /**
97
     * @var DOMElement
98
     */
99
    protected $total;
100
    /**
101
     * @var DOMElement
102
     */
103
    protected $cobr;
104
    /**
105
     * @var DOMElement
106
     */
107
    protected $transp;
108
    /**
109
     * @var DOMElement
110
     */
111
    protected $infAdic;
112
    /**
113
     * @var DOMElement
114
     */
115
    protected $exporta;
116
    /**
117
     * @var DOMElement
118
     */
119
    protected $compra;
120
    /**
121
     * @var DOMElement
122
     */
123
    protected $cana;
124
    /**
125
     * @var DOMElement
126
     */
127
    protected $infNFeSupl;
128
    /**
129
     * @var array of DOMElements
130
     */
131
    protected $aNFref = [];
132
    /**
133
     * @var array of DOMElements
134
     */
135
    protected $aDup = [];
136
    /**
137
     * @var DOMElement
138
     */
139
    protected $pag;
140
    /**
141
     * @var array of DOMElements
142
     */
143
    protected $aDetPag = [];
144
    /**
145
     * @var array of DOMElements
146
     */
147
    protected $aReboque = [];
148
    /**
149
     * @var array of DOMElements
150
     */
151
    protected $aVol = [];
152
    /**
153
     * @var array of DOMElements
154
     */
155
    protected $aAutXML = [];
156
    /**
157
     * @var array of DOMElements
158
     */
159
    protected $aDet = [];
160
    /**
161
     * @var array of DOMElements
162
     */
163
    protected $aProd = [];
164
    /**
165
     * @var array of DOMElements
166
     */
167
    protected $aRastro = [];
168
    /**
169
     * @var array of DOMElements
170
     */
171
    protected $aNVE = [];
172
    /**
173
     * @var array of DOMElements
174
     */
175
    protected $aCest = [];
176
    /**
177
     * @var array of DOMElements
178
     */
179
    protected $aRECOPI = [];
180
    /**
181
     * @var array of DOMElements
182
     */
183
    protected $aDetExport = [];
184
    /**
185
     * @var array of DOMElements
186
     */
187
    protected $aDI = [];
188
    /**
189
     * @var array of DOMElements
190
     */
191
    protected $aAdi = [];
192
    /**
193
     * @var array of DOMElements
194
     */
195
    protected $aVeicProd = [];
196
    /**
197
     * @var array of DOMElements
198
     */
199
    protected $aMed = [];
200
    /**
201
     * @var array of DOMElements
202
     */
203
    protected $aArma = [];
204
    /**
205
     * @var array of DOMElements
206
     */
207
    protected $aComb = [];
208
    /**
209
     * @var array of DOMElements
210
     */
211
    protected $aEncerrante = [];
212
    /**
213
     * @var array of DOMElements
214
     */
215
    protected $aImposto = [];
216
    /**
217
     * @var array of DOMElements
218
     */
219
    protected $aICMS = [];
220
    /**
221
     * @var array of DOMElements
222
     */
223
    protected $aICMSUFDest = [];
224
    /**
225
     * @var array of DOMElements
226
     */
227
    protected $aIPI = [];
228
    /**
229
     * @var array of DOMElements
230
     */
231
    protected $aII = [];
232
    /**
233
     * @var array of DOMElements
234
     */
235
    protected $aISSQN = [];
236
    /**
237
     * @var array
238
     */
239
    protected $aPIS = [];
240
    /**
241
     * @var array of DOMElements
242
     */
243
    protected $aPISST = [];
244
    /**
245
     * @var array of DOMElements
246
     */
247
    protected $aCOFINS = [];
248
    /**
249
     * @var array of DOMElements
250
     */
251
    protected $aCOFINSST = [];
252
    /**
253
     * @var array of DOMElements
254
     */
255
    protected $aImpostoDevol = [];
256
    /**
257
     * @var array of DOMElements
258
     */
259
    protected $aInfAdProd = [];
260
    /**
261
     * @var array of DOMElements
262
     */
263
    protected $aObsCont = [];
264
    /**
265
     * @var array of DOMElements
266
     */
267
    protected $aObsFisco = [];
268
    /**
269
     * @var array of DOMElements
270
     */
271
    protected $aProcRef = [];
272
    /**
273
     * @var stdClass
274
     */
275
    protected $stdTot;
276
    /**
277
     * @var DOMElement
278
     */
279
    protected $infRespTec;
280
    /**
281
     * @var string
282
     */
283
    protected $csrt;
284
    /**
285
     * @var boolean
286
     */
287
    protected $replaceAccentedChars = false;
288
289
    /**
290
     * Função construtora cria um objeto DOMDocument
291
     * que será carregado com o documento fiscal
292
     */
293 7
    public function __construct()
294
    {
295 7
        $this->dom = new Dom('1.0', 'UTF-8');
296 7
        $this->dom->preserveWhiteSpace = false;
297 7
        $this->dom->formatOutput = false;
298
        //elemento totalizador
299 7
        $this->stdTot = new \stdClass();
300 7
        $this->stdTot->vBC = 0;
301 7
        $this->stdTot->vICMS = 0;
302 7
        $this->stdTot->vICMSDeson = 0;
303 7
        $this->stdTot->vFCP = 0;
304 7
        $this->stdTot->vFCPUFDest = 0;
305 7
        $this->stdTot->vICMSUFDest = 0;
306 7
        $this->stdTot->vICMSUFRemet = 0;
307 7
        $this->stdTot->vBCST = 0;
308 7
        $this->stdTot->vST = 0;
309 7
        $this->stdTot->vFCPST = 0;
310 7
        $this->stdTot->vFCPSTRet = 0;
311 7
        $this->stdTot->vProd = 0;
312 7
        $this->stdTot->vFrete = 0;
313 7
        $this->stdTot->vSeg = 0;
314 7
        $this->stdTot->vDesc = 0;
315 7
        $this->stdTot->vII = 0;
316 7
        $this->stdTot->vIPI = 0;
317 7
        $this->stdTot->vIPIDevol = 0;
318 7
        $this->stdTot->vPIS = 0;
319 7
        $this->stdTot->vCOFINS = 0;
320 7
        $this->stdTot->vOutro = 0;
321 7
        $this->stdTot->vNF = 0;
322 7
        $this->stdTot->vTotTrib = 0;
323 7
    }
324
325
    /**
326
     * Set character convertion to ASCII only ou not
327
     * @param bool $option
328
     */
329
    public function setOnlyAscii($option = false)
330
    {
331
        $this->replaceAccentedChars = $option;
332
    }
333
334
    /**
335
     * Returns xml string and assembly it is necessary
336
     * @return string
337
     */
338
    public function getXML()
339
    {
340
        if (empty($this->xml)) {
341
            $this->montaNFe();
342
        }
343
        return $this->xml;
344
    }
345
346
    /**
347
     * Retorns the key number of NFe (44 digits)
348
     * @return string
349
     */
350 1
    public function getChave()
351
    {
352 1
        return $this->chNFe;
353
    }
354
355
    /**
356
     * Returns the model of NFe 55 or 65
357
     * @return int
358
     */
359
    public function getModelo()
360
    {
361
        return $this->mod;
362
    }
363
364
    /**
365
     * Call method of xml assembly. For compatibility only.
366
     * @return string
367
     */
368
    public function montaNFe()
369
    {
370
        return $this->monta();
371
    }
372
373
    /**
374
     * NFe xml mount method
375
     * this function returns TRUE on success or FALSE on error
376
     * The xml of the NFe must be retrieved by the getXML() function or
377
     * directly by the public property $xml
378
     *
379
     * @return string
380
     * @throws RuntimeException
381
     */
382
    public function monta()
383
    {
384
        $this->errors = $this->dom->errors;
385
        //cria a tag raiz da Nfe
386
        $this->buildNFe();
387
        //processa nfeRef e coloca as tags na tag ide
388
        foreach ($this->aNFref as $nfeRef) {
389
            $this->dom->appChild($this->ide, $nfeRef, 'Falta tag "ide"');
390
        }
391
        //monta as tags det e coloca no array $this->aDet com os detalhes dos produtos
392
        $this->buildDet();
393
        //[2] tag ide (5 B01)
394
        $this->dom->appChild($this->infNFe, $this->ide, 'Falta tag "infNFe"');
395
        //[8] tag emit (30 C01)
396
        $this->dom->appChild($this->infNFe, $this->emit, 'Falta tag "infNFe"');
397
        //[10] tag dest (62 E01)
398
        $this->dom->appChild($this->infNFe, $this->dest, 'Falta tag "infNFe"');
399
        //[12] tag retirada (80 F01)
400
        $this->dom->appChild($this->infNFe, $this->retirada, 'Falta tag "infNFe"');
401
        //[13] tag entrega (89 G01)
402
        $this->dom->appChild($this->infNFe, $this->entrega, 'Falta tag "infNFe"');
403
        //[14] tag autXML (97a.1 G50)
404
        foreach ($this->aAutXML as $aut) {
405
            $this->dom->appChild($this->infNFe, $aut, 'Falta tag "infNFe"');
406
        }
407
        //[14a] tag det (98 H01)
408
        foreach ($this->aDet as $det) {
409
            $this->dom->appChild($this->infNFe, $det, 'Falta tag "infNFe"');
410
        }
411
        //[28a] tag total (326 W01)
412
        $this->dom->appChild($this->infNFe, $this->total, 'Falta tag "infNFe"');
413
        //mota a tag vol
414
        $this->buildVol();
415
        //[32] tag transp (356 X01)
416
        $this->dom->appChild($this->infNFe, $this->transp, 'Falta tag "infNFe"');
417
        //[39a] tag cobr (389 Y01)
418
        $this->dom->appChild($this->infNFe, $this->cobr, 'Falta tag "infNFe"');
419
        //[42] tag pag (398a YA01)
420
        //processa Pag e coloca as tags na tag pag
421
        $this->buildTagPag();
422
        //[44] tag infAdic (399 Z01)
423
        $this->dom->appChild($this->infNFe, $this->infAdic, 'Falta tag "infNFe"');
424
        //[48] tag exporta (402 ZA01)
425
        $this->dom->appChild($this->infNFe, $this->exporta, 'Falta tag "infNFe"');
426
        //[49] tag compra (405 ZB01)
427
        $this->dom->appChild($this->infNFe, $this->compra, 'Falta tag "infNFe"');
428
        //[50] tag cana (409 ZC01)
429
        $this->dom->appChild($this->infNFe, $this->cana, 'Falta tag "infNFe"');
430
        //Responsável Técnico
431
        $this->dom->appChild($this->infNFe, $this->infRespTec, 'Falta tag "infNFe"');
432
        //[1] tag infNFe (1 A01)
433
        $this->dom->appChild($this->NFe, $this->infNFe, 'Falta tag "NFe"');
434
        //[0] tag NFe
435
        $this->dom->appendChild($this->NFe);
436
        // testa da chave
437
        $this->checkNFeKey($this->dom);
438
        $this->xml = $this->dom->saveXML();
439
        if (count($this->errors) > 0) {
440
            throw new RuntimeException('Existem erros nas tags. Obtenha os erros com getErrors().');
441
        }
442
        return $this->xml;
443
    }
444
445
    /**
446
     * Informações da NF-e A01 pai NFe
447
     * tag NFe/infNFe
448
     * @param  stdClass $std
449
     * @return DOMElement
450
     */
451 7
    public function taginfNFe(stdClass $std)
452
    {
453 7
        $possible = ['Id', 'versao', 'pk_nItem'];
454 7
        $std = $this->equilizeParameters($std, $possible);
455 7
        $chave = preg_replace('/[^0-9]/', '', $std->Id);
456 7
        $this->infNFe = $this->dom->createElement("infNFe");
457 7
        $this->infNFe->setAttribute("Id", 'NFe' . $chave);
458 7
        $this->infNFe->setAttribute(
459 7
            "versao",
460 7
            $std->versao
461
        );
462 7
        $this->version = $std->versao;
463 7
        if (!empty($std->pk_nItem)) {
464 1
            $this->infNFe->setAttribute("pk_nItem", $std->pk_nItem);
465
        }
466 7
        $this->chNFe = $chave;
467 7
        return $this->infNFe;
468
    }
469
470
    /**
471
     * Informações de identificação da NF-e B01 pai A01
472
     * NOTA: Ajustado para NT2016_002_v1.30
473
     * tag NFe/infNFe/ide
474
     * @param  stdClass $std
475
     * @return DOMElement
476
     */
477 4
    public function tagide(stdClass $std)
478
    {
479
        $possible = [
480 4
            'cUF',
481
            'cNF',
482
            'natOp',
483
            'indPag',
484
            'mod',
485
            'serie',
486
            'nNF',
487
            'dhEmi',
488
            'dhSaiEnt',
489
            'tpNF',
490
            'idDest',
491
            'cMunFG',
492
            'tpImp',
493
            'tpEmis',
494
            'cDV',
495
            'tpAmb',
496
            'finNFe',
497
            'indFinal',
498
            'indPres',
499
            'procEmi',
500
            'verProc',
501
            'dhCont',
502
            'xJust'
503
        ];
504 4
        $std = $this->equilizeParameters($std, $possible);
505
        
506 4
        if (empty($std->cNF)) {
507 1
            $std->cNF = Keys::random($std->nNF);
508
        }
509 4
        if (empty($std->cDV)) {
510 2
            $std->cDV = 0;
511
        }
512
        //validação conforme NT 2019.001
513 4
        $std->cNF = str_pad($std->cNF, 8, '0', STR_PAD_LEFT);
514 4
        if (intval($std->cNF) == intval($std->nNF)) {
515
            throw new InvalidArgumentException("O valor [{$std->cNF}] não é "
516
            . "aceitável para cNF, não pode ser igual ao de nNF, vide NT2019.001");
517
        }
518 4
        if (method_exists(Keys::class, 'cNFIsValid')) {
519 4
            if (!Keys::cNFIsValid($std->cNF)) {
520
                throw new InvalidArgumentException("O valor [{$std->cNF}] para cNF "
521
                . "é invalido, deve respeitar a NT2019.001");
522
            }
523
        }
524 4
        $this->tpAmb = $std->tpAmb;
525 4
        $this->mod = $std->mod;
526 4
        $identificador = 'B01 <ide> - ';
527 4
        $ide = $this->dom->createElement("ide");
528 4
        $this->dom->addChild(
529 4
            $ide,
530 4
            "cUF",
531 4
            $std->cUF,
532 4
            true,
533 4
            $identificador . "Código da UF do emitente do Documento Fiscal"
534
        );
535 4
        $this->dom->addChild(
536 4
            $ide,
537 4
            "cNF",
538 4
            $std->cNF,
539 4
            true,
540 4
            $identificador . "Código Numérico que compõe a Chave de Acesso"
541
        );
542 4
        $this->dom->addChild(
543 4
            $ide,
544 4
            "natOp",
545 4
            substr(trim($std->natOp), 0, 60),
546 4
            true,
547 4
            $identificador . "Descrição da Natureza da Operação"
548
        );
549 4
        $this->dom->addChild(
550 4
            $ide,
551 4
            "mod",
552 4
            $std->mod,
553 4
            true,
554 4
            $identificador . "Código do Modelo do Documento Fiscal"
555
        );
556 4
        $this->dom->addChild(
557 4
            $ide,
558 4
            "serie",
559 4
            $std->serie,
560 4
            true,
561 4
            $identificador . "Série do Documento Fiscal"
562
        );
563 4
        $this->dom->addChild(
564 4
            $ide,
565 4
            "nNF",
566 4
            $std->nNF,
567 4
            true,
568 4
            $identificador . "Número do Documento Fiscal"
569
        );
570 4
        $this->dom->addChild(
571 4
            $ide,
572 4
            "dhEmi",
573 4
            $std->dhEmi,
574 4
            true,
575 4
            $identificador . "Data e hora de emissão do Documento Fiscal"
576
        );
577 4
        if ($std->mod == '55' && !empty($std->dhSaiEnt)) {
578 1
            $this->dom->addChild(
579 1
                $ide,
580 1
                "dhSaiEnt",
581 1
                $std->dhSaiEnt,
582 1
                false,
583 1
                $identificador . "Data e hora de Saída ou da Entrada da Mercadoria/Produto"
584
            );
585
        }
586 4
        $this->dom->addChild(
587 4
            $ide,
588 4
            "tpNF",
589 4
            $std->tpNF,
590 4
            true,
591 4
            $identificador . "Tipo de Operação"
592
        );
593 4
        $this->dom->addChild(
594 4
            $ide,
595 4
            "idDest",
596 4
            $std->idDest,
597 4
            true,
598 4
            $identificador . "Identificador de local de destino da operação"
599
        );
600 4
        $this->dom->addChild(
601 4
            $ide,
602 4
            "cMunFG",
603 4
            $std->cMunFG,
604 4
            true,
605 4
            $identificador . "Código do Município de Ocorrência do Fato Gerador"
606
        );
607 4
        $this->dom->addChild(
608 4
            $ide,
609 4
            "tpImp",
610 4
            $std->tpImp,
611 4
            true,
612 4
            $identificador . "Formato de Impressão do DANFE"
613
        );
614 4
        $this->dom->addChild(
615 4
            $ide,
616 4
            "tpEmis",
617 4
            $std->tpEmis,
618 4
            true,
619 4
            $identificador . "Tipo de Emissão da NF-e"
620
        );
621 4
        $this->dom->addChild(
622 4
            $ide,
623 4
            "cDV",
624 4
            !empty($std->cDV) ? $std->cDV : '0',
625 4
            true,
626 4
            $identificador . "Dígito Verificador da Chave de Acesso da NF-e"
627
        );
628 4
        $this->dom->addChild(
629 4
            $ide,
630 4
            "tpAmb",
631 4
            $std->tpAmb,
632 4
            true,
633 4
            $identificador . "Identificação do Ambiente"
634
        );
635 4
        $this->dom->addChild(
636 4
            $ide,
637 4
            "finNFe",
638 4
            $std->finNFe,
639 4
            true,
640 4
            $identificador . "Finalidade de emissão da NF-e"
641
        );
642 4
        $this->dom->addChild(
643 4
            $ide,
644 4
            "indFinal",
645 4
            $std->indFinal,
646 4
            true,
647 4
            $identificador . "Indica operação com Consumidor final"
648
        );
649 4
        $this->dom->addChild(
650 4
            $ide,
651 4
            "indPres",
652 4
            $std->indPres,
653 4
            true,
654 4
            $identificador . "Indicador de presença do comprador no estabelecimento comercial no momento da operação"
655
        );
656 4
        $this->dom->addChild(
657 4
            $ide,
658 4
            "procEmi",
659 4
            $std->procEmi,
660 4
            true,
661 4
            $identificador . "Processo de emissão da NF-e"
662
        );
663 4
        $this->dom->addChild(
664 4
            $ide,
665 4
            "verProc",
666 4
            $std->verProc,
667 4
            true,
668 4
            $identificador . "Versão do Processo de emissão da NF-e"
669
        );
670 4
        if (!empty($std->dhCont) && !empty($std->xJust)) {
671 1
            $this->dom->addChild(
672 1
                $ide,
673 1
                "dhCont",
674 1
                $std->dhCont,
675 1
                true,
676 1
                $identificador . "Data e Hora da entrada em contingência"
677
            );
678 1
            $this->dom->addChild(
679 1
                $ide,
680 1
                "xJust",
681 1
                substr(trim($std->xJust), 0, 256),
682 1
                true,
683 1
                $identificador . "Justificativa da entrada em contingência"
684
            );
685
        }
686 4
        $this->ide = $ide;
687 4
        return $ide;
688
    }
689
690
    /**
691
     * Chave de acesso da NF-e referenciada BA02 pai BA01
692
     * tag NFe/infNFe/ide/NFref/refNFe
693
     * @param  stdClass $std
694
     * @return DOMElement
695
     */
696
    public function tagrefNFe(stdClass $std)
697
    {
698
        $possible = ['refNFe'];
699
        $std = $this->equilizeParameters($std, $possible);
700
701
        $num = $this->buildNFref();
702
        $refNFe = $this->dom->createElement("refNFe", $std->refNFe);
703
        $this->dom->appChild($this->aNFref[$num - 1], $refNFe);
704
        return $refNFe;
705
    }
706
707
    /**
708
     * Informação da NF modelo 1/1A referenciada BA03 pai BA01
709
     * tag NFe/infNFe/ide/NFref/NF DOMNode
710
     * @param  stdClass $std
711
     * @return DOMElement
712
     */
713
    public function tagrefNF(stdClass $std)
714
    {
715
        $possible = ['cUF', 'AAMM', 'CNPJ', 'mod', 'serie', 'nNF'];
716
        $std = $this->equilizeParameters($std, $possible);
717
718
        $identificador = 'BA03 <refNF> - ';
719
        $num = $this->buildNFref();
720
        $refNF = $this->dom->createElement("refNF");
721
        $this->dom->addChild(
722
            $refNF,
723
            "cUF",
724
            $std->cUF,
725
            true,
726
            $identificador . "Código da UF do emitente"
727
        );
728
        $this->dom->addChild(
729
            $refNF,
730
            "AAMM",
731
            $std->AAMM,
732
            true,
733
            $identificador . "Ano e Mês de emissão da NF-e"
734
        );
735
        $this->dom->addChild(
736
            $refNF,
737
            "CNPJ",
738
            $std->CNPJ,
739
            true,
740
            $identificador . "CNPJ do emitente"
741
        );
742
        $this->dom->addChild(
743
            $refNF,
744
            "mod",
745
            $std->mod,
746
            true,
747
            $identificador . "Modelo do Documento Fiscal"
748
        );
749
        $this->dom->addChild(
750
            $refNF,
751
            "serie",
752
            $std->serie,
753
            true,
754
            $identificador . "Série do Documento Fiscal"
755
        );
756
        $this->dom->addChild(
757
            $refNF,
758
            "nNF",
759
            $std->nNF,
760
            true,
761
            $identificador . "Número do Documento Fiscal"
762
        );
763
        $this->dom->appChild($this->aNFref[$num - 1], $refNF);
764
        return $refNF;
765
    }
766
767
    /**
768
     * Informações da NF de produtor rural referenciada BA10 pai BA01
769
     * tag NFe/infNFe/ide/NFref/refNFP
770
     * @param  stdClass $std
771
     * @return DOMElement
772
     */
773
    public function tagrefNFP(stdClass $std)
774
    {
775
        $possible = [
776
            'cUF',
777
            'AAMM',
778
            'CNPJ',
779
            'CPF',
780
            'IE',
781
            'mod',
782
            'serie',
783
            'nNF'
784
        ];
785
        $std = $this->equilizeParameters($std, $possible);
786
787
        $identificador = 'BA10 <refNFP> - ';
788
        $num = $this->buildNFref();
789
        $refNFP = $this->dom->createElement("refNFP");
790
        $this->dom->addChild(
791
            $refNFP,
792
            "cUF",
793
            $std->cUF,
794
            true,
795
            $identificador . "Código da UF do emitente"
796
        );
797
        $this->dom->addChild(
798
            $refNFP,
799
            "AAMM",
800
            $std->AAMM,
801
            true,
802
            $identificador . "AAMM da emissão da NF de produtor"
803
        );
804
        $this->dom->addChild(
805
            $refNFP,
806
            "CNPJ",
807
            $std->CNPJ,
808
            false,
809
            $identificador . "Informar o CNPJ do emitente da NF de produtor"
810
        );
811
        $this->dom->addChild(
812
            $refNFP,
813
            "CPF",
814
            $std->CPF,
815
            false,
816
            $identificador . "Informar o CPF do emitente da NF de produtor"
817
        );
818
        $this->dom->addChild(
819
            $refNFP,
820
            "IE",
821
            $std->IE,
822
            true,
823
            $identificador . "Informar a IE do emitente da NF de Produtor ou o literal 'ISENTO'"
824
        );
825
        $this->dom->addChild(
826
            $refNFP,
827
            "mod",
828
            str_pad($std->mod, 2, '0', STR_PAD_LEFT),
829
            true,
830
            $identificador . "Modelo do Documento Fiscal"
831
        );
832
        $this->dom->addChild(
833
            $refNFP,
834
            "serie",
835
            $std->serie,
836
            true,
837
            $identificador . "Série do Documento Fiscal"
838
        );
839
        $this->dom->addChild(
840
            $refNFP,
841
            "nNF",
842
            $std->nNF,
843
            true,
844
            $identificador . "Número do Documento Fiscal"
845
        );
846
        $this->dom->appChild($this->aNFref[$num - 1], $refNFP);
847
        return $refNFP;
848
    }
849
850
    /**
851
     * Chave de acesso do CT-e referenciada BA19 pai BA01
852
     * tag NFe/infNFe/ide/NFref/refCTe
853
     * @param  stdClass $std
854
     * @return DOMElement
855
     */
856
    public function tagrefCTe(stdClass $std)
857
    {
858
        $possible = ['refCTe'];
859
        $std = $this->equilizeParameters($std, $possible);
860
861
        $num = $this->buildNFref();
862
        $refCTe = $this->dom->createElement("refCTe", $std->refCTe);
863
        $this->dom->appChild($this->aNFref[$num - 1], $refCTe);
864
        return $refCTe;
865
    }
866
867
    /**
868
     * Informações do Cupom Fiscal referenciado BA20 pai BA01
869
     * tag NFe/infNFe/ide/NFref/refECF
870
     * @param stdClass $std
871
     * @return DOMElement
872
     */
873
    public function tagrefECF(stdClass $std)
874
    {
875
        $possible = ['mod', 'nECF', 'nCOO'];
876
        $std = $this->equilizeParameters($std, $possible);
877
878
        $identificador = 'BA20 <refECF> - ';
879
        $num = $this->buildNFref();
880
        $refECF = $this->dom->createElement("refECF");
881
        $this->dom->addChild(
882
            $refECF,
883
            "mod",
884
            $std->mod,
885
            true,
886
            $identificador . "Modelo do Documento Fiscal"
887
        );
888
        $this->dom->addChild(
889
            $refECF,
890
            "nECF",
891
            str_pad($std->nECF, 3, '0', STR_PAD_LEFT),
892
            true,
893
            $identificador . "Número de ordem sequencial do ECF"
894
        );
895
        $this->dom->addChild(
896
            $refECF,
897
            "nCOO",
898
            str_pad($std->nCOO, 6, '0', STR_PAD_LEFT),
899
            true,
900
            $identificador . "Número do Contador de Ordem de Operação - COO"
901
        );
902
        $this->dom->appChild($this->aNFref[$num - 1], $refECF);
903
        return $refECF;
904
    }
905
906
    /**
907
     * Identificação do emitente da NF-e C01 pai A01
908
     * tag NFe/infNFe/emit
909
     * @param  stdClass $std
910
     * @return DOMElement
911
     */
912
    public function tagemit(stdClass $std)
913
    {
914
        $possible = [
915
            'xNome',
916
            'xFant',
917
            'IE',
918
            'IEST',
919
            'IM',
920
            'CNAE',
921
            'CRT',
922
            'CNPJ',
923
            'CPF'
924
        ];
925
        $std = $this->equilizeParameters($std, $possible);
926
        $identificador = 'C01 <emit> - ';
927
        $this->emit = $this->dom->createElement("emit");
928
        $this->dom->addChild(
929
            $this->emit,
930
            "CNPJ",
931
            Strings::onlyNumbers($std->CNPJ),
932
            false,
933
            $identificador . "CNPJ do emitente"
934
        );
935
        $this->dom->addChild(
936
            $this->emit,
937
            "CPF",
938
            Strings::onlyNumbers($std->CPF),
939
            false,
940
            $identificador . "CPF do remetente"
941
        );
942
        $this->dom->addChild(
943
            $this->emit,
944
            "xNome",
945
            substr(trim($std->xNome), 0, 60),
946
            true,
947
            $identificador . "Razão Social ou Nome do emitente"
948
        );
949
        $this->dom->addChild(
950
            $this->emit,
951
            "xFant",
952
            substr(trim($std->xFant), 0, 60),
953
            false,
954
            $identificador . "Nome fantasia do emitente"
955
        );
956
        if ($std->IE != 'ISENTO') {
957
            $std->IE = Strings::onlyNumbers($std->IE);
958
        }
959
        $this->dom->addChild(
960
            $this->emit,
961
            "IE",
962
            $std->IE,
963
            true,
964
            $identificador . "Inscrição Estadual do emitente"
965
        );
966
        $this->dom->addChild(
967
            $this->emit,
968
            "IEST",
969
            Strings::onlyNumbers($std->IEST),
970
            false,
971
            $identificador . "IE do Substituto Tributário do emitente"
972
        );
973
        $this->dom->addChild(
974
            $this->emit,
975
            "IM",
976
            Strings::onlyNumbers($std->IM),
977
            false,
978
            $identificador . "Inscrição Municipal do Prestador de Serviço do emitente"
979
        );
980
        $this->dom->addChild(
981
            $this->emit,
982
            "CNAE",
983
            Strings::onlyNumbers($std->CNAE),
984
            false,
985
            $identificador . "CNAE fiscal do emitente"
986
        );
987
        $this->dom->addChild(
988
            $this->emit,
989
            "CRT",
990
            $std->CRT,
991
            true,
992
            $identificador . "Código de Regime Tributário do emitente"
993
        );
994
        return $this->emit;
995
    }
996
997
    /**
998
     * Endereço do emitente C05 pai C01
999
     * tag NFe/infNFe/emit/endEmit
1000
     * @param stdClass $std
1001
     * @return DOMElement
1002
     */
1003
    public function tagenderEmit(stdClass $std)
1004
    {
1005
        $possible = [
1006
            'xLgr',
1007
            'nro',
1008
            'xCpl',
1009
            'xBairro',
1010
            'cMun',
1011
            'xMun',
1012
            'UF',
1013
            'CEP',
1014
            'cPais',
1015
            'xPais',
1016
            'fone'
1017
        ];
1018
        $std = $this->equilizeParameters($std, $possible);
1019
1020
        $identificador = 'C05 <enderEmit> - ';
1021
        $this->enderEmit = $this->dom->createElement("enderEmit");
1022
        $this->dom->addChild(
1023
            $this->enderEmit,
1024
            "xLgr",
1025
            substr(trim($std->xLgr), 0, 60),
1026
            true,
1027
            $identificador . "Logradouro do Endereço do emitente"
1028
        );
1029
        $this->dom->addChild(
1030
            $this->enderEmit,
1031
            "nro",
1032
            substr(trim($std->nro), 0, 60),
1033
            true,
1034
            $identificador . "Número do Endereço do emitente"
1035
        );
1036
        $this->dom->addChild(
1037
            $this->enderEmit,
1038
            "xCpl",
1039
            substr(trim($std->xCpl), 0, 60),
1040
            false,
1041
            $identificador . "Complemento do Endereço do emitente"
1042
        );
1043
        $this->dom->addChild(
1044
            $this->enderEmit,
1045
            "xBairro",
1046
            substr(trim($std->xBairro), 0, 60),
1047
            true,
1048
            $identificador . "Bairro do Endereço do emitente"
1049
        );
1050
        $this->dom->addChild(
1051
            $this->enderEmit,
1052
            "cMun",
1053
            Strings::onlyNumbers($std->cMun),
1054
            true,
1055
            $identificador . "Código do município do Endereço do emitente"
1056
        );
1057
        $this->dom->addChild(
1058
            $this->enderEmit,
1059
            "xMun",
1060
            substr(trim($std->xMun), 0, 60),
1061
            true,
1062
            $identificador . "Nome do município do Endereço do emitente"
1063
        );
1064
        $this->dom->addChild(
1065
            $this->enderEmit,
1066
            "UF",
1067
            strtoupper(trim($std->UF)),
1068
            true,
1069
            $identificador . "Sigla da UF do Endereço do emitente"
1070
        );
1071
        $this->dom->addChild(
1072
            $this->enderEmit,
1073
            "CEP",
1074
            Strings::onlyNumbers($std->CEP),
1075
            true,
1076
            $identificador . "Código do CEP do Endereço do emitente"
1077
        );
1078
        $this->dom->addChild(
1079
            $this->enderEmit,
1080
            "cPais",
1081
            Strings::onlyNumbers($std->cPais),
1082
            false,
1083
            $identificador . "Código do País do Endereço do emitente"
1084
        );
1085
        $this->dom->addChild(
1086
            $this->enderEmit,
1087
            "xPais",
1088
            substr(trim($std->xPais), 0, 60),
1089
            false,
1090
            $identificador . "Nome do País do Endereço do emitente"
1091
        );
1092
        $this->dom->addChild(
1093
            $this->enderEmit,
1094
            "fone",
1095
            trim($std->fone),
1096
            false,
1097
            $identificador . "Telefone do Endereço do emitente"
1098
        );
1099
        $node = $this->emit->getElementsByTagName("IE")->item(0);
1100
        $this->emit->insertBefore($this->enderEmit, $node);
1101
        return $this->enderEmit;
1102
    }
1103
1104
    /**
1105
     * Identificação do Destinatário da NF-e E01 pai A01
1106
     * tag NFe/infNFe/dest (opcional para modelo 65)
1107
     * @param stdClass $std
1108
     * @return DOMElement
1109
     */
1110
    public function tagdest(stdClass $std)
1111
    {
1112
        $possible = [
1113
            'xNome',
1114
            'indIEDest',
1115
            'IE',
1116
            'ISUF',
1117
            'IM',
1118
            'email',
1119
            'CNPJ',
1120
            'CPF',
1121
            'idEstrangeiro'
1122
        ];
1123
        $std = $this->equilizeParameters($std, $possible);
1124
        $identificador = 'E01 <dest> - ';
1125
        $flagNome = true; //marca se xNome é ou não obrigatório
1126
        $temIE = $std->IE != '' && $std->IE != 'ISENTO'; // Tem inscrição municipal
1127
        $this->dest = $this->dom->createElement("dest");
1128
        if (!$temIE && $std->indIEDest == 1) {
1129
            $std->indIEDest = 2;
1130
        }
1131
        if ($this->mod == '65') {
1132
            $std->indIEDest = 9;
1133
            if ($std->xNome == '') {
1134
                $flagNome = false; //marca se xNome é ou não obrigatório
1135
            }
1136
        }
1137
        $xNome = $std->xNome;
1138
        if ($this->tpAmb == '2') {
1139
            $xNome = 'NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL';
1140
            //a exigência do CNPJ 99999999000191 não existe mais
1141
        }
1142
        if (!empty($std->CNPJ)) {
1143
            $this->dom->addChild(
1144
                $this->dest,
1145
                "CNPJ",
1146
                Strings::onlyNumbers($std->CNPJ),
1147
                true,
1148
                $identificador . "CNPJ do destinatário"
1149
            );
1150
        } elseif (!empty($std->CPF)) {
1151
            $this->dom->addChild(
1152
                $this->dest,
1153
                "CPF",
1154
                Strings::onlyNumbers($std->CPF),
1155
                true,
1156
                $identificador . "CPF do destinatário"
1157
            );
1158
        } elseif ($std->idEstrangeiro !== null) {
1159
            $this->dom->addChild(
1160
                $this->dest,
1161
                "idEstrangeiro",
1162
                $std->idEstrangeiro,
1163
                true,
1164
                $identificador . "Identificação do destinatário no caso de comprador estrangeiro",
1165
                true
1166
            );
1167
            $std->indIEDest = '9';
1168
        }
1169
        $this->dom->addChild(
1170
            $this->dest,
1171
            "xNome",
1172
            substr(trim($xNome), 0, 60),
1173
            $flagNome, //se mod 55 true ou mod 65 false
1174
            $identificador . "Razão Social ou nome do destinatário"
1175
        );
1176
        $this->dom->addChild(
1177
            $this->dest,
1178
            "indIEDest",
1179
            Strings::onlyNumbers($std->indIEDest),
1180
            true,
1181
            $identificador . "Indicador da IE do Destinatário"
1182
        );
1183
        if ($temIE) {
1184
            $this->dom->addChild(
1185
                $this->dest,
1186
                "IE",
1187
                $std->IE,
1188
                true,
1189
                $identificador . "Inscrição Estadual do Destinatário"
1190
            );
1191
        }
1192
        $this->dom->addChild(
1193
            $this->dest,
1194
            "ISUF",
1195
            Strings::onlyNumbers($std->ISUF),
1196
            false,
1197
            $identificador . "Inscrição na SUFRAMA do destinatário"
1198
        );
1199
        $this->dom->addChild(
1200
            $this->dest,
1201
            "IM",
1202
            Strings::onlyNumbers($std->IM),
1203
            false,
1204
            $identificador . "Inscrição Municipal do Tomador do Serviço do destinatário"
1205
        );
1206
        $this->dom->addChild(
1207
            $this->dest,
1208
            "email",
1209
            substr(trim($std->email), 0, 60),
1210
            false,
1211
            $identificador . "Email do destinatário"
1212
        );
1213
        return $this->dest;
1214
    }
1215
1216
    /**
1217
     * Endereço do Destinatário da NF-e E05 pai E01
1218
     * tag NFe/infNFe/dest/enderDest  (opcional para modelo 65)
1219
     * Os dados do destinatário devem ser inseridos antes deste método
1220
     * @param stdClass $std
1221
     * @return DOMElement
1222
     */
1223
    public function tagenderDest(stdClass $std)
1224
    {
1225
        $possible = [
1226
            'xLgr',
1227
            'nro',
1228
            'xCpl',
1229
            'xBairro',
1230
            'cMun',
1231
            'xMun',
1232
            'UF',
1233
            'CEP',
1234
            'cPais',
1235
            'xPais',
1236
            'fone'
1237
        ];
1238
        $std = $this->equilizeParameters($std, $possible);
1239
1240
        $identificador = 'E05 <enderDest> - ';
1241
        if (empty($this->dest)) {
1242
            throw new RuntimeException('A TAG dest deve ser criada antes do endereço do mesmo.');
1243
        }
1244
        $this->enderDest = $this->dom->createElement("enderDest");
1245
        $this->dom->addChild(
1246
            $this->enderDest,
1247
            "xLgr",
1248
            $std->xLgr,
1249
            true,
1250
            $identificador . "Logradouro do Endereço do Destinatário"
1251
        );
1252
        $this->dom->addChild(
1253
            $this->enderDest,
1254
            "nro",
1255
            $std->nro,
1256
            true,
1257
            $identificador . "Número do Endereço do Destinatário"
1258
        );
1259
        $this->dom->addChild(
1260
            $this->enderDest,
1261
            "xCpl",
1262
            $std->xCpl,
1263
            false,
1264
            $identificador . "Complemento do Endereço do Destinatário"
1265
        );
1266
        $this->dom->addChild(
1267
            $this->enderDest,
1268
            "xBairro",
1269
            $std->xBairro,
1270
            true,
1271
            $identificador . "Bairro do Endereço do Destinatário"
1272
        );
1273
        $this->dom->addChild(
1274
            $this->enderDest,
1275
            "cMun",
1276
            $std->cMun,
1277
            true,
1278
            $identificador . "Código do município do Endereço do Destinatário"
1279
        );
1280
        $this->dom->addChild(
1281
            $this->enderDest,
1282
            "xMun",
1283
            $std->xMun,
1284
            true,
1285
            $identificador . "Nome do município do Endereço do Destinatário"
1286
        );
1287
        $this->dom->addChild(
1288
            $this->enderDest,
1289
            "UF",
1290
            $std->UF,
1291
            true,
1292
            $identificador . "Sigla da UF do Endereço do Destinatário"
1293
        );
1294
        $this->dom->addChild(
1295
            $this->enderDest,
1296
            "CEP",
1297
            $std->CEP,
1298
            false,
1299
            $identificador . "Código do CEP do Endereço do Destinatário"
1300
        );
1301
        $this->dom->addChild(
1302
            $this->enderDest,
1303
            "cPais",
1304
            $std->cPais,
1305
            false,
1306
            $identificador . "Código do País do Endereço do Destinatário"
1307
        );
1308
        $this->dom->addChild(
1309
            $this->enderDest,
1310
            "xPais",
1311
            $std->xPais,
1312
            false,
1313
            $identificador . "Nome do País do Endereço do Destinatário"
1314
        );
1315
        $this->dom->addChild(
1316
            $this->enderDest,
1317
            "fone",
1318
            $std->fone,
1319
            false,
1320
            $identificador . "Telefone do Endereço do Destinatário"
1321
        );
1322
        $node = $this->dest->getElementsByTagName("indIEDest")->item(0);
1323
        if (!isset($node)) {
1324
            $node = $this->dest->getElementsByTagName("IE")->item(0);
1325
        }
1326
        $this->dest->insertBefore($this->enderDest, $node);
1327
        return $this->enderDest;
1328
    }
1329
1330
    /**
1331
     * Identificação do Local de retirada F01 pai A01
1332
     * tag NFe/infNFe/retirada (opcional)
1333
     * NOTA: ajustado para NT 2018.005
1334
     * @param stdClass $std
1335
     * @return DOMElement
1336
     */
1337
    public function tagretirada(stdClass $std)
1338
    {
1339
        $possible = [
1340
            'xLgr',
1341
            'nro',
1342
            'xCpl',
1343
            'xBairro',
1344
            'cMun',
1345
            'xMun',
1346
            'UF',
1347
            'CNPJ',
1348
            'CPF',
1349
            'xNome',
1350
            'CEP',
1351
            'cPais',
1352
            'xPais',
1353
            'fone',
1354
            'email',
1355
            'IE'
1356
        ];
1357
        $std = $this->equilizeParameters($std, $possible);
1358
        $identificador = 'F01 <retirada> - ';
1359
        $this->retirada = $this->dom->createElement("retirada");
1360
        $this->dom->addChild(
1361
            $this->retirada,
1362
            "CNPJ",
1363
            $std->CNPJ,
1364
            false,
1365
            $identificador . "CNPJ do Cliente da Retirada"
1366
        );
1367
        $this->dom->addChild(
1368
            $this->retirada,
1369
            "CPF",
1370
            $std->CPF,
1371
            false,
1372
            $identificador . "CPF do Cliente da Retirada"
1373
        );
1374
        $this->dom->addChild(
1375
            $this->retirada,
1376
            "xNome",
1377
            $std->xNome,
1378
            false,
1379
            $identificador . "Nome do Cliente da Retirada"
1380
        );
1381
        $this->dom->addChild(
1382
            $this->retirada,
1383
            "xLgr",
1384
            $std->xLgr,
1385
            true,
1386
            $identificador . "Logradouro do Endereco do Cliente da Retirada"
1387
        );
1388
        $this->dom->addChild(
1389
            $this->retirada,
1390
            "nro",
1391
            $std->nro,
1392
            true,
1393
            $identificador . "Número do Endereco do Cliente da Retirada"
1394
        );
1395
        $this->dom->addChild(
1396
            $this->retirada,
1397
            "xCpl",
1398
            $std->xCpl,
1399
            false,
1400
            $identificador . "Complemento do Endereco do Cliente da Retirada"
1401
        );
1402
        $this->dom->addChild(
1403
            $this->retirada,
1404
            "xBairro",
1405
            $std->xBairro,
1406
            true,
1407
            $identificador . "Bairro do Endereco do Cliente da Retirada"
1408
        );
1409
        $this->dom->addChild(
1410
            $this->retirada,
1411
            "cMun",
1412
            $std->cMun,
1413
            true,
1414
            $identificador . "Código do município do Endereco do Cliente da Retirada"
1415
        );
1416
        $this->dom->addChild(
1417
            $this->retirada,
1418
            "xMun",
1419
            $std->xMun,
1420
            true,
1421
            $identificador . "Nome do município do Endereco do Cliente da Retirada"
1422
        );
1423
        $this->dom->addChild(
1424
            $this->retirada,
1425
            "UF",
1426
            $std->UF,
1427
            true,
1428
            $identificador . "Sigla da UF do Endereco do Cliente da Retirada"
1429
        );
1430
        $this->dom->addChild(
1431
            $this->retirada,
1432
            "CEP",
1433
            $std->CEP,
1434
            false,
1435
            $identificador . "CEP do Endereco do Cliente da Retirada"
1436
        );
1437
        $this->dom->addChild(
1438
            $this->retirada,
1439
            "cPais",
1440
            $std->cPais,
1441
            false,
1442
            $identificador . "Codigo do Pais do Endereco do Cliente da Retirada"
1443
        );
1444
        $this->dom->addChild(
1445
            $this->retirada,
1446
            "xPais",
1447
            $std->xPais,
1448
            false,
1449
            $identificador . "Pais do Endereco do Cliente da Retirada"
1450
        );
1451
        $this->dom->addChild(
1452
            $this->retirada,
1453
            "fone",
1454
            $std->fone,
1455
            false,
1456
            $identificador . "Fone do Endereco do Cliente da Retirada"
1457
        );
1458
        $this->dom->addChild(
1459
            $this->retirada,
1460
            "email",
1461
            $std->email,
1462
            false,
1463
            $identificador . "Email do Endereco do Cliente da Retirada"
1464
        );
1465
        $this->dom->addChild(
1466
            $this->retirada,
1467
            "IE",
1468
            $std->IE,
1469
            false,
1470
            $identificador . "IE do Cliente da Retirada"
1471
        );
1472
        return $this->retirada;
1473
    }
1474
1475
    /**
1476
     * Identificação do Local de entrega G01 pai A01
1477
     * tag NFe/infNFe/entrega (opcional)
1478
     * NOTA: ajustado para NT 2018.005
1479
     * @param stdClass $std
1480
     * @return DOMElement
1481
     */
1482
    public function tagentrega(stdClass $std)
1483
    {
1484
        $possible = [
1485
            'xLgr',
1486
            'nro',
1487
            'xCpl',
1488
            'xBairro',
1489
            'cMun',
1490
            'xMun',
1491
            'UF',
1492
            'CNPJ',
1493
            'CPF',
1494
            'xNome',
1495
            'CEP',
1496
            'cPais',
1497
            'xPais',
1498
            'fone',
1499
            'email',
1500
            'IE'
1501
        ];
1502
        $std = $this->equilizeParameters($std, $possible);
1503
        $identificador = 'G01 <entrega> - ';
1504
        $this->entrega = $this->dom->createElement("entrega");
1505
        $this->dom->addChild(
1506
            $this->entrega,
1507
            "CNPJ",
1508
            $std->CNPJ,
1509
            false,
1510
            $identificador . "CNPJ do Cliente da Entrega"
1511
        );
1512
        $this->dom->addChild(
1513
            $this->entrega,
1514
            "CPF",
1515
            $std->CPF,
1516
            false,
1517
            $identificador . "CPF do Cliente da Entrega"
1518
        );
1519
        $this->dom->addChild(
1520
            $this->entrega,
1521
            "xNome",
1522
            $std->xNome,
1523
            false,
1524
            $identificador . "Nome do Cliente da Entrega"
1525
        );
1526
        $this->dom->addChild(
1527
            $this->entrega,
1528
            "xLgr",
1529
            $std->xLgr,
1530
            true,
1531
            $identificador . "Logradouro do Endereco do Cliente da Entrega"
1532
        );
1533
        $this->dom->addChild(
1534
            $this->entrega,
1535
            "nro",
1536
            $std->nro,
1537
            true,
1538
            $identificador . "Número do Endereco do Cliente da Entrega"
1539
        );
1540
        $this->dom->addChild(
1541
            $this->entrega,
1542
            "xCpl",
1543
            $std->xCpl,
1544
            false,
1545
            $identificador . "Complemento do Endereco do Cliente da Entrega"
1546
        );
1547
        $this->dom->addChild(
1548
            $this->entrega,
1549
            "xBairro",
1550
            $std->xBairro,
1551
            true,
1552
            $identificador . "Bairro do Endereco do Cliente da Entrega"
1553
        );
1554
        $this->dom->addChild(
1555
            $this->entrega,
1556
            "cMun",
1557
            $std->cMun,
1558
            true,
1559
            $identificador . "Código do município do Endereco do Cliente da Entrega"
1560
        );
1561
        $this->dom->addChild(
1562
            $this->entrega,
1563
            "xMun",
1564
            $std->xMun,
1565
            true,
1566
            $identificador . "Nome do município do Endereco do Cliente da Entrega"
1567
        );
1568
        $this->dom->addChild(
1569
            $this->entrega,
1570
            "UF",
1571
            $std->UF,
1572
            true,
1573
            $identificador . "Sigla da UF do Endereco do Cliente da Entrega"
1574
        );
1575
        $this->dom->addChild(
1576
            $this->entrega,
1577
            "CEP",
1578
            $std->CEP,
1579
            false,
1580
            $identificador . "CEP do Endereco do Cliente da Entrega"
1581
        );
1582
        $this->dom->addChild(
1583
            $this->entrega,
1584
            "cPais",
1585
            $std->cPais,
1586
            false,
1587
            $identificador . "Codigo do Pais do Endereco do Cliente da Entrega"
1588
        );
1589
        $this->dom->addChild(
1590
            $this->entrega,
1591
            "xPais",
1592
            $std->xPais,
1593
            false,
1594
            $identificador . "Pais do Endereco do Cliente da Entrega"
1595
        );
1596
        $this->dom->addChild(
1597
            $this->entrega,
1598
            "fone",
1599
            $std->fone,
1600
            false,
1601
            $identificador . "Fone do Endereco do Cliente da Entrega"
1602
        );
1603
        $this->dom->addChild(
1604
            $this->entrega,
1605
            "email",
1606
            $std->email,
1607
            false,
1608
            $identificador . "Email do Endereco do Cliente da Entrega"
1609
        );
1610
        $this->dom->addChild(
1611
            $this->entrega,
1612
            "IE",
1613
            $std->IE,
1614
            false,
1615
            $identificador . "IE do Cliente da Entrega"
1616
        );
1617
        return $this->entrega;
1618
    }
1619
1620
    /**
1621
     * Pessoas autorizadas para o download do XML da NF-e G50 pai A01
1622
     * tag NFe/infNFe/autXML
1623
     * @param stdClass $std
1624
     * @return DOMElement
1625
     */
1626
    public function tagautXML(stdClass $std)
1627
    {
1628
        $possible = ['CNPJ', 'CPF'];
1629
        $std = $this->equilizeParameters($std, $possible);
1630
        $identificador = 'G50 <autXML> - ';
1631
        $std->CNPJ = !empty($std->CNPJ) ? $std->CNPJ : null;
1632
        $std->CPF = !empty($std->CPF) ? $std->CPF : null;
1633
        $autXML = $this->dom->createElement("autXML");
1634
        $this->dom->addChild(
1635
            $autXML,
1636
            "CNPJ",
1637
            $std->CNPJ,
1638
            false,
1639
            $identificador . "CNPJ do Cliente Autorizado"
1640
        );
1641
        $this->dom->addChild(
1642
            $autXML,
1643
            "CPF",
1644
            $std->CPF,
1645
            false,
1646
            $identificador . "CPF do Cliente Autorizado"
1647
        );
1648
        $this->aAutXML[] = $autXML;
1649
        return $autXML;
1650
    }
1651
1652
    /**
1653
     * Informações adicionais do produto V01 pai H01
1654
     * tag NFe/infNFe/det[]/infAdProd
1655
     * @param stdClass $std
1656
     * @return DOMElement
1657
     */
1658
    public function taginfAdProd(stdClass $std)
1659
    {
1660
        $possible = ['item', 'infAdProd'];
1661
        $std = $this->equilizeParameters($std, $possible);
1662
        $infAdProd = $this->dom->createElement(
1663
            "infAdProd",
1664
            substr(trim($std->infAdProd), 0, 500)
1665
        );
1666
        $this->aInfAdProd[$std->item] = $infAdProd;
1667
        return $infAdProd;
1668
    }
1669
1670
    /**
1671
     * Detalhamento de Produtos e Serviços I01 pai H01
1672
     * tag NFe/infNFe/det[]/prod
1673
     * NOTA: Ajustado para NT2016_002_v1.30
1674
     * @param stdClass $std
1675
     * @return DOMElement
1676
     */
1677
    public function tagprod(stdClass $std)
1678
    {
1679
        $possible = [
1680
            'item',
1681
            'cProd',
1682
            'cEAN',
1683
            'xProd',
1684
            'NCM',
1685
            'cBenef',
1686
            'EXTIPI',
1687
            'CFOP',
1688
            'uCom',
1689
            'qCom',
1690
            'vUnCom',
1691
            'vProd',
1692
            'cEANTrib',
1693
            'uTrib',
1694
            'qTrib',
1695
            'vUnTrib',
1696
            'vFrete',
1697
            'vSeg',
1698
            'vDesc',
1699
            'vOutro',
1700
            'indTot',
1701
            'xPed',
1702
            'nItemPed',
1703
            'nFCI'
1704
        ];
1705
        $std = $this->equilizeParameters($std, $possible);
1706
        //totalizador
1707
        if ($std->indTot == 1) {
1708
            $this->stdTot->vProd += (float) $std->vProd;
1709
        }
1710
        $this->stdTot->vFrete += (float) $std->vFrete;
1711
        $this->stdTot->vSeg += (float) $std->vSeg;
1712
        $this->stdTot->vDesc += (float) $std->vDesc;
1713
        $this->stdTot->vOutro += (float) $std->vOutro;
1714
1715
        $cean = !empty($std->cEAN) ? trim(strtoupper($std->cEAN)) : '';
1716
        $ceantrib = !empty($std->cEANTrib) ? trim(strtoupper($std->cEANTrib)) : '';
1717
        //throw exception if not is Valid
1718
        try {
1719
            Gtin::isValid($cean);
1720
        } catch (\InvalidArgumentException $e) {
1721
            $this->errors[] = "cEANT {$cean} " . $e->getMessage();
1722
        }
1723
        
1724
        try {
1725
            Gtin::isValid($ceantrib);
1726
        } catch (\InvalidArgumentException $e) {
1727
            $this->errors[] = "cEANTrib {$ceantrib} " . $e->getMessage();
1728
        }
1729
        
1730
        $identificador = 'I01 <prod> - ';
1731
        $prod = $this->dom->createElement("prod");
1732
        $this->dom->addChild(
1733
            $prod,
1734
            "cProd",
1735
            $std->cProd,
1736
            true,
1737
            $identificador . "[item $std->item] Código do produto ou serviço"
1738
        );
1739
        $this->dom->addChild(
1740
            $prod,
1741
            "cEAN",
1742
            $cean,
1743
            true,
1744
            $identificador . "[item $std->item] GTIN (Global Trade Item Number) do produto, antigo "
1745
            . "código EAN ou código de barras",
1746
            true
1747
        );
1748
        $xProd = $std->xProd;
1749
        if ($this->tpAmb == '2' && $this->mod == '65' && $std->item === 1) {
1750
            $xProd = 'NOTA FISCAL EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL';
1751
        }
1752
        $this->dom->addChild(
1753
            $prod,
1754
            "xProd",
1755
            $xProd,
1756
            true,
1757
            $identificador . "[item $std->item] Descrição do produto ou serviço"
1758
        );
1759
        $this->dom->addChild(
1760
            $prod,
1761
            "NCM",
1762
            $std->NCM,
1763
            true,
1764
            $identificador . "[item $std->item] Código NCM com 8 dígitos ou 2 dígitos (gênero)"
1765
        );
1766
        //incluido no layout 4.00
1767
        $this->dom->addChild(
1768
            $prod,
1769
            "cBenef",
1770
            $std->cBenef,
1771
            false,
1772
            $identificador . "[item $std->item] Código de Benefício Fiscal utilizado pela UF"
1773
        );
1774
        $this->dom->addChild(
1775
            $prod,
1776
            "EXTIPI",
1777
            $std->EXTIPI,
1778
            false,
1779
            $identificador . "[item $std->item] Preencher de acordo com o código EX da TIPI"
1780
        );
1781
        $this->dom->addChild(
1782
            $prod,
1783
            "CFOP",
1784
            $std->CFOP,
1785
            true,
1786
            $identificador . "[item $std->item] Código Fiscal de Operações e Prestações"
1787
        );
1788
        $this->dom->addChild(
1789
            $prod,
1790
            "uCom",
1791
            $std->uCom,
1792
            true,
1793
            $identificador . "[item $std->item] Unidade Comercial do produto"
1794
        );
1795
        $this->dom->addChild(
1796
            $prod,
1797
            "qCom",
1798
            $std->qCom,
1799
            true,
1800
            $identificador . "[item $std->item] Quantidade Comercial do produto"
1801
        );
1802
        $this->dom->addChild(
1803
            $prod,
1804
            "vUnCom",
1805
            $std->vUnCom,
1806
            true,
1807
            $identificador . "[item $std->item] Valor Unitário de Comercialização do produto"
1808
        );
1809
        $this->dom->addChild(
1810
            $prod,
1811
            "vProd",
1812
            $std->vProd,
1813
            true,
1814
            $identificador . "[item $std->item] Valor Total Bruto dos Produtos ou Serviços"
1815
        );
1816
        $this->dom->addChild(
1817
            $prod,
1818
            "cEANTrib",
1819
            $ceantrib,
1820
            true,
1821
            $identificador . "[item $std->item] GTIN (Global Trade Item Number) da unidade tributável, antigo "
1822
            . "código EAN ou código de barras",
1823
            true
1824
        );
1825
        $this->dom->addChild(
1826
            $prod,
1827
            "uTrib",
1828
            $std->uTrib,
1829
            true,
1830
            $identificador . "[item $std->item] Unidade Tributável do produto"
1831
        );
1832
        $this->dom->addChild(
1833
            $prod,
1834
            "qTrib",
1835
            $std->qTrib,
1836
            true,
1837
            $identificador . "[item $std->item] Quantidade Tributável do produto"
1838
        );
1839
        $this->dom->addChild(
1840
            $prod,
1841
            "vUnTrib",
1842
            $std->vUnTrib,
1843
            true,
1844
            $identificador . "[item $std->item] Valor Unitário de tributação do produto"
1845
        );
1846
        $this->dom->addChild(
1847
            $prod,
1848
            "vFrete",
1849
            $this->conditionalNumberFormatting($std->vFrete),
1850
            false,
1851
            $identificador . "[item $std->item] Valor Total do Frete"
1852
        );
1853
        $this->dom->addChild(
1854
            $prod,
1855
            "vSeg",
1856
            $this->conditionalNumberFormatting($std->vSeg),
1857
            false,
1858
            $identificador . "[item $std->item] Valor Total do Seguro"
1859
        );
1860
        $this->dom->addChild(
1861
            $prod,
1862
            "vDesc",
1863
            $this->conditionalNumberFormatting($std->vDesc),
1864
            false,
1865
            $identificador . "[item $std->item] Valor do Desconto"
1866
        );
1867
        $this->dom->addChild(
1868
            $prod,
1869
            "vOutro",
1870
            $this->conditionalNumberFormatting($std->vOutro),
1871
            false,
1872
            $identificador . "[item $std->item] Outras despesas acessórias"
1873
        );
1874
        $this->dom->addChild(
1875
            $prod,
1876
            "indTot",
1877
            $std->indTot,
1878
            true,
1879
            $identificador . "[item $std->item] Indica se valor do Item (vProd) entra no valor total da NF-e (vProd)"
1880
        );
1881
        $this->dom->addChild(
1882
            $prod,
1883
            "xPed",
1884
            $std->xPed,
1885
            false,
1886
            $identificador . "[item $std->item] Número do Pedido de Compra"
1887
        );
1888
        $this->dom->addChild(
1889
            $prod,
1890
            "nItemPed",
1891
            $std->nItemPed,
1892
            false,
1893
            $identificador . "[item $std->item] Item do Pedido de Compra"
1894
        );
1895
        $this->dom->addChild(
1896
            $prod,
1897
            "nFCI",
1898
            $std->nFCI,
1899
            false,
1900
            $identificador . "[item $std->item] Número de controle da FCI "
1901
                . "Ficha de Conteúdo de Importação"
1902
        );
1903
        $this->aProd[$std->item] = $prod;
1904
        return $prod;
1905
    }
1906
1907
    /**
1908
     * NVE NOMENCLATURA DE VALOR ADUANEIRO E ESTATÍSTICA
1909
     * Podem ser até 8 NVE's por item
1910
     *
1911
     * @param stdClass $std
1912
     * @return DOMElement|null
1913
     */
1914
    public function tagNVE(stdClass $std)
1915
    {
1916
        $possible = ['item', 'NVE'];
1917
        $std = $this->equilizeParameters($std, $possible);
1918
1919
        if ($std->NVE == '') {
1920
            return null;
1921
        }
1922
        $nve = $this->dom->createElement("NVE", $std->NVE);
1923
        $this->aNVE[$std->item][] = $nve;
1924
        return $nve;
1925
    }
1926
1927
    /**
1928
     * Código Especificador da Substituição Tributária – CEST,
1929
     * que identifica a mercadoria sujeita aos regimes de substituição
1930
     * tributária e de antecipação do recolhimento do imposto.
1931
     * vide NT2015.003  I05C pai
1932
     * tag NFe/infNFe/det[item]/prod/CEST (opcional)
1933
     * NOTA: Ajustado para NT2016_002_v1.30
1934
     * @param  stdClass $std
1935
     * @return DOMElement
1936
     */
1937
    public function tagCEST(stdClass $std)
1938
    {
1939
        $possible = ['item', 'CEST', 'indEscala', 'CNPJFab'];
1940
        $std = $this->equilizeParameters($std, $possible);
1941
        $identificador = 'I05b <ctrltST> - ';
1942
        $ctrltST = $this->dom->createElement("ctrltST");
1943
        $this->dom->addChild(
1944
            $ctrltST,
1945
            "CEST",
1946
            Strings::onlyNumbers($std->CEST),
1947
            true,
1948
            "$identificador [item $std->item] Numero CEST"
1949
        );
1950
        //incluido no layout 4.00
1951
        $this->dom->addChild(
1952
            $ctrltST,
1953
            "indEscala",
1954
            trim($std->indEscala),
1955
            false,
1956
            "$identificador [item $std->item] Indicador de Produção em escala relevante"
1957
        );
1958
        //incluido no layout 4.00
1959
        $this->dom->addChild(
1960
            $ctrltST,
1961
            "CNPJFab",
1962
            Strings::onlyNumbers($std->CNPJFab),
1963
            false,
1964
            "$identificador [item $std->item] CNPJ do Fabricante da Mercadoria,"
1965
                . "obrigatório para produto em escala NÃO relevante."
1966
        );
1967
        $this->aCest[$std->item][] = $ctrltST;
1968
        return $ctrltST;
1969
    }
1970
1971
    /**
1972
     * tag NFe/infNFe/det[item]/prod/nRECOPI
1973
     * @param  stdClass $std
1974
     * @return DOMElement
1975
     */
1976
    public function tagRECOPI(stdClass $std)
1977
    {
1978
        $possible = ['item', 'nRECOPI'];
1979
        $std = $this->equilizeParameters($std, $possible);
1980
        $recopi = $this->dom->createElement("nRECOPI", $std->nRECOPI);
1981
        $this->aRECOPI[$std->item] = $recopi;
1982
        return $recopi;
1983
    }
1984
1985
    /**
1986
     * Declaração de Importação I8 pai I01
1987
     * tag NFe/infNFe/det[]/prod/DI
1988
     * @param stdClass $std
1989
     * @return DOMElement
1990
     */
1991
    public function tagDI(stdClass $std)
1992
    {
1993
        $possible = [
1994
            'item',
1995
            'nDI',
1996
            'dDI',
1997
            'xLocDesemb',
1998
            'UFDesemb',
1999
            'dDesemb',
2000
            'tpViaTransp',
2001
            'vAFRMM',
2002
            'tpIntermedio',
2003
            'CNPJ',
2004
            'UFTerceiro',
2005
            'cExportador'
2006
        ];
2007
        $std = $this->equilizeParameters($std, $possible);
2008
        $identificador = 'I8 <DI> - ';
2009
        $tDI = $this->dom->createElement("DI");
2010
        $this->dom->addChild(
2011
            $tDI,
2012
            "nDI",
2013
            $std->nDI,
2014
            true,
2015
            $identificador . "[item $std->item] Número do Documento de Importação (DI, DSI, DIRE, ...)"
2016
        );
2017
        $this->dom->addChild(
2018
            $tDI,
2019
            "dDI",
2020
            $std->dDI,
2021
            true,
2022
            $identificador . "[item $std->item] Data de Registro do documento"
2023
        );
2024
        $this->dom->addChild(
2025
            $tDI,
2026
            "xLocDesemb",
2027
            $std->xLocDesemb,
2028
            true,
2029
            $identificador . "[item $std->item] Local de desembaraço"
2030
        );
2031
        $this->dom->addChild(
2032
            $tDI,
2033
            "UFDesemb",
2034
            $std->UFDesemb,
2035
            true,
2036
            $identificador . "[item $std->item] Sigla da UF onde ocorreu o Desembaraço Aduaneiro"
2037
        );
2038
        $this->dom->addChild(
2039
            $tDI,
2040
            "dDesemb",
2041
            $std->dDesemb,
2042
            true,
2043
            $identificador . "[item $std->item] Data do Desembaraço Aduaneiro"
2044
        );
2045
        $this->dom->addChild(
2046
            $tDI,
2047
            "tpViaTransp",
2048
            $std->tpViaTransp,
2049
            true,
2050
            $identificador . "[item $std->item] Via de transporte internacional "
2051
                . "informada na Declaração de Importação (DI)"
2052
        );
2053
        $this->dom->addChild(
2054
            $tDI,
2055
            "vAFRMM",
2056
            $this->conditionalNumberFormatting($std->vAFRMM),
2057
            false,
2058
            $identificador . "[item $std->item] Valor da AFRMM "
2059
                . "- Adicional ao Frete para Renovação da Marinha Mercante"
2060
        );
2061
        $this->dom->addChild(
2062
            $tDI,
2063
            "tpIntermedio",
2064
            $std->tpIntermedio,
2065
            true,
2066
            $identificador . "[item $std->item] Forma de importação quanto a intermediação"
2067
        );
2068
        $this->dom->addChild(
2069
            $tDI,
2070
            "CNPJ",
2071
            $std->CNPJ,
2072
            false,
2073
            $identificador . "[item $std->item] CNPJ do adquirente ou do encomendante"
2074
        );
2075
        $this->dom->addChild(
2076
            $tDI,
2077
            "UFTerceiro",
2078
            $std->UFTerceiro,
2079
            false,
2080
            $identificador . "[item $std->item] Sigla da UF do adquirente ou do encomendante"
2081
        );
2082
        $this->dom->addChild(
2083
            $tDI,
2084
            "cExportador",
2085
            $std->cExportador,
2086
            true,
2087
            $identificador . "[item $std->item] Código do Exportador"
2088
        );
2089
        $this->aDI[$std->item][$std->nDI] = $tDI;
2090
        return $tDI;
2091
    }
2092
2093
    /**
2094
     * Adições I25 pai I18
2095
     * tag NFe/infNFe/det[]/prod/DI/adi
2096
     * @param stdClass $std
2097
     * @return DOMElement
2098
     */
2099
    public function tagadi(stdClass $std)
2100
    {
2101
        $possible = [
2102
            'item',
2103
            'nDI',
2104
            'nAdicao',
2105
            'nSeqAdic',
2106
            'cFabricante',
2107
            'vDescDI',
2108
            'nDraw'
2109
        ];
2110
        $std = $this->equilizeParameters($std, $possible);
2111
        $identificador = 'I25 <adi> - ';
2112
        $adi = $this->dom->createElement("adi");
2113
        $this->dom->addChild(
2114
            $adi,
2115
            "nAdicao",
2116
            $std->nAdicao,
2117
            true,
2118
            $identificador . "[item $std->item] Número da Adição"
2119
        );
2120
        $this->dom->addChild(
2121
            $adi,
2122
            "nSeqAdic",
2123
            $std->nSeqAdic,
2124
            true,
2125
            $identificador . "[item $std->item] Número sequencial do item dentro da Adição"
2126
        );
2127
        $this->dom->addChild(
2128
            $adi,
2129
            "cFabricante",
2130
            $std->cFabricante,
2131
            true,
2132
            $identificador . "[item $std->item] Código do fabricante estrangeiro"
2133
        );
2134
        $this->dom->addChild(
2135
            $adi,
2136
            "vDescDI",
2137
            $this->conditionalNumberFormatting($std->vDescDI),
2138
            false,
2139
            $identificador . "[item $std->item] Valor do desconto do item da DI Adição"
2140
        );
2141
        $this->dom->addChild(
2142
            $adi,
2143
            "nDraw",
2144
            $std->nDraw,
2145
            false,
2146
            $identificador . "[item $std->item] Número do ato concessório de Drawback"
2147
        );
2148
        $this->aAdi[$std->item][$std->nDI][] = $adi;
2149
        //colocar a adi em seu DI respectivo
2150
        $nodeDI = $this->aDI[$std->item][$std->nDI];
2151
        $this->dom->appChild($nodeDI, $adi);
2152
        $this->aDI[$std->item][$std->nDI] = $nodeDI;
2153
        return $adi;
2154
    }
2155
2156
    /**
2157
     * Grupo de informações de exportação para o item I50 pai I01
2158
     * tag NFe/infNFe/det[]/prod/detExport
2159
     * @param stdClass $std
2160
     * @return DOMElement
2161
     */
2162
    public function tagdetExport(stdClass $std)
2163
    {
2164
        $possible = [
2165
            'item',
2166
            'nDraw'
2167
        ];
2168
        $std = $this->equilizeParameters($std, $possible);
2169
        $identificador = 'I50 <detExport> - ';
2170
        $detExport = $this->dom->createElement("detExport");
2171
        $this->dom->addChild(
2172
            $detExport,
2173
            "nDraw",
2174
            Strings::onlyNumbers($std->nDraw),
2175
            false,
2176
            $identificador . "[item $std->item] Número do ato concessório de Drawback"
2177
        );
2178
        $this->aDetExport[$std->item][] = $detExport;
2179
        return $detExport;
2180
    }
2181
2182
    /**
2183
     * Grupo de informações de exportação para o item I52 pai I52
2184
     * tag NFe/infNFe/det[]/prod/detExport
2185
     * @param stdClass $std
2186
     * @return DOMElement
2187
     */
2188
    public function tagdetExportInd(stdClass $std)
2189
    {
2190
        $possible = [
2191
            'item',
2192
            'nRE',
2193
            'chNFe',
2194
            'qExport'
2195
        ];
2196
        $std = $this->equilizeParameters($std, $possible);
2197
        $identificador = 'I52 <exportInd> - ';
2198
        $exportInd = $this->dom->createElement("exportInd");
2199
        $this->dom->addChild(
2200
            $exportInd,
2201
            "nRE",
2202
            Strings::onlyNumbers($std->nRE),
2203
            true,
2204
            $identificador . "[item $std->item] Número do Registro de Exportação"
2205
        );
2206
        $this->dom->addChild(
2207
            $exportInd,
2208
            "chNFe",
2209
            Strings::onlyNumbers($std->chNFe),
2210
            true,
2211
            $identificador . "[item $std->item] Chave de Acesso da NF-e recebida para exportação"
2212
        );
2213
        $this->dom->addChild(
2214
            $exportInd,
2215
            "qExport",
2216
            $this->conditionalNumberFormatting($std->qExport, 4),
2217
            true,
2218
            $identificador . "[item $std->item] Quantidade do item realmente exportado"
2219
        );
2220
        //obtem o ultimo detExport
2221
        $nDE = count($this->aDetExport[$std->item]) - 1;
2222
        if ($nDE < 0) {
2223
            throw new RuntimeException('A TAG detExportInd deve ser criada depois da detExport, pois pertence a ela.');
2224
        }
2225
        //colocar a exportInd em seu DetExport respectivo
2226
        $nodeDetExport = $this->aDetExport[$std->item][$nDE];
2227
        $this->dom->appChild($nodeDetExport, $exportInd);
2228
        $this->aDetExport[$std->item][$nDE] = $nodeDetExport;
2229
        return $exportInd;
2230
    }
2231
2232
    /**
2233
     * Rastreabilidade do produto podem ser até 500 por item TAG I80 pai I01
2234
     * NOTA: Ajustado para NT2016_002_v1.00
2235
     * tag NFe/infNFe/det[]/prod/rastro
2236
     * @param stdClass $std
2237
     * @return DOMElement
2238
     */
2239
    public function tagRastro(stdClass $std)
2240
    {
2241
        $possible = [
2242
            'item',
2243
            'nLote',
2244
            'qLote',
2245
            'dFab',
2246
            'dVal',
2247
            'cAgreg'
2248
        ];
2249
        $std = $this->equilizeParameters($std, $possible);
2250
        $identificador = 'I80 <rastro> - ';
2251
        $rastro = $this->dom->createElement("rastro");
2252
        $this->dom->addChild(
2253
            $rastro,
2254
            "nLote",
2255
            substr(trim($std->nLote), 0, 20),
2256
            true,
2257
            $identificador . "[item $std->item] Número do lote"
2258
        );
2259
        $this->dom->addChild(
2260
            $rastro,
2261
            "qLote",
2262
            $this->conditionalNumberFormatting($std->qLote, 3),
2263
            true,
2264
            $identificador . "[item $std->item] Quantidade do lote"
2265
        );
2266
        $this->dom->addChild(
2267
            $rastro,
2268
            "dFab",
2269
            trim($std->dFab),
2270
            true,
2271
            $identificador . "[item $std->item] Data de fabricação"
2272
        );
2273
        $this->dom->addChild(
2274
            $rastro,
2275
            "dVal",
2276
            trim($std->dVal),
2277
            true,
2278
            $identificador . "[item $std->item] Data da validade"
2279
        );
2280
        $this->dom->addChild(
2281
            $rastro,
2282
            "cAgreg",
2283
            Strings::onlyNumbers($std->cAgreg),
2284
            false,
2285
            $identificador . "[item $std->item] Código de Agregação"
2286
        );
2287
        $this->aRastro[$std->item][] = $rastro;
2288
        return $rastro;
2289
    }
2290
2291
    /**
2292
     * Detalhamento de Veículos novos J01 pai I90
2293
     * tag NFe/infNFe/det[]/prod/veicProd (opcional)
2294
     * @param stdClass $std
2295
     * @return DOMElement
2296
     */
2297
    public function tagveicProd(stdClass $std)
2298
    {
2299
        $possible = [
2300
            'item',
2301
            'tpOp',
2302
            'chassi',
2303
            'cCor',
2304
            'xCor',
2305
            'pot',
2306
            'cilin',
2307
            'pesoL',
2308
            'pesoB',
2309
            'nSerie',
2310
            'tpComb',
2311
            'nMotor',
2312
            'CMT',
2313
            'dist',
2314
            'anoMod',
2315
            'anoFab',
2316
            'tpPint',
2317
            'tpVeic',
2318
            'espVeic',
2319
            'VIN',
2320
            'condVeic',
2321
            'cMod',
2322
            'cCorDENATRAN',
2323
            'lota',
2324
            'tpRest'
2325
        ];
2326
        $std = $this->equilizeParameters($std, $possible);
2327
        $identificador = 'J01 <veicProd> - ';
2328
        $veicProd = $this->dom->createElement("veicProd");
2329
        $this->dom->addChild(
2330
            $veicProd,
2331
            "tpOp",
2332
            $std->tpOp,
2333
            true,
2334
            "$identificador [item $std->item] Tipo da operação do veículo"
2335
        );
2336
        $this->dom->addChild(
2337
            $veicProd,
2338
            "chassi",
2339
            $std->chassi,
2340
            true,
2341
            "$identificador [item $std->item] Chassi do veículo"
2342
        );
2343
        $this->dom->addChild(
2344
            $veicProd,
2345
            "cCor",
2346
            $std->cCor,
2347
            true,
2348
            "$identificador [item $std->item] Cor do veículo"
2349
        );
2350
        $this->dom->addChild(
2351
            $veicProd,
2352
            "xCor",
2353
            $std->xCor,
2354
            true,
2355
            "$identificador [item $std->item] Descrição da Cor do veículo"
2356
        );
2357
        $this->dom->addChild(
2358
            $veicProd,
2359
            "pot",
2360
            $std->pot,
2361
            true,
2362
            "$identificador [item $std->item] Potência Motor (CV) do veículo"
2363
        );
2364
        $this->dom->addChild(
2365
            $veicProd,
2366
            "cilin",
2367
            $std->cilin,
2368
            true,
2369
            "$identificador [item $std->item] Cilindradas do veículo"
2370
        );
2371
        $this->dom->addChild(
2372
            $veicProd,
2373
            "pesoL",
2374
            $this->conditionalNumberFormatting($std->pesoL, 3),
2375
            true,
2376
            "$identificador [item $std->item] Peso Líquido do veículo"
2377
        );
2378
        $this->dom->addChild(
2379
            $veicProd,
2380
            "pesoB",
2381
            $this->conditionalNumberFormatting($std->pesoB, 3),
2382
            true,
2383
            "$identificador [item $std->item] Peso Bruto do veículo"
2384
        );
2385
        $this->dom->addChild(
2386
            $veicProd,
2387
            "nSerie",
2388
            $std->nSerie,
2389
            true,
2390
            "$identificador [item $std->item] Serial (série) do veículo"
2391
        );
2392
        $this->dom->addChild(
2393
            $veicProd,
2394
            "tpComb",
2395
            $std->tpComb,
2396
            true,
2397
            "$identificador [item $std->item] Tipo de combustível do veículo"
2398
        );
2399
        $this->dom->addChild(
2400
            $veicProd,
2401
            "nMotor",
2402
            $std->nMotor,
2403
            true,
2404
            "$identificador [item $std->item] Número de Motor do veículo"
2405
        );
2406
        $this->dom->addChild(
2407
            $veicProd,
2408
            "CMT",
2409
            $this->conditionalNumberFormatting($std->CMT, 4),
2410
            true,
2411
            "$identificador [item $std->item] Capacidade Máxima de Tração do veículo"
2412
        );
2413
        $this->dom->addChild(
2414
            $veicProd,
2415
            "dist",
2416
            $std->dist,
2417
            true,
2418
            "$identificador [item $std->item] Distância entre eixos do veículo"
2419
        );
2420
        $this->dom->addChild(
2421
            $veicProd,
2422
            "anoMod",
2423
            $std->anoMod,
2424
            true,
2425
            "$identificador [item $std->item] Ano Modelo de Fabricação do veículo"
2426
        );
2427
        $this->dom->addChild(
2428
            $veicProd,
2429
            "anoFab",
2430
            $std->anoFab,
2431
            true,
2432
            "$identificador [item $std->item] Ano de Fabricação do veículo"
2433
        );
2434
        $this->dom->addChild(
2435
            $veicProd,
2436
            "tpPint",
2437
            $std->tpPint,
2438
            true,
2439
            "$identificador [item $std->item] Tipo de Pintura do veículo"
2440
        );
2441
        $this->dom->addChild(
2442
            $veicProd,
2443
            "tpVeic",
2444
            $std->tpVeic,
2445
            true,
2446
            "$identificador [item $std->item] Tipo de Veículo"
2447
        );
2448
        $this->dom->addChild(
2449
            $veicProd,
2450
            "espVeic",
2451
            $std->espVeic,
2452
            true,
2453
            "$identificador [item $std->item] Espécie de Veículo"
2454
        );
2455
        $this->dom->addChild(
2456
            $veicProd,
2457
            "VIN",
2458
            $std->VIN,
2459
            true,
2460
            "$identificador [item $std->item] Condição do VIN do veículo"
2461
        );
2462
        $this->dom->addChild(
2463
            $veicProd,
2464
            "condVeic",
2465
            $std->condVeic,
2466
            true,
2467
            "$identificador [item $std->item] Condição do Veículo"
2468
        );
2469
        $this->dom->addChild(
2470
            $veicProd,
2471
            "cMod",
2472
            $std->cMod,
2473
            true,
2474
            "$identificador [item $std->item] Código Marca Modelo do veículo"
2475
        );
2476
        $this->dom->addChild(
2477
            $veicProd,
2478
            "cCorDENATRAN",
2479
            $std->cCorDENATRAN,
2480
            true,
2481
            "$identificador [item $std->item] Código da Cor do veículo"
2482
        );
2483
        $this->dom->addChild(
2484
            $veicProd,
2485
            "lota",
2486
            $std->lota,
2487
            true,
2488
            "$identificador [item $std->item] Capacidade máxima de lotação do veículo"
2489
        );
2490
        $this->dom->addChild(
2491
            $veicProd,
2492
            "tpRest",
2493
            $std->tpRest,
2494
            true,
2495
            "$identificador [item $std->item] Restrição do veículo"
2496
        );
2497
        $this->aVeicProd[$std->item] = $veicProd;
2498
        return $veicProd;
2499
    }
2500
2501
    /**
2502
     * Detalhamento de medicamentos K01 pai I90
2503
     * NOTA: Ajustado para NT2018.005
2504
     * tag NFe/infNFe/det[]/prod/med (opcional)
2505
     * @param stdClass $std
2506
     * @return DOMElement
2507
     */
2508
    public function tagmed(stdClass $std)
2509
    {
2510
        $possible = [
2511
            'item',
2512
            'vPMC',
2513
            'cProdANVISA',
2514
            'xMotivoIsencao'
2515
        ];
2516
        $std = $this->equilizeParameters($std, $possible);
2517
        $identificador = 'K01 <med> - ';
2518
        $med = $this->dom->createElement("med");
2519
        $this->dom->addChild(
2520
            $med,
2521
            "cProdANVISA",
2522
            $std->cProdANVISA,
2523
            false,
2524
            "$identificador [item $std->item] Numero ANVISA"
2525
        );
2526
        $this->dom->addChild(
2527
            $med,
2528
            "xMotivoIsencao",
2529
            $std->xMotivoIsencao,
2530
            false,
2531
            "$identificador [item $std->item] Motivo da isenção da ANVISA"
2532
        );
2533
        $this->dom->addChild(
2534
            $med,
2535
            "vPMC",
2536
            $this->conditionalNumberFormatting($std->vPMC),
2537
            true,
2538
            "$identificador [item $std->item] Preço máximo consumidor"
2539
        );
2540
        $this->aMed[$std->item] = $med;
2541
        return $med;
2542
    }
2543
2544
    /**
2545
     * Detalhamento de armas L01 pai I90
2546
     * tag NFe/infNFe/det[]/prod/arma (opcional)
2547
     * @param stdClass $std
2548
     * @return DOMElement
2549
     */
2550
    public function tagarma(stdClass $std)
2551
    {
2552
        $possible = [
2553
            'item',
2554
            'nAR',
2555
            'tpArma',
2556
            'nSerie',
2557
            'nCano',
2558
            'descr'
2559
        ];
2560
        $std = $this->equilizeParameters($std, $possible);
2561
        $identificador = 'L01 <arma> - ';
2562
        $arma = $this->dom->createElement("arma");
2563
        $this->dom->addChild(
2564
            $arma,
2565
            "tpArma",
2566
            $std->tpArma,
2567
            true,
2568
            "$identificador [item $std->item] Indicador do tipo de arma de fogo"
2569
        );
2570
        $this->dom->addChild(
2571
            $arma,
2572
            "nSerie",
2573
            $std->nSerie,
2574
            true,
2575
            "$identificador [item $std->item] Número de série da arma"
2576
        );
2577
        $this->dom->addChild(
2578
            $arma,
2579
            "nCano",
2580
            $std->nCano,
2581
            true,
2582
            "$identificador [item $std->item] Número de série do cano"
2583
        );
2584
        $this->dom->addChild(
2585
            $arma,
2586
            "descr",
2587
            $std->descr,
2588
            true,
2589
            "$identificador [item $std->item] Descrição completa da arma, compreendendo: calibre, marca, capacidade, "
2590
                . "tipo de funcionamento, comprimento e demais elementos que "
2591
                . "permitam a sua perfeita identificação."
2592
        );
2593
        $this->aArma[$std->item][$std->nAR] = $arma;
2594
        return $arma;
2595
    }
2596
2597
    /**
2598
     * Detalhamento de combustiveis L101 pai I90
2599
     * tag NFe/infNFe/det[]/prod/comb (opcional)
2600
     * LA|cProdANP|pMixGN|CODIF|qTemp|UFCons|
2601
     *
2602
     * NOTA: Ajustado para NT2016_002_v1.30
2603
     * LA|cProdANP|descANP|pGLP|pGNn|pGNi|vPart|CODIF|qTemp|UFCons|
2604
     * @param stdClass $std
2605
     * @return DOMElement
2606
     */
2607
    public function tagcomb(stdClass $std)
2608
    {
2609
        $possible = [
2610
            'item',
2611
            'cProdANP',
2612
            'descANP',
2613
            'pGLP',
2614
            'pGNn',
2615
            'pGNi',
2616
            'vPart',
2617
            'CODIF',
2618
            'qTemp',
2619
            'UFCons',
2620
            'qBCProd',
2621
            'vAliqProd',
2622
            'vCIDE'
2623
        ];
2624
        $std = $this->equilizeParameters($std, $possible);
2625
2626
        $identificador = 'L101 <comb> - ';
2627
        $comb = $this->dom->createElement("comb");
2628
        $this->dom->addChild(
2629
            $comb,
2630
            "cProdANP",
2631
            $std->cProdANP,
2632
            true,
2633
            "$identificador [item $std->item] Código de produto da ANP"
2634
        );
2635
        //incluso no layout 4.00
2636
        $this->dom->addChild(
2637
            $comb,
2638
            "descANP",
2639
            $std->descANP,
2640
            false,
2641
            "$identificador [item $std->item] Utilizar a descrição de produtos do "
2642
                . "Sistema de Informações de Movimentação de Produtos - "
2643
                . "SIMP (http://www.anp.gov.br/simp/"
2644
        );
2645
        //incluso no layout 4.00
2646
        $this->dom->addChild(
2647
            $comb,
2648
            "pGLP",
2649
            $this->conditionalNumberFormatting($std->pGLP, 4),
2650
            false,
2651
            "$identificador [item $std->item] Percentual do GLP derivado do "
2652
                . "petróleo no produto GLP (cProdANP=210203001) 1v4"
2653
        );
2654
        //incluso no layout 4.00
2655
        $this->dom->addChild(
2656
            $comb,
2657
            "pGNn",
2658
            $this->conditionalNumberFormatting($std->pGNn, 4),
2659
            false,
2660
            "$identificador [item $std->item] Percentual de Gás Natural Nacional"
2661
                . " – GLGNn para o produto GLP (cProdANP=210203001) 1v4"
2662
        );
2663
        //incluso no layout 4.00
2664
        $this->dom->addChild(
2665
            $comb,
2666
            "pGNi",
2667
            $this->conditionalNumberFormatting($std->pGNi, 4),
2668
            false,
2669
            "$identificador [item $std->item] Percentual de Gás Natural Importado"
2670
                . " – GLGNi para o produto GLP (cProdANP=210203001) 1v4"
2671
        );
2672
        //incluso no layout 4.00
2673
        $this->dom->addChild(
2674
            $comb,
2675
            "vPart",
2676
            $this->conditionalNumberFormatting($std->vPart),
2677
            false,
2678
            "$identificador [item $std->item] Valor de partida (cProdANP=210203001) "
2679
        );
2680
        $this->dom->addChild(
2681
            $comb,
2682
            "CODIF",
2683
            $std->CODIF,
2684
            false,
2685
            "[item $std->item] Código de autorização / registro do CODIF"
2686
        );
2687
        $this->dom->addChild(
2688
            $comb,
2689
            "qTemp",
2690
            $this->conditionalNumberFormatting($std->qTemp, 4),
2691
            false,
2692
            "$identificador [item $std->item] Quantidade de combustível faturada à temperatura ambiente."
2693
        );
2694
        $this->dom->addChild(
2695
            $comb,
2696
            "UFCons",
2697
            $std->UFCons,
2698
            true,
2699
            "[item $std->item] Sigla da UF de consumo"
2700
        );
2701
        if ($std->qBCProd != "") {
2702
            $tagCIDE = $this->dom->createElement("CIDE");
2703
            $this->dom->addChild(
2704
                $tagCIDE,
2705
                "qBCProd",
2706
                $std->qBCProd,
2707
                true,
2708
                "$identificador [item $std->item] BC da CIDE"
2709
            );
2710
            $this->dom->addChild(
2711
                $tagCIDE,
2712
                "vAliqProd",
2713
                $this->conditionalNumberFormatting($std->vAliqProd, 4),
2714
                true,
2715
                "$identificador [item $std->item] Valor da alíquota da CIDE"
2716
            );
2717
            $this->dom->addChild(
2718
                $tagCIDE,
2719
                "vCIDE",
2720
                $this->conditionalNumberFormatting($std->vCIDE),
2721
                true,
2722
                "$identificador [item $std->item] Valor da CIDE"
2723
            );
2724
            $this->dom->appChild($comb, $tagCIDE);
2725
        }
2726
        $this->aComb[$std->item] = $comb;
2727
        return $comb;
2728
    }
2729
2730
    /**
2731
     * informações relacionadas com as operações de combustíveis, subgrupo de
2732
     * encerrante que permite o controle sobre as operações de venda de combustíveis
2733
     * LA11 pai LA01
2734
     * tag NFe/infNFe/det[]/prod/comb/encerrante (opcional)
2735
     * @param stdClass $std
2736
     * @return DOMElement
2737
     */
2738
    public function tagencerrante(stdClass $std)
2739
    {
2740
        $possible = [
2741
            'item',
2742
            'nBico',
2743
            'nBomba',
2744
            'nTanque',
2745
            'vEncIni',
2746
            'vEncFin'
2747
        ];
2748
        $std = $this->equilizeParameters($std, $possible);
2749
        $identificador = 'LA11 <encerrante> - ';
2750
        $encerrante = $this->dom->createElement("encerrante");
2751
        $this->dom->addChild(
2752
            $encerrante,
2753
            "nBico",
2754
            $std->nBico,
2755
            true,
2756
            "$identificador [item $std->item] Número de identificação do bico utilizado no abastecimento"
2757
        );
2758
        $this->dom->addChild(
2759
            $encerrante,
2760
            "nBomba",
2761
            $std->nBomba,
2762
            false,
2763
            "$identificador [item $std->item] Número de identificação da bomba ao qual o bico está interligado"
2764
        );
2765
        $this->dom->addChild(
2766
            $encerrante,
2767
            "nTanque",
2768
            $std->nTanque,
2769
            true,
2770
            "$identificador [item $std->item] Número de identificação do tanque ao qual o bico está interligado"
2771
        );
2772
        $this->dom->addChild(
2773
            $encerrante,
2774
            "vEncIni",
2775
            $this->conditionalNumberFormatting($std->vEncIni),
2776
            true,
2777
            "$identificador [item $std->item] Valor do Encerrante no início do abastecimento"
2778
        );
2779
        $this->dom->addChild(
2780
            $encerrante,
2781
            "vEncFin",
2782
            $this->conditionalNumberFormatting($std->vEncFin),
2783
            true,
2784
            "$identificador [item $std->item] Valor do Encerrante no final do abastecimento"
2785
        );
2786
        $this->aEncerrante[$std->item] = $encerrante;
2787
        return $encerrante;
2788
    }
2789
2790
    /**
2791
     * Impostos com o valor total tributado M01 pai H01
2792
     * tag NFe/infNFe/det[]/imposto
2793
     * @param stdClass $std
2794
     * @return DOMElement
2795
     */
2796
    public function tagimposto(stdClass $std)
2797
    {
2798
        $possible = ['item', 'vTotTrib'];
2799
        $std = $this->equilizeParameters($std, $possible);
2800
        //totalizador dos valores dos itens
2801
        $this->stdTot->vTotTrib += (float) $std->vTotTrib;
2802
        $identificador = 'M01 <imposto> - ';
2803
        $imposto = $this->dom->createElement("imposto");
2804
        $this->dom->addChild(
2805
            $imposto,
2806
            "vTotTrib",
2807
            $this->conditionalNumberFormatting($std->vTotTrib),
2808
            false,
2809
            "$identificador [item $std->item] Valor aproximado total de tributos federais, estaduais e municipais."
2810
        );
2811
        $this->aImposto[$std->item] = $imposto;
2812
        return $imposto;
2813
    }
2814
2815
    /**
2816
     * Informações do ICMS da Operação própria e ST N01 pai M01
2817
     * tag NFe/infNFe/det[]/imposto/ICMS
2818
     * @param stdClass $std
2819
     * @return DOMElement
2820
     */
2821
    public function tagICMS(stdClass $std)
2822
    {
2823
        $possible = [
2824
            'item',
2825
            'orig',
2826
            'CST',
2827
            'modBC',
2828
            'vBC',
2829
            'pICMS',
2830
            'vICMS',
2831
            'pFCP',
2832
            'vFCP',
2833
            'vBCFCP',
2834
            'modBCST',
2835
            'pMVAST',
2836
            'pRedBCST',
2837
            'vBCST',
2838
            'pICMSST',
2839
            'vICMSST',
2840
            'vBCFCPST',
2841
            'pFCPST',
2842
            'vFCPST',
2843
            'vICMSDeson',
2844
            'motDesICMS',
2845
            'pRedBC',
2846
            'vICMSOp',
2847
            'pDif',
2848
            'vICMSDif',
2849
            'vBCSTRet',
2850
            'pST',
2851
            'vICMSSTRet',
2852
            'vBCFCPSTRet',
2853
            'pFCPSTRet',
2854
            'vFCPSTRet',
2855
            'pRedBCEfet',
2856
            'vBCEfet',
2857
            'pICMSEfet',
2858
            'vICMSEfet',
2859
            'vICMSSubstituto'
2860
        ];
2861
        $std = $this->equilizeParameters($std, $possible);
2862
        //totalização generica
2863
        $this->stdTot->vICMSDeson += (float) !empty($std->vICMSDeson) ? $std->vICMSDeson : 0;
2864
        $this->stdTot->vFCP += (float) !empty($std->vFCP) ? $std->vFCP : 0;
2865
        $this->stdTot->vFCPST += (float) !empty($std->vFCPST) ? $std->vFCPST : 0;
2866
        $this->stdTot->vFCPSTRet += (float) !empty($std->vFCPSTRet) ? $std->vFCPSTRet : 0;
2867
        $identificador = 'N01 <ICMSxx> - ';
2868
        switch ($std->CST) {
2869
            case '00':
2870
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
2871
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
2872
2873
                $icms = $this->dom->createElement("ICMS00");
2874
                $this->dom->addChild(
2875
                    $icms,
2876
                    'orig',
2877
                    $std->orig,
2878
                    true,
2879
                    "$identificador [item $std->item] Origem da mercadoria"
2880
                );
2881
                $this->dom->addChild(
2882
                    $icms,
2883
                    'CST',
2884
                    $std->CST,
2885
                    true,
2886
                    "$identificador [item $std->item] Tributação do ICMS = 00"
2887
                );
2888
                $this->dom->addChild(
2889
                    $icms,
2890
                    'modBC',
2891
                    $std->modBC,
2892
                    true,
2893
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
2894
                );
2895
                $this->dom->addChild(
2896
                    $icms,
2897
                    'vBC',
2898
                    $this->conditionalNumberFormatting($std->vBC),
2899
                    true,
2900
                    "$identificador [item $std->item] Valor da BC do ICMS"
2901
                );
2902
                $this->dom->addChild(
2903
                    $icms,
2904
                    'pICMS',
2905
                    $this->conditionalNumberFormatting($std->pICMS, 4),
2906
                    true,
2907
                    "$identificador [item $std->item] Alíquota do imposto"
2908
                );
2909
                $this->dom->addChild(
2910
                    $icms,
2911
                    'vICMS',
2912
                    $this->conditionalNumberFormatting($std->vICMS),
2913
                    true,
2914
                    "$identificador [item $std->item] Valor do ICMS"
2915
                );
2916
                $this->dom->addChild(
2917
                    $icms,
2918
                    'pFCP',
2919
                    $this->conditionalNumberFormatting($std->pFCP, 4),
2920
                    false,
2921
                    "$identificador [item $std->item] Percentual do Fundo de "
2922
                    . "Combate à Pobreza (FCP)"
2923
                );
2924
                $this->dom->addChild(
2925
                    $icms,
2926
                    'vFCP',
2927
                    $this->conditionalNumberFormatting($std->vFCP),
2928
                    false,
2929
                    "$identificador [item $std->item] Valor do Fundo de Combate "
2930
                    . "à Pobreza (FCP)"
2931
                );
2932
                break;
2933
            case '10':
2934
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
2935
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
2936
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
2937
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
2938
2939
                $icms = $this->dom->createElement("ICMS10");
2940
                $this->dom->addChild(
2941
                    $icms,
2942
                    'orig',
2943
                    $std->orig,
2944
                    true,
2945
                    "$identificador [item $std->item] Origem da mercadoria"
2946
                );
2947
                $this->dom->addChild(
2948
                    $icms,
2949
                    'CST',
2950
                    $std->CST,
2951
                    true,
2952
                    "$identificador [item $std->item] Tributação do ICMS = 10"
2953
                );
2954
                $this->dom->addChild(
2955
                    $icms,
2956
                    'modBC',
2957
                    $std->modBC,
2958
                    true,
2959
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
2960
                );
2961
                $this->dom->addChild(
2962
                    $icms,
2963
                    'vBC',
2964
                    $this->conditionalNumberFormatting($std->vBC),
2965
                    true,
2966
                    "$identificador [item $std->item] Valor da BC do ICMS"
2967
                );
2968
                $this->dom->addChild(
2969
                    $icms,
2970
                    'pICMS',
2971
                    $this->conditionalNumberFormatting($std->pICMS, 4),
2972
                    true,
2973
                    "$identificador [item $std->item] Alíquota do imposto"
2974
                );
2975
                $this->dom->addChild(
2976
                    $icms,
2977
                    'vICMS',
2978
                    $this->conditionalNumberFormatting($std->vICMS),
2979
                    true,
2980
                    "$identificador [item $std->item] Valor do ICMS"
2981
                );
2982
                $this->dom->addChild(
2983
                    $icms,
2984
                    'vBCFCP',
2985
                    $this->conditionalNumberFormatting($std->vBCFCP),
2986
                    false,
2987
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP"
2988
                );
2989
                $this->dom->addChild(
2990
                    $icms,
2991
                    'pFCP',
2992
                    $this->conditionalNumberFormatting($std->pFCP, 4),
2993
                    false,
2994
                    "$identificador [item $std->item] Percentual do Fundo de "
2995
                    . "Combate à Pobreza (FCP)"
2996
                );
2997
                $this->dom->addChild(
2998
                    $icms,
2999
                    'vFCP',
3000
                    $this->conditionalNumberFormatting($std->vFCP),
3001
                    false,
3002
                    "$identificador [item $std->item] Valor do FCP"
3003
                );
3004
                $this->dom->addChild(
3005
                    $icms,
3006
                    'modBCST',
3007
                    $std->modBCST,
3008
                    true,
3009
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS ST"
3010
                );
3011
                $this->dom->addChild(
3012
                    $icms,
3013
                    'pMVAST',
3014
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
3015
                    false,
3016
                    "$identificador [item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
3017
                );
3018
                $this->dom->addChild(
3019
                    $icms,
3020
                    'pRedBCST',
3021
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
3022
                    false,
3023
                    "$identificador [item $std->item] Percentual da Redução de BC do ICMS ST"
3024
                );
3025
                $this->dom->addChild(
3026
                    $icms,
3027
                    'vBCST',
3028
                    $this->conditionalNumberFormatting($std->vBCST),
3029
                    true,
3030
                    "$identificador [item $std->item] Valor da BC do ICMS ST"
3031
                );
3032
                $this->dom->addChild(
3033
                    $icms,
3034
                    'pICMSST',
3035
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
3036
                    true,
3037
                    "$identificador [item $std->item] Alíquota do imposto do ICMS ST"
3038
                );
3039
                $this->dom->addChild(
3040
                    $icms,
3041
                    'vICMSST',
3042
                    $this->conditionalNumberFormatting($std->vICMSST),
3043
                    true,
3044
                    "$identificador [item $std->item] Valor do ICMS ST"
3045
                );
3046
                $this->dom->addChild(
3047
                    $icms,
3048
                    'vBCFCPST',
3049
                    $this->conditionalNumberFormatting($std->vBCFCPST),
3050
                    false,
3051
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP ST"
3052
                );
3053
                $this->dom->addChild(
3054
                    $icms,
3055
                    'pFCPST',
3056
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
3057
                    false,
3058
                    "$identificador [item $std->item] Percentual do Fundo de "
3059
                    . "Combate à Pobreza (FCP) ST"
3060
                );
3061
                $this->dom->addChild(
3062
                    $icms,
3063
                    'vFCPST',
3064
                    $this->conditionalNumberFormatting($std->vFCPST),
3065
                    false,
3066
                    "$identificador [item $std->item] Valor do FCP ST"
3067
                );
3068
                break;
3069
            case '20':
3070
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
3071
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
3072
3073
                $icms = $this->dom->createElement("ICMS20");
3074
                $this->dom->addChild(
3075
                    $icms,
3076
                    'orig',
3077
                    $std->orig,
3078
                    true,
3079
                    "$identificador [item $std->item] Origem da mercadoria"
3080
                );
3081
                $this->dom->addChild(
3082
                    $icms,
3083
                    'CST',
3084
                    $std->CST,
3085
                    true,
3086
                    "$identificador [item $std->item] Tributação do ICMS = 20"
3087
                );
3088
                $this->dom->addChild(
3089
                    $icms,
3090
                    'modBC',
3091
                    $std->modBC,
3092
                    true,
3093
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
3094
                );
3095
                $this->dom->addChild(
3096
                    $icms,
3097
                    'pRedBC',
3098
                    $this->conditionalNumberFormatting($std->pRedBC, 4),
3099
                    true,
3100
                    "$identificador [item $std->item] Percentual da Redução de BC"
3101
                );
3102
                $this->dom->addChild(
3103
                    $icms,
3104
                    'vBC',
3105
                    $this->conditionalNumberFormatting($std->vBC),
3106
                    true,
3107
                    "$identificador [item $std->item] Valor da BC do ICMS"
3108
                );
3109
                $this->dom->addChild(
3110
                    $icms,
3111
                    'pICMS',
3112
                    $this->conditionalNumberFormatting($std->pICMS, 4),
3113
                    true,
3114
                    "$identificador [item $std->item] Alíquota do imposto"
3115
                );
3116
                $this->dom->addChild(
3117
                    $icms,
3118
                    'vICMS',
3119
                    $this->conditionalNumberFormatting($std->vICMS),
3120
                    true,
3121
                    "$identificador [item $std->item] Valor do ICMS"
3122
                );
3123
                $this->dom->addChild(
3124
                    $icms,
3125
                    'vBCFCP',
3126
                    $this->conditionalNumberFormatting($std->vBCFCP),
3127
                    false,
3128
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP"
3129
                );
3130
                $this->dom->addChild(
3131
                    $icms,
3132
                    'pFCP',
3133
                    $this->conditionalNumberFormatting($std->pFCP, 4),
3134
                    false,
3135
                    "$identificador [item $std->item] Percentual do Fundo de "
3136
                    . "Combate à Pobreza (FCP)"
3137
                );
3138
                $this->dom->addChild(
3139
                    $icms,
3140
                    'vFCP',
3141
                    $this->conditionalNumberFormatting($std->vFCP),
3142
                    false,
3143
                    "$identificador [item $std->item] Valor do FCP"
3144
                );
3145
                $this->dom->addChild(
3146
                    $icms,
3147
                    'vICMSDeson',
3148
                    $this->conditionalNumberFormatting($std->vICMSDeson),
3149
                    false,
3150
                    "$identificador [item $std->item] Valor do ICMS desonerado"
3151
                );
3152
                $this->dom->addChild(
3153
                    $icms,
3154
                    'motDesICMS',
3155
                    $std->motDesICMS,
3156
                    false,
3157
                    "$identificador [item $std->item] Motivo da desoneração do ICMS"
3158
                );
3159
                break;
3160
            case '30':
3161
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
3162
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
3163
3164
                $icms = $this->dom->createElement("ICMS30");
3165
                $this->dom->addChild(
3166
                    $icms,
3167
                    'orig',
3168
                    $std->orig,
3169
                    true,
3170
                    "$identificador [item $std->item] Origem da mercadoria"
3171
                );
3172
                $this->dom->addChild(
3173
                    $icms,
3174
                    'CST',
3175
                    $std->CST,
3176
                    true,
3177
                    "$identificador [item $std->item] Tributação do ICMS = 30"
3178
                );
3179
                $this->dom->addChild(
3180
                    $icms,
3181
                    'modBCST',
3182
                    $std->modBCST,
3183
                    true,
3184
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS ST"
3185
                );
3186
                $this->dom->addChild(
3187
                    $icms,
3188
                    'pMVAST',
3189
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
3190
                    false,
3191
                    "$identificador [item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
3192
                );
3193
                $this->dom->addChild(
3194
                    $icms,
3195
                    'pRedBCST',
3196
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
3197
                    false,
3198
                    "$identificador [item $std->item] Percentual da Redução de BC do ICMS ST"
3199
                );
3200
                $this->dom->addChild(
3201
                    $icms,
3202
                    'vBCST',
3203
                    $this->conditionalNumberFormatting($std->vBCST),
3204
                    true,
3205
                    "$identificador [item $std->item] Valor da BC do ICMS ST"
3206
                );
3207
                $this->dom->addChild(
3208
                    $icms,
3209
                    'pICMSST',
3210
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
3211
                    true,
3212
                    "$identificador [item $std->item] Alíquota do imposto do ICMS ST"
3213
                );
3214
                $this->dom->addChild(
3215
                    $icms,
3216
                    'vICMSST',
3217
                    $this->conditionalNumberFormatting($std->vICMSST),
3218
                    true,
3219
                    "$identificador [item $std->item] Valor do ICMS ST"
3220
                );
3221
                $this->dom->addChild(
3222
                    $icms,
3223
                    'vBCFCPST',
3224
                    $this->conditionalNumberFormatting($std->vBCFCPST),
3225
                    false,
3226
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP ST"
3227
                );
3228
                $this->dom->addChild(
3229
                    $icms,
3230
                    'pFCPST',
3231
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
3232
                    false,
3233
                    "$identificador [item $std->item] Percentual do Fundo de "
3234
                    . "Combate à Pobreza (FCP) ST"
3235
                );
3236
                $this->dom->addChild(
3237
                    $icms,
3238
                    'vFCPST',
3239
                    $this->conditionalNumberFormatting($std->vFCPST),
3240
                    false,
3241
                    "$identificador [item $std->item] Valor do FCP ST"
3242
                );
3243
                $this->dom->addChild(
3244
                    $icms,
3245
                    'vICMSDeson',
3246
                    $this->conditionalNumberFormatting($std->vICMSDeson),
3247
                    false,
3248
                    "$identificador [item $std->item] Valor do ICMS desonerado"
3249
                );
3250
                $this->dom->addChild(
3251
                    $icms,
3252
                    'motDesICMS',
3253
                    $std->motDesICMS,
3254
                    false,
3255
                    "$identificador [item $std->item] Motivo da desoneração do ICMS"
3256
                );
3257
                break;
3258
            case '40':
3259
            case '41':
3260
            case '50':
3261
                $icms = $this->dom->createElement("ICMS40");
3262
                $this->dom->addChild(
3263
                    $icms,
3264
                    'orig',
3265
                    $std->orig,
3266
                    true,
3267
                    "$identificador [item $std->item] Origem da mercadoria"
3268
                );
3269
                $this->dom->addChild(
3270
                    $icms,
3271
                    'CST',
3272
                    $std->CST,
3273
                    true,
3274
                    "$identificador [item $std->item] Tributação do ICMS $std->CST"
3275
                );
3276
                $this->dom->addChild(
3277
                    $icms,
3278
                    'vICMSDeson',
3279
                    $this->conditionalNumberFormatting($std->vICMSDeson),
3280
                    false,
3281
                    "$identificador [item $std->item] Valor do ICMS desonerado"
3282
                );
3283
                $this->dom->addChild(
3284
                    $icms,
3285
                    'motDesICMS',
3286
                    $std->motDesICMS,
3287
                    false,
3288
                    "$identificador [item $std->item] Motivo da desoneração do ICMS"
3289
                );
3290
                break;
3291
            case '51':
3292
                $icms = $this->dom->createElement("ICMS51");
3293
                $this->dom->addChild(
3294
                    $icms,
3295
                    'orig',
3296
                    $std->orig,
3297
                    true,
3298
                    "$identificador [item $std->item] Origem da mercadoria"
3299
                );
3300
                $this->dom->addChild(
3301
                    $icms,
3302
                    'CST',
3303
                    $std->CST,
3304
                    true,
3305
                    "$identificador [item $std->item] Tributação do ICMS = 51"
3306
                );
3307
                $this->dom->addChild(
3308
                    $icms,
3309
                    'modBC',
3310
                    $std->modBC,
3311
                    false,
3312
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
3313
                );
3314
                $this->dom->addChild(
3315
                    $icms,
3316
                    'pRedBC',
3317
                    $this->conditionalNumberFormatting($std->pRedBC, 4),
3318
                    false,
3319
                    "$identificador [item $std->item] Percentual da Redução de BC"
3320
                );
3321
                $this->dom->addChild(
3322
                    $icms,
3323
                    'vBC',
3324
                    $this->conditionalNumberFormatting($std->vBC),
3325
                    false,
3326
                    "$identificador [item $std->item] Valor da BC do ICMS"
3327
                );
3328
                $this->dom->addChild(
3329
                    $icms,
3330
                    'pICMS',
3331
                    $this->conditionalNumberFormatting($std->pICMS, 4),
3332
                    false,
3333
                    "$identificador [item $std->item] Alíquota do imposto"
3334
                );
3335
                $this->dom->addChild(
3336
                    $icms,
3337
                    'vICMSOp',
3338
                    $this->conditionalNumberFormatting($std->vICMSOp),
3339
                    false,
3340
                    "$identificador [item $std->item] Valor do ICMS da Operação"
3341
                );
3342
                $this->dom->addChild(
3343
                    $icms,
3344
                    'pDif',
3345
                    $this->conditionalNumberFormatting($std->pDif, 4),
3346
                    false,
3347
                    "$identificador [item $std->item] Percentual do diferimento"
3348
                );
3349
                $this->dom->addChild(
3350
                    $icms,
3351
                    'vICMSDif',
3352
                    $this->conditionalNumberFormatting($std->vICMSDif),
3353
                    false,
3354
                    "$identificador [item $std->item] Valor do ICMS diferido"
3355
                );
3356
                $this->dom->addChild(
3357
                    $icms,
3358
                    'vICMS',
3359
                    $this->conditionalNumberFormatting($std->vICMS),
3360
                    false,
3361
                    "$identificador [item $std->item] Valor do ICMS realmente devido"
3362
                );
3363
                $this->dom->addChild(
3364
                    $icms,
3365
                    'vBCFCP',
3366
                    $this->conditionalNumberFormatting($std->vBCFCP),
3367
                    false,
3368
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP"
3369
                );
3370
                $this->dom->addChild(
3371
                    $icms,
3372
                    'pFCP',
3373
                    $this->conditionalNumberFormatting($std->pFCP, 4),
3374
                    false,
3375
                    "$identificador [item $std->item] Percentual do Fundo de "
3376
                    . "Combate à Pobreza (FCP)"
3377
                );
3378
                $this->dom->addChild(
3379
                    $icms,
3380
                    'vFCP',
3381
                    $this->conditionalNumberFormatting($std->vFCP),
3382
                    false,
3383
                    "$identificador [item $std->item] Valor do FCP"
3384
                );
3385
                break;
3386
            case '60':
3387
                $icms = $this->dom->createElement("ICMS60");
3388
                $this->dom->addChild(
3389
                    $icms,
3390
                    'orig',
3391
                    $std->orig,
3392
                    true,
3393
                    "$identificador [item $std->item] Origem da mercadoria"
3394
                );
3395
                $this->dom->addChild(
3396
                    $icms,
3397
                    'CST',
3398
                    $std->CST,
3399
                    true,
3400
                    "$identificador [item $std->item] Tributação do ICMS = 60"
3401
                );
3402
                $this->dom->addChild(
3403
                    $icms,
3404
                    'vBCSTRet',
3405
                    $this->conditionalNumberFormatting($std->vBCSTRet),
3406
                    false,
3407
                    "$identificador [item $std->item] Valor da BC do ICMS ST retido"
3408
                );
3409
                $this->dom->addChild(
3410
                    $icms,
3411
                    'pST',
3412
                    $this->conditionalNumberFormatting($std->pST, 4),
3413
                    false,
3414
                    "$identificador [item $std->item] Valor do ICMS ST retido"
3415
                );
3416
                $this->dom->addChild(
3417
                    $icms,
3418
                    'vICMSSubstituto',
3419
                    $this->conditionalNumberFormatting($std->vICMSSubstituto),
3420
                    false,
3421
                    "$identificador [item $std->item] Valor do ICMS próprio do Substituto"
3422
                );
3423
                $this->dom->addChild(
3424
                    $icms,
3425
                    'vICMSSTRet',
3426
                    $this->conditionalNumberFormatting($std->vICMSSTRet),
3427
                    false,
3428
                    "$identificador [item $std->item] Valor do ICMS ST retido"
3429
                );
3430
                $this->dom->addChild(
3431
                    $icms,
3432
                    'vBCFCPSTRet',
3433
                    $this->conditionalNumberFormatting($std->vBCFCPSTRet),
3434
                    false,
3435
                    "$identificador [item $std->item] Valor da Base de Cálculo "
3436
                    . "do FCP retido anteriormente por ST"
3437
                );
3438
                $this->dom->addChild(
3439
                    $icms,
3440
                    'pFCPSTRet',
3441
                    $this->conditionalNumberFormatting($std->pFCPSTRet, 4),
3442
                    false,
3443
                    "$identificador [item $std->item] Percentual do FCP retido "
3444
                    . "anteriormente por Substituição Tributária"
3445
                );
3446
                $this->dom->addChild(
3447
                    $icms,
3448
                    'vFCPSTRet',
3449
                    $this->conditionalNumberFormatting($std->vFCPSTRet),
3450
                    false,
3451
                    "$identificador [item $std->item] Valor do FCP retido por "
3452
                    . "Substituição Tributária"
3453
                );
3454
                $this->dom->addChild(
3455
                    $icms,
3456
                    'pRedBCEfet',
3457
                    $this->conditionalNumberFormatting($std->pRedBCEfet, 4),
3458
                    false,
3459
                    "$identificador [item $std->item] Percentual de redução "
3460
                    . "para obtenção da base de cálculo efetiva (vBCEfet)"
3461
                );
3462
                $this->dom->addChild(
3463
                    $icms,
3464
                    'vBCEfet',
3465
                    $this->conditionalNumberFormatting($std->vBCEfet),
3466
                    false,
3467
                    "$identificador [item $std->item] base de calculo efetiva"
3468
                );
3469
                $this->dom->addChild(
3470
                    $icms,
3471
                    'pICMSEfet',
3472
                    $this->conditionalNumberFormatting($std->pICMSEfet, 4),
3473
                    false,
3474
                    "$identificador [item $std->item] Alíquota do ICMS na operação a consumidor final"
3475
                );
3476
                $this->dom->addChild(
3477
                    $icms,
3478
                    'vICMSEfet',
3479
                    $this->conditionalNumberFormatting($std->vICMSEfet),
3480
                    false,
3481
                    "$identificador [item $std->item] Valor do ICMS efetivo"
3482
                );
3483
                break;
3484
            case '70':
3485
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
3486
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
3487
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
3488
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
3489
3490
                $icms = $this->dom->createElement("ICMS70");
3491
                $this->dom->addChild(
3492
                    $icms,
3493
                    'orig',
3494
                    $std->orig,
3495
                    true,
3496
                    "$identificador [item $std->item] Origem da mercadoria"
3497
                );
3498
                $this->dom->addChild(
3499
                    $icms,
3500
                    'CST',
3501
                    $std->CST,
3502
                    true,
3503
                    "$identificador [item $std->item] Tributação do ICMS = 70"
3504
                );
3505
                $this->dom->addChild(
3506
                    $icms,
3507
                    'modBC',
3508
                    $std->modBC,
3509
                    true,
3510
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
3511
                );
3512
                $this->dom->addChild(
3513
                    $icms,
3514
                    'pRedBC',
3515
                    $this->conditionalNumberFormatting($std->pRedBC, 4),
3516
                    true,
3517
                    "$identificador [item $std->item] Percentual da Redução de BC"
3518
                );
3519
                $this->dom->addChild(
3520
                    $icms,
3521
                    'vBC',
3522
                    $this->conditionalNumberFormatting($std->vBC),
3523
                    true,
3524
                    "$identificador [item $std->item] Valor da BC do ICMS"
3525
                );
3526
                $this->dom->addChild(
3527
                    $icms,
3528
                    'pICMS',
3529
                    $this->conditionalNumberFormatting($std->pICMS, 4),
3530
                    true,
3531
                    "$identificador [item $std->item] Alíquota do imposto"
3532
                );
3533
                $this->dom->addChild(
3534
                    $icms,
3535
                    'vICMS',
3536
                    $this->conditionalNumberFormatting($std->vICMS),
3537
                    true,
3538
                    "$identificador [item $std->item] Valor do ICMS"
3539
                );
3540
                $this->dom->addChild(
3541
                    $icms,
3542
                    'vBCFCP',
3543
                    $this->conditionalNumberFormatting($std->vBCFCP),
3544
                    false,
3545
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP"
3546
                );
3547
                $this->dom->addChild(
3548
                    $icms,
3549
                    'pFCP',
3550
                    $this->conditionalNumberFormatting($std->pFCP, 4),
3551
                    false,
3552
                    "$identificador [item $std->item] Percentual do Fundo de "
3553
                    . "Combate à Pobreza (FCP)"
3554
                );
3555
                $this->dom->addChild(
3556
                    $icms,
3557
                    'vFCP',
3558
                    $std->vFCP,
3559
                    false,
3560
                    "$identificador [item $std->item] Valor do FCP"
3561
                );
3562
                $this->dom->addChild(
3563
                    $icms,
3564
                    'modBCST',
3565
                    $std->modBCST,
3566
                    true,
3567
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS ST"
3568
                );
3569
                $this->dom->addChild(
3570
                    $icms,
3571
                    'pMVAST',
3572
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
3573
                    false,
3574
                    "$identificador [item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
3575
                );
3576
                $this->dom->addChild(
3577
                    $icms,
3578
                    'pRedBCST',
3579
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
3580
                    false,
3581
                    "$identificador [item $std->item] Percentual da Redução de BC do ICMS ST"
3582
                );
3583
                $this->dom->addChild(
3584
                    $icms,
3585
                    'vBCST',
3586
                    $this->conditionalNumberFormatting($std->vBCST),
3587
                    true,
3588
                    "$identificador [item $std->item] Valor da BC do ICMS ST"
3589
                );
3590
                $this->dom->addChild(
3591
                    $icms,
3592
                    'pICMSST',
3593
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
3594
                    true,
3595
                    "$identificador [item $std->item] Alíquota do imposto do ICMS ST"
3596
                );
3597
                $this->dom->addChild(
3598
                    $icms,
3599
                    'vICMSST',
3600
                    $this->conditionalNumberFormatting($std->vICMSST),
3601
                    true,
3602
                    "$identificador [item $std->item] Valor do ICMS ST"
3603
                );
3604
                $this->dom->addChild(
3605
                    $icms,
3606
                    'vBCFCPST',
3607
                    $this->conditionalNumberFormatting($std->vBCFCPST),
3608
                    false,
3609
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP ST"
3610
                );
3611
                $this->dom->addChild(
3612
                    $icms,
3613
                    'pFCPST',
3614
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
3615
                    false,
3616
                    "$identificador [item $std->item] Percentual do Fundo de "
3617
                    . "Combate à Pobreza (FCP) ST"
3618
                );
3619
                $this->dom->addChild(
3620
                    $icms,
3621
                    'vFCPST',
3622
                    $this->conditionalNumberFormatting($std->vFCPST),
3623
                    false,
3624
                    "$identificador [item $std->item] Valor do FCP ST"
3625
                );
3626
                $this->dom->addChild(
3627
                    $icms,
3628
                    'vICMSDeson',
3629
                    $this->conditionalNumberFormatting($std->vICMSDeson),
3630
                    false,
3631
                    "$identificador [item $std->item] Valor do ICMS desonerado"
3632
                );
3633
                $this->dom->addChild(
3634
                    $icms,
3635
                    'motDesICMS',
3636
                    $std->motDesICMS,
3637
                    false,
3638
                    "$identificador [item $std->item] Motivo da desoneração do ICMS"
3639
                );
3640
                break;
3641
            case '90':
3642
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
3643
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
3644
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
3645
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
3646
3647
                $icms = $this->dom->createElement("ICMS90");
3648
                $this->dom->addChild(
3649
                    $icms,
3650
                    'orig',
3651
                    $std->orig,
3652
                    true,
3653
                    "$identificador [item $std->item] Origem da mercadoria"
3654
                );
3655
                $this->dom->addChild(
3656
                    $icms,
3657
                    'CST',
3658
                    $std->CST,
3659
                    true,
3660
                    "$identificador [item $std->item] Tributação do ICMS = 90"
3661
                );
3662
                $this->dom->addChild(
3663
                    $icms,
3664
                    'modBC',
3665
                    $std->modBC,
3666
                    false,
3667
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS"
3668
                );
3669
                $this->dom->addChild(
3670
                    $icms,
3671
                    'vBC',
3672
                    $this->conditionalNumberFormatting($std->vBC),
3673
                    false,
3674
                    "$identificador [item $std->item] Valor da BC do ICMS"
3675
                );
3676
                $this->dom->addChild(
3677
                    $icms,
3678
                    'pRedBC',
3679
                    $this->conditionalNumberFormatting($std->pRedBC, 4),
3680
                    false,
3681
                    "$identificador [item $std->item] Percentual da Redução de BC"
3682
                );
3683
                $this->dom->addChild(
3684
                    $icms,
3685
                    'pICMS',
3686
                    $this->conditionalNumberFormatting($std->pICMS, 4),
3687
                    false,
3688
                    "$identificador [item $std->item] Alíquota do imposto"
3689
                );
3690
                $this->dom->addChild(
3691
                    $icms,
3692
                    'vICMS',
3693
                    $this->conditionalNumberFormatting($std->vICMS),
3694
                    false,
3695
                    "$identificador [item $std->item] Valor do ICMS"
3696
                );
3697
                $this->dom->addChild(
3698
                    $icms,
3699
                    'vBCFCP',
3700
                    $this->conditionalNumberFormatting($std->vBCFCP),
3701
                    false,
3702
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP"
3703
                );
3704
                $this->dom->addChild(
3705
                    $icms,
3706
                    'pFCP',
3707
                    $this->conditionalNumberFormatting($std->pFCP, 4),
3708
                    false,
3709
                    "$identificador [item $std->item] Percentual do Fundo de "
3710
                    . "Combate à Pobreza (FCP)"
3711
                );
3712
                $this->dom->addChild(
3713
                    $icms,
3714
                    'vFCP',
3715
                    $this->conditionalNumberFormatting($std->vFCP),
3716
                    false,
3717
                    "$identificador [item $std->item] Valor do FCP"
3718
                );
3719
                $this->dom->addChild(
3720
                    $icms,
3721
                    'modBCST',
3722
                    $std->modBCST,
3723
                    false,
3724
                    "$identificador [item $std->item] Modalidade de determinação da BC do ICMS ST"
3725
                );
3726
                $this->dom->addChild(
3727
                    $icms,
3728
                    'pMVAST',
3729
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
3730
                    false,
3731
                    "$identificador [item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
3732
                );
3733
                $this->dom->addChild(
3734
                    $icms,
3735
                    'pRedBCST',
3736
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
3737
                    false,
3738
                    "$identificador [item $std->item] Percentual da Redução de BC do ICMS ST"
3739
                );
3740
                $this->dom->addChild(
3741
                    $icms,
3742
                    'vBCST',
3743
                    $this->conditionalNumberFormatting($std->vBCST),
3744
                    false,
3745
                    "$identificador [item $std->item] Valor da BC do ICMS ST"
3746
                );
3747
                $this->dom->addChild(
3748
                    $icms,
3749
                    'pICMSST',
3750
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
3751
                    false,
3752
                    "$identificador [item $std->item] Alíquota do imposto do ICMS ST"
3753
                );
3754
                $this->dom->addChild(
3755
                    $icms,
3756
                    'vICMSST',
3757
                    $this->conditionalNumberFormatting($std->vICMSST),
3758
                    false,
3759
                    "$identificador [item $std->item] Valor do ICMS ST"
3760
                );
3761
                $this->dom->addChild(
3762
                    $icms,
3763
                    'vBCFCPST',
3764
                    $this->conditionalNumberFormatting($std->vBCFCPST),
3765
                    false,
3766
                    "$identificador [item $std->item] Valor da Base de Cálculo do FCP ST"
3767
                );
3768
                $this->dom->addChild(
3769
                    $icms,
3770
                    'pFCPST',
3771
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
3772
                    false,
3773
                    "$identificador [item $std->item] Percentual do Fundo de "
3774
                    . "Combate à Pobreza (FCP) ST"
3775
                );
3776
                $this->dom->addChild(
3777
                    $icms,
3778
                    'vFCPST',
3779
                    $this->conditionalNumberFormatting($std->vFCPST),
3780
                    false,
3781
                    "$identificador [item $std->item] Valor do FCP ST"
3782
                );
3783
                $this->dom->addChild(
3784
                    $icms,
3785
                    'vICMSDeson',
3786
                    $this->conditionalNumberFormatting($std->vICMSDeson),
3787
                    false,
3788
                    "$identificador [item $std->item] Valor do ICMS desonerado"
3789
                );
3790
                $this->dom->addChild(
3791
                    $icms,
3792
                    'motDesICMS',
3793
                    $std->motDesICMS,
3794
                    false,
3795
                    "$identificador [item $std->item] Motivo da desoneração do ICMS"
3796
                );
3797
                break;
3798
        }
3799
        $tagIcms = $this->dom->createElement('ICMS');
3800
        if (isset($icms)) {
3801
            $tagIcms->appendChild($icms);
3802
        }
3803
        $this->aICMS[$std->item] = $tagIcms;
3804
        return $tagIcms;
3805
    }
3806
3807
    /**
3808
     * Grupo de Partilha do ICMS entre a UF de origem e UF de destino ou
3809
     * a UF definida na legislação. N10a pai N01
3810
     * tag NFe/infNFe/det[]/imposto/ICMS/ICMSPart
3811
     * @param stdClass $std
3812
     * @return DOMElement
3813
     */
3814
    public function tagICMSPart(stdClass $std)
3815
    {
3816
        $possible = [
3817
            'item',
3818
            'orig',
3819
            'CST',
3820
            'modBC',
3821
            'vBC',
3822
            'pRedBC',
3823
            'pICMS',
3824
            'vICMS',
3825
            'modBCST',
3826
            'pMVAST',
3827
            'pRedBCST',
3828
            'vBCST',
3829
            'pICMSST',
3830
            'vICMSST',
3831
            'pBCOp',
3832
            'UFST'
3833
        ];
3834
        $std = $this->equilizeParameters($std, $possible);
3835
        $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
3836
        $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
3837
        $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
3838
        $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
3839
        $icmsPart = $this->dom->createElement("ICMSPart");
3840
        $this->dom->addChild(
3841
            $icmsPart,
3842
            'orig',
3843
            $std->orig,
3844
            true,
3845
            "[item $std->item] Origem da mercadoria"
3846
        );
3847
        $this->dom->addChild(
3848
            $icmsPart,
3849
            'CST',
3850
            $std->CST,
3851
            true,
3852
            "[item $std->item] Tributação do ICMS 10 ou 90"
3853
        );
3854
        $this->dom->addChild(
3855
            $icmsPart,
3856
            'modBC',
3857
            $std->modBC,
3858
            true,
3859
            "[item $std->item] Modalidade de determinação da BC do ICMS"
3860
        );
3861
        $this->dom->addChild(
3862
            $icmsPart,
3863
            'vBC',
3864
            $this->conditionalNumberFormatting($std->vBC),
3865
            true,
3866
            "[item $std->item] Valor da BC do ICMS"
3867
        );
3868
        $this->dom->addChild(
3869
            $icmsPart,
3870
            'pRedBC',
3871
            $this->conditionalNumberFormatting($std->pRedBC, 4),
3872
            false,
3873
            "[item $std->item] Percentual da Redução de BC"
3874
        );
3875
        $this->dom->addChild(
3876
            $icmsPart,
3877
            'pICMS',
3878
            $this->conditionalNumberFormatting($std->pICMS, 4),
3879
            true,
3880
            "[item $std->item] Alíquota do imposto"
3881
        );
3882
        $this->dom->addChild(
3883
            $icmsPart,
3884
            'vICMS',
3885
            $this->conditionalNumberFormatting($std->vICMS),
3886
            true,
3887
            "[item $std->item] Valor do ICMS"
3888
        );
3889
        $this->dom->addChild(
3890
            $icmsPart,
3891
            'modBCST',
3892
            $std->modBCST,
3893
            true,
3894
            "[item $std->item] Modalidade de determinação da BC do ICMS ST"
3895
        );
3896
        $this->dom->addChild(
3897
            $icmsPart,
3898
            'pMVAST',
3899
            $this->conditionalNumberFormatting($std->pMVAST, 4),
3900
            false,
3901
            "[item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
3902
        );
3903
        $this->dom->addChild(
3904
            $icmsPart,
3905
            'pRedBCST',
3906
            $this->conditionalNumberFormatting($std->pRedBCST, 4),
3907
            false,
3908
            "[item $std->item] Percentual da Redução de BC do ICMS ST"
3909
        );
3910
        $this->dom->addChild(
3911
            $icmsPart,
3912
            'vBCST',
3913
            $this->conditionalNumberFormatting($std->vBCST),
3914
            true,
3915
            "[item $std->item] Valor da BC do ICMS ST"
3916
        );
3917
        $this->dom->addChild(
3918
            $icmsPart,
3919
            'pICMSST',
3920
            $this->conditionalNumberFormatting($std->pICMSST, 4),
3921
            true,
3922
            "[item $std->item] Alíquota do imposto do ICMS ST"
3923
        );
3924
        $this->dom->addChild(
3925
            $icmsPart,
3926
            'vICMSST',
3927
            $this->conditionalNumberFormatting($std->vICMSST),
3928
            true,
3929
            "[item $std->item] Valor do ICMS ST"
3930
        );
3931
        $this->dom->addChild(
3932
            $icmsPart,
3933
            'pBCOp',
3934
            $this->conditionalNumberFormatting($std->pBCOp, 4),
3935
            true,
3936
            "[item $std->item] Percentual da BC operação própria"
3937
        );
3938
        $this->dom->addChild(
3939
            $icmsPart,
3940
            'UFST',
3941
            $std->UFST,
3942
            true,
3943
            "[item $std->item] UF para qual é devido o ICMS ST"
3944
        );
3945
        //caso exista a tag aICMS[$std->item] inserir nela caso contrario criar
3946
        if (!empty($this->aICMS[$std->item])) {
3947
            $tagIcms = $this->aICMS[$std->item];
3948
        } else {
3949
            $tagIcms = $this->dom->createElement('ICMS');
3950
        }
3951
        $this->dom->appChild($tagIcms, $icmsPart, "Inserindo ICMSPart em ICMS[$std->item]");
3952
        $this->aICMS[$std->item] = $tagIcms;
3953
        return $tagIcms;
3954
    }
3955
3956
    /**
3957
     * Grupo de Repasse de ICMSST retido anteriormente em operações
3958
     * interestaduais com repasses através do Substituto Tributário
3959
     * NOTA: ajustado NT 2018.005
3960
     * tag NFe/infNFe/det[]/imposto/ICMS/ICMSST N10b pai N01
3961
     * @param stdClass $std
3962
     * @return DOMElement
3963
     */
3964
    public function tagICMSST(stdClass $std)
3965
    {
3966
        $possible = [
3967
            'item',
3968
            'orig',
3969
            'CST',
3970
            'vBCSTRet',
3971
            'vICMSSTRet',
3972
            'vBCSTDest',
3973
            'vICMSSTDest',
3974
            'vBCFCPSTRet',
3975
            'pFCPSTRet',
3976
            'vFCPSTRet',
3977
            'pST',
3978
            'vICMSSubstituto',
3979
            'pRedBCEfet',
3980
            'vBCEfet',
3981
            'pICMSEfet',
3982
            'vICMSEfet'
3983
        ];
3984
        $std = $this->equilizeParameters($std, $possible);
3985
        $icmsST = $this->dom->createElement("ICMSST");
3986
        $this->dom->addChild(
3987
            $icmsST,
3988
            'orig',
3989
            $std->orig,
3990
            true,
3991
            "[item $std->item] Origem da mercadoria"
3992
        );
3993
        $this->dom->addChild(
3994
            $icmsST,
3995
            'CST',
3996
            $std->CST,
3997
            true,
3998
            "[item $std->item] Tributação do ICMS 41 ou 60"
3999
        );
4000
        $this->dom->addChild(
4001
            $icmsST,
4002
            'vBCSTRet',
4003
            $this->conditionalNumberFormatting($std->vBCSTRet),
4004
            true,
4005
            "[item $std->item] Valor do BC do ICMS ST retido na UF remetente"
4006
        );
4007
        $this->dom->addChild(
4008
            $icmsST,
4009
            'pST',
4010
            $this->conditionalNumberFormatting($std->pST, 4),
4011
            false,
4012
            "[item $std->item] Alíquota suportada pelo Consumidor Final"
4013
        );
4014
        $this->dom->addChild(
4015
            $icmsST,
4016
            'vICMSSubstituto',
4017
            $this->conditionalNumberFormatting($std->vICMSSubstituto),
4018
            false,
4019
            "[item $std->item] Valor do ICMS próprio do Substituto"
4020
        );
4021
        $this->dom->addChild(
4022
            $icmsST,
4023
            'vICMSSTRet',
4024
            $this->conditionalNumberFormatting($std->vICMSSTRet),
4025
            true,
4026
            "[item $std->item] Valor do ICMS ST retido na UF remetente"
4027
        );
4028
        $this->dom->addChild(
4029
            $icmsST,
4030
            'vBCFCPSTRet',
4031
            $this->conditionalNumberFormatting($std->vBCFCPSTRet),
4032
            false,
4033
            "[item $std->item] Valor da Base de Cálculo do FCP"
4034
        );
4035
        $this->dom->addChild(
4036
            $icmsST,
4037
            'pFCPSTRet',
4038
            $this->conditionalNumberFormatting($std->pFCPSTRet, 4),
4039
            false,
4040
            "[item $std->item] Percentual do FCP retido"
4041
        );
4042
        $this->dom->addChild(
4043
            $icmsST,
4044
            'vFCPSTRet',
4045
            $this->conditionalNumberFormatting($std->vFCPSTRet),
4046
            false,
4047
            "[item $std->item] Valor do FCP retido"
4048
        );
4049
        $this->dom->addChild(
4050
            $icmsST,
4051
            'vBCSTDest',
4052
            $this->conditionalNumberFormatting($std->vBCSTDest),
4053
            true,
4054
            "[item $std->item] Valor da BC do ICMS ST da UF destino"
4055
        );
4056
        $this->dom->addChild(
4057
            $icmsST,
4058
            'vICMSSTDest',
4059
            $this->conditionalNumberFormatting($std->vICMSSTDest),
4060
            true,
4061
            "[item $std->item] Valor do ICMS ST da UF destino"
4062
        );
4063
        $this->dom->addChild(
4064
            $icmsST,
4065
            'pRedBCEfet',
4066
            $this->conditionalNumberFormatting($std->pRedBCEfet, 4),
4067
            false,
4068
            "[item $std->item] Percentual de redução da base de cálculo efetiva"
4069
        );
4070
        $this->dom->addChild(
4071
            $icmsST,
4072
            'vBCEfet',
4073
            $this->conditionalNumberFormatting($std->vBCEfet),
4074
            false,
4075
            "[item $std->item] Valor da base de cálculo efetiva"
4076
        );
4077
        $this->dom->addChild(
4078
            $icmsST,
4079
            'pICMSEfet',
4080
            $this->conditionalNumberFormatting($std->pICMSEfet, 4),
4081
            false,
4082
            "[item $std->item] Alíquota do ICMS efetiva"
4083
        );
4084
        $this->dom->addChild(
4085
            $icmsST,
4086
            'vICMSEfet',
4087
            $this->conditionalNumberFormatting($std->vICMSEfet),
4088
            false,
4089
            "[item $std->item] Valor do ICMS efetivo"
4090
        );
4091
        //caso exista a tag aICMS[$std->item] inserir nela caso contrario criar
4092
        if (!empty($this->aICMS[$std->item])) {
4093
            $tagIcms = $this->aICMS[$std->item];
4094
        } else {
4095
            $tagIcms = $this->dom->createElement('ICMS');
4096
        }
4097
        $this->dom->appChild($tagIcms, $icmsST, "Inserindo ICMSST em ICMS[$std->item]");
4098
        $this->aICMS[$std->item] = $tagIcms;
4099
        return $tagIcms;
4100
    }
4101
4102
    /**
4103
     * Tributação ICMS pelo Simples Nacional N10c pai N01
4104
     * tag NFe/infNFe/det[]/imposto/ICMS/ICMSSN N10c pai N01
4105
     * @param stdClass $std
4106
     * @return DOMElement
4107
     */
4108
    public function tagICMSSN(stdClass $std)
4109
    {
4110
        $possible = [
4111
            'item',
4112
            'orig',
4113
            'CSOSN',
4114
            'pCredSN',
4115
            'vCredICMSSN',
4116
            'modBCST',
4117
            'pMVAST',
4118
            'pRedBCST',
4119
            'vBCST',
4120
            'pICMSST',
4121
            'vICMSST',
4122
            'vBCFCPST',
4123
            'pFCPST',
4124
            'vFCPST',
4125
            'vBCSTRet',
4126
            'pST',
4127
            'vICMSSTRet',
4128
            'vBCFCPSTRet',
4129
            'pFCPSTRet',
4130
            'vFCPSTRet',
4131
            'modBC',
4132
            'vBC',
4133
            'pRedBC',
4134
            'pICMS',
4135
            'vICMS',
4136
            'pRedBCEfet',
4137
            'vBCEfet',
4138
            'pICMSEfet',
4139
            'vICMSEfet',
4140
            'vICMSSubstituto'
4141
        ];
4142
        $std = $this->equilizeParameters($std, $possible);
4143
        //totalizador generico
4144
        $this->stdTot->vFCPST += (float) !empty($std->vFCPST) ? $std->vFCPST : 0;
4145
        $this->stdTot->vFCPSTRet += (float) !empty($std->vFCPSTRet) ? $std->vFCPSTRet : 0;
4146
        switch ($std->CSOSN) {
4147
            case '101':
4148
                $icmsSN = $this->dom->createElement("ICMSSN101");
4149
                $this->dom->addChild(
4150
                    $icmsSN,
4151
                    'orig',
4152
                    $std->orig,
4153
                    true,
4154
                    "[item $std->item] Origem da mercadoria"
4155
                );
4156
                $this->dom->addChild(
4157
                    $icmsSN,
4158
                    'CSOSN',
4159
                    $std->CSOSN,
4160
                    true,
4161
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4162
                );
4163
                $this->dom->addChild(
4164
                    $icmsSN,
4165
                    'pCredSN',
4166
                    $this->conditionalNumberFormatting($std->pCredSN, 2),
4167
                    true,
4168
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4169
                );
4170
                $this->dom->addChild(
4171
                    $icmsSN,
4172
                    'vCredICMSSN',
4173
                    $this->conditionalNumberFormatting($std->vCredICMSSN),
4174
                    true,
4175
                    "[item $std->item] Valor crédito do ICMS que pode ser aproveitado nos termos do"
4176
                    . " art. 23 da LC 123 (Simples Nacional)"
4177
                );
4178
                break;
4179
            case '102':
4180
            case '103':
4181
            case '300':
4182
            case '400':
4183
                $icmsSN = $this->dom->createElement("ICMSSN102");
4184
                $this->dom->addChild(
4185
                    $icmsSN,
4186
                    'orig',
4187
                    $std->orig,
4188
                    true,
4189
                    "[item $std->item] Origem da mercadoria"
4190
                );
4191
                $this->dom->addChild(
4192
                    $icmsSN,
4193
                    'CSOSN',
4194
                    $std->CSOSN,
4195
                    true,
4196
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4197
                );
4198
                break;
4199
            case '201':
4200
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
4201
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
4202
4203
                $icmsSN = $this->dom->createElement("ICMSSN201");
4204
                $this->dom->addChild(
4205
                    $icmsSN,
4206
                    'orig',
4207
                    $std->orig,
4208
                    true,
4209
                    "[item $std->item] Origem da mercadoria"
4210
                );
4211
                $this->dom->addChild(
4212
                    $icmsSN,
4213
                    'CSOSN',
4214
                    $std->CSOSN,
4215
                    true,
4216
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4217
                );
4218
                $this->dom->addChild(
4219
                    $icmsSN,
4220
                    'modBCST',
4221
                    $std->modBCST,
4222
                    true,
4223
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4224
                );
4225
                $this->dom->addChild(
4226
                    $icmsSN,
4227
                    'pMVAST',
4228
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
4229
                    false,
4230
                    "[item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
4231
                );
4232
                $this->dom->addChild(
4233
                    $icmsSN,
4234
                    'pRedBCST',
4235
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
4236
                    false,
4237
                    "[item $std->item] Percentual da Redução de BC do ICMS ST"
4238
                );
4239
                $this->dom->addChild(
4240
                    $icmsSN,
4241
                    'vBCST',
4242
                    $this->conditionalNumberFormatting($std->vBCST),
4243
                    true,
4244
                    "[item $std->item] Valor da BC do ICMS ST"
4245
                );
4246
                $this->dom->addChild(
4247
                    $icmsSN,
4248
                    'pICMSST',
4249
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
4250
                    true,
4251
                    "[item $std->item] Alíquota do imposto do ICMS ST"
4252
                );
4253
                $this->dom->addChild(
4254
                    $icmsSN,
4255
                    'vICMSST',
4256
                    $this->conditionalNumberFormatting($std->vICMSST),
4257
                    true,
4258
                    "[item $std->item] Valor do ICMS ST"
4259
                );
4260
                $this->dom->addChild(
4261
                    $icmsSN,
4262
                    'vBCFCPST',
4263
                    $this->conditionalNumberFormatting($std->vBCFCPST),
4264
                    isset($std->vBCFCPST) ? true : false,
4265
                    "[item $std->item] Valor da Base de Cálculo do FCP "
4266
                    . "retido por Substituição Tributária"
4267
                );
4268
                $this->dom->addChild(
4269
                    $icmsSN,
4270
                    'pFCPST',
4271
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
4272
                    isset($std->pFCPST) ? true : false,
4273
                    "[item $std->item] Percentual do FCP retido por "
4274
                    . "Substituição Tributária"
4275
                );
4276
                $this->dom->addChild(
4277
                    $icmsSN,
4278
                    'vFCPST',
4279
                    $this->conditionalNumberFormatting($std->vFCPST),
4280
                    isset($std->vFCPST) ? true : false,
4281
                    "[item $std->item] Valor do FCP retido por Substituição Tributária"
4282
                );
4283
                $this->dom->addChild(
4284
                    $icmsSN,
4285
                    'pCredSN',
4286
                    $this->conditionalNumberFormatting($std->pCredSN, 4),
4287
                    false,
4288
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4289
                );
4290
                $this->dom->addChild(
4291
                    $icmsSN,
4292
                    'vCredICMSSN',
4293
                    $this->conditionalNumberFormatting($std->vCredICMSSN),
4294
                    false,
4295
                    "[item $std->item] Valor crédito do ICMS que pode ser aproveitado nos "
4296
                    . "termos do art. 23 da LC 123 (Simples Nacional)"
4297
                );
4298
                break;
4299
            case '202':
4300
            case '203':
4301
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
4302
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
4303
4304
                $icmsSN = $this->dom->createElement("ICMSSN202");
4305
                $this->dom->addChild(
4306
                    $icmsSN,
4307
                    'orig',
4308
                    $std->orig,
4309
                    true,
4310
                    "[item $std->item] Origem da mercadoria"
4311
                );
4312
                $this->dom->addChild(
4313
                    $icmsSN,
4314
                    'CSOSN',
4315
                    $std->CSOSN,
4316
                    true,
4317
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4318
                );
4319
                $this->dom->addChild(
4320
                    $icmsSN,
4321
                    'modBCST',
4322
                    $std->modBCST,
4323
                    true,
4324
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4325
                );
4326
                $this->dom->addChild(
4327
                    $icmsSN,
4328
                    'pMVAST',
4329
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
4330
                    false,
4331
                    "[item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
4332
                );
4333
                $this->dom->addChild(
4334
                    $icmsSN,
4335
                    'pRedBCST',
4336
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
4337
                    false,
4338
                    "[item $std->item] Percentual da Redução de BC do ICMS ST"
4339
                );
4340
                $this->dom->addChild(
4341
                    $icmsSN,
4342
                    'vBCST',
4343
                    $this->conditionalNumberFormatting($std->vBCST),
4344
                    true,
4345
                    "[item $std->item] Valor da BC do ICMS ST"
4346
                );
4347
                $this->dom->addChild(
4348
                    $icmsSN,
4349
                    'pICMSST',
4350
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
4351
                    true,
4352
                    "[item $std->item] Alíquota do imposto do ICMS ST"
4353
                );
4354
                $this->dom->addChild(
4355
                    $icmsSN,
4356
                    'vICMSST',
4357
                    $this->conditionalNumberFormatting($std->vICMSST),
4358
                    true,
4359
                    "[item $std->item] Valor do ICMS ST"
4360
                );
4361
                $this->dom->addChild(
4362
                    $icmsSN,
4363
                    'vBCFCPST',
4364
                    $this->conditionalNumberFormatting($std->vBCFCPST),
4365
                    isset($std->vBCFCPST) ? true : false,
4366
                    "[item $std->item] Valor da Base de Cálculo do FCP "
4367
                    . "retido por Substituição Tributária"
4368
                );
4369
                $this->dom->addChild(
4370
                    $icmsSN,
4371
                    'pFCPST',
4372
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
4373
                    isset($std->pFCPST) ? true : false,
4374
                    "[item $std->item] Percentual do FCP retido por "
4375
                    . "Substituição Tributária"
4376
                );
4377
                $this->dom->addChild(
4378
                    $icmsSN,
4379
                    'vFCPST',
4380
                    $this->conditionalNumberFormatting($std->vFCPST),
4381
                    isset($std->vFCPST) ? true : false,
4382
                    "[item $std->item] Valor do FCP retido por Substituição Tributária"
4383
                );
4384
                break;
4385
            case '500':
4386
                $icmsSN = $this->dom->createElement("ICMSSN500");
4387
                $this->dom->addChild(
4388
                    $icmsSN,
4389
                    'orig',
4390
                    $std->orig,
4391
                    true,
4392
                    "[item $std->item] Origem da mercadoria"
4393
                );
4394
                $this->dom->addChild(
4395
                    $icmsSN,
4396
                    'CSOSN',
4397
                    $std->CSOSN,
4398
                    true,
4399
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4400
                );
4401
                $this->dom->addChild(
4402
                    $icmsSN,
4403
                    'vBCSTRet',
4404
                    $this->conditionalNumberFormatting($std->vBCSTRet),
4405
                    isset($std->vBCSTRet) ? true : false,
4406
                    "[item $std->item] Valor da BC do ICMS ST retido"
4407
                );
4408
                $this->dom->addChild(
4409
                    $icmsSN,
4410
                    'pST',
4411
                    $this->conditionalNumberFormatting($std->pST, 4),
4412
                    isset($std->pST) ? true : false,
4413
                    "[item $std->item] Alíquota suportada pelo Consumidor Final"
4414
                );
4415
                $this->dom->addChild(
4416
                    $icmsSN,
4417
                    'vICMSSubstituto',
4418
                    $this->conditionalNumberFormatting($std->vICMSSubstituto),
4419
                    false,
4420
                    "[item $std->item] Valor do ICMS próprio do Substituto"
4421
                );
4422
                $this->dom->addChild(
4423
                    $icmsSN,
4424
                    'vICMSSTRet',
4425
                    $this->conditionalNumberFormatting($std->vICMSSTRet),
4426
                    isset($std->vICMSSTRet) ? true : false,
4427
                    "[item $std->item] Valor do ICMS ST retido"
4428
                );
4429
                $this->dom->addChild(
4430
                    $icmsSN,
4431
                    'vBCFCPSTRet',
4432
                    $this->conditionalNumberFormatting($std->vBCFCPSTRet, 2),
4433
                    isset($std->vBCFCPSTRet) ? true : false,
4434
                    "[item $std->item] Valor da Base de Cálculo do FCP "
4435
                    . "retido anteriormente por Substituição Tributária"
4436
                );
4437
                $this->dom->addChild(
4438
                    $icmsSN,
4439
                    'pFCPSTRet',
4440
                    $this->conditionalNumberFormatting($std->pFCPSTRet, 4),
4441
                    isset($std->pFCPSTRet) ? true : false,
4442
                    "[item $std->item] Percentual do FCP retido anteriormente por "
4443
                    . "Substituição Tributária"
4444
                );
4445
                $this->dom->addChild(
4446
                    $icmsSN,
4447
                    'vFCPSTRet',
4448
                    $this->conditionalNumberFormatting($std->vFCPSTRet),
4449
                    isset($std->vFCPSTRet) ? true : false,
4450
                    "[item $std->item] Valor do FCP retido anteiormente por "
4451
                    . "Substituição Tributária"
4452
                );
4453
                $this->dom->addChild(
4454
                    $icmsSN,
4455
                    'pRedBCEfet',
4456
                    $this->conditionalNumberFormatting($std->pRedBCEfet, 4),
4457
                    isset($std->pRedBCEfet) ? true : false,
4458
                    "[item $std->item] Percentual de redução da base "
4459
                    . "de cálculo efetiva"
4460
                );
4461
                $this->dom->addChild(
4462
                    $icmsSN,
4463
                    'vBCEfet',
4464
                    $this->conditionalNumberFormatting($std->vBCEfet),
4465
                    isset($std->vBCEfet) ? true : false,
4466
                    "[item $std->item] Valor da base de cálculo efetiva"
4467
                );
4468
                $this->dom->addChild(
4469
                    $icmsSN,
4470
                    'pICMSEfet',
4471
                    $this->conditionalNumberFormatting($std->pICMSEfet, 4),
4472
                    isset($std->pICMSEfet) ? true : false,
4473
                    "[item $std->item] Alíquota do ICMS efetiva"
4474
                );
4475
                $this->dom->addChild(
4476
                    $icmsSN,
4477
                    'vICMSEfet',
4478
                    $std->vICMSEfet,
4479
                    isset($std->vICMSEfet) ? true : false,
4480
                    "[item $std->item] Valor do ICMS efetivo"
4481
                );
4482
                break;
4483
            case '900':
4484
                $this->stdTot->vBC += (float) !empty($std->vBC) ? $std->vBC : 0;
4485
                $this->stdTot->vICMS += (float) !empty($std->vICMS) ? $std->vICMS : 0;
4486
                $this->stdTot->vBCST += (float) !empty($std->vBCST) ? $std->vBCST : 0;
4487
                $this->stdTot->vST += (float) !empty($std->vICMSST) ? $std->vICMSST : 0;
4488
                $icmsSN = $this->dom->createElement("ICMSSN900");
4489
                $this->dom->addChild(
4490
                    $icmsSN,
4491
                    'orig',
4492
                    $std->orig,
4493
                    true,
4494
                    "[item $std->item] Origem da mercadoria"
4495
                );
4496
                $this->dom->addChild(
4497
                    $icmsSN,
4498
                    'CSOSN',
4499
                    $std->CSOSN,
4500
                    true,
4501
                    "[item $std->item] Código de Situação da Operação Simples Nacional"
4502
                );
4503
                $this->dom->addChild(
4504
                    $icmsSN,
4505
                    'modBC',
4506
                    $std->modBC,
4507
                    isset($std->modBC) ? true : false,
4508
                    "[item $std->item] Modalidade de determinação da BC do ICMS"
4509
                );
4510
                $this->dom->addChild(
4511
                    $icmsSN,
4512
                    'vBC',
4513
                    $std->vBC,
4514
                    isset($std->vBC) ? true : false,
4515
                    "[item $std->item] Valor da BC do ICMS"
4516
                );
4517
                $this->dom->addChild(
4518
                    $icmsSN,
4519
                    'pRedBC',
4520
                    $this->conditionalNumberFormatting($std->pRedBC, 4),
4521
                    false,
4522
                    "[item $std->item] Percentual da Redução de BC"
4523
                );
4524
                $this->dom->addChild(
4525
                    $icmsSN,
4526
                    'pICMS',
4527
                    $this->conditionalNumberFormatting($std->pICMS, 4),
4528
                    isset($std->pICMS) ? true : false,
4529
                    "[item $std->item] Alíquota do imposto"
4530
                );
4531
                $this->dom->addChild(
4532
                    $icmsSN,
4533
                    'vICMS',
4534
                    $this->conditionalNumberFormatting($std->vICMS),
4535
                    isset($std->pICMS) ? true : false,
4536
                    "[item $std->item] Valor do ICMS"
4537
                );
4538
                $this->dom->addChild(
4539
                    $icmsSN,
4540
                    'modBCST',
4541
                    $std->modBCST,
4542
                    isset($std->modBCST) ? true : false,
4543
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4544
                );
4545
                $this->dom->addChild(
4546
                    $icmsSN,
4547
                    'pMVAST',
4548
                    $this->conditionalNumberFormatting($std->pMVAST, 4),
4549
                    false,
4550
                    "[item $std->item] Percentual da margem de valor Adicionado do ICMS ST"
4551
                );
4552
                $this->dom->addChild(
4553
                    $icmsSN,
4554
                    'pRedBCST',
4555
                    $this->conditionalNumberFormatting($std->pRedBCST, 4),
4556
                    false,
4557
                    "[item $std->item] Percentual da Redução de BC do ICMS ST"
4558
                );
4559
                $this->dom->addChild(
4560
                    $icmsSN,
4561
                    'vBCST',
4562
                    $this->conditionalNumberFormatting($std->vBCST),
4563
                    isset($std->vBCST) ? true : false,
4564
                    "[item $std->item] Valor da BC do ICMS ST"
4565
                );
4566
                $this->dom->addChild(
4567
                    $icmsSN,
4568
                    'pICMSST',
4569
                    $this->conditionalNumberFormatting($std->pICMSST, 4),
4570
                    isset($std->pICMSST) ? true : false,
4571
                    "[item $std->item] Alíquota do imposto do ICMS ST"
4572
                );
4573
                $this->dom->addChild(
4574
                    $icmsSN,
4575
                    'vICMSST',
4576
                    $std->vICMSST,
4577
                    isset($std->vICMSST) ? true : false,
4578
                    "[item $std->item] Valor do ICMS ST"
4579
                );
4580
                $this->dom->addChild(
4581
                    $icmsSN,
4582
                    'vBCFCPST',
4583
                    $this->conditionalNumberFormatting($std->vBCFCPST),
4584
                    isset($std->vBCFCPST) ? true : false,
4585
                    "[item $std->item] Valor da Base de Cálculo do FCP "
4586
                    . "retido por Substituição Tributária"
4587
                );
4588
                $this->dom->addChild(
4589
                    $icmsSN,
4590
                    'pFCPST',
4591
                    $this->conditionalNumberFormatting($std->pFCPST, 4),
4592
                    isset($std->pFCPST) ? true : false,
4593
                    "[item $std->item] Percentual do FCP retido por "
4594
                    . "Substituição Tributária"
4595
                );
4596
                $this->dom->addChild(
4597
                    $icmsSN,
4598
                    'vFCPST',
4599
                    $this->conditionalNumberFormatting($std->vFCPST),
4600
                    isset($std->vFCPST) ? true : false,
4601
                    "[item $std->item] Valor do FCP retido por Substituição Tributária"
4602
                );
4603
                $this->dom->addChild(
4604
                    $icmsSN,
4605
                    'pCredSN',
4606
                    $this->conditionalNumberFormatting($std->pCredSN, 4),
4607
                    isset($std->pCredSN) ? true : false,
4608
                    "[item $std->item] Alíquota aplicável de cálculo do crédito (Simples Nacional)."
4609
                );
4610
                $this->dom->addChild(
4611
                    $icmsSN,
4612
                    'vCredICMSSN',
4613
                    $this->conditionalNumberFormatting($std->vCredICMSSN),
4614
                    isset($std->vCredICMSSN) ? true : false,
4615
                    "[item $std->item] Valor crédito do ICMS que pode ser aproveitado nos termos do"
4616
                    . " art. 23 da LC 123 (Simples Nacional)"
4617
                );
4618
                break;
4619
        }
4620
        //caso exista a tag aICMS[$std-item] inserir nela caso contrario criar
4621
        if (!empty($this->aICMS[$std->item])) {
4622
            $tagIcms = $this->aICMS[$std->item];
4623
        } else {
4624
            $tagIcms = $this->dom->createElement('ICMS');
4625
        }
4626
        if (isset($icmsSN)) {
4627
            $this->dom->appChild($tagIcms, $icmsSN, "Inserindo ICMSST em ICMS[$std->item]");
4628
        }
4629
        $this->aICMS[$std->item] = $tagIcms;
4630
        return $tagIcms;
4631
    }
4632
4633
    /**
4634
     * Grupo ICMSUFDest NA01 pai M01
4635
     * tag NFe/infNFe/det[]/imposto/ICMSUFDest (opcional)
4636
     * Grupo a ser informado nas vendas interestaduais para consumidor final,
4637
     * não contribuinte do ICMS
4638
     * @param stdClass $std
4639
     * @return DOMElement
4640
     */
4641
    public function tagICMSUFDest(stdClass $std)
4642
    {
4643
        $possible = [
4644
            'item',
4645
            'vBCUFDest',
4646
            'vBCFCPUFDest',
4647
            'pFCPUFDest',
4648
            'pICMSUFDest',
4649
            'pICMSInter',
4650
            'pICMSInterPart',
4651
            'vFCPUFDest',
4652
            'vICMSUFDest',
4653
            'vICMSUFRemet'
4654
        ];
4655
        $std = $this->equilizeParameters($std, $possible);
4656
        $this->stdTot->vICMSUFDest += (float) $std->vICMSUFDest;
4657
        $this->stdTot->vFCPUFDest += (float) $std->vFCPUFDest;
4658
        $this->stdTot->vICMSUFRemet += (float) $std->vICMSUFRemet;
4659
        $icmsUFDest = $this->dom->createElement('ICMSUFDest');
4660
        $this->dom->addChild(
4661
            $icmsUFDest,
4662
            "vBCUFDest",
4663
            $std->vBCUFDest,
4664
            true,
4665
            "[item $std->item] Valor da BC do ICMS na UF do destinatário"
4666
        );
4667
        $this->dom->addChild(
4668
            $icmsUFDest,
4669
            "vBCFCPUFDest",
4670
            $std->vBCFCPUFDest,
4671
            false,
4672
            "[item $std->item] Valor da BC do ICMS na UF do destinatário"
4673
        );
4674
        $this->dom->addChild(
4675
            $icmsUFDest,
4676
            "pFCPUFDest",
4677
            $this->conditionalNumberFormatting($std->pFCPUFDest, 4),
4678
            false,
4679
            "[item $std->item] Percentual do ICMS relativo ao Fundo de Combate à Pobreza (FCP) na UF de destino"
4680
        );
4681
        $this->dom->addChild(
4682
            $icmsUFDest,
4683
            "pICMSUFDest",
4684
            $this->conditionalNumberFormatting($std->pICMSUFDest, 4),
4685
            true,
4686
            "[item $std->item] Alíquota interna da UF do destinatário"
4687
        );
4688
        $this->dom->addChild(
4689
            $icmsUFDest,
4690
            "pICMSInter",
4691
            $this->conditionalNumberFormatting($std->pICMSInter, 2),
4692
            true,
4693
            "[item $std->item] Alíquota interestadual das UF envolvidas"
4694
        );
4695
        $this->dom->addChild(
4696
            $icmsUFDest,
4697
            "pICMSInterPart",
4698
            $this->conditionalNumberFormatting($std->pICMSInterPart, 4),
4699
            true,
4700
            "[item $std->item] Percentual provisório de partilha entre os Estados"
4701
        );
4702
        $this->dom->addChild(
4703
            $icmsUFDest,
4704
            "vFCPUFDest",
4705
            $std->vFCPUFDest,
4706
            false,
4707
            "[item $std->item] Valor do ICMS relativo ao Fundo de Combate à Pobreza (FCP) da UF de destino"
4708
        );
4709
        $this->dom->addChild(
4710
            $icmsUFDest,
4711
            "vICMSUFDest",
4712
            $std->vICMSUFDest,
4713
            true,
4714
            "[item $std->item] Valor do ICMS de partilha para a UF do destinatário"
4715
        );
4716
        $this->dom->addChild(
4717
            $icmsUFDest,
4718
            "vICMSUFRemet",
4719
            $std->vICMSUFRemet,
4720
            true,
4721
            "[item $std->item] Valor do ICMS de partilha para a UF do remetente"
4722
        );
4723
        $this->aICMSUFDest[$std->item] = $icmsUFDest;
4724
        return $icmsUFDest;
4725
    }
4726
4727
    /**
4728
     * Grupo IPI O01 pai M01
4729
     * tag NFe/infNFe/det[]/imposto/IPI (opcional)
4730
     * @param stdClass $std
4731
     * @return DOMElement
4732
     */
4733
    public function tagIPI(stdClass $std)
4734
    {
4735
        $possible = [
4736
            'item',
4737
            'clEnq',
4738
            'CNPJProd',
4739
            'cSelo',
4740
            'qSelo',
4741
            'cEnq',
4742
            'CST',
4743
            'vIPI',
4744
            'vBC',
4745
            'pIPI',
4746
            'qUnid',
4747
            'vUnid'
4748
        ];
4749
        $std = $this->equilizeParameters($std, $possible);
4750
        //totalizador
4751
        $this->stdTot->vIPI += (float) $std->vIPI;
4752
        $ipi = $this->dom->createElement('IPI');
4753
        $this->dom->addChild(
4754
            $ipi,
4755
            "clEnq",
4756
            $std->clEnq,
4757
            false,
4758
            "[item $std->item] Classe de enquadramento do IPI para Cigarros e Bebidas"
4759
        );
4760
        $this->dom->addChild(
4761
            $ipi,
4762
            "CNPJProd",
4763
            $std->CNPJProd,
4764
            false,
4765
            "[item $std->item] CNPJ do produtor da mercadoria, quando diferente do emitente. "
4766
            . "Somente para os casos de exportação direta ou indireta."
4767
        );
4768
        $this->dom->addChild(
4769
            $ipi,
4770
            "cSelo",
4771
            $std->cSelo,
4772
            false,
4773
            "[item $std->item] Código do selo de controle IPI"
4774
        );
4775
        $this->dom->addChild(
4776
            $ipi,
4777
            "qSelo",
4778
            $std->qSelo,
4779
            false,
4780
            "[item $std->item] Quantidade de selo de controle"
4781
        );
4782
        $this->dom->addChild(
4783
            $ipi,
4784
            "cEnq",
4785
            $std->cEnq,
4786
            true,
4787
            "[item $std->item] Código de Enquadramento Legal do IPI"
4788
        );
4789
        if ($std->CST == '00' || $std->CST == '49' || $std->CST == '50' || $std->CST == '99') {
4790
            $ipiTrib = $this->dom->createElement('IPITrib');
4791
            $this->dom->addChild(
4792
                $ipiTrib,
4793
                "CST",
4794
                $std->CST,
4795
                true,
4796
                "[item $std->item] Código da situação tributária do IPI"
4797
            );
4798
            $this->dom->addChild(
4799
                $ipiTrib,
4800
                "vBC",
4801
                $this->conditionalNumberFormatting($std->vBC),
4802
                false,
4803
                "[item $std->item] Valor da BC do IPI"
4804
            );
4805
            $this->dom->addChild(
4806
                $ipiTrib,
4807
                "pIPI",
4808
                $this->conditionalNumberFormatting($std->pIPI, 4),
4809
                false,
4810
                "[item $std->item] Alíquota do IPI"
4811
            );
4812
            $this->dom->addChild(
4813
                $ipiTrib,
4814
                "qUnid",
4815
                $std->qUnid,
4816
                false,
4817
                "[item $std->item] Quantidade total na unidade padrão para tributação (somente para os "
4818
                . "produtos tributados por unidade)"
4819
            );
4820
            $this->dom->addChild(
4821
                $ipiTrib,
4822
                "vUnid",
4823
                $std->vUnid,
4824
                false,
4825
                "[item $std->item] Valor por Unidade Tributável"
4826
            );
4827
            $this->dom->addChild(
4828
                $ipiTrib,
4829
                "vIPI",
4830
                $this->conditionalNumberFormatting($std->vIPI),
4831
                true,
4832
                "[item $std->item] Valor do IPI"
4833
            );
4834
            $ipi->appendChild($ipiTrib);
4835
        } else {
4836
            $ipINT = $this->dom->createElement('IPINT');
4837
            $this->dom->addChild(
4838
                $ipINT,
4839
                "CST",
4840
                $std->CST,
4841
                true,
4842
                "[item $std->item] Código da situação tributária do IPINT"
4843
            );
4844
            $ipi->appendChild($ipINT);
4845
        }
4846
        $this->aIPI[$std->item] = $ipi;
4847
        return $ipi;
4848
    }
4849
4850
    /**
4851
     * Grupo Imposto de Importação P01 pai M01
4852
     * tag NFe/infNFe/det[]/imposto/II
4853
     * @param stdClass $std
4854
     * @return DOMElement
4855
     */
4856
    public function tagII(stdClass $std)
4857
    {
4858
        $possible = [
4859
            'item',
4860
            'vBC',
4861
            'vDespAdu',
4862
            'vII',
4863
            'vIOF'
4864
        ];
4865
        $std = $this->equilizeParameters($std, $possible);
4866
        //totalizador
4867
        $this->stdTot->vII += (float) $std->vII;
4868
        $tii = $this->dom->createElement('II');
4869
        $this->dom->addChild(
4870
            $tii,
4871
            "vBC",
4872
            $this->conditionalNumberFormatting($std->vBC),
4873
            true,
4874
            "[item $std->item] Valor BC do Imposto de Importação"
4875
        );
4876
        $this->dom->addChild(
4877
            $tii,
4878
            "vDespAdu",
4879
            $this->conditionalNumberFormatting($std->vDespAdu),
4880
            true,
4881
            "[item $std->item] Valor despesas aduaneiras"
4882
        );
4883
        $this->dom->addChild(
4884
            $tii,
4885
            "vII",
4886
            $this->conditionalNumberFormatting($std->vII),
4887
            true,
4888
            "[item $std->item] Valor Imposto de Importação"
4889
        );
4890
        $this->dom->addChild(
4891
            $tii,
4892
            "vIOF",
4893
            $this->conditionalNumberFormatting($std->vIOF),
4894
            true,
4895
            "[item $std->item] Valor Imposto sobre Operações Financeiras"
4896
        );
4897
        $this->aII[$std->item] = $tii;
4898
        return $tii;
4899
    }
4900
4901
    /**
4902
     * Grupo PIS Q01 pai M01
4903
     * tag NFe/infNFe/det[]/imposto/PIS
4904
     * @param stdClass $std
4905
     * @return DOMElement
4906
     */
4907
    public function tagPIS(stdClass $std)
4908
    {
4909
        $possible = [
4910
            'item',
4911
            'CST',
4912
            'vBC',
4913
            'pPIS',
4914
            'vPIS',
4915
            'qBCProd',
4916
            'vAliqProd'
4917
        ];
4918
        $std = $this->equilizeParameters($std, $possible);
4919
        //totalizador
4920
        $this->stdTot->vPIS += (float) !empty($std->vPIS) ? $std->vPIS : 0;
4921
        switch ($std->CST) {
4922
            case '01':
4923
            case '02':
4924
                $pisItem = $this->dom->createElement('PISAliq');
4925
                $this->dom->addChild(
4926
                    $pisItem,
4927
                    'CST',
4928
                    $std->CST,
4929
                    true,
4930
                    "[item $std->item] Código de Situação Tributária do PIS"
4931
                );
4932
                $this->dom->addChild(
4933
                    $pisItem,
4934
                    'vBC',
4935
                    $this->conditionalNumberFormatting($std->vBC),
4936
                    true,
4937
                    "[item $std->item] Valor da Base de Cálculo do PIS"
4938
                );
4939
                $this->dom->addChild(
4940
                    $pisItem,
4941
                    'pPIS',
4942
                    $this->conditionalNumberFormatting($std->pPIS, 4),
4943
                    true,
4944
                    "[item $std->item] Alíquota do PIS (em percentual)"
4945
                );
4946
                $this->dom->addChild(
4947
                    $pisItem,
4948
                    'vPIS',
4949
                    $this->conditionalNumberFormatting($std->vPIS),
4950
                    true,
4951
                    "[item $std->item] Valor do PIS"
4952
                );
4953
                break;
4954
            case '03':
4955
                $pisItem = $this->dom->createElement('PISQtde');
4956
                $this->dom->addChild(
4957
                    $pisItem,
4958
                    'CST',
4959
                    $std->CST,
4960
                    true,
4961
                    "[item $std->item] Código de Situação Tributária do PIS"
4962
                );
4963
                $this->dom->addChild(
4964
                    $pisItem,
4965
                    'qBCProd',
4966
                    $std->qBCProd,
4967
                    true,
4968
                    "[item $std->item] Quantidade Vendida"
4969
                );
4970
                $this->dom->addChild(
4971
                    $pisItem,
4972
                    'vAliqProd',
4973
                    $std->vAliqProd,
4974
                    true,
4975
                    "[item $std->item] Alíquota do PIS (em reais)"
4976
                );
4977
                $this->dom->addChild(
4978
                    $pisItem,
4979
                    'vPIS',
4980
                    $this->conditionalNumberFormatting($std->vPIS),
4981
                    true,
4982
                    "[item $std->item] Valor do PIS"
4983
                );
4984
                break;
4985
            case '04':
4986
            case '05':
4987
            case '06':
4988
            case '07':
4989
            case '08':
4990
            case '09':
4991
                $pisItem = $this->dom->createElement('PISNT');
4992
                $this->dom->addChild(
4993
                    $pisItem,
4994
                    'CST',
4995
                    $std->CST,
4996
                    true,
4997
                    "[item $std->item] Código de Situação Tributária do PIS"
4998
                );
4999
                break;
5000
            case '49':
5001
            case '50':
5002
            case '51':
5003
            case '52':
5004
            case '53':
5005
            case '54':
5006
            case '55':
5007
            case '56':
5008
            case '60':
5009
            case '61':
5010
            case '62':
5011
            case '63':
5012
            case '64':
5013
            case '65':
5014
            case '66':
5015
            case '67':
5016
            case '70':
5017
            case '71':
5018
            case '72':
5019
            case '73':
5020
            case '74':
5021
            case '75':
5022
            case '98':
5023
            case '99':
5024
                $pisItem = $this->dom->createElement('PISOutr');
5025
                $this->dom->addChild(
5026
                    $pisItem,
5027
                    'CST',
5028
                    $std->CST,
5029
                    true,
5030
                    "[item $std->item] Código de Situação Tributária do PIS"
5031
                );
5032
                $this->dom->addChild(
5033
                    $pisItem,
5034
                    'vBC',
5035
                    $this->conditionalNumberFormatting($std->vBC),
5036
                    ($std->vBC !== null) ? true : false,
5037
                    "[item $std->item] Valor da Base de Cálculo do PIS"
5038
                );
5039
                $this->dom->addChild(
5040
                    $pisItem,
5041
                    'pPIS',
5042
                    $this->conditionalNumberFormatting($std->pPIS, 4),
5043
                    ($std->pPIS !== null) ? true : false,
5044
                    "[item $std->item] Alíquota do PIS (em percentual)"
5045
                );
5046
                $this->dom->addChild(
5047
                    $pisItem,
5048
                    'qBCProd',
5049
                    $std->qBCProd,
5050
                    ($std->qBCProd !== null) ? true : false,
5051
                    "[item $std->item] Quantidade Vendida"
5052
                );
5053
                $this->dom->addChild(
5054
                    $pisItem,
5055
                    'vAliqProd',
5056
                    $std->vAliqProd,
5057
                    ($std->vAliqProd !== null) ? true : false,
5058
                    "[item $std->item] Alíquota do PIS (em reais)"
5059
                );
5060
                $this->dom->addChild(
5061
                    $pisItem,
5062
                    'vPIS',
5063
                    $this->conditionalNumberFormatting($std->vPIS),
5064
                    true,
5065
                    "[item $std->item] Valor do PIS"
5066
                );
5067
                break;
5068
        }
5069
        $pis = $this->dom->createElement('PIS');
5070
        if (isset($pisItem)) {
5071
            $pis->appendChild($pisItem);
5072
        }
5073
        $this->aPIS[$std->item] = $pis;
5074
        return $pis;
5075
    }
5076
5077
    /**
5078
     * Grupo PIS Substituição Tributária R01 pai M01
5079
     * tag NFe/infNFe/det[]/imposto/PISST (opcional)
5080
     * @param stdClass $std
5081
     * @return DOMElement
5082
     */
5083
    public function tagPISST(stdClass $std)
5084
    {
5085
        $possible = [
5086
            'item',
5087
            'vPIS',
5088
            'vBC',
5089
            'pPIS',
5090
            'qBCProd',
5091
            'vAliqProd'
5092
        ];
5093
        $std = $this->equilizeParameters($std, $possible);
5094
        $pisst = $this->dom->createElement('PISST');
5095
        if (!isset($std->qBCProd)) {
5096
            $this->dom->addChild(
5097
                $pisst,
5098
                'vBC',
5099
                $this->conditionalNumberFormatting($std->vBC),
5100
                true,
5101
                "[item $std->item] Valor da Base de Cálculo do PIS"
5102
            );
5103
            $this->dom->addChild(
5104
                $pisst,
5105
                'pPIS',
5106
                $this->conditionalNumberFormatting($std->pPIS, 4),
5107
                true,
5108
                "[item $std->item] Alíquota do PIS (em percentual)"
5109
            );
5110
        } else {
5111
            $this->dom->addChild(
5112
                $pisst,
5113
                'qBCProd',
5114
                $std->qBCProd,
5115
                true,
5116
                "[item $std->item] Quantidade Vendida"
5117
            );
5118
            $this->dom->addChild(
5119
                $pisst,
5120
                'vAliqProd',
5121
                $std->vAliqProd,
5122
                true,
5123
                "[item $std->item] Alíquota do PIS (em reais)"
5124
            );
5125
        }
5126
        $this->dom->addChild(
5127
            $pisst,
5128
            'vPIS',
5129
            $this->conditionalNumberFormatting($std->vPIS),
5130
            true,
5131
            "[item $std->item] Valor do PIS"
5132
        );
5133
        $this->aPISST[$std->item] = $pisst;
5134
        return $pisst;
5135
    }
5136
5137
    /**
5138
     * Grupo COFINS S01 pai M01
5139
     * tag det[item]/imposto/COFINS (opcional)
5140
     * @param  stdClass $std
5141
     * @return DOMElement
5142
     */
5143
    public function tagCOFINS(stdClass $std)
5144
    {
5145
        $possible = [
5146
            'item',
5147
            'CST',
5148
            'vBC',
5149
            'pCOFINS',
5150
            'vCOFINS',
5151
            'qBCProd',
5152
            'vAliqProd'
5153
        ];
5154
        $std = $this->equilizeParameters($std, $possible);
5155
        //totalizador
5156
        $this->stdTot->vCOFINS += (float) $std->vCOFINS;
5157
        switch ($std->CST) {
5158
            case '01':
5159
            case '02':
5160
                $confinsItem = $this->buildCOFINSAliq($std);
5161
                break;
5162
            case '03':
5163
                $confinsItem = $this->dom->createElement('COFINSQtde');
5164
                $this->dom->addChild(
5165
                    $confinsItem,
5166
                    'CST',
5167
                    $std->CST,
5168
                    true,
5169
                    "[item $std->item] Código de Situação Tributária da COFINS"
5170
                );
5171
                $this->dom->addChild(
5172
                    $confinsItem,
5173
                    'qBCProd',
5174
                    $std->qBCProd,
5175
                    true,
5176
                    "[item $std->item] Quantidade Vendida"
5177
                );
5178
                $this->dom->addChild(
5179
                    $confinsItem,
5180
                    'vAliqProd',
5181
                    $std->vAliqProd,
5182
                    true,
5183
                    "[item $std->item] Alíquota do COFINS (em reais)"
5184
                );
5185
                $this->dom->addChild(
5186
                    $confinsItem,
5187
                    'vCOFINS',
5188
                    $this->conditionalNumberFormatting($std->vCOFINS),
5189
                    true,
5190
                    "[item $std->item] Valor do COFINS"
5191
                );
5192
                break;
5193
            case '04':
5194
            case '05':
5195
            case '06':
5196
            case '07':
5197
            case '08':
5198
            case '09':
5199
                $confinsItem = $this->buildCOFINSNT($std);
5200
                break;
5201
            case '49':
5202
            case '50':
5203
            case '51':
5204
            case '52':
5205
            case '53':
5206
            case '54':
5207
            case '55':
5208
            case '56':
5209
            case '60':
5210
            case '61':
5211
            case '62':
5212
            case '63':
5213
            case '64':
5214
            case '65':
5215
            case '66':
5216
            case '67':
5217
            case '70':
5218
            case '71':
5219
            case '72':
5220
            case '73':
5221
            case '74':
5222
            case '75':
5223
            case '98':
5224
            case '99':
5225
                $confinsItem = $this->buildCOFINSoutr($std);
5226
                break;
5227
        }
5228
        $confins = $this->dom->createElement('COFINS');
5229
        if (isset($confinsItem)) {
5230
            $confins->appendChild($confinsItem);
5231
        }
5232
        $this->aCOFINS[$std->item] = $confins;
5233
        return $confins;
5234
    }
5235
5236
    /**
5237
     * Grupo COFINS Substituição Tributária T01 pai M01
5238
     * tag NFe/infNFe/det[]/imposto/COFINSST (opcional)
5239
     * @param stdClass $std
5240
     * @return DOMElement
5241
     */
5242
    public function tagCOFINSST(stdClass $std)
5243
    {
5244
        $possible = [
5245
            'item',
5246
            'vCOFINS',
5247
            'vBC',
5248
            'pCOFINS',
5249
            'qBCProd',
5250
            'vAliqProd'
5251
        ];
5252
        $std = $this->equilizeParameters($std, $possible);
5253
        $cofinsst = $this->dom->createElement("COFINSST");
5254
        if (!isset($std->qBCProd)) {
5255
            $this->dom->addChild(
5256
                $cofinsst,
5257
                "vBC",
5258
                $this->conditionalNumberFormatting($std->vBC),
5259
                true,
5260
                "[item $std->item] Valor da Base de Cálculo da COFINS"
5261
            );
5262
            $this->dom->addChild(
5263
                $cofinsst,
5264
                "pCOFINS",
5265
                $this->conditionalNumberFormatting($std->pCOFINS, 4),
5266
                true,
5267
                "[item $std->item] Alíquota da COFINS (em percentual)"
5268
            );
5269
        } else {
5270
            $this->dom->addChild(
5271
                $cofinsst,
5272
                "qBCProd",
5273
                $std->qBCProd,
5274
                true,
5275
                "[item $std->item] Quantidade Vendida"
5276
            );
5277
            $this->dom->addChild(
5278
                $cofinsst,
5279
                "vAliqProd",
5280
                $std->vAliqProd,
5281
                true,
5282
                "[item $std->item] Alíquota da COFINS (em reais)"
5283
            );
5284
        }
5285
        $this->dom->addChild(
5286
            $cofinsst,
5287
            "vCOFINS",
5288
            $this->conditionalNumberFormatting($std->vCOFINS),
5289
            true,
5290
            "[item $std->item] Valor da COFINS"
5291
        );
5292
        $this->aCOFINSST[$std->item] = $cofinsst;
5293
        return $cofinsst;
5294
    }
5295
5296
    /**
5297
     * Grupo ISSQN U01 pai M01
5298
     * tag NFe/infNFe/det[]/imposto/ISSQN (opcional)
5299
     * @param stdClass $std
5300
     * @return DOMElement
5301
     */
5302
    public function tagISSQN(stdClass $std)
5303
    {
5304
        $possible = [
5305
            'item',
5306
            'vBC',
5307
            'vAliq',
5308
            'vISSQN',
5309
            'cMunFG',
5310
            'cListServ',
5311
            'vDeducao',
5312
            'vOutro',
5313
            'vDescIncond',
5314
            'vDescCond',
5315
            'vISSRet',
5316
            'indISS',
5317
            'cServico',
5318
            'cMun',
5319
            'cPais',
5320
            'nProcesso',
5321
            'indIncentivo'
5322
        ];
5323
        $std = $this->equilizeParameters($std, $possible);
5324
        $issqn = $this->dom->createElement("ISSQN");
5325
        $this->dom->addChild(
5326
            $issqn,
5327
            "vBC",
5328
            $this->conditionalNumberFormatting($std->vBC),
5329
            true,
5330
            "[item $std->item] Valor da Base de Cálculo do ISSQN"
5331
        );
5332
        $this->dom->addChild(
5333
            $issqn,
5334
            "vAliq",
5335
            $this->conditionalNumberFormatting($std->vAliq, 4),
5336
            true,
5337
            "[item $std->item] Alíquota do ISSQN"
5338
        );
5339
        $this->dom->addChild(
5340
            $issqn,
5341
            "vISSQN",
5342
            $this->conditionalNumberFormatting($std->vISSQN),
5343
            true,
5344
            "[item $std->item] Valor do ISSQN"
5345
        );
5346
        $this->dom->addChild(
5347
            $issqn,
5348
            "cMunFG",
5349
            $std->cMunFG,
5350
            true,
5351
            "[item $std->item] Código do município de ocorrência do fato gerador do ISSQN"
5352
        );
5353
        $this->dom->addChild(
5354
            $issqn,
5355
            "cListServ",
5356
            $std->cListServ,
5357
            true,
5358
            "[item $std->item] Item da Lista de Serviços"
5359
        );
5360
        $this->dom->addChild(
5361
            $issqn,
5362
            "vDeducao",
5363
            $this->conditionalNumberFormatting($std->vDeducao),
5364
            false,
5365
            "[item $std->item] Valor dedução para redução da Base de Cálculo"
5366
        );
5367
        $this->dom->addChild(
5368
            $issqn,
5369
            "vOutro",
5370
            $this->conditionalNumberFormatting($std->vOutro),
5371
            false,
5372
            "[item $std->item] Valor outras retenções"
5373
        );
5374
        $this->dom->addChild(
5375
            $issqn,
5376
            "vDescIncond",
5377
            $this->conditionalNumberFormatting($std->vDescIncond),
5378
            false,
5379
            "[item $std->item] Valor desconto incondicionado"
5380
        );
5381
        $this->dom->addChild(
5382
            $issqn,
5383
            "vDescCond",
5384
            $this->conditionalNumberFormatting($std->vDescCond),
5385
            false,
5386
            "[item $std->item] Valor desconto condicionado"
5387
        );
5388
        $this->dom->addChild(
5389
            $issqn,
5390
            "vISSRet",
5391
            $this->conditionalNumberFormatting($std->vISSRet),
5392
            false,
5393
            "[item $std->item] Valor retenção ISS"
5394
        );
5395
        $this->dom->addChild(
5396
            $issqn,
5397
            "indISS",
5398
            $std->indISS,
5399
            true,
5400
            "[item $std->item] Indicador da exigibilidade do ISS"
5401
        );
5402
        $this->dom->addChild(
5403
            $issqn,
5404
            "cServico",
5405
            $std->cServico,
5406
            false,
5407
            "[item $std->item] Código do serviço prestado dentro do município"
5408
        );
5409
        $this->dom->addChild(
5410
            $issqn,
5411
            "cMun",
5412
            $std->cMun,
5413
            false,
5414
            "[item $std->item] Código do Município de incidência do imposto"
5415
        );
5416
        $this->dom->addChild(
5417
            $issqn,
5418
            "cPais",
5419
            $std->cPais,
5420
            false,
5421
            "[item $std->item] Código do País onde o serviço foi prestado"
5422
        );
5423
        $this->dom->addChild(
5424
            $issqn,
5425
            "nProcesso",
5426
            $std->nProcesso,
5427
            false,
5428
            "[item $std->item] Número do processo judicial ou administrativo de suspensão da exigibilidade"
5429
        );
5430
        $this->dom->addChild(
5431
            $issqn,
5432
            "indIncentivo",
5433
            $std->indIncentivo,
5434
            true,
5435
            "[item $std->item] Indicador de incentivo Fiscal"
5436
        );
5437
        $this->aISSQN[$std->item] = $issqn;
5438
        return $issqn;
5439
    }
5440
5441
    /**
5442
     * Informação do Imposto devolvido U50 pai H01
5443
     * tag NFe/infNFe/det[]/impostoDevol (opcional)
5444
     * @param stdClass $std
5445
     * @return DOMElement
5446
     */
5447
    public function tagimpostoDevol(stdClass $std)
5448
    {
5449
        $possible = [
5450
            'item',
5451
            'pDevol',
5452
            'vIPIDevol'
5453
        ];
5454
        $std = $this->equilizeParameters($std, $possible);
5455
        //totalizador
5456
        $this->stdTot->vIPIDevol += (float) $std->vIPIDevol;
5457
        $impostoDevol = $this->dom->createElement("impostoDevol");
5458
        $this->dom->addChild(
5459
            $impostoDevol,
5460
            "pDevol",
5461
            $this->conditionalNumberFormatting($std->pDevol, 2),
5462
            true,
5463
            "[item $std->item] Percentual da mercadoria devolvida"
5464
        );
5465
        $parent = $this->dom->createElement("IPI");
5466
        $this->dom->addChild(
5467
            $parent,
5468
            "vIPIDevol",
5469
            $this->conditionalNumberFormatting($std->vIPIDevol),
5470
            true,
5471
            "[item $std->item] Valor do IPI devolvido"
5472
        );
5473
        $impostoDevol->appendChild($parent);
5474
        $this->aImpostoDevol[$std->item] = $impostoDevol;
5475
        return $impostoDevol;
5476
    }
5477
5478
    /**
5479
     * Grupo Totais referentes ao ICMS W02 pai W01
5480
     * tag NFe/infNFe/total/ICMSTot
5481
     * @param stdClass $std
5482
     * @return DOMElement
5483
     */
5484
    public function tagICMSTot(stdClass $std)
5485
    {
5486
        $this->buildTotal();
5487
        $vBC = isset($std->vBC) ? $std->vBC : $this->stdTot->vBC;
5488
        $vICMS = isset($std->vICMS) ? $std->vICMS : $this->stdTot->vICMS;
5489
        $vICMSDeson = !empty($std->vICMSDeson) ? $std->vICMSDeson : $this->stdTot->vICMSDeson;
5490
        $vBCST = !empty($std->vBCST) ? $std->vBCST : $this->stdTot->vBCST;
5491
        $vST = !empty($std->vST) ? $std->vST : $this->stdTot->vST;
5492
        $vProd = !empty($std->vProd) ? $std->vProd : $this->stdTot->vProd;
5493
        $vFrete = !empty($std->vFrete) ? $std->vFrete : $this->stdTot->vFrete;
5494
        $vSeg = !empty($std->vSeg) ? $std->vSeg : $this->stdTot->vSeg;
5495
        $vDesc = !empty($std->vDesc) ? $std->vDesc : $this->stdTot->vDesc;
5496
        $vII = !empty($std->vII) ? $std->vII : $this->stdTot->vII;
5497
        $vIPI = !empty($std->vIPI) ? $std->vIPI : $this->stdTot->vIPI;
5498
        $vPIS = !empty($std->vPIS) ? $std->vPIS : $this->stdTot->vPIS;
5499
        $vCOFINS = !empty($std->vCOFINS) ? $std->vCOFINS : $this->stdTot->vCOFINS;
5500
        $vOutro = !empty($std->vOutro) ? $std->vOutro : $this->stdTot->vOutro;
5501
        $vNF = !empty($std->vNF) ? $std->vNF : $this->stdTot->vNF;
5502
        $vIPIDevol = !empty($std->vIPIDevol) ? $std->vIPIDevol : $this->stdTot->vIPIDevol;
5503
        $vTotTrib = !empty($std->vTotTrib) ? $std->vTotTrib : $this->stdTot->vTotTrib;
5504
        $vFCP = !empty($std->vFCP) ? $std->vFCP : $this->stdTot->vFCP;
5505
        $vFCPST = !empty($std->vFCPST) ? $std->vFCPST : $this->stdTot->vFCPST;
5506
        $vFCPSTRet = !empty($std->vFCPSTRet) ? $std->vFCPSTRet : $this->stdTot->vFCPSTRet;
5507
        $vFCPUFDest = !empty($std->vFCPUFDest) ? $std->vFCPUFDest : $this->stdTot->vFCPUFDest;
5508
        $vICMSUFDest = !empty($std->vICMSUFDest) ? $std->vICMSUFDest : $this->stdTot->vICMSUFDest;
5509
        $vICMSUFRemet = !empty($std->vICMSUFRemet) ? $std->vICMSUFRemet : $this->stdTot->vICMSUFRemet;
5510
5511
        //campos opcionais incluir se maior que zero
5512
        $vFCPUFDest = ($vFCPUFDest > 0) ? number_format($vFCPUFDest, 2, '.', '') : null;
5513
        $vICMSUFDest = ($vICMSUFDest > 0) ? number_format($vICMSUFDest, 2, '.', '') : null;
5514
        $vICMSUFRemet = ($vICMSUFRemet > 0) ? number_format($vICMSUFRemet, 2, '.', '') : null;
5515
        $vTotTrib = ($vTotTrib > 0) ? number_format($vTotTrib, 2, '.', '') : null;
5516
5517
        //campos obrigatórios para 4.00
5518
        $vFCP = number_format($vFCP, 2, '.', '');
5519
        $vFCPST = number_format($vFCPST, 2, '.', '');
5520
        $vFCPSTRet = number_format($vFCPSTRet, 2, '.', '');
5521
        $vIPIDevol = number_format($vIPIDevol, 2, '.', '');
5522
5523
        $ICMSTot = $this->dom->createElement("ICMSTot");
5524
        $this->dom->addChild(
5525
            $ICMSTot,
5526
            "vBC",
5527
            number_format($vBC, 2, '.', ''),
5528
            true,
5529
            "Base de Cálculo do ICMS"
5530
        );
5531
        $this->dom->addChild(
5532
            $ICMSTot,
5533
            "vICMS",
5534
            number_format($vICMS, 2, '.', ''),
5535
            true,
5536
            "Valor Total do ICMS"
5537
        );
5538
        $this->dom->addChild(
5539
            $ICMSTot,
5540
            "vICMSDeson",
5541
            number_format($vICMSDeson, 2, '.', ''),
5542
            true,
5543
            "Valor Total do ICMS desonerado"
5544
        );
5545
        $this->dom->addChild(
5546
            $ICMSTot,
5547
            "vFCPUFDest",
5548
            $vFCPUFDest,
5549
            false,
5550
            "Valor total do ICMS relativo ao Fundo de Combate à Pobreza(FCP) "
5551
            . "para a UF de destino"
5552
        );
5553
        $this->dom->addChild(
5554
            $ICMSTot,
5555
            "vICMSUFDest",
5556
            $vICMSUFDest,
5557
            false,
5558
            "Valor total do ICMS de partilha para a UF do destinatário"
5559
        );
5560
        $this->dom->addChild(
5561
            $ICMSTot,
5562
            "vICMSUFRemet",
5563
            $vICMSUFRemet,
5564
            false,
5565
            "Valor total do ICMS de partilha para a UF do remetente"
5566
        );
5567
        //incluso no layout 4.00
5568
        $this->dom->addChild(
5569
            $ICMSTot,
5570
            "vFCP",
5571
            $vFCP,
5572
            false,
5573
            "Valor total do ICMS relativo ao Fundo de Combate à Pobreza(FCP) "
5574
            . "para a UF de destino"
5575
        );
5576
        $this->dom->addChild(
5577
            $ICMSTot,
5578
            "vBCST",
5579
            number_format($vBCST, 2, '.', ''),
5580
            true,
5581
            "Base de Cálculo do ICMS ST"
5582
        );
5583
        $this->dom->addChild(
5584
            $ICMSTot,
5585
            "vST",
5586
            number_format($vST, 2, '.', ''),
5587
            true,
5588
            "Valor Total do ICMS ST"
5589
        );
5590
        //incluso na 4.00
5591
        $this->dom->addChild(
5592
            $ICMSTot,
5593
            "vFCPST",
5594
            $vFCPST,
5595
            false, //true para 4.00
5596
            "Valor Total do FCP (Fundo de Combate à Pobreza) "
5597
            . "retido por substituição tributária"
5598
        );
5599
        //incluso na 4.00
5600
        $this->dom->addChild(
5601
            $ICMSTot,
5602
            "vFCPSTRet",
5603
            $vFCPSTRet,
5604
            false, //true para 4.00
5605
            "Valor Total do FCP retido anteriormente por "
5606
            . "Substituição Tributária"
5607
        );
5608
        $this->dom->addChild(
5609
            $ICMSTot,
5610
            "vProd",
5611
            number_format($vProd, 2, '.', ''),
5612
            true,
5613
            "Valor Total dos produtos e serviços"
5614
        );
5615
        $this->dom->addChild(
5616
            $ICMSTot,
5617
            "vFrete",
5618
            number_format($vFrete, 2, '.', ''),
5619
            true,
5620
            "Valor Total do Frete"
5621
        );
5622
        $this->dom->addChild(
5623
            $ICMSTot,
5624
            "vSeg",
5625
            number_format($vSeg, 2, '.', ''),
5626
            true,
5627
            "Valor Total do Seguro"
5628
        );
5629
        $this->dom->addChild(
5630
            $ICMSTot,
5631
            "vDesc",
5632
            number_format($vDesc, 2, '.', ''),
5633
            true,
5634
            "Valor Total do Desconto"
5635
        );
5636
        $this->dom->addChild(
5637
            $ICMSTot,
5638
            "vII",
5639
            number_format($vII, 2, '.', ''),
5640
            true,
5641
            "Valor Total do II"
5642
        );
5643
        $this->dom->addChild(
5644
            $ICMSTot,
5645
            "vIPI",
5646
            number_format($vIPI, 2, '.', ''),
5647
            true,
5648
            "Valor Total do IPI"
5649
        );
5650
        //incluso 4.00
5651
        $this->dom->addChild(
5652
            $ICMSTot,
5653
            "vIPIDevol",
5654
            $vIPIDevol,
5655
            false,
5656
            "Valor Total do IPI"
5657
        );
5658
        $this->dom->addChild(
5659
            $ICMSTot,
5660
            "vPIS",
5661
            number_format($vPIS, 2, '.', ''),
5662
            true,
5663
            "Valor do PIS"
5664
        );
5665
        $this->dom->addChild(
5666
            $ICMSTot,
5667
            "vCOFINS",
5668
            number_format($vCOFINS, 2, '.', ''),
5669
            true,
5670
            "Valor da COFINS"
5671
        );
5672
        $this->dom->addChild(
5673
            $ICMSTot,
5674
            "vOutro",
5675
            number_format($vOutro, 2, '.', ''),
5676
            true,
5677
            "Outras Despesas acessórias"
5678
        );
5679
        $this->dom->addChild(
5680
            $ICMSTot,
5681
            "vNF",
5682
            number_format($vNF, 2, '.', ''),
5683
            true,
5684
            "Valor Total da NF-e"
5685
        );
5686
        $this->dom->addChild(
5687
            $ICMSTot,
5688
            "vTotTrib",
5689
            $vTotTrib,
5690
            false,
5691
            "Valor aproximado total de tributos federais, estaduais e municipais."
5692
        );
5693
        $this->dom->appChild($this->total, $ICMSTot, '');
5694
        return $ICMSTot;
5695
    }
5696
5697
    /**
5698
     * Grupo Totais referentes ao ISSQN W17 pai W01
5699
     * tag NFe/infNFe/total/ISSQNTot (opcional)
5700
     * @param stdClass $std
5701
     * @return DOMElement
5702
     */
5703
    public function tagISSQNTot(stdClass $std)
5704
    {
5705
        $possible = [
5706
            'vServ',
5707
            'vBC',
5708
            'vISS',
5709
            'vPIS',
5710
            'vCOFINS',
5711
            'dCompet',
5712
            'vDeducao',
5713
            'vOutro',
5714
            'vDescIncond',
5715
            'vDescCond',
5716
            'vISSRet',
5717
            'cRegTrib'
5718
        ];
5719
        $std = $this->equilizeParameters($std, $possible);
5720
        $this->buildTotal();
5721
        $ISSQNTot = $this->dom->createElement("ISSQNtot");
5722
        $this->dom->addChild(
5723
            $ISSQNTot,
5724
            "vServ",
5725
            $this->conditionalNumberFormatting($std->vServ),
5726
            false,
5727
            "Valor total dos Serviços sob não incidência ou não tributados pelo ICMS"
5728
        );
5729
        $this->dom->addChild(
5730
            $ISSQNTot,
5731
            "vBC",
5732
            $this->conditionalNumberFormatting($std->vBC),
5733
            false,
5734
            "Valor total Base de Cálculo do ISS"
5735
        );
5736
        $this->dom->addChild(
5737
            $ISSQNTot,
5738
            "vISS",
5739
            $this->conditionalNumberFormatting($std->vISS),
5740
            false,
5741
            "Valor total do ISS"
5742
        );
5743
        $this->dom->addChild(
5744
            $ISSQNTot,
5745
            "vPIS",
5746
            $this->conditionalNumberFormatting($std->vPIS),
5747
            false,
5748
            "Valor total do PIS sobre serviços"
5749
        );
5750
        $this->dom->addChild(
5751
            $ISSQNTot,
5752
            "vCOFINS",
5753
            $this->conditionalNumberFormatting($std->vCOFINS),
5754
            false,
5755
            "Valor total da COFINS sobre serviços"
5756
        );
5757
        $this->dom->addChild(
5758
            $ISSQNTot,
5759
            "dCompet",
5760
            $std->dCompet,
5761
            true,
5762
            "Data da prestação do serviço"
5763
        );
5764
        $this->dom->addChild(
5765
            $ISSQNTot,
5766
            "vDeducao",
5767
            $this->conditionalNumberFormatting($std->vDeducao),
5768
            false,
5769
            "Valor total dedução para redução da Base de Cálculo"
5770
        );
5771
        $this->dom->addChild(
5772
            $ISSQNTot,
5773
            "vOutro",
5774
            $this->conditionalNumberFormatting($std->vOutro),
5775
            false,
5776
            "Valor total outras retenções"
5777
        );
5778
        $this->dom->addChild(
5779
            $ISSQNTot,
5780
            "vDescIncond",
5781
            $this->conditionalNumberFormatting($std->vDescIncond),
5782
            false,
5783
            "Valor total desconto incondicionado"
5784
        );
5785
        $this->dom->addChild(
5786
            $ISSQNTot,
5787
            "vDescCond",
5788
            $this->conditionalNumberFormatting($std->vDescCond),
5789
            false,
5790
            "Valor total desconto condicionado"
5791
        );
5792
        $this->dom->addChild(
5793
            $ISSQNTot,
5794
            "vISSRet",
5795
            $this->conditionalNumberFormatting($std->vISSRet),
5796
            false,
5797
            "Valor total retenção ISS"
5798
        );
5799
        $this->dom->addChild(
5800
            $ISSQNTot,
5801
            "cRegTrib",
5802
            $std->cRegTrib,
5803
            false,
5804
            "Código do Regime Especial de Tributação"
5805
        );
5806
        $this->dom->appChild($this->total, $ISSQNTot, '');
5807
        return $ISSQNTot;
5808
    }
5809
5810
    /**
5811
     * Grupo Retenções de Tributos W23 pai W01
5812
     * tag NFe/infNFe/total/reTrib (opcional)
5813
     * @param stdClass $std
5814
     * @return DOMElement
5815
     */
5816
    public function tagretTrib(stdClass $std)
5817
    {
5818
        $possible = [
5819
            'vRetPIS',
5820
            'vRetCOFINS',
5821
            'vRetCSLL',
5822
            'vBCIRRF',
5823
            'vIRRF',
5824
            'vBCRetPrev',
5825
            'vRetPrev'
5826
        ];
5827
        $std = $this->equilizeParameters($std, $possible);
5828
        $retTrib = $this->dom->createElement("retTrib");
5829
        $this->dom->addChild(
5830
            $retTrib,
5831
            "vRetPIS",
5832
            $this->conditionalNumberFormatting($std->vRetPIS),
5833
            false,
5834
            "Valor Retido de PIS"
5835
        );
5836
        $this->dom->addChild(
5837
            $retTrib,
5838
            "vRetCOFINS",
5839
            $this->conditionalNumberFormatting($std->vRetCOFINS),
5840
            false,
5841
            "Valor Retido de COFINS"
5842
        );
5843
        $this->dom->addChild(
5844
            $retTrib,
5845
            "vRetCSLL",
5846
            $this->conditionalNumberFormatting($std->vRetCSLL),
5847
            false,
5848
            "Valor Retido de CSLL"
5849
        );
5850
        $this->dom->addChild(
5851
            $retTrib,
5852
            "vBCIRRF",
5853
            $this->conditionalNumberFormatting($std->vBCIRRF),
5854
            false,
5855
            "Base de Cálculo do IRRF"
5856
        );
5857
        $this->dom->addChild(
5858
            $retTrib,
5859
            "vIRRF",
5860
            $this->conditionalNumberFormatting($std->vIRRF),
5861
            false,
5862
            "Valor Retido do IRRF"
5863
        );
5864
        $this->dom->addChild(
5865
            $retTrib,
5866
            "vBCRetPrev",
5867
            $this->conditionalNumberFormatting($std->vBCRetPrev),
5868
            false,
5869
            "Base de Cálculo da Retenção da Previdência Social"
5870
        );
5871
        $this->dom->addChild(
5872
            $retTrib,
5873
            "vRetPrev",
5874
            $this->conditionalNumberFormatting($std->vRetPrev),
5875
            false,
5876
            "Valor da Retenção da Previdência Social"
5877
        );
5878
        $this->dom->appChild($this->total, $retTrib, '');
5879
        return $retTrib;
5880
    }
5881
5882
    /**
5883
     * Grupo Informações do Transporte X01 pai A01
5884
     * tag NFe/infNFe/transp (obrigatório)
5885
     * @param stdClass $std
5886
     * @return DOMElement
5887
     */
5888
    public function tagtransp(stdClass $std)
5889
    {
5890
        $this->transp = $this->dom->createElement("transp");
5891
        $this->dom->addChild(
5892
            $this->transp,
5893
            "modFrete",
5894
            $std->modFrete,
5895
            true,
5896
            "Modalidade do frete"
5897
        );
5898
        return $this->transp;
5899
    }
5900
5901
    /**
5902
     * Grupo Transportador X03 pai X01
5903
     * tag NFe/infNFe/transp/tranporta (opcional)
5904
     * @param stdClass $std
5905
     * @return DOMElement
5906
     */
5907
    public function tagtransporta(stdClass $std)
5908
    {
5909
        $possible = [
5910
            'xNome',
5911
            'IE',
5912
            'xEnder',
5913
            'xMun',
5914
            'UF',
5915
            'CNPJ',
5916
            'CPF'
5917
        ];
5918
        $std = $this->equilizeParameters($std, $possible);
5919
        $transporta = $this->dom->createElement("transporta");
5920
        $this->dom->addChild(
5921
            $transporta,
5922
            "CNPJ",
5923
            $std->CNPJ,
5924
            false,
5925
            "CNPJ do Transportador"
5926
        );
5927
        $this->dom->addChild(
5928
            $transporta,
5929
            "CPF",
5930
            $std->CPF,
5931
            false,
5932
            "CPF do Transportador"
5933
        );
5934
        $this->dom->addChild(
5935
            $transporta,
5936
            "xNome",
5937
            $std->xNome,
5938
            false,
5939
            "Razão Social ou nome do Transportador"
5940
        );
5941
        $this->dom->addChild(
5942
            $transporta,
5943
            "IE",
5944
            $std->IE,
5945
            false,
5946
            "Inscrição Estadual do Transportador"
5947
        );
5948
        $this->dom->addChild(
5949
            $transporta,
5950
            "xEnder",
5951
            $std->xEnder,
5952
            false,
5953
            "Endereço Completo do Transportador"
5954
        );
5955
        $this->dom->addChild(
5956
            $transporta,
5957
            "xMun",
5958
            $std->xMun,
5959
            false,
5960
            "Nome do município do Transportador"
5961
        );
5962
        $this->dom->addChild(
5963
            $transporta,
5964
            "UF",
5965
            $std->UF,
5966
            false,
5967
            "Sigla da UF do Transportador"
5968
        );
5969
        $this->dom->appChild(
5970
            $this->transp,
5971
            $transporta,
5972
            'A tag transp deveria ter sido carregada primeiro.'
5973
        );
5974
        $this->dom->appChild($this->transp, $transporta, "Inclusão do node vol");
5975
        return $transporta;
5976
    }
5977
5978
    /**
5979
     * Grupo Retenção ICMS transporte X11 pai X01
5980
     * tag NFe/infNFe/transp/retTransp (opcional)
5981
     * @param stdClass $std
5982
     * @return DOMElement
5983
     */
5984
    public function tagretTransp(stdClass $std)
5985
    {
5986
        $possible = [
5987
            'vServ',
5988
            'vBCRet',
5989
            'pICMSRet',
5990
            'vICMSRet',
5991
            'CFOP',
5992
            'cMunFG'
5993
        ];
5994
        $std = $this->equilizeParameters($std, $possible);
5995
        $retTransp = $this->dom->createElement("retTransp");
5996
        $this->dom->addChild(
5997
            $retTransp,
5998
            "vServ",
5999
            $this->conditionalNumberFormatting($std->vServ),
6000
            true,
6001
            "Valor do Serviço"
6002
        );
6003
        $this->dom->addChild(
6004
            $retTransp,
6005
            "vBCRet",
6006
            $this->conditionalNumberFormatting($std->vBCRet),
6007
            true,
6008
            "BC da Retenção do ICMS"
6009
        );
6010
        $this->dom->addChild(
6011
            $retTransp,
6012
            "pICMSRet",
6013
            $this->conditionalNumberFormatting($std->pICMSRet, 4),
6014
            true,
6015
            "Alíquota da Retenção"
6016
        );
6017
        $this->dom->addChild(
6018
            $retTransp,
6019
            "vICMSRet",
6020
            $this->conditionalNumberFormatting($std->vICMSRet),
6021
            true,
6022
            "Valor do ICMS Retido"
6023
        );
6024
        $this->dom->addChild(
6025
            $retTransp,
6026
            "CFOP",
6027
            $std->CFOP,
6028
            true,
6029
            "CFOP"
6030
        );
6031
        $this->dom->addChild(
6032
            $retTransp,
6033
            "cMunFG",
6034
            $std->cMunFG,
6035
            true,
6036
            "Código do município de ocorrência do fato gerador do ICMS do transporte"
6037
        );
6038
        $this->dom->appChild(
6039
            $this->transp,
6040
            $retTransp,
6041
            'A tag transp deveria ter sido carregada primeiro.'
6042
        );
6043
        return $retTransp;
6044
    }
6045
6046
    /**
6047
     * Grupo Veículo Transporte X18 pai X17.1
6048
     * tag NFe/infNFe/transp/veicTransp (opcional)
6049
     * @param stdClass $std
6050
     * @return DOMElement
6051
     */
6052
    public function tagveicTransp(stdClass $std)
6053
    {
6054
        $possible = [
6055
            'placa',
6056
            'UF',
6057
            'RNTC'
6058
        ];
6059
        $std = $this->equilizeParameters($std, $possible);
6060
        $veicTransp = $this->dom->createElement("veicTransp");
6061
        $this->dom->addChild(
6062
            $veicTransp,
6063
            "placa",
6064
            $std->placa,
6065
            true,
6066
            "Placa do Veículo"
6067
        );
6068
        $this->dom->addChild(
6069
            $veicTransp,
6070
            "UF",
6071
            $std->UF,
6072
            true,
6073
            "Sigla da UF do Veículo"
6074
        );
6075
        $this->dom->addChild(
6076
            $veicTransp,
6077
            "RNTC",
6078
            $std->RNTC,
6079
            false,
6080
            "Registro Nacional de Transportador de Carga (ANTT) do Veículo"
6081
        );
6082
        $this->dom->appChild(
6083
            $this->transp,
6084
            $veicTransp,
6085
            'A tag transp deveria ter sido carregada primeiro.'
6086
        );
6087
        return $veicTransp;
6088
    }
6089
6090
    /**
6091
     * Grupo Reboque X22 pai X17.1
6092
     * tag NFe/infNFe/transp/reboque (opcional)
6093
     * @param stdClass $std
6094
     * @return DOMElement
6095
     */
6096
    public function tagreboque(stdClass $std)
6097
    {
6098
        $possible = [
6099
            'placa',
6100
            'UF',
6101
            'RNTC'
6102
        ];
6103
        $std = $this->equilizeParameters($std, $possible);
6104
        $reboque = $this->dom->createElement("reboque");
6105
        $this->dom->addChild(
6106
            $reboque,
6107
            "placa",
6108
            $std->placa,
6109
            true,
6110
            "Placa do Veículo Reboque"
6111
        );
6112
        $this->dom->addChild(
6113
            $reboque,
6114
            "UF",
6115
            $std->UF,
6116
            true,
6117
            "Sigla da UF do Veículo Reboque"
6118
        );
6119
        $this->dom->addChild(
6120
            $reboque,
6121
            "RNTC",
6122
            $std->RNTC,
6123
            false,
6124
            "Registro Nacional de Transportador de Carga (ANTT) do Veículo Reboque"
6125
        );
6126
        $this->dom->appChild(
6127
            $this->transp,
6128
            $reboque,
6129
            'A tag transp deveria ter sido carregada primeiro.'
6130
        );
6131
        return $reboque;
6132
    }
6133
6134
    /**
6135
     * Campo Vagao X25a pai X01
6136
     * tag NFe/infNFe/transp/vagao (opcional)
6137
     * @param stdClass $std
6138
     */
6139
    public function tagvagao(stdClass $std)
6140
    {
6141
        $possible = [
6142
            'vagao'
6143
        ];
6144
        $std = $this->equilizeParameters($std, $possible);
6145
        $this->dom->addChild(
6146
            $this->transp,
6147
            "vagao",
6148
            $std->vagao,
6149
            false,
6150
            "Identificação do vagão do Veículo Reboque"
6151
        );
6152
    }
6153
6154
    /**
6155
     * Campo Balsa X25b pai X01
6156
     * tag NFe/infNFe/transp/balsa (opcional)
6157
     * @param stdClass $std
6158
     */
6159
    public function tagbalsa(stdClass $std)
6160
    {
6161
        $possible = [
6162
            'balsa'
6163
        ];
6164
        $std = $this->equilizeParameters($std, $possible);
6165
        $this->dom->addChild(
6166
            $this->transp,
6167
            "balsa",
6168
            $std->balsa,
6169
            false,
6170
            "Identificação da balsa do Veículo Reboque"
6171
        );
6172
    }
6173
6174
    /**
6175
     * Grupo Volumes X26 pai X01
6176
     * tag NFe/infNFe/transp/vol (opcional)
6177
     * @param  stdClass $std
6178
     * @return DOMElement
6179
     */
6180
    public function tagvol(stdClass $std)
6181
    {
6182
        $possible = [
6183
            'item',
6184
            'qVol',
6185
            'esp',
6186
            'marca',
6187
            'nVol',
6188
            'pesoL',
6189
            'pesoB'
6190
        ];
6191
        $std = $this->equilizeParameters($std, $possible);
6192
        $vol = $this->dom->createElement("vol");
6193
        $this->dom->addChild(
6194
            $vol,
6195
            "qVol",
6196
            $std->qVol,
6197
            false,
6198
            "Quantidade de volumes transportados"
6199
        );
6200
        $this->dom->addChild(
6201
            $vol,
6202
            "esp",
6203
            $std->esp,
6204
            false,
6205
            "Espécie dos volumes transportados"
6206
        );
6207
        $this->dom->addChild(
6208
            $vol,
6209
            "marca",
6210
            $std->marca,
6211
            false,
6212
            "Marca dos volumes transportados"
6213
        );
6214
        $this->dom->addChild(
6215
            $vol,
6216
            "nVol",
6217
            $std->nVol,
6218
            false,
6219
            "Numeração dos volumes transportados"
6220
        );
6221
        $this->dom->addChild(
6222
            $vol,
6223
            "pesoL",
6224
            $this->conditionalNumberFormatting($std->pesoL, 3),
6225
            false,
6226
            "Peso Líquido (em kg) dos volumes transportados"
6227
        );
6228
        $this->dom->addChild(
6229
            $vol,
6230
            "pesoB",
6231
            $this->conditionalNumberFormatting($std->pesoB, 3),
6232
            false,
6233
            "Peso Bruto (em kg) dos volumes transportados"
6234
        );
6235
        $this->aVol[$std->item] = $vol;
6236
        return $vol;
6237
    }
6238
6239
    /**
6240
     * Grupo Lacres X33 pai X26
6241
     * tag NFe/infNFe/transp/vol/lacres (opcional)
6242
     * @param stdClass $std
6243
     * @return DOMElement
6244
     */
6245
    public function taglacres(stdClass $std)
6246
    {
6247
        $lacre = $this->dom->createElement("lacres");
6248
        $this->dom->addChild(
6249
            $lacre,
6250
            "nLacre",
6251
            $std->nLacre,
6252
            true,
6253
            "Número dos Lacres"
6254
        );
6255
        $this->dom->appChild($this->aVol[$std->item], $lacre, "Inclusão do node lacres");
6256
        return $lacre;
6257
    }
6258
6259
    /**
6260
     * Node vol
6261
     */
6262
    protected function buildVol()
6263
    {
6264
        foreach ($this->aVol as $num => $vol) {
6265
            $this->dom->appChild($this->transp, $vol, "Inclusão do node vol");
6266
        }
6267
    }
6268
6269
    /**
6270
     * Grupo Pagamento Y pai A01
6271
     * NOTA: Ajustado para NT2016_002_v1.30
6272
     * tag NFe/infNFe/pag (obrigatorio na NT2016_002_v1.30)
6273
     * Obrigatório para 55 e 65
6274
     * @param stdClass $std
6275
     * @return DOMElement
6276
     */
6277
    public function tagpag($std)
6278
    {
6279
        $possible = [
6280
            'vTroco'
6281
        ];
6282
        $std = $this->equilizeParameters($std, $possible);
6283
        $pag = $this->dom->createElement("pag");
6284
        //incluso no layout 4.00
6285
        $this->dom->addChild(
6286
            $pag,
6287
            "vTroco",
6288
            $this->conditionalNumberFormatting($std->vTroco),
6289
            false,
6290
            "Valor do troco"
6291
        );
6292
        return $this->pag = $pag;
6293
    }
6294
6295
    /**
6296
     * Grupo de Formas de Pagamento YA01a pai YA01
6297
     * NOTA: Ajuste NT_2016_002_v1.30
6298
     * NOTA: Ajuste NT_2016_002_v1 51
6299
     * tag NFe/infNFe/pag/detPag
6300
     * @param stdClass $std
6301
     * @return DOMElement
6302
     */
6303
    public function tagdetPag($std)
6304
    {
6305
        $possible = [
6306
            'indPag',
6307
            'tPag',
6308
            'vPag',
6309
            'CNPJ',
6310
            'tBand',
6311
            'cAut',
6312
            'tpIntegra'
6313
        ];
6314
        $std = $this->equilizeParameters($std, $possible);
6315
        //padrão para layout 4.00
6316
        $detPag = $this->dom->createElement("detPag");
6317
        $this->dom->addChild(
6318
            $detPag,
6319
            "indPag",
6320
            $std->indPag,
6321
            false,
6322
            "Indicador da Forma de Pagamento"
6323
        );
6324
        $this->dom->addChild(
6325
            $detPag,
6326
            "tPag",
6327
            $std->tPag,
6328
            true,
6329
            "Forma de pagamento"
6330
        );
6331
        $this->dom->addChild(
6332
            $detPag,
6333
            "vPag",
6334
            $this->conditionalNumberFormatting($std->vPag),
6335
            true,
6336
            "Valor do Pagamento"
6337
        );
6338
        if (!empty($std->tpIntegra)) {
6339
            $card = $this->dom->createElement("card");
6340
            $this->dom->addChild(
6341
                $card,
6342
                "tpIntegra",
6343
                $std->tpIntegra,
6344
                true,
6345
                "Tipo de Integração para pagamento"
6346
            );
6347
            $this->dom->addChild(
6348
                $card,
6349
                "CNPJ",
6350
                !empty($std->CNPJ) ? $std->CNPJ : null,
6351
                false,
6352
                "CNPJ da Credenciadora de cartão de crédito e/ou débito"
6353
            );
6354
            $this->dom->addChild(
6355
                $card,
6356
                "tBand",
6357
                !empty($std->tBand) ? $std->tBand : null,
6358
                false,
6359
                "Bandeira da operadora de cartão de crédito e/ou débito"
6360
            );
6361
            $this->dom->addChild(
6362
                $card,
6363
                "cAut",
6364
                !empty($std->cAut) ? $std->cAut : null,
6365
                false,
6366
                "Número de autorização da operação cartão de crédito e/ou débito"
6367
            );
6368
            $this->dom->appChild($detPag, $card, "Inclusão do node Card");
6369
        }
6370
        $node = !empty($this->pag->getElementsByTagName("vTroco")->item(0))
6371
            ? $this->pag->getElementsByTagName("vTroco")->item(0)
6372
            : null;
6373
        if (!empty($node)) {
6374
            $this->pag->insertBefore($detPag, $node);
6375
        } else {
6376
            $this->dom->appChild($this->pag, $detPag, 'Falta tag "Pag"');
6377
        }
6378
        return $detPag;
6379
    }
6380
6381
    /**
6382
     * Grupo Fatura Y02 pai Y01
6383
     * tag NFe/infNFe/cobr/fat (opcional)
6384
     * @param stdClass $std
6385
     * @return DOMElement
6386
     */
6387
    public function tagfat(stdClass $std)
6388
    {
6389
        $possible = [
6390
            'nFat',
6391
            'vOrig',
6392
            'vDesc',
6393
            'vLiq'
6394
        ];
6395
        $std = $this->equilizeParameters($std, $possible);
6396
        $this->buildCobr();
6397
        $fat = $this->dom->createElement("fat");
6398
        $this->dom->addChild(
6399
            $fat,
6400
            "nFat",
6401
            $std->nFat,
6402
            false,
6403
            "Número da Fatura"
6404
        );
6405
        $this->dom->addChild(
6406
            $fat,
6407
            "vOrig",
6408
            $this->conditionalNumberFormatting($std->vOrig),
6409
            false,
6410
            "Valor Original da Fatura"
6411
        );
6412
        $this->dom->addChild(
6413
            $fat,
6414
            "vDesc",
6415
            $this->conditionalNumberFormatting($std->vDesc),
6416
            false,
6417
            "Valor do desconto"
6418
        );
6419
        $this->dom->addChild(
6420
            $fat,
6421
            "vLiq",
6422
            $this->conditionalNumberFormatting($std->vLiq),
6423
            false,
6424
            "Valor Líquido da Fatura"
6425
        );
6426
        $this->dom->appChild($this->cobr, $fat);
6427
        return $fat;
6428
    }
6429
6430
    /**
6431
     * Grupo Duplicata Y07 pai Y02
6432
     * tag NFe/infNFe/cobr/fat/dup (opcional)
6433
     * É necessário criar a tag fat antes de criar as duplicatas
6434
     * @param stdClass $std
6435
     * @return DOMElement
6436
     */
6437
    public function tagdup(stdClass $std)
6438
    {
6439
        $possible = [
6440
            'nDup',
6441
            'dVenc',
6442
            'vDup'
6443
        ];
6444
        $std = $this->equilizeParameters($std, $possible);
6445
        $this->buildCobr();
6446
        $dup = $this->dom->createElement("dup");
6447
        $this->dom->addChild(
6448
            $dup,
6449
            "nDup",
6450
            $std->nDup,
6451
            false,
6452
            "Número da Duplicata"
6453
        );
6454
        $this->dom->addChild(
6455
            $dup,
6456
            "dVenc",
6457
            $std->dVenc,
6458
            false,
6459
            "Data de vencimento"
6460
        );
6461
        $this->dom->addChild(
6462
            $dup,
6463
            "vDup",
6464
            $this->conditionalNumberFormatting($std->vDup),
6465
            true,
6466
            "Valor da duplicata"
6467
        );
6468
        $this->dom->appChild($this->cobr, $dup, 'Inclui duplicata na tag cobr');
6469
        return $dup;
6470
    }
6471
6472
    /**
6473
     * Grupo de Informações Adicionais Z01 pai A01
6474
     * tag NFe/infNFe/infAdic (opcional)
6475
     * @param stdClass $std
6476
     * @return DOMElement
6477
     */
6478
    public function taginfAdic(stdClass $std)
6479
    {
6480
        $possible = ['infAdFisco', 'infCpl'];
6481
        $std = $this->equilizeParameters($std, $possible);
6482
        $this->buildInfAdic();
6483
        $this->dom->addChild(
6484
            $this->infAdic,
6485
            "infAdFisco",
6486
            $std->infAdFisco,
6487
            false,
6488
            "Informações Adicionais de Interesse do Fisco"
6489
        );
6490
        $this->dom->addChild(
6491
            $this->infAdic,
6492
            "infCpl",
6493
            $std->infCpl,
6494
            false,
6495
            "Informações Complementares de interesse do Contribuinte"
6496
        );
6497
        return $this->infAdic;
6498
    }
6499
6500
    /**
6501
     * Grupo Campo de uso livre do contribuinte Z04 pai Z01
6502
     * tag NFe/infNFe/infAdic/obsCont (opcional)
6503
     * O método taginfAdic deve ter sido carregado antes
6504
     * @param stdClass $std
6505
     * @return DOMElement
6506
     */
6507
    public function tagobsCont(stdClass $std)
6508
    {
6509
        $possible = ['xCampo', 'xTexto'];
6510
        $std = $this->equilizeParameters($std, $possible);
6511
        $this->buildInfAdic();
6512
        $obsCont = $this->dom->createElement("obsCont");
6513
        $obsCont->setAttribute("xCampo", $std->xCampo);
6514
        $this->dom->addChild(
6515
            $obsCont,
6516
            "xTexto",
6517
            $std->xTexto,
6518
            true,
6519
            "Conteúdo do campo"
6520
        );
6521
        $this->dom->appChild($this->infAdic, $obsCont, '');
6522
        return $obsCont;
6523
    }
6524
6525
    /**
6526
     * Grupo Campo de uso livre do Fisco Z07 pai Z01
6527
     * tag NFe/infNFe/infAdic/obsFisco (opcional)
6528
     * O método taginfAdic deve ter sido carregado antes
6529
     * @param stdClass $std
6530
     * @return DOMElement
6531
     */
6532
    public function tagobsFisco(stdClass $std)
6533
    {
6534
        $possible = ['xCampo', 'xTexto'];
6535
        $std = $this->equilizeParameters($std, $possible);
6536
        $this->buildInfAdic();
6537
        $obsFisco = $this->dom->createElement("obsFisco");
6538
        $obsFisco->setAttribute("xCampo", $std->xCampo);
6539
        $this->dom->addChild(
6540
            $obsFisco,
6541
            "xTexto",
6542
            $std->xTexto,
6543
            true,
6544
            "Conteúdo do campo"
6545
        );
6546
        $this->dom->appChild($this->infAdic, $obsFisco, '');
6547
        return $obsFisco;
6548
    }
6549
6550
    /**
6551
     * Grupo Processo referenciado Z10 pai Z01 (NT2012.003)
6552
     * tag NFe/infNFe/procRef (opcional)
6553
     * O método taginfAdic deve ter sido carregado antes
6554
     * @param stdClass $std
6555
     * @return DOMElement
6556
     */
6557
    public function tagprocRef($std)
6558
    {
6559
        $possible = ['nProc', 'indProc'];
6560
        $std = $this->equilizeParameters($std, $possible);
6561
        $this->buildInfAdic();
6562
        $procRef = $this->dom->createElement("procRef");
6563
        $this->dom->addChild(
6564
            $procRef,
6565
            "nProc",
6566
            $std->nProc,
6567
            true,
6568
            "Identificador do processo ou ato concessório"
6569
        );
6570
        $this->dom->addChild(
6571
            $procRef,
6572
            "indProc",
6573
            $std->indProc,
6574
            true,
6575
            "Indicador da origem do processo"
6576
        );
6577
        $this->dom->appChild($this->infAdic, $procRef, '');
6578
        return $procRef;
6579
    }
6580
6581
    /**
6582
     * Grupo Exportação ZA01 pai A01
6583
     * tag NFe/infNFe/exporta (opcional)
6584
     * @param stdClass $std
6585
     * @return DOMElement
6586
     */
6587
    public function tagexporta(stdClass $std)
6588
    {
6589
        $possible = ['UFSaidaPais', 'xLocExporta', 'xLocDespacho'];
6590
        $std = $this->equilizeParameters($std, $possible);
6591
        $this->exporta = $this->dom->createElement("exporta");
6592
        $this->dom->addChild(
6593
            $this->exporta,
6594
            "UFSaidaPais",
6595
            $std->UFSaidaPais,
6596
            true,
6597
            "Sigla da UF de Embarque ou de transposição de fronteira"
6598
        );
6599
        $this->dom->addChild(
6600
            $this->exporta,
6601
            "xLocExporta",
6602
            $std->xLocExporta,
6603
            true,
6604
            "Descrição do Local de Embarque ou de transposição de fronteira"
6605
        );
6606
        $this->dom->addChild(
6607
            $this->exporta,
6608
            "xLocDespacho",
6609
            $std->xLocDespacho,
6610
            false,
6611
            "Descrição do local de despacho"
6612
        );
6613
        return $this->exporta;
6614
    }
6615
6616
    /**
6617
     * Grupo Compra ZB01 pai A01
6618
     * tag NFe/infNFe/compra (opcional)
6619
     * @param stdClass $std
6620
     * @return DOMElement
6621
     */
6622
    public function tagcompra(stdClass $std)
6623
    {
6624
        $possible = ['xNEmp', 'xPed', 'xCont'];
6625
        $std = $this->equilizeParameters($std, $possible);
6626
        $this->compra = $this->dom->createElement("compra");
6627
        $this->dom->addChild(
6628
            $this->compra,
6629
            "xNEmp",
6630
            $std->xNEmp,
6631
            false,
6632
            "Nota de Empenho"
6633
        );
6634
        $this->dom->addChild(
6635
            $this->compra,
6636
            "xPed",
6637
            $std->xPed,
6638
            false,
6639
            "Pedido"
6640
        );
6641
        $this->dom->addChild(
6642
            $this->compra,
6643
            "xCont",
6644
            $std->xCont,
6645
            false,
6646
            "Contrato"
6647
        );
6648
        return $this->compra;
6649
    }
6650
6651
    /**
6652
     * Grupo Cana ZC01 pai A01
6653
     * tag NFe/infNFe/cana (opcional)
6654
     * @param stdClass $std
6655
     * @return DOMElement
6656
     */
6657
    public function tagcana(stdClass $std)
6658
    {
6659
        $possible = [
6660
            'safra',
6661
            'ref',
6662
            'qTotMes',
6663
            'qTotAnt',
6664
            'qTotGer',
6665
            'vFor',
6666
            'vTotDed',
6667
            'vLiqFor'
6668
        ];
6669
        $std = $this->equilizeParameters($std, $possible);
6670
        $this->cana = $this->dom->createElement("cana");
6671
        $this->dom->addChild(
6672
            $this->cana,
6673
            "safra",
6674
            $std->safra,
6675
            true,
6676
            "Identificação da safra"
6677
        );
6678
        $this->dom->addChild(
6679
            $this->cana,
6680
            "ref",
6681
            $std->ref,
6682
            true,
6683
            "Mês e ano de referência"
6684
        );
6685
        $this->dom->addChild(
6686
            $this->cana,
6687
            "qTotMes",
6688
            $std->qTotMes,
6689
            true,
6690
            "Quantidade Total do Mês"
6691
        );
6692
        $this->dom->addChild(
6693
            $this->cana,
6694
            "qTotAnt",
6695
            $std->qTotAnt,
6696
            true,
6697
            "Quantidade Total Anterior"
6698
        );
6699
        $this->dom->addChild(
6700
            $this->cana,
6701
            "qTotGer",
6702
            $std->qTotGer,
6703
            true,
6704
            "Quantidade Total Geral"
6705
        );
6706
        $this->dom->addChild(
6707
            $this->cana,
6708
            "vFor",
6709
            $this->conditionalNumberFormatting($std->vFor),
6710
            true,
6711
            "Valor dos Fornecimentos"
6712
        );
6713
        $this->dom->addChild(
6714
            $this->cana,
6715
            "vTotDed",
6716
            $this->conditionalNumberFormatting($std->vTotDed),
6717
            true,
6718
            "Valor Total da Dedução"
6719
        );
6720
        $this->dom->addChild(
6721
            $this->cana,
6722
            "vLiqFor",
6723
            $this->conditionalNumberFormatting($std->vLiqFor),
6724
            true,
6725
            "Valor Líquido dos Fornecimentos"
6726
        );
6727
        return $this->cana;
6728
    }
6729
6730
    /**
6731
     * Grupo Fornecimento diário de cana ZC04 pai ZC01
6732
     * tag NFe/infNFe/cana/forDia
6733
     * @param stdClass $std
6734
     * @return DOMElement
6735
     */
6736
    public function tagforDia(stdClass $std)
6737
    {
6738
        $forDia = $this->dom->createElement("forDia");
6739
        $forDia->setAttribute("dia", $std->dia);
6740
        $this->dom->addChild(
6741
            $forDia,
6742
            "qtde",
6743
            $std->qtde,
6744
            true,
6745
            "Quantidade"
6746
        );
6747
        $qTotMes = $this->cana->getElementsByTagName('qTotMes')->item(0);
6748
        $this->cana->insertBefore($forDia, $qTotMes);
6749
        return $forDia;
6750
    }
6751
6752
    /**
6753
     * Grupo Deduções – Taxas e Contribuições ZC10 pai ZC01
6754
     * tag NFe/infNFe/cana/deduc (opcional)
6755
     * @param stdClass $std
6756
     * @return DOMElement
6757
     */
6758
    public function tagdeduc(stdClass $std)
6759
    {
6760
        $possible = ['xDed', 'vDed'];
6761
        $std = $this->equilizeParameters($std, $possible);
6762
        $deduc = $this->dom->createElement("deduc");
6763
        $this->dom->addChild(
6764
            $deduc,
6765
            "xDed",
6766
            $std->xDed,
6767
            true,
6768
            "Descrição da Dedução"
6769
        );
6770
        $this->dom->addChild(
6771
            $deduc,
6772
            "vDed",
6773
            $this->conditionalNumberFormatting($std->vDed),
6774
            true,
6775
            "Valor da Dedução"
6776
        );
6777
        $vFor = $this->cana->getElementsByTagName('vFor')->item(0);
6778
        $this->cana->insertBefore($deduc, $vFor);
6779
        return $deduc;
6780
    }
6781
6782
    /**
6783
     * Informações suplementares da Nota Fiscal
6784
     * @param stdClass $std
6785
     * @return DOMElement
6786
     */
6787
    public function taginfNFeSupl(stdClass $std)
6788
    {
6789
        $possible = ['qrcode', 'urlChave'];
6790
        $std = $this->equilizeParameters($std, $possible);
6791
6792
        $infNFeSupl = $this->dom->createElement("infNFeSupl");
6793
        $nodeqr = $infNFeSupl->appendChild($this->dom->createElement('qrCode'));
6794
        $nodeqr->appendChild($this->dom->createCDATASection($std->qrcode));
6795
        //incluido no layout 4.00
6796
        $std->urlChave = !empty($std->urlChave) ? $std->urlChave : null;
6797
        $this->dom->addChild(
6798
            $infNFeSupl,
6799
            "urlChave",
6800
            $std->urlChave,
6801
            false,
6802
            "URL de consulta por chave de acesso a ser impressa no DANFE NFC-e"
6803
        );
6804
        $this->infNFeSupl = $infNFeSupl;
6805
        return $infNFeSupl;
6806
    }
6807
6808
    /**
6809
     * Informações do Responsável técnico ZD01 pai A01
6810
     * tag NFe/infNFe/infRespTec (opcional)
6811
     * @param stdClass $std
6812
     * @return DOMElement
6813
     * @throws RuntimeException
6814
     */
6815
    public function taginfRespTec(stdClass $std)
6816
    {
6817
        $possible = [
6818
            'CNPJ',
6819
            'xContato',
6820
            'email',
6821
            'fone',
6822
            'CSRT',
6823
            'idCSRT'
6824
        ];
6825
6826
        $std = $this->equilizeParameters($std, $possible);
6827
        $infRespTec = $this->dom->createElement("infRespTec");
6828
        $this->dom->addChild(
6829
            $infRespTec,
6830
            "CNPJ",
6831
            $std->CNPJ,
6832
            true,
6833
            "Informar o CNPJ da pessoa jurídica responsável pelo sistema "
6834
            . "utilizado na emissão do documento fiscal eletrônico"
6835
        );
6836
        $this->dom->addChild(
6837
            $infRespTec,
6838
            "xContato",
6839
            $std->xContato,
6840
            true,
6841
            "Informar o nome da pessoa a ser contatada na empresa desenvolvedora "
6842
            . "do sistema utilizado na emissão do documento fiscal eletrônico"
6843
        );
6844
        $this->dom->addChild(
6845
            $infRespTec,
6846
            "email",
6847
            $std->email,
6848
            true,
6849
            "Informar o e-mail da pessoa a ser contatada na empresa "
6850
            . "desenvolvedora do sistema."
6851
        );
6852
        $this->dom->addChild(
6853
            $infRespTec,
6854
            "fone",
6855
            $std->fone,
6856
            true,
6857
            "Informar o telefone da pessoa a ser contatada na empresa "
6858
            . "desenvolvedora do sistema."
6859
        );
6860
        if (!empty($std->CSRT) && !empty($std->idCSRT)) {
6861
            $this->csrt = $std->CSRT;
6862
            $this->dom->addChild(
6863
                $infRespTec,
6864
                "idCSRT",
6865
                $std->idCSRT,
6866
                true,
6867
                "Identificador do CSRT utilizado para montar o hash do CSRT"
6868
            );
6869
            $this->dom->addChild(
6870
                $infRespTec,
6871
                "hashCSRT",
6872
                $this->hashCSRT($std->CSRT),
6873
                true,
6874
                "hash do CSRT"
6875
            );
6876
        }
6877
        $this->infRespTec = $infRespTec;
6878
        return $infRespTec;
6879
    }
6880
6881
    /**
6882
     * Tag raiz da NFe
6883
     * tag NFe DOMNode
6884
     * Função chamada pelo método [ monta ]
6885
     *
6886
     * @return DOMElement
6887
     */
6888
    protected function buildNFe()
6889
    {
6890
        if (empty($this->NFe)) {
6891
            $this->NFe = $this->dom->createElement("NFe");
6892
            $this->NFe->setAttribute("xmlns", "http://www.portalfiscal.inf.br/nfe");
6893
        }
6894
        return $this->NFe;
6895
    }
6896
6897
    /**
6898
     * Informação de Documentos Fiscais referenciados BA01 pai B01
6899
     * tag NFe/infNFe/ide/NFref
6900
     * Podem ser criados até 500 desses Nodes por NFe
6901
     * Função chamada pelos métodos
6902
     * [tagrefNFe] [tagrefNF] [tagrefNFP]  [tagCTeref] [tagrefECF]
6903
     */
6904
    protected function buildNFref()
6905
    {
6906
        $this->aNFref[] = $this->dom->createElement("NFref");
6907
        return count($this->aNFref);
6908
    }
6909
6910
    /**
6911
     * Insere dentro dentro das tags imposto o ICMS IPI II PIS COFINS ISSQN
6912
     * tag NFe/infNFe/det[]/imposto
6913
     * @return void
6914
     */
6915
    protected function buildImp()
6916
    {
6917
        foreach ($this->aImposto as $nItem => $imposto) {
6918
            if (!empty($this->aICMS[$nItem])) {
6919
                $this->dom->appChild($imposto, $this->aICMS[$nItem], "Inclusão do node ICMS");
6920
            }
6921
            if (!empty($this->aIPI[$nItem])) {
6922
                $this->dom->appChild($imposto, $this->aIPI[$nItem], "Inclusão do node IPI");
6923
            }
6924
            if (!empty($this->aII[$nItem])) {
6925
                $this->dom->appChild($imposto, $this->aII[$nItem], "Inclusão do node II");
6926
            }
6927
            if (!empty($this->aISSQN[$nItem])) {
6928
                $this->dom->appChild($imposto, $this->aISSQN[$nItem], "Inclusão do node ISSQN");
6929
            }
6930
            if (!empty($this->aPIS[$nItem])) {
6931
                $this->dom->appChild($imposto, $this->aPIS[$nItem], "Inclusão do node PIS");
6932
            }
6933
            if (!empty($this->aPISST[$nItem])) {
6934
                $this->dom->appChild($imposto, $this->aPISST[$nItem], "Inclusão do node PISST");
6935
            }
6936
            if (!empty($this->aCOFINS[$nItem])) {
6937
                $this->dom->appChild($imposto, $this->aCOFINS[$nItem], "Inclusão do node COFINS");
6938
            }
6939
            if (!empty($this->aCOFINSST[$nItem])) {
6940
                $this->dom->appChild($imposto, $this->aCOFINSST[$nItem], "Inclusão do node COFINSST");
6941
            }
6942
            if (!empty($this->aICMSUFDest[$nItem])) {
6943
                $this->dom->appChild($imposto, $this->aICMSUFDest[$nItem], "Inclusão do node ICMSUFDest");
6944
            }
6945
            $this->aImposto[$nItem] = $imposto;
6946
        }
6947
    }
6948
6949
    /**
6950
     * Grupo COFINS tributado pela alíquota S02 pai S01
6951
     * tag det/imposto/COFINS/COFINSAliq (opcional)
6952
     * Função chamada pelo método [ tagCOFINS ]
6953
     * @param stdClass $std
6954
     * @return DOMElement
6955
     */
6956
    protected function buildCOFINSAliq($std)
6957
    {
6958
        $confinsAliq = $this->dom->createElement('COFINSAliq');
6959
        $this->dom->addChild(
6960
            $confinsAliq,
6961
            'CST',
6962
            $std->CST,
6963
            true,
6964
            "Código de Situação Tributária da COFINS"
6965
        );
6966
        $this->dom->addChild(
6967
            $confinsAliq,
6968
            'vBC',
6969
            $this->conditionalNumberFormatting($std->vBC),
6970
            true,
6971
            "Valor da Base de Cálculo da COFINS"
6972
        );
6973
        $this->dom->addChild(
6974
            $confinsAliq,
6975
            'pCOFINS',
6976
            $this->conditionalNumberFormatting($std->pCOFINS, 4),
6977
            true,
6978
            "Alíquota da COFINS (em percentual)"
6979
        );
6980
        $this->dom->addChild(
6981
            $confinsAliq,
6982
            'vCOFINS',
6983
            $this->conditionalNumberFormatting($std->vCOFINS),
6984
            true,
6985
            "Valor da COFINS"
6986
        );
6987
        return $confinsAliq;
6988
    }
6989
6990
    /**
6991
     * Grupo COFINS não tributado S04 pai S01
6992
     * tag NFe/infNFe/det[]/imposto/COFINS/COFINSNT (opcional)
6993
     * Função chamada pelo método [ tagCOFINS ]
6994
     * @param stdClass $std
6995
     * @return DOMElement
6996
     */
6997
    protected function buildCOFINSNT(stdClass $std)
6998
    {
6999
        $confinsnt = $this->dom->createElement('COFINSNT');
7000
        $this->dom->addChild(
7001
            $confinsnt,
7002
            "CST",
7003
            $std->CST,
7004
            true,
7005
            "Código de Situação Tributária da COFINS"
7006
        );
7007
        return $confinsnt;
7008
    }
7009
7010
    /**
7011
     * Grupo COFINS Outras Operações S05 pai S01
7012
     * tag NFe/infNFe/det[]/imposto/COFINS/COFINSoutr (opcional)
7013
     * Função chamada pelo método [ tagCOFINS ]
7014
     * @param stdClass $std
7015
     * @return DOMElement
7016
     */
7017
    protected function buildCOFINSoutr(stdClass $std)
7018
    {
7019
        $confinsoutr = $this->dom->createElement('COFINSOutr');
7020
        $this->dom->addChild(
7021
            $confinsoutr,
7022
            "CST",
7023
            $std->CST,
7024
            true,
7025
            "Código de Situação Tributária da COFINS"
7026
        );
7027
        $this->dom->addChild(
7028
            $confinsoutr,
7029
            "vBC",
7030
            $this->conditionalNumberFormatting($std->vBC),
7031
            ($std->vBC !== null) ? true : false,
7032
            "Valor da Base de Cálculo da COFINS"
7033
        );
7034
        $this->dom->addChild(
7035
            $confinsoutr,
7036
            "pCOFINS",
7037
            $this->conditionalNumberFormatting($std->pCOFINS, 4),
7038
            ($std->pCOFINS !== null) ? true : false,
7039
            "Alíquota da COFINS (em percentual)"
7040
        );
7041
        $this->dom->addChild(
7042
            $confinsoutr,
7043
            "qBCProd",
7044
            $std->qBCProd,
7045
            ($std->qBCProd !== null) ? true : false,
7046
            "Quantidade Vendida"
7047
        );
7048
        $this->dom->addChild(
7049
            $confinsoutr,
7050
            "vAliqProd",
7051
            $std->vAliqProd,
7052
            ($std->vAliqProd !== null) ? true : false,
7053
            "Alíquota da COFINS (em reais)"
7054
        );
7055
        $this->dom->addChild(
7056
            $confinsoutr,
7057
            "vCOFINS",
7058
            $this->conditionalNumberFormatting($std->vCOFINS),
7059
            true,
7060
            "Valor da COFINS"
7061
        );
7062
        return $confinsoutr;
7063
    }
7064
7065
    /**
7066
     * Insere dentro da tag det os produtos
7067
     * tag NFe/infNFe/det[]
7068
     * @return array|string
7069
     */
7070
    protected function buildDet()
7071
    {
7072
        if (empty($this->aProd)) {
7073
            return '';
7074
        }
7075
        //insere NVE
7076
        foreach ($this->aNVE as $nItem => $nve) {
7077
            $prod = $this->aProd[$nItem];
7078
            foreach ($nve as $child) {
7079
                $node = $prod->getElementsByTagName("cBenef")->item(0);
7080
                if (empty($node)) {
7081
                    $node = $prod->getElementsByTagName("EXTIPI")->item(0);
7082
                    if (empty($node)) {
7083
                        $node = $prod->getElementsByTagName("CFOP")->item(0);
7084
                    }
7085
                }
7086
                $prod->insertBefore($child, $node);
7087
            }
7088
        }
7089
        //insere CEST
7090
        foreach ($this->aCest as $nItem => $cest) {
7091
            $prod = $this->aProd[$nItem];
7092
            /** @var \DOMElement $child */
7093
            foreach ($cest as $child) {
7094
                $node = $prod->getElementsByTagName("cBenef")->item(0);
7095
                if (empty($node)) {
7096
                    $node = $prod->getElementsByTagName("EXTIPI")->item(0);
7097
                    if (empty($node)) {
7098
                        $node = $prod->getElementsByTagName("CFOP")->item(0);
7099
                    }
7100
                }
7101
                $cchild = $child->getElementsByTagName("CEST")->item(0);
7102
                $prod->insertBefore($cchild, $node);
7103
                $cchild = $child->getElementsByTagName("indEscala")->item(0);
7104
                if (!empty($cchild)) {
7105
                    $prod->insertBefore($cchild, $node);
7106
                }
7107
                $cchild = $child->getElementsByTagName("CNPJFab")->item(0);
7108
                if (!empty($cchild)) {
7109
                    $prod->insertBefore($cchild, $node);
7110
                    $this->aProd[$nItem] = $prod;
7111
                }
7112
            }
7113
        }
7114
        //insere DI
7115
        foreach ($this->aDI as $nItem => $aDI) {
7116
            $prod = $this->aProd[$nItem];
7117
            foreach ($aDI as $child) {
7118
                $node = $prod->getElementsByTagName("xPed")->item(0);
7119
                if (!empty($node)) {
7120
                    $prod->insertBefore($child, $node);
7121
                } else {
7122
                    $node = $prod->getElementsByTagName("FCI")->item(0);
7123
                    if (!empty($node)) {
7124
                        $prod->insertBefore($child, $node);
7125
                    } else {
7126
                        $this->dom->appChild($prod, $child, "Inclusão do node DI");
7127
                    }
7128
                }
7129
            }
7130
            $this->aProd[$nItem] = $prod;
7131
        }
7132
        //insere detExport
7133
        foreach ($this->aDetExport as $nItem => $detexport) {
7134
            $prod = $this->aProd[$nItem];
7135
            foreach ($detexport as $child) {
7136
                $node = $prod->getElementsByTagName("xPed")->item(0);
7137
                if (!empty($node)) {
7138
                    $prod->insertBefore($child, $node);
7139
                } else {
7140
                    $this->dom->appChild($prod, $child, "Inclusão do node DetExport");
7141
                }
7142
            }
7143
            $this->aProd[$nItem] = $prod;
7144
        }
7145
        //insere Rastro
7146
        foreach ($this->aRastro as $nItem => $child) {
7147
            $prod = $this->aProd[$nItem];
7148
            foreach ($child as $rastro) {
7149
                $this->dom->appChild($prod, $rastro, "Inclusão do node Rastro");
7150
            }
7151
            $this->aProd[$nItem] = $prod;
7152
        }
7153
        //insere veiculo
7154
        foreach ($this->aVeicProd as $nItem => $child) {
7155
            $prod = $this->aProd[$nItem];
7156
            $this->dom->appChild($prod, $child, "Inclusão do node veiculo");
7157
            $this->aProd[$nItem] = $prod;
7158
        }
7159
        //insere medicamentos
7160
        foreach ($this->aMed as $nItem => $child) {
7161
            $prod = $this->aProd[$nItem];
7162
            $this->dom->appChild($prod, $child, "Inclusão do node medicamento");
7163
            $this->aProd[$nItem] = $prod;
7164
        }
7165
        //insere armas
7166
        foreach ($this->aArma as $nItem => $arma) {
7167
            $prod = $this->aProd[$nItem];
7168
            foreach ($arma as $child) {
7169
                $node = $prod->getElementsByTagName("imposto")->item(0);
7170
                if (!empty($node)) {
7171
                    $prod->insertBefore($child, $node);
7172
                } else {
7173
                    $this->dom->appChild($prod, $child, "Inclusão do node arma");
7174
                }
7175
            }
7176
            $this->aProd[$nItem] = $prod;
7177
        }
7178
        //insere combustivel
7179
        foreach ($this->aComb as $nItem => $child) {
7180
            $prod = $this->aProd[$nItem];
7181
            if (!empty($this->aEncerrante)) {
7182
                $encerrante = $this->aEncerrante[$nItem];
7183
                if (!empty($encerrante)) {
7184
                    $this->dom->appChild($child, $encerrante, "inclusão do node encerrante na tag comb");
7185
                }
7186
            }
7187
            $this->dom->appChild($prod, $child, "Inclusão do node combustivel");
7188
            $this->aProd[$nItem] = $prod;
7189
        }
7190
        //insere RECOPI
7191
        foreach ($this->aRECOPI as $nItem => $child) {
7192
            $prod = $this->aProd[$nItem];
7193
            $this->dom->appChild($prod, $child, "Inclusão do node RECOPI");
7194
            $this->aProd[$nItem] = $prod;
7195
        }
7196
        //montagem da tag imposto[]
7197
        $this->buildImp();
7198
        //montagem da tag det[]
7199
        foreach ($this->aProd as $nItem => $prod) {
7200
            $det = $this->dom->createElement("det");
7201
            $det->setAttribute("nItem", $nItem);
7202
            $det->appendChild($prod);
7203
            //insere imposto
7204
            if (!empty($this->aImposto[$nItem])) {
7205
                $child = $this->aImposto[$nItem];
7206
                $this->dom->appChild($det, $child, "Inclusão do node imposto");
7207
            }
7208
            //insere impostoDevol
7209
            if (!empty($this->aImpostoDevol)) {
7210
                $child = $this->aImpostoDevol[$nItem];
7211
                $this->dom->appChild($det, $child, "Inclusão do node impostoDevol");
7212
            }
7213
            //insere infAdProd
7214
            if (!empty($this->aInfAdProd[$nItem])) {
7215
                $child = $this->aInfAdProd[$nItem];
7216
                $this->dom->appChild($det, $child, "Inclusão do node infAdProd");
7217
            }
7218
            $this->aDet[] = $det;
7219
            $det = null;
0 ignored issues
show
Unused Code introduced by
$det 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...
7220
        }
7221
        return $this->aProd;
7222
    }
7223
7224
    /**
7225
     * Insere a tag pag, os detalhamentos dos pagamentos e cartoes
7226
     * NOTA: Ajustado para NT2016_002_v1.30
7227
     * tag NFe/infNFe/pag/
7228
     * tag NFe/infNFe/pag/detPag[]
7229
     * tag NFe/infNFe/pag/detPag[]/Card
7230
     */
7231
    protected function buildTagPag()
7232
    {
7233
        $this->dom->appChild($this->infNFe, $this->pag, 'Falta tag "infNFe"');
7234
    }
7235
7236
    /**
7237
     * Grupo Totais da NF-e W01 pai A01
7238
     * tag NFe/infNFe/total
7239
     */
7240
    protected function buildTotal()
7241
    {
7242
        if (empty($this->total)) {
7243
            $this->total = $this->dom->createElement("total");
7244
        }
7245
7246
        $this->stdTot->vNF = $this->stdTot->vProd
7247
            - $this->stdTot->vDesc
7248
            + $this->stdTot->vST
7249
            + $this->stdTot->vFrete
7250
            + $this->stdTot->vSeg
7251
            + $this->stdTot->vOutro
7252
            + $this->stdTot->vII
7253
            + $this->stdTot->vIPI
7254
            + $this->stdTot->vIPIDevol;
7255
        //round all values
7256
        $this->stdTot->vBC = round($this->stdTot->vBC, 2);
7257
        $this->stdTot->vICMS = round($this->stdTot->vICMS, 2);
7258
        $this->stdTot->vICMSDeson = round($this->stdTot->vICMSDeson, 2);
7259
        $this->stdTot->vFCP = round($this->stdTot->vFCP, 2);
7260
        $this->stdTot->vFCPUFDest = round($this->stdTot->vFCPUFDest, 2);
7261
        $this->stdTot->vICMSUFDest = round($this->stdTot->vICMSUFDest, 2);
7262
        $this->stdTot->vICMSUFRemet = round($this->stdTot->vICMSUFRemet, 2);
7263
        $this->stdTot->vBCST = round($this->stdTot->vBCST, 2);
7264
        $this->stdTot->vST = round($this->stdTot->vST, 2);
7265
        $this->stdTot->vFCPST = round($this->stdTot->vFCPST, 2);
7266
        $this->stdTot->vFCPSTRet = round($this->stdTot->vFCPSTRet, 2);
7267
        $this->stdTot->vProd = round($this->stdTot->vProd, 2);
7268
        $this->stdTot->vFrete = round($this->stdTot->vFrete, 2);
7269
        $this->stdTot->vSeg = round($this->stdTot->vSeg, 2);
7270
        $this->stdTot->vDesc = round($this->stdTot->vDesc, 2);
7271
        $this->stdTot->vII = round($this->stdTot->vII, 2);
7272
        $this->stdTot->vIPI = round($this->stdTot->vIPI, 2);
7273
        $this->stdTot->vIPIDevol = round($this->stdTot->vIPIDevol, 2);
7274
        $this->stdTot->vPIS = round($this->stdTot->vPIS, 2);
7275
        $this->stdTot->vCOFINS = round($this->stdTot->vCOFINS, 2);
7276
        $this->stdTot->vOutro = round($this->stdTot->vOutro, 2);
7277
        $this->stdTot->vNF = round($this->stdTot->vNF, 2);
7278
        $this->stdTot->vTotTrib = round($this->stdTot->vTotTrib, 2);
7279
    }
7280
7281
    /**
7282
     * Grupo Cobrança Y01 pai A01
7283
     * tag NFe/infNFe/cobr (opcional)
7284
     * Depende de fat
7285
     */
7286
    protected function buildCobr()
7287
    {
7288
        if (empty($this->cobr)) {
7289
            $this->cobr = $this->dom->createElement("cobr");
7290
        }
7291
    }
7292
7293
    /**
7294
     * Grupo de Informações Adicionais Z01 pai A01
7295
     * tag NFe/infNFe/infAdic (opcional)
7296
     * Função chamada pelos metodos
7297
     * [taginfAdic] [tagobsCont] [tagobsFisco] [tagprocRef]
7298
     * @return DOMElement
7299
     */
7300
    protected function buildInfAdic()
7301
    {
7302
        if (empty($this->infAdic)) {
7303
            $this->infAdic = $this->dom->createElement("infAdic");
7304
        }
7305
        return $this->infAdic;
7306
    }
7307
7308
    /**
7309
     * Remonta a chave da NFe de 44 digitos com base em seus dados
7310
     * já contidos na NFE.
7311
     * Isso é útil no caso da chave informada estar errada
7312
     * se a chave estiver errada a mesma é substituida
7313
     * @param Dom $dom
7314
     * @return void
7315
     */
7316
    protected function checkNFeKey(Dom $dom)
7317
    {
7318
        $infNFe = $dom->getElementsByTagName("infNFe")->item(0);
7319
        $ide = $dom->getElementsByTagName("ide")->item(0);
7320
        $emit = $dom->getElementsByTagName("emit")->item(0);
7321
        $cUF = $ide->getElementsByTagName('cUF')->item(0)->nodeValue;
7322
        $dhEmi = $ide->getElementsByTagName('dhEmi')->item(0)->nodeValue;
7323
        if (!empty($emit->getElementsByTagName('CNPJ')->item(0)->nodeValue)) {
7324
            $doc = $emit->getElementsByTagName('CNPJ')->item(0)->nodeValue;
7325
        } else {
7326
            $doc = $emit->getElementsByTagName('CPF')->item(0)->nodeValue;
7327
        }
7328
        $mod = $ide->getElementsByTagName('mod')->item(0)->nodeValue;
7329
        $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue;
7330
        $nNF = $ide->getElementsByTagName('nNF')->item(0)->nodeValue;
7331
        $tpEmis = $ide->getElementsByTagName('tpEmis')->item(0)->nodeValue;
7332
        $cNF = $ide->getElementsByTagName('cNF')->item(0)->nodeValue;
7333
        $chave = str_replace('NFe', '', $infNFe->getAttribute("Id"));
7334
        $dt = new DateTime($dhEmi);
7335
        $infRespTec = $dom->getElementsByTagName("infRespTec")->item(0);
7336
        $chaveMontada = Keys::build(
7337
            $cUF,
7338
            $dt->format('y'),
7339
            $dt->format('m'),
7340
            $doc,
7341
            $mod,
7342
            $serie,
7343
            $nNF,
7344
            $tpEmis,
7345
            $cNF
7346
        );
7347
        if (empty($chave)) {
7348
            //chave não foi passada por parâmetro então colocar a chavemontada
7349
            $infNFe->setAttribute('Id', "NFe$chaveMontada");
7350
            $chave = $chaveMontada;
7351
            $this->chNFe = $chaveMontada;
7352
            $ide->getElementsByTagName('cDV')->item(0)->nodeValue = substr($chave, -1);
7353
            //trocar também o hash se o CSRT for passado
7354
            if (!empty($this->csrt)) {
7355
                $hashCSRT = $this->hashCSRT($this->csrt);
7356
                $infRespTec->getElementsByTagName("hashCSRT")
7357
                    ->item(0)->nodeValue = $hashCSRT;
7358
            }
7359
        }
7360
        //caso a chave contida na NFe esteja errada
7361
        //substituir a chave
7362
        if ($chaveMontada != $chave) {
7363
            $this->chNFe = $chaveMontada;
7364
            $this->errors[] = "A chave informada está incorreta [$chave] => [correto: $chaveMontada].";
7365
        }
7366
    }
7367
    
7368
    /**
7369
     * Retorna os erros detectados
7370
     * @return array
7371
     */
7372
    public function getErrors()
7373
    {
7374
        return $this->errors;
7375
    }
7376
7377
    /**
7378
     * Includes missing or unsupported properties in stdClass
7379
     * Replace all unsuported chars
7380
     * @param stdClass $std
7381
     * @param array $possible
7382
     * @return stdClass
7383
     */
7384 7
    protected function equilizeParameters(stdClass $std, $possible)
7385
    {
7386 7
        $arr = get_object_vars($std);
7387 7
        foreach ($possible as $key) {
7388 7
            if (!array_key_exists($key, $arr)) {
7389 6
                $std->$key = null;
7390
            } else {
7391 7
                if (is_string($std->$key)) {
7392 7
                    $std->$key = trim(Strings::replaceUnacceptableCharacters($std->$key));
7393 7
                    if ($this->replaceAccentedChars) {
7394 7
                        $std->$key = Strings::toASCII($std->$key);
7395
                    }
7396
                }
7397
            }
7398
        }
7399 7
        return $std;
7400
    }
7401
7402
    /**
7403
     * Calcula hash sha1 retornando Base64Binary
7404
     * @param string $CSRT
7405
     * @return string
7406
     */
7407
    protected function hashCSRT($CSRT)
7408
    {
7409
        $comb = $CSRT . $this->chNFe;
7410
        return base64_encode(sha1($comb, true));
7411
    }
7412
    
7413
    protected function conditionalNumberFormatting($value = null, $decimal = 2)
7414
    {
7415
        if (is_numeric($value)) {
7416
            return number_format($value, $decimal, '.', '');
7417
        }
7418
        return null;
7419
    }
7420
}
7421