forked from srkirkland/DataAnnotationsExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CuitAttribute.cs
90 lines (76 loc) · 2.68 KB
/
CuitAttribute.cs
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
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions.Resources;
namespace DataAnnotationsExtensions
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class CuitAttribute : DataTypeAttribute
{
public CuitAttribute()
: base("cuit")
{
}
public override string FormatErrorMessage(string name)
{
if (ErrorMessage == null && ErrorMessageResourceName == null)
{
ErrorMessage = ValidatorResources.CuitAttribute_Invalid;
}
return base.FormatErrorMessage(name);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
try
{
string cuit = (string)value;
List<int> prefijosValidos = new List<int>(new[] { 20, 22, 23, 27, 30 });
int preFijo = 0;
int dni = 0;
int control = 0;
if (cuit.Length == 11 || cuit.Length == 13)
{
if (cuit.Contains("-"))
{
string[] aCuit = cuit.Split('-');
preFijo = int.Parse(aCuit[0]);
dni = int.Parse(aCuit[1]);
control = int.Parse(aCuit[2]);
}
else
{
preFijo = int.Parse(cuit.Substring(0, 2));
dni = int.Parse(cuit.Substring(2, 8));
control = int.Parse(cuit.Substring(10, 1));
}
}
string prefijoDni = preFijo + dni.ToString().PadLeft(8, '0');
int suma = 0;
int n;
if (!prefijosValidos.Contains(preFijo))
return false;
//Algoritmo de calculo del digito verificador
const string coef = "5432765432";
for (n = 0; n < 10; n++)
suma += int.Parse(prefijoDni.Substring(n, 1)) * int.Parse(coef.Substring(n, 1));
int resto = suma % 11;
int digitoVerificador;
if (resto == 0)
digitoVerificador = 0;
else if (resto == 10)
digitoVerificador = 1;
else
digitoVerificador = 11 - resto;
return (digitoVerificador == control);
}
catch (Exception)
{
return false;
}
}
}
}