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