/* * 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, NFEInutilizacaoServiceImpl, SaveFilesImpl } from '@treeunfe/types/interfaces'; import { InutilizacaoData } from '@treeunfe/types'; import { AxiosInstance } from 'axios'; class NFEInutilizacaoService extends BaseNFe implements NFEInutilizacaoServiceImpl { constructor(environment: Environment, utility: Utility, xmlBuilder: XmlBuilder, axios: AxiosInstance, saveFiles: SaveFilesImpl, gerarConsulta: GerarConsultaImpl) { super(environment, utility, xmlBuilder, 'NFEInutilizacao', axios, saveFiles, gerarConsulta); } protected gerarXml(chave: InutilizacaoData): string { return this.gerarXmlNFeInutilizacao(chave); } /** * Método para criação do ID de Inutilização */ private criarId(codigoUF: number, ano: string, cnpj: string, modelo: string, serie: string, numeroInicial: string, numeroFinal: string) { const id = `ID${codigoUF}${ano}${cnpj}${modelo}${serie}${numeroInicial}${numeroFinal}`; return id; } /** * Sobrescreve o método pai para determinar o modelo baseado nos dados de inutilização */ protected getModelo(data?: InutilizacaoData): string { if (data?.mod) { return data.mod === '55' ? 'NFe' : 'NFCe'; } return super.getModelo(data); // usa o comportamento padrão } /** * Método utilitário para criação do XML a partir de um Objeto */ gerarXmlNFeInutilizacao(data: InutilizacaoData) { logger.info('Montando estrutuda do XML em JSON', { context: 'NFEInutilizacaoService', }); const { ambiente } = this.environment.getConfig(); const { cUF, ano, CNPJ, mod, serie, nNFIni, nNFFin, xJust } = data; const anoFormatado = ano.toString().slice(-2); const serieFormatada = serie.padStart(3, '0') const numNFIniFormatado = nNFIni.padStart(9, '0') const numNFFinFormatado = nNFFin.padStart(9, '0') const id = this.criarId(cUF, anoFormatado, CNPJ, mod, serieFormatada, numNFIniFormatado, numNFFinFormatado); const xServ = 'INUTILIZAR'; if ([55, 65].indexOf(Number(mod)) === -1) { throw new Error(`Modelo do documento para inutilização deve ser 55 ou 65. Modelo informado: ${mod}`); } const xmlObject = { $: { versao: "4.00", xmlns: 'http://www.portalfiscal.inf.br/nfe' }, infInut: { $: { Id: `${id}`, }, tpAmb: ambiente, xServ, cUF, ano, CNPJ, mod, serie, nNFIni, nNFFin, xJust, } } // Gerar XML const xml = this.xmlBuilder.gerarXml(xmlObject, 'inutNFe', this.metodo) // Assinar XML return this.xmlBuilder.assinarXML(xml, 'infInut'); } } export default NFEInutilizacaoService;