PTC-Tests: ein weiterer Test mit großen Mengen und kleinen Preisen
[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 use SL::Dev::ALL qw(:ALL);
14
15 use SL::DB::Buchungsgruppe;
16 use SL::DB::Currency;
17 use SL::DB::Customer;
18 use SL::DB::DeliveryOrder;
19 use SL::DB::Employee;
20 use SL::DB::Invoice;
21 use SL::DB::Order;
22 use SL::DB::Part;
23 use SL::DB::Unit;
24 use SL::DB::TaxZone;
25
26 my ($customer, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
27
28 sub clear_up {
29   SL::DB::Manager::Order->delete_all(all => 1);
30   SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
31   SL::DB::Manager::InvoiceItem->delete_all(all => 1);
32   SL::DB::Manager::Invoice->delete_all(all => 1);
33   SL::DB::Manager::Part->delete_all(all => 1);
34   SL::DB::Manager::Customer->delete_all(all => 1);
35 };
36
37 sub reset_state {
38   my %params = @_;
39
40   $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
41
42   clear_up();
43
44   $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
45   $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%')                                || croak "No accounting group for 7\%";
46   $unit            = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} })                                      || croak "No unit";
47   $employee        = SL::DB::Manager::Employee->current                                                                    || croak "No employee";
48   $tax             = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} })                           || croak "No tax";
49   $tax7            = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07)                                              || croak "No tax for 7\%";
50   $taxzone         = SL::DB::Manager::TaxZone->find_by( description => 'Inland')                                           || croak "No taxzone";
51
52   $customer     = new_customer(
53     name        => 'Test Customer',
54     taxzone_id  => $taxzone->id,
55     %{ $params{customer} }
56   )->save;
57
58   @parts = ();
59   push @parts, new_part(
60     partnumber         => 'T4254',
61     description        => 'Fourty-two fifty-four',
62     lastcost           => 1.93,
63     sellprice          => 2.34,
64     buchungsgruppen_id => $buchungsgruppe->id,
65     unit               => $unit->name,
66     %{ $params{part1} }
67   )->save;
68
69   push @parts, new_part(
70     partnumber         => 'T0815',
71     description        => 'Zero EIGHT fifteeN @ 7%',
72     lastcost           => 5.473,
73     sellprice          => 9.714,
74     buchungsgruppen_id => $buchungsgruppe7->id,
75     unit               => $unit->name,
76     %{ $params{part2} }
77   )->save;
78
79   push @parts, new_part(
80     partnumber         => 'T888',
81     description        => 'Triple 8',
82     lastcost           => 0,
83     sellprice          => 0.6,
84     buchungsgruppen_id => $buchungsgruppe->id,
85     unit               => $unit->name,
86     %{ $params{part3} }
87   )->save;
88
89 }
90
91 sub new_invoice {
92   my %params  = @_;
93
94   return create_sales_invoice(
95     taxzone_id  => $taxzone->id,
96     %params,
97   );
98 }
99
100 sub new_item {
101   my (%params) = @_;
102
103   my $part = delete($params{part}) || $parts[0];
104
105   return create_invoice_item(
106     part => $part,
107     %params,
108   );
109 }
110
111 sub test_default_invoice_one_item_19_tax_not_included() {
112   reset_state();
113
114   my $item = new_item(qty => 2.5);
115   my $invoice = new_invoice(
116     taxincluded  => 0,
117     invoiceitems => [ $item ],
118   );
119
120   my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
121
122   # sellprice 2.34 * qty 2.5 = 5.85
123   # 19%(5.85) = 1.1115; rounded = 1.11
124   # total rounded = 6.96
125
126   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
127   # line marge_total = 1.02
128   # line marge_percent = 17.4358974358974
129
130   my $title = 'default invoice, one item, 19% tax not included';
131   my %data  = $invoice->calculate_prices_and_taxes;
132
133   is($item->marge_total,        1.02,             "${title}: item marge_total");
134   is($item->marge_percent,      17.4358974358974, "${title}: item marge_percent");
135   is($item->marge_price_factor, 1,                "${title}: item marge_price_factor");
136
137   is($invoice->netamount,       5.85,             "${title}: netamount");
138   is($invoice->amount,          6.96,             "${title}: amount");
139   is($invoice->marge_total,     1.02,             "${title}: marge_total");
140   is($invoice->marge_percent,   17.4358974358974, "${title}: marge_percent");
141
142   is_deeply(\%data, {
143     allocated                                    => {},
144     amounts                                      => {
145       $buchungsgruppe->income_accno_id($taxzone) => {
146         amount                                   => 5.85,
147         tax_id                                   => $tax->id,
148         taxkey                                   => 3,
149       },
150     },
151     amounts_cogs                                 => {},
152     assembly_items                               => [
153       [],
154     ],
155     exchangerate                                 => 1,
156     taxes                                        => {
157       $tax->chart_id                             => 1.11,
158     },
159     items                                        => [
160       { linetotal                                => 5.85,
161         linetotal_cost                           => 4.83,
162         sellprice                                => 2.34,
163         tax_amount                               => 1.1115,
164         taxkey_id                                => $taxkey->id,
165       },
166     ],
167     rounding                                    =>  0,
168   }, "${title}: calculated data");
169 }
170
171 sub test_default_invoice_two_items_19_7_tax_not_included() {
172   reset_state();
173
174   my $item1   = new_item(qty => 2.5);
175   my $item2   = new_item(qty => 1.2, part => $parts[1]);
176   my $invoice = new_invoice(
177     taxincluded  => 0,
178     invoiceitems => [ $item1, $item2 ],
179   );
180
181   my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
182   my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
183
184   # item 1:
185   # sellprice 2.34 * qty 2.5 = 5.85
186   # 19%(5.85) = 1.1115; rounded = 1.11
187   # total rounded = 6.96
188
189   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
190   # line marge_total = 1.02
191   # line marge_percent = 17.4358974358974
192
193   # item 2:
194   # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
195   # 7%(11.6568) = 0.815976; rounded = 0.82
196   # total rounded = 12.48
197
198   # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
199   # line marge_total = 5.09
200   # line marge_percent = 43.6535162950257
201
202   my $title = 'default invoice, two item, 19/7% tax not included';
203   my %data  = $invoice->calculate_prices_and_taxes;
204
205   is($item1->marge_total,        1.02,             "${title}: item1 marge_total");
206   is($item1->marge_percent,      17.4358974358974, "${title}: item1 marge_percent");
207   is($item1->marge_price_factor, 1,                "${title}: item1 marge_price_factor");
208
209   is($item2->marge_total,        5.09,             "${title}: item2 marge_total");
210   is($item2->marge_percent,      43.6535162950257, "${title}: item2 marge_percent");
211   is($item2->marge_price_factor, 1,                "${title}: item2 marge_price_factor");
212
213   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
214   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
215   is($invoice->marge_total,      1.02 + 5.09,      "${title}: marge_total");
216   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
217
218   is_deeply(\%data, {
219     allocated                                     => {},
220     amounts                                       => {
221       $buchungsgruppe->income_accno_id($taxzone)  => {
222         amount                                    => 5.85,
223         tax_id                                    => $tax->id,
224         taxkey                                    => 3,
225       },
226       $buchungsgruppe7->income_accno_id($taxzone) => {
227         amount                                    => 11.66,
228         tax_id                                    => $tax7->id,
229         taxkey                                    => 2,
230       },
231     },
232     amounts_cogs                                  => {},
233     assembly_items                                => [
234       [], [],
235     ],
236     exchangerate                                  => 1,
237     taxes                                         => {
238       $tax->chart_id                              => 1.11,
239       $tax7->chart_id                             => 0.82,
240     },
241     items                                        => [
242       { linetotal                                => 5.85,
243         linetotal_cost                           => 4.83,
244         sellprice                                => 2.34,
245         tax_amount                               => 1.1115,
246         taxkey_id                                => $taxkey1->id,
247       },
248       { linetotal                                => 11.66,
249         linetotal_cost                           => 6.57,
250         sellprice                                => 9.714,
251         tax_amount                               => 0.8162,
252         taxkey_id                                => $taxkey2->id,
253       },
254     ],
255     rounding                                    =>  0,
256   }, "${title}: calculated data");
257 }
258
259 sub test_default_invoice_three_items_sellprice_rounding_discount() {
260   reset_state();
261
262   my $item1   = new_item(qty => 1, sellprice => 5.55, discount => .05);
263   my $item2   = new_item(qty => 1, sellprice => 5.50, discount => .05);
264   my $item3   = new_item(qty => 1, sellprice => 5.00, discount => .05);
265   my $invoice = new_invoice(
266     taxincluded  => 0,
267     invoiceitems => [ $item1, $item2, $item3 ],
268   );
269
270   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
271
272   # item 1:
273   # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
274   # linetotal = sellprice 5.55 * (1 - discount 0.05) * qty 1 = 5.2725; rounded 5.27
275   # 19%(5.27) = 1.0013; rounded = 1.00
276   # total rounded = 6.27
277
278   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
279   # line marge_total = 5.27 - 1.93 = 3.34
280   # line marge_percent = 63.3776091081594
281
282   # item 2:
283   # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
284   # linetotal = sellprice 5.50 * (1 - discount 0.05) * qty 1 = 5.225; rounded 5.23
285   # 19%(5.23) = .99370; rounded = 0.99
286   # total rounded = 6.22
287
288   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
289   # line marge_total = 5.23 - 1.93 = 3.30
290   # line marge_percent = 3.30/5.23 = 0.630975143403442
291
292   # item 3:
293   # discount = sellprice 5.00 * discount 0.05 = 0.05 = 0.25; rounded 0.25
294   # linetotal = sellprice 5.00 (1 - discount 0.05) * qty 1 = 4.75; rounded 4.75
295   # 19%(4.75) = 0.9025; rounded = 0.90
296   # total rounded = 5.65
297
298   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
299   # line marge_total = 4.75 - 1.93 = 2.82
300   # line marge_percent = 2.82/4.75 = 59.3684210526316
301
302   my $title = 'default invoice, three items, sellprice, rounding, discount';
303   my %data  = $invoice->calculate_prices_and_taxes;
304
305   is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
306   is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
307   is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
308
309   is($item2->marge_total,        3.30,               "${title}: item2 marge_total");
310   is($item2->marge_percent,      63.0975143403442,   "${title}: item2 marge_percent");
311   is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
312
313   is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
314   is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
315   is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
316
317   is($invoice->netamount,        5.27 + 5.23 + 4.75, "${title}: netamount");
318
319   # 6.27 + 6.22 + 5.65 = 18.14
320   # 1.19*(5.27 + 5.23 + 4.75) = 18.1475; rounded 18.15
321   #is($invoice->amount,           6.27 + 6.22 + 5.65, "${title}: amount");
322   is($invoice->amount,           18.15,              "${title}: amount");
323
324   is($invoice->marge_total,      3.34 + 3.30 + 2.82, "${title}: marge_total");
325   is($invoice->marge_percent,    62.0327868852459,   "${title}: marge_percent");
326
327   is_deeply(\%data, {
328     allocated                                    => {},
329     amounts                                      => {
330       $buchungsgruppe->income_accno_id($taxzone) => {
331         amount                                   => 15.25,
332         tax_id                                   => $tax->id,
333         taxkey                                   => 3,
334       },
335     },
336     amounts_cogs                                 => {},
337     assembly_items                               => [
338       [], [], [],
339     ],
340     exchangerate                                 => 1,
341     taxes                                        => {
342       $tax->chart_id                             => 2.9,
343     },
344     items                                        => [
345       { linetotal                                => 5.27,
346         linetotal_cost                           => 1.93,
347         sellprice                                => 5.27,
348         tax_amount                               => 1.0013,
349         taxkey_id                                => $taxkeys{$item1->parts_id}->id,
350       },
351       { linetotal                                => 5.23,
352         linetotal_cost                           => 1.93,
353         sellprice                                => 5.23,
354         tax_amount                               => 0.9937,
355         taxkey_id                                => $taxkeys{$item2->parts_id}->id,
356       },
357       { linetotal                                => 4.75,
358         linetotal_cost                           => 1.93,
359         sellprice                                => 4.75,
360         tax_amount                               => 0.9025,
361         taxkey_id                                => $taxkeys{$item3->parts_id}->id,
362       }
363     ],
364     rounding                                    =>  0,
365   }, "${title}: calculated data");
366 }
367
368 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
369   reset_state();
370
371   my $item   = new_item(qty => 6, part => $parts[2], discount => 0.03);
372   my $invoice = new_invoice(
373     taxincluded  => 0,
374     invoiceitems => [ $item ],
375   );
376
377   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
378
379   # 6 parts for 0.60 with 3% discount
380   #
381   # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
382   # total = 3.49 + 0.66 = 4.15
383   #
384
385   my $title = 'default invoice, one item, sellprice, rounding, discount';
386   my %data  = $invoice->calculate_prices_and_taxes;
387
388   is($invoice->netamount,         3.49,              "${title}: netamount");
389
390   is($invoice->amount,            4.15,              "${title}: amount");
391
392   is($invoice->marge_total,       3.49,              "${title}: marge_total");
393   is($invoice->marge_percent,      100,              "${title}: marge_percent");
394
395   is_deeply(\%data, {
396     allocated                                    => {},
397     amounts                                      => {
398       $buchungsgruppe->income_accno_id($taxzone) => {
399         amount                                   => 3.49,
400         tax_id                                   => $tax->id,
401         taxkey                                   => 3,
402       },
403     },
404     amounts_cogs                                 => {},
405     assembly_items                               => [
406       [],
407     ],
408     exchangerate                                 => 1,
409     taxes                                        => {
410       $tax->chart_id                             => 0.66,
411     },
412     items                                        => [
413       { linetotal                                => 3.49,
414         linetotal_cost                           => 0,
415         sellprice                                => 0.58,
416         tax_amount                               => 0.6631,
417         taxkey_id                                => $taxkeys{$item->parts_id}->id,
418       },
419     ],
420     rounding                                     =>  0,
421   }, "${title}: calculated data");
422 }
423
424 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty() {
425   reset_state();
426
427   my $item   = new_item(qty => 100000, part => $parts[2], discount => 0.03, sellprice => 0.10);
428   my $invoice = new_invoice(
429     taxincluded  => 0,
430     invoiceitems => [ $item ],
431   );
432
433   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
434
435   my $title = 'default invoice, one item, 19% tax not included, rounding, discount, huge qty';
436   my %data  = $invoice->calculate_prices_and_taxes;
437
438   is($invoice->netamount,         9700,              "${title}: netamount");
439
440   is($invoice->amount,           11543,              "${title}: amount");
441
442   is($invoice->marge_total,       9700,              "${title}: marge_total");
443   is($invoice->marge_percent,      100,              "${title}: marge_percent");
444
445   is_deeply(\%data, {
446     allocated                                    => {},
447     amounts                                      => {
448       $buchungsgruppe->income_accno_id($taxzone) => {
449         amount                                   => 9700,
450         tax_id                                   => $tax->id,
451         taxkey                                   => 3,
452       },
453     },
454     amounts_cogs                                 => {},
455     assembly_items                               => [
456       [],
457     ],
458     exchangerate                                 => 1,
459     taxes                                        => {
460       $tax->chart_id                             => 1843,
461     },
462     items                                        => [
463       { linetotal                                => 9700,
464         linetotal_cost                           => 0,
465         sellprice                                => 0.1,
466         tax_amount                               => 1843,
467         taxkey_id                                => $taxkeys{$item->parts_id}->id,
468       },
469     ],
470     rounding                                    =>  0,
471   }, "${title}: calculated data");
472 }
473
474 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice() {
475   reset_state();
476
477   my $item   = new_item(qty => 10001, sellprice => 0.007, discount => 0.035);
478   my $invoice = new_invoice(
479     taxincluded  => 0,
480     invoiceitems => [ $item ],
481   );
482
483   my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
484
485   # item 1:
486   # discount = sellprice 0.007 * discount (0.035) = 0.000245; rounded 0.00
487   # sellprice = sellprice 0.007 - discount 0.00 = 0.007
488   # linetotal = sellprice 0.007 * qty 10001 * (1 - 0.035) = 67.556755; rounded 67.56
489   # 19%(67.56) = 12.8364; rounded = 12.84
490   # total rounded = 80.40
491
492   # lastcost 1.93 * qty 10001 = 19301.93; rounded 19301.93
493   # line marge_total = 67.56-19301.93 = -19234.37
494   # line marge_percent = 100*-19234.37/67.56 = -28470.0562462996
495
496   my $title = 'default invoice one item 19 tax not included rounding discount big qty low sellprice';
497   my %data  = $invoice->calculate_prices_and_taxes;
498
499   is($invoice->netamount,                 67.56,    "${title}: netamount");
500
501   is($invoice->amount,                    80.40,    "${title}: amount");
502
503   is($invoice->marge_total,           -19234.37,    "${title}: marge_total");
504   is($invoice->marge_percent, -28470.0562462996,    "${title}: marge_percent");
505
506   is_deeply(\%data, {
507     allocated                                    => {},
508     amounts                                      => {
509       $buchungsgruppe->income_accno_id($taxzone) => {
510         amount                                   => 67.56,
511         tax_id                                   => $tax->id,
512         taxkey                                   => 3,
513       },
514     },
515     amounts_cogs                                 => {},
516     assembly_items                               => [
517       [],
518     ],
519     exchangerate                                 => 1,
520     taxes                                        => {
521       $tax->chart_id                             => 12.84,
522     },
523     items                                        => [
524       { linetotal                                => 67.56,
525         linetotal_cost                           => 19301.93,
526         sellprice                                => 0.007,
527         tax_amount                               => 12.8364,
528         taxkey_id                                => $taxkeys{$item->parts_id}->id,
529       },
530     ],
531     rounding                                    =>  0,
532   }, "${title}: calculated data");
533 }
534
535
536 Support::TestSetup::login();
537
538 test_default_invoice_one_item_19_tax_not_included();
539 test_default_invoice_two_items_19_7_tax_not_included();
540 test_default_invoice_three_items_sellprice_rounding_discount();
541 test_default_invoice_one_item_19_tax_not_included_rounding_discount();
542 test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty();
543 test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice();
544
545 clear_up();
546 done_testing();
547
548 # vim: ft=perl
549 # set emacs to perl mode
550 # Local Variables:
551 # mode: perl
552 # End: