Skip to content

Commit 7e14490

Browse files
authored
Merge pull request lealhugui#6 from tfiliano/salvando_arquivos
salvando arquivos de distribuicao e retorno de transmissao quando der…
2 parents 4bde192 + 1133656 commit 7e14490

File tree

5 files changed

+83
-25
lines changed

5 files changed

+83
-25
lines changed

nfetest.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ async function testeConsultaRecibo() {
334334

335335
// testeConsultaRecibo()
336336
// testeEmissaoNFCeContingenciaOffline(empresa);
337-
// testeEmissaoNFe();
337+
testeEmissaoNFe();
338338
// testeEventoCancelar()
339339
// testeEventoCartaCorrecao()
340-
testeInutilizacao()
340+
// testeInutilizacao()
341341

342342
// TRANSFORMAR CERTIFICADO .PEM, REMOVER CHAVE....
343343
// openssl pkcs12 -in mycaservercert.pfx -nokeys -out mycaservercert.pem

src/factory/processor/enviaProcessor.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
InfoAdicional, DetalhesProduto, Imposto, Icms, Cofins, Pis, IcmsTot, IssqnTot, DetalhePagamento, DetalhePgtoCartao, RetornoContingenciaOffline, ResponsavelTecnico, ServicosSefaz, II, PisST, Ipi, CofinsST, IcmsUfDest, impostoDevol, Configuracoes
44
} from '../interface/nfe';
55

6-
import { WebServiceHelper, WebProxy } from "../webservices/webserviceHelper";
6+
import { WebServiceHelper } from "../webservices/webserviceHelper";
77
import * as schema from '../schema/index';
88
import { XmlHelper } from '../xmlHelper';
99
import * as Utils from '../utils/utils';
1010
import { Signature } from '../signature';
1111
import { SefazNFCe } from '../webservices/sefazNfce';
1212
import { SefazNFe } from '../webservices/sefazNfe';
13+
import * as fs from 'fs';
14+
import * as path from 'path';
1315

1416
const sha1 = require('sha1');
1517

@@ -48,6 +50,14 @@ export class EnviaProcessor {
4850
if (!this.configuracoes.webservices) this.configuracoes.webservices = { tentativas: 3, aguardarConsultaRetorno: 1000 };
4951
if (!this.configuracoes.webservices.tentativas) this.configuracoes.webservices.tentativas = 3;
5052
if (!this.configuracoes.webservices.aguardarConsultaRetorno) this.configuracoes.webservices.aguardarConsultaRetorno = 1000;
53+
if (this.configuracoes.arquivos) {
54+
if (this.configuracoes.arquivos.pastaEnvio && (!'/\\'.includes(this.configuracoes.arquivos.pastaEnvio.substr(-1))))
55+
this.configuracoes.arquivos.pastaEnvio = this.configuracoes.arquivos.pastaEnvio + path.sep;
56+
if (this.configuracoes.arquivos.pastaRetorno && (!'/\\'.includes(this.configuracoes.arquivos.pastaRetorno.substr(-1))))
57+
this.configuracoes.arquivos.pastaRetorno = this.configuracoes.arquivos.pastaRetorno + path.sep;
58+
if (this.configuracoes.arquivos.pastaXML && (!'/\\'.includes(this.configuracoes.arquivos.pastaXML.substr(-1))))
59+
this.configuracoes.arquivos.pastaXML = this.configuracoes.arquivos.pastaXML + path.sep;
60+
}
5161
}
5262

5363
/**
@@ -56,7 +66,6 @@ export class EnviaProcessor {
5666
* @param assincrono Boolean para definir se a execução sera sincrona ou assincrona, por padrao === sincrona!
5767
*/
5868
public async executar(documento: NFeDocumento | NFCeDocumento, assincrono: boolean = false) {
59-
6069
let result = <RetornoProcessamentoNF>{
6170
success: false
6271
};
@@ -71,6 +80,7 @@ export class EnviaProcessor {
7180
xmlAssinado = appendQRCode.xml;
7281
doc.nfe.infNFeSupl = appendQRCode.qrCode;
7382
}
83+
console.log('ASSINADO============================', xmlAssinado)
7484

7585
let xmlLote = this.gerarXmlLote(xmlAssinado, assincrono);
7686

@@ -81,7 +91,6 @@ export class EnviaProcessor {
8191
} else {
8292
result = await this.transmitirXml(xmlLote, doc.nfe);
8393
}
84-
8594
} catch (ex) {
8695
result.success = false;
8796
result.error = ex;
@@ -1349,4 +1358,4 @@ export class EnviaProcessor {
13491358
return Buffer.from(sha1(CSRT + chave), 'hex').toString('base64');
13501359
}
13511360

1352-
}
1361+
}

