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