10 use List::MoreUtils qw(uniq);
11 use Support::TestSetup;
13 use SL::Dev::ALL qw(:ALL);
15 use SL::DB::Buchungsgruppe;
18 use SL::DB::DeliveryOrder;
26 my ($customer, @parts, $buchungsgruppe, $buchungsgruppe7, $unit, $employee, $tax, $tax7, $taxzone);
30 SL::DB::Manager::Order->delete_all(all => 1);
31 SL::DB::Manager::DeliveryOrder->delete_all(all => 1);
32 SL::DB::Manager::InvoiceItem->delete_all(all => 1);
33 SL::DB::Manager::Invoice->delete_all(all => 1);
34 SL::DB::Manager::Part->delete_all(all => 1);
35 SL::DB::Manager::Customer->delete_all(all => 1);
41 $params{$_} ||= {} for qw(buchungsgruppe unit customer part tax);
45 $buchungsgruppe = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%', %{ $params{buchungsgruppe} }) || croak "No accounting group";
46 $buchungsgruppe7 = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 7%') || croak "No accounting group for 7\%";
47 $unit = SL::DB::Manager::Unit->find_by(name => 'kg', %{ $params{unit} }) || croak "No unit";
48 $employee = SL::DB::Manager::Employee->current || croak "No employee";
49 $tax = SL::DB::Manager::Tax->find_by(taxkey => 3, rate => 0.19, %{ $params{tax} }) || croak "No tax";
50 $tax7 = SL::DB::Manager::Tax->find_by(taxkey => 2, rate => 0.07) || croak "No tax for 7\%";
51 $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || croak "No taxzone";
53 $customer = new_customer(
54 name => 'Test Customer',
55 taxzone_id => $taxzone->id,
56 %{ $params{customer} }
60 push @parts, new_part(
61 partnumber => 'T4254',
62 description => 'Fourty-two fifty-four',
65 buchungsgruppen_id => $buchungsgruppe->id,
70 push @parts, new_part(
71 partnumber => 'T0815',
72 description => 'Zero EIGHT fifteeN @ 7%',
75 buchungsgruppen_id => $buchungsgruppe7->id,
80 push @parts, new_part(
82 description => 'Triple 8',
85 buchungsgruppen_id => $buchungsgruppe->id,
95 return create_sales_invoice(
96 transdate => $transdate,
97 taxzone_id => $taxzone->id,
104 return create_sales_order(
105 transdate => $transdate,
106 taxzone_id => $taxzone->id,
114 my $part = delete($params{part}) || $parts[0];
116 return create_invoice_item(
124 my $part = delete($params{part}) || $parts[0];
126 return create_order_item(
132 sub test_default_invoice_one_item_19_tax_not_included() {
135 my $item = new_item(qty => 2.5);
136 my $invoice = new_invoice(
138 invoiceitems => [ $item ],
141 my $taxkey = $item->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
143 # sellprice 2.34 * qty 2.5 = 5.85
144 # 19%(5.85) = 1.1115; rounded = 1.11
145 # total rounded = 6.96
147 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
148 # line marge_total = 1.02
149 # line marge_percent = 17.4358974358974
151 my $title = 'default invoice, one item, 19% tax not included';
152 my %data = $invoice->calculate_prices_and_taxes;
154 is($item->marge_total, 1.02, "${title}: item marge_total");
155 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
156 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
158 is($invoice->netamount, 5.85, "${title}: netamount");
159 is($invoice->amount, 6.96, "${title}: amount");
160 is($invoice->marge_total, 1.02, "${title}: marge_total");
161 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
166 $buchungsgruppe->income_accno_id($taxzone) => {
177 taxes_by_chart_id => {
178 $tax->chart_id => 1.11,
185 linetotal_cost => 4.83,
187 tax_amount => 1.1115,
188 taxkey_id => $taxkey->id,
192 }, "${title}: calculated data");
195 sub test_default_invoice_two_items_19_7_tax_not_included() {
198 my $item1 = new_item(qty => 2.5);
199 my $item2 = new_item(qty => 1.2, part => $parts[1]);
200 my $invoice = new_invoice(
202 invoiceitems => [ $item1, $item2 ],
205 my $taxkey1 = $item1->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
206 my $taxkey2 = $item2->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
209 # sellprice 2.34 * qty 2.5 = 5.85
210 # 19%(5.85) = 1.1115; rounded = 1.11
211 # total rounded = 6.96
213 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
214 # line marge_total = 1.02
215 # line marge_percent = 17.4358974358974
218 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
219 # 7%(11.6568) = 0.815976; rounded = 0.82
221 # total rounded = 12.48
223 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
224 # line marge_total = 5.09
225 # line marge_percent = 43.6535162950257
227 my $title = 'default invoice, two item, 19/7% tax not included';
228 my %data = $invoice->calculate_prices_and_taxes;
230 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
231 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
232 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
234 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
235 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
236 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
238 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
239 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
240 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
241 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
246 $buchungsgruppe->income_accno_id($taxzone) => {
251 $buchungsgruppe7->income_accno_id($taxzone) => {
262 taxes_by_chart_id => {
263 $tax->chart_id => 1.11,
264 $tax7->chart_id => 0.82,
272 linetotal_cost => 4.83,
274 tax_amount => 1.1115,
275 taxkey_id => $taxkey1->id,
277 { linetotal => 11.66,
278 linetotal_cost => 6.57,
280 tax_amount => 0.8162,
281 taxkey_id => $taxkey2->id,
285 }, "${title}: calculated data");
288 sub test_default_invoice_three_items_sellprice_rounding_discount() {
291 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
292 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
293 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
294 my $invoice = new_invoice(
296 invoiceitems => [ $item1, $item2, $item3 ],
299 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
302 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
303 # linetotal = sellprice 5.55 * (1 - discount 0.05) * qty 1 = 5.2725; rounded 5.27
304 # 19%(5.27) = 1.0013; rounded = 1.00
305 # total rounded = 6.27
307 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
308 # line marge_total = 5.27 - 1.93 = 3.34
309 # line marge_percent = 63.3776091081594
312 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
313 # linetotal = sellprice 5.50 * (1 - discount 0.05) * qty 1 = 5.225; rounded 5.23
314 # 19%(5.23) = .99370; rounded = 0.99
315 # total rounded = 6.22
317 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
318 # line marge_total = 5.23 - 1.93 = 3.30
319 # line marge_percent = 3.30/5.23 = 0.630975143403442
322 # discount = sellprice 5.00 * discount 0.05 = 0.05 = 0.25; rounded 0.25
323 # linetotal = sellprice 5.00 (1 - discount 0.05) * qty 1 = 4.75; rounded 4.75
324 # 19%(4.75) = 0.9025; rounded = 0.90
325 # total rounded = 5.65
327 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
328 # line marge_total = 4.75 - 1.93 = 2.82
329 # line marge_percent = 2.82/4.75 = 59.3684210526316
331 my $title = 'default invoice, three items, sellprice, rounding, discount';
332 my %data = $invoice->calculate_prices_and_taxes;
334 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
335 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
336 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
338 is($item2->marge_total, 3.30, "${title}: item2 marge_total");
339 is($item2->marge_percent, 63.0975143403442, "${title}: item2 marge_percent");
340 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
342 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
343 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
344 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
346 is($invoice->netamount, 5.27 + 5.23 + 4.75, "${title}: netamount");
348 # 6.27 + 6.22 + 5.65 = 18.14
349 # 1.19*(5.27 + 5.23 + 4.75) = 18.1475; rounded 18.15
350 #is($invoice->amount, 6.27 + 6.22 + 5.65, "${title}: amount");
351 is($invoice->amount, 18.15, "${title}: amount");
353 is($invoice->marge_total, 3.34 + 3.30 + 2.82, "${title}: marge_total");
354 is($invoice->marge_percent, 62.0327868852459, "${title}: marge_percent");
359 $buchungsgruppe->income_accno_id($taxzone) => {
370 taxes_by_chart_id => {
371 $tax->chart_id => 2.9,
378 linetotal_cost => 1.93,
380 tax_amount => 1.0013,
381 taxkey_id => $taxkeys{$item1->parts_id}->id,
384 linetotal_cost => 1.93,
386 tax_amount => 0.9937,
387 taxkey_id => $taxkeys{$item2->parts_id}->id,
390 linetotal_cost => 1.93,
392 tax_amount => 0.9025,
393 taxkey_id => $taxkeys{$item3->parts_id}->id,
397 }, "${title}: calculated data");
400 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
403 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
404 my $invoice = new_invoice(
406 invoiceitems => [ $item ],
409 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
411 # 6 parts for 0.60 with 3% discount
413 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
414 # total = 3.49 + 0.66 = 4.15
417 my $title = 'default invoice, one item, sellprice, rounding, discount';
418 my %data = $invoice->calculate_prices_and_taxes;
420 is($invoice->netamount, 3.49, "${title}: netamount");
422 is($invoice->amount, 4.15, "${title}: amount");
424 is($invoice->marge_total, 3.49, "${title}: marge_total");
425 is($invoice->marge_percent, 100, "${title}: marge_percent");
430 $buchungsgruppe->income_accno_id($taxzone) => {
441 taxes_by_chart_id => {
442 $tax->chart_id => 0.66,
451 tax_amount => 0.6631,
452 taxkey_id => $taxkeys{$item->parts_id}->id,
456 }, "${title}: calculated data");
459 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty() {
462 my $item = new_item(qty => 100000, part => $parts[2], discount => 0.03, sellprice => 0.10);
463 my $invoice = new_invoice(
465 invoiceitems => [ $item ],
468 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
470 my $title = 'default invoice, one item, 19% tax not included, rounding, discount, huge qty';
471 my %data = $invoice->calculate_prices_and_taxes;
473 is($invoice->netamount, 9700, "${title}: netamount");
475 is($invoice->amount, 11543, "${title}: amount");
477 is($invoice->marge_total, 9700, "${title}: marge_total");
478 is($invoice->marge_percent, 100, "${title}: marge_percent");
483 $buchungsgruppe->income_accno_id($taxzone) => {
494 taxes_by_chart_id => {
495 $tax->chart_id => 1843,
505 taxkey_id => $taxkeys{$item->parts_id}->id,
509 }, "${title}: calculated data");
512 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice() {
515 my $item = new_item(qty => 10001, sellprice => 0.007, discount => 0.035);
516 my $invoice = new_invoice(
518 invoiceitems => [ $item ],
521 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
524 # discount = sellprice 0.007 * discount (0.035) = 0.000245; rounded 0.00
525 # sellprice = sellprice 0.007 - discount 0.00 = 0.007
526 # linetotal = sellprice 0.007 * qty 10001 * (1 - 0.035) = 67.556755; rounded 67.56
527 # 19%(67.56) = 12.8364; rounded = 12.84
528 # total rounded = 80.40
530 # lastcost 1.93 * qty 10001 = 19301.93; rounded 19301.93
531 # line marge_total = 67.56-19301.93 = -19234.37
532 # line marge_percent = 100*-19234.37/67.56 = -28470.0562462996
534 my $title = 'default invoice one item 19 tax not included rounding discount big qty low sellprice';
535 my %data = $invoice->calculate_prices_and_taxes;
537 is($invoice->netamount, 67.56, "${title}: netamount");
539 is($invoice->amount, 80.40, "${title}: amount");
541 is($invoice->marge_total, -19234.37, "${title}: marge_total");
542 is($invoice->marge_percent, -28470.0562462996, "${title}: marge_percent");
547 $buchungsgruppe->income_accno_id($taxzone) => {
558 taxes_by_chart_id => {
559 $tax->chart_id => 12.84,
565 { linetotal => 67.56,
566 linetotal_cost => 19301.93,
568 tax_amount => 12.8364,
569 taxkey_id => $taxkeys{$item->parts_id}->id,
573 }, "${title}: calculated data");
575 sub test_default_order_two_items_19_one_optional() {
578 my $item = new_order_item(qty => 2.5);
579 my $item_optional = new_order_item(qty => 2.5, optional => 1);
581 my $order = new_order(
583 orderitems => [ $item, $item_optional ],
586 my $taxkey = $item->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $order->taxzone_id);
588 # sellprice 2.34 * qty 2.5 = 5.85
589 # 19%(5.85) = 1.1115; rounded = 1.11
590 # total rounded = 6.96
592 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
593 # line marge_total = 1.02
594 # line marge_percent = 17.4358974358974
596 my $title = 'default order, two item, one item optional, 19% tax not included';
597 my %data = $order->calculate_prices_and_taxes;
599 is($item->marge_total, 1.02, "${title}: item marge_total");
600 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
601 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
603 # optional items have a linetotal and marge, but ...
604 is($item_optional->marge_total, 1.02, "${title}: item optional marge_total");
605 is($item_optional->marge_percent, 17.4358974358974, "${title}: item optional marge_percent");
606 is($item_optional->marge_price_factor, 1, "${title}: item optional marge_price_factor");
608 # ... should not be calculated for the record sum
609 is($order->netamount, 5.85, "${title}: netamount");
610 is($order->amount, 6.96, "${title}: amount");
611 is($order->marge_total, 1.02, "${title}: marge_total");
612 is($order->marge_percent, 17.4358974358974, "${title}: marge_percent");
613 is($order->orderitems->[1]->optional, 1, "${title}: second order item has attribute optional");
614 # diag explain $order->orderitems->[1]->optional;
615 # diag explain \%data;
619 $buchungsgruppe->income_accno_id($taxzone) => {
631 taxes_by_chart_id => {
632 $tax->chart_id => 1.11,
639 linetotal_cost => 4.83,
641 tax_amount => 1.1115,
642 taxkey_id => $taxkey->id,
645 linetotal_cost => 4.83,
647 tax_amount => 1.1115,
648 taxkey_id => $taxkey->id,
652 }, "${title}: calculated data");
656 Support::TestSetup::login();
658 $transdate = DateTime->today_local;
659 $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
661 test_default_invoice_one_item_19_tax_not_included();
662 test_default_invoice_two_items_19_7_tax_not_included();
663 test_default_invoice_three_items_sellprice_rounding_discount();
664 test_default_invoice_one_item_19_tax_not_included_rounding_discount();
665 test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty();
666 test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice();
667 test_default_order_two_items_19_one_optional();
673 # set emacs to perl mode