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

wmixvideo / nfe / #6266

21 Mar 2025 11:46AM UTC coverage: 52.671% (+25.7%) from 26.996%
#6266

push

luciano.antunes
Nota Fiscal Fácil

0 of 21 new or added lines in 2 files covered. (0.0%)

2924 existing lines in 401 files now uncovered.

14002 of 26584 relevant lines covered (52.67%)

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) {
×
UNCOV
17
        this.nota = nota;
×
UNCOV
18
        this.cteOS = null;
×
19
    }
×
20

UNCOV
21
    public CTeGeraChave(final CTeOS cteOS) {
×
UNCOV
22
        this.nota = null;
×
UNCOV
23
        this.cteOS = cteOS;
×
24
    }
×
25
    
26
    public String geraCodigoRandomico() {
27
        long seed;
28
        if (nota != null) {
×
29
            seed = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
×
30
        } else {
31
            seed = this.cteOS.getInfo().getIdentificacao().getDataEmissao().toInstant().toEpochMilli();
×
32
        }
UNCOV
33
        final Random random = new Random(seed);
×
34
        return StringUtils.leftPad(String.valueOf(random.nextInt(100000000)), 8, "0");
×
35
    }
36
    
37
    public String getChaveAcesso() {
UNCOV
38
        return String.format("%s%s", this.geraChaveAcessoSemDV(), this.getDV());
×
39
    }
40
    
41
    public Integer getDV() {
UNCOV
42
        final char[] valores = this.geraChaveAcessoSemDV().toCharArray();
×
43
        final int[] valoresInt = {2, 3, 4, 5, 6, 7, 8, 9};
×
44
        int indice = 0;
×
UNCOV
45
        int soma = 0;
×
46
        int valorTemp;
47
        int multTemp;
48
        for (int i = valores.length; i > 0; i--) {
×
49
            if (indice >= valoresInt.length) {
×
UNCOV
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;
UNCOV
70
        if (nota != null) {
×
UNCOV
71
            codigoUF = this.nota.getCteNotaInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
×
UNCOV
72
            dataEmissao = this.nota.getCteNotaInfo().getIdentificacao().getDataEmissao();
×
UNCOV
73
            cnpj = this.nota.getCteNotaInfo().getEmitente().getCnpj();
×
UNCOV
74
            modelo = this.nota.getCteNotaInfo().getIdentificacao().getModelo().getCodigo();
×
UNCOV
75
            serie = this.nota.getCteNotaInfo().getIdentificacao().getSerie().toString();
×
UNCOV
76
            numero = this.nota.getCteNotaInfo().getIdentificacao().getNumero().toString();
×
UNCOV
77
            tipoEmissao = this.nota.getCteNotaInfo().getIdentificacao().getTipoEmissao().getCodigo();
×
UNCOV
78
            codigoNumerico = this.nota.getCteNotaInfo().getIdentificacao().getCodigoNumerico();
×
79
        } else {
UNCOV
80
            codigoUF = this.cteOS.getInfo().getIdentificacao().getCodigoUF().getCodigoIbge();
×
UNCOV
81
            dataEmissao = this.cteOS.getInfo().getIdentificacao().getDataEmissao();
×
UNCOV
82
            cnpj = this.cteOS.getInfo().getEmitente().getCnpj();
×
UNCOV
83
            modelo = this.cteOS.getInfo().getIdentificacao().getModelo().getCodigo();
×
UNCOV
84
            serie = this.cteOS.getInfo().getIdentificacao().getSerie().toString();
×
UNCOV
85
            numero = this.cteOS.getInfo().getIdentificacao().getNumero().toString();
×
UNCOV
86
            tipoEmissao = this.cteOS.getInfo().getIdentificacao().getTipoEmissao().getCodigo();
×
UNCOV
87
            codigoNumerico = this.cteOS.getInfo().getIdentificacao().getCodigoNumerico();
×
88
        }
UNCOV
89
        if (StringUtils.isBlank(codigoNumerico)) {
×
UNCOV
90
            throw new IllegalStateException("Codigo numerico deve estar presente para gerar a chave de acesso");
×
91
        }
UNCOV
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