/*
* 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 { ConsultaCadastroNFe } from '@treeunfe/types';
import { GerarConsultaImpl, NfeConsultaCadastroServiceImpl, SaveFilesImpl } from '@treeunfe/types/interfaces';
import { AxiosInstance } from 'axios';
const METHOD_NAME = 'NfeConsultaCadastro';
class NfeConsultaCadastroService extends BaseNFe implements NfeConsultaCadastroServiceImpl {
constructor(environment: Environment, utility: Utility, xmlBuilder: XmlBuilder, axios: AxiosInstance, saveFiles: SaveFilesImpl, gerarConsulta: GerarConsultaImpl) {
super(environment, utility, xmlBuilder, METHOD_NAME, axios, saveFiles, gerarConsulta);
}
protected gerarXml(data: ConsultaCadastroNFe): string {
logger.info('Montando estrutuda do XML em JSON', {
context: 'NfeConsultaCadastroService',
});
const ie = data.IE?.trim() ?? '';
const cnpj = data.CNPJ?.trim() ?? '';
const cpf = data.CPF?.trim() ?? '';
const filled = [ie, cnpj, cpf].filter((v) => v.length > 0).length;
if (filled !== 1) {
throw new Error('Consulta cadastro: informe exatamente um dos campos IE, CNPJ ou CPF.');
}
if (!data.UF?.trim()) {
throw new Error('Consulta cadastro: UF é obrigatória.');
}
const infCons: Record = {
xServ: 'CONS-CAD',
UF: data.UF.trim(),
};
if (ie) {
infCons.IE = ie;
} else if (cnpj) {
infCons.CNPJ = cnpj.replace(/\D/g, '');
} else {
infCons.CPF = cpf.replace(/\D/g, '');
}
const xmlObject = {
$: {
versao: '2.00',
xmlns: 'http://www.portalfiscal.inf.br/nfe',
},
infCons,
};
return this.xmlBuilder.gerarXml(xmlObject, 'ConsCad', this.metodo);
}
}
export default NfeConsultaCadastroService;