Skip to content

Commit 16c6a68

Browse files
authored
Merge pull request #1687 from joaovictorjs/Branch_27216
feat: considera caracteres alfanuméricos ao calcular digito verificado da chave de acesso
2 parents fc8678b + 1fd0a72 commit 16c6a68

6 files changed

Lines changed: 109 additions & 3 deletions

File tree

DFe.Utils/ChaveFiscal.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ private static string ObterDigitoVerificador(string chave)
9595
//percorrendo cada caractere da chave da direita para esquerda para fazer os cálculos com o peso
9696
for (var i = chave.Length - 1; i != -1; i--)
9797
{
98-
var ch = Convert.ToInt32(chave[i].ToString());
99-
soma += ch*peso;
98+
var valorCaractere = ObterValorDoCaractere(chave[i]);
99+
soma += valorCaractere * peso;
100100
//sempre que for 9 voltamos o peso a 2
101101
if (peso < 9)
102102
peso += 1;
@@ -115,6 +115,18 @@ private static string ObterDigitoVerificador(string chave)
115115
return dv.ToString();
116116
}
117117

118+
/// <summary>
119+
/// Obtem o valor de um caractere
120+
/// </summary>
121+
/// <param name="caractere"></param>
122+
/// <returns></returns>
123+
internal static int ObterValorDoCaractere(char caractere)
124+
{
125+
const int zeroASCII = 48;
126+
var valor = caractere - zeroASCII;
127+
return valor;
128+
}
129+
118130
/// <summary>
119131
/// Informa se a chave de um DF-e é válida
120132
/// </summary>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("NFe.Utils.Testes")]

NFe.Classes/Informacoes/Emitente/emit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public string CNPJ
5252
{
5353
if (string.IsNullOrEmpty(value)) return;
5454
if (string.IsNullOrEmpty(_cpf))
55-
_cnpj = Regex.Match(value, @"\d+").Value;
55+
_cnpj = Regex.Match(value, @"[0-9A-Z]+").Value;
5656

5757
else
5858
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using DFe.Utils;
3+
using DFe.Classes.Entidades;
4+
using DFe.Classes.Flags;
5+
using NFe.Utils.Testes.Dados;
6+
using Xunit;
7+
8+
namespace NFe.Utils.Testes;
9+
10+
public class ChaveFiscalTesteUnitario
11+
{
12+
[Theory(DisplayName = "Dado cnpj alfanumérico, quando obter chave fiscal, então não deve lançar exceção.")]
13+
[InlineData("T6J3XFX0IVDD47")]
14+
[InlineData("W2MNK0KZ000190")]
15+
public void DadoCnpjAlfanumericoQuandoObterChaveFiscalEntaoNaoDeveLancarExcecao(string cnpj)
16+
{
17+
// Arrange
18+
var estado = Estado.SE;
19+
var data = DateTime.Now;
20+
var modelo = ModeloDocumento.NFe;
21+
var serie = 100;
22+
var numero = 12345;
23+
var tipoEmissao = 1;
24+
var cNf = 12345678;
25+
26+
// Act
27+
var excecaoCapturada = Record.Exception(() => ChaveFiscal.ObterChave(estado, data, cnpj, modelo, serie, numero, tipoEmissao, cNf));
28+
29+
// Assert
30+
Assert.Null(excecaoCapturada);
31+
}
32+
33+
[Theory(DisplayName = "Dado caractere, quando obter valor do caractere, então deve retornar o valor esperado.")]
34+
[MemberData(nameof(ChaveFiscalDadosDeTeste.ObterCaracteresEValores), MemberType = typeof(ChaveFiscalDadosDeTeste))]
35+
public void DadoCaractereQuandoObterValorEntaoDeveRetornarValorEsperado(char caractere, int valorEsperado)
36+
{
37+
// Act
38+
var valorObtido = ChaveFiscal.ObterValorDoCaractere(caractere);
39+
40+
// Assert
41+
Assert.Equal(valorEsperado, valorObtido);
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.Generic;
2+
3+
namespace NFe.Utils.Testes.Dados;
4+
5+
public static class ChaveFiscalDadosDeTeste
6+
{
7+
public static IEnumerable<object[]> ObterCaracteresEValores()
8+
{
9+
yield return new object[] { '0', 0 };
10+
yield return new object[] { '1', 1 };
11+
yield return new object[] { '2', 2 };
12+
yield return new object[] { '3', 3 };
13+
yield return new object[] { '4', 4 };
14+
yield return new object[] { '5', 5 };
15+
yield return new object[] { '6', 6 };
16+
yield return new object[] { '7', 7 };
17+
yield return new object[] { '8', 8 };
18+
yield return new object[] { '9', 9 };
19+
20+
yield return new object[] { 'A', 17 };
21+
yield return new object[] { 'B', 18 };
22+
yield return new object[] { 'C', 19 };
23+
yield return new object[] { 'D', 20 };
24+
yield return new object[] { 'E', 21 };
25+
yield return new object[] { 'F', 22 };
26+
yield return new object[] { 'G', 23 };
27+
yield return new object[] { 'H', 24 };
28+
yield return new object[] { 'I', 25 };
29+
yield return new object[] { 'J', 26 };
30+
yield return new object[] { 'K', 27 };
31+
yield return new object[] { 'L', 28 };
32+
yield return new object[] { 'M', 29 };
33+
yield return new object[] { 'N', 30 };
34+
yield return new object[] { 'O', 31 };
35+
yield return new object[] { 'P', 32 };
36+
yield return new object[] { 'Q', 33 };
37+
yield return new object[] { 'R', 34 };
38+
yield return new object[] { 'S', 35 };
39+
yield return new object[] { 'T', 36 };
40+
yield return new object[] { 'U', 37 };
41+
yield return new object[] { 'V', 38 };
42+
yield return new object[] { 'W', 39 };
43+
yield return new object[] { 'X', 40 };
44+
yield return new object[] { 'Y', 41 };
45+
yield return new object[] { 'Z', 42 };
46+
}
47+
}

NFe.Utils.Testes/NFe.Utils.Testes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<ItemGroup>
2020
<ProjectReference Include="..\DadosDeTestes\DadosDeTestes.csproj" />
2121
<ProjectReference Include="..\DFe.Classes\DFe.Classes.csproj" />
22+
<ProjectReference Include="..\DFe.Utils\DFe.Utils.csproj" />
2223
<ProjectReference Include="..\NFe.Classes\NFe.Classes.csproj" />
2324
<ProjectReference Include="..\NFe.Utils\NFe.Utils.csproj" />
2425
</ItemGroup>

0 commit comments

Comments
 (0)