/* * This file is part of Treeunfe DFe. * * Treeunfe DFe is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Treeunfe DFe is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Treeunfe DFe. If not, see . */ import { XmlBuilder, BaseNFe, Environment, Utility, logger } from '@treeunfe/shared'; import { GerarConsultaImpl, SaveFilesImpl } from '@treeunfe/types/interfaces'; import { GenericObject } from '@treeunfe/types/common'; import { ConsultaCTe } from '@treeunfe/types/cte'; import { AxiosInstance, AxiosResponse } from 'axios'; import { Agent } from 'http'; import DistribuicaoHandler from './util/DistribuicaoHandler.js'; class CTEDistribuicaoDFeService extends BaseNFe { constructor(environment: Environment, utility: Utility, xmlBuilder: XmlBuilder, axios: AxiosInstance, saveFiles: SaveFilesImpl, gerarConsulta: GerarConsultaImpl) { super(environment, utility, xmlBuilder, 'CTeDistribuicaoDFe', axios, saveFiles, gerarConsulta); } protected gerarXml(data: ConsultaCTe): string { return this.gerarXmlCTeDistribuicaoDFe(data); } gerarXmlCTeDistribuicaoDFe(data: ConsultaCTe) { logger.info('Montando estrutura do XML em JSON', { context: 'CTEDistribuicaoDFeService', }); const { ambiente } = this.environment.getConfig(); // XML para CTe - versão 1.00 const xmlObject = { $: { versao: "1.00", xmlns: 'http://www.portalfiscal.inf.br/cte' }, tpAmb: ambiente, ...data, } return this.xmlBuilder.gerarXml(xmlObject, 'distDFeInt', this.metodo) } protected async callWebService(xmlConsulta: string, webServiceUrl: string, ContentType: string, action: string, agent: Agent): Promise> { const startTime = Date.now(); const headers = { 'Content-Type': ContentType, }; logger.http('Iniciando comunicação com o webservice', { context: `CTEDistribuicaoDFeService`, method: this.metodo, url: webServiceUrl, action, headers, }); const response = await this.axios.post(webServiceUrl, xmlConsulta, { headers, httpsAgent: agent }); const duration = Date.now() - startTime; logger.http('Comunicação concluída com sucesso', { context: `CTEDistribuicaoDFeService`, method: this.metodo, duration: `${duration}ms`, responseSize: response.data ? JSON.stringify(response.data).length : 0 }); return response; } async Exec(data: ConsultaCTe) { let xmlConsulta: string = ''; let xmlConsultaSoap: string = ''; // webServiceUrlTmp não é usado, mas mantido para compatibilidade // let webServiceUrlTmp: string = ''; let responseInJson: GenericObject | undefined = undefined; let xmlRetorno: AxiosResponse = {} as AxiosResponse; const ContentType = this.setContentType(); try { // Gerando XML para consulta de Distribuição DFe do CTe xmlConsulta = this.gerarXmlCTeDistribuicaoDFe(data); console.log({ xmlConsulta }) const { xmlFormated, agent, webServiceUrl, action } = await this.gerarConsulta.gerarConsulta(xmlConsulta, this.metodo, true, '1.00', 'CTe', true, 'cteDistDFeInteresse', 'cteDadosMsg'); xmlConsultaSoap = xmlFormated; // webServiceUrlTmp não é usado, mas mantido para compatibilidade // webServiceUrlTmp = webServiceUrl; const xmlRetorno = await this.callWebService(xmlFormated, webServiceUrl, ContentType, action, agent); // Verifica se houve rejeição responseInJson = this.utility.verificaRejeicao(xmlRetorno.data, this.metodo); if (responseInJson.retDistDFeInt.cStat === '137') { return { data: {} as GenericObject, xMotivo: responseInJson.retDistDFeInt.xMotivo, filesList: [], } } /** * Descompacta XML * Converte XML para Json * Salva XML * Gera erro em caso de rejeição */ const handlerDistribuicao = new DistribuicaoHandler(this.environment, this.utility, this.metodo); const filesList = handlerDistribuicao.deCompressDFeXML(xmlRetorno.data, this.metodo, xmlConsulta); const xMotivo = this.utility.findInObj(responseInJson, 'xMotivo'); return { data: responseInJson, xMotivo, filesList, } } finally { // Salva XML de Consulta this.utility.salvaConsulta(xmlConsulta, xmlConsultaSoap, this.metodo); // Salva XML de Retorno this.utility.salvaRetorno(xmlRetorno.data, responseInJson, this.metodo); } } } export default CTEDistribuicaoDFeService;