Skip to content

Commit

Permalink
Gerador de chave gerando codigo randomico, quando nao informado.
Browse files Browse the repository at this point in the history
  • Loading branch information
fincatto committed Jun 20, 2016
1 parent 91c451a commit 59d3a74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
14 changes: 5 additions & 9 deletions src/main/java/com/fincatto/nfe310/utils/NFGeraChave.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fincatto.nfe310.classes.nota.NFNota;
import org.apache.commons.lang3.StringUtils;

import java.util.Random;

public class NFGeraChave {

private final NFNota nota;
Expand All @@ -17,7 +19,7 @@ public String getChaveAcesso() {

public Integer getDV() {
final char[] valores = this.geraChaveAcessoSemDV().toCharArray();
final int[] valoresInt = { 2, 3, 4, 5, 6, 7, 8, 9 };
final int[] valoresInt = {2, 3, 4, 5, 6, 7, 8, 9};
int indice = 0;
int soma = 0;
int valorTemp;
Expand All @@ -36,20 +38,14 @@ public Integer getDV() {
}

private String geraChaveAcessoSemDV() {
final String chaveRandomica = String.valueOf(new Random(this.nota.getInfo().getIdentificacao().getDataHoraEmissao().getMillis()).nextInt(100000000));
return StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getUf().getCodigoIbge(), 2, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getDataHoraEmissao().toString("yyMM"), 4, "0") +
StringUtils.leftPad(this.nota.getInfo().getEmitente().getCnpj() == null ? this.nota.getInfo().getEmitente().getCpf() : this.nota.getInfo().getEmitente().getCnpj(), 14, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getModelo().getCodigo(), 2, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getSerie(), 3, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getNumeroNota(), 9, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getTipoEmissao().getCodigo(), 1, "0") +
StringUtils.leftPad(this.nota.getInfo().getIdentificacao().getCodigoRandomico() == null || this.nota.getInfo().getIdentificacao().getCodigoRandomico().isEmpty() ? this.gerarCodigoRandomico() : this.nota.getInfo().getIdentificacao().getCodigoRandomico(), 8, "0");
}

public String gerarCodigoRandomico() {
Random gerador = new Random();
Integer numero = gerador.nextInt(100000000);
return numero.toString;
StringUtils.leftPad(StringUtils.defaultIfBlank(this.nota.getInfo().getIdentificacao().getCodigoRandomico(), chaveRandomica), 8, "0");
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.fincatto.nfe310.classes.nota;

import org.junit.Assert;
import org.junit.Test;

import com.fincatto.nfe310.FabricaDeObjetosFake;
import com.fincatto.nfe310.utils.NFGeraChave;
import org.junit.Assert;
import org.junit.Test;

public class NFGeraChaveTest {

Expand Down Expand Up @@ -42,4 +41,23 @@ public void geraChaveDeAcessoComCNPJConformeEsperado() {
Assert.assertEquals("NFe43101012345678901234559999999999991999999999", info.getIdentificador());
Assert.assertEquals(47, info.getIdentificador().length());
}

@Test
public void geraChaveDeAcessoRandomica() {
final NFNotaInfo info = FabricaDeObjetosFake.getNFNotaInfo();
info.getEmitente().setCpf(null);
info.getEmitente().setCnpj("12345678901234");
info.getIdentificacao().setCodigoRandomico(null);

final NFNota nota = new NFNota();
nota.setInfo(info);

final String chaveAcesso = new NFGeraChave(nota).getChaveAcesso();
Assert.assertEquals("43101012345678901234559999999999991696178050", chaveAcesso);
Assert.assertEquals(44, chaveAcesso.length());

info.setIdentificador(chaveAcesso);
Assert.assertEquals("NFe43101012345678901234559999999999991696178050", info.getIdentificador());
Assert.assertEquals(47, info.getIdentificador().length());
}
}

0 comments on commit 59d3a74

Please sign in to comment.