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