@@ -8,6 +8,12 @@ import { EventoProcessor } from './eventoProcessor';
8
8
import { InutilizaProcessor } from './inutilizaProcessor' ;
9
9
import { Inutilizar } from '../interface/inutilizacao' ;
10
10
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
+
11
17
12
18
13
19
/**
@@ -26,6 +32,15 @@ export class NFeProcessor {
26
32
if ( ! this . configuracoes . webservices ) this . configuracoes . webservices = { tentativas : 3 , aguardarConsultaRetorno : 1000 } ;
27
33
if ( ! this . configuracoes . webservices . tentativas ) this . configuracoes . webservices . tentativas = 3 ;
28
34
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
+
29
44
30
45
this . retornoProcessor = new RetornoProcessor ( this . configuracoes ) ;
31
46
this . enviaProcessor = new EnviaProcessor ( this . configuracoes ) ;
@@ -43,6 +58,7 @@ export class NFeProcessor {
43
58
}
44
59
45
60
public async executar ( documento : NFeDocumento | NFCeDocumento , assincrono : boolean = false ) {
61
+ const { arquivos } = this . configuracoes ;
46
62
let result = < RetornoProcessamentoNF > { } ;
47
63
try {
48
64
result = < RetornoProcessamentoNF > await this . enviaProcessor . executar ( documento ) ;
@@ -66,6 +82,38 @@ export class NFeProcessor {
66
82
result . confirmada = true ;
67
83
result . success = true ;
68
84
}
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
+ }
69
117
} else {
70
118
throw new Error ( 'Erro ao realizar requisição' ) ;
71
119
}
0 commit comments