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,178 +1,178 @@
|
|
|
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 { codigoUf, formatDecimal, appendEndereco, appendImpostosItem } from '../../shared/fiscal.helpers.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Monta XML da NFC-e (modelo 65, layout 4.00).
|
|
9
|
-
*/
|
|
10
|
-
export class NFCeXmlBuilder {
|
|
11
|
-
/**
|
|
12
|
-
* @param {import('../types/NFCeDocument.js').NFCeDocumento} documento
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
build(documento) {
|
|
16
|
-
this.#validar(documento);
|
|
17
|
-
|
|
18
|
-
const dhEmi = formatSefazDateTime();
|
|
19
|
-
const idInfNFe = `NFe${documento.chaveAcesso}`;
|
|
20
|
-
const ambiente = documento.ambiente ?? 2;
|
|
21
|
-
|
|
22
|
-
const totalProdutos = documento.itens.reduce((s, i) => s + i.valorTotal, 0);
|
|
23
|
-
const valorIcms = documento.itens.reduce((s, i) => s + (i.valorIcms ?? 0), 0);
|
|
24
|
-
const valorDesc = documento.itens.reduce((s, i) => s + (i.valorDesconto ?? 0), 0);
|
|
25
|
-
const valorNf = totalProdutos - valorDesc;
|
|
26
|
-
|
|
27
|
-
const root = create({ version: '1.0', encoding: 'UTF-8' })
|
|
28
|
-
.ele('NFe', { xmlns: 'http://www.portalfiscal.inf.br/nfe' })
|
|
29
|
-
.ele('infNFe', { versao: NFE_VERSAO, Id: idInfNFe });
|
|
30
|
-
|
|
31
|
-
const inf = root;
|
|
32
|
-
|
|
33
|
-
// ide — NFC-e modelo 65
|
|
34
|
-
inf
|
|
35
|
-
.ele('ide')
|
|
36
|
-
.ele('cUF').txt(codigoUf(documento.ide.uf)).up()
|
|
37
|
-
.ele('cNF').txt(documento.chaveAcesso.substring(35, 43)).up()
|
|
38
|
-
.ele('natOp').txt(documento.ide.naturezaOperacao ?? 'VENDA').up()
|
|
39
|
-
.ele('mod').txt('65').up()
|
|
40
|
-
.ele('serie').txt(String(documento.ide.serie ?? 1)).up()
|
|
41
|
-
.ele('nNF').txt(String(documento.ide.numero)).up()
|
|
42
|
-
.ele('dhEmi').txt(dhEmi).up()
|
|
43
|
-
.ele('tpNF').txt('1').up()
|
|
44
|
-
.ele('idDest').txt('1').up()
|
|
45
|
-
.ele('cMunFG').txt(documento.ide.codigoMunicipio).up()
|
|
46
|
-
.ele('tpImp').txt('4').up()
|
|
47
|
-
.ele('tpEmis').txt('1').up()
|
|
48
|
-
.ele('cDV').txt(documento.chaveAcesso.slice(-1)).up()
|
|
49
|
-
.ele('tpAmb').txt(String(ambiente)).up()
|
|
50
|
-
.ele('finNFe').txt('1').up()
|
|
51
|
-
.ele('indFinal').txt('1').up()
|
|
52
|
-
.ele('indPres').txt(String(documento.ide.presenca ?? 1)).up()
|
|
53
|
-
.ele('procEmi').txt('0').up()
|
|
54
|
-
.ele('verProc').txt('ghinfe-0.3.0').up()
|
|
55
|
-
.up();
|
|
56
|
-
|
|
57
|
-
// emit
|
|
58
|
-
const emit = inf.ele('emit');
|
|
59
|
-
emit.ele('CNPJ').txt(documento.emitente.cnpj.replace(/\D/g, '')).up();
|
|
60
|
-
emit.ele('xNome').txt(documento.emitente.razaoSocial).up();
|
|
61
|
-
if (documento.emitente.nomeFantasia) {
|
|
62
|
-
emit.ele('xFant').txt(documento.emitente.nomeFantasia).up();
|
|
63
|
-
}
|
|
64
|
-
appendEndereco(emit, 'enderEmit', documento.emitente.endereco);
|
|
65
|
-
emit.ele('IE').txt(documento.emitente.ie).up();
|
|
66
|
-
emit.ele('CRT').txt(documento.emitente.crt).up();
|
|
67
|
-
emit.up();
|
|
68
|
-
|
|
69
|
-
// dest — opcional na NFC-e (se informado CPF/CNPJ, exige indIEDest ou enderDest)
|
|
70
|
-
if (documento.destinatario?.cpf || documento.destinatario?.cnpj) {
|
|
71
|
-
const dest = inf.ele('dest');
|
|
72
|
-
const docDest = (documento.destinatario.cnpj ?? documento.destinatario.cpf ?? '').replace(/\D/g, '');
|
|
73
|
-
if (docDest.length === 14) dest.ele('CNPJ').txt(docDest).up();
|
|
74
|
-
else if (docDest.length === 11) dest.ele('CPF').txt(docDest).up();
|
|
75
|
-
if (documento.destinatario.razaoSocial) {
|
|
76
|
-
dest.ele('xNome').txt(documento.destinatario.razaoSocial).up();
|
|
77
|
-
}
|
|
78
|
-
if (documento.destinatario.endereco) {
|
|
79
|
-
appendEndereco(dest, 'enderDest', documento.destinatario.endereco);
|
|
80
|
-
} else {
|
|
81
|
-
dest.ele('indIEDest').txt('9').up();
|
|
82
|
-
}
|
|
83
|
-
dest.up();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// det — itens
|
|
87
|
-
documento.itens.forEach((item, index) => {
|
|
88
|
-
const det = inf.ele('det', { nItem: String(index + 1) });
|
|
89
|
-
const prod = det.ele('prod');
|
|
90
|
-
prod.ele('cProd').txt(item.codigo).up();
|
|
91
|
-
prod.ele('cEAN').txt(item.ean ?? 'SEM GTIN').up();
|
|
92
|
-
prod.ele('xProd').txt(item.descricao).up();
|
|
93
|
-
prod.ele('NCM').txt(item.ncm).up();
|
|
94
|
-
prod.ele('CFOP').txt(item.cfop).up();
|
|
95
|
-
prod.ele('uCom').txt(item.unidade).up();
|
|
96
|
-
prod.ele('qCom').txt(formatDecimal(item.quantidade, 4)).up();
|
|
97
|
-
prod.ele('vUnCom').txt(formatDecimal(item.valorUnitario, 10)).up();
|
|
98
|
-
prod.ele('vProd').txt(formatDecimal(item.valorTotal, 2)).up();
|
|
99
|
-
prod.ele('cEANTrib').txt(item.ean ?? 'SEM GTIN').up();
|
|
100
|
-
prod.ele('uTrib').txt(item.unidade).up();
|
|
101
|
-
prod.ele('qTrib').txt(formatDecimal(item.quantidade, 4)).up();
|
|
102
|
-
prod.ele('vUnTrib').txt(formatDecimal(item.valorUnitario, 10)).up();
|
|
103
|
-
prod.ele('indTot').txt('1').up();
|
|
104
|
-
prod.up();
|
|
105
|
-
|
|
106
|
-
appendImpostosItem(det.ele('imposto'), item);
|
|
107
|
-
det.up();
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
// total
|
|
111
|
-
const total = inf.ele('total').ele('ICMSTot');
|
|
112
|
-
total
|
|
113
|
-
.ele('vBC').txt(formatDecimal(totalProdutos, 2)).up()
|
|
114
|
-
.ele('vICMS').txt(formatDecimal(valorIcms, 2)).up()
|
|
115
|
-
.ele('vICMSDeson').txt('0.00').up()
|
|
116
|
-
.ele('vFCP').txt('0.00').up()
|
|
117
|
-
.ele('vBCST').txt('0.00').up()
|
|
118
|
-
.ele('vST').txt('0.00').up()
|
|
119
|
-
.ele('vFCPST').txt('0.00').up()
|
|
120
|
-
.ele('vFCPSTRet').txt('0.00').up()
|
|
121
|
-
.ele('vProd').txt(formatDecimal(totalProdutos, 2)).up()
|
|
122
|
-
.ele('vFrete').txt('0.00').up()
|
|
123
|
-
.ele('vSeg').txt('0.00').up()
|
|
124
|
-
.ele('vDesc').txt(formatDecimal(valorDesc, 2)).up()
|
|
125
|
-
.ele('vII').txt('0.00').up()
|
|
126
|
-
.ele('vIPI').txt('0.00').up()
|
|
127
|
-
.ele('vIPIDevol').txt('0.00').up()
|
|
128
|
-
.ele('vPIS').txt('0.00').up()
|
|
129
|
-
.ele('vCOFINS').txt('0.00').up()
|
|
130
|
-
.ele('vOutro').txt('0.00').up()
|
|
131
|
-
.ele('vNF').txt(formatDecimal(valorNf, 2)).up();
|
|
132
|
-
total.up().up();
|
|
133
|
-
|
|
134
|
-
// transp — NFC-e sem frete
|
|
135
|
-
inf.ele('transp').ele('modFrete').txt('9').up().up();
|
|
136
|
-
|
|
137
|
-
// pag
|
|
138
|
-
const pag = inf.ele('pag');
|
|
139
|
-
const pagamentos = documento.pagamentos ?? [{ forma: '01', valor: valorNf }];
|
|
140
|
-
pagamentos.forEach((p) => {
|
|
141
|
-
const detPag = pag.ele('detPag');
|
|
142
|
-
detPag.ele('tPag').txt(p.forma).up();
|
|
143
|
-
detPag.ele('vPag').txt(formatDecimal(p.valor, 2)).up();
|
|
144
|
-
detPag.up();
|
|
145
|
-
});
|
|
146
|
-
pag.up();
|
|
147
|
-
|
|
148
|
-
// infAdic
|
|
149
|
-
if (documento.informacoesComplementares || documento.informacoesFisco) {
|
|
150
|
-
const infAdic = inf.ele('infAdic');
|
|
151
|
-
if (documento.informacoesFisco) {
|
|
152
|
-
infAdic.ele('infAdFisco').txt(documento.informacoesFisco).up();
|
|
153
|
-
}
|
|
154
|
-
if (documento.informacoesComplementares) {
|
|
155
|
-
infAdic.ele('infCpl').txt(documento.informacoesComplementares).up();
|
|
156
|
-
}
|
|
157
|
-
infAdic.up();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return root.end({ prettyPrint: false, headless: false });
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** @param {import('../types/NFCeDocument.js').NFCeDocumento} documento */
|
|
164
|
-
#validar(documento) {
|
|
165
|
-
if (!documento.chaveAcesso || documento.chaveAcesso.length !== 44) {
|
|
166
|
-
throw new GhinfeError('chaveAcesso deve conter 44 dígitos', 'INVALID_CHAVE');
|
|
167
|
-
}
|
|
168
|
-
if (!documento.emitente?.cnpj) {
|
|
169
|
-
throw new GhinfeError('emitente.cnpj é obrigatório', 'INVALID_EMITENTE');
|
|
170
|
-
}
|
|
171
|
-
if (!documento.itens?.length) {
|
|
172
|
-
throw new GhinfeError('A NFC-e deve conter ao menos um item', 'INVALID_ITENS');
|
|
173
|
-
}
|
|
174
|
-
if (!documento.csc?.idCSC || !documento.csc?.codigo) {
|
|
175
|
-
throw new GhinfeError('csc.idCSC e csc.codigo são obrigatórios para QR Code', 'INVALID_CSC');
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
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 { codigoUf, formatDecimal, appendEndereco, appendImpostosItem } from '../../shared/fiscal.helpers.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Monta XML da NFC-e (modelo 65, layout 4.00).
|
|
9
|
+
*/
|
|
10
|
+
export class NFCeXmlBuilder {
|
|
11
|
+
/**
|
|
12
|
+
* @param {import('../types/NFCeDocument.js').NFCeDocumento} documento
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
build(documento) {
|
|
16
|
+
this.#validar(documento);
|
|
17
|
+
|
|
18
|
+
const dhEmi = formatSefazDateTime();
|
|
19
|
+
const idInfNFe = `NFe${documento.chaveAcesso}`;
|
|
20
|
+
const ambiente = documento.ambiente ?? 2;
|
|
21
|
+
|
|
22
|
+
const totalProdutos = documento.itens.reduce((s, i) => s + i.valorTotal, 0);
|
|
23
|
+
const valorIcms = documento.itens.reduce((s, i) => s + (i.valorIcms ?? 0), 0);
|
|
24
|
+
const valorDesc = documento.itens.reduce((s, i) => s + (i.valorDesconto ?? 0), 0);
|
|
25
|
+
const valorNf = totalProdutos - valorDesc;
|
|
26
|
+
|
|
27
|
+
const root = create({ version: '1.0', encoding: 'UTF-8' })
|
|
28
|
+
.ele('NFe', { xmlns: 'http://www.portalfiscal.inf.br/nfe' })
|
|
29
|
+
.ele('infNFe', { versao: NFE_VERSAO, Id: idInfNFe });
|
|
30
|
+
|
|
31
|
+
const inf = root;
|
|
32
|
+
|
|
33
|
+
// ide — NFC-e modelo 65
|
|
34
|
+
inf
|
|
35
|
+
.ele('ide')
|
|
36
|
+
.ele('cUF').txt(codigoUf(documento.ide.uf)).up()
|
|
37
|
+
.ele('cNF').txt(documento.chaveAcesso.substring(35, 43)).up()
|
|
38
|
+
.ele('natOp').txt(documento.ide.naturezaOperacao ?? 'VENDA').up()
|
|
39
|
+
.ele('mod').txt('65').up()
|
|
40
|
+
.ele('serie').txt(String(documento.ide.serie ?? 1)).up()
|
|
41
|
+
.ele('nNF').txt(String(documento.ide.numero)).up()
|
|
42
|
+
.ele('dhEmi').txt(dhEmi).up()
|
|
43
|
+
.ele('tpNF').txt('1').up()
|
|
44
|
+
.ele('idDest').txt('1').up()
|
|
45
|
+
.ele('cMunFG').txt(documento.ide.codigoMunicipio).up()
|
|
46
|
+
.ele('tpImp').txt('4').up()
|
|
47
|
+
.ele('tpEmis').txt('1').up()
|
|
48
|
+
.ele('cDV').txt(documento.chaveAcesso.slice(-1)).up()
|
|
49
|
+
.ele('tpAmb').txt(String(ambiente)).up()
|
|
50
|
+
.ele('finNFe').txt('1').up()
|
|
51
|
+
.ele('indFinal').txt('1').up()
|
|
52
|
+
.ele('indPres').txt(String(documento.ide.presenca ?? 1)).up()
|
|
53
|
+
.ele('procEmi').txt('0').up()
|
|
54
|
+
.ele('verProc').txt('ghinfe-0.3.0').up()
|
|
55
|
+
.up();
|
|
56
|
+
|
|
57
|
+
// emit
|
|
58
|
+
const emit = inf.ele('emit');
|
|
59
|
+
emit.ele('CNPJ').txt(documento.emitente.cnpj.replace(/\D/g, '')).up();
|
|
60
|
+
emit.ele('xNome').txt(documento.emitente.razaoSocial).up();
|
|
61
|
+
if (documento.emitente.nomeFantasia) {
|
|
62
|
+
emit.ele('xFant').txt(documento.emitente.nomeFantasia).up();
|
|
63
|
+
}
|
|
64
|
+
appendEndereco(emit, 'enderEmit', documento.emitente.endereco);
|
|
65
|
+
emit.ele('IE').txt(documento.emitente.ie).up();
|
|
66
|
+
emit.ele('CRT').txt(documento.emitente.crt).up();
|
|
67
|
+
emit.up();
|
|
68
|
+
|
|
69
|
+
// dest — opcional na NFC-e (se informado CPF/CNPJ, exige indIEDest ou enderDest)
|
|
70
|
+
if (documento.destinatario?.cpf || documento.destinatario?.cnpj) {
|
|
71
|
+
const dest = inf.ele('dest');
|
|
72
|
+
const docDest = (documento.destinatario.cnpj ?? documento.destinatario.cpf ?? '').replace(/\D/g, '');
|
|
73
|
+
if (docDest.length === 14) dest.ele('CNPJ').txt(docDest).up();
|
|
74
|
+
else if (docDest.length === 11) dest.ele('CPF').txt(docDest).up();
|
|
75
|
+
if (documento.destinatario.razaoSocial) {
|
|
76
|
+
dest.ele('xNome').txt(documento.destinatario.razaoSocial).up();
|
|
77
|
+
}
|
|
78
|
+
if (documento.destinatario.endereco) {
|
|
79
|
+
appendEndereco(dest, 'enderDest', documento.destinatario.endereco);
|
|
80
|
+
} else {
|
|
81
|
+
dest.ele('indIEDest').txt('9').up();
|
|
82
|
+
}
|
|
83
|
+
dest.up();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// det — itens
|
|
87
|
+
documento.itens.forEach((item, index) => {
|
|
88
|
+
const det = inf.ele('det', { nItem: String(index + 1) });
|
|
89
|
+
const prod = det.ele('prod');
|
|
90
|
+
prod.ele('cProd').txt(item.codigo).up();
|
|
91
|
+
prod.ele('cEAN').txt(item.ean ?? 'SEM GTIN').up();
|
|
92
|
+
prod.ele('xProd').txt(item.descricao).up();
|
|
93
|
+
prod.ele('NCM').txt(item.ncm).up();
|
|
94
|
+
prod.ele('CFOP').txt(item.cfop).up();
|
|
95
|
+
prod.ele('uCom').txt(item.unidade).up();
|
|
96
|
+
prod.ele('qCom').txt(formatDecimal(item.quantidade, 4)).up();
|
|
97
|
+
prod.ele('vUnCom').txt(formatDecimal(item.valorUnitario, 10)).up();
|
|
98
|
+
prod.ele('vProd').txt(formatDecimal(item.valorTotal, 2)).up();
|
|
99
|
+
prod.ele('cEANTrib').txt(item.ean ?? 'SEM GTIN').up();
|
|
100
|
+
prod.ele('uTrib').txt(item.unidade).up();
|
|
101
|
+
prod.ele('qTrib').txt(formatDecimal(item.quantidade, 4)).up();
|
|
102
|
+
prod.ele('vUnTrib').txt(formatDecimal(item.valorUnitario, 10)).up();
|
|
103
|
+
prod.ele('indTot').txt('1').up();
|
|
104
|
+
prod.up();
|
|
105
|
+
|
|
106
|
+
appendImpostosItem(det.ele('imposto'), item);
|
|
107
|
+
det.up();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// total
|
|
111
|
+
const total = inf.ele('total').ele('ICMSTot');
|
|
112
|
+
total
|
|
113
|
+
.ele('vBC').txt(formatDecimal(totalProdutos, 2)).up()
|
|
114
|
+
.ele('vICMS').txt(formatDecimal(valorIcms, 2)).up()
|
|
115
|
+
.ele('vICMSDeson').txt('0.00').up()
|
|
116
|
+
.ele('vFCP').txt('0.00').up()
|
|
117
|
+
.ele('vBCST').txt('0.00').up()
|
|
118
|
+
.ele('vST').txt('0.00').up()
|
|
119
|
+
.ele('vFCPST').txt('0.00').up()
|
|
120
|
+
.ele('vFCPSTRet').txt('0.00').up()
|
|
121
|
+
.ele('vProd').txt(formatDecimal(totalProdutos, 2)).up()
|
|
122
|
+
.ele('vFrete').txt('0.00').up()
|
|
123
|
+
.ele('vSeg').txt('0.00').up()
|
|
124
|
+
.ele('vDesc').txt(formatDecimal(valorDesc, 2)).up()
|
|
125
|
+
.ele('vII').txt('0.00').up()
|
|
126
|
+
.ele('vIPI').txt('0.00').up()
|
|
127
|
+
.ele('vIPIDevol').txt('0.00').up()
|
|
128
|
+
.ele('vPIS').txt('0.00').up()
|
|
129
|
+
.ele('vCOFINS').txt('0.00').up()
|
|
130
|
+
.ele('vOutro').txt('0.00').up()
|
|
131
|
+
.ele('vNF').txt(formatDecimal(valorNf, 2)).up();
|
|
132
|
+
total.up().up();
|
|
133
|
+
|
|
134
|
+
// transp — NFC-e sem frete
|
|
135
|
+
inf.ele('transp').ele('modFrete').txt('9').up().up();
|
|
136
|
+
|
|
137
|
+
// pag
|
|
138
|
+
const pag = inf.ele('pag');
|
|
139
|
+
const pagamentos = documento.pagamentos ?? [{ forma: '01', valor: valorNf }];
|
|
140
|
+
pagamentos.forEach((p) => {
|
|
141
|
+
const detPag = pag.ele('detPag');
|
|
142
|
+
detPag.ele('tPag').txt(p.forma).up();
|
|
143
|
+
detPag.ele('vPag').txt(formatDecimal(p.valor, 2)).up();
|
|
144
|
+
detPag.up();
|
|
145
|
+
});
|
|
146
|
+
pag.up();
|
|
147
|
+
|
|
148
|
+
// infAdic
|
|
149
|
+
if (documento.informacoesComplementares || documento.informacoesFisco) {
|
|
150
|
+
const infAdic = inf.ele('infAdic');
|
|
151
|
+
if (documento.informacoesFisco) {
|
|
152
|
+
infAdic.ele('infAdFisco').txt(documento.informacoesFisco).up();
|
|
153
|
+
}
|
|
154
|
+
if (documento.informacoesComplementares) {
|
|
155
|
+
infAdic.ele('infCpl').txt(documento.informacoesComplementares).up();
|
|
156
|
+
}
|
|
157
|
+
infAdic.up();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return root.end({ prettyPrint: false, headless: false });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** @param {import('../types/NFCeDocument.js').NFCeDocumento} documento */
|
|
164
|
+
#validar(documento) {
|
|
165
|
+
if (!documento.chaveAcesso || documento.chaveAcesso.length !== 44) {
|
|
166
|
+
throw new GhinfeError('chaveAcesso deve conter 44 dígitos', 'INVALID_CHAVE');
|
|
167
|
+
}
|
|
168
|
+
if (!documento.emitente?.cnpj) {
|
|
169
|
+
throw new GhinfeError('emitente.cnpj é obrigatório', 'INVALID_EMITENTE');
|
|
170
|
+
}
|
|
171
|
+
if (!documento.itens?.length) {
|
|
172
|
+
throw new GhinfeError('A NFC-e deve conter ao menos um item', 'INVALID_ITENS');
|
|
173
|
+
}
|
|
174
|
+
if (!documento.csc?.idCSC || !documento.csc?.codigo) {
|
|
175
|
+
throw new GhinfeError('csc.idCSC e csc.codigo são obrigatórios para QR Code', 'INVALID_CSC');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/** URLs de consulta QR Code NFC-e por UF e ambiente. */
|
|
2
|
-
export const NFCE_QRCODE_URLS = {
|
|
3
|
-
SP: {
|
|
4
|
-
1: 'https://www.nfce.fazenda.sp.gov.br/NFCeConsultaPublica/Paginas/ConsultaQRCode.aspx',
|
|
5
|
-
2: 'https://www.homologacao.nfce.fazenda.sp.gov.br/NFCeConsultaPublica/Paginas/ConsultaQRCode.aspx',
|
|
6
|
-
},
|
|
7
|
-
PR: {
|
|
8
|
-
1: 'http://www.fazenda.pr.gov.br/nfce/qrcode',
|
|
9
|
-
2: 'http://www.fazenda.pr.gov.br/nfce/qrcode',
|
|
10
|
-
},
|
|
11
|
-
RS: {
|
|
12
|
-
1: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM-QRCode',
|
|
13
|
-
2: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM-QRCode',
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/** URLs de consulta por chave (portal SEFAZ). */
|
|
18
|
-
export const NFCE_URL_CHAVE = {
|
|
19
|
-
SP: {
|
|
20
|
-
1: 'https://www.nfce.fazenda.sp.gov.br/consulta',
|
|
21
|
-
2: 'https://www.homologacao.nfce.fazenda.sp.gov.br/consulta',
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/** Versão do QR Code NFC-e (2 = atual). */
|
|
26
|
-
export const NFCE_QRCODE_VERSAO = '2';
|
|
1
|
+
/** URLs de consulta QR Code NFC-e por UF e ambiente. */
|
|
2
|
+
export const NFCE_QRCODE_URLS = {
|
|
3
|
+
SP: {
|
|
4
|
+
1: 'https://www.nfce.fazenda.sp.gov.br/NFCeConsultaPublica/Paginas/ConsultaQRCode.aspx',
|
|
5
|
+
2: 'https://www.homologacao.nfce.fazenda.sp.gov.br/NFCeConsultaPublica/Paginas/ConsultaQRCode.aspx',
|
|
6
|
+
},
|
|
7
|
+
PR: {
|
|
8
|
+
1: 'http://www.fazenda.pr.gov.br/nfce/qrcode',
|
|
9
|
+
2: 'http://www.fazenda.pr.gov.br/nfce/qrcode',
|
|
10
|
+
},
|
|
11
|
+
RS: {
|
|
12
|
+
1: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM-QRCode',
|
|
13
|
+
2: 'https://www.sefaz.rs.gov.br/NFCE/NFCE-COM-QRCode',
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** URLs de consulta por chave (portal SEFAZ). */
|
|
18
|
+
export const NFCE_URL_CHAVE = {
|
|
19
|
+
SP: {
|
|
20
|
+
1: 'https://www.nfce.fazenda.sp.gov.br/consulta',
|
|
21
|
+
2: 'https://www.homologacao.nfce.fazenda.sp.gov.br/consulta',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Versão do QR Code NFC-e (2 = atual). */
|
|
26
|
+
export const NFCE_QRCODE_VERSAO = '2';
|