src/factory/processor/nfeProcessor2.ts

+48
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import { EventoProcessor } from './eventoProcessor';
88
import { InutilizaProcessor } from './inutilizaProcessor';
99
import { Inutilizar } from '../interface/inutilizacao';
1010
import { Evento } from '../interface';
11+
import { XmlHelper } from '../xmlHelper';
12+
import * as Utils from '../utils/utils';
13+
import * as fs from 'fs';
14+
import * as path from 'path';
15+
import * as schema from '../schema';
16+
1117

1218

1319
/**
@@ -26,6 +32,15 @@ export class NFeProcessor {
2632
if (!this.configuracoes.webservices) this.configuracoes.webservices = { tentativas: 3, aguardarConsultaRetorno: 1000 };
2733
if (!this.configuracoes.webservices.tentativas) this.configuracoes.webservices.tentativas = 3;
2834
if (!this.configuracoes.webservices.aguardarConsultaRetorno) this.configuracoes.webservices.aguardarConsultaRetorno = 1000;
35+
if (this.configuracoes.arquivos) {
36+
if (this.configuracoes.arquivos.pastaEnvio && (!'/\\'.includes(this.configuracoes.arquivos.pastaEnvio.substr(-1))))
37+
this.configuracoes.arquivos.pastaEnvio = this.configuracoes.arquivos.pastaEnvio + path.sep;
38+
if (this.configuracoes.arquivos.pastaRetorno && (!'/\\'.includes(this.configuracoes.arquivos.pastaRetorno.substr(-1))))
39+
this.configuracoes.arquivos.pastaRetorno = this.configuracoes.arquivos.pastaRetorno + path.sep;
40+
if (this.configuracoes.arquivos.pastaXML && (!'/\\'.includes(this.configuracoes.arquivos.pastaXML.substr(-1))))
41+
this.configuracoes.arquivos.pastaXML = this.configuracoes.arquivos.pastaXML + path.sep;
42+
}
43+
2944

3045
this.retornoProcessor = new RetornoProcessor(this.configuracoes);
3146
this.enviaProcessor = new EnviaProcessor(this.configuracoes);
@@ -43,6 +58,7 @@ export class NFeProcessor {
4358
}
4459

4560
public async executar(documento: NFeDocumento | NFCeDocumento, assincrono: boolean = false) {
61+
const { arquivos } = this.configuracoes;
4662
let result = <RetornoProcessamentoNF>{};
4763
try {
4864
result = <RetornoProcessamentoNF>await this.enviaProcessor.executar(documento);
@@ -66,6 +82,38 @@ export class NFeProcessor {
6682
result.confirmada = true;
6783
result.success = true;
6884
}
85+
86+
if (arquivos.salvar) {
87+
if (! await fs.existsSync(arquivos.pastaEnvio)) await fs.mkdirSync(arquivos.pastaEnvio, { recursive: true });
88+
if (! await fs.existsSync(arquivos.pastaRetorno)) await fs.mkdirSync(arquivos.pastaRetorno, { recursive: true });
89+
if (!await fs.existsSync(arquivos.pastaXML)) await fs.mkdirSync(arquivos.pastaXML, { recursive: true });
90+
91+
if ((result.success == true) && (retConsReciNFe.protNFe.infProt.cStat == '100')) {
92+
const filename = `${arquivos.pastaXML}${retConsReciNFe.protNFe.infProt.chNFe}-procNFe.xml`;
93+
94+
const nfe_enviada = Object(XmlHelper.deserializeXml(result.envioNF.xml_enviado, { explicitArray: false }));
95+
const nfeProc = <schema.TNfeProc>{
96+
$: { versao: "1.00", xmlns: "http://www.portalfiscal.inf.br/nfe" },
97+
NFe: nfe_enviada.enviNFe.NFe,
98+
protNFe: retConsReciNFe.protNFe
99+
};
100+
101+
Utils.removeSelfClosedFields(nfeProc);
102+
let xmlNfeProc = XmlHelper.serializeXml(nfeProc, 'nfeProc');
103+
104+
await fs.writeFileSync(filename, xmlNfeProc);
105+
} else {
106+
const filenameEnvio = `${arquivos.pastaEnvio}${retEnviNFe.infRec.nRec}-enviNFe.xml`;
107+
const filenameRetorno = `${arquivos.pastaRetorno}${retEnviNFe.infRec.nRec}-retEnviNFe.xml`;
108+
await fs.writeFileSync(filenameEnvio, result.envioNF.xml_enviado);
109+
await fs.writeFileSync(filenameRetorno, result.envioNF.xml_recebido);
110+
111+
const filenameConsultaEnvio = `${arquivos.pastaEnvio}${retConsReciNFe.nRec}-consReciNFe.xml`;
112+
const filenameConsultaRetorno = `${arquivos.pastaRetorno}${retConsReciNFe.nRec}-retConsReciNFe.xml`;
113+
await fs.writeFileSync(filenameConsultaEnvio, result.consultaProc.xml_enviado);
114+
await fs.writeFileSync(filenameConsultaRetorno, result.consultaProc.xml_recebido);
115+
}
116+
}
69117
} else {
70118
throw new Error('Erro ao realizar requisição');
71119
}

src/factory/schema/index.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export enum TEnviNFeIndSinc {
105105
[System.Xml.Serialization.XmlRootAttribute("NFe", Namespace="http://www.portalfiscal.inf.br/nfe", IsNullable=false)]
106106
*/
107107
export interface TNFe {
108-
$: { xmlns: string },
108+
$: { versao: string, xmlns: string },
109109
infNFe: TNFeInfNFe;
110110
infNFeSupl: TNFeInfNFeSupl;
111111
//[System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.w3.org/2000/09/xmldsig#")]
@@ -2535,31 +2535,32 @@ export interface TRetInutNFeInfInut {
25352535
//[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.portalfiscal.inf.br/nfe")]
25362536
//[System.Xml.Serialization.XmlRootAttribute("nfeProc", Namespace="http://www.portalfiscal.inf.br/nfe", IsNullable=false)]
25372537
export interface TNfeProc {
2538-
nFeField: TNFe;
2539-
protNFeField: TProtNFe;
2540-
versaoField: string;
2538+
NFe: TNFe;
2539+
protNFe: TProtNFe;
2540+
$: { versao: string, xmlns: string };
2541+
_: string;
25412542
}
25422543

25432544
//[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.portalfiscal.inf.br/nfe")]
25442545
export interface TProtNFe {
2545-
infProtField: TProtNFeInfProt;
2546-
signatureField: SignatureType;
2547-
versaoField: string;
2546+
infProt: TProtNFeInfProt;
2547+
signature: SignatureType;
2548+
versao: string;
25482549
}
25492550

25502551
//[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.portalfiscal.inf.br/nfe")]
25512552
export interface TProtNFeInfProt {
2552-
tpAmbField: TAmb;
2553-
verAplicField: string;
2554-
chNFeField: string;
2555-
dhRecbtoField: string;
2556-
nProtField: string;
2557-
digValField: number[];
2558-
cStatField: string;
2559-
xMotivoField: string;
2560-
cMsgField: string;
2561-
xMsgField: string;
2562-
idField: string;
2553+
tpAmb: TAmb;
2554+
verAplic: string;
2555+
chNFe: string;
2556+
dhRecbto: string;
2557+
nProt: string;
2558+
digVal: number[];
2559+
cStat: string;
2560+
xMotivo: string;
2561+
cMsg: string;
2562+
xMsg: string;
2563+
Id: string;
25632564
}
25642565

25652566
//[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.portalfiscal.inf.br/nfe")]

src/factory/webservices/webserviceHelper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export abstract class WebServiceHelper {
126126
result.success = true;
127127

128128
//let retorno = (require('util').inspect(XmlHelper.deserializeXml(res.data), false, null));
129-
let retorno = XmlHelper.deserializeXml(result.xml_recebido);
129+
let retorno = XmlHelper.deserializeXml(result.xml_recebido, {explicitArray: false});
130130
if (retorno) {
131131
//result.data = retorno;
132132
result.data = Object(retorno)['soap:Envelope']['soap:Body']['nfeResultMsg'];

0 commit comments

Comments
 (0)