Skip to content

Consulta Situacao CTe

Samuel Oliveira edited this page Nov 7, 2017 · 4 revisions

Situação Cte

Função para consultar a Situação da Cte na Sefaz:

import br.com.samuelweb.certificado.exception.CertificadoException;
import br.com.samuelweb.cte.Cte;
import br.com.samuelweb.cte.dom.ConfiguracoesIniciais;
import br.com.samuelweb.cte.exception.CteException;
import br.com.samuelweb.cte.util.ObjetoUtil;
import br.com.samuelweb.cte.util.XmlUtil;
import br.inf.portalfiscal.cte.schema_300.enviCTe.TEnviCTe;
import br.inf.portalfiscal.cte.schema_300.procCTe.TProtCTe;
import br.inf.portalfiscal.cte.schema_300.retConsSitCTe.TRetConsSitCTe;
import javax.xml.bind.JAXBException;

/**
 * @author Samuel Oliveira
 *
 */
public class ConsultaCte {

public static void main(String[] args) {

	try {
		//Veja https://github.com/Samuel-Oliveira/Java_CTe/wiki/Configura%C3%A7%C3%B5es-CTe
        ConfiguracoesIniciais config = ConfigTeste.iniciaConfiguracoes();

        String chave = ""; // TODO Preencha A Chave

        TRetConsSitCTe retorno = Cte.consultaXml(chave);

		System.out.println("Status:" + retorno.getCStat());
		System.out.println("Motivo:" + retorno.getXMotivo());

		// Transforma O ProtCte do Retorno em XML
		String infProt = ObjetoUtil.elementToString(retorno.getProtCTe().getAny());
		TProtCTe tProtCTe = new TProtCTe();
		tProtCTe.setVersao(retorno.getProtCTe().getVersao());
		tProtCTe.setInfProt( XmlUtil.xmlToObject(infProt, TProtCTe.InfProt.class));

		//Pega a NOta Assinado e Adiciona o Protocolo. Transformando ela em ProcNfe.
		TEnviCTe enviCte = XmlUtil.xmlToObject(XmlUtil.leXml("d:/teste/cte/teste.xml"), TEnviCTe.class);
		System.out.println("Xml Final: " + XmlUtil.criaCteProc(enviCte, tProtCTe));

	} catch (CteException | JAXBException | CertificadoException e) {
		System.out.println("Erro:" + e.getMessage());
	}


}
}
Clone this wiki locally