File Coverage

lib/Business/Tax/Withholding/JP.pm
Criterion Covered Total %
statement 55 55 100.0
branch 13 14 92.8
condition 3 3 100.0
subroutine 16 16 100.0
pod 6 8 75.0
total 93 96 96.8


line stmt bran cond sub pod time code
1             package Business::Tax::Withholding::JP;
2 6     6   298990 use 5.008001;
  6         53  
3 6     6   27 use strict;
  6         7  
  6         114  
4 6     6   32 use warnings;
  6         15  
  6         185  
5 6     6   1860 use Time::Seconds;
  6         12670  
  6         406  
6              
7             our $VERSION = "0.08";
8              
9 6     6   35 use constant { border => 1000000 };
  6         8  
  6         563  
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   2730 use Moose;
  6         2332031  
  6         44  
43 6     6   40698 use Time::Piece;
  6         31926  
  6         28  
44             my $t = localtime();
45              
46             has price => ( is => 'rw', isa => 'Int', default => 0 );
47             has amount => ( is => 'rw', isa => 'Int', default => 1 );
48             has date => ( is => 'rw', isa => 'Str', default => $t->ymd() );
49             has no_wh => ( is => 'ro', isa => 'Bool', default => 0 );
50              
51             __PACKAGE__->meta->make_immutable;
52 6     6   745 no Moose;
  6         11  
  6         34  
