5 use Support::TestSetup;
9 # this test tests the functions calculate_arap and calculate_tax in SL/Form.pm
10 # calculate_arap is used for post_invoice in AR and AP
11 # calculate_tax is used in calculate_arap as well as update in ar/ap/gl and post_transaction in gl
13 my ($ar_tax_19, $ar_tax_7,$ar_tax_0);
15 $config->{numberformat} = '1.000,00';
20 $params{$_} ||= {} for qw(ar_tax_19 ar_tax_7 ar_tax_0 );
22 # delete rowcount lines in form, would be better to reset form completely
23 for my $hv ( 1 .. 10 ) {
24 foreach my $type ( qw(amount tax tax_id tax_chart) ) {
25 delete $::form{"$type\_$hv"};
29 $ar_tax_19 = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{ar_tax_19} }) || croak "No 19% tax";
30 $ar_tax_7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07, %{ $params{ar_tax_7} }) || croak "No 7% tax";
31 $ar_tax_0 = SL::DB::Manager::Tax->find_by(taxkey => 0, rate => 0.00, %{ $params{ar_tax_0} }) || croak "No 0% tax";
40 # values from testcase
41 $::form->{taxincluded} = $testcase->{taxincluded};
42 $::form->{currency} = $testcase->{currency};
43 $::form->{rowcount} = scalar @{$testcase->{lines}};
45 # parse exchangerate, because it was added in the same numberformat as the
46 # other amounts in the testcases
47 $testcase->{exchangerate} = $::form->parse_amount(\%::myconfig, $testcase->{exchangerate});
49 foreach my $a ( 1 .. scalar @{$testcase->{lines}} ) {
50 my ($taxrate, $form_amount, $netamount, $taxamount, $totalamount) = @{ @{ $testcase->{lines} }[$a-1] };
52 if ( $taxrate == 19 ) {
54 } elsif ( $taxrate == 7 ) {
56 } elsif ( $taxrate == 0 ) {
59 croak "illegal taxrate $taxrate";
62 $::form->{"amount_$a"} = $form_amount;
63 $::form->{"tax_$a"} = $taxamount; # tax according to UI, will recalculate anyway?
64 $::form->{"taxchart_$a"} = $tax->id . '--' . $tax->rate;
68 # calculate totals using lines in $::form
69 ($::form->{netamount},$::form->{total_tax},$::form->{amount}) = $::form->calculate_arap($testcase->{'buysell'}, $::form->{taxincluded}, $testcase->{'exchangerate'});
71 # create tests comparing calculated and expected values
72 is($::form->format_amount(\%::myconfig , $::form->{total_tax} , 2) , $testcase->{'total_taxamount'} , "total tax = $testcase->{'total_taxamount'}");
73 is($::form->format_amount(\%::myconfig , $::form->{netamount} , 2) , $testcase->{'total_netamount'} , "netamount = $testcase->{'total_netamount'}");
74 is($::form->format_amount(\%::myconfig , $::form->{amount} , 2) , $testcase->{'total_amount'} , "totalamount = $testcase->{'total_amount'}");
75 is($::form->{taxincluded}, $testcase->{'taxincluded'}, "taxincluded = $testcase->{'taxincluded'}");
79 sub calculate_tax_test {
80 my ($amount, $rate, $taxincluded, $net, $tax, $total, $dec) = @_;
81 # amount, rate and taxincluded are the values that we want to calculate with
82 # net, tax and total are the values that we expect, dec is the number of decimals we round to
84 my ($calculated_net,$calculated_tax) = $::form->calculate_tax($amount,$rate,$taxincluded,$dec);
86 is($tax, $calculated_tax, "calculated tax for taxincluded = $taxincluded for net $amount and rate $rate is = $calculated_tax");
87 is($calculated_net, $net, "calculated net for taxincluded = $taxincluded for net $amount and rate $rate is = $net");
90 Support::TestSetup::login();
92 # define the various lines that can be used for the testcases
93 # always use positive values for buy/sell, like in the interface
94 # tax input net tax total type
95 my @testline1 = qw(19 56,53 47,50 9,03 56,53 sell);
96 my @testline2 = qw(19 11,90 10,00 1,90 11,90 sell);
97 my @testline3 = qw( 7 14,39 13,45 0,94 11,90 sell);
98 my @testline4 = qw(19 133,08 133,08 25,29 158,37 sell);
99 my @testline5 = qw( 0 100,00 83,00 0,00 83,00 sell); # exchangerate of 0,83
100 my @testline6 = qw(19 56,53 47,50 9,03 56,53 buy);
101 my @testline7 = qw(19 309,86 309,86 58,87 368,73 buy);
102 my @testline8 = qw( 7 130,00 121,50 8,50 130,00 buy);
103 my @testline9 = qw( 7 121,49 121,49 8,50 129,99 buy);
104 my @testline10 = qw( 7 121,50 121,50 8,51 130,01 buy);
105 my @testline11 = qw(19 -2,77 -2,77 -0,53 -3,30 buy);
106 my @testline12 = qw( 7 12,88 12,88 0,90 13,78 buy);
107 my @testline13 = qw(19 41,93 41,93 7,97 49,90 buy);
108 my @testline14 = qw(19 84,65 84,65 16,08 107,73 buy);
109 my @testline15 = qw(19 8,39 8,39 1,59 9,98 buy);
110 my @testline16 = qw(19 100,73 84,65 16,08 107,73 buy);
111 my @testline17 = qw(19 9,99 8,39 1,60 9,99 buy);
113 # create testcases, made up of one or more lines, with expected values
116 lines => [ \@testline1 ], # lines to be used in testcase
117 total_amount => '56,53', # expected result
118 total_netamount => '47,50', # expected result
119 total_taxamount => '9,03', # expected result
120 # invoice parameters:
128 lines => [ \@testline1, \@testline2, \@testline3 ],
129 total_amount => '82,82',
130 total_netamount => '70,95',
131 total_taxamount => '11,87',
139 lines => [ \@testline4 ],
140 total_amount => '158,37',
141 total_netamount => '133,08',
142 total_taxamount => '25,29',
150 lines => [ \@testline5 ],
151 total_amount => '83,00',
152 total_netamount => '83,00',
153 total_taxamount => '0,00',
155 exchangerate => '0,83',
161 lines => [ \@testline6 ],
162 total_amount => '56,53',
163 total_netamount => '47,50',
164 total_taxamount => '9,03',
172 lines => [ \@testline7 ],
173 total_netamount => '309,86',
174 total_taxamount => '58,87',
175 total_amount => '368,73',
183 lines => [ \@testline8 ],
184 total_netamount => '121,50',
185 total_taxamount => '8,50',
186 total_amount => '130,00',
194 lines => [ \@testline9 ],
195 total_netamount => '121,49',
196 total_taxamount => '8,50',
197 total_amount => '129,99',
205 lines => [ \@testline10 ],
206 total_netamount => '121,50',
207 total_taxamount => '8,51',
208 total_amount => '130,01',
216 # mixed invoices, -2,77€ net with 19% as credit note, 12,88€ net with 7%
217 lines => [ \@testline11 , \@testline12 ],
218 total_netamount => '10,11',
219 total_taxamount => '0,37',
220 total_amount => '10,48',
228 # ap transaction, example from bug 2435
229 lines => [ \@testline13 ],
230 total_netamount => '41,93',
231 total_taxamount => '7,97',
232 total_amount => '49,90',
240 # ap transaction, example from bug 2094, tax not included
241 lines => [ \@testline14 , \@testline15 ],
242 total_netamount => '93,04',
243 total_taxamount => '17,67',
244 total_amount => '110,71',
252 # ap transaction, example from bug 2094, tax included
253 lines => [ \@testline16 , \@testline17 ],
254 total_netamount => '93,04',
255 total_taxamount => '17,68',
256 total_amount => '110,72',
264 arap_test($testcase1);
265 arap_test($testcase2);
266 arap_test($testcase3);
267 arap_test($testcase4);
268 arap_test($testcase6);
269 arap_test($testcase7);
270 arap_test($testcase8);
271 arap_test($testcase9);
272 arap_test($testcase10);
273 arap_test($testcase11);
274 arap_test($testcase12);
275 arap_test($testcase13);
276 arap_test($testcase14);
278 # tests for calculate_tax:
280 # tests for 1 Cent, calculated tax should be 0
281 calculate_tax_test(0.01,0.07,1,0.01,0.00,0.01,2);
282 calculate_tax_test(0.01,0.19,1,0.01,0.00,0.01,2);
284 # tax for rate 7% taxincluded flips at 0.08
285 calculate_tax_test(0.07,0.07,1,0.07,0.00,0.07,2);
286 calculate_tax_test(0.08,0.07,1,0.07,0.01,0.08,2);
288 # tax for rate 7% taxexcluded flips at 0.08
289 calculate_tax_test(0.07,0.07,0,0.07,0.00,0.07,2);
290 calculate_tax_test(0.08,0.07,0,0.08,0.01,0.09,2);
292 # tax for rate 19% taxexcluded flips at 0.03
293 calculate_tax_test(0.02,0.19,0,0.02,0.00,0.02,2);
294 calculate_tax_test(0.03,0.19,0,0.03,0.01,0.04,2);
296 # tax for rate 19% taxincluded flips at 0.04
297 calculate_tax_test(0.03,0.19,1,0.03,0.00,0.03,2);
298 calculate_tax_test(0.04,0.19,1,0.03,0.01,0.04,2);
300 calculate_tax_test(8.39,0.19,0,8.39,1.59,9.98,2);
301 calculate_tax_test(9.99,0.19,1,8.39,1.60,9.99,2);
303 calculate_tax_test(11.21,0.07,0,11.21,0.78,11.99,2);
304 calculate_tax_test(11.22,0.07,0,11.22,0.79,12.01,2);
305 calculate_tax_test(12.00,0.07,1,11.21,0.79,12.00,2);