diff --git a/DFe.Utils/ChaveFiscal.cs b/DFe.Utils/ChaveFiscal.cs
index 779a366a0..30c7ed088 100644
--- a/DFe.Utils/ChaveFiscal.cs
+++ b/DFe.Utils/ChaveFiscal.cs
@@ -95,8 +95,8 @@ private static string ObterDigitoVerificador(string chave)
//percorrendo cada caractere da chave da direita para esquerda para fazer os cálculos com o peso
for (var i = chave.Length - 1; i != -1; i--)
{
- var ch = Convert.ToInt32(chave[i].ToString());
- soma += ch*peso;
+ var valorCaractere = ObterValorDoCaractere(chave[i]);
+ soma += valorCaractere * peso;
//sempre que for 9 voltamos o peso a 2
if (peso < 9)
peso += 1;
@@ -115,6 +115,18 @@ private static string ObterDigitoVerificador(string chave)
return dv.ToString();
}
+ ///
+ /// Obtem o valor de um caractere
+ ///
+ ///
+ ///
+ internal static int ObterValorDoCaractere(char caractere)
+ {
+ const int zeroASCII = 48;
+ var valor = caractere - zeroASCII;
+ return valor;
+ }
+
///
/// Informa se a chave de um DF-e é válida
///
diff --git a/DFe.Utils/Properties/AssemblyInfo.cs b/DFe.Utils/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..9d397211c
--- /dev/null
+++ b/DFe.Utils/Properties/AssemblyInfo.cs
@@ -0,0 +1,3 @@
+using System.Runtime.CompilerServices;
+
+[assembly: InternalsVisibleTo("NFe.Utils.Testes")]
diff --git a/NFe.Classes/Informacoes/Emitente/emit.cs b/NFe.Classes/Informacoes/Emitente/emit.cs
index 2b94d7ac0..238d8f5c4 100644
--- a/NFe.Classes/Informacoes/Emitente/emit.cs
+++ b/NFe.Classes/Informacoes/Emitente/emit.cs
@@ -52,7 +52,7 @@ public string CNPJ
{
if (string.IsNullOrEmpty(value)) return;
if (string.IsNullOrEmpty(_cpf))
- _cnpj = Regex.Match(value, @"\d+").Value;
+ _cnpj = Regex.Match(value, @"[0-9A-Z]+").Value;
else
{
diff --git a/NFe.Utils.Testes/ChaveFiscalTesteUnitario.cs b/NFe.Utils.Testes/ChaveFiscalTesteUnitario.cs
new file mode 100644
index 000000000..7d17f9955
--- /dev/null
+++ b/NFe.Utils.Testes/ChaveFiscalTesteUnitario.cs
@@ -0,0 +1,43 @@
+using System;
+using DFe.Utils;
+using DFe.Classes.Entidades;
+using DFe.Classes.Flags;
+using NFe.Utils.Testes.Dados;
+using Xunit;
+
+namespace NFe.Utils.Testes;
+
+public class ChaveFiscalTesteUnitario
+{
+ [Theory(DisplayName = "Dado cnpj alfanumérico, quando obter chave fiscal, então não deve lançar exceção.")]
+ [InlineData("T6J3XFX0IVDD47")]
+ [InlineData("W2MNK0KZ000190")]
+ public void DadoCnpjAlfanumericoQuandoObterChaveFiscalEntaoNaoDeveLancarExcecao(string cnpj)
+ {
+ // Arrange
+ var estado = Estado.SE;
+ var data = DateTime.Now;
+ var modelo = ModeloDocumento.NFe;
+ var serie = 100;
+ var numero = 12345;
+ var tipoEmissao = 1;
+ var cNf = 12345678;
+
+ // Act
+ var excecaoCapturada = Record.Exception(() => ChaveFiscal.ObterChave(estado, data, cnpj, modelo, serie, numero, tipoEmissao, cNf));
+
+ // Assert
+ Assert.Null(excecaoCapturada);
+ }
+
+ [Theory(DisplayName = "Dado caractere, quando obter valor do caractere, então deve retornar o valor esperado.")]
+ [MemberData(nameof(ChaveFiscalDadosDeTeste.ObterCaracteresEValores), MemberType = typeof(ChaveFiscalDadosDeTeste))]
+ public void DadoCaractereQuandoObterValorEntaoDeveRetornarValorEsperado(char caractere, int valorEsperado)
+ {
+ // Act
+ var valorObtido = ChaveFiscal.ObterValorDoCaractere(caractere);
+
+ // Assert
+ Assert.Equal(valorEsperado, valorObtido);
+ }
+}
diff --git a/NFe.Utils.Testes/Dados/ChaveFiscalDadosDeTeste.cs b/NFe.Utils.Testes/Dados/ChaveFiscalDadosDeTeste.cs
new file mode 100644
index 000000000..7bf54ebe1
--- /dev/null
+++ b/NFe.Utils.Testes/Dados/ChaveFiscalDadosDeTeste.cs
@@ -0,0 +1,47 @@
+using System.Collections.Generic;
+
+namespace NFe.Utils.Testes.Dados;
+
+public static class ChaveFiscalDadosDeTeste
+{
+ public static IEnumerable