53              
54             sub net {
55 5     5 1 20 my $self = shift;
56 5         96 return $self->price(@_);
57             }
58              
59             sub subtotal {
60 131     131 1 134 my $self = shift;
61 131         2605 return $self->price() * $self->amount();
62             }
63              
64             sub tax {
65 48     48 1 71 my $self = shift;
66 48         66 return int( $self->subtotal() * $self->tax_rate() );
67             }
68              
69             sub full {
70 29     29 1 41 my $self = shift;
71 29         46 return int( $self->subtotal() + $self->tax() );
72             }
73              
74             sub withholding {
75 36     36 1 2271 my $self = shift;
76 36 100       804 return 0 if $self->no_wh();
77 24         54 my $rate = $self->wh_rate();
78 24 100       176 if( $self->subtotal() <= border ) {
79 18         31 return int( $self->subtotal() * $rate );
80             }else{
81 6         8 my $base = $self->subtotal() - border;
82 6         33 return int( $base * $rate * 2 + border * $rate );
83             }
84             }
85              
86             sub tax_rate {
87 48     48 0 59 my $self = shift;
88 48         868 my $date = $t->strptime( $self->date(), '%Y-%m-%d' );
89 48 100       4091 return 0 if $date < $t->strptime( $history[0]{'since'}, '%Y-%m-%d' );
90 46 100       3923 return $consumption{'rate'} if $date > $t->strptime( $history[-1]{'until'}, '%Y-%m-%d' ) + ONE_DAY;
91              
92 10         1434 foreach my $h (@history) {
93 22 100       1624 next unless $date < $t->strptime( $h->{'until'}, '%Y-%m-%d' ) + ONE_DAY;
94 8 50       1052 return $h->{'rate'} if $date >= $t->strptime( $h->{'since'}, '%Y-%m-%d' );
95             }
96 2         265 return $consumption{'rate'};
97             }
98              
99             sub wh_rate {
100 24     24 0 27 my $self = shift;
101 24         31 my $rate = $withholding{'rate'};
102 24         62 my $since = $t->strptime( $special{'since'}, '%Y-%m-%d' );
103 24         1859 my $until = $t->strptime( $special{'until'}, '%Y-%m-%d' );
104 24         2088 my $date = $t->strptime( $self->date(), '%Y-%m-%d' );
105              
106 24 100 100     1662 return $rate if $date < $since or $until < $date;
107 20         693 return $rate + $special{'rate'};
108             }
109              
110             sub total {
111 18     18 1 31 my $self = shift;
112 18         35 return $self->full - $self->withholding;
113             }
114              
115             1;
116             __END__
117              
118             =encoding utf-8
119              
120             =head1 NAME
121              
122             Business::Tax::Withholding::JP - auto calculation for Japanese tax and withholding
123              
124             Business::Tax::Withholding::JP - 日本の消費税と源泉徴収のややこしい計算を自動化します。
125            
126             =head1 SYNOPSIS
127              
128             use Business::Tax::Withholding::JP;
129             my $calc = Business::Tax::Withholding::JP->new( price => 10000 );
130              
131             $calc->net(); # 10000
132             $calc->amount(); # 1
133             $calc->subtotal(); # 10000
134             $calc->tax(); # 800
135             $calc->full(); # 10800
136             $calc->withholding(); # 1021
137             $calc->total(); # 9779
138              
139             # Or you can set the date in period of special tax will expire
140             $calc = Business::Tax::Withholding::JP->new( date => '2038-01-01' );
141             $calc->price(10000);
142             $calc->withholding(); # 1000
143             $calc->total(); # 9800
144              
145             # And you may ignore the withholdings
146             $calc = Business::Tax::Withholding::JP->new( no_wh => 1 );
147             $calc->price(10000); # 10000
148             $calc->amount(2); # 2
149             $calc->subtotal(); # 20000
150             $calc->tax(); # 1600
151             $calc->withholding(); # 0
152             $calc->total(); # 21600
153              
154             # After 2019/10/01, this module will calculate with 10% consumption tax
155             $calc = Business::Tax::Withholding::JP->new( price => 10000 );
156             $calc->net(); # 10000
157             $calc->amount(2); # 2
158             $calc->subtotal(); # 20000
159             $calc->tax(); # 2000
160             $calc->withholding(); # 2042
161             $calc->total(); # 19958
162              
163             =head1 DESCRIPTION
164              
165             Business::Tax::Withholding::JP
166             is useful calculator for long term in Japanese Business.
167              
168             You can get correctly taxes and withholdings from price in your context
169             without worrying about the special tax for reconstructing from the Earthquake.
170              
171             the consumption tax B<rate is 8% (automatically up to 10% after 2019/10/01)>
172            
173             You can also ignore the withholings. It means this module can be a tax calculator
174              
175             Business::Tax::Withholding::JP は日本のビジネスで長期的に使えるモジュールです。
176             特別復興所得税の期限を心配することなく、請求価格から正しく税金額と源泉徴収額を計算できます。
177             なお、源泉徴収をしない経理にも対応します。B<消費税率は8%、2019å¹´10月1日から自動的に10ï¼…> です。
178            
179             =head2 Constructor
180              
181             =head3 new( price => I<Int>, amount => I<Int>, date => I<Date>, no_wh => I<Bool> );
182              
183             You can omit these paramators.
184              
185             パラメータは指定しなくて構いません。
186            
187             =over
188            
189             =item price
190            
191             the price of your products will be set. defaults 0.
192            
193             税抜価格を指定してください。指定しなければ0です。
194              
195             =item amount
196            
197             the amount of your products will be set. defaults 1.
198            
199             数量を指定してください。指定しなければ1です。
200              
201             =item date
202              
203             You can set payday. the net of tax and withholding depends on this. default is today.
204            
205             支払日を指定してください。消費税額と源泉徴収額が変動します。指定しなければ今日として計算します。
206            
207             =item no_wh
208            
209             If you set this flag, the all you can get is only tax and total. defaults 0 and this is read-only.
210              
211             このフラグを立てるとこのモジュールの長所を台無しにできます。初期値はもちろん0で、あとから変えることはできません。
212            
213             =back
214            
215             =head2 Methods and subroutine
216              
217             =over
218              
219             =item price
220              
221             You can reset the price.
222              
223             price に値を代入可能です。
224            
225             =item amount
226            
227             You can reset the amount.
228            
229             amount に値を代入可能です。
230              
231             =item date
232              
233             You can reset the payday like 'YYYY-MM-DD'
234              
235             date にも値を代入可能です。フォーマットは'YYYY-MM-DD'(-区切り)です。
236              
237             =item net
238              
239             You can get the net of your pay. it's equal to the price.
240             So it's the alias of price().
241            
242             net は price と同じ働きをします。
243            
244             =item subtotal
245            
246             it returns price() * amount()
247              
248             subtotal は値と数量の積(小計)を返します。
249            
250             =item tax
251            
252             You can get the net of your tax.
253            
254             税額のみを取得したい場合はこちらを
255              
256             =item full
257            
258             You can get the net of your pay including tax.
259              
260             税込金額を知りたい場合はこちらを
261            
262             =item withholding
263            
264             You can get the net of your withholding from your pay.
265              
266             源泉徴収額を知りたい場合はこちらを
267            
268             =item total
269              
270             You can get the total of your pay including tax without withholding
271            
272             源泉徴収額を差し引いた税込支払額を知りたい場合はこちらをお使いください。
273            
274             =back
275              
276             =head1 LICENSE
277              
278             Copyright (C) Yuki Yoshida(worthmine).
279              
280             This library is free software; you can redistribute it and/or modify
281             it under the same terms as Perl itself.
282              
283             =head1 AUTHOR
284              
285             Yuki Yoshida E<lt>worthmine@gmail.comE<gt>
286              
287             =cut