File Coverage

lib/Business/Tax/Withholding/JP.pm
Criterion Covered Total %
statement 61 61 100.0
branch 13 14 92.8
condition 3 3 100.0
subroutine 18 18 100.0
pod 6 8 75.0
total 101 104 97.1


line stmt bran cond sub pod time code
1             package Business::Tax::Withholding::JP;
2 6     6   964997 use 5.008001;
  6         22  
3 6     6   36 use strict;
  6         10  
  6         185  
4 6     6   24 use warnings;
  6         12  
  6         390  
5 6     6   2607 use Time::Seconds;
  6         17934  
  6         681  
6              
7             our $VERSION = "0.93";
8              
9 6     6   44 use constant { border => 1000000 };
  6         9  
  6         766  
10              
11             my %consumption = (
12             rate => 0.10,
13             name => '消費税',
14             );
15              
16             my @history = ({
17             rate => 0.03,
18             since => '1989-04-01',
19             until => '1997-03-31',
20             },{
21             rate => 0.05,
22             since => '1997-04-01',
23             until => '2014-03-31',
24             },{
25             rate => 0.08,
26             since => '2014-04-01',
27             until => '2019-09-30',
28             });
29              
30             my %withholding = (
31             rate => 0.10,
32             name => '源泉徴収',
33             );
34              
35             my %special = (
36             rate => 0.0021,
37             name => '復興特別所得税',
38             since => '2013-01-01',
39             until => '2037-12-31',
40             );
41              
42 6     6   3079 use Time::Piece;
  6         66136  
  6         28  
43             my $t = localtime();
44              
45             # rewrite with Type::Tiny
46 6     6   3918 use Moo;
  6         49147  
  6         1611  
47 6     6   19672 use Types::Standard qw( Int Str Bool );
  6         809061  
  6         94  
48 6     6   25906 use Type::Params qw( signature );
  6         35357  
  6         71  
49 6     6   5625 use namespace::clean;
  6         113927  
  6         86  
50              
51             has 'price' => ( is => 'rw', isa => Int, default => 0 );
52             has 'amount' => ( is => 'rw', isa => Int, default => 1 );
53             has 'date' => ( is => 'rw', isa => Str, default => sub { $t->ymd() } );
54             has 'no_wh' => ( is => 'ro', isa => Bool, default => 0 );
55              
56             sub net {
57 5     5 1 14925 my $self = shift;
58 5         146 return $self->price(@_);
59             }
60              
61             sub subtotal {
62 131     131 1 5442 my $self = shift;
63 131         3852 return $self->price() * $self->amount();
64             }
65              
66             sub tax {
67 48     48 1 23306 my $self = shift;
68 48         130 return int( $self->subtotal() * $self->tax_rate() );
69             }
70              
71             sub full {
72 29     29 1 59 my $self = shift;
73 29         78 return int( $self->subtotal() + $self->tax() );
74             }
75              
76             sub withholding {
77 36     36 1 8729 my $self = shift;
78 36 100       315 return 0 if $self->no_wh();
79 24         81 my $rate = $self->wh_rate();
80 24 100       1055 if( $self->subtotal() <= border ) {
81 18         704 return int( $self->subtotal() * $rate );
82             }else{
83 6         263 my $base = $self->subtotal() - border;
84 6         210 return int( $base * $rate * 2 + border * $rate );
85             }
86             }
87              
88             sub tax_rate {
89 48     48 0 1563 my $self = shift;
90 48         1043 my $date = $t->strptime( $self->date(), '%Y-%m-%d' );
91 48 100       2482 return 0 if $date < $t->strptime( $history[0]{'since'}, '%Y-%m-%d' );
92 46 100       9785 return $consumption{'rate'} if $date > $t->strptime( $history[-1]{'until'}, '%Y-%m-%d' ) + ONE_DAY;
93              
94 10         1909 foreach my $h (@history) {
95 22 100       2355 next unless $date < $t->strptime( $h->{'until'}, '%Y-%m-%d' ) + ONE_DAY;
96 8 50       1536 return $h->{'rate'} if $date >= $t->strptime( $h->{'since'}, '%Y-%m-%d' );
97             }
98 2         367 return $consumption{'rate'};
99             }
100              
101             sub wh_rate {
102 24     24 0 43 my $self = shift;
103 24         58 my $rate = $withholding{'rate'};
104 24         98 my $since = $t->strptime( $special{'since'}, '%Y-%m-%d' );
105 24         1030 my $until = $t->strptime( $special{'until'}, '%Y-%m-%d' );
106 24         1542 my $date = $t->strptime( $self->date(), '%Y-%m-%d' );
107              
108 24 100 100     948 return $rate if $date < $since or $until < $date;
109 20         5661 return $rate + $special{'rate'};
110             }
111              
112             sub total {
113 18     18 1 9168 my $self = shift;
114 18         74 return $self->full - $self->withholding;
115             }
116              
117             1;
118             __END__