File Coverage

blib/lib/Business/ES/NIF.pm
Criterion Covered Total %
statement 106 123 86.1
branch 38 44 86.3
condition 4 8 50.0
subroutine 22 27 81.4
pod 10 10 100.0
total 180 212 84.9


line stmt bran cond sub pod time code
1             package Business::ES::NIF;
2              
3             # ABSTRACT: Validate Spanish NIF, NIE and CIF numbers
4              
5              
6             our $VERSION = '0.15';
7              
8 7     7   1062658 use strict;
  7         14  
  7         282  
9 7     7   36 use warnings;
  7         13  
  7         578  
10              
11 7     7   3407 use Class::XSAccessor { accessors => [qw(error extra nif status type iso3166 provincia VIES vies_check vies_error)] };
  7         19531  
  7         62  
12              
13              
14             use constant {
15 7         1279 NIF_LETRAS => 'TRWAGMYFPDXBNJZSQVHLCKE',
16             CIF_LETRAS => 'JABCDEFGHI',
17             RE_CIF => qr/^([ABCDEFGHJPQRUVNW])([0-9]{7})([0-9])$/,
18             RE_CIFe => qr/^([SQPK])([0-9]{7})([A-J])$/,
19             RE_NIF => qr/^([0-9]{8})([A-Z])$/,
20             RE_NIFe => qr/^([KLM])([0-9]{7})([A-Z])$/,
21             RE_NIE => qr/^([XYZ])([0-9]{7})([A-Z])$/
22 7     7   10050 };
  7         29  
23              
24 7     7   62 use constant NIE_PREFIX => { X => '0', Y => '1', Z => '2' };
  7         13  
  7         761  
25              
26 7         720 use constant CIF_EXTRA => {
27             'A' => 'Sociedad Anonima - S.A',
28             'B' => 'Sociedad Limitada - S.L',
29             'C' => 'Sociedad Colectiva - S.C',
30             'D' => 'Sociedades comanditarias',
31             'E' => 'Comunidad de bienes y herencias',
32             'F' => 'Sociedades cooperativas',
33             'G' => 'Asociaciones',
34             'H' => 'Comunidaddes de propietarios',
35             'J' => 'Sociedades civiles',
36             'P' => 'Corporaciones locales',
37             'Q' => 'Organismos publicos',
38             'N' => 'Entidades extranjeras',
39             'R' => 'Congregaciones e instituciones religiosas',
40             'U' => 'Uniones temporales de epresas',
41             'V' => 'Otros tipos de sociedades',
42             'W' => 'Establecimientos permanentes de entidades no residentes en España'
43 7     7   43 };
  7         12  
44              
45 7         2338 use constant CIFe_EXTRA => {
46             'S' => 'Órganos de la Administración del Estado',
47             'Q' => 'Organismos autónomos o instituciones religiosas',
48             'P' => 'Corporaciones locales',
49             'K' => 'Formato antiguo (DNI sin letra de control)'
50 7     7   44 };
  7         12  
51              
52 7         11584 use constant PROVINCIAS => {
53             '00' => 'No Residente',
54             '01' => 'Álava',
55             '02' => 'Albacete',
56             '03' => 'Alicante',
57             '04' => 'Almería',
58             '05' => 'Ávila',
59             '06' => 'Badajoz',
60             '07' => 'Islas Baleares',
61             '08' => 'Barcelona',
62             '09' => 'Burgos',
63             '10' => 'Cáceres',
64             '11' => 'Cádiz',
65             '12' => 'Castellón',
66             '13' => 'Ciudad Real',
67             '14' => 'Córdoba',
68             '15' => 'La Coruña',
69             '16' => 'Cuenca',
70             '17' => 'Gerona',
71             '18' => 'Granada',
72             '19' => 'Guadalajara',
73             '20' => 'Guipúzcoa',
74             '21' => 'Huelva',
75             '22' => 'Huesca',
76             '23' => 'Jaén',
77             '24' => 'León',
78             '25' => 'Lérida',
79             '26' => 'La Rioja',
80             '27' => 'Lugo',
81             '28' => 'Madrid',
82             '29' => 'Málaga',
83             '30' => 'Murcia',
84             '31' => 'Navarra',
85             '32' => 'Orense',
86             '33' => 'Asturias',
87             '34' => 'Palencia',
88             '35' => 'Las Palmas',
89             '36' => 'Pontevedra',
90             '37' => 'Salamanca',
91             '38' => 'Santa Cruz de Tenerife',
92             '39' => 'Cantabria',
93             '40' => 'Segovia',
94             '41' => 'Sevilla',
95             '42' => 'Soria',
96             '43' => 'Tarragona',
97             '44' => 'Teruel',
98             '45' => 'Toledo',
99             '46' => 'Valencia',
100             '47' => 'Valladolid',
101             '48' => 'Vizcaya',
102             '49' => 'Zamora',
103             '50' => 'Zaragoza',
104             '51' => 'Ceuta',
105             '52' => 'Melilla',
106             '53' => 'Alicante',
107             '54' => 'Alicante',
108             '55' => 'Gerona',
109             '56' => 'Córdoba',
110             '57' => 'Islas Baleares',
111             '58' => 'Barcelona',
112             '59' => 'Barcelona',
113             '60' => 'Barcelona',
114             '61' => 'Barcelona',
115             '62' => 'Barcelona',
116             '63' => 'Barcelona',
117             '64' => 'Barcelona',
118             '65' => 'Barcelona',
119             '66' => 'Barcelona',
120             '67' => 'Gerona',
121             '68' => 'Barcelona',
122             '70' => 'La Coruña',
123             '71' => 'Guipúzcoa',
124             '72' => 'Cádiz',
125             '73' => 'Murcia',
126             '74' => 'Asturias',
127             '75' => 'Las Palmas',
128             '76' => 'Santa Cruz de Tenerife',
129             '77' => 'Tarragona',
130             '78' => 'Madrid',
131             '79' => 'Madrid',
132             '80' => 'Madrid',
133             '81' => 'Madrid',
134             '82' => 'Madrid',
135             '83' => 'Madrid',
136             '84' => 'Madrid',
137             '85' => 'Madrid',
138             '86' => 'Madrid',
139             '87' => 'Madrid',
140             '88' => 'Madrid',
141             '90' => 'Sevilla',
142             '91' => 'Sevilla',
143             '92' => 'Málaga',
144             '93' => 'Málaga',
145             '94' => 'Pontevedra',
146             '95' => 'Vizcaya',
147             '96' => 'Valencia',
148             '97' => 'Valencia',
149             '98' => 'Valencia',
150             '99' => 'Zaragoza'
151 7     7   75 };
  7         26  
152              
153              
154 0     0 1 0 sub is_nie { return shift->type eq 'NIE' }
155 0     0 1 0 sub is_nif { return shift->type eq 'NIF' }
156 0     0 1 0 sub is_nife { return shift->type eq 'NIFe' }
157 0     0 1 0 sub is_cif { return shift->type eq 'CIF' }
158 0     0 1 0 sub is_cife { return shift->type eq 'CIFe' }
159              
160 7     7 1 43 sub vat { return 'ES' . shift->nif; }
161              
162             sub _check_cif {
163 25     25   47 my ($sociedad,$inscripcion,$control) = @_;
164            
165 25         74 my @n = split //, $inscripcion;
166 25         60 my $pares = $n[1] + $n[3] + $n[5];
167 25         29 my $nones;
168              
169 25         72 for (0, 2, 4, 6) {
170 100         140 my $d = $n[$_] * 2;
171 100 100       173 $nones += $d < 10 ? $d : $d - 9;
172             }
173              
174 25         108 my $c = (10 - substr($pares + $nones, -1)) % 10;
175 25         46 my $l = substr(CIF_LETRAS, $c, 1);
176            
177 25 100       72 if ( $sociedad =~ /[KPQS]/i) {
178 9 100       50 return 0 if $l ne uc($control);
179             } else {
180 16 100 66     63 return 0 if $c != $control and $l ne uc($control);
181             }
182            
183 16         51 return 1;
184             }
185              
186             sub _check_nif {
187 242     242   1089 my ($NIF,$DC) = @_;
188              
189 242 100       1292 return substr( NIF_LETRAS , $NIF % 23, 1) eq $DC ? 1 : 0;
190             }
191              
192             sub _validate_cif {
193 16     16   59 my ($self,$sociedad, $inscripcion, $control) = @_;
194              
195 16         37 $self->status(_check_cif($sociedad, $inscripcion, $control));
196 16         53 $self->provincia( PROVINCIAS->{ substr($inscripcion,0,2) });
197              
198 16         30 $self->type ('CIF');
199 16         53 $self->extra( CIF_EXTRA->{ $sociedad } );
200             }
201              
202             sub _validate_cife {
203 9     9   41 my ($self,$sociedad, $inscripcion, $control) = @_;
204              
205 9         26 $self->status(_check_cif($sociedad, $inscripcion, $control));
206              
207 9         25 $self->type ('CIFe');
208 9         57 $self->extra( CIFe_EXTRA->{ $sociedad } );
209             }
210              
211             sub _validate_nif {
212 219     219   434 my $self = shift;
213              
214 219         481 $self->status(_check_nif(shift,shift));
215              
216 219         628 $self->type ('NIF');
217 219         615 $self->extra('NIF');
218             }
219              
220             sub _validate_nife {
221 7     7   8 my $self = shift;
222              
223 7         15 $self->status(_check_nif(shift,shift));
224              
225 7         11 $self->type ('NIFe');
226 7         17 $self->extra('NIF Especial (KLM)');
227             }
228              
229             sub _validate_nie {
230 16     16   86 my ($self,$NIE,$NIF,$DC) = @_;
231              
232 16         41 $NIF = NIE_PREFIX->{$NIE} . $NIF;
233              
234 16         39 $self->status(_check_nif($NIF,$DC));
235              
236 16         33 $self->type ('NIE');
237 16         53 $self->extra('NIE');
238             }
239              
240             sub _normalize_nif {
241 288     288   536 my $nif = shift;
242              
243 288         597 $nif =~ tr/-. //d;
244            
245 288         1051 return uc($nif);
246             }
247              
248              
249             sub new {
250 287     287 1 1372332 my ($class, %args) = @_;
251 287         797 my $self = bless { }, $class;
252              
253 287 100       1339 $self->VIES( defined $args{vies} ? 1 : 0 );
254 287 100       835 $self->iso3166( defined $args{iso3166} ? 1 : 0 );
255              
256 287 100       1252 $self->set($args{nif}) if defined $args{nif};
257            
258 287         954 return $self;
259             }
260              
261              
262             sub set {
263 288     288 1 1067 my $self = shift;
264 288         459 my $nif = shift;
265              
266 288 50       726 unless (defined $nif) {
267 0         0 $self->status(0);
268 0         0 $self->error("NIF vacío");
269 0         0 return;
270             }
271              
272 288 100       918 if ($nif =~ /^ES(.*)$/) {
273 3         8 $self->iso3166(1);
274 3         7 $nif = $1;
275             }
276            
277 288         592 $self->nif(_normalize_nif($nif));
278              
279 288         777 $self->check;
280             }
281              
282              
283 241     241 1 1252 sub is_valid { !!shift->status }
284              
285              
286             sub check {
287 288     288 1 472 my $self = shift;
288 288         607 my $nif = $self->nif;
289              
290 288 100       1818 if ($nif =~ RE_NIF) { $self->_validate_nif ($1, $2); }
  219 100       647  
    100          
    100          
    100          
291 16         42 elsif ($nif =~ RE_CIF) { $self->_validate_cif ($1, $2, $3); }
292 16         77 elsif ($nif =~ RE_NIE) { $self->_validate_nie ($1, $2, $3); }
293 7         15 elsif ($nif =~ RE_NIFe) { $self->_validate_nife($2, $3); }
294 9         28 elsif ($nif =~ RE_CIFe) { $self->_validate_cife($1, $2, $3); }
295              
296 288 100       793 if ( $self->status ) {
297 56 100       145 $self->_check_vies() if $self->VIES;
298 56         122 return;
299             }
300            
301 232         796 $self->error("Error formato de NIF/CIF/NIE");
302 232         469 $self->status(0);
303            
304 232         490 return;
305             }
306              
307              
308             sub _check_vies {
309 5     5   11 my $self = shift;
310 5         14 my $nif = $self->nif;
311            
312 5 50       19 unless ($self->status) {
313 0         0 $self->vies_error("No se puede validar VIES: NIF no es válido");
314 0         0 $self->vies_check(0);
315 0         0 return;
316             }
317            
318 5 50 33     36 unless (defined $nif && length($nif) > 0) {
319 0         0 $self->vies_error("No hay NIF para validar en VIES");
320 0         0 $self->vies_check(0);
321 0         0 return;
322             }
323            
324 5         10 eval {
325 5         46 require Business::Tax::VAT::Validation;
326            
327 5         47 my $VatValidation = Business::Tax::VAT::Validation->new();
328            
329 5 50       322 unless ($VatValidation) {
330 0         0 die "Error Business::Tax::VAT::Validation";
331             }
332            
333 5         20 $self->vies_check($VatValidation->check($self->vat));
334            
335 5 50       7822761 unless ($self->vies_check) {
336 5   50     29 $self->vies_error( $VatValidation->get_last_error() // 'Error VIES');
337             }
338             };
339            
340 5 50       224 if ($@) {
341 0           $self->vies_error("Error VIES: $@");
342 0           $self->vies_check(0);
343             }
344              
345             }
346              
347              
348             1;
349              
350             __END__