ghinfe 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +24 -15
- package/LICENSE +21 -21
- package/README.md +491 -413
- package/docs/developer-guide.html +421 -421
- package/docs/openapi.yaml +428 -428
- package/examples/dce-homologacao.js +141 -0
- package/examples/emitir-nfce.js +51 -51
- package/examples/emitir-nfe.js +83 -83
- package/examples/emitir-nfse.js +46 -46
- package/examples/endpoints-sefaz.js +14 -14
- package/examples/eventos-nfe.js +49 -49
- package/examples/gerar-chave.js +25 -25
- package/examples/gerar-dace.js +59 -0
- package/examples/gerar-danfe.js +75 -75
- package/examples/gerar-dce.js +58 -0
- package/examples/layouts-nfse.js +51 -51
- package/examples/mock-sefaz.js +52 -52
- package/examples/nfse-eventos.js +57 -57
- package/examples/reforma-ibscbs.js +27 -27
- package/examples/reforma-nfe-xml.js +52 -52
- package/examples/status-sefaz.js +12 -12
- package/examples/validar-xsd.js +48 -48
- package/package.json +59 -68
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/consSitDCeTiposBasico_v1.00.xsd +123 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/consSitDCe_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/consStatServDCeTiposBasico_v1.00.xsd +110 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/consStatServDCe_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/dceTiposBasico_v1.00.xsd +1388 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/retConsSitDCe_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/retConsStatServDCe_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/tiposGeralDCe_v1.00.xsd +712 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/consulta/xmldsig-core-schema_v1.01.xsd +98 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/dceTiposBasico_v1.00.xsd +1410 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/dce_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/evCancDCe_v1.00.xsd +36 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/eventoDCeTiposBasico_v1.00.xsd +283 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/eventoDCe_v1.00.xsd +9 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/procEventoDCe_v1.00.xsd +10 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/retEventoDCe_v1.00.xsd +9 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/tiposGeralDCe_v1.00.xsd +712 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/evento/xmldsig-core-schema_v1.01.xsd +98 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/procDCe_v1.00.xsd +37 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/retDCe_v1.00.xsd +9 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/tiposGeralDCe_v1.00.xsd +712 -0
- package/schemas/dce/PL_DCe_100a_NT2024.001/xmldsig-core-schema_v1.01.xsd +98 -0
- package/schemas/nfe/PL_009_V4/enviNFe_v3.10.xsd +8 -8
- package/schemas/nfe/PL_009_V4/xmldsig-core-schema_v1.01.xsd +97 -97
- package/scripts/download-schemas.js +39 -39
- package/scripts/test-xsd.js +34 -34
- package/src/api/server.js +316 -316
- package/src/constants/sefaz.js +69 -69
- package/src/danfe/DanfeService.js +205 -205
- package/src/danfe/xml-extract.js +69 -69
- package/src/dce/builders/DCeEventoXmlBuilder.js +86 -0
- package/src/dce/builders/DCeXmlBuilder.js +345 -0
- package/src/dce/constants/dce.js +87 -0
- package/src/dce/dace/DaceService.js +356 -0
- package/src/dce/dace/dace-extract.js +114 -0
- package/src/dce/services/DCeService.js +263 -0
- package/src/dce/services/DCeSoapService.js +235 -0
- package/src/dce/services/DCeXmlService.js +67 -0
- package/src/dce/types/DCeDocument.js +106 -0
- package/src/dce/utils/qrcode.utils.js +59 -0
- package/src/errors/GhinfeError.js +13 -13
- package/src/errors/SefazError.js +25 -25
- package/src/errors/XsdValidationError.js +15 -15
- package/src/index.js +84 -60
- package/src/nfce/builders/NFCeXmlBuilder.js +178 -178
- package/src/nfce/constants/qrcode.js +26 -26
- package/src/nfce/services/NFCeService.js +188 -188
- package/src/nfce/services/NFCeXmlService.js +38 -38
- package/src/nfce/types/NFCeDocument.js +42 -42
- package/src/nfce/utils/qrcode.utils.js +92 -92
- package/src/nfe/builders/NFeEventoXmlBuilder.js +110 -110
- package/src/nfe/builders/NFeXmlBuilder.js +215 -215
- package/src/nfe/mocks/sefaz-responses.js +195 -195
- package/src/nfe/services/NFeEventoService.js +95 -95
- package/src/nfe/services/NFeService.js +196 -196
- package/src/nfe/services/NFeSignService.js +127 -111
- package/src/nfe/services/NFeSoapService.js +437 -437
- package/src/nfe/services/NFeXmlService.js +41 -41
- package/src/nfe/services/NFeXsdService.js +56 -56
- package/src/nfe/types/NFeDocument.js +92 -92
- package/src/nfe/types/NFeEvento.js +44 -44
- package/src/nfse/NFSeMunicipioResolver.js +42 -42
- package/src/nfse/builders/NFSeEventoXmlBuilder.js +92 -92
- package/src/nfse/builders/NFSeXmlBuilder.js +16 -16
- package/src/nfse/constants/municipios.js +82 -82
- package/src/nfse/layouts/adapters.js +202 -202
- package/src/nfse/mocks/nfse-responses.js +142 -142
- package/src/nfse/services/NFSeService.js +191 -191
- package/src/nfse/services/NFSeSoapService.js +232 -232
- package/src/nfse/services/NFSeXmlService.js +12 -12
- package/src/nfse/types/NFSeDocument.js +54 -54
- package/src/queue/NFCeQueue.js +63 -63
- package/src/queue/NFSeQueue.js +59 -59
- package/src/queue/NFeQueue.js +76 -76
- package/src/reforma/ibscbs.js +175 -175
- package/src/sefaz/SefazEndpointResolver.js +95 -95
- package/src/sefaz/ca/icp-brasil-raiz-v10.pem +38 -0
- package/src/sefaz/endpoints.registry.js +205 -205
- package/src/sefaz/httpsAgent.js +33 -0
- package/src/shared/fiscal.helpers.js +53 -53
- package/src/utils/certificate.utils.js +69 -47
- package/src/utils/chave.utils.js +205 -117
- package/src/utils/code128.utils.js +67 -0
- package/src/utils/date.utils.js +26 -26
- package/src/utils/xml.utils.js +44 -44
- package/src/validation/XsdValidator.js +133 -114
- package/src/validation/schemas.js +18 -14
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
import { create } from 'xmlbuilder2';
|
|
2
|
-
import { NFE_VERSAO } from '../../constants/sefaz.js';
|
|
3
|
-
import { formatSefazDateTime } from '../../utils/date.utils.js';
|
|
4
|
-
import { GhinfeError } from '../../errors/GhinfeError.js';
|
|
5
|
-
import {
|
|
6
|
-
reformaHabilitada,
|
|
7
|
-
normalizarItensReforma,
|
|
8
|
-
somarTotaisReforma,
|
|
9
|
-
appendTotaisIBSCBS,
|
|
10
|
-
appendIBSCBS,
|
|
11
|
-
} from '../../reforma/ibscbs.js';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Monta o XML da NF-e (layout 4.00).
|
|
15
|
-
* Com item.reforma ou reformaTributaria, injeta grupos IBS/CBS (PL_010 experimental).
|
|
16
|
-
* Atenção: validar XSD PL_009 com reforma ativa pode falhar — use validarXsd só sem reforma,
|
|
17
|
-
* ou aguarde schemas PL_010.
|
|
18
|
-
*/
|
|
19
|
-
export class NFeXmlBuilder {
|
|
20
|
-
/**
|
|
21
|
-
* @param {import('../types/NFeDocument.js').NFeDocument} documento
|
|
22
|
-
* @returns {string}
|
|
23
|
-
*/
|
|
24
|
-
build(documento) {
|
|
25
|
-
this.#validar(documento);
|
|
26
|
-
|
|
27
|
-
const comReforma = reformaHabilitada(documento);
|
|
28
|
-
const itens = comReforma ? normalizarItensReforma(documento.itens, true) : documento.itens;
|
|
29
|
-
|
|
30
|
-
const dhEmi = formatSefazDateTime();
|
|
31
|
-
const dhSaiEnt = formatSefazDateTime();
|
|
32
|
-
const idInfNFe = `NFe${documento.chaveAcesso}`;
|
|
33
|
-
|
|
34
|
-
const totalProdutos = itens.reduce((s, i) => s + i.valorTotal, 0);
|
|
35
|
-
const valorFrete = documento.frete?.valor ?? 0;
|
|
36
|
-
const valorIcms = itens.reduce((s, i) => s + (i.valorIcms ?? 0), 0);
|
|
37
|
-
const valorNf = totalProdutos + valorFrete;
|
|
38
|
-
const totaisReforma = comReforma ? somarTotaisReforma(itens) : null;
|
|
39
|
-
|
|
40
|
-
const root = create({ version: '1.0', encoding: 'UTF-8' })
|
|
41
|
-
.ele('NFe', { xmlns: 'http://www.portalfiscal.inf.br/nfe' })
|
|
42
|
-
.ele('infNFe', { versao: NFE_VERSAO, Id: idInfNFe });
|
|
43
|
-
|
|
44
|
-
const inf = root;
|
|
45
|
-
|
|
46
|
-
inf
|
|
47
|
-
.ele('ide')
|
|
48
|
-
.ele('cUF').txt(this.#codigoUf(documento.ide.uf)).up()
|
|
49
|
-
.ele('cNF').txt(documento.chaveAcesso.substring(35, 43)).up()
|
|
50
|
-
.ele('natOp').txt(documento.ide.naturezaOperacao ?? 'VENDA').up()
|
|
51
|
-
.ele('mod').txt('55').up()
|
|
52
|
-
.ele('serie').txt(String(documento.ide.serie ?? 1)).up()
|
|
53
|
-
.ele('nNF').txt(String(documento.ide.numero)).up()
|
|
54
|
-
.ele('dhEmi').txt(dhEmi).up()
|
|
55
|
-
.ele('dhSaiEnt').txt(dhSaiEnt).up()
|
|
56
|
-
.ele('tpNF').txt('1').up()
|
|
57
|
-
.ele('idDest').txt('1').up()
|
|
58
|
-
.ele('cMunFG').txt(documento.ide.codigoMunicipio).up()
|
|
59
|
-
.ele('tpImp').txt('1').up()
|
|
60
|
-
.ele('tpEmis').txt(String(documento.ide.tipoEmissao ?? 1)).up()
|
|
61
|
-
.ele('cDV').txt(documento.chaveAcesso.slice(-1)).up()
|
|
62
|
-
.ele('tpAmb').txt(String(documento.ambiente ?? 2)).up()
|
|
63
|
-
.ele('finNFe').txt(String(documento.ide.finalidade ?? 1)).up()
|
|
64
|
-
.ele('indFinal').txt(String(documento.ide.consumidorFinal ?? 1)).up()
|
|
65
|
-
.ele('indPres').txt(String(documento.ide.presenca ?? 1)).up()
|
|
66
|
-
.ele('procEmi').txt('0').up()
|
|
67
|
-
.ele('verProc').txt('ghinfe-0.6.0').up()
|
|
68
|
-
.up();
|
|
69
|
-
|
|
70
|
-
const emit = inf.ele('emit');
|
|
71
|
-
emit.ele('CNPJ').txt(documento.emitente.cnpj.replace(/\D/g, '')).up();
|
|
72
|
-
emit.ele('xNome').txt(documento.emitente.razaoSocial).up();
|
|
73
|
-
if (documento.emitente.nomeFantasia) emit.ele('xFant').txt(documento.emitente.nomeFantasia).up();
|
|
74
|
-
this.#appendEndereco(emit, 'enderEmit', documento.emitente.endereco);
|
|
75
|
-
emit.ele('IE').txt(documento.emitente.ie).up();
|
|
76
|
-
emit.ele('CRT').txt(documento.emitente.crt).up();
|
|
77
|
-
emit.up();
|
|
78
|
-
|
|
79
|
-
const dest = inf.ele('dest');
|
|
80
|
-
const docDest = (documento.destinatario.cnpj ?? documento.destinatario.cpf ?? '').replace(/\D/g, '');
|
|
81
|
-
if (docDest.length === 14) dest.ele('CNPJ').txt(docDest).up();
|
|
82
|
-
else if (docDest.length === 11) dest.ele('CPF').txt(docDest).up();
|
|
83
|
-
dest.ele('xNome').txt(documento.destinatario.razaoSocial).up();
|
|
84
|
-
this.#appendEndereco(dest, 'enderDest', documento.destinatario.endereco);
|
|
85
|
-
if (documento.destinatario.indIEDest !== undefined) {
|
|
86
|
-
dest.ele('indIEDest').txt(String(documento.destinatario.indIEDest)).up();
|
|
87
|
-
}
|
|
88
|
-
if (documento.destinatario.ie) dest.ele('IE').txt(documento.destinatario.ie).up();
|
|
89
|
-
dest.up();
|
|
90
|
-
|
|
91
|
-
itens.forEach((item, index) => {
|
|
92
|
-
const det = inf.ele('det', { nItem: String(index + 1) });
|
|
93
|
-
const prod = det.ele('prod');
|
|
94
|
-
prod.ele('cProd').txt(item.codigo).up();
|
|
95
|
-
prod.ele('cEAN').txt(item.ean ?? 'SEM GTIN').up();
|
|
96
|
-
prod.ele('xProd').txt(item.descricao).up();
|
|
97
|
-
prod.ele('NCM').txt(item.ncm).up();
|
|
98
|
-
prod.ele('CFOP').txt(item.cfop).up();
|
|
99
|
-
prod.ele('uCom').txt(item.unidade).up();
|
|
100
|
-
prod.ele('qCom').txt(this.#decimal(item.quantidade, 4)).up();
|
|
101
|
-
prod.ele('vUnCom').txt(this.#decimal(item.valorUnitario, 10)).up();
|
|
102
|
-
prod.ele('vProd').txt(this.#decimal(item.valorTotal, 2)).up();
|
|
103
|
-
prod.ele('cEANTrib').txt(item.ean ?? 'SEM GTIN').up();
|
|
104
|
-
prod.ele('uTrib').txt(item.unidade).up();
|
|
105
|
-
prod.ele('qTrib').txt(this.#decimal(item.quantidade, 4)).up();
|
|
106
|
-
prod.ele('vUnTrib').txt(this.#decimal(item.valorUnitario, 10)).up();
|
|
107
|
-
prod.ele('indTot').txt('1').up();
|
|
108
|
-
prod.up();
|
|
109
|
-
|
|
110
|
-
const imposto = det.ele('imposto');
|
|
111
|
-
const icms = imposto.ele('ICMS').ele('ICMS00');
|
|
112
|
-
icms.ele('orig').txt('0').up();
|
|
113
|
-
icms.ele('CST').txt(item.cst ?? '00').up();
|
|
114
|
-
icms.ele('modBC').txt('0').up();
|
|
115
|
-
icms.ele('vBC').txt(this.#decimal(item.valorTotal, 2)).up();
|
|
116
|
-
icms.ele('pICMS').txt(this.#decimal(item.aliquotaIcms ?? 0, 4)).up();
|
|
117
|
-
icms.ele('vICMS').txt(this.#decimal(item.valorIcms ?? 0, 2)).up();
|
|
118
|
-
icms.up().up();
|
|
119
|
-
imposto.ele('PIS').ele('PISNT').ele('CST').txt('07').up().up().up();
|
|
120
|
-
imposto.ele('COFINS').ele('COFINSNT').ele('CST').txt('07').up().up().up();
|
|
121
|
-
if (item.reforma) appendIBSCBS(imposto, item.reforma);
|
|
122
|
-
imposto.up();
|
|
123
|
-
det.up();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const totalParent = inf.ele('total');
|
|
127
|
-
const total = totalParent.ele('ICMSTot');
|
|
128
|
-
total
|
|
129
|
-
.ele('vBC').txt(this.#decimal(totalProdutos, 2)).up()
|
|
130
|
-
.ele('vICMS').txt(this.#decimal(valorIcms, 2)).up()
|
|
131
|
-
.ele('vICMSDeson').txt('0.00').up()
|
|
132
|
-
.ele('vFCP').txt('0.00').up()
|
|
133
|
-
.ele('vBCST').txt('0.00').up()
|
|
134
|
-
.ele('vST').txt('0.00').up()
|
|
135
|
-
.ele('vFCPST').txt('0.00').up()
|
|
136
|
-
.ele('vFCPSTRet').txt('0.00').up()
|
|
137
|
-
.ele('vProd').txt(this.#decimal(totalProdutos, 2)).up()
|
|
138
|
-
.ele('vFrete').txt(this.#decimal(valorFrete, 2)).up()
|
|
139
|
-
.ele('vSeg').txt('0.00').up()
|
|
140
|
-
.ele('vDesc').txt('0.00').up()
|
|
141
|
-
.ele('vII').txt('0.00').up()
|
|
142
|
-
.ele('vIPI').txt('0.00').up()
|
|
143
|
-
.ele('vIPIDevol').txt('0.00').up()
|
|
144
|
-
.ele('vPIS').txt('0.00').up()
|
|
145
|
-
.ele('vCOFINS').txt('0.00').up()
|
|
146
|
-
.ele('vOutro').txt('0.00').up()
|
|
147
|
-
.ele('vNF').txt(this.#decimal(valorNf, 2)).up();
|
|
148
|
-
total.up();
|
|
149
|
-
if (totaisReforma) appendTotaisIBSCBS(totalParent, totaisReforma);
|
|
150
|
-
totalParent.up();
|
|
151
|
-
|
|
152
|
-
const transp = inf.ele('transp');
|
|
153
|
-
transp.ele('modFrete').txt(String(documento.frete?.modalidade ?? 9)).up();
|
|
154
|
-
if (documento.frete?.transportadora) {
|
|
155
|
-
const transporta = transp.ele('transporta');
|
|
156
|
-
if (documento.frete.cnpjTransportadora) {
|
|
157
|
-
transporta.ele('CNPJ').txt(documento.frete.cnpjTransportadora.replace(/\D/g, '')).up();
|
|
158
|
-
}
|
|
159
|
-
transporta.ele('xNome').txt(documento.frete.transportadora).up();
|
|
160
|
-
transporta.up();
|
|
161
|
-
}
|
|
162
|
-
transp.up();
|
|
163
|
-
|
|
164
|
-
const pag = inf.ele('pag');
|
|
165
|
-
(documento.pagamentos ?? [{ forma: '01', valor: valorNf }]).forEach((p) => {
|
|
166
|
-
const detPag = pag.ele('detPag');
|
|
167
|
-
detPag.ele('tPag').txt(p.forma).up();
|
|
168
|
-
detPag.ele('vPag').txt(this.#decimal(p.valor, 2)).up();
|
|
169
|
-
detPag.up();
|
|
170
|
-
});
|
|
171
|
-
pag.up();
|
|
172
|
-
|
|
173
|
-
if (documento.informacoesComplementares) {
|
|
174
|
-
inf.ele('infAdic').ele('infCpl').txt(documento.informacoesComplementares).up().up();
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
return root.end({ prettyPrint: false, headless: false });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
#validar(documento) {
|
|
181
|
-
if (!documento.chaveAcesso || documento.chaveAcesso.length !== 44) {
|
|
182
|
-
throw new GhinfeError('chaveAcesso deve conter 44 dígitos', 'INVALID_CHAVE');
|
|
183
|
-
}
|
|
184
|
-
if (!documento.emitente?.cnpj) throw new GhinfeError('emitente.cnpj é obrigatório', 'INVALID_EMITENTE');
|
|
185
|
-
if (!documento.itens?.length) throw new GhinfeError('A NF-e deve conter ao menos um item', 'INVALID_ITENS');
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
#appendEndereco(parent, tag, endereco) {
|
|
189
|
-
const node = parent.ele(tag);
|
|
190
|
-
node.ele('xLgr').txt(endereco.logradouro).up();
|
|
191
|
-
node.ele('nro').txt(endereco.numero).up();
|
|
192
|
-
if (endereco.complemento) node.ele('xCpl').txt(endereco.complemento).up();
|
|
193
|
-
node.ele('xBairro').txt(endereco.bairro).up();
|
|
194
|
-
node.ele('cMun').txt(endereco.codigoMunicipio).up();
|
|
195
|
-
node.ele('xMun').txt(endereco.municipio).up();
|
|
196
|
-
node.ele('UF').txt(endereco.uf).up();
|
|
197
|
-
node.ele('CEP').txt(endereco.cep.replace(/\D/g, '')).up();
|
|
198
|
-
if (endereco.telefone) node.ele('fone').txt(endereco.telefone.replace(/\D/g, '')).up();
|
|
199
|
-
node.up();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
#codigoUf(uf) {
|
|
203
|
-
const map = {
|
|
204
|
-
AC: '12', AL: '27', AM: '13', AP: '16', BA: '29', CE: '23', DF: '53',
|
|
205
|
-
ES: '32', GO: '52', MA: '21', MG: '31', MS: '50', MT: '51', PA: '15',
|
|
206
|
-
PB: '25', PE: '26', PI: '22', PR: '41', RJ: '33', RN: '24', RO: '11',
|
|
207
|
-
RR: '14', RS: '43', SC: '42', SE: '28', SP: '35', TO: '17',
|
|
208
|
-
};
|
|
209
|
-
return map[uf.toUpperCase()] ?? '35';
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
#decimal(value, decimals) {
|
|
213
|
-
return Number(value).toFixed(decimals);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
1
|
+
import { create } from 'xmlbuilder2';
|
|
2
|
+
import { NFE_VERSAO } from '../../constants/sefaz.js';
|
|
3
|
+
import { formatSefazDateTime } from '../../utils/date.utils.js';
|
|
4
|
+
import { GhinfeError } from '../../errors/GhinfeError.js';
|
|
5
|
+
import {
|
|
6
|
+
reformaHabilitada,
|
|
7
|
+
normalizarItensReforma,
|
|
8
|
+
somarTotaisReforma,
|
|
9
|
+
appendTotaisIBSCBS,
|
|
10
|
+
appendIBSCBS,
|
|
11
|
+
} from '../../reforma/ibscbs.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Monta o XML da NF-e (layout 4.00).
|
|
15
|
+
* Com item.reforma ou reformaTributaria, injeta grupos IBS/CBS (PL_010 experimental).
|
|
16
|
+
* Atenção: validar XSD PL_009 com reforma ativa pode falhar — use validarXsd só sem reforma,
|
|
17
|
+
* ou aguarde schemas PL_010.
|
|
18
|
+
*/
|
|
19
|
+
export class NFeXmlBuilder {
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('../types/NFeDocument.js').NFeDocument} documento
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
build(documento) {
|
|
25
|
+
this.#validar(documento);
|
|
26
|
+
|
|
27
|
+
const comReforma = reformaHabilitada(documento);
|
|
28
|
+
const itens = comReforma ? normalizarItensReforma(documento.itens, true) : documento.itens;
|
|
29
|
+
|
|
30
|
+
const dhEmi = formatSefazDateTime();
|
|
31
|
+
const dhSaiEnt = formatSefazDateTime();
|
|
32
|
+
const idInfNFe = `NFe${documento.chaveAcesso}`;
|
|
33
|
+
|
|
34
|
+
const totalProdutos = itens.reduce((s, i) => s + i.valorTotal, 0);
|
|
35
|
+
const valorFrete = documento.frete?.valor ?? 0;
|
|
36
|
+
const valorIcms = itens.reduce((s, i) => s + (i.valorIcms ?? 0), 0);
|
|
37
|
+
const valorNf = totalProdutos + valorFrete;
|
|
38
|
+
const totaisReforma = comReforma ? somarTotaisReforma(itens) : null;
|
|
39
|
+
|
|
40
|
+
const root = create({ version: '1.0', encoding: 'UTF-8' })
|
|
41
|
+
.ele('NFe', { xmlns: 'http://www.portalfiscal.inf.br/nfe' })
|
|
42
|
+
.ele('infNFe', { versao: NFE_VERSAO, Id: idInfNFe });
|
|
43
|
+
|
|
44
|
+
const inf = root;
|
|
45
|
+
|
|
46
|
+
inf
|
|
47
|
+
.ele('ide')
|
|
48
|
+
.ele('cUF').txt(this.#codigoUf(documento.ide.uf)).up()
|
|
49
|
+
.ele('cNF').txt(documento.chaveAcesso.substring(35, 43)).up()
|
|
50
|
+
.ele('natOp').txt(documento.ide.naturezaOperacao ?? 'VENDA').up()
|
|
51
|
+
.ele('mod').txt('55').up()
|
|
52
|
+
.ele('serie').txt(String(documento.ide.serie ?? 1)).up()
|
|
53
|
+
.ele('nNF').txt(String(documento.ide.numero)).up()
|
|
54
|
+
.ele('dhEmi').txt(dhEmi).up()
|
|
55
|
+
.ele('dhSaiEnt').txt(dhSaiEnt).up()
|
|
56
|
+
.ele('tpNF').txt('1').up()
|
|
57
|
+
.ele('idDest').txt('1').up()
|
|
58
|
+
.ele('cMunFG').txt(documento.ide.codigoMunicipio).up()
|
|
59
|
+
.ele('tpImp').txt('1').up()
|
|
60
|
+
.ele('tpEmis').txt(String(documento.ide.tipoEmissao ?? 1)).up()
|
|
61
|
+
.ele('cDV').txt(documento.chaveAcesso.slice(-1)).up()
|
|
62
|
+
.ele('tpAmb').txt(String(documento.ambiente ?? 2)).up()
|
|
63
|
+
.ele('finNFe').txt(String(documento.ide.finalidade ?? 1)).up()
|
|
64
|
+
.ele('indFinal').txt(String(documento.ide.consumidorFinal ?? 1)).up()
|
|
65
|
+
.ele('indPres').txt(String(documento.ide.presenca ?? 1)).up()
|
|
66
|
+
.ele('procEmi').txt('0').up()
|
|
67
|
+
.ele('verProc').txt('ghinfe-0.6.0').up()
|
|
68
|
+
.up();
|
|
69
|
+
|
|
70
|
+
const emit = inf.ele('emit');
|
|
71
|
+
emit.ele('CNPJ').txt(documento.emitente.cnpj.replace(/\D/g, '')).up();
|
|
72
|
+
emit.ele('xNome').txt(documento.emitente.razaoSocial).up();
|
|
73
|
+
if (documento.emitente.nomeFantasia) emit.ele('xFant').txt(documento.emitente.nomeFantasia).up();
|
|
74
|
+
this.#appendEndereco(emit, 'enderEmit', documento.emitente.endereco);
|
|
75
|
+
emit.ele('IE').txt(documento.emitente.ie).up();
|
|
76
|
+
emit.ele('CRT').txt(documento.emitente.crt).up();
|
|
77
|
+
emit.up();
|
|
78
|
+
|
|
79
|
+
const dest = inf.ele('dest');
|
|
80
|
+
const docDest = (documento.destinatario.cnpj ?? documento.destinatario.cpf ?? '').replace(/\D/g, '');
|
|
81
|
+
if (docDest.length === 14) dest.ele('CNPJ').txt(docDest).up();
|
|
82
|
+
else if (docDest.length === 11) dest.ele('CPF').txt(docDest).up();
|
|
83
|
+
dest.ele('xNome').txt(documento.destinatario.razaoSocial).up();
|
|
84
|
+
this.#appendEndereco(dest, 'enderDest', documento.destinatario.endereco);
|
|
85
|
+
if (documento.destinatario.indIEDest !== undefined) {
|
|
86
|
+
dest.ele('indIEDest').txt(String(documento.destinatario.indIEDest)).up();
|
|
87
|
+
}
|
|
88
|
+
if (documento.destinatario.ie) dest.ele('IE').txt(documento.destinatario.ie).up();
|
|
89
|
+
dest.up();
|
|
90
|
+
|
|
91
|
+
itens.forEach((item, index) => {
|
|
92
|
+
const det = inf.ele('det', { nItem: String(index + 1) });
|
|
93
|
+
const prod = det.ele('prod');
|
|
94
|
+
prod.ele('cProd').txt(item.codigo).up();
|
|
95
|
+
prod.ele('cEAN').txt(item.ean ?? 'SEM GTIN').up();
|
|
96
|
+
prod.ele('xProd').txt(item.descricao).up();
|
|
97
|
+
prod.ele('NCM').txt(item.ncm).up();
|
|
98
|
+
prod.ele('CFOP').txt(item.cfop).up();
|
|
99
|
+
prod.ele('uCom').txt(item.unidade).up();
|
|
100
|
+
prod.ele('qCom').txt(this.#decimal(item.quantidade, 4)).up();
|
|
101
|
+
prod.ele('vUnCom').txt(this.#decimal(item.valorUnitario, 10)).up();
|
|
102
|
+
prod.ele('vProd').txt(this.#decimal(item.valorTotal, 2)).up();
|
|
103
|
+
prod.ele('cEANTrib').txt(item.ean ?? 'SEM GTIN').up();
|
|
104
|
+
prod.ele('uTrib').txt(item.unidade).up();
|
|
105
|
+
prod.ele('qTrib').txt(this.#decimal(item.quantidade, 4)).up();
|
|
106
|
+
prod.ele('vUnTrib').txt(this.#decimal(item.valorUnitario, 10)).up();
|
|
107
|
+
prod.ele('indTot').txt('1').up();
|
|
108
|
+
prod.up();
|
|
109
|
+
|
|
110
|
+
const imposto = det.ele('imposto');
|
|
111
|
+
const icms = imposto.ele('ICMS').ele('ICMS00');
|
|
112
|
+
icms.ele('orig').txt('0').up();
|
|
113
|
+
icms.ele('CST').txt(item.cst ?? '00').up();
|
|
114
|
+
icms.ele('modBC').txt('0').up();
|
|
115
|
+
icms.ele('vBC').txt(this.#decimal(item.valorTotal, 2)).up();
|
|
116
|
+
icms.ele('pICMS').txt(this.#decimal(item.aliquotaIcms ?? 0, 4)).up();
|
|
117
|
+
icms.ele('vICMS').txt(this.#decimal(item.valorIcms ?? 0, 2)).up();
|
|
118
|
+
icms.up().up();
|
|
119
|
+
imposto.ele('PIS').ele('PISNT').ele('CST').txt('07').up().up().up();
|
|
120
|
+
imposto.ele('COFINS').ele('COFINSNT').ele('CST').txt('07').up().up().up();
|
|
121
|
+
if (item.reforma) appendIBSCBS(imposto, item.reforma);
|
|
122
|
+
imposto.up();
|
|
123
|
+
det.up();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const totalParent = inf.ele('total');
|
|
127
|
+
const total = totalParent.ele('ICMSTot');
|
|
128
|
+
total
|
|
129
|
+
.ele('vBC').txt(this.#decimal(totalProdutos, 2)).up()
|
|
130
|
+
.ele('vICMS').txt(this.#decimal(valorIcms, 2)).up()
|
|
131
|
+
.ele('vICMSDeson').txt('0.00').up()
|
|
132
|
+
.ele('vFCP').txt('0.00').up()
|
|
133
|
+
.ele('vBCST').txt('0.00').up()
|
|
134
|
+
.ele('vST').txt('0.00').up()
|
|
135
|
+
.ele('vFCPST').txt('0.00').up()
|
|
136
|
+
.ele('vFCPSTRet').txt('0.00').up()
|
|
137
|
+
.ele('vProd').txt(this.#decimal(totalProdutos, 2)).up()
|
|
138
|
+
.ele('vFrete').txt(this.#decimal(valorFrete, 2)).up()
|
|
139
|
+
.ele('vSeg').txt('0.00').up()
|
|
140
|
+
.ele('vDesc').txt('0.00').up()
|
|
141
|
+
.ele('vII').txt('0.00').up()
|
|
142
|
+
.ele('vIPI').txt('0.00').up()
|
|
143
|
+
.ele('vIPIDevol').txt('0.00').up()
|
|
144
|
+
.ele('vPIS').txt('0.00').up()
|
|
145
|
+
.ele('vCOFINS').txt('0.00').up()
|
|
146
|
+
.ele('vOutro').txt('0.00').up()
|
|
147
|
+
.ele('vNF').txt(this.#decimal(valorNf, 2)).up();
|
|
148
|
+
total.up();
|
|
149
|
+
if (totaisReforma) appendTotaisIBSCBS(totalParent, totaisReforma);
|
|
150
|
+
totalParent.up();
|
|
151
|
+
|
|
152
|
+
const transp = inf.ele('transp');
|
|
153
|
+
transp.ele('modFrete').txt(String(documento.frete?.modalidade ?? 9)).up();
|
|
154
|
+
if (documento.frete?.transportadora) {
|
|
155
|
+
const transporta = transp.ele('transporta');
|
|
156
|
+
if (documento.frete.cnpjTransportadora) {
|
|
157
|
+
transporta.ele('CNPJ').txt(documento.frete.cnpjTransportadora.replace(/\D/g, '')).up();
|
|
158
|
+
}
|
|
159
|
+
transporta.ele('xNome').txt(documento.frete.transportadora).up();
|
|
160
|
+
transporta.up();
|
|
161
|
+
}
|
|
162
|
+
transp.up();
|
|
163
|
+
|
|
164
|
+
const pag = inf.ele('pag');
|
|
165
|
+
(documento.pagamentos ?? [{ forma: '01', valor: valorNf }]).forEach((p) => {
|
|
166
|
+
const detPag = pag.ele('detPag');
|
|
167
|
+
detPag.ele('tPag').txt(p.forma).up();
|
|
168
|
+
detPag.ele('vPag').txt(this.#decimal(p.valor, 2)).up();
|
|
169
|
+
detPag.up();
|
|
170
|
+
});
|
|
171
|
+
pag.up();
|
|
172
|
+
|
|
173
|
+
if (documento.informacoesComplementares) {
|
|
174
|
+
inf.ele('infAdic').ele('infCpl').txt(documento.informacoesComplementares).up().up();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return root.end({ prettyPrint: false, headless: false });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#validar(documento) {
|
|
181
|
+
if (!documento.chaveAcesso || documento.chaveAcesso.length !== 44) {
|
|
182
|
+
throw new GhinfeError('chaveAcesso deve conter 44 dígitos', 'INVALID_CHAVE');
|
|
183
|
+
}
|
|
184
|
+
if (!documento.emitente?.cnpj) throw new GhinfeError('emitente.cnpj é obrigatório', 'INVALID_EMITENTE');
|
|
185
|
+
if (!documento.itens?.length) throw new GhinfeError('A NF-e deve conter ao menos um item', 'INVALID_ITENS');
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#appendEndereco(parent, tag, endereco) {
|
|
189
|
+
const node = parent.ele(tag);
|
|
190
|
+
node.ele('xLgr').txt(endereco.logradouro).up();
|
|
191
|
+
node.ele('nro').txt(endereco.numero).up();
|
|
192
|
+
if (endereco.complemento) node.ele('xCpl').txt(endereco.complemento).up();
|
|
193
|
+
node.ele('xBairro').txt(endereco.bairro).up();
|
|
194
|
+
node.ele('cMun').txt(endereco.codigoMunicipio).up();
|
|
195
|
+
node.ele('xMun').txt(endereco.municipio).up();
|
|
196
|
+
node.ele('UF').txt(endereco.uf).up();
|
|
197
|
+
node.ele('CEP').txt(endereco.cep.replace(/\D/g, '')).up();
|
|
198
|
+
if (endereco.telefone) node.ele('fone').txt(endereco.telefone.replace(/\D/g, '')).up();
|
|
199
|
+
node.up();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#codigoUf(uf) {
|
|
203
|
+
const map = {
|
|
204
|
+
AC: '12', AL: '27', AM: '13', AP: '16', BA: '29', CE: '23', DF: '53',
|
|
205
|
+
ES: '32', GO: '52', MA: '21', MG: '31', MS: '50', MT: '51', PA: '15',
|
|
206
|
+
PB: '25', PE: '26', PI: '22', PR: '41', RJ: '33', RN: '24', RO: '11',
|
|
207
|
+
RR: '14', RS: '43', SC: '42', SE: '28', SP: '35', TO: '17',
|
|
208
|
+
};
|
|
209
|
+
return map[uf.toUpperCase()] ?? '35';
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
#decimal(value, decimals) {
|
|
213
|
+
return Number(value).toFixed(decimals);
|
|
214
|
+
}
|
|
215
|
+
}
|