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