Finanzcontrollingbericht: Spalte »Vorgangsbezeichnung« anzeigen
[kivitendo-erp.git] / t / form / arap.t
1 use strict;
2 use Test::More;
3
4 use lib 't';
5 use Support::TestSetup;
6 use Carp;
7 use Test::Exception;
8
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 
12
13 my ($ar_tax_19, $ar_tax_7,$ar_tax_0);
14 my $config = {};
15 $config->{numberformat} = '1.000,00';
16
17 sub reset_state {
18   my %params = @_;
19
20   $params{$_} ||= {} for qw(ar_tax_19 ar_tax_7 ar_tax_0 );
21
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"};
26       };
27   };
28
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";
32
33 };
34
35 sub arap_test {
36   my ($testcase) = @_;
37
38   reset_state;
39
40   # values from testcase
41   $::form->{taxincluded}     = $testcase->{taxincluded};
42   $::form->{currency}        = $testcase->{currency};
43   $::form->{rowcount}        = scalar @{$testcase->{lines}};
44
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});
48
49   foreach my $a ( 1 .. scalar @{$testcase->{lines}} ) {
50     my ($taxrate, $form_amount, $netamount, $taxamount, $totalamount) = @{ @{ $testcase->{lines} }[$a-1] };
51     my $tax;
52     if ( $taxrate == 19 ) {
53         $tax = $ar_tax_19;
54     } elsif ( $taxrate == 7 ) {
55         $tax = $ar_tax_7;
56     } elsif ( $taxrate == 0 ) {
57         $tax = $ar_tax_0;
58     } else {
59         croak "illegal taxrate $taxrate";
60     };
61
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;
65
66   };
67
68   # calculate totals using lines in $::form
69   ($::form->{netamount},$::form->{total_tax},$::form->{amount}) = $::form->calculate_arap($testcase->{'buysell'}, $::form->{taxincluded}, $testcase->{'exchangerate'});
70
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'}");
76
77 };
78
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
83
84   my ($calculated_net,$calculated_tax) = $::form->calculate_tax($amount,$rate,$taxincluded,$dec);
85
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");
88 };
89
90 Support::TestSetup::login();
91
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);
112
113 # create testcases, made up of one or more lines, with expected values
114
115 my $testcase1 = {
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:
121     taxincluded     => 1,
122     exchangerate    => 1,
123     currency        => 'EUR',
124     buysell         => 'sell',
125 };
126
127 my $testcase2 = {
128     lines           => [ \@testline1, \@testline2, \@testline3 ],
129     total_amount    => '82,82',
130     total_netamount => '70,95',
131     total_taxamount => '11,87',
132     taxincluded     => 1,
133     exchangerate    => 1,
134     currency        => 'EUR',
135     buysell         => 'sell',
136 };
137
138 my $testcase3 = {
139     lines           => [ \@testline4 ],
140     total_amount    => '158,37',
141     total_netamount => '133,08',
142     total_taxamount => '25,29',
143     taxincluded     => 0,
144     exchangerate    => 1,
145     currency        => 'EUR',
146     buysell         => 'sell',
147 };
148
149 my $testcase4 = {
150     lines           => [ \@testline5 ],
151     total_amount    => '83,00',
152     total_netamount => '83,00',
153     total_taxamount => '0,00',
154     taxincluded     => 0,
155     exchangerate    => '0,83',
156     currency        => 'USD',
157     buysell         => 'sell',
158 };
159
160 my $testcase6 = {
161     lines           => [ \@testline6 ],
162     total_amount    => '56,53',
163     total_netamount => '47,50',
164     total_taxamount => '9,03',
165     taxincluded     => 1,
166     exchangerate    => 1,
167     currency        => 'EUR',
168     buysell         => 'buy',
169 };
170
171 my $testcase7 = {
172     lines           => [ \@testline7 ],
173     total_netamount => '309,86',
174     total_taxamount => '58,87',
175     total_amount    => '368,73',
176     taxincluded     => 0,
177     exchangerate    => 1,
178     currency        => 'EUR',
179     buysell         => 'buy',
180 };
181
182 my $testcase8 = {
183     lines           => [ \@testline8 ],
184     total_netamount => '121,50',
185     total_taxamount => '8,50',
186     total_amount    => '130,00',
187     taxincluded     => 1,
188     exchangerate    => 1,
189     currency        => 'EUR',
190     buysell         => 'buy',
191 };
192
193 my $testcase9 = {
194     lines           => [ \@testline9 ],
195     total_netamount => '121,49',
196     total_taxamount => '8,50',
197     total_amount    => '129,99',
198     taxincluded     => 0,
199     exchangerate    => 1,
200     currency        => 'EUR',
201     buysell         => 'buy',
202 };
203
204 my $testcase10 = {
205     lines           => [ \@testline10 ],
206     total_netamount => '121,50',
207     total_taxamount => '8,51',
208     total_amount    => '130,01',
209     taxincluded     => 0,
210     exchangerate    => 1,
211     currency        => 'EUR',
212     buysell         => 'buy',
213 };
214
215 my $testcase11 = {
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',
221     taxincluded     => 0,
222     exchangerate    => 1,
223     currency        => 'EUR',
224     buysell         => 'buy',
225 };
226
227 my $testcase12 = {
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',
233     taxincluded     => 0,
234     exchangerate    => 1,
235     currency        => 'EUR',
236     buysell         => 'buy',
237 };
238
239 my $testcase13 = {
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',
245     taxincluded     => 0,
246     exchangerate    => 1,
247     currency        => 'EUR',
248     buysell         => 'buy',
249 };
250
251 my $testcase14 = {
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',
257     taxincluded     => 1,
258     exchangerate    => 1,
259     currency        => 'EUR',
260     buysell         => 'buy',
261 };
262
263 # run tests
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);
277
278 # tests for calculate_tax:
279
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);
283
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);
287
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);
291
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);
295
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);
299
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);
302
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);
306
307 done_testing(82);
308
309 1;