File Coverage

blib/lib/Business/BR/CNPJ.pm
Criterion Covered Total %
statement 34 42 80.9
branch 13 18 72.2
condition 6 9 66.6
subroutine 8 10 80.0
pod 5 5 100.0
total 66 84 78.5


line stmt bran cond sub pod time code
1            
2             package Business::BR::CNPJ;
3            
4 3     3   78358 use 5;
  3         11  
  3         378  
5 3     3   24 use strict;
  3         5  
  3         128  
6 3     3   18 use warnings;
  3         7  
  3         1024  
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.0022';
20            
21 3     3   1601 use Business::BR::Ids::Common qw(_dot _canon_id);
  3         7  
  3         3731  
22            
23             sub canon_cnpj {
24 204     204 1 969 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 1110 my $cnpj = canon_cnpj shift;
33 204 50       464 return undef if length $cnpj != 14;
34 204         1092 my @cnpj = split '', $cnpj;
35 204         915 my $s1 = _dot([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], \@cnpj) % 11;
36 204         1148 my $s2 = _dot([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2, 1], \@cnpj) % 11;
37 204 100 66     1738 unless ($s1==0 || $s1==1 && $cnpj[12]==0) {
      66        
38 2         16 return 0;
39             }
40 202 100 66     1844 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   247 my $base = shift; # expected to be canon'ed already ?!
69 200 50       363 my $valid = @_ ? shift : 1;
70 200 100       331 my $dev = $valid ? 0 : 2; # deviation (to make CNPJ invalid)
71 200         1167 my @base = split '', substr($base, 0, 12);
72 200         1180 my $dv1 = -_dot([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], \@base) % 11 % 10;
73 200         1892 my $dv2 = (-_dot([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], [ @base, $dv1 ]) + $dev) % 11 % 10;
74 200 50       774 return ($dv1, $dv2) if wantarray;
75 200         539 substr($base, 12, 2) = "$dv1$dv2";
76 200         859 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 60994 my $valid = @_ ? shift : 1; # valid CNPJ by default
87 200         738 my $base = sprintf "%08s", int(rand(1E8)); # 8 dígitos
88 200 100       608 my $var = sprintf "%04s", ((rand()<0.95) ? "0001" : int(sqrt rand(1E8)));
89 200         575 return scalar _dv_cnpj("$base$var", $valid);
90             }
91            
92            
93             1;
94            
95             __END__