Nachtrag Steuerzone - Tests angepasst
[kivitendo-erp.git] / t / db_helper / price_tax_calculator.t
1 use Test::More;
2
3 use strict;
4
5 use lib 't';
6 use utf8;
7
8 use Carp;
9 use Data::Dumper;
10 use Support::TestSetup;
11 use Test::Exception;
12
13 use SL::DB::Buchungsgruppe;
14 use SL::DB::Currency;
15 use SL::DB::Customer;
16 use SL::DB::Employee;
17 use SL::DB::Invoice;
18 use SL::DB::Part;
19 use SL::DB::Unit;
20
21 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
22
23 sub reset_state {
24   my %params = @_;
25
26   $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
27
28   SL::DB::Manager::Invoice->delete_all(all => 1);
29   SL::DB::Manager::Part->delete_all(all => 1);
30   SL::DB::Manager::Customer->delete_all(all => 1);
31
32   $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
33   $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%')                                || croak "No accounting group for 7\%";
34   $unit            = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} })                                      || croak "No unit";
35   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
36   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
37   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                                              || croak "No tax for 7\%";
38   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                                           || croak "No taxzone";
39
40   $currency_id     = $::instance_conf->get_currency_id;
41
42   $customer     = SL::DB::Customer->new(
43     name        => 'Test Customer',
44     currency_id => $currency_id,
45     %{ $params{customer} }
46   )->save;
47
48   @parts = ();
49   push @parts, SL::DB::Part->new(
50     partnumber         => 'T4254',
51     description        => 'Fourty-two fifty-four',
52     lastcost           => 1.93,
53     sellprice          => 2.34,
54     buchungsgruppen_id => $buchungsgruppe->id,
55     unit               => $unit->name,
56     %{ $params{part1} }
57   )->save;
58
59   push @parts, SL::DB::Part->new(
60     partnumber         => 'T0815',
61     description        => 'Zero EIGHT fifteeN @ 7%',
62     lastcost           => 5.473,
63     sellprice          => 9.714,
64     buchungsgruppen_id => $buchungsgruppe7->id,
65     unit               => $unit->name,
66     %{ $params{part2} }
67   )->save;
68 }
69
70 sub new_invoice {
71   my %params  = @_;
72
73   return SL::DB::Invoice->new(
74     customer_id => $customer->id,
75     currency_id => $currency_id,
76     employee_id => $employee->id,
77     salesman_id => $employee->id,
78     gldate      => DateTime->today_local->to_kivitendo,
79     taxzone_id  => $taxzone->id,
80     transdate   => DateTime->today_local->to_kivitendo,
81     invoice     => 1,
82     type        => 'invoice',
83     %params,
84   );
85 }
86
87 sub new_item {
88   my (%params) = @_;
89
90   my $part = delete($params{part}) || $parts[0];
91
92   return SL::DB::InvoiceItem->new(
93     parts_id    => $part->id,
94     lastcost    => $part->lastcost,
95     sellprice   => $part->sellprice,
96     description => $part->description,
97     unit        => $part->unit,
98     %params,
99   );
100 }
101
102 sub test_default_invoice_one_item_19_tax_not_included() {
103   reset_state();
104
105   my $item    = new_item(qty => 2.5);
106   my $invoice = new_invoice(
107     taxincluded  => 0,
108     invoiceitems => [ $item ],
109   );
110
111   # sellprice 2.34 * qty 2.5 = 5.85
112   # 19%(5.85) = 1.1115; rounded = 1.11
113   # total rounded = 6.96
114
115   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
116   # line marge_total = 1.02
117   # line marge_percent = 17.4358974358974
118
119   my $title = 'default invoice, one item, 19% tax not included';
120   my %data  = $invoice->calculate_prices_and_taxes;
121
122   is($item->marge_total,        1.02,             "${title}: item marge_total");
123   is($item->marge_percent,      17.4358974358974, "${title}: item marge_percent");
124   is($item->marge_price_factor, 1,                "${title}: item marge_price_factor");
125
126   is($invoice->netamount,       5.85,             "${title}: netamount");
127   is($invoice->amount,          6.96,             "${title}: amount");
128   is($invoice->marge_total,     1.02,             "${title}: marge_total");
129   is($invoice->marge_percent,   17.4358974358974, "${title}: marge_percent");
130
131   is_deeply(\%data, {
132     allocated                                    => {},
133     amounts                                      => {
134       $buchungsgruppe->income_accno_id($taxzone) => {
135         amount                                   => 5.85,
136         tax_id                                   => $tax->id,
137         taxkey                                   => 3,
138       },
139     },
140     amounts_cogs                                 => {},
141     assembly_items                               => [
142       [],
143     ],
144     exchangerate                                 => 1,
145     taxes                                        => {
146       $tax->chart_id                             => 1.11,
147     },
148   }, "${title}: calculated data");
149 }
150
151 sub test_default_invoice_two_items_19_7_tax_not_included() {
152   reset_state();
153
154   my $item1   = new_item(qty => 2.5);
155   my $item2   = new_item(qty => 1.2, part => $parts[1]);
156   my $invoice = new_invoice(
157     taxincluded  => 0,
158     invoiceitems => [ $item1, $item2 ],
159   );
160
161   # item 1:
162   # sellprice 2.34 * qty 2.5 = 5.85
163   # 19%(5.85) = 1.1115; rounded = 1.11
164   # total rounded = 6.96
165
166   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
167   # line marge_total = 1.02
168   # line marge_percent = 17.4358974358974
169
170   # item 2:
171   # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
172   # 7%(11.6568) = 0.815976; rounded = 0.82
173   # total rounded = 12.48
174
175   # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
176   # line marge_total = 5.09
177   # line marge_percent = 43.6535162950257
178
179   my $title = 'default invoice, two item, 19/7% tax not included';
180   my %data  = $invoice->calculate_prices_and_taxes;
181
182   is($item1->marge_total,        1.02,             "${title}: item1 marge_total");
183   is($item1->marge_percent,      17.4358974358974, "${title}: item1 marge_percent");
184   is($item1->marge_price_factor, 1,                "${title}: item1 marge_price_factor");
185
186   is($item2->marge_total,        5.09,             "${title}: item2 marge_total");
187   is($item2->marge_percent,      43.6535162950257, "${title}: item2 marge_percent");
188   is($item2->marge_price_factor, 1,                "${title}: item2 marge_price_factor");
189
190   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
191   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
192   is($invoice->marge_total,      1.02 + 5.09,      "${title}: marge_total");
193   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
194
195   is_deeply(\%data, {
196     allocated                                     => {},
197     amounts                                       => {
198       $buchungsgruppe->income_accno_id($taxzone)  => {
199         amount                                    => 5.85,
200         tax_id                                    => $tax->id,
201         taxkey                                    => 3,
202       },
203       $buchungsgruppe7->income_accno_id($taxzone) => {
204         amount                                    => 11.66,
205         tax_id                                    => $tax7->id,
206         taxkey                                    => 2,
207       },
208     },
209     amounts_cogs                                  => {},
210     assembly_items                                => [
211       [], [],
212     ],
213     exchangerate                                  => 1,
214     taxes                                         => {
215       $tax->chart_id                              => 1.11,
216       $tax7->chart_id                             => 0.82,
217     },
218   }, "${title}: calculated data");
219 }
220
221 sub test_default_invoice_three_items_sellprice_rounding_discount() {
222   reset_state();
223
224   my $item1   = new_item(qty => 1, sellprice => 5.55, discount => .05);
225   my $item2   = new_item(qty => 1, sellprice => 5.50, discount => .05);
226   my $item3   = new_item(qty => 1, sellprice => 5.00, discount => .05);
227   my $invoice = new_invoice(
228     taxincluded  => 0,
229     invoiceitems => [ $item1, $item2, $item3 ],
230   );
231
232   # this is how price_tax_calculator is implemented. It differs from
233   # the way sales_order / invoice - forms are calculating:
234   # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
235   # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
236   # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
237   # ...
238
239   # item 1:
240   # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
241   # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
242   # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
243   # 19%(5.27) = 1.0013; rounded = 1.00
244   # total rounded = 6.27
245
246   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
247   # line marge_total = 3.34
248   # line marge_percent = 63.3776091081594
249
250   # item 2:
251   # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
252   # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
253   # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
254   # 19%(5.22) = 0.9918; rounded = 0.99
255   # total rounded = 6.21
256
257   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
258   # line marge_total = 5.22 - 1.93 = 3.29
259   # line marge_percent = 3.29/5.22 = 0.630268199233716
260
261   # item 3:
262   # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
263   # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
264   # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
265   # 19%(4.75) = 0.9025; rounded = 0.90
266   # total rounded = 5.65
267
268   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
269   # line marge_total = 2.82
270   # line marge_percent = 59.3684210526316
271
272   my $title = 'default invoice, three items, sellprice, rounding, discount';
273   my %data  = $invoice->calculate_prices_and_taxes;
274
275   is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
276   is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
277   is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
278
279   is($item2->marge_total,        3.29,               "${title}: item2 marge_total");
280   is($item2->marge_percent,      63.0268199233716,  "${title}: item2 marge_percent");
281   is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
282
283   is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
284   is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
285   is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
286
287   is($invoice->netamount,        5.27 + 5.22 + 4.75, "${title}: netamount");
288
289   # 6.27 + 6.21 + 5.65 = 18.13
290   # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
291   #is($invoice->amount,           6.27 + 6.21 + 5.65, "${title}: amount");
292   is($invoice->amount,           18.14,              "${title}: amount");
293
294   is($invoice->marge_total,      3.34 + 3.29 + 2.82, "${title}: marge_total");
295   is($invoice->marge_percent,    62.007874015748,    "${title}: marge_percent");
296
297   is_deeply(\%data, {
298     allocated                                    => {},
299     amounts                                      => {
300       $buchungsgruppe->income_accno_id($taxzone) => {
301         amount                                   => 15.24,
302         tax_id                                   => $tax->id,
303         taxkey                                   => 3,
304       },
305     },
306     amounts_cogs                                 => {},
307     assembly_items                               => [
308       [], [], [],
309     ],
310     exchangerate                                 => 1,
311     taxes                                        => {
312       $tax->chart_id                             => 2.9,
313     },
314   }, "${title}: calculated data");
315 }
316
317 Support::TestSetup::login();
318
319 test_default_invoice_one_item_19_tax_not_included();
320 test_default_invoice_two_items_19_7_tax_not_included();
321 test_default_invoice_three_items_sellprice_rounding_discount();
322
323 done_testing();