Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 695 | blopes | 1 | package nfe.util; |
| 2 | |||
| 3 | import br.com.swconsultoria.nfe.dom.enuns.EstadosEnum; |
||
| 4 | |||
| 5 | import java.time.LocalDateTime; |
||
| 6 | import java.time.format.DateTimeFormatter; |
||
| 7 | |||
| 8 | public class ChaveUtil { |
||
| 9 | |||
| 10 | private String cDv; |
||
| 11 | private String chave; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Contrutor para montar a Chave |
||
| 15 | * @param estado |
||
| 16 | * @param cnpj |
||
| 17 | * @param modelo |
||
| 18 | * @param serie |
||
| 19 | * @param numeroNf |
||
| 20 | * @param tipoEmissao |
||
| 21 | * @param codigoNf |
||
| 22 | * @param dataEmissao |
||
| 23 | */ |
||
| 24 | public ChaveUtil(EstadosEnum estado, String cnpj, String modelo, int serie, int numeroNf, String tipoEmissao, String codigoNf, LocalDateTime dataEmissao) { |
||
| 25 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMM"); |
||
| 26 | String cUf = estado.getCodigoUF(); |
||
| 27 | String aamm = dataEmissao.format(formatter); |
||
| 28 | String serie1 = completarComZerosAEsquerda(String.valueOf(serie), 3); |
||
| 29 | String nNf = completarComZerosAEsquerda(String.valueOf(numeroNf), 9); |
||
| 30 | String cNf = completarComZerosAEsquerda(codigoNf, 8); |
||
| 31 | cnpj = cnpj.length() < 14 ? "000"+cnpj : cnpj; |
||
| 32 | this.chave = cUf + aamm + cnpj + modelo + serie1 + nNf + tipoEmissao + cNf; |
||
| 33 | this.cDv = String.valueOf(modulo11(chave)); |
||
| 34 | |||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Retorna a Chave da Nfe |
||
| 39 | * @return |
||
| 40 | */ |
||
| 41 | public String getChaveNF() { |
||
| 42 | return "NFe" + chave + cDv; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Retorna o Digito Verificador |
||
| 47 | * @return |
||
| 48 | */ |
||
| 49 | public String getDigitoVerificador() { |
||
| 50 | return cDv; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Calcula Digito Verificador |
||
| 55 | * @param chave |
||
| 56 | * @return |
||
| 57 | */ |
||
| 58 | private static int modulo11(String chave) { |
||
| 59 | int total = 0; |
||
| 60 | int peso = 2; |
||
| 61 | |||
| 62 | for (int i = 0; i < chave.length(); i++) { |
||
| 63 | total += (chave.charAt((chave.length() - 1) - i) - '0') * peso; |
||
| 64 | peso++; |
||
| 65 | if (peso == 10) |
||
| 66 | peso = 2; |
||
| 67 | } |
||
| 68 | int resto = total % 11; |
||
| 69 | return (resto == 0 || resto == 1) ? 0 : (11 - resto); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Completa com zeros a esquerda ate o tamanho passado. |
||
| 74 | * |
||
| 75 | * @param value |
||
| 76 | * @param length |
||
| 77 | * @return |
||
| 78 | */ |
||
| 79 | public static String completarComZerosAEsquerda(String value, int length) { |
||
| 80 | int tam = value.length(); |
||
| 81 | StringBuilder result = new StringBuilder(value); |
||
| 82 | |||
| 83 | for (int i = tam; i < length; i++) { |
||
| 84 | result.insert(0, "0"); |
||
| 85 | } |
||
| 86 | return result.toString(); |
||
| 87 | |||
| 88 | } |
||
| 89 | } |