blib/lib/Business/ID/NPWP.pm

filter: Statementstmt 93.3% 42 / 45 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 90.5% 67 / 74 CC 12 SCAR 20.6
CC
12
Cov
90%
parse_npwp
11.5
_z
2.0
BEGIN
1.0
CRAP 7.9
↑ Summary
1 package Business::ID::NPWP;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2024-07-11'; # DATE
11 2 our $DIST = 'Business-ID-NPWP'; # DIST
12 2 our $VERSION = '0.092'; # VERSION
13  
14 2 our @EXPORT_OK = qw(parse_npwp);
15  
16 2 our %SPEC;
17  
18 117 ▶sub _z { $_[0] > 9 ? $_[0]-9 : $_[0] }
Subroutine _z: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 9 ? : 20 97 117
19  
20 $SPEC{parse_npwp} = {
21 2 v => 1.1,
22 summary => 'Parse Indonesian taxpayer registration number (NPWP)',
23 args => {
24 npwp => {
25 summary => 'Input NPWP to be parsed',
26 schema => 'str',
27 pos => 0,
28 req => 1,
29 },
30 allow_nik => {
31 schema => 'bool*',
32 description => <<'MARKDOWN',
33  
34 Based on PMK 112 Tahun 2022, Indonesian citizenship registration number (NIK -
35 nomor induk kependudukan) is also allowed as NPWP. NIK has 16 digits, while the
36 traditional NPWP is 15 digits. A prefix 0 will be added later so all NPWP will
37 be 16 digits in the future.
38  
39 By default, NPWP is allowed to have 16 digits but the first digit must be 0. If
40 this setting is set to true, NPWP with the first digit other than 0 is also
41 allowed but will be checked for NIK validness.
42  
43 MARKDOWN
44 default => 0,
45 },
46 },
47 };
48 17 ▶sub parse_npwp {
Subroutine parse_npwp: called CC=11 SCAR=24.4
Pod documented
49 17 my %args = @_;
50  
51 17 ▶ my $npwp = $args{npwp} or return [400, "Please specify npwp"];
Branch: 1 branch
BranchTrueFalseTotal
my $npwp = $args{"npwp"} or return [400, "Please specify npwp"] 16 1 17
52 16 my $res = {};
53  
54 16 $npwp =~ s/^\s+//;
55 # assume A = 0 if not specified
56 0 ▶ if ($npwp =~ /^\d\./) { $npwp = "0$npwp" }
Branch: 1 branch
BranchTrueFalseTotal
if ($npwp =~ /^\d\./) 0 16 16
57  
58 16 $npwp =~ s/\D+//g;
59 # assume BBB = 000 if not specified
60 0 ▶ if (length($npwp) == 12) { $npwp .= "000" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $npwp == 12) 0 16 16
61  
62 16 ▶ if (length $npwp == 16) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $npwp == 16) { } 4 12 16
elsif (length $npwp != 15) { } 0 12 12
63 4 ▶ if ($npwp =~ s/\A0//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($npwp =~ s/\A0//) { } 1 3 4
elsif ($args{'allow_nik'}) { } 2 1 3
64 # will be checked as traditional NPWP
65 } elsif ($args{allow_nik}) {
66 2 require Business::ID::NIK;
67 2 my $nik_res = Business::ID::NIK::parse_nik(nik => $npwp);
68 2 ▶ return $nik_res unless $nik_res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $nik_res->[0] == 200 1 1 2
69 1 $nik_res->[2]{is_nik} = 1;
70 1 return $nik_res;
71 } else {
72 1 return [400, "For 16-digit NPWP, the first digit must be 0"];
73 }
74 } elsif (length $npwp != 15) {
75 0 return [400, "Must be 15 or 16 digit"];
76 }
77  
78 13 $npwp =~ /^(.)(.)(.)(.)(.)(.)(.)(.)(.)/;
79 13 ▶ if ((_z(1*$1) + _z(2*$2) + _z(1*$3) +
Branch: 1 branch
BranchTrueFalseTotal
if ((_z(1 * $1) + _z(2 * $2) + _z(1 * $3) + _z(2 * $4) + _z(1 * $5) + _z(2 * $6) + _z(1 * $7) + _z(2 * $8) + _z(1 * $9)) % 10) 1 12 13
80 _z(2*$4) + _z(1*$5) + _z(2*$6) +
81 _z(1*$7) + _z(2*$8) + _z(1*$9)) % 10) {
82 1 return [400, "Wrong check digit"];
83 }
84  
85 (
86 $res->{taxpayer_code}, $res->{serial}, $res->{check_digit},
87 $res->{tax_office_code}, $res->{branch_code},
88 12 ) = $npwp =~ /(..)(.{6})(.)(...)(...)/;
89  
90 12 ▶ return [400, "Serial starts from 1, not 0"] if $res->{serial} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"serial"} < 1 0 12 12
91  
92 $res->{normalized} = join(
93 "",
94 $res->{taxpayer_code}, ".",
95 substr($res->{serial}, 0, 3), ".", substr($res->{serial}, 3), ".",
96 $res->{check_digit}, "-",
97 $res->{tax_office_code}, ".", $res->{branch_code},
98 12 );
99  
100 12 [200, "OK", $res];
101 }
102  
103 2 1;
104 # ABSTRACT: Parse Indonesian taxpayer registration number (NPWP)
105  
106 __END__
↑ Summary