node-sped-nfe 1.0.6 → 1.0.8

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.
@@ -56,4 +56,227 @@ const cUF2UF: any = {
56
56
  "GO": "52",
57
57
  "DF": "53"
58
58
  };
59
+
60
+ //Função auxliar de imposto
61
+ const impEstrutura = (imposto: any) => {
62
+ const gStruct = { //Estrutura global
63
+ ICMS: {
64
+ ICMS_ICMSSN: {
65
+ "@label": "Tributação",
66
+ "@type": "select",
67
+ "@obrig": true,
68
+ "@values": [
69
+ { "102": "Tributação Normal" },
70
+ { "103": "Simples Nacional" }
71
+ ],
72
+ "@next": {
73
+ CST: {
74
+ "@label": "Situação Tributaria",
75
+ "@type": "select",
76
+ "@obrig": true,
77
+ "@values": [
78
+ { "00": "00 - Tributada integralmente" },
79
+ { "10": "10 - Tributada com cobrança do ICMS por ST" },
80
+ { "10v2": "10 - Tributada com cobrança do ICMS por ST(com partilha do ICMS entre UF de origem e a UF de destino ou a UFdefinida na legislação)" },
81
+ { "20": "20 - Com redução de base de cálculo" },
82
+ { "30": "30 - Isenta ou não tributada e com cobrança do ICMS por ST" },
83
+ { "40": "40 - Isenta" },
84
+ { "41": "41 - Não tributada" },
85
+ { "41v2": "41 - Não tributada (ICMSST devido para UF de destino, nas operações interestaduais de produtos que tiveram retenção de ICMS na UF do rementente)" },
86
+ { "50": "50 - Suspensão" },
87
+ { "51": "51 - Diferimento" },
88
+ { "60": "60 - Cobrado anteriormente por ST" },
89
+ { "60v2": "60 - Cobrado anteriormente por ST" },
90
+ { "70": "70 - Com redução de base de cálculo e cobrança do ICMS por ST" },
91
+ { "90": "90 - Outros (Com partilha do ICMS entre a UF de origem e a UF de destino ou a UF definida na legistação)" },
92
+ { "90v2": "90 - Outros" },
93
+ ],
94
+ },
95
+ orig: {
96
+ "@label": "Origem",
97
+ "@type": "select",
98
+ "@obrig": true,
99
+ "@values": [
100
+ { "0": "Nacional, exceto as indicadas nos códigos 3, 4, 5 e 8" },
101
+ { "1": "Estrangeira - Importação direta, exceto a indicada no código 6" },
102
+ { "2": "Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7" },
103
+ { "3": "Nacional, mercadoria ou bem com Conteúdo de Importação superior a 40% e inferior ou igual a 70%" },
104
+ { "4": "Nacional, cuja produção tenha sido feita em conformidade com os processos produtivos básicos de que tratam as legislações citadas nos Ajustes" },
105
+ { "5": "Nacional, mercadoria ou bem com Conteúdo de Importação inferior ou igual a 40% " },
106
+ { "6": "Estrangeira - Importação direta, sem similar nacional, constante em lista da CAMEX e gás natural" },
107
+ { "7": "Estrangeira - Adquirida no mercado interno, sem similar nacional, constante lista CAMEX e gás natural" }
108
+ ],
109
+ },
110
+ "@next": {
111
+ "@CST_00": {
112
+ modBC: {
113
+ "@label": "Modalidade de determinação da base de calculo ICMS",
114
+ "@type": "select",
115
+ "@obrig": true,
116
+ "@values": [
117
+ { "0": "0-Margem Valor Agregado (%)" },
118
+ { "1": "1-Pauta (Valor)" },
119
+ { "2": "2-Preço Tabelado Máx" },
120
+ { "3": "3-Valor da operação" }
121
+ ],
122
+ },
123
+ vBC: {
124
+ "@label": "Base de calculo ICMS",
125
+ "@type": "input",
126
+ "@obrig": true,
127
+ },
128
+ pICMS: {
129
+ "@label": "Líquota do ICMS",
130
+ "@type": "input",
131
+ "@obrig": true,
132
+ },
133
+ vICMS: {
134
+ "@label": "Valor ICMS",
135
+ "@type": "input",
136
+ "@obrig": true,
137
+ },
138
+ pFCP: {
139
+ "@label": "% Relativo ao FCP ST",
140
+ "@type": "input",
141
+ "@obrig": false,
142
+ },
143
+ vFCP: {
144
+ "@label": "Valor ICMS FCP ST",
145
+ "@type": "input",
146
+ "@obrig": false,
147
+ }
148
+ },
149
+ "@CST_10": {
150
+ modBC: {
151
+ "@label": "Modalidade de determinação da base de calculo ICMS",
152
+ "@type": "select",
153
+ "@obrig": true,
154
+ "@values": [
155
+ { "0": "0-Margem Valor Agregado (%)" },
156
+ { "1": "1-Pauta (Valor)" },
157
+ { "2": "2-Preço Tabelado Máx" },
158
+ { "3": "3-Valor da operação" }
159
+ ],
160
+ },
161
+ vBC: {
162
+ "@label": "Base de calculo ICMS",
163
+ "@type": "input",
164
+ "@obrig": true,
165
+ },
166
+ pICMS: {
167
+ "@label": "Líquota do ICMS",
168
+ "@type": "input",
169
+ "@obrig": true,
170
+ },
171
+ vICMS: {
172
+ "@label": "Valor ICMS",
173
+ "@type": "input",
174
+ "@obrig": true,
175
+ },
176
+ modBCST: {
177
+ "@label": "Modalidade de determinação da BC do ICMS ST",
178
+ "@type": "select",
179
+ "@obrig": true,
180
+ "@values": [
181
+ { "0": "Preço tabelado ou máximo sugerido" },
182
+ { "1": "Lista Negativa (valor)" },
183
+ { "2": "Lista Positiva (valor)" },
184
+ { "3": "Lista Neutra (valor)" },
185
+ { "4": "Margem Valor Agregado (%)" },
186
+ { "5": "Pauta (valor)" },
187
+ { "6": "Valor da Operação (NT 2019.001)" }
188
+ ],
189
+ },
190
+ pMVAST: {
191
+ "@label": "% Margem de valor adic. ICMS ST",
192
+ "@type": "input",
193
+ "@obrig": true,
194
+ },
195
+ pRedBCST: {
196
+ "@label": "% Redução de BC ICMS ST",
197
+ "@type": "input",
198
+ "@obrig": true,
199
+ },
200
+ vBCST: {
201
+ "@label": "BC ICMS ST",
202
+ "@type": "input",
203
+ "@obrig": true,
204
+ },
205
+ pICMSST: {
206
+ "@label": "Alíquota de ICMS ST",
207
+ "@type": "input",
208
+ "@obrig": true,
209
+ },
210
+ vICMSST: {
211
+ "@label": "ICMS ST",
212
+ "@type": "input",
213
+ "@obrig": true,
214
+ },
215
+ vBCFCPST: {
216
+ "@label": "BC ICMS FCP ST",
217
+ "@type": "input",
218
+ "@obrig": true,
219
+ },
220
+ pFCPST: {
221
+ "@label": "% Relativo ao FCP ST",
222
+ "@type": "input",
223
+ "@obrig": true,
224
+ },
225
+ vFCPST: {
226
+ "@label": "Valor ICMS FCP ST",
227
+ "@type": "input",
228
+ "@obrig": true,
229
+ },
230
+
231
+ vBCFCP: {
232
+ "@label": "BC ICMS FCP",
233
+ "@type": "input",
234
+ "@obrig": false,
235
+ },
236
+ pFCP: {
237
+ "@label": "% Relativo ao FCP",
238
+ "@type": "input",
239
+ "@obrig": false,
240
+ },
241
+ vFCP: {
242
+ "@label": "Valor ICMS FCP",
243
+ "@type": "input",
244
+ "@obrig": false,
245
+ },
246
+ }
247
+ }
248
+ }
249
+ },
250
+ },
251
+ II: {
252
+
253
+ },
254
+ ICMSint: {
255
+
256
+ },
257
+ IPI: {
258
+
259
+ },
260
+ IPIDev: {
261
+
262
+ },
263
+ ISSQN: {
264
+
265
+ },
266
+ COFINS: {
267
+
268
+ },
269
+ PIS: {
270
+
271
+ }
272
+ }
273
+
274
+ //Configurar valores
275
+ let configStruct = async (el: any, struc: any) => {
276
+
277
+ return el;
278
+ };
279
+
280
+ imposto = configStruct(imposto, gStruct)
281
+ }
59
282
  export { cUF2UF, UF2cUF }
