-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChaveAcessoNFe.java
79 lines (66 loc) · 2.69 KB
/
ChaveAcessoNFe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package br.com.auditoria.nfe;
public class ChaveAcessoNFe {
public static void main(String[] args) {
try {
String cUF = "35"; // Código da UF do emitente do Documento Fiscal.
String dataAAMM = "1501"; // Ano e Mês de emissão da NF-e.
String cnpjCpf = "47.508.411/0016-32"; // CNPJ do emitente.
String mod = "55"; // Modelo do Documento Fiscal.
String serie = "102"; // Série do Documento Fiscal.
String tpEmis = "1"; // Forma de emissão da NF-e
String nNF = "000000896"; // Número do Documento Fiscal.
String cNF = "00184742"; // Código Numérico que compõe a Chave de Acesso.
StringBuilder chave = new StringBuilder();
chave.append(lpadTo(cUF, 2, '0'));
chave.append(lpadTo(dataAAMM, 4, '0'));
chave.append(lpadTo(cnpjCpf.replaceAll("\\D",""), 14, '0'));
chave.append(lpadTo(mod, 2, '0'));
chave.append(lpadTo(serie, 3, '0'));
chave.append(lpadTo(String.valueOf(nNF), 9, '0'));
chave.append(lpadTo(tpEmis, 1, '0'));
chave.append(lpadTo(cNF, 8, '0'));
chave.append(modulo11(chave.toString()));
chave.insert(0, "NFe");
info("Chave NF-e: " + chave.toString());
} catch (Exception e) {
error(e.toString());
}
}
public static int modulo11(String chave) {
int total = 0;
int peso = 2;
for (int i = 0; i < chave.length(); i++) {
total += (chave.charAt((chave.length()-1) - i) - '0') * peso;
peso ++;
if (peso == 10)
peso = 2;
}
int resto = total % 11;
return (resto == 0 || resto == 1) ? 0 : (11 - resto);
}
public static String lpadTo(String input, int width, char ch) {
String strPad = "";
StringBuffer sb = new StringBuffer(input.trim());
while (sb.length() < width)
sb.insert(0,ch);
strPad = sb.toString();
if (strPad.length() > width) {
strPad = strPad.substring(0,width);
}
return strPad;
}
/**
* Log ERROR.
* @param error
*/
private static void error(String error) {
System.out.println("| ERROR: " + error);
}
/**
* Log INFO.
* @param info
*/
private static void info(String info) {
System.out.println("| INFO: " + info);
}
}