From 865271f7e923af6ad51e15b1f3e3b3d10bdaf6c0 Mon Sep 17 00:00:00 2001 From: computador PRC Date: Mon, 18 Apr 2022 12:57:58 -0300 Subject: [PATCH 01/15] ... --- lib/factory/interface/nfe/index.d.ts | 3 ++ lib/factory/processor/enviaProcessor.js | 1 + lib/factory/schema/index.d.ts | 4 +-- lib/factory/webservices/webserviceHelper.js | 2 +- src/factory/interface/nfe/index.ts | 1 + src/factory/processor/enviaProcessor.ts | 2 +- src/factory/schema/index.ts | 4 +-- src/factory/webservices/webserviceHelper.ts | 2 +- test.js | 34 ++++++++++++++++----- 9 files changed, 39 insertions(+), 14 deletions(-) diff --git a/lib/factory/interface/nfe/index.d.ts b/lib/factory/interface/nfe/index.d.ts index 0230c03..2d0712b 100644 --- a/lib/factory/interface/nfe/index.d.ts +++ b/lib/factory/interface/nfe/index.d.ts @@ -66,6 +66,9 @@ export interface DocumentoFiscal { justificativaContingencia: string; isContingenciaOffline: boolean; indIntermed?: string; + NFref?: { + refNFe?: string; + }[]; } export interface Produto { prod: DetalhesProduto; diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index f807c40..c6a018d 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -319,6 +319,7 @@ class EnviaProcessor { dhSaiEnt: documento.dhSaiEnt, dhCont: documento.dhContingencia, xJust: documento.justificativaContingencia, + NFref: documento.NFref, }; if (documento.indIntermed) { ide.indIntermed = Utils.getEnumByValue(schema.TNFeInfNFeIdeIndIntermed, documento.indIntermed); // NT 2020.006 diff --git a/lib/factory/schema/index.d.ts b/lib/factory/schema/index.d.ts index f783091..d43e97a 100644 --- a/lib/factory/schema/index.d.ts +++ b/lib/factory/schema/index.d.ts @@ -130,7 +130,7 @@ export interface TNFeInfNFeIde { verProc: string; dhCont: string; xJust: string; - nFref: TNFeInfNFeIdeNFref[]; + NFref?: TNFeInfNFeIdeNFref[]; indIntermed?: TNFeInfNFeIdeIndIntermed; } export declare enum TNFeInfNFeIdeIndIntermed { @@ -203,7 +203,7 @@ export interface TNFeInfNFeIdeNFref { refNF?: TNFeInfNFeIdeNFrefRefNF; refNFP?: TNFeInfNFeIdeNFrefRefNFP; refNFe?: string; - itemElementName: ItemChoiceType1; + itemElementName?: ItemChoiceType1; } export interface TNFeInfNFeIdeNFrefRefECF { mod: TNFeInfNFeIdeNFrefRefECFMod; diff --git a/lib/factory/webservices/webserviceHelper.js b/lib/factory/webservices/webserviceHelper.js index b8e9fa9..9638962 100644 --- a/lib/factory/webservices/webserviceHelper.js +++ b/lib/factory/webservices/webserviceHelper.js @@ -69,7 +69,7 @@ class WebServiceHelper { if (proxy) { result.proxy = proxyToUrl(proxy); } - console.log(result); + // console.log(result) return result; } static buildCertAgentOpt(cert) { diff --git a/src/factory/interface/nfe/index.ts b/src/factory/interface/nfe/index.ts index 30959c0..de42f01 100644 --- a/src/factory/interface/nfe/index.ts +++ b/src/factory/interface/nfe/index.ts @@ -74,6 +74,7 @@ export interface DocumentoFiscal { justificativaContingencia: string; isContingenciaOffline: boolean; indIntermed?: string; + NFref?: {refNFe?: string}[]; } export interface Produto { diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index e0d8d9a..0b21965 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -387,7 +387,7 @@ export class EnviaProcessor { dhSaiEnt: documento.dhSaiEnt, dhCont: documento.dhContingencia, xJust: documento.justificativaContingencia, - //nFref: schema.TNFeInfNFeIdeNFref[], + NFref: documento.NFref, }; if (documento.indIntermed) { diff --git a/src/factory/schema/index.ts b/src/factory/schema/index.ts index 884240e..fb78dbd 100644 --- a/src/factory/schema/index.ts +++ b/src/factory/schema/index.ts @@ -158,7 +158,7 @@ export interface TNFeInfNFeIde { verProc: string; dhCont: string; xJust: string; - nFref: TNFeInfNFeIdeNFref[]; + NFref?: TNFeInfNFeIdeNFref[]; indIntermed?: TNFeInfNFeIdeIndIntermed; } @@ -253,7 +253,7 @@ export interface TNFeInfNFeIdeNFref { refNF?: TNFeInfNFeIdeNFrefRefNF; refNFP?: TNFeInfNFeIdeNFrefRefNFP; refNFe?: string; - itemElementName: ItemChoiceType1; + itemElementName?: ItemChoiceType1; } //[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.portalfiscal.inf.br/nfe")] diff --git a/src/factory/webservices/webserviceHelper.ts b/src/factory/webservices/webserviceHelper.ts index cc909f0..6524219 100644 --- a/src/factory/webservices/webserviceHelper.ts +++ b/src/factory/webservices/webserviceHelper.ts @@ -96,7 +96,7 @@ export abstract class WebServiceHelper { if (proxy) { result.proxy = proxyToUrl(proxy) } - console.log(result) + // console.log(result) return result } diff --git a/test.js b/test.js index c023760..598df94 100644 --- a/test.js +++ b/test.js @@ -4,11 +4,13 @@ const lib = require('./lib'); const signUtils = require('./lib/factory/signature'); const XmlHelper = require('./lib/factory/xmlHelper'); +const versao = '4.00' + let cert = { - key: fs.readFileSync('C:\\cert\\newKey.key'), - pem: fs.readFileSync('C:\\cert\\test.pem'), - pfx: fs.readFileSync('C:\\cert\\certificado.pfx'), - password: fs.readFileSync('C:\\cert\\senha.txt') + key: fs.readFileSync('C:\\node-dfe\\cert\\newKey.key'), + pem: fs.readFileSync('C:\\node-dfe\\cert\\test.pem'), + pfx: fs.readFileSync('C:\\node-dfe\\cert\\certificado.pfx'), + password: fs.readFileSync('C:\\node-dfe\\cert\\senha.txt').toString() }; let empresa = { @@ -68,6 +70,7 @@ let documento = { tipoEmissao: '1', tipoImpressao: '4', versaoAplicativoEmissao: 'NODE-NFE TEST 1.0', + NFref: [{refNFe: 'XXXXXXXXXXXXX'}, {refNFe: 'YYYYYYYYYYYYYY'}], }; let dest = { @@ -202,8 +205,25 @@ let nfce = { infoAdicional: infoAdic }; +const configuracoes = { + empresa: empresa, + certificado: cert, + geral: { + ambiente: documento.ambiente, + modelo: documento.modelo, + versao + }, + arquivos: { + salvar: true, + pastaEnvio: 'c:/node-dfe/xmls/envio', + pastaRetorno: 'c:/node-dfe/xmls/retorno', + pastaXML: 'c:/node-dfe/xmls/xml', + }, + responsavelTecnico: responsavelTecnico + } + async function testeEmissaoNFCe() { - const nfeProc = new lib.NFeProcessor(empresa, null); + const nfeProc = new lib.NFeProcessor(configuracoes, null); const ini = new Date(); let result = await nfeProc.processarDocumento(nfce); @@ -279,7 +299,7 @@ function testHashRespTec(){ //testeAssinaturaXML(); //testeConsultaStatusServico(empresa, '2', '65'); //testeDesereliaze(); -//testeEmissaoNFCe(); -testeEmissaoNFCeContingenciaOffline(empresa); +testeEmissaoNFCe(); +// testeEmissaoNFCeContingenciaOffline(empresa); //testeQRcodeNFCe(); //testHashRespTec(); \ No newline at end of file From b2fbb42cf583ddb6069e8e09b0d86d9c12668dec Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Mon, 18 Apr 2022 16:51:43 -0300 Subject: [PATCH 02/15] ... --- lib/factory/interface/nfe/index.d.ts | 4 +++- lib/factory/processor/nfeProcessor2.js | 30 ++++++++++++++++---------- src/factory/interface/nfe/index.ts | 2 +- src/factory/processor/nfeProcessor2.ts | 30 +++++++++++++++----------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/lib/factory/interface/nfe/index.d.ts b/lib/factory/interface/nfe/index.d.ts index 2d0712b..9d91ff2 100644 --- a/lib/factory/interface/nfe/index.d.ts +++ b/lib/factory/interface/nfe/index.d.ts @@ -25,7 +25,9 @@ export interface RetornoProcessamento { xml_recebido: string; status: number; success: boolean; - data: Object; + data: { + retEnviNFe?: Object; + }; error: string; } export interface NFeBase { diff --git a/lib/factory/processor/nfeProcessor2.js b/lib/factory/processor/nfeProcessor2.js index 6284af0..5b1ab99 100644 --- a/lib/factory/processor/nfeProcessor2.js +++ b/lib/factory/processor/nfeProcessor2.js @@ -49,6 +49,7 @@ class NFeProcessor { return result; } async executar(documento) { + var _a, _b, _c, _d; const { arquivos, geral } = this.configuracoes; let result = {}; try { @@ -58,11 +59,16 @@ class NFeProcessor { let cStat = ''; if (result.envioNF && result.envioNF.data) { const data = Object(result.envioNF.data); - if (data.retEnviNFe && geral.modelo == '55') { - retEnviNFe = data.retEnviNFe; - const recibo = retEnviNFe.infRec.nRec; - result.consultaProc = await this.retornoProcessor.executar(recibo); - retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + retEnviNFe = data.retEnviNFe; + if (data.retEnviNFe) { + if (geral.modelo == '55') { + const recibo = retEnviNFe.infRec.nRec; + result.consultaProc = await this.retornoProcessor.executar(recibo); + } + if ((_b = (_a = result.envioNF) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.retEnviNFe) + retConsReciNFe = Object(result.envioNF.data.retEnviNFe); + else + retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; cStat = retConsReciNFe.cStat; } if (retEnviNFe && retConsReciNFe) @@ -90,14 +96,16 @@ class NFeProcessor { fs.writeFileSync(filename, xmlNfeProc); } else { - const filenameEnvio = `${arquivos.pastaEnvio}${retEnviNFe.infRec.nRec}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${retEnviNFe.infRec.nRec}-retEnviNFe.xml`; + const filenameEnvio = `${arquivos.pastaEnvio}${new Date().toISOString()}${((_c = retEnviNFe.infRec) === null || _c === void 0 ? void 0 : _c.nRec) || ''}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${new Date().toISOString()}${((_d = retEnviNFe.infRec) === null || _d === void 0 ? void 0 : _d.nRec) || ''}-retEnviNFe.xml`; fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); - const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; - const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; - fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); - fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + if (result.consultaProc) { + const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; + const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; + fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); + fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + } } } } diff --git a/src/factory/interface/nfe/index.ts b/src/factory/interface/nfe/index.ts index de42f01..9134a7b 100644 --- a/src/factory/interface/nfe/index.ts +++ b/src/factory/interface/nfe/index.ts @@ -29,7 +29,7 @@ export interface RetornoProcessamento { xml_recebido: string, status: number, success: boolean, - data: Object, + data: {retEnviNFe?: Object}, error: string } diff --git a/src/factory/processor/nfeProcessor2.ts b/src/factory/processor/nfeProcessor2.ts index 530db89..f0d0ac0 100644 --- a/src/factory/processor/nfeProcessor2.ts +++ b/src/factory/processor/nfeProcessor2.ts @@ -69,11 +69,16 @@ export class NFeProcessor { if (result.envioNF && result.envioNF.data) { const data = Object(result.envioNF.data); - if (data.retEnviNFe && geral.modelo == '55') { - retEnviNFe = data.retEnviNFe; - const recibo = retEnviNFe.infRec.nRec; - result.consultaProc = await this.retornoProcessor.executar(recibo); - retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + retEnviNFe = data.retEnviNFe; + if (data.retEnviNFe) { + if (geral.modelo == '55') { + const recibo = retEnviNFe.infRec.nRec; + result.consultaProc = await this.retornoProcessor.executar(recibo); + } + if (result.envioNF?.data?.retEnviNFe) + retConsReciNFe = Object(result.envioNF.data.retEnviNFe); + else + retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; cStat = retConsReciNFe.cStat; } @@ -103,15 +108,16 @@ export class NFeProcessor { fs.writeFileSync(filename, xmlNfeProc); } else { - const filenameEnvio = `${arquivos.pastaEnvio}${retEnviNFe.infRec.nRec}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${retEnviNFe.infRec.nRec}-retEnviNFe.xml`; + const filenameEnvio = `${arquivos.pastaEnvio}${new Date().toISOString()}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${new Date().toISOString()}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); - - const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; - const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; - fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); - fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + if (result.consultaProc) { + const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; + const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; + fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); + fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + } } } } else if (result.retornoContingenciaOffline && result.success) { From e941bf1687302da227d52faa4f2854f17a53846a Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Mon, 18 Apr 2022 17:07:33 -0300 Subject: [PATCH 03/15] ... --- lib/factory/processor/nfeProcessor2.js | 5 +++-- src/factory/processor/nfeProcessor2.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/factory/processor/nfeProcessor2.js b/lib/factory/processor/nfeProcessor2.js index 5b1ab99..86e1230 100644 --- a/lib/factory/processor/nfeProcessor2.js +++ b/lib/factory/processor/nfeProcessor2.js @@ -96,8 +96,9 @@ class NFeProcessor { fs.writeFileSync(filename, xmlNfeProc); } else { - const filenameEnvio = `${arquivos.pastaEnvio}${new Date().toISOString()}${((_c = retEnviNFe.infRec) === null || _c === void 0 ? void 0 : _c.nRec) || ''}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${new Date().toISOString()}${((_d = retEnviNFe.infRec) === null || _d === void 0 ? void 0 : _d.nRec) || ''}-retEnviNFe.xml`; + const date = new Date().toISOString().replace(/:/g, '-'); + const filenameEnvio = `${arquivos.pastaEnvio}${date}${((_c = retEnviNFe.infRec) === null || _c === void 0 ? void 0 : _c.nRec) || ''}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${date}${((_d = retEnviNFe.infRec) === null || _d === void 0 ? void 0 : _d.nRec) || ''}-retEnviNFe.xml`; fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); if (result.consultaProc) { diff --git a/src/factory/processor/nfeProcessor2.ts b/src/factory/processor/nfeProcessor2.ts index f0d0ac0..3f7edf3 100644 --- a/src/factory/processor/nfeProcessor2.ts +++ b/src/factory/processor/nfeProcessor2.ts @@ -108,8 +108,9 @@ export class NFeProcessor { fs.writeFileSync(filename, xmlNfeProc); } else { - const filenameEnvio = `${arquivos.pastaEnvio}${new Date().toISOString()}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${new Date().toISOString()}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; + const date = new Date().toISOString().replace(/:/g, '-'); + const filenameEnvio = `${arquivos.pastaEnvio}${date}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${date}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); if (result.consultaProc) { From 480d433b40d16cedb29536e86e47abeb6e7006fe Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Mon, 18 Apr 2022 19:20:33 -0300 Subject: [PATCH 04/15] ... --- autorizadores.json | 4 ++++ autorizadoresNFe.json | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/autorizadores.json b/autorizadores.json index 98a49f6..8d48493 100644 --- a/autorizadores.json +++ b/autorizadores.json @@ -143,6 +143,10 @@ "inutilizacao": { "url_producao": "https://nfe.sefa.pr.gov.br/nfe/NFeInutilizacao4", "url_homologacao": "https://homologacao.nfce.sefa.pr.gov.br/nfce/Nfeinutilizacao4.asmx" + }, + "recepcaoEvento": { + "url_producao": "https://nfce.sefa.pr.gov.br/nfce/NFeRecepcaoEvento4", + "url_homologacao": "https://homologacao.nfce.sefa.pr.gov.br/nfce/NFeRecepcaoEvento4" } } }, diff --git a/autorizadoresNFe.json b/autorizadoresNFe.json index 4833b73..94a27b1 100644 --- a/autorizadoresNFe.json +++ b/autorizadoresNFe.json @@ -149,6 +149,10 @@ "consultarStatusServico": { "url_producao": "https://nfe.sefa.pr.gov.br/nfe/NFeStatusServico4?wsdl", "url_homologacao": "https://homologacao.nfe.sefa.pr.gov.br/nfe/NFeStatusServico4?wsdl" + }, + "recepcaoEvento": { + "url_producao": "https://nfe.sefa.pr.gov.br/nfe/NFeRecepcaoEvento4", + "url_homologacao": "https://homologacao.nfe.sefa.pr.gov.br/nfe/NFeRecepcaoEvento4" } } }, From af5756eb9bb364b44d45d86e9f94608f86ea647d Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Tue, 19 Apr 2022 00:19:42 -0300 Subject: [PATCH 05/15] ... --- lib/factory/interface/nfe/index.d.ts | 4 ++++ lib/factory/processor/enviaProcessor.js | 22 ++++++++++++---------- lib/factory/schema/index.d.ts | 2 ++ src/factory/interface/nfe/index.ts | 4 ++++ src/factory/processor/enviaProcessor.ts | 22 ++++++++++++---------- src/factory/schema/index.ts | 2 ++ 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/factory/interface/nfe/index.d.ts b/lib/factory/interface/nfe/index.d.ts index 9d91ff2..4b60955 100644 --- a/lib/factory/interface/nfe/index.d.ts +++ b/lib/factory/interface/nfe/index.d.ts @@ -77,6 +77,8 @@ export interface Produto { imposto: Imposto; infoAdicional: string; numeroItem: string; + pis: Object | undefined; + cofins: Object | undefined; } export interface DetalhesProduto { codigo: string; @@ -298,6 +300,8 @@ export interface RetTrib { } export interface Transporte { modalidateFrete: string; + transporta: Object; + vol: Object; } export interface Cobranca { fatura: Fatura; diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index c6a018d..75ddc74 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -395,7 +395,9 @@ class EnviaProcessor { prod: this.getDetProd(produtos[i].prod, ambiente, i == 0), imposto: this.getDetImposto(produtos[i].imposto, modelo, produtos[i].prod.CFOP), infAdProd: produtos[i].infoAdicional, - impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined + impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined, + PIS: produtos[i].pis, + COFINS: produtos[i].cofins, }); } return det_list; @@ -1168,18 +1170,18 @@ class EnviaProcessor { } getTransp(transp) { return { - modFrete: transp.modalidateFrete + modFrete: transp.modalidateFrete, + transporta: transp.transporta, /** - * transporta: TNFeInfNFeTranspTransporta; - retTransp: TNFeInfNFeTranspRetTransp; - //balsa - //reboque - //vagao - //veicTransp + * retTransp: TNFeInfNFeTranspRetTransp; + balsa + reboque + vagao + veicTransp items: object[]; itemsElementName: ItemsChoiceType5[]; - vol: TNFeInfNFeTranspVol[]; - */ + */ + vol: transp.vol, }; } getCobr(cobranca) { diff --git a/lib/factory/schema/index.d.ts b/lib/factory/schema/index.d.ts index d43e97a..133c4fa 100644 --- a/lib/factory/schema/index.d.ts +++ b/lib/factory/schema/index.d.ts @@ -440,6 +440,8 @@ export interface TNFeInfNFeDet { imposto: TNFeInfNFeDetImposto; impostoDevol: any; infAdProd: string; + PIS: Object; + COFINS: Object; } export interface TNFeInfNFeDetProd { cProd: string; diff --git a/src/factory/interface/nfe/index.ts b/src/factory/interface/nfe/index.ts index 9134a7b..38636a7 100644 --- a/src/factory/interface/nfe/index.ts +++ b/src/factory/interface/nfe/index.ts @@ -82,6 +82,8 @@ export interface Produto { imposto: Imposto; infoAdicional: string; numeroItem: string; + pis: Object | undefined, + cofins: Object | undefined, } export interface DetalhesProduto { @@ -330,6 +332,8 @@ export interface RetTrib { export interface Transporte { modalidateFrete: string; + transporta: Object, + vol: Object, //.. } diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index 0b21965..e0554ae 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -476,7 +476,9 @@ export class EnviaProcessor { prod: this.getDetProd(produtos[i].prod, ambiente, i == 0), imposto: this.getDetImposto(produtos[i].imposto, modelo, produtos[i].prod.CFOP), infAdProd: produtos[i].infoAdicional, - impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined + impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined, + PIS: produtos[i].pis, + COFINS: produtos[i].cofins, }); } @@ -1273,18 +1275,18 @@ export class EnviaProcessor { private getTransp(transp: Transporte) { return { - modFrete: transp.modalidateFrete + modFrete: transp.modalidateFrete, + transporta: transp.transporta, /** - * transporta: TNFeInfNFeTranspTransporta; - retTransp: TNFeInfNFeTranspRetTransp; - //balsa - //reboque - //vagao - //veicTransp + * retTransp: TNFeInfNFeTranspRetTransp; + balsa + reboque + vagao + veicTransp items: object[]; itemsElementName: ItemsChoiceType5[]; - vol: TNFeInfNFeTranspVol[]; - */ + */ + vol: transp.vol, } } diff --git a/src/factory/schema/index.ts b/src/factory/schema/index.ts index fb78dbd..13eda65 100644 --- a/src/factory/schema/index.ts +++ b/src/factory/schema/index.ts @@ -560,6 +560,8 @@ export interface TNFeInfNFeDet { imposto: TNFeInfNFeDetImposto; impostoDevol: any; //TNFeInfNFeDetImpostoDevol; infAdProd: string; + PIS: Object, + COFINS: Object, } //[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.portalfiscal.inf.br/nfe")] From f12ace016fa5632c5e9d94aecc8607f1a381574d Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Tue, 19 Apr 2022 13:14:20 -0300 Subject: [PATCH 06/15] ... --- lib/factory/processor/nfeProcessor2.js | 2 +- src/factory/processor/nfeProcessor2.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/factory/processor/nfeProcessor2.js b/lib/factory/processor/nfeProcessor2.js index 86e1230..549125a 100644 --- a/lib/factory/processor/nfeProcessor2.js +++ b/lib/factory/processor/nfeProcessor2.js @@ -61,7 +61,7 @@ class NFeProcessor { const data = Object(result.envioNF.data); retEnviNFe = data.retEnviNFe; if (data.retEnviNFe) { - if (geral.modelo == '55') { + if (geral.modelo == '55' && retEnviNFe.infRec) { const recibo = retEnviNFe.infRec.nRec; result.consultaProc = await this.retornoProcessor.executar(recibo); } diff --git a/src/factory/processor/nfeProcessor2.ts b/src/factory/processor/nfeProcessor2.ts index 3f7edf3..5bfe630 100644 --- a/src/factory/processor/nfeProcessor2.ts +++ b/src/factory/processor/nfeProcessor2.ts @@ -71,7 +71,7 @@ export class NFeProcessor { const data = Object(result.envioNF.data); retEnviNFe = data.retEnviNFe; if (data.retEnviNFe) { - if (geral.modelo == '55') { + if (geral.modelo == '55' && retEnviNFe.infRec) { const recibo = retEnviNFe.infRec.nRec; result.consultaProc = await this.retornoProcessor.executar(recibo); } From aca562f0f21c1e3114d26072b7bd319f39a7e17c Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Tue, 19 Apr 2022 13:42:09 -0300 Subject: [PATCH 07/15] ... --- lib/factory/processor/enviaProcessor.js | 2 +- src/factory/processor/enviaProcessor.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index 75ddc74..b395d57 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -1224,13 +1224,13 @@ class EnviaProcessor { detPag = {}; detPag.indPag = Utils.getEnumByValue(schema.TNFeInfNFePagDetPagIndPag, pag.indicadorFormaPagamento); detPag.tPag = Utils.getEnumByValue(schema.TNFeInfNFePagDetPagTPag, pag.formaPagamento); - detPag.vPag = pag.valor; if (pag.descricaoFormaPagamento) { detPag.xPag = pag.descricaoFormaPagamento; // YA02A NT 2020.006 } if (pag.dadosCartao) { detPag.card = this.getDetalhamentoCartao(pag.dadosCartao); } + detPag.vPag = pag.valor; listPagamentos.push(detPag); } return listPagamentos; diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index e0554ae..e2c43a7 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -1341,15 +1341,15 @@ export class EnviaProcessor { detPag.indPag = Utils.getEnumByValue(schema.TNFeInfNFePagDetPagIndPag, pag.indicadorFormaPagamento); detPag.tPag = Utils.getEnumByValue(schema.TNFeInfNFePagDetPagTPag, pag.formaPagamento); - detPag.vPag = pag.valor; - + if (pag.descricaoFormaPagamento) { detPag.xPag = pag.descricaoFormaPagamento; // YA02A NT 2020.006 } - + if (pag.dadosCartao) { detPag.card = this.getDetalhamentoCartao(pag.dadosCartao); } + detPag.vPag = pag.valor; listPagamentos.push(detPag); } From c043fc08d8d122eb758138c4de15d6a78541b13a Mon Sep 17 00:00:00 2001 From: computador PRC Date: Tue, 19 Apr 2022 15:40:26 -0300 Subject: [PATCH 08/15] ... --- src/factory/processor/enviaProcessor.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index e2c43a7..0e90881 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -477,8 +477,6 @@ export class EnviaProcessor { imposto: this.getDetImposto(produtos[i].imposto, modelo, produtos[i].prod.CFOP), infAdProd: produtos[i].infoAdicional, impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined, - PIS: produtos[i].pis, - COFINS: produtos[i].cofins, }); } From 9d41a192af9273408005a814acb08d4447688c60 Mon Sep 17 00:00:00 2001 From: computador PRC Date: Tue, 19 Apr 2022 15:42:04 -0300 Subject: [PATCH 09/15] ... --- lib/factory/processor/enviaProcessor.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index b395d57..7299aed 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -396,8 +396,6 @@ class EnviaProcessor { imposto: this.getDetImposto(produtos[i].imposto, modelo, produtos[i].prod.CFOP), infAdProd: produtos[i].infoAdicional, impostoDevol: (produtos[i].prod.percentualDevolucao && (produtos[i].prod.percentualDevolucao > 0)) ? this.getImpostoDevolucao({ pDevol: produtos[i].prod.percentualDevolucao, vIPIDevol: produtos[i].prod.valorIPIDevolucao }) : undefined, - PIS: produtos[i].pis, - COFINS: produtos[i].cofins, }); } return det_list; From 88d2bdcf46c416f98c711d459c8c35100d6c9d99 Mon Sep 17 00:00:00 2001 From: EduardoReolon Date: Fri, 27 May 2022 18:08:04 -0300 Subject: [PATCH 10/15] ... --- lib/factory/processor/enviaProcessor.js | 8 +++----- src/factory/processor/enviaProcessor.ts | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index 7299aed..84addea 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -86,11 +86,9 @@ class EnviaProcessor { } configuraUrlsSefaz() { const { geral: { modelo, ambiente }, empresa } = this.configuracoes; - if (!soapAutorizacao || !soapRetAutorizacao) { - let Sefaz = modelo == '65' ? sefazNfce_1.SefazNFCe : sefazNfe_1.SefazNFe; - soapAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, nfe_1.ServicosSefaz.autorizacao); - soapRetAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, nfe_1.ServicosSefaz.retAutorizacao); - } + let Sefaz = modelo == '65' ? sefazNfce_1.SefazNFCe : sefazNfe_1.SefazNFe; + soapAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, nfe_1.ServicosSefaz.autorizacao); + soapRetAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, nfe_1.ServicosSefaz.retAutorizacao); } appendQRCodeXML(documento, xmlAssinado) { let qrCode = null; diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index 0e90881..128a76e 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -100,12 +100,10 @@ export class EnviaProcessor { private configuraUrlsSefaz() { const { geral: { modelo, ambiente }, empresa } = this.configuracoes; - if (!soapAutorizacao || !soapRetAutorizacao) { - let Sefaz = modelo == '65' ? SefazNFCe : SefazNFe; + let Sefaz = modelo == '65' ? SefazNFCe : SefazNFe; - soapAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, ServicosSefaz.autorizacao); - soapRetAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, ServicosSefaz.retAutorizacao); - } + soapAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, ServicosSefaz.autorizacao); + soapRetAutorizacao = Sefaz.getSoapInfo(empresa.endereco.uf, ambiente, ServicosSefaz.retAutorizacao); } private appendQRCodeXML(documento: NFCeDocumento, xmlAssinado: string) { From 474c05f621f18bf3ad4c77fccd8804e7b40c0adc Mon Sep 17 00:00:00 2001 From: Eduardo Reolon Date: Sat, 29 Mar 2025 12:15:38 -0300 Subject: [PATCH 11/15] getImpostoPIS estava faltando uns breaks --- lib/factory/processor/enviaProcessor.js | 2 ++ lib/factory/processor/nfeProcessor.js | 2 ++ src/factory/processor/enviaProcessor.ts | 2 ++ src/factory/processor/nfeProcessor.ts | 2 ++ 4 files changed, 8 insertions(+) diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index 84addea..6d2d4d8 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -928,6 +928,7 @@ class EnviaProcessor { CST: pis.CST } }; + break; case '49': case '50': case '51': @@ -964,6 +965,7 @@ class EnviaProcessor { vPIS: pis.vPIS } }; + break; default: result = { PISOutr: { diff --git a/lib/factory/processor/nfeProcessor.js b/lib/factory/processor/nfeProcessor.js index fbc685a..85c3063 100644 --- a/lib/factory/processor/nfeProcessor.js +++ b/lib/factory/processor/nfeProcessor.js @@ -984,6 +984,7 @@ class NFeProcessor { CST: pis.CST } }; + break; case '49': case '50': case '51': @@ -1020,6 +1021,7 @@ class NFeProcessor { vPIS: pis.vPIS } }; + break; default: result = { PISOutr: { diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index 128a76e..5e40ba8 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -1025,6 +1025,7 @@ export class EnviaProcessor { CST: pis.CST } } + break; case '49': case '50': case '51': @@ -1060,6 +1061,7 @@ export class EnviaProcessor { vPIS: pis.vPIS } } + break; default: result = { PISOutr: { diff --git a/src/factory/processor/nfeProcessor.ts b/src/factory/processor/nfeProcessor.ts index f7ff89f..811f5f2 100644 --- a/src/factory/processor/nfeProcessor.ts +++ b/src/factory/processor/nfeProcessor.ts @@ -1109,6 +1109,7 @@ export class NFeProcessor { CST: pis.CST } } + break; case '49': case '50': case '51': @@ -1144,6 +1145,7 @@ export class NFeProcessor { vPIS: pis.vPIS } } + break; default: result = { PISOutr: { From 5a9ca1a0ec5d6b1f4acce976ab0e7244495f7316 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Tue, 2 Dec 2025 19:45:59 -0300 Subject: [PATCH 12/15] inclusao ibscbs nos impostos --- lib/factory/interface/nfe/index.d.ts | 1 + lib/factory/processor/enviaProcessor.js | 1 + src/factory/interface/nfe/index.ts | 3 ++- src/factory/processor/enviaProcessor.ts | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/factory/interface/nfe/index.d.ts b/lib/factory/interface/nfe/index.d.ts index 4b60955..f66ad5a 100644 --- a/lib/factory/interface/nfe/index.d.ts +++ b/lib/factory/interface/nfe/index.d.ts @@ -123,6 +123,7 @@ export interface Imposto { pisst: PisST; cofinsst: CofinsST; icmsUfDest: IcmsUfDest; + IBSCBS: any; } export interface Icms { orig: string; diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index 6d2d4d8..1a2ef31 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -442,6 +442,7 @@ class EnviaProcessor { II: imposto.ii ? [this.getImpostoII(imposto.ii, cfop)] : [], ICMSUFDest: imposto.icmsUfDest ? [this.getIcmsUfDest(imposto.icmsUfDest)] : [], ISSQN: '', + IBSCBS: imposto.IBSCBS }; return detImposto; } diff --git a/src/factory/interface/nfe/index.ts b/src/factory/interface/nfe/index.ts index 38636a7..22ea10d 100644 --- a/src/factory/interface/nfe/index.ts +++ b/src/factory/interface/nfe/index.ts @@ -141,7 +141,8 @@ export interface Imposto { issqn: Issqn; pisst: PisST; cofinsst: CofinsST; - icmsUfDest: IcmsUfDest + icmsUfDest: IcmsUfDest; + IBSCBS: any } export interface Icms { diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index 5e40ba8..e0261e1 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -533,6 +533,7 @@ export class EnviaProcessor { II: imposto.ii ? [this.getImpostoII(imposto.ii, cfop)] : [], ICMSUFDest: imposto.icmsUfDest ? [this.getIcmsUfDest(imposto.icmsUfDest)] : [], ISSQN: '', + IBSCBS: imposto.IBSCBS }; return detImposto; From d49904c718042e10f2a874501c9d1c1d0485097b Mon Sep 17 00:00:00 2001 From: Eduardo Date: Wed, 3 Dec 2025 09:40:38 -0300 Subject: [PATCH 13/15] inclusao ibscbs nos impostos --- lib/factory/processor/nfeProcessor2.js | 129 ++++++++++++++++++---- src/factory/processor/nfeProcessor2.ts | 146 +++++++++++++++++++++---- 2 files changed, 232 insertions(+), 43 deletions(-) diff --git a/lib/factory/processor/nfeProcessor2.js b/lib/factory/processor/nfeProcessor2.js index 549125a..ffc0f87 100644 --- a/lib/factory/processor/nfeProcessor2.js +++ b/lib/factory/processor/nfeProcessor2.js @@ -49,33 +49,46 @@ class NFeProcessor { return result; } async executar(documento) { - var _a, _b, _c, _d; + var _a; const { arquivos, geral } = this.configuracoes; let result = {}; try { result = await this.enviaProcessor.executar(documento); let retEnviNFe = null; let retConsReciNFe = null; - let cStat = ''; + let finalResponse = null; // Objeto que conterá o protNFe final if (result.envioNF && result.envioNF.data) { const data = Object(result.envioNF.data); - retEnviNFe = data.retEnviNFe; if (data.retEnviNFe) { - if (geral.modelo == '55' && retEnviNFe.infRec) { - const recibo = retEnviNFe.infRec.nRec; - result.consultaProc = await this.retornoProcessor.executar(recibo); + retEnviNFe = data.retEnviNFe; + // Lógica Assíncrona (NF-e Modelo 55) + if (geral.modelo == '55') { + if (retEnviNFe.infRec) { + const recibo = retEnviNFe.infRec.nRec; + result.consultaProc = await this.retornoProcessor.executar(recibo); + retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + finalResponse = retConsReciNFe; + } + } + // Lógica Síncrona (NFC-e Modelo 65) + else { + // Na NFC-e o retorno do envio JÁ É o retorno final com protocolo + finalResponse = retEnviNFe; } - if ((_b = (_a = result.envioNF) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.retEnviNFe) - retConsReciNFe = Object(result.envioNF.data.retEnviNFe); - else - retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; - cStat = retConsReciNFe.cStat; } - if (retEnviNFe && retConsReciNFe) - if (retEnviNFe.cStat == '103' && retConsReciNFe.cStat == '104') { //protNFe.infProt. + // Verificação de Sucesso + if (finalResponse) { + // Sucesso NF-e (Lote Recebido + Lote Processado) + if (geral.modelo == '55' && retEnviNFe.cStat == '103' && finalResponse.cStat == '104') { result.confirmada = true; result.success = true; } + // Sucesso NFC-e (Lote Processado Direto) + if (geral.modelo == '65' && finalResponse.cStat == '104') { + result.confirmada = true; + result.success = true; + } + } if (arquivos && arquivos.salvar) { if (!fs.existsSync(arquivos.pastaEnvio)) fs.mkdirSync(arquivos.pastaEnvio, { recursive: true }); @@ -83,24 +96,30 @@ class NFeProcessor { fs.mkdirSync(arquivos.pastaRetorno, { recursive: true }); if (!fs.existsSync(arquivos.pastaXML)) fs.mkdirSync(arquivos.pastaXML, { recursive: true }); - if ((result.success == true) && (retConsReciNFe.cStat == '104')) { - const filename = `${arquivos.pastaXML}${retConsReciNFe.protNFe.infProt.chNFe}-procNFe.xml`; + // Salvar XML de Processamento (Autorizado) + if ((result.success == true) && (finalResponse && finalResponse.cStat == '104' && finalResponse.protNFe)) { + const filename = `${arquivos.pastaXML}${finalResponse.protNFe.infProt.chNFe}-procNFe.xml`; const nfe_enviada = Object(xmlHelper_1.XmlHelper.deserializeXml(result.envioNF.xml_enviado, { explicitArray: false })); const nfeProc = { - $: { versao: "1.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, + $: { versao: "4.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, NFe: nfe_enviada.enviNFe.NFe, - protNFe: retConsReciNFe.protNFe + protNFe: finalResponse.protNFe }; Utils.removeSelfClosedFields(nfeProc); let xmlNfeProc = xmlHelper_1.XmlHelper.serializeXml(nfeProc, 'nfeProc'); fs.writeFileSync(filename, xmlNfeProc); } + // Salvar XMLs de Erro / Logs else { const date = new Date().toISOString().replace(/:/g, '-'); - const filenameEnvio = `${arquivos.pastaEnvio}${date}${((_c = retEnviNFe.infRec) === null || _c === void 0 ? void 0 : _c.nRec) || ''}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${date}${((_d = retEnviNFe.infRec) === null || _d === void 0 ? void 0 : _d.nRec) || ''}-retEnviNFe.xml`; - fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); - fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); + // Proteção: Se não houver nRec (erro de schema), usa timestamp + const idIdentificador = ((_a = retEnviNFe === null || retEnviNFe === void 0 ? void 0 : retEnviNFe.infRec) === null || _a === void 0 ? void 0 : _a.nRec) || new Date().getTime().toString(); + const filenameEnvio = `${arquivos.pastaEnvio}${date}-${idIdentificador}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${date}-${idIdentificador}-retEnviNFe.xml`; + if (result.envioNF.xml_enviado) + fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); + if (result.envioNF.xml_recebido) + fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); if (result.consultaProc) { const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; @@ -124,6 +143,74 @@ class NFeProcessor { } return result; } + // public async executar(documento: NFeDocumento | NFCeDocumento) { + // const { arquivos, geral } = this.configuracoes; + // let result = {}; + // try { + // result = await this.enviaProcessor.executar(documento); + // let retEnviNFe = null; + // let retConsReciNFe = null; + // let cStat = ''; + // if (result.envioNF && result.envioNF.data) { + // const data = Object(result.envioNF.data); + // retEnviNFe = data.retEnviNFe; + // if (data.retEnviNFe) { + // if (geral.modelo == '55' && retEnviNFe.infRec) { + // const recibo = retEnviNFe.infRec.nRec; + // result.consultaProc = await this.retornoProcessor.executar(recibo); + // } + // if (result.envioNF?.data?.retEnviNFe) + // retConsReciNFe = Object(result.envioNF.data.retEnviNFe); + // else + // retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + // cStat = retConsReciNFe.cStat; + // } + // if (retEnviNFe && retConsReciNFe) + // if (retEnviNFe.cStat == '103' && retConsReciNFe.cStat == '104') { //protNFe.infProt. + // result.confirmada = true; + // result.success = true; + // } + // if (arquivos && arquivos.salvar) { + // if (!fs.existsSync(arquivos.pastaEnvio)) fs.mkdirSync(arquivos.pastaEnvio, { recursive: true }); + // if (!fs.existsSync(arquivos.pastaRetorno)) fs.mkdirSync(arquivos.pastaRetorno, { recursive: true }); + // if (!fs.existsSync(arquivos.pastaXML)) fs.mkdirSync(arquivos.pastaXML, { recursive: true }); + // if ((result.success == true) && (retConsReciNFe.cStat == '104')) { + // const filename = `${arquivos.pastaXML}${retConsReciNFe.protNFe.infProt.chNFe}-procNFe.xml`; + // const nfe_enviada = Object(XmlHelper.deserializeXml(result.envioNF.xml_enviado, { explicitArray: false })); + // const nfeProc = { + // $: { versao: "1.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, + // NFe: nfe_enviada.enviNFe.NFe, + // protNFe: retConsReciNFe.protNFe + // }; + // Utils.removeSelfClosedFields(nfeProc); + // let xmlNfeProc = XmlHelper.serializeXml(nfeProc, 'nfeProc'); + // fs.writeFileSync(filename, xmlNfeProc); + // } else { + // const date = new Date().toISOString().replace(/:/g, '-'); + // const filenameEnvio = `${arquivos.pastaEnvio}${date}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; + // const filenameRetorno = `${arquivos.pastaRetorno}${date}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; + // fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); + // fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); + // if (result.consultaProc) { + // const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; + // const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; + // fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); + // fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + // } + // } + // } + // } else if (result.retornoContingenciaOffline && result.success) { + // return result; + // } else { + // console.error('nfeProcessor.executar: Erro ao realizar requisição', result); + // throw new Error('Erro ao realizar requisição'); + // } + // } catch (ex) { + // result.success = false; + // result.error = ex; + // } + // return result; + // } async inutilizarNumeracao(dados) { return await this.inutlizacaoProcessor.executar(dados); } diff --git a/src/factory/processor/nfeProcessor2.ts b/src/factory/processor/nfeProcessor2.ts index 5bfe630..21c7a67 100644 --- a/src/factory/processor/nfeProcessor2.ts +++ b/src/factory/processor/nfeProcessor2.ts @@ -65,54 +65,78 @@ export class NFeProcessor { let retEnviNFe = null; let retConsReciNFe = null; - let cStat = ''; + let finalResponse = null; // Objeto que conterá o protNFe final if (result.envioNF && result.envioNF.data) { const data = Object(result.envioNF.data); - retEnviNFe = data.retEnviNFe; + if (data.retEnviNFe) { - if (geral.modelo == '55' && retEnviNFe.infRec) { - const recibo = retEnviNFe.infRec.nRec; - result.consultaProc = await this.retornoProcessor.executar(recibo); + retEnviNFe = data.retEnviNFe; + + // Lógica Assíncrona (NF-e Modelo 55) + if (geral.modelo == '55') { + if (retEnviNFe.infRec) { + const recibo = retEnviNFe.infRec.nRec; + result.consultaProc = await this.retornoProcessor.executar(recibo); + retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + finalResponse = retConsReciNFe; + } + } + // Lógica Síncrona (NFC-e Modelo 65) + else { + // Na NFC-e o retorno do envio JÁ É o retorno final com protocolo + finalResponse = retEnviNFe; } - if (result.envioNF?.data?.retEnviNFe) - retConsReciNFe = Object(result.envioNF.data.retEnviNFe); - else - retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; - cStat = retConsReciNFe.cStat; } - if (retEnviNFe && retConsReciNFe) - if (retEnviNFe.cStat == '103' && retConsReciNFe.cStat == '104') { //protNFe.infProt. + // Verificação de Sucesso + if (finalResponse) { + // Sucesso NF-e (Lote Recebido + Lote Processado) + if (geral.modelo == '55' && retEnviNFe.cStat == '103' && finalResponse.cStat == '104') { + result.confirmada = true; + result.success = true; + } + // Sucesso NFC-e (Lote Processado Direto) + if (geral.modelo == '65' && finalResponse.cStat == '104') { result.confirmada = true; result.success = true; } + } if (arquivos && arquivos.salvar) { if (!fs.existsSync(arquivos.pastaEnvio)) fs.mkdirSync(arquivos.pastaEnvio, { recursive: true }); if (!fs.existsSync(arquivos.pastaRetorno)) fs.mkdirSync(arquivos.pastaRetorno, { recursive: true }); if (!fs.existsSync(arquivos.pastaXML)) fs.mkdirSync(arquivos.pastaXML, { recursive: true }); - if ((result.success == true) && (retConsReciNFe.cStat == '104')) { - const filename = `${arquivos.pastaXML}${retConsReciNFe.protNFe.infProt.chNFe}-procNFe.xml`; + // Salvar XML de Processamento (Autorizado) + if ((result.success == true) && (finalResponse && finalResponse.cStat == '104' && finalResponse.protNFe)) { + const filename = `${arquivos.pastaXML}${finalResponse.protNFe.infProt.chNFe}-procNFe.xml`; const nfe_enviada = Object(XmlHelper.deserializeXml(result.envioNF.xml_enviado, { explicitArray: false })); + const nfeProc = { - $: { versao: "1.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, + $: { versao: "4.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, // Ajustado para 4.00 NFe: nfe_enviada.enviNFe.NFe, - protNFe: retConsReciNFe.protNFe + protNFe: finalResponse.protNFe }; Utils.removeSelfClosedFields(nfeProc); let xmlNfeProc = XmlHelper.serializeXml(nfeProc, 'nfeProc'); fs.writeFileSync(filename, xmlNfeProc); - } else { + } + // Salvar XMLs de Erro / Logs + else { const date = new Date().toISOString().replace(/:/g, '-'); - const filenameEnvio = `${arquivos.pastaEnvio}${date}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; - const filenameRetorno = `${arquivos.pastaRetorno}${date}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; - fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); - fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); + // Proteção: Se não houver nRec (erro de schema), usa timestamp + const idIdentificador = retEnviNFe?.infRec?.nRec || new Date().getTime().toString(); + + const filenameEnvio = `${arquivos.pastaEnvio}${date}-${idIdentificador}-enviNFe.xml`; + const filenameRetorno = `${arquivos.pastaRetorno}${date}-${idIdentificador}-retEnviNFe.xml`; + + if (result.envioNF.xml_enviado) fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); + if (result.envioNF.xml_recebido) fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); + if (result.consultaProc) { const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; @@ -129,12 +153,90 @@ export class NFeProcessor { } } catch (ex) { result.success = false; - result.error = ex; + result.error = ex as any; } return result; } + // public async executar(documento: NFeDocumento | NFCeDocumento) { + // const { arquivos, geral } = this.configuracoes; + // let result = {}; + // try { + // result = await this.enviaProcessor.executar(documento); + + // let retEnviNFe = null; + // let retConsReciNFe = null; + // let cStat = ''; + + // if (result.envioNF && result.envioNF.data) { + // const data = Object(result.envioNF.data); + // retEnviNFe = data.retEnviNFe; + // if (data.retEnviNFe) { + // if (geral.modelo == '55' && retEnviNFe.infRec) { + // const recibo = retEnviNFe.infRec.nRec; + // result.consultaProc = await this.retornoProcessor.executar(recibo); + // } + // if (result.envioNF?.data?.retEnviNFe) + // retConsReciNFe = Object(result.envioNF.data.retEnviNFe); + // else + // retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; + // cStat = retConsReciNFe.cStat; + // } + + // if (retEnviNFe && retConsReciNFe) + // if (retEnviNFe.cStat == '103' && retConsReciNFe.cStat == '104') { //protNFe.infProt. + // result.confirmada = true; + // result.success = true; + // } + + // if (arquivos && arquivos.salvar) { + // if (!fs.existsSync(arquivos.pastaEnvio)) fs.mkdirSync(arquivos.pastaEnvio, { recursive: true }); + // if (!fs.existsSync(arquivos.pastaRetorno)) fs.mkdirSync(arquivos.pastaRetorno, { recursive: true }); + // if (!fs.existsSync(arquivos.pastaXML)) fs.mkdirSync(arquivos.pastaXML, { recursive: true }); + + // if ((result.success == true) && (retConsReciNFe.cStat == '104')) { + // const filename = `${arquivos.pastaXML}${retConsReciNFe.protNFe.infProt.chNFe}-procNFe.xml`; + + // const nfe_enviada = Object(XmlHelper.deserializeXml(result.envioNF.xml_enviado, { explicitArray: false })); + // const nfeProc = { + // $: { versao: "1.00", xmlns: "http://www.portalfiscal.inf.br/nfe" }, + // NFe: nfe_enviada.enviNFe.NFe, + // protNFe: retConsReciNFe.protNFe + // }; + + // Utils.removeSelfClosedFields(nfeProc); + // let xmlNfeProc = XmlHelper.serializeXml(nfeProc, 'nfeProc'); + + // fs.writeFileSync(filename, xmlNfeProc); + // } else { + // const date = new Date().toISOString().replace(/:/g, '-'); + // const filenameEnvio = `${arquivos.pastaEnvio}${date}${retEnviNFe.infRec?.nRec || ''}-enviNFe.xml`; + // const filenameRetorno = `${arquivos.pastaRetorno}${date}${retEnviNFe.infRec?.nRec || ''}-retEnviNFe.xml`; + // fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado); + // fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido); + // if (result.consultaProc) { + // const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`; + // const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`; + // fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado); + // fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido); + // } + // } + // } + // } else if (result.retornoContingenciaOffline && result.success) { + // return result; + // } else { + // console.error('nfeProcessor.executar: Erro ao realizar requisição', result); + // throw new Error('Erro ao realizar requisição'); + // } + // } catch (ex) { + // result.success = false; + // result.error = ex; + // } + + // return result; + // } + public async inutilizarNumeracao( dados: Inutilizar) { return await this.inutlizacaoProcessor.executar( dados ); } From 8de47edaa282af29c7c1eea6dce2c2bc16f56f5f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Mon, 8 Dec 2025 21:34:17 -0300 Subject: [PATCH 14/15] ajustes --- lib/factory/processor/nfeProcessor2.js | 3 +++ src/factory/processor/nfeProcessor2.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lib/factory/processor/nfeProcessor2.js b/lib/factory/processor/nfeProcessor2.js index ffc0f87..813c2f3 100644 --- a/lib/factory/processor/nfeProcessor2.js +++ b/lib/factory/processor/nfeProcessor2.js @@ -69,6 +69,9 @@ class NFeProcessor { retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; finalResponse = retConsReciNFe; } + else if (retEnviNFe.protNFe) { + finalResponse = retEnviNFe; + } } // Lógica Síncrona (NFC-e Modelo 65) else { diff --git a/src/factory/processor/nfeProcessor2.ts b/src/factory/processor/nfeProcessor2.ts index 21c7a67..5d86927 100644 --- a/src/factory/processor/nfeProcessor2.ts +++ b/src/factory/processor/nfeProcessor2.ts @@ -80,6 +80,8 @@ export class NFeProcessor { result.consultaProc = await this.retornoProcessor.executar(recibo); retConsReciNFe = Object(result.consultaProc.data).retConsReciNFe; finalResponse = retConsReciNFe; + } else if (retEnviNFe.protNFe) { + finalResponse = retEnviNFe; } } // Lógica Síncrona (NFC-e Modelo 65) From ae90b01983bf19e82890d9829410c4ec2b912332 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Wed, 4 Mar 2026 16:23:15 -0300 Subject: [PATCH 15/15] ajustes --- lib/factory/interface/nfe/index.d.ts | 6 ++++++ lib/factory/processor/enviaProcessor.js | 6 ++---- src/factory/interface/nfe/index.ts | 7 +++++++ src/factory/processor/enviaProcessor.ts | 9 +++++---- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/factory/interface/nfe/index.d.ts b/lib/factory/interface/nfe/index.d.ts index f66ad5a..f5a4a3b 100644 --- a/lib/factory/interface/nfe/index.d.ts +++ b/lib/factory/interface/nfe/index.d.ts @@ -246,10 +246,16 @@ export interface Issqn { nProcesso: string; indIncentivo: string; } +export interface IbsCbsTot { + vIBS: string; + vCBS: string; + vIS: string; +} export interface Total { icmsTot: IcmsTot; issqnTot: IssqnTot; retTrib: RetTrib; + IBSCBSTot?: IbsCbsTot; } export interface IcmsTot { vBC: string; diff --git a/lib/factory/processor/enviaProcessor.js b/lib/factory/processor/enviaProcessor.js index 1a2ef31..74c8dcc 100644 --- a/lib/factory/processor/enviaProcessor.js +++ b/lib/factory/processor/enviaProcessor.js @@ -135,7 +135,7 @@ class EnviaProcessor { log(jsonOneLevel(retornoEnvio), 'retornoEnvio.full'); } catch (e) { - log(`ja deu erro pra logar.......${e.toString()}`, 'retornoEnvio'); + log(`ja deu erro pra logar.......${e === null || e === void 0 ? void 0 : e.toString()}`, 'retornoEnvio'); } if (retornoEnvio && retornoEnvio.data) { const data = Object(retornoEnvio.data); @@ -1160,9 +1160,7 @@ class EnviaProcessor { }; } getTotal(total) { - return { - ICMSTot: total.icmsTot - }; + return Object.assign({ ICMSTot: total.icmsTot }, (total.IBSCBSTot ? { IBSCBSTot: total.IBSCBSTot } : {})); } getIcmsTot(icmsTot) { return icmsTot; diff --git a/src/factory/interface/nfe/index.ts b/src/factory/interface/nfe/index.ts index 22ea10d..6361e9c 100644 --- a/src/factory/interface/nfe/index.ts +++ b/src/factory/interface/nfe/index.ts @@ -274,10 +274,17 @@ export interface Issqn { indIncentivo: string } +export interface IbsCbsTot { + vIBS: string; + vCBS: string; + vIS: string; +} + export interface Total { icmsTot: IcmsTot; issqnTot: IssqnTot; retTrib: RetTrib; + IBSCBSTot?: IbsCbsTot; } export interface IcmsTot { diff --git a/src/factory/processor/enviaProcessor.ts b/src/factory/processor/enviaProcessor.ts index e0261e1..7da6b88 100644 --- a/src/factory/processor/enviaProcessor.ts +++ b/src/factory/processor/enviaProcessor.ts @@ -92,7 +92,7 @@ export class EnviaProcessor { } } catch (ex) { result.success = false; - result.error = ex; + result.error = ex as any; } return result; @@ -163,7 +163,7 @@ export class EnviaProcessor { log(jsonOneLevel(retornoEnvio), 'retornoEnvio.full'); } catch (e) { - log(`ja deu erro pra logar.......${e.toString()}`, 'retornoEnvio') + log(`ja deu erro pra logar.......${e?.toString()}`, 'retornoEnvio') } if (retornoEnvio && retornoEnvio.data) { @@ -189,7 +189,7 @@ export class EnviaProcessor { result.envioNF = retornoEnvio; } catch (ex) { result.success = false; - result.error = ex; + result.error = ex as any; } result.success = true; //nao esta confirmada, mas houve sucesso nessa requisicao de envio da nota return result; @@ -1263,7 +1263,8 @@ export class EnviaProcessor { private getTotal(total: Total) { return { - ICMSTot: total.icmsTot + ICMSTot: total.icmsTot, + ...(total.IBSCBSTot ? { IBSCBSTot: total.IBSCBSTot } : {}) } }