Neuer Minimaltestfall für Rabattrundung im PTC
[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   }, "${title}: calculated data");
181 }
182
183 sub test_default_invoice_two_items_19_7_tax_not_included() {
184   reset_state();
185
186   my $item1   = new_item(qty => 2.5);
187   my $item2   = new_item(qty => 1.2, part => $parts[1]);
188   my $invoice = new_invoice(
189     taxincluded  => 0,
190     invoiceitems => [ $item1, $item2 ],
191   );
192
193   my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
194   my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
195
196   # item 1:
197   # sellprice 2.34 * qty 2.5 = 5.85
198   # 19%(5.85) = 1.1115; rounded = 1.11
199   # total rounded = 6.96
200
201   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
202   # line marge_total = 1.02
203   # line marge_percent = 17.4358974358974
204
205   # item 2:
206   # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
207   # 7%(11.6568) = 0.815976; rounded = 0.82
208   # total rounded = 12.48
209
210   # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
211   # line marge_total = 5.09
212   # line marge_percent = 43.6535162950257
213
214   my $title = 'default invoice, two item, 19/7% tax not included';
215   my %data  = $invoice->calculate_prices_and_taxes;
216
217   is($item1->marge_total,        1.02,             "${title}: item1 marge_total");
218   is($item1->marge_percent,      17.4358974358974, "${title}: item1 marge_percent");
219   is($item1->marge_price_factor, 1,                "${title}: item1 marge_price_factor");
220
221   is($item2->marge_total,        5.09,             "${title}: item2 marge_total");
222   is($item2->marge_percent,      43.6535162950257, "${title}: item2 marge_percent");
223   is($item2->marge_price_factor, 1,                "${title}: item2 marge_price_factor");
224
225   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
226   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
227   is($invoice->marge_total,      1.02 + 5.09,      "${title}: marge_total");
228   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
229
230   is_deeply(\%data, {
231     allocated                                     => {},
232     amounts                                       => {
233       $buchungsgruppe->income_accno_id($taxzone)  => {
234         amount                                    => 5.85,
235         tax_id                                    => $tax->id,
236         taxkey                                    => 3,
237       },
238       $buchungsgruppe7->income_accno_id($taxzone) => {
239         amount                                    => 11.66,
240         tax_id                                    => $tax7->id,
241         taxkey                                    => 2,
242       },
243     },
244     amounts_cogs                                  => {},
245     assembly_items                                => [
246       [], [],
247     ],
248     exchangerate                                  => 1,
249     taxes                                         => {
250       $tax->chart_id                              => 1.11,
251       $tax7->chart_id                             => 0.82,
252     },
253     items                                        => [
254       { linetotal                                => 5.85,
255         linetotal_cost                           => 4.83,
256         sellprice                                => 2.34,
257         tax_amount                               => 1.1115,
258         taxkey_id                                => $taxkey1->id,
259       },
260       { linetotal                                => 11.66,
261         linetotal_cost                           => 6.57,
262         sellprice                                => 9.714,
263         tax_amount                               => 0.8162,
264         taxkey_id                                => $taxkey2->id,
265       },
266     ],
267   }, "${title}: calculated data");
268 }
269
270 sub test_default_invoice_three_items_sellprice_rounding_discount() {
271   reset_state();
272
273   my $item1   = new_item(qty => 1, sellprice => 5.55, discount => .05);
274   my $item2   = new_item(qty => 1, sellprice => 5.50, discount => .05);
275   my $item3   = new_item(qty => 1, sellprice => 5.00, discount => .05);
276   my $invoice = new_invoice(
277     taxincluded  => 0,
278     invoiceitems => [ $item1, $item2, $item3 ],
279   );
280
281   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
282
283   # this is how price_tax_calculator is implemented. It differs from
284   # the way sales_order / invoice - forms are calculating:
285   # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
286   # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
287   # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
288   # ...
289
290   # item 1:
291   # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
292   # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
293   # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
294   # 19%(5.27) = 1.0013; rounded = 1.00
295   # total rounded = 6.27
296
297   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
298   # line marge_total = 3.34
299   # line marge_percent = 63.3776091081594
300
301   # item 2:
302   # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
303   # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
304   # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
305   # 19%(5.22) = 0.9918; rounded = 0.99
306   # total rounded = 6.21
307
308   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
309   # line marge_total = 5.22 - 1.93 = 3.29
310   # line marge_percent = 3.29/5.22 = 0.630268199233716
311
312   # item 3:
313   # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
314   # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
315   # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
316   # 19%(4.75) = 0.9025; rounded = 0.90
317   # total rounded = 5.65
318
319   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
320   # line marge_total = 2.82
321   # line marge_percent = 59.3684210526316
322
323   my $title = 'default invoice, three items, sellprice, rounding, discount';
324   my %data  = $invoice->calculate_prices_and_taxes;
325
326   is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
327   is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
328   is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
329
330   is($item2->marge_total,        3.29,               "${title}: item2 marge_total");
331   is($item2->marge_percent,      63.0268199233716,  "${title}: item2 marge_percent");
332   is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
333
334   is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
335   is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
336   is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
337
338   is($invoice->netamount,        5.27 + 5.22 + 4.75, "${title}: netamount");
339
340   # 6.27 + 6.21 + 5.65 = 18.13
341   # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
342   #is($invoice->amount,           6.27 + 6.21 + 5.65, "${title}: amount");
343   is($invoice->amount,           18.14,              "${title}: amount");
344
345   is($invoice->marge_total,      3.34 + 3.29 + 2.82, "${title}: marge_total");
346   is($invoice->marge_percent,    62.007874015748,    "${title}: marge_percent");
347
348   is_deeply(\%data, {
349     allocated                                    => {},
350     amounts                                      => {
351       $buchungsgruppe->income_accno_id($taxzone) => {
352         amount                                   => 15.24,
353         tax_id                                   => $tax->id,
354         taxkey                                   => 3,
355       },
356     },
357     amounts_cogs                                 => {},
358     assembly_items                               => [
359       [], [], [],
360     ],
361     exchangerate                                 => 1,
362     taxes                                        => {
363       $tax->chart_id                             => 2.9,
364     },
365     items                                        => [
366       { linetotal                                => 5.27,
367         linetotal_cost                           => 1.93,
368         sellprice                                => 5.27,
369         tax_amount                               => 1.0013,
370         taxkey_id                                => $taxkeys{$item1->parts_id}->id,
371       },
372       { linetotal                                => 5.22,
373         linetotal_cost                           => 1.93,
374         sellprice                                => 5.22,
375         tax_amount                               => 0.9918,
376         taxkey_id                                => $taxkeys{$item2->parts_id}->id,
377       },
378       { linetotal                                => 4.75,
379         linetotal_cost                           => 1.93,
380         sellprice                                => 4.75,
381         tax_amount                               => 0.9025,
382         taxkey_id                                => $taxkeys{$item3->parts_id}->id,
383       }
384     ],
385   }, "${title}: calculated data");
386 }
387
388 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
389   reset_state();
390
391   my $item   = new_item(qty => 6, part => $parts[2], discount => 0.03);
392   my $invoice = new_invoice(
393     taxincluded  => 0,
394     invoiceitems => [ $item ],
395   );
396
397   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
398
399   # PTC and ar form calculate linetotal differently:
400   # 6 parts for 0.60 with 3% discount
401   #
402   # ar form:
403   # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
404   # total = 3.49 + 0.66 = 4.15
405   #
406   # PTC:
407   # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
408   # sellprice = sellprice 0.60 - discount 0.02  = 0.58
409   # linetotal = sellprice 0.58 * qty 6 = 3.48
410   # 19%(3.48) = 0.6612; rounded = 0.66
411   # total rounded = 3.48 + 0.66 = 4.14
412
413   my $title = 'default invoice, one item, sellprice, rounding, discount';
414   my %data  = $invoice->calculate_prices_and_taxes;
415
416   is($invoice->netamount,         3.48,              "${title}: netamount");
417
418   is($invoice->amount,            4.14,              "${title}: amount");
419
420   is($invoice->marge_total,       3.48,              "${title}: marge_total");
421   is($invoice->marge_percent,      100,              "${title}: marge_percent");
422
423   is_deeply(\%data, {
424     allocated                                    => {},
425     amounts                                      => {
426       $buchungsgruppe->income_accno_id($taxzone) => {
427         amount                                   => 3.48,
428         tax_id                                   => $tax->id,
429         taxkey                                   => 3,
430       },
431     },
432     amounts_cogs                                 => {},
433     assembly_items                               => [
434       [],
435     ],
436     exchangerate                                 => 1,
437     taxes                                        => {
438       $tax->chart_id                             => 0.66,
439     },
440     items                                        => [
441       { linetotal                                => 3.48,
442         linetotal_cost                           => 0,
443         sellprice                                => 0.58,
444         tax_amount                               => 0.6612,
445         taxkey_id                                => $taxkeys{$item->parts_id}->id,
446       },
447     ],
448   }, "${title}: calculated data");
449 }
450
451 Support::TestSetup::login();
452
453 test_default_invoice_one_item_19_tax_not_included();
454 test_default_invoice_two_items_19_7_tax_not_included();
455 test_default_invoice_three_items_sellprice_rounding_discount();
456 test_default_invoice_one_item_19_tax_not_included_rounding_discount();
457
458 clear_up();
459 done_testing();