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,
105 my $part = delete($params{part}) || $parts[0];
107 return create_invoice_item(
113 sub test_default_invoice_one_item_19_tax_not_included() {
116 my $item = new_item(qty => 2.5);
117 my $invoice = new_invoice(
119 invoiceitems => [ $item ],
122 my $taxkey = $item->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
124 # sellprice 2.34 * qty 2.5 = 5.85
125 # 19%(5.85) = 1.1115; rounded = 1.11
126 # total rounded = 6.96
128 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
129 # line marge_total = 1.02
130 # line marge_percent = 17.4358974358974
132 my $title = 'default invoice, one item, 19% tax not included';
133 my %data = $invoice->calculate_prices_and_taxes;
135 is($item->marge_total, 1.02, "${title}: item marge_total");
136 is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
137 is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
139 is($invoice->netamount, 5.85, "${title}: netamount");
140 is($invoice->amount, 6.96, "${title}: amount");
141 is($invoice->marge_total, 1.02, "${title}: marge_total");
142 is($invoice->marge_percent, 17.4358974358974, "${title}: marge_percent");
147 $buchungsgruppe->income_accno_id($taxzone) => {
158 taxes_by_chart_id => {
159 $tax->chart_id => 1.11,
166 linetotal_cost => 4.83,
168 tax_amount => 1.1115,
169 taxkey_id => $taxkey->id,
173 }, "${title}: calculated data");
176 sub test_default_invoice_two_items_19_7_tax_not_included() {
179 my $item1 = new_item(qty => 2.5);
180 my $item2 = new_item(qty => 1.2, part => $parts[1]);
181 my $invoice = new_invoice(
183 invoiceitems => [ $item1, $item2 ],
186 my $taxkey1 = $item1->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
187 my $taxkey2 = $item2->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id);
190 # sellprice 2.34 * qty 2.5 = 5.85
191 # 19%(5.85) = 1.1115; rounded = 1.11
192 # total rounded = 6.96
194 # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
195 # line marge_total = 1.02
196 # line marge_percent = 17.4358974358974
199 # sellprice 9.714 * qty 1.2 = 11.6568 rounded 11.66
200 # 7%(11.6568) = 0.815976; rounded = 0.82
202 # total rounded = 12.48
204 # lastcost 5.473 * qty 1.2 = 6.5676; rounded 6.57
205 # line marge_total = 5.09
206 # line marge_percent = 43.6535162950257
208 my $title = 'default invoice, two item, 19/7% tax not included';
209 my %data = $invoice->calculate_prices_and_taxes;
211 is($item1->marge_total, 1.02, "${title}: item1 marge_total");
212 is($item1->marge_percent, 17.4358974358974, "${title}: item1 marge_percent");
213 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
215 is($item2->marge_total, 5.09, "${title}: item2 marge_total");
216 is($item2->marge_percent, 43.6535162950257, "${title}: item2 marge_percent");
217 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
219 is($invoice->netamount, 5.85 + 11.66, "${title}: netamount");
220 is($invoice->amount, 6.96 + 12.48, "${title}: amount");
221 is($invoice->marge_total, 1.02 + 5.09, "${title}: marge_total");
222 is($invoice->marge_percent, 34.8943460879497, "${title}: marge_percent");
227 $buchungsgruppe->income_accno_id($taxzone) => {
232 $buchungsgruppe7->income_accno_id($taxzone) => {
243 taxes_by_chart_id => {
244 $tax->chart_id => 1.11,
245 $tax7->chart_id => 0.82,
253 linetotal_cost => 4.83,
255 tax_amount => 1.1115,
256 taxkey_id => $taxkey1->id,
258 { linetotal => 11.66,
259 linetotal_cost => 6.57,
261 tax_amount => 0.8162,
262 taxkey_id => $taxkey2->id,
266 }, "${title}: calculated data");
269 sub test_default_invoice_three_items_sellprice_rounding_discount() {
272 my $item1 = new_item(qty => 1, sellprice => 5.55, discount => .05);
273 my $item2 = new_item(qty => 1, sellprice => 5.50, discount => .05);
274 my $item3 = new_item(qty => 1, sellprice => 5.00, discount => .05);
275 my $invoice = new_invoice(
277 invoiceitems => [ $item1, $item2, $item3 ],
280 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item1, $item2, $item3);
283 # discount = sellprice 5.55 * discount (0.05) = 0.2775; rounded 0.28
284 # linetotal = sellprice 5.55 * (1 - discount 0.05) * qty 1 = 5.2725; rounded 5.27
285 # 19%(5.27) = 1.0013; rounded = 1.00
286 # total rounded = 6.27
288 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
289 # line marge_total = 5.27 - 1.93 = 3.34
290 # line marge_percent = 63.3776091081594
293 # discount = sellprice 5.50 * discount 0.05 = 0.275; rounded 0.28
294 # linetotal = sellprice 5.50 * (1 - discount 0.05) * qty 1 = 5.225; rounded 5.23
295 # 19%(5.23) = .99370; rounded = 0.99
296 # total rounded = 6.22
298 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
299 # line marge_total = 5.23 - 1.93 = 3.30
300 # line marge_percent = 3.30/5.23 = 0.630975143403442
303 # discount = sellprice 5.00 * discount 0.05 = 0.05 = 0.25; rounded 0.25
304 # linetotal = sellprice 5.00 (1 - discount 0.05) * qty 1 = 4.75; rounded 4.75
305 # 19%(4.75) = 0.9025; rounded = 0.90
306 # total rounded = 5.65
308 # lastcost 1.93 * qty 1 = 1.93; rounded 1.93
309 # line marge_total = 4.75 - 1.93 = 2.82
310 # line marge_percent = 2.82/4.75 = 59.3684210526316
312 my $title = 'default invoice, three items, sellprice, rounding, discount';
313 my %data = $invoice->calculate_prices_and_taxes;
315 is($item1->marge_total, 3.34, "${title}: item1 marge_total");
316 is($item1->marge_percent, 63.3776091081594, "${title}: item1 marge_percent");
317 is($item1->marge_price_factor, 1, "${title}: item1 marge_price_factor");
319 is($item2->marge_total, 3.30, "${title}: item2 marge_total");
320 is($item2->marge_percent, 63.0975143403442, "${title}: item2 marge_percent");
321 is($item2->marge_price_factor, 1, "${title}: item2 marge_price_factor");
323 is($item3->marge_total, 2.82, "${title}: item3 marge_total");
324 is($item3->marge_percent, 59.3684210526316, "${title}: item3 marge_percent");
325 is($item3->marge_price_factor, 1, "${title}: item3 marge_price_factor");
327 is($invoice->netamount, 5.27 + 5.23 + 4.75, "${title}: netamount");
329 # 6.27 + 6.22 + 5.65 = 18.14
330 # 1.19*(5.27 + 5.23 + 4.75) = 18.1475; rounded 18.15
331 #is($invoice->amount, 6.27 + 6.22 + 5.65, "${title}: amount");
332 is($invoice->amount, 18.15, "${title}: amount");
334 is($invoice->marge_total, 3.34 + 3.30 + 2.82, "${title}: marge_total");
335 is($invoice->marge_percent, 62.0327868852459, "${title}: marge_percent");
340 $buchungsgruppe->income_accno_id($taxzone) => {
351 taxes_by_chart_id => {
352 $tax->chart_id => 2.9,
359 linetotal_cost => 1.93,
361 tax_amount => 1.0013,
362 taxkey_id => $taxkeys{$item1->parts_id}->id,
365 linetotal_cost => 1.93,
367 tax_amount => 0.9937,
368 taxkey_id => $taxkeys{$item2->parts_id}->id,
371 linetotal_cost => 1.93,
373 tax_amount => 0.9025,
374 taxkey_id => $taxkeys{$item3->parts_id}->id,
378 }, "${title}: calculated data");
381 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
384 my $item = new_item(qty => 6, part => $parts[2], discount => 0.03);
385 my $invoice = new_invoice(
387 invoiceitems => [ $item ],
390 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
392 # 6 parts for 0.60 with 3% discount
394 # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
395 # total = 3.49 + 0.66 = 4.15
398 my $title = 'default invoice, one item, sellprice, rounding, discount';
399 my %data = $invoice->calculate_prices_and_taxes;
401 is($invoice->netamount, 3.49, "${title}: netamount");
403 is($invoice->amount, 4.15, "${title}: amount");
405 is($invoice->marge_total, 3.49, "${title}: marge_total");
406 is($invoice->marge_percent, 100, "${title}: marge_percent");
411 $buchungsgruppe->income_accno_id($taxzone) => {
422 taxes_by_chart_id => {
423 $tax->chart_id => 0.66,
432 tax_amount => 0.6631,
433 taxkey_id => $taxkeys{$item->parts_id}->id,
437 }, "${title}: calculated data");
440 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty() {
443 my $item = new_item(qty => 100000, part => $parts[2], discount => 0.03, sellprice => 0.10);
444 my $invoice = new_invoice(
446 invoiceitems => [ $item ],
449 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
451 my $title = 'default invoice, one item, 19% tax not included, rounding, discount, huge qty';
452 my %data = $invoice->calculate_prices_and_taxes;
454 is($invoice->netamount, 9700, "${title}: netamount");
456 is($invoice->amount, 11543, "${title}: amount");
458 is($invoice->marge_total, 9700, "${title}: marge_total");
459 is($invoice->marge_percent, 100, "${title}: marge_percent");
464 $buchungsgruppe->income_accno_id($taxzone) => {
475 taxes_by_chart_id => {
476 $tax->chart_id => 1843,
486 taxkey_id => $taxkeys{$item->parts_id}->id,
490 }, "${title}: calculated data");
493 sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice() {
496 my $item = new_item(qty => 10001, sellprice => 0.007, discount => 0.035);
497 my $invoice = new_invoice(
499 invoiceitems => [ $item ],
502 my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
505 # discount = sellprice 0.007 * discount (0.035) = 0.000245; rounded 0.00
506 # sellprice = sellprice 0.007 - discount 0.00 = 0.007
507 # linetotal = sellprice 0.007 * qty 10001 * (1 - 0.035) = 67.556755; rounded 67.56
508 # 19%(67.56) = 12.8364; rounded = 12.84
509 # total rounded = 80.40
511 # lastcost 1.93 * qty 10001 = 19301.93; rounded 19301.93
512 # line marge_total = 67.56-19301.93 = -19234.37
513 # line marge_percent = 100*-19234.37/67.56 = -28470.0562462996
515 my $title = 'default invoice one item 19 tax not included rounding discount big qty low sellprice';
516 my %data = $invoice->calculate_prices_and_taxes;
518 is($invoice->netamount, 67.56, "${title}: netamount");
520 is($invoice->amount, 80.40, "${title}: amount");
522 is($invoice->marge_total, -19234.37, "${title}: marge_total");
523 is($invoice->marge_percent, -28470.0562462996, "${title}: marge_percent");
528 $buchungsgruppe->income_accno_id($taxzone) => {
539 taxes_by_chart_id => {
540 $tax->chart_id => 12.84,
546 { linetotal => 67.56,
547 linetotal_cost => 19301.93,
549 tax_amount => 12.8364,
550 taxkey_id => $taxkeys{$item->parts_id}->id,
554 }, "${title}: calculated data");
558 Support::TestSetup::login();
560 $transdate = DateTime->today_local;
561 $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
563 test_default_invoice_one_item_19_tax_not_included();
564 test_default_invoice_two_items_19_7_tax_not_included();
565 test_default_invoice_three_items_sellprice_rounding_discount();
566 test_default_invoice_one_item_19_tax_not_included_rounding_discount();
567 test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty();
568 test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice();
574 # set emacs to perl mode