Skip to content

Commit

Permalink
Correções diversas no CT-e (#918)
Browse files Browse the repository at this point in the history
* Corrigido schema cteTiposBasico_v4.00.xsd alterado indevidamente

* Alterado CTeGeraChave e CTeGeraQRCode para aceitar CT-e OS

* Adicionado CTeGeraChave e CTeGeraQRCode para o CT-e 4.00

* Corrigido para usar a url correta de recepção do CT-e OS 4.00 e corrigido nome do método

* Alteradas tags de evento para não serem obrigatórias pois na consulta de CT-e a SEFAZ pode retornar eventos de marcação que não estão atualmente mapeados como, por exemplo, o evento "240130 - Autorizado CTe complementar" que vem com uma tag "evCTeComplementar" que não existe nos schemas do CT-e

* A IE e a UF dentro de emiDocAnt (dados do emitente do documento de transporte anterior) são opcionais no CT-e de acordo com a definição dos schemas

* Alterado atributo "versao" do CTeNotaConsultaRetorno para não ser obrigatório porque algumas UF não retornam esse atributo (mesmo sendo obrigatório nos schemas)

* Mapeados atributos opcionais das tags cteProc e procEventoCTe que só estão documentadas no schema XSD

* Ajustados stubs para não gerar prefixo de namespace nas tags pois a SEFAZ-MS estava recusando com "404 - Rejeicao: Uso de prefixo de namespace nao permitido"

---------

Co-authored-by: Diego Fincatto <[email protected]>
  • Loading branch information
felipebz and fincatto authored Sep 4, 2023
1 parent 45849fe commit d4b823d
Show file tree
Hide file tree
Showing 24 changed files with 406 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,26 @@
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementUnion;
import org.simpleframework.xml.Root;

import java.math.BigDecimal;

@Root(strict = false)
public class CTeDetalhamentoEvento extends DFBase {
private static final long serialVersionUID = 7006866358832001912L;

@Attribute(name = "versaoEvento")
private String versaoEvento;

@ElementUnion({
@Element(name = "evCancCECTe", type = CTeEnviaEventoCancelamentoComprovanteEntrega.class),
@Element(name = "evCancCTe", type = CTeEnviaEventoCancelamento.class),
@Element(name = "evCCeCTe", type = CTeEnviaEventoCartaCorrecao.class),
@Element(name = "evCECTe", type = CTeEnviaEventoComprovanteEntrega.class),
@Element(name = "evEPECCTe", type = CTeEnviaEventoEpec.class),
@Element(name = "evPrestDesacordo", type = CTeEnviaEventoPrestacaoEmDesacordo.class),
@Element(name = "evGTV", type = CTeEnviaEventoGtv.class),
@Element(name = "evRegMultimodal", type = CTeEnviaEventoRegistroMultimodal.class),
@Element(name = "evCancCECTe", type = CTeEnviaEventoCancelamentoComprovanteEntrega.class, required = false),
@Element(name = "evCancCTe", type = CTeEnviaEventoCancelamento.class, required = false),
@Element(name = "evCCeCTe", type = CTeEnviaEventoCartaCorrecao.class, required = false),
@Element(name = "evCECTe", type = CTeEnviaEventoComprovanteEntrega.class, required = false),
@Element(name = "evEPECCTe", type = CTeEnviaEventoEpec.class, required = false),
@Element(name = "evPrestDesacordo", type = CTeEnviaEventoPrestacaoEmDesacordo.class, required = false),
@Element(name = "evGTV", type = CTeEnviaEventoGtv.class, required = false),
@Element(name = "evRegMultimodal", type = CTeEnviaEventoRegistroMultimodal.class, required = false),
})
private CTeTipoEvento evento;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class CTeNotaInfoCTeNormalDocumentosAnterioresEmissorDocumentosAnteriores
@Element(name = "CPF", required = false)
private String cpf;

@Element(name = "IE")
@Element(name = "IE", required = false)
private String inscricaoEstadual;

@Element(name = "UF")
@Element(name = "UF", required = false)
private String siglaUF;

@Element(name = "xNome")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.fincatto.documentofiscal.cte300.classes.enviolote.consulta.CTeProtocolo;
import org.simpleframework.xml.Namespace;

import java.time.ZonedDateTime;

/**
* Created by Eldevan Nery Junior on 09/10/17.
*/
Expand All @@ -22,6 +24,12 @@ public class CTeProcessado extends DFBase {
@Attribute(name = "ipTransmissor", required = false)
private String ipTransmissor;

@Attribute(name = "nPortaCon", required = false)
private String portaConexao;

@Attribute(name = "dhConexao", required = false)
private ZonedDateTime dataHoraConexao;

@Attribute(name = "versao")
private String versao;

Expand All @@ -39,6 +47,22 @@ public void setIpTransmissor(final String ipTransmissor) {
this.ipTransmissor = ipTransmissor;
}

public String getPortaConexao() {
return portaConexao;
}

public void setPortaConexao(String portaConexao) {
this.portaConexao = portaConexao;
}

public ZonedDateTime getDataHoraConexao() {
return dataHoraConexao;
}

public void setDataHoraConexao(ZonedDateTime dataHoraConexao) {
this.dataHoraConexao = dataHoraConexao;
}

public String getVersao() {
return this.versao;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class CTeNotaConsultaRetorno extends DFBase {
private static final long serialVersionUID = 3229234247371007557L;

@Attribute(name = "versao")
@Attribute(name = "versao", required = false)
private String versao;

@Element(name = "tpAmb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.simpleframework.xml.Root;

import java.math.BigDecimal;
import java.time.ZonedDateTime;

@Root(name = "procEventoCTe")
@Namespace(reference = "http://www.portalfiscal.inf.br/cte")
Expand All @@ -18,6 +19,15 @@ public class CTeProtocoloEvento extends DFBase {

@Attribute(name = "versao")
private String versao;

@Attribute(name = "ipTransmissor", required = false)
private String ipTransmissor;

@Attribute(name = "nPortaCon", required = false)
private String portaConexao;

@Attribute(name = "dhConexao", required = false)
private ZonedDateTime dataHoraConexao;

@Element(name = "eventoCTe")
private CTeEvento evento;
Expand All @@ -33,6 +43,30 @@ public void setVersao(final BigDecimal versao) {
this.versao = DFBigDecimalValidador.tamanho4Com2CasasDecimais(versao, "Versao");
}

public String getIpTransmissor() {
return ipTransmissor;
}

public void setIpTransmissor(String ipTransmissor) {
this.ipTransmissor = ipTransmissor;
}

public String getPortaConexao() {
return portaConexao;
}

public void setPortaConexao(String portaConexao) {
this.portaConexao = portaConexao;
}

public ZonedDateTime getDataHoraConexao() {
return dataHoraConexao;
}

public void setDataHoraConexao(ZonedDateTime dataHoraConexao) {
this.dataHoraConexao = dataHoraConexao;
}

public CTeEvento getEvento() {
return this.evento;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

import java.time.ZonedDateTime;

@Root(name = "cteOSProc")
@Namespace(reference = CTeConfig.NAMESPACE)
public class CTeOSProcessado extends DFBase {
Expand All @@ -19,6 +21,12 @@ public class CTeOSProcessado extends DFBase {
@Attribute(name = "ipTransmissor", required = false)
private String ipTransmissor;

@Attribute(name = "nPortaCon", required = false)
private String portaConexao;

@Attribute(name = "dhConexao", required = false)
private ZonedDateTime dataHoraConexao;

@Attribute(name = "versao")
private String versao;

Expand All @@ -36,6 +44,22 @@ public void setIpTransmissor(final String ipTransmissor) {
this.ipTransmissor = ipTransmissor;
}

public String getPortaConexao() {
return portaConexao;
}

public void setPortaConexao(String portaConexao) {
this.portaConexao = portaConexao;
}

public ZonedDateTime getDataHoraConexao() {
return dataHoraConexao;
}

public void setDataHoraConexao(ZonedDateTime dataHoraConexao) {
this.dataHoraConexao = dataHoraConexao;
}

public String getVersao() {
return this.versao;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package com.fincatto.documentofiscal.cte300.utils;

import org.apache.commons.lang3.StringUtils;

import com.fincatto.documentofiscal.cte300.classes.nota.CTeNota;
import com.fincatto.documentofiscal.cte300.classes.os.CTeOS;
import org.apache.commons.lang3.StringUtils;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.UUID;

public class CTeGeraChave {

private final CTeNota nota;

private final CTeOS cteOS;

public CTeGeraChave(final CTeNota nota) {
this.nota = nota;
this.cteOS = null;
}

public CTeGeraChave(final CTeOS cteOS) {
this.nota = null;
this.cteOS = cteOS;
}

public String geraCodigoRandomico() {
final Random random = new Random(UUID.randomUUID().timestamp());
long seed;
if (nota != null) {
seed = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
} else {
seed = this.cteOS.getInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
}
final Random random = new Random(seed);
return StringUtils.leftPad(String.valueOf(random.nextInt(100000000)), 8, "0");
}

Expand Down Expand Up @@ -46,9 +59,36 @@ public Integer getDV() {
}

private String geraChaveAcessoSemDV() {
if (StringUtils.isBlank(this.nota.getCteNotaInfo().getIdentificacao().getCodigoNumerico())) {
String codigoNumerico;
String codigoUF;
ZonedDateTime dataEmissao;
String cnpj;
String modelo;
String serie;
String numero;
String tipoEmissao;
if (nota != null) {
codigoUF = this.nota.getCteNotaInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
dataEmissao = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao();
cnpj = this.nota.getCteNotaInfo().getEmitente().getCnpj();
modelo = this.nota.getCteNotaInfo().getIdentificacao().getModelo().getCodigo();
serie = this.nota.getCteNotaInfo().getIdentificacao().getSerie().toString();
numero = this.nota.getCteNotaInfo().getIdentificacao().getNumero().toString();
tipoEmissao = this.nota.getCteNotaInfo().getIdentificacao().getTipoEmissao().getCodigo();
codigoNumerico = this.nota.getCteNotaInfo().getIdentificacao().getCodigoNumerico();
} else {
codigoUF = this.cteOS.getInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
dataEmissao = this.cteOS.getInfo().getIdentificacao().getDataEmissao();
cnpj = this.cteOS.getInfo().getEmitente().getCnpj();
modelo = this.cteOS.getInfo().getIdentificacao().getModelo().getCodigo();
serie = this.cteOS.getInfo().getIdentificacao().getSerie().toString();
numero = this.cteOS.getInfo().getIdentificacao().getNumero().toString();
tipoEmissao = this.cteOS.getInfo().getIdentificacao().getTipoEmissao().getCodigo();
codigoNumerico = this.cteOS.getInfo().getIdentificacao().getCodigoNumerico();
}
if (StringUtils.isBlank(codigoNumerico)) {
throw new IllegalStateException("Codigo numerico deve estar presente para gerar a chave de acesso");
}
return StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getCodigoUF().getCodigoIbge(), 2, "0") + StringUtils.leftPad(DateTimeFormatter.ofPattern("yyMM").format(this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao()), 4, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getEmitente().getCnpj(), 14, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getModelo().getCodigo(), 2, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getSerie() + "", 3, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getNumero() + "", 9, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getTipoEmissao().getCodigo(), 1, "0") + StringUtils.leftPad(this.nota.getCteNotaInfo().getIdentificacao().getCodigoNumerico(), 8, "0");
return StringUtils.leftPad(codigoUF, 2, "0") + StringUtils.leftPad(DateTimeFormatter.ofPattern("yyMM").format(dataEmissao), 4, "0") + StringUtils.leftPad(cnpj, 14, "0") + StringUtils.leftPad(modelo, 2, "0") + StringUtils.leftPad(serie, 3, "0") + StringUtils.leftPad(numero, 9, "0") + StringUtils.leftPad(tipoEmissao, 1, "0") + StringUtils.leftPad(codigoNumerico, 8, "0");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fincatto.documentofiscal.cte300.utils;

import com.fincatto.documentofiscal.cte.CTeConfig;
import com.fincatto.documentofiscal.cte.CTTipoEmissao;
import com.fincatto.documentofiscal.cte.CTeConfig;
import com.fincatto.documentofiscal.cte300.classes.CTAutorizador31;
import com.fincatto.documentofiscal.cte300.classes.nota.CTeNota;
import com.fincatto.documentofiscal.cte300.classes.os.CTeOS;
import com.fincatto.documentofiscal.utils.DFAssinaturaDigital;

public class CTeGeraQRCode {
Expand All @@ -14,8 +15,7 @@ public CTeGeraQRCode(CTeConfig config) {
this.config = config;
}

public String getQRCode(CTeNota cTeNota) throws Exception {
String chaveAcesso = cTeNota.getCteNotaInfo().getChaveAcesso();
private String getQRCode(String chaveAcesso) throws Exception {
String url = CTAutorizador31.valueOfChaveAcesso(chaveAcesso).getCteQrCode(this.config.getAmbiente());
final StringBuilder parametros = new StringBuilder();
parametros.append("chCTe=").append(chaveAcesso).append("&");
Expand All @@ -26,5 +26,13 @@ public String getQRCode(CTeNota cTeNota) throws Exception {
// retorna a url do qrcode
return url + "?" + parametros.toString();
}


public String getQRCode(CTeNota cteNota) throws Exception {
return getQRCode(cteNota.getCteNotaInfo().getChaveAcesso());
}

public String getQRCode(CTeOS cteOS) throws Exception {
return getQRCode(cteOS.getInfo().getChaveAcesso());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementUnion;
import org.simpleframework.xml.Root;

import java.math.BigDecimal;

@Root(strict = false)
public class CTeDetalhamentoEvento extends DFBase {
private static final long serialVersionUID = 7006866358832001912L;

@Attribute(name = "versaoEvento")
private String versaoEvento;

@ElementUnion({
@Element(name = "evCancCECTe", type = CTeEnviaEventoCancelamentoComprovanteEntrega.class),
@Element(name = "evCancCTe", type = CTeEnviaEventoCancelamento.class),
@Element(name = "evCCeCTe", type = CTeEnviaEventoCartaCorrecao.class),
@Element(name = "evEPECCTe", type = CTeEnviaEventoEpec.class),
@Element(name = "evCECTe", type = CTeEnviaEventoComprovanteEntrega.class),
@Element(name = "evPrestDesacordo", type = CTeEnviaEventoPrestacaoEmDesacordo.class),
@Element(name = "evGTV", type = CTeEnviaEventoGtv.class),
@Element(name = "evRegMultimodal", type = CTeEnviaEventoRegistroMultimodal.class),
@Element(name = "evCancPrestDesacordo", type = CTeEnviaEventoCancelamentoPrestacaoEmDesacordo.class),
@Element(name = "evIECTe", type = CTeEnviaEventoInsucessoEntrega.class),
@Element(name = "evCancIECTe", type = CTeEnviaEventoCancelamentoInsucessoEntrega.class),
@Element(name = "evCancCECTe", type = CTeEnviaEventoCancelamentoComprovanteEntrega.class, required = false),
@Element(name = "evCancCTe", type = CTeEnviaEventoCancelamento.class, required = false),
@Element(name = "evCCeCTe", type = CTeEnviaEventoCartaCorrecao.class, required = false),
@Element(name = "evEPECCTe", type = CTeEnviaEventoEpec.class, required = false),
@Element(name = "evCECTe", type = CTeEnviaEventoComprovanteEntrega.class, required = false),
@Element(name = "evPrestDesacordo", type = CTeEnviaEventoPrestacaoEmDesacordo.class, required = false),
@Element(name = "evGTV", type = CTeEnviaEventoGtv.class, required = false),
@Element(name = "evRegMultimodal", type = CTeEnviaEventoRegistroMultimodal.class, required = false),
@Element(name = "evCancPrestDesacordo", type = CTeEnviaEventoCancelamentoPrestacaoEmDesacordo.class, required = false),
@Element(name = "evIECTe", type = CTeEnviaEventoInsucessoEntrega.class, required = false),
@Element(name = "evCancIECTe", type = CTeEnviaEventoCancelamentoInsucessoEntrega.class, required = false),
})
private CTeTipoEvento evento;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class CTeNotaInfoCTeNormalDocumentosAnterioresEmissorDocumentosAnteriores
@Element(name = "CPF", required = false)
private String cpf;

@Element(name = "IE")
@Element(name = "IE", required = false)
private String inscricaoEstadual;

@Element(name = "UF")
@Element(name = "UF", required = false)
private String siglaUF;

@Element(name = "xNome")
Expand Down
Loading

0 comments on commit d4b823d

Please sign in to comment.