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