• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wmixvideo / nfe / #4476

04 Sep 2023 02:00PM UTC coverage: 52.961% (-0.3%) from 53.216%
#4476

push

web-flow
Correções diversas no CT-e (#918)

* 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 <58352+fincatto@users.noreply.github.com>

0 of 130 new or added lines in 10 files covered. (0.0%)

5 existing lines in 3 files now uncovered.

13942 of 26325 relevant lines covered (52.96%)

0.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/fincatto/documentofiscal/cte300/utils/CTeGeraChave.java
1
package com.fincatto.documentofiscal.cte300.utils;
2

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

7
import java.time.ZonedDateTime;
8
import java.time.format.DateTimeFormatter;
9
import java.util.Random;
10

11
public class CTeGeraChave {
12
    
13
    private final CTeNota nota;
14
    private final CTeOS cteOS;
15

16
    public CTeGeraChave(final CTeNota nota) {
×
17
        this.nota = nota;
×
NEW
18
        this.cteOS = null;
×
NEW
19
    }
×
20

NEW
21
    public CTeGeraChave(final CTeOS cteOS) {
×
NEW
22
        this.nota = null;
×
NEW
23
        this.cteOS = cteOS;
×
UNCOV
24
    }
×
25
    
26
    public String geraCodigoRandomico() {
27
        long seed;
NEW
28
        if (nota != null) {
×
NEW
29
            seed = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
×
30
        } else {
NEW
31
            seed = this.cteOS.getInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
×
32
        }
NEW
33
        final Random random = new Random(seed);
×
UNCOV
34
        return StringUtils.leftPad(String.valueOf(random.nextInt(100000000)), 8, "0");
×
35
    }
36
    
37
    public String getChaveAcesso() {
38
        return String.format("%s%s", this.geraChaveAcessoSemDV(), this.getDV());
×
39
    }
40
    
41
    public Integer getDV() {
42
        final char[] valores = this.geraChaveAcessoSemDV().toCharArray();
×
43
        final int[] valoresInt = {2, 3, 4, 5, 6, 7, 8, 9};
×
44
        int indice = 0;
×
45
        int soma = 0;
×
46
        int valorTemp;
47
        int multTemp;
48
        for (int i = valores.length; i > 0; i--) {
×
49
            if (indice >= valoresInt.length) {
×
50
                indice = 0;
×
51
            }
52
    
53
            valorTemp = Integer.parseInt(String.valueOf(valores[i - 1]));
×
54
            multTemp = valoresInt[indice++];
×
55
            soma += valorTemp * multTemp;
×
56
        }
57
        final int dv = 11 - (soma % 11);
×
58
        return ((dv == 11) || (dv == 10)) ? 0 : dv;
×
59
    }
60
    
61
    private String geraChaveAcessoSemDV() {
62
        String codigoNumerico;
63
        String codigoUF;
64
        ZonedDateTime dataEmissao;
65
        String cnpj;
66
        String modelo;
67
        String serie;
68
        String numero;
69
        String tipoEmissao;
NEW
70
        if (nota != null) {
×
NEW
71
            codigoUF = this.nota.getCteNotaInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
×
NEW
72
            dataEmissao = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao();
×
NEW
73
            cnpj = this.nota.getCteNotaInfo().getEmitente().getCnpj();
×
NEW
74
            modelo = this.nota.getCteNotaInfo().getIdentificacao().getModelo().getCodigo();
×
NEW
75
            serie = this.nota.getCteNotaInfo().getIdentificacao().getSerie().toString();
×
NEW
76
            numero = this.nota.getCteNotaInfo().getIdentificacao().getNumero().toString();
×
NEW
77
            tipoEmissao = this.nota.getCteNotaInfo().getIdentificacao().getTipoEmissao().getCodigo();
×
NEW
78
            codigoNumerico = this.nota.getCteNotaInfo().getIdentificacao().getCodigoNumerico();
×
79
        } else {
NEW
80
            codigoUF = this.cteOS.getInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
×
NEW
81
            dataEmissao = this.cteOS.getInfo().getIdentificacao().getDataEmissao();
×
NEW
82
            cnpj = this.cteOS.getInfo().getEmitente().getCnpj();
×
NEW
83
            modelo = this.cteOS.getInfo().getIdentificacao().getModelo().getCodigo();
×
NEW
84
            serie = this.cteOS.getInfo().getIdentificacao().getSerie().toString();
×
NEW
85
            numero = this.cteOS.getInfo().getIdentificacao().getNumero().toString();
×
NEW
86
            tipoEmissao = this.cteOS.getInfo().getIdentificacao().getTipoEmissao().getCodigo();
×
NEW
87
            codigoNumerico = this.cteOS.getInfo().getIdentificacao().getCodigoNumerico();
×
88
        }
NEW
89
        if (StringUtils.isBlank(codigoNumerico)) {
×
UNCOV
90
            throw new IllegalStateException("Codigo numerico deve estar presente para gerar a chave de acesso");
×
91
        }
NEW
92
        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");
×
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc