line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Business::BR::CNPJ;
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
31327
|
use 5;
|
|
3
|
|
|
|
|
9
|
|
5
|
3
|
|
|
3
|
|
13
|
use strict;
|
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
77
|
|
6
|
3
|
|
|
3
|
|
19
|
use warnings;
|
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
333
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter;
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
#our %EXPORT_TAGS = ( 'all' => [ qw() ] );
|
13
|
|
|
|
|
|
|
#our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
14
|
|
|
|
|
|
|
#our @EXPORT = qw();
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw( canon_cnpj format_cnpj parse_cnpj random_cnpj );
|
17
|
|
|
|
|
|
|
our @EXPORT = qw( test_cnpj );
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.00_16';
|
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
3
|
|
883
|
use Business::BR::Ids::Common qw(_dot _canon_id);
|
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1887
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub canon_cnpj {
|
24
|
204
|
|
|
204
|
1
|
366
|
return _canon_id(shift, size => 14);
|
25
|
|
|
|
|
|
|
}
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# there is a subtle difference here between the return for
|
28
|
|
|
|
|
|
|
# for an input which is not 14 digits long (undef)
|
29
|
|
|
|
|
|
|
# and one that does not satisfy the check equations (0).
|
30
|
|
|
|
|
|
|
# Correct CNPJ numbers return 1.
|
31
|
|
|
|
|
|
|
sub test_cnpj {
|
32
|
204
|
|
|
204
|
1
|
537
|
my $cnpj = canon_cnpj shift;
|
33
|
204
|
50
|
|
|
|
340
|
return undef if length $cnpj != 14;
|
34
|
204
|
|
|
|
|
531
|
my @cnpj = split '', $cnpj;
|
35
|
204
|
|
|
|
|
491
|
my $s1 = _dot([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], \@cnpj) % 11;
|
36
|
204
|
|
|
|
|
883
|
my $s2 = _dot([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 1], \@cnpj) % 11;
|
37
|
204
|
100
|
66
|
|
|
529
|
unless ($s1==0 || $s1==1 && $cnpj[12]==0) {
|
|
|
|
66
|
|
|
|
|
38
|
2
|
|
|
|
|
10
|
return 0;
|
39
|
|
|
|
|
|
|
}
|
40
|
202
|
100
|
66
|
|
|
1238
|
return ($s2==0 || $s2==1 && $cnpj[13]==0) ? 1 : 0;
|
41
|
|
|
|
|
|
|
}
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub format_cnpj {
|
45
|
0
|
|
|
0
|
1
|
0
|
my $cnpj = canon_cnpj shift;
|
46
|
0
|
|
|
|
|
0
|
$cnpj =~ s|^(..)(...)(...)(....)(..).*|$1.$2.$3/$4-$5|;
|
47
|
0
|
|
|
|
|
0
|
return $cnpj;
|
48
|
|
|
|
|
|
|
}
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub parse_cnpj {
|
51
|
0
|
|
|
0
|
1
|
0
|
my $cnpj = canon_cnpj shift;
|
52
|
0
|
|
|
|
|
0
|
my ($base, $filial, $dv) = $cnpj =~ /(\d{8})(\d{4})(\d{2})/;
|
53
|
0
|
0
|
|
|
|
0
|
if (wantarray) {
|
54
|
0
|
|
|
|
|
0
|
return ($base, $filial, $dv);
|
55
|
|
|
|
|
|
|
}
|
56
|
0
|
|
|
|
|
0
|
return { base => $base, filial => $filial, dv => $dv };
|
57
|
|
|
|
|
|
|
}
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# my ($dv1, $dv2) = _dv_cnpj('') # => $dv1 = ?, $dv2 = ?
|
60
|
|
|
|
|
|
|
# my ($dv1, $dv2) = _dv_cnpj('', 0) # computes non-valid check digits
|
61
|
|
|
|
|
|
|
#
|
62
|
|
|
|
|
|
|
# computes the check digits of the candidate CNPJ number given as argument
|
63
|
|
|
|
|
|
|
# (only the first 12 digits enter the computation)
|
64
|
|
|
|
|
|
|
#
|
65
|
|
|
|
|
|
|
# In list context, it returns the check digits.
|
66
|
|
|
|
|
|
|
# In scalar context, it returns the complete CNPJ (base and check digits)
|
67
|
|
|
|
|
|
|
sub _dv_cnpj {
|
68
|
200
|
|
|
200
|
|
168
|
my $base = shift; # expected to be canon'ed already ?!
|
69
|
200
|
50
|
|
|
|
248
|
my $valid = @_ ? shift : 1;
|
70
|
200
|
100
|
|
|
|
242
|
my $dev = $valid ? 0 : 2; # deviation (to make CNPJ invalid)
|
71
|
200
|
|
|
|
|
619
|
my @base = split '', substr($base, 0, 12);
|
72
|
200
|
|
|
|
|
623
|
my $dv1 = -_dot([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], \@base) % 11 % 10;
|
73
|
200
|
|
|
|
|
1125
|
my $dv2 = (-_dot([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], [ @base, $dv1 ]) + $dev) % 11 % 10;
|
74
|
200
|
50
|
|
|
|
473
|
return ($dv1, $dv2) if wantarray;
|
75
|
200
|
|
|
|
|
283
|
substr($base, 12, 2) = "$dv1$dv2";
|
76
|
200
|
|
|
|
|
566
|
return $base;
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
}
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# generates a random (correct or incorrect) CNPJ
|
81
|
|
|
|
|
|
|
# $cpf = rand_cnpj();
|
82
|
|
|
|
|
|
|
# $cpf = rand_cnpj($valid);
|
83
|
|
|
|
|
|
|
#
|
84
|
|
|
|
|
|
|
# if $valid==0, produces an invalid CNPJ.
|
85
|
|
|
|
|
|
|
sub random_cnpj {
|
86
|
200
|
100
|
|
200
|
1
|
42822
|
my $valid = @_ ? shift : 1; # valid CNPJ by default
|
87
|
200
|
|
|
|
|
685
|
my $base = sprintf "%08s", int(rand(1E8)); # 8 dígitos
|
88
|
200
|
100
|
|
|
|
410
|
my $var = sprintf "%04s", ((rand()<0.95) ? "0001" : int(sqrt rand(1E8)));
|
89
|
200
|
|
|
|
|
399
|
return scalar _dv_cnpj("$base$var", $valid);
|
90
|
|
|
|
|
|
|
}
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1;
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__
|