Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suporte a CNPJ alfanumérico #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Flunt.Br/Extensions/PersonContractExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,27 @@ public static Contract IsCnpj(this Contract contract, string value, string prope
return contract;
}

public static Contract IsCnpjAlphaNumeric(this Contract contract, string value, string property, string message)
{
if (string.IsNullOrEmpty(value) || !new CnpjAlphaNumeric().Validate(value))
contract.AddNotification(property, message);
return contract;
}

public static Contract IsCnpjOrCPF(this Contract contract, string value, string property, string message)
{
if (string.IsNullOrEmpty(value) || (!new Cnpj().Validate(value) && !new Cpf().Validate(value)))
contract.AddNotification(property, message);
return contract;
}

public static Contract IsCnpjAlphaNumericOrCPF(this Contract contract, string value, string property, string message)
{
if (string.IsNullOrEmpty(value) || (!new CnpjAlphaNumeric().Validate(value) && !new Cpf().Validate(value)))
contract.AddNotification(property, message);
return contract;
}

public static Contract IsVoterDocument(this Contract contract, string value, string property, string message)
{
if (string.IsNullOrEmpty(value) || !new VoterDocument().Validate(value))
Expand Down
42 changes: 42 additions & 0 deletions src/Flunt.Br/Validations/CnpjAlphaNumeric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using System.Text.RegularExpressions;
using Flunt.Br.Document.Interfaces;

namespace Flunt.Br.Validations
{
internal class CnpjAlphaNumeric : IValidate
{
public bool Validate(string value)
{
var multiplicador1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
var multiplicador2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
string pattern = @"^[a-zA-Z0-9]{12}\d{2}$";
int soma;
int resto;
string digito;
string tempCnpj;
value = value.Trim();
value = value.Replace(".", "").Replace("-", "").Replace("/", "");
if (!Regex.IsMatch(value, pattern))
return false;
tempCnpj = value.Substring(0, 12).ToUpper();
// CNPJ com todos os caracteres iguais � inv�lido
if (tempCnpj.All(caractere => caractere == value[0]))
return false;
soma = 0;
for (int i = 0; i < 12; i++)
soma += (tempCnpj[i] - '0') * multiplicador1[i];
resto = (soma % 11);
resto = resto < 2 ? 0 : 11 - resto;
digito = resto.ToString();
tempCnpj = tempCnpj + digito;
soma = 0;
for (int i = 0; i < 13; i++)
soma += (tempCnpj[i] - '0') * multiplicador2[i];
resto = (soma % 11);
resto = resto < 2 ? 0 : 11 - resto;
digito = digito + resto;
return value.EndsWith(digito);
}
}
}
66 changes: 66 additions & 0 deletions tests/Flunt.Br.Tests/PersonContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ public void IsCnpj_InValid(string value)
Assert.IsFalse(wrong.IsValid);
}

[TestMethod]
[DataRow("123456789101112")]
[DataRow("655618111115522")]
[DataRow("00000000000abc")]
[DataRow("45.448.481/0501-18")]
[DataRow("00.000.000/0000-00")]
[DataRow("11.111.111/1111-11")]
[DataRow("22.222.222/2222-22")]
[DataRow("33.333.333/3333-33")]
[DataRow("44.444.444/4444-44")]
[DataRow("55.555.555/5555-55")]
[DataRow("66.666.666/6666-66")]
[DataRow("77.777.777/7777-77")]
[DataRow("88.888.888/8888-88")]
[DataRow("99.999.999/9999-99")]
[DataRow("AA.AAA.AAA/AAAA-99")]
[DataRow("ZZ.ZZZ.ZZZ/ZZZZ-99")]
[DataRow("00.D5H.PL9/AONL-48")]
[DataRow("4O.X01.0AO/7DD9-38")]
[DataRow("50.WXI.YTL/4O33-08")]
[DataRow("61.HXL.XIY/33TQ-48")]
[DataRow("A8.9Y4.JB9/VLHE-88")]
[DataRow(null)]
public void IsCnpjAlphaNumeric_InValid(string value)
{
var wrong = new Contract()
.IsCnpjAlphaNumeric(value, "document", "Invalid document");
Assert.IsFalse(wrong.IsValid);
}

[TestMethod]
[DataRow("16880249000179")]
[DataRow("45.448.421/0001-18")]
Expand All @@ -120,6 +150,42 @@ public void IsCnpj_Valid(string value)
Assert.IsTrue(right.IsValid);
}

[TestMethod]
[DataRow("16880249000179")]
[DataRow("45.448.421/0001-18")]
[DataRow("00.D5H.PL9/AONL-84")]
[DataRow("4O.X01.0AO/7DD9-03")]
[DataRow("50.WXI.YTL/4O33-00")]
[DataRow("61.HXL.XIY/33TQ-64")]
[DataRow("A8.9Y4.JB9/VLHE-18")]
[DataRow("B0.8JJ.D0M/FJ3M-84")]
[DataRow("D2.108.T5M/R2IQ-20")]
[DataRow("EW.WXK.YC8/HTB2-84")]
[DataRow("GL.SPN.P58/EQC7-74")]
[DataRow("IU.BYB.2WV/GJKT-39")]
[DataRow("LN.L4L.VPN/LDIU-24")]
[DataRow("QE.V53.HE5/NY08-13")]
[DataRow("00D5HPL9AONL84")]
[DataRow("4OX010AO7DD903")]
[DataRow("50WXIYTL4O3300")]
[DataRow("61HXLXIY33TQ64")]
[DataRow("A89Y4JB9VLHE18")]
[DataRow("B08JJD0MFJ3M84")]
[DataRow("D2108T5MR2IQ20")]
[DataRow("EWWXKYC8HTB284")]
[DataRow("GLSPNP58EQC774")]
[DataRow("IUBYB2WVGJKT39")]
[DataRow("LNL4LVPNLDIU24")]
[DataRow("QEV53HE5NY0813")]

public void IsCnpjAlphaNumeric_Valid(string value)
{
var right = new Contract()
.IsCnpjAlphaNumeric(value, "document", "Invalid document");
Assert.IsTrue(right.IsValid);
}


[TestMethod]
[DataRow("668247690132")]
[DataRow("333438450601")]
Expand Down