package/src/utils/make.ts CHANGED
@@ -63,23 +63,38 @@ class Make {
63
63
  }
64
64
 
65
65
  tagRefNFe(obj: any) {
66
- throw "Não implementado!";
66
+ if (typeof this.#NFe.infNFe.ide.NFref == "undefined") {
67
+ this.#NFe.infNFe.ide.NFref = new Array();
68
+ }
69
+ this.#NFe.infNFe.ide.NFref.push({ refNFe: obj });
67
70
  }
68
71
 
69
72
  tagRefNF(obj: any) {
70
- throw "Não implementado!";
73
+ if (typeof this.#NFe.infNFe.ide.NFref == "undefined") {
74
+ this.#NFe.infNFe.ide.NFref = new Array();
75
+ }
76
+ this.#NFe.infNFe.ide.NFref.push({ refNF: obj });
71
77
  }
72
78
 
73
79
  tagRefNFP(obj: any) {
74
- throw "Não implementado!";
80
+ if (typeof this.#NFe.infNFe.ide.NFref == "undefined") {
81
+ this.#NFe.infNFe.ide.NFref = new Array();
82
+ }
83
+ this.#NFe.infNFe.ide.NFref.push({ refNFP: obj });
75
84
  }
76
85
 
77
86
  tagRefCTe(obj: any) {
78
- throw "Não implementado!";
87
+ if (typeof this.#NFe.infNFe.ide.NFref == "undefined") {
88
+ this.#NFe.infNFe.ide.NFref = new Array();
89
+ }
90
+ this.#NFe.infNFe.ide.NFref.push({ refCTe: obj });
79
91
  }
80
92
 
81
93
  tagRefECF(obj: any) {
82
- throw "Não implementado!";
94
+ if (typeof this.#NFe.infNFe.ide.NFref == "undefined") {
95
+ this.#NFe.infNFe.ide.NFref = new Array();
96
+ }
97
+ this.#NFe.infNFe.ide.NFref.push({ refECF: obj });
83
98
  }
84
99
 
85
100
  tagEmit(obj: any) {
@@ -119,11 +134,17 @@ class Make {
119
134
  }
120
135
 
121
136
  tagRetirada(obj: any) {
122
- throw "não implementado!";
137
+ this.#NFe.infNFe.retirada = {};
138
+ Object.keys(obj).forEach(key => {
139
+ this.#NFe.infNFe.retirada[key] = obj[key];
140
+ });
123
141
  }
124
142
 
125
143
  tagAutXML(obj: any) {
126
- throw "não implementado!";
144
+ if (typeof this.#NFe.infNFe.autXML == "undefined") {
145
+ this.#NFe.infNFe.autXML = new Array();
146
+ }
147
+ this.#NFe.infNFe.autXML.push(obj);
127
148
  }
128
149
 
129
150
  //tagprod
@@ -159,8 +180,10 @@ class Make {
159
180
  throw "não implementado!";
160
181
  }
161
182
 
162
- taginfAdProd(obj: any) {
163
- throw "não implementado!";
183
+ taginfAdProd(index: any, obj: any) {
184
+ Object.keys(obj).forEach(key => {
185
+ this.#NFe.infNFe.det[index][key] = obj[key];
186
+ });
164
187
  }
165
188
 
166
189
  tagCEST(obj: any) {
@@ -171,8 +194,26 @@ class Make {
171
194
  throw "não implementado!";
172
195
  }
173
196
 
174
- tagAdi(obj: any) {
175
- throw "não implementado!";
197
+ tagDI(index: any, obj: any) {
198
+ if (this.#NFe.infNFe.det[index].DI === undefined) this.#NFe.infNFe.det[index].DI = {};
199
+ Object.keys(obj).forEach(key => {
200
+ this.#NFe.infNFe.det[index].DI[key] = obj[key];
201
+ });
202
+
203
+ //Adicionar ao imposto global
204
+ this.#calICMSTot(obj);
205
+ }
206
+
207
+ tagAdi(index: any, obj: any) {
208
+ if (this.#NFe.infNFe.det[index].DI === undefined) this.#NFe.infNFe.det[index].DI = {};
209
+ if (this.#NFe.infNFe.det[index].DI.adi === undefined) this.#NFe.infNFe.det[index].DI.adi = {};
210
+
211
+ Object.keys(obj).forEach(key => {
212
+ this.#NFe.infNFe.det[index].DI.adi[key] = obj[key];
213
+ });
214
+
215
+ //Adicionar ao imposto global
216
+ this.#calICMSTot(obj);
176
217
  }
177
218
 
178
219
  tagDetExport(obj: any) {
@@ -594,9 +635,8 @@ class Make {
594
635
  throw "Ainda não configurado!";
595
636
  }
596
637
 
597
-
638
+ //Sistema gera a chave da nota fiscal
598
639
  #gerarChaveNFe() {
599
-
600
640
  const chaveSemDV =
601
641
  this.#NFe.infNFe.ide.cUF.padStart(2, '0') + // Código da UF (2 dígitos)
602
642
  this.#NFe.infNFe.ide.dhEmi.substring(2, 4) + this.#NFe.infNFe.ide.dhEmi.substring(5, 7) + // Ano e Mês da emissão (AAMM, 4 dígitos)
@@ -641,10 +681,10 @@ class Make {
641
681
 
642
682
  //Adicionar QrCode
643
683
  if (this.#NFe.infNFe.ide.mod == 65) {
644
- //Como ja temos cUF, usamo dados do extras para convere em UF e achar os dados de url!
684
+ //Como ja temos cUF, vamos usar o extras.cUF2UF
645
685
  let tempUF = urlEventos(cUF2UF[this.#NFe.infNFe.ide.cUF], this.#NFe.infNFe['@versao']);
646
686
  this.#NFe.infNFeSupl = {
647
- qrCode: tempUF.mod65[this.#NFe.infNFe.ide.tpAmb == 1 ? 'producao' : 'homologacao'].NFeConsultaQR,
687
+ qrCode: tempUF.mod65[this.#NFe.infNFe.ide.tpAmb == 1 ? 'producao' : 'homologacao'].NFeConsultaQR, //Este não e o valor final, vamos utilizar apenas para carregar os dados que vão ser utlizados no make
648
688
  urlChave: tempUF.mod65[this.#NFe.infNFe.ide.tpAmb == 1 ? 'producao' : 'homologacao'].urlChave
649
689
  }
650
690
  }
@@ -656,6 +696,7 @@ class Make {
656
696
  return tempBuild.build({ NFe: this.#NFe });
657
697
  }
658
698
 
699
+ //Obtem os dados de importo e soma no total, utlizado sempre que for setado algum imposto.
659
700
  #calICMSTot(obj: any) {
660
701
  Object.keys(obj).map(key => {
661
702
  if (this.#ICMSTot[key] !== undefined) {
@@ -43,9 +43,18 @@ class Tools {
43
43
  CSC: string;
44
44
  CSCid: string;
45
45
  versao: string;
46
+ timeout: number;
46
47
  };
47
48
 
48
- constructor(config = { mod: "", xmllint: 'xmllint', UF: '', tpAmb: 2, CSC: "", CSCid: "", versao: "4.00" }, certificado = { pfx: "", senha: "" }) {
49
+ constructor(config = { mod: "", xmllint: 'xmllint', UF: '', tpAmb: 2, CSC: "", CSCid: "", versao: "4.00", timeout: 30 }, certificado = { pfx: "", senha: "" }) {
50
+ if (typeof config != "object") throw "Tools({config},{}): Config deve ser um objecto!";
51
+ if (typeof config.UF == "undefined") throw "Tools({...,UF:?},{}): UF não definida!";
52
+ if (typeof config.tpAmb == "undefined") throw "Tools({...,tpAmb:?},{}): tpAmb não definida!";
53
+ if (typeof config.versao == "undefined") throw "Tools({...,versao:?},{}): versao não definida!";
54
+
55
+ if (typeof config.timeout == "undefined") config.timeout = 30;
56
+ if (typeof config.xmllint == "undefined") config.xmllint = 'xmllint';
57
+
49
58
  //Configurar certificado
50
59
  this.#config = config;
51
60
  this.#cert = certificado;
@@ -113,6 +122,13 @@ class Tools {
113
122
  });
114
123
  });
115
124
 
125
+ req.setTimeout(this.#config.timeout * 1000, () => {
126
+ reject({
127
+ name: 'TimeoutError',
128
+ message: 'The operation was aborted due to timeout'
129
+ });
130
+ req.destroy(); // cancela a requisição
131
+ });
116
132
  req.on('error', (erro) => {
117
133
  reject(erro);
118
134
  });
@@ -183,7 +199,7 @@ class Tools {
183
199
  }
184
200
 
185
201
  this.json2xml(xml).then(async res => {
186
- this.#xmlValido(res, `nfe_${this.#config.versao}`);
202
+ this.#xmlValido(res, `nfe_v${this.#config.versao}`);
187
203
  resvol(res);
188
204
  }).catch(err => {
189
205
  reject(err)
@@ -191,7 +207,8 @@ class Tools {
191
207
  })
192
208
  }
193
209
 
194
- #gerarQRCodeNFCe(NFe: any, versaoQRCode: string = "2", idCSC: string, CSC: string) {
210
+ //Gerar QRCode da NFCe
211
+ #gerarQRCodeNFCe(NFe: any, versaoQRCode: string = "2", idCSC: string, CSC: string): string {
195
212
  let s = '|',
196
213
  concat,
197
214
  hash;
@@ -217,12 +234,14 @@ class Tools {
217
234
  })
218
235
  }
219
236
 
220
- async getCertificado() {
237
+ //Obter certificado
238
+ async getCertificado(): Promise<object> {
221
239
  return new Promise(async (resvol, reject) => {
222
240
  this.#certTools().then(resvol).catch(reject)
223
241
  })
224
242
  }
225
243
 
244
+ //Consulta NFe
226
245
  consultarNFe(chNFe: string): Promise<string> {
227
246
  return new Promise(async (resolve, reject) => {
228
247
  if (!chNFe || chNFe.length !== 44) {
@@ -282,8 +301,14 @@ class Tools {
282
301
  res.on('end', () => resolve(data));
283
302
  });
284
303
 
304
+ req.setTimeout(this.#config.timeout * 1000, () => {
305
+ reject({
306
+ name: 'TimeoutError',
307
+ message: 'The operation was aborted due to timeout'
308
+ });
309
+ req.destroy(); // cancela a requisição
310
+ });
285
311
  req.on('error', (err) => reject(err));
286
-
287
312
  req.write(xml);
288
313
  req.end();
289
314
  } catch (err) {
@@ -355,6 +380,13 @@ class Tools {
355
380
  });
356
381
  });
357
382
 
383
+ req.setTimeout(this.#config.timeout * 1000, () => {
384
+ reject({
385
+ name: 'TimeoutError',
386
+ message: 'The operation was aborted due to timeout'
387
+ });
388
+ req.destroy(); // cancela a requisição
389
+ });
358
390
  req.on('error', (erro) => {
359
391
  reject(erro);
360
392
  });