/* * 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 { Environment, GerarConsulta, applyDefaultTmpStoragePaths, logger, SaveFiles, Utility, XmlBuilder, } from '@treeunfe/shared'; import { ConsultaCTe, CTeConfig, DFePorNSUCTe, DFePorUltimoNSUCTe, } from '@treeunfe/types'; import type { AxiosInstance } from 'axios'; import { CTEDistribuicaoDFe, } from '../operations'; import CTEDistribuicaoDFePorNSU from '../operations/CTEDistribuicaoDFe/CTEDistribuicaoDFePorNSU.js'; import CTEDistribuicaoDFePorUltNSU from '../operations/CTEDistribuicaoDFe/CTEDistribuicaoDFePorUltNSU.js'; import { CTEDistribuicaoDFeService } from '../services'; export default class CTe { private environment: Environment; private axios!: AxiosInstance; private utility!: Utility; private xmlBuilder!: XmlBuilder; private saveFiles!: SaveFiles; private gerarConsulta!: GerarConsulta; private loadEnvironmentPromise: Promise; constructor(config: CTeConfig) { // Valida se a configuração obrigatória foi fornecida if (!config.ambiente) { throw new Error('Configuração CTe incompleta. Por favor, forneça "ambiente".'); } const environment = new Environment(applyDefaultTmpStoragePaths(config, 'CTe')); this.environment = environment; // Carrega o environment automaticamente this.loadEnvironmentPromise = (async () => { try { const { axios: axiosInstance } = await environment.loadEnvironment(); this.axios = axiosInstance; this.utility = new Utility(environment); this.saveFiles = new SaveFiles(environment, this.utility); this.xmlBuilder = new XmlBuilder(environment); this.gerarConsulta = new GerarConsulta(environment, this.utility, this.xmlBuilder); } catch (error) { logger.error(``, error, { context: 'CTE_LoadEnvironment' }); throw new Error(`Erro ao inicializar a lib CTe: ${error}`); } })(); } /** * Distribuição DFe CTe */ async DistribuicaoDFe(data: ConsultaCTe) { await this.loadEnvironmentPromise; try { const service = new CTEDistribuicaoDFeService(this.environment, this.utility, this.xmlBuilder, this.axios, this.saveFiles, this.gerarConsulta); const operation = new CTEDistribuicaoDFe(service); const response = await operation.Exec(data); console.log('Retorno CTE_DistribuicaoDFe'); console.log(` ${response.xMotivo}`); console.log('==================================='); return response.data; } catch (error: any) { logger.error(``, error, { context: 'CTE_DistribuicaoDFe' }); throw new Error(`CTE_DistribuicaoDFe: ${error.message}`); } } async DistribuicaoDFePorUltNSU(data: DFePorUltimoNSUCTe) { await this.loadEnvironmentPromise; try { const service = new CTEDistribuicaoDFePorUltNSU(this.environment, this.utility, this.xmlBuilder, this.axios, this.saveFiles, this.gerarConsulta); const response = await service.Exec(data); console.log('Retorno CTE_DistribuicaoDFePorUltNSU'); console.log(` ${response.xMotivo}`); console.log('==================================='); return response.data; } catch (error: any) { logger.error(``, error, { context: 'CTE_DistribuicaoDFePorUltNSU' }); throw new Error(`CTE_DistribuicaoDFePorUltNSU: ${error.message}`); } } async DistribuicaoDFePorNSU(data: DFePorNSUCTe) { await this.loadEnvironmentPromise; try { const service = new CTEDistribuicaoDFePorNSU(this.environment, this.utility, this.xmlBuilder, this.axios, this.saveFiles, this.gerarConsulta); const response = await service.Exec(data); console.log('Retorno CTE_DistribuicaoDFePorNSU'); console.log(` ${response.xMotivo}`); console.log('==================================='); return response.data; } catch (error: any) { logger.error(``, error, { context: 'CTE_DistribuicaoDFePorNSU' }); throw new Error(`CTE_DistribuicaoDFePorNSU: ${error.message}`); } } }