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