Passed
Push — master ( 28679e...eb8237 )
by Roberto
58s queued 10s
created

Make::buildCOFINSAliq()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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