98462641d3a6055fe681a60af90f785a34da4141
[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
81 sub new_invoice {
82   my %params  = @_;
83
84   return SL::DB::Invoice->new(
85     customer_id => $customer->id,
86     currency_id => $currency_id,
87     employee_id => $employee->id,
88     salesman_id => $employee->id,
89     gldate      => DateTime->today_local->to_kivitendo,
90     taxzone_id  => $taxzone->id,
91     transdate   => DateTime->today_local->to_kivitendo,
92     invoice     => 1,
93     type        => 'invoice',
94     %params,
95   );
96 }
97
98 sub new_item {
99   my (%params) = @_;
100
101   my $part = delete($params{part}) || $parts[0];
102
103   return SL::DB::InvoiceItem->new(
104     parts_id    => $part->id,
105     lastcost    => $part->lastcost,
106     sellprice   => $part->sellprice,
107     description => $part->description,
108     unit        => $part->unit,
109     %params,
110   );
111 }
112
113 sub test_default_invoice_one_item_19_tax_not_included() {
114   reset_state();
115
116   my $item    = new_item(qty => 2.5);
117   my $invoice = new_invoice(
118     taxincluded  => 0,
119     invoiceitems => [ $item ],
120   );
121
122   my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
123
124   # sellprice 2.34 * qty 2.5 = 5.85
125   # 19%(5.85) = 1.1115; rounded = 1.11
126   # total rounded = 6.96
127
128   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
129   # line marge_total = 1.02
130   # line marge_percent = 17.4358974358974
131
132   my $title = 'default invoice, one item, 19% tax not included';
133   my %data  = $invoice->calculate_prices_and_taxes;
134
135   is($item->marge_total,        1.02,             "${title}: item marge_total");
136   is($item->marge_percent,      17.4358974358974, "${title}: item marge_percent");
137   is($item->marge_price_factor, 1,                "${title}: item marge_price_factor");
138
139   is($invoice->netamount,       5.85,             "${title}: netamount");
140   is($invoice->amount,          6.96,             "${title}: amount");
141   is($invoice->marge_total,     1.02,             "${title}: marge_total");
142   is($invoice->marge_percent,   17.4358974358974, "${title}: marge_percent");
143
144   is_deeply(\%data, {
145     allocated                                    => {},
146     amounts                                      => {
147       $buchungsgruppe->income_accno_id($taxzone) => {
148         amount                                   => 5.85,
149         tax_id                                   => $tax->id,
150         taxkey                                   => 3,
151       },
152     },
153     amounts_cogs                                 => {},
154     assembly_items                               => [
155       [],
156     ],
157     exchangerate                                 => 1,
158     taxes                                        => {
159       $tax->chart_id                             => 1.11,
160     },
161     items                                        => [
162       { linetotal                                => 5.85,
163         linetotal_cost                           => 4.83,
164         sellprice                                => 2.34,
165         tax_amount                               => 1.1115,
166         taxkey_id                                => $taxkey->id,
167       },
168     ],
169   }, "${title}: calculated data");
170 }
171
172 sub test_default_invoice_two_items_19_7_tax_not_included() {
173   reset_state();
174
175   my $item1   = new_item(qty => 2.5);
176   my $item2   = new_item(qty => 1.2, part => $parts[1]);
177   my $invoice = new_invoice(
178     taxincluded  => 0,
179     invoiceitems => [ $item1, $item2 ],
180   );
181
182   my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
183   my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
184
185   # item 1:
186   # sellprice 2.34 * qty 2.5 = 5.85
187   # 19%(5.85) = 1.1115; rounded = 1.11
188   # total rounded = 6.96
189
190   # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
191   # line marge_total = 1.02
192   # line marge_percent = 17.4358974358974
193
194   # item 2:
195   # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
196   # 7%(11.6568) = 0.815976; rounded = 0.82
197   # total rounded = 12.48
198
199   # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
200   # line marge_total = 5.09
201   # line marge_percent = 43.6535162950257
202
203   my $title = 'default invoice, two item, 19/7% tax not included';
204   my %data  = $invoice->calculate_prices_and_taxes;
205
206   is($item1->marge_total,        1.02,             "${title}: item1 marge_total");
207   is($item1->marge_percent,      17.4358974358974, "${title}: item1 marge_percent");
208   is($item1->marge_price_factor, 1,                "${title}: item1 marge_price_factor");
209
210   is($item2->marge_total,        5.09,             "${title}: item2 marge_total");
211   is($item2->marge_percent,      43.6535162950257, "${title}: item2 marge_percent");
212   is($item2->marge_price_factor, 1,                "${title}: item2 marge_price_factor");
213
214   is($invoice->netamount,        5.85 + 11.66,     "${title}: netamount");
215   is($invoice->amount,           6.96 + 12.48,     "${title}: amount");
216   is($invoice->marge_total,      1.02 + 5.09,      "${title}: marge_total");
217   is($invoice->marge_percent,    34.8943460879497, "${title}: marge_percent");
218
219   is_deeply(\%data, {
220     allocated                                     => {},
221     amounts                                       => {
222       $buchungsgruppe->income_accno_id($taxzone)  => {
223         amount                                    => 5.85,
224         tax_id                                    => $tax->id,
225         taxkey                                    => 3,
226       },
227       $buchungsgruppe7->income_accno_id($taxzone) => {
228         amount                                    => 11.66,
229         tax_id                                    => $tax7->id,
230         taxkey                                    => 2,
231       },
232     },
233     amounts_cogs                                  => {},
234     assembly_items                                => [
235       [], [],
236     ],
237     exchangerate                                  => 1,
238     taxes                                         => {
239       $tax->chart_id                              => 1.11,
240       $tax7->chart_id                             => 0.82,
241     },
242     items                                        => [
243       { linetotal                                => 5.85,
244         linetotal_cost                           => 4.83,
245         sellprice                                => 2.34,
246         tax_amount                               => 1.1115,
247         taxkey_id                                => $taxkey1->id,
248       },
249       { linetotal                                => 11.66,
250         linetotal_cost                           => 6.57,
251         sellprice                                => 9.714,
252         tax_amount                               => 0.8162,
253         taxkey_id                                => $taxkey2->id,
254       },
255     ],
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   # this is how price_tax_calculator is implemented. It differs from
273   # the way sales_order / invoice - forms are calculating:
274   # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
275   # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
276   # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
277   # ...
278
279   # item 1:
280   # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
281   # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
282   # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
283   # 19%(5.27) = 1.0013; rounded = 1.00
284   # total rounded = 6.27
285
286   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
287   # line marge_total = 3.34
288   # line marge_percent = 63.3776091081594
289
290   # item 2:
291   # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
292   # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
293   # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
294   # 19%(5.22) = 0.9918; rounded = 0.99
295   # total rounded = 6.21
296
297   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
298   # line marge_total = 5.22 - 1.93 = 3.29
299   # line marge_percent = 3.29/5.22 = 0.630268199233716
300
301   # item 3:
302   # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
303   # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
304   # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
305   # 19%(4.75) = 0.9025; rounded = 0.90
306   # total rounded = 5.65
307
308   # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
309   # line marge_total = 2.82
310   # line marge_percent = 59.3684210526316
311
312   my $title = 'default invoice, three items, sellprice, rounding, discount';
313   my %data  = $invoice->calculate_prices_and_taxes;
314
315   is($item1->marge_total,        3.34,               "${title}: item1 marge_total");
316   is($item1->marge_percent,      63.3776091081594,   "${title}: item1 marge_percent");
317   is($item1->marge_price_factor, 1,                  "${title}: item1 marge_price_factor");
318
319   is($item2->marge_total,        3.29,               "${title}: item2 marge_total");
320   is($item2->marge_percent,      63.0268199233716,  "${title}: item2 marge_percent");
321   is($item2->marge_price_factor, 1,                  "${title}: item2 marge_price_factor");
322
323   is($item3->marge_total,        2.82,               "${title}: item3 marge_total");
324   is($item3->marge_percent,      59.3684210526316,   "${title}: item3 marge_percent");
325   is($item3->marge_price_factor, 1,                  "${title}: item3 marge_price_factor");
326
327   is($invoice->netamount,        5.27 + 5.22 + 4.75, "${title}: netamount");
328
329   # 6.27 + 6.21 + 5.65 = 18.13
330   # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
331   #is($invoice->amount,           6.27 + 6.21 + 5.65, "${title}: amount");
332   is($invoice->amount,           18.14,              "${title}: amount");
333
334   is($invoice->marge_total,      3.34 + 3.29 + 2.82, "${title}: marge_total");
335   is($invoice->marge_percent,    62.007874015748,    "${title}: marge_percent");
336
337   is_deeply(\%data, {
338     allocated                                    => {},
339     amounts                                      => {
340       $buchungsgruppe->income_accno_id($taxzone) => {
341         amount                                   => 15.24,
342         tax_id                                   => $tax->id,
343         taxkey                                   => 3,
344       },
345     },
346     amounts_cogs                                 => {},
347     assembly_items                               => [
348       [], [], [],
349     ],
350     exchangerate                                 => 1,
351     taxes                                        => {
352       $tax->chart_id                             => 2.9,
353     },
354     items                                        => [
355       { linetotal                                => 5.27,
356         linetotal_cost                           => 1.93,
357         sellprice                                => 5.27,
358         tax_amount                               => 1.0013,
359         taxkey_id                                => $taxkeys{$item1->parts_id}->id,
360       },
361       { linetotal                                => 5.22,
362         linetotal_cost                           => 1.93,
363         sellprice                                => 5.22,
364         tax_amount                               => 0.9918,
365         taxkey_id                                => $taxkeys{$item2->parts_id}->id,
366       },
367       { linetotal                                => 4.75,
368         linetotal_cost                           => 1.93,
369         sellprice                                => 4.75,
370         tax_amount                               => 0.9025,
371         taxkey_id                                => $taxkeys{$item3->parts_id}->id,
372       }
373     ],
374   }, "${title}: calculated data");
375 }
376
377 Support::TestSetup::login();
378
379 test_default_invoice_one_item_19_tax_not_included();
380 test_default_invoice_two_items_19_7_tax_not_included();
381 test_default_invoice_three_items_sellprice_rounding_discount();
382
383 clear_up();
384 done_testing();