10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
14 use SL::DB::Buchungsgruppe;
17 use SL::DB::DeliveryOrder;
25 my ($customer, $currency_id, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
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);
38 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
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";
50 $currency_id = $::instance_conf->get_currency_id;
52 $customer = SL::DB::Customer->new(
53 name => 'Test Customer',
54 currency_id => $currency_id,
55 taxzone_id => $taxzone->id,
56 %{ $params{customer} }
60 push @parts, SL::DB::Part->new(
61 partnumber => 'T4254',
62 description => 'Fourty-two fifty-four',
65 buchungsgruppen_id => $buchungsgruppe->id,
70 push @parts, SL::DB::Part->new(
71 partnumber => 'T0815',
72 description => 'Zero EIGHT fifteeN @ 7%',
75 buchungsgruppen_id => $buchungsgruppe7->id,
80 push @parts, SL::DB::Part->new(
82 description => 'Triple 8',
85 buchungsgruppen_id => $buchungsgruppe->id,
95 return SL::DB::Invoice->new(
96 customer_id => $customer->id,
97 currency_id => $currency_id,
98 employee_id => $employee->id,
99 salesman_id => $employee->id,
100 gldate => DateTime->today_local->to_kivitendo,
101 taxzone_id => $taxzone->id,
102 transdate => DateTime->today_local->to_kivitendo,
112 my $part = delete($params{part}) || $parts[0];
114 return SL::DB::InvoiceItem->new(
115 parts_id => $part->id,
116 lastcost => $part->lastcost,
117 sellprice => $part->sellprice,
118 description => $part->description,
124 sub test_default_invoice_one_item_19_tax_not_included() {
127 my $item = new_item(qty => 2.5);
128 my $invoice = new_invoice(
130 invoiceitems => [ $item ],
133 my $taxkey = $item->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
135 # sellprice 2.34 * qty 2.5 = 5.85
136 # 19%(5.85) = 1.1115; rounded = 1.11
137 # total rounded = 6.96
139 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
140 # line marge_total = 1.02
141 # line marge_percent = 17.4358974358974
143 my $title = 'default invoice, one item, 19% tax not included';
144 my %data = $invoice->calculate_prices_and_taxes;
146 is($item->marge_total, 1.02, "${title}: item marge_total");
147 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
148 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
150 is($invoice->netamount, 5.85, "${title}: netamount");
151 is($invoice->amount, 6.96, "${title}: amount");
152 is($invoice->marge_total, 1.02, "${title}: marge_total");
153 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
158 $buchungsgruppe->income_accno_id($taxzone) => {
170 $tax->chart_id => 1.11,
174 linetotal_cost => 4.83,
176 tax_amount => 1.1115,
177 taxkey_id => $taxkey->id,
180 }, "${title}: calculated data");
183 sub test_default_invoice_two_items_19_7_tax_not_included() {
186 my $item1 = new_item(qty => 2.5);
187 my $item2 = new_item(qty => 1.2, part => $parts[1]);
188 my $invoice = new_invoice(
190 invoiceitems => [ $item1, $item2 ],
193 my $taxkey1 = $item1->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
194 my $taxkey2 = $item2->part->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id);
197 # sellprice 2.34 * qty 2.5 = 5.85
198 # 19%(5.85) = 1.1115; rounded = 1.11
199 # total rounded = 6.96
201 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
202 # line marge_total = 1.02
203 # line marge_percent = 17.4358974358974
206 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
207 # 7%(11.6568) = 0.815976; rounded = 0.82
208 # total rounded = 12.48
210 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
211 # line marge_total = 5.09
212 # line marge_percent = 43.6535162950257
214 my $title = 'default invoice, two item, 19/7% tax not included';
215 my %data = $invoice->calculate_prices_and_taxes;
217 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
218 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
219 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
221 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
222 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
223 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
225 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
226 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
227 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
228 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
233 $buchungsgruppe->income_accno_id($taxzone) => {
238 $buchungsgruppe7->income_accno_id($taxzone) => {
250 $tax->chart_id => 1.11,
251 $tax7->chart_id => 0.82,
255 linetotal_cost => 4.83,
257 tax_amount => 1.1115,
258 taxkey_id => $taxkey1->id,
260 { linetotal => 11.66,
261 linetotal_cost => 6.57,
263 tax_amount => 0.8162,
264 taxkey_id => $taxkey2->id,
267 }, "${title}: calculated data");
270 sub test_default_invoice_three_items_sellprice_rounding_discount() {
273 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
274 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
275 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
276 my $invoice = new_invoice(
278 invoiceitems => [ $item1, $item2, $item3 ],
281 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
283 # this is how price_tax_calculator is implemented. It differs from
284 # the way sales_order / invoice - forms are calculating:
285 # linetotal = sellprice 5.55 * qty 1 * (1 - 0.05) = 5.2725; rounded 5.27
286 # linetotal = sellprice 5.50 * qty 1 * (1 - 0.05) = 5.225 rounded 5.23
287 # linetotal = sellprice 5.00 * qty 1 * (1 - 0.05) = 4.75; rounded 4.75
291 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
292 # sellprice = sellprice 5.55 - discount 0.28 = 5.27; rounded 5.27
293 # linetotal = sellprice 5.27 * qty 1 = 5.27; rounded 5.27
294 # 19%(5.27) = 1.0013; rounded = 1.00
295 # total rounded = 6.27
297 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
298 # line marge_total = 3.34
299 # line marge_percent = 63.3776091081594
302 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
303 # sellprice = sellprice 5.50 - discount 0.28 = 5.22; rounded 5.22
304 # linetotal = sellprice 5.22 * qty 1 = 5.22; rounded 5.22
305 # 19%(5.22) = 0.9918; rounded = 0.99
306 # total rounded = 6.21
308 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
309 # line marge_total = 5.22 - 1.93 = 3.29
310 # line marge_percent = 3.29/5.22 = 0.630268199233716
313 # discount = sellprice 5.00 * discount 0.25 = 0.25; rounded 0.25
314 # sellprice = sellprice 5.00 - discount 0.25 = 4.75; rounded 4.75
315 # linetotal = sellprice 4.75 * qty 1 = 4.75; rounded 4.75
316 # 19%(4.75) = 0.9025; rounded = 0.90
317 # total rounded = 5.65
319 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
320 # line marge_total = 2.82
321 # line marge_percent = 59.3684210526316
323 my $title = 'default invoice, three items, sellprice, rounding, discount';
324 my %data = $invoice->calculate_prices_and_taxes;
326 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
327 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
328 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
330 is($item2->marge_total, 3.29, "${title}: item2 marge_total");
331 is($item2->marge_percent, 63.0268199233716, "${title}: item2 marge_percent");
332 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
334 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
335 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
336 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
338 is($invoice->netamount, 5.27 + 5.22 + 4.75, "${title}: netamount");
340 # 6.27 + 6.21 + 5.65 = 18.13
341 # 1.19*(5.27 + 5.22 + 4.75) = 18.1356; rounded 18.14
342 #is($invoice->amount, 6.27 + 6.21 + 5.65, "${title}: amount");
343 is($invoice->amount, 18.14, "${title}: amount");
345 is($invoice->marge_total, 3.34 + 3.29 + 2.82, "${title}: marge_total");
346 is($invoice->marge_percent, 62.007874015748, "${title}: marge_percent");
351 $buchungsgruppe->income_accno_id($taxzone) => {
363 $tax->chart_id => 2.9,
367 linetotal_cost => 1.93,
369 tax_amount => 1.0013,
370 taxkey_id => $taxkeys{$item1->parts_id}->id,
373 linetotal_cost => 1.93,
375 tax_amount => 0.9918,
376 taxkey_id => $taxkeys{$item2->parts_id}->id,
379 linetotal_cost => 1.93,
381 tax_amount => 0.9025,
382 taxkey_id => $taxkeys{$item3->parts_id}->id,
385 }, "${title}: calculated data");
388 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
391 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
392 my $invoice = new_invoice(
394 invoiceitems => [ $item ],
397 my %taxkeys = map { ($_->id => $_->get_taxkey(date => DateTime->today_local, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
399 # PTC and ar form calculate linetotal differently:
400 # 6 parts for 0.60 with 3% discount
403 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
404 # total = 3.49 + 0.66 = 4.15
407 # discount = sellprice 0.60 * discount (0.03) = 0.018; rounded 0.02
408 # sellprice = sellprice 0.60 - discount 0.02 = 0.58
409 # linetotal = sellprice 0.58 * qty 6 = 3.48
410 # 19%(3.48) = 0.6612; rounded = 0.66
411 # total rounded = 3.48 + 0.66 = 4.14
413 my $title = 'default invoice, one item, sellprice, rounding, discount';
414 my %data = $invoice->calculate_prices_and_taxes;
416 is($invoice->netamount, 3.48, "${title}: netamount");
418 is($invoice->amount, 4.14, "${title}: amount");
420 is($invoice->marge_total, 3.48, "${title}: marge_total");
421 is($invoice->marge_percent, 100, "${title}: marge_percent");
426 $buchungsgruppe->income_accno_id($taxzone) => {
438 $tax->chart_id => 0.66,
444 tax_amount => 0.6612,
445 taxkey_id => $taxkeys{$item->parts_id}->id,
448 }, "${title}: calculated data");
451 Support::TestSetup::login();
453 test_default_invoice_one_item_19_tax_not_included();
454 test_default_invoice_two_items_19_7_tax_not_included();
455 test_default_invoice_three_items_sellprice_rounding_discount();
456 test_default_invoice_one_item_19_tax_not_included_rounding_discount();