PTC Test um Rundungsgenauigkeit ergänzt
[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 List::MoreUtils qw(uniq);
11 use Support::TestSetup;
12 use Test::Exception;
13
14 use SL::DB::Buchungsgruppe;
15 use SL::DB::Currency;
16 use SL::DB::Customer;
17 use SL::DB::DeliveryOrder;
18 use SL::DB::Employee;
19 use SL::DB::Invoice;
20 use SL::DB::Order;
21 use SL::DB::Part;
22 use SL::DB::Unit;
23 use SL::DB::TaxZone;
24
25 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
26
27 sub clear_up {
28   SL::DB::Manager::Order->delete_all(all => 1);
29   SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
30   SL::DB::Manager::Invoice->delete_all(all => 1);
31   SL::DB::Manager::Part->delete_all(all => 1);
32   SL::DB::Manager::Customer->delete_all(all => 1);
33 };
34
35 sub reset_state {
36   my %params = @_;
37
38   $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
39
40   clear_up();
41
42   $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
43   $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%')                                || croak "No accounting group for 7\%";
44   $unit            = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} })                                      || croak "No unit";
45   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
46   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
47   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                                              || croak "No tax for 7\%";
48   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                                           || croak "No taxzone";
49
50   $currency_id     = $::instance_conf->get_currency_id;
51
52   $customer     = SL::DB::Customer->new(
53     name        => 'Test Customer',
54     currency_id => $currency_id,
55     taxzone_id  => $taxzone->id,
56     %{ $params{customer} }
57   )->save;
58
59   @parts = ();
60   push @parts, SL::DB::Part->new(
61     partnumber         => 'T4254',
62     description        => 'Fourty-two fifty-four',
63     lastcost           => 1.93,
64     sellprice          => 2.34,
65     buchungsgruppen_id => $buchungsgruppe->id,
66     unit               => $unit->name,
67     %{ $params{part1} }
68   )->save;
69
70   push @parts, SL::DB::Part->new(
71     partnumber         => 'T0815',
72     description        => 'Zero EIGHT fifteeN @ 7%',
73     lastcost           => 5.473,
74     sellprice          => 9.714,
75     buchungsgruppen_id => $buchungsgruppe7->id,
76     unit               => $unit->name,
77     %{ $params{part2} }
78   )->save;
79
80   push @parts, SL::DB::Part->new(
81     partnumber         => 'T888',
82     description        => 'Triple 8',
83     lastcost           => 0,
84     sellprice          => 0.6,
85     buchungsgruppen_id => $buchungsgruppe->id,
86     unit               => $unit->name,
87     %{ $params{part3} }
88   )->save;
89
90 }
91
92 sub new_invoice {
93   my %params  = @_;
94
95   return SL::DB::Invoice->new(
96     customer_id => $customer->id,
97     currency_id => $currency_id,
98     employee_id => $employee->id,
99     salesman_id => $employee->id,
100     gldate      => DateTime->today_local->to_kivitendo,
101     taxzone_id  => $taxzone->id,
102     transdate   => DateTime->today_local->to_kivitendo,
103     invoice     => 1,
104     type        => 'invoice',
105     %params,
106   );
107 }
108
109 sub new_item {
110   my (%params) = @_;
111
112   my $part = delete($params{part}) || $parts[0];
113
114   return SL::DB::InvoiceItem->new(
115     parts_id    => $part->id,
116     lastcost    => $part->lastcost,
117     sellprice   => $part->sellprice,
118     description => $part->description,
119     unit        => $part->unit,
120     %params,
121   );
122 }
123
124 sub test_default_invoice_one_item_19_tax_not_included() {
125   reset_state();
126
127   my $item    = new_item(qty => 2.5);
128   my $invoice = new_invoice(
129     taxincluded  => 0,
130     invoiceitems => [ $item ],
131   );
132
133   my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
134
135   # sellprice 2.34 * qty 2.5 = 5.85
136   # 19%(5.85) = 1.1115; rounded = 1.11
137   # total rounded = 6.96
138
139   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
140   # line marge_total = 1.02
141   # line marge_percent = 17.4358974358974
142
143   my $title = 'default invoice, one item, 19% tax not included';
144   my %data  = $invoice->calculate_prices_and_taxes;
145
146   is($item->marge_total,        1.02,             "${title}: item marge_total");
147   is($item->marge_percent,      17.4358974358974, "${title}: item marge_percent");
148   is($item->marge_price_factor, 1,                "${title}: item marge_price_factor");
149
150   is($invoice->netamount,       5.85,             "${title}: netamount");
151   is($invoice->amount,          6.96,             "${title}: amount");
152   is($invoice->marge_total,     1.02,             "${title}: marge_total");
153   is($invoice->marge_percent,   17.4358974358974, "${title}: marge_percent");
154
155   is_deeply(\%data, {
156     allocated                                    => {},
157     amounts                                      => {
158       $buchungsgruppe->income_accno_id($taxzone) => {
159         amount                                   => 5.85,
160         tax_id                                   => $tax->id,
161         taxkey                                   => 3,
162       },
163     },
164     amounts_cogs                                 => {},
165     assembly_items                               => [
166       [],
167     ],
168     exchangerate                                 => 1,
169     taxes                                        => {
170       $tax->chart_id                             => 1.11,
171     },
172     items                                        => [
173       { linetotal                                => 5.85,
174         linetotal_cost                           => 4.83,
175         sellprice                                => 2.34,
176         tax_amount                               => 1.1115,
177         taxkey_id                                => $taxkey->id,
178       },
179     ],
180     rounding                                    =>  0,
181   }, "${title}: calculated data");
182 }
183
184 sub test_default_invoice_two_items_19_7_tax_not_included() {
185   reset_state();
186
187   my $item1   = new_item(qty => 2.5);
188   my $item2   = new_item(qty => 1.2, part => $parts[1]);
189   my $invoice = new_invoice(
190     taxincluded  => 0,
191     invoiceitems => [ $item1, $item2 ],
192   );
193
194   my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
195   my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
196
197   # item 1:
198   # sellprice 2.34 * qty 2.5 = 5.85
199   # 19%(5.85) = 1.1115; rounded = 1.11
200   # total rounded = 6.96
201
202   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
203   # line marge_total = 1.02
204   # line marge_percent = 17.4358974358974
205
206   # item 2:
207   # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
208   # 7%(11.6568) = 0.815976; rounded = 0.82
209   # total rounded = 12.48
210
211   # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
212   # line marge_total = 5.09
213   # line marge_percent = 43.6535162950257
214
215   my $title = 'default invoice, two item, 19/7% tax not included';
216   my %data  = $invoice->calculate_prices_and_taxes;
217
218   is($item1->marge_total,        1.02,             "${title}: item1 marge_total");
219   is($item1->marge_percent,      17.4358974358974, "${title}: item1 marge_percent");
220   is($item1->marge_price_factor, 1,                "${title}: item1 marge_price_factor");
221
222   is($item2->marge_total,        5.09,             "${title}: item2 marge_total");
223   is($item2->marge_percent,      43.6535162950257, "${title}: item2 marge_percent");
224   is($item2->marge_price_factor, 1,                "${title}: item2 marge_price_factor");
225
226   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
227   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
228   is($invoice->marge_total,      1.02 + 5.09,      "${title}: marge_total");
229   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
230
231   is_deeply(\%data, {
232     allocated                                     => {},
233     amounts                                       => {
234       $buchungsgruppe->income_accno_id($taxzone)  => {
235         amount                                    => 5.85,
236         tax_id                                    => $tax->id,
237         taxkey                                    => 3,
238       },
239       $buchungsgruppe7->income_accno_id($taxzone) => {
240         amount                                    => 11.66,
241         tax_id                                    => $tax7->id,
242         taxkey                                    => 2,
243       },
244     },
245     amounts_cogs                                  => {},
246     assembly_items                                => [
247       [], [],
248     ],
249     exchangerate                                  => 1,
250     taxes                                         => {
251       $tax->chart_id                              => 1.11,
252       $tax7->chart_id                             => 0.82,
253     },
254     items                                        => [
255       { linetotal                                => 5.85,
256         linetotal_cost                           => 4.83,
257         sellprice                                => 2.34,
258         tax_amount                               => 1.1115,
259         taxkey_id                                => $taxkey1->id,
260       },
261       { linetotal                                => 11.66,
262         linetotal_cost                           => 6.57,
263         sellprice                                => 9.714,
264         tax_amount                               => 0.8162,
265         taxkey_id                                => $taxkey2->id,
266       },
267     ],
268     rounding                                    =>  0,
269   }, "${title}: calculated data");
270 }
271
272 sub test_default_invoice_three_items_sellprice_rounding_discount() {
273   reset_state();
274
275   my $item1   = new_item(qty => 1, sellprice => 5.55, discount => .05);
276   my $item2   = new_item(qty => 1, sellprice => 5.50, discount => .05);
277   my $item3   = new_item(qty => 1, sellprice => 5.00, discount => .05);
278   my $invoice = new_invoice(
279     taxincluded  => 0,
280     invoiceitems => [ $item1, $item2, $item3 ],
281   );
282
283   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
284
285   # this is how price_tax_calculator is implemented. It differs from
286   # the way sales_order / invoice - forms are calculating:
287   # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
288   # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
289   # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
290   # ...
291
292   # item 1:
293   # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
294   # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
295   # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
296   # 19%(5.27) = 1.0013; rounded = 1.00
297   # total rounded = 6.27
298
299   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
300   # line marge_total = 3.34
301   # line marge_percent = 63.3776091081594
302
303   # item 2:
304   # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
305   # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
306   # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
307   # 19%(5.22) = 0.9918; rounded = 0.99
308   # total rounded = 6.21
309
310   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
311   # line marge_total = 5.22 - 1.93 = 3.29
312   # line marge_percent = 3.29/5.22 = 0.630268199233716
313
314   # item 3:
315   # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
316   # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
317   # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
318   # 19%(4.75) = 0.9025; rounded = 0.90
319   # total rounded = 5.65
320
321   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
322   # line marge_total = 2.82
323   # line marge_percent = 59.3684210526316
324
325   my $title = 'default invoice, three items, sellprice, rounding, discount';
326   my %data  = $invoice->calculate_prices_and_taxes;
327
328   is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
329   is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
330   is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
331
332   is($item2->marge_total,        3.29,               "${title}: item2 marge_total");
333   is($item2->marge_percent,      63.0268199233716,  "${title}: item2 marge_percent");
334   is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
335
336   is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
337   is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
338   is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
339
340   is($invoice->netamount,        5.27 + 5.22 + 4.75, "${title}: netamount");
341
342   # 6.27 + 6.21 + 5.65 = 18.13
343   # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
344   #is($invoice->amount,           6.27 + 6.21 + 5.65, "${title}: amount");
345   is($invoice->amount,           18.14,              "${title}: amount");
346
347   is($invoice->marge_total,      3.34 + 3.29 + 2.82, "${title}: marge_total");
348   is($invoice->marge_percent,    62.007874015748,    "${title}: marge_percent");
349
350   is_deeply(\%data, {
351     allocated                                    => {},
352     amounts                                      => {
353       $buchungsgruppe->income_accno_id($taxzone) => {
354         amount                                   => 15.24,
355         tax_id                                   => $tax->id,
356         taxkey                                   => 3,
357       },
358     },
359     amounts_cogs                                 => {},
360     assembly_items                               => [
361       [], [], [],
362     ],
363     exchangerate                                 => 1,
364     taxes                                        => {
365       $tax->chart_id                             => 2.9,
366     },
367     items                                        => [
368       { linetotal                                => 5.27,
369         linetotal_cost                           => 1.93,
370         sellprice                                => 5.27,
371         tax_amount                               => 1.0013,
372         taxkey_id                                => $taxkeys{$item1->parts_id}->id,
373       },
374       { linetotal                                => 5.22,
375         linetotal_cost                           => 1.93,
376         sellprice                                => 5.22,
377         tax_amount                               => 0.9918,
378         taxkey_id                                => $taxkeys{$item2->parts_id}->id,
379       },
380       { linetotal                                => 4.75,
381         linetotal_cost                           => 1.93,
382         sellprice                                => 4.75,
383         tax_amount                               => 0.9025,
384         taxkey_id                                => $taxkeys{$item3->parts_id}->id,
385       }
386     ],
387     rounding                                    =>  0,
388   }, "${title}: calculated data");
389 }
390
391 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
392   reset_state();
393
394   my $item   = new_item(qty => 6, part => $parts[2], discount => 0.03);
395   my $invoice = new_invoice(
396     taxincluded  => 0,
397     invoiceitems => [ $item ],
398   );
399
400   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
401
402   # PTC and ar form calculate linetotal differently:
403   # 6 parts for 0.60 with 3% discount
404   #
405   # ar form:
406   # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
407   # total = 3.49 + 0.66 = 4.15
408   #
409   # PTC:
410   # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
411   # sellprice = sellprice 0.60 - discount 0.02  = 0.58
412   # linetotal = sellprice 0.58 * qty 6 = 3.48
413   # 19%(3.48) = 0.6612; rounded = 0.66
414   # total rounded = 3.48 + 0.66 = 4.14
415
416   my $title = 'default invoice, one item, sellprice, rounding, discount';
417   my %data  = $invoice->calculate_prices_and_taxes;
418
419   is($invoice->netamount,         3.48,              "${title}: netamount");
420
421   is($invoice->amount,            4.14,              "${title}: amount");
422
423   is($invoice->marge_total,       3.48,              "${title}: marge_total");
424   is($invoice->marge_percent,      100,              "${title}: marge_percent");
425
426   is_deeply(\%data, {
427     allocated                                    => {},
428     amounts                                      => {
429       $buchungsgruppe->income_accno_id($taxzone) => {
430         amount                                   => 3.48,
431         tax_id                                   => $tax->id,
432         taxkey                                   => 3,
433       },
434     },
435     amounts_cogs                                 => {},
436     assembly_items                               => [
437       [],
438     ],
439     exchangerate                                 => 1,
440     taxes                                        => {
441       $tax->chart_id                             => 0.66,
442     },
443     items                                        => [
444       { linetotal                                => 3.48,
445         linetotal_cost                           => 0,
446         sellprice                                => 0.58,
447         tax_amount                               => 0.6612,
448         taxkey_id                                => $taxkeys{$item->parts_id}->id,
449       },
450     ],
451     rounding                                    =>  0,
452   }, "${title}: calculated data");
453 }
454
455 Support::TestSetup::login();
456
457 test_default_invoice_one_item_19_tax_not_included();
458 test_default_invoice_two_items_19_7_tax_not_included();
459 test_default_invoice_three_items_sellprice_rounding_discount();
460 test_default_invoice_one_item_19_tax_not_included_rounding_discount();
461
462 clear_up();
463 done_testing();