+    rounding                                    =>  0,
+  }, "${title}: calculated data");
+}
+
+sub test_default_invoice_one_item_19_tax_not_included_rounding_discount() {
+  reset_state();
+
+  my $item   = new_item(qty => 6, part => $parts[2], discount => 0.03);
+  my $invoice = new_invoice(
+    taxincluded  => 0,
+    invoiceitems => [ $item ],
+  );
+
+  my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
+
+  # 6 parts for 0.60 with 3% discount
+  #
+  # linetotal = sellprice 0.60 * qty 6 * discount (1 - 0.03) = 3.492 rounded 3.49
+  # total = 3.49 + 0.66 = 4.15
+  #
+
+  my $title = 'default invoice, one item, sellprice, rounding, discount';
+  my %data  = $invoice->calculate_prices_and_taxes;
+
+  is($invoice->netamount,         3.49,              "${title}: netamount");
+
+  is($invoice->amount,            4.15,              "${title}: amount");
+
+  is($invoice->marge_total,       3.49,              "${title}: marge_total");
+  is($invoice->marge_percent,      100,              "${title}: marge_percent");
+
+  is_deeply(\%data, {
+    allocated                                    => {},
+    amounts                                      => {
+      $buchungsgruppe->income_accno_id($taxzone) => {
+        amount                                   => 3.49,
+        tax_id                                   => $tax->id,
+        taxkey                                   => 3,
+      },
+    },
+    amounts_cogs                                 => {},
+    assembly_items                               => [
+      [],
+    ],
+    exchangerate                                 => 1,
+    taxes_by_chart_id                            => {
+      $tax->chart_id                             => 0.66,
+    },
+    taxes_by_tax_id                              => {
+      $tax->id                                   => 0.66310,
+    },
+    items                                        => [
+      { linetotal                                => 3.49,
+        linetotal_cost                           => 0,
+        sellprice                                => 0.58,
+        tax_amount                               => 0.6631,
+        taxkey_id                                => $taxkeys{$item->parts_id}->id,
+      },
+    ],
+    rounding                                     =>  0,
+  }, "${title}: calculated data");
+}
+
+sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_huge_qty() {
+  reset_state();
+
+  my $item   = new_item(qty => 100000, part => $parts[2], discount => 0.03, sellprice => 0.10);
+  my $invoice = new_invoice(
+    taxincluded  => 0,
+    invoiceitems => [ $item ],
+  );
+
+  my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
+
+  my $title = 'default invoice, one item, 19% tax not included, rounding, discount, huge qty';
+  my %data  = $invoice->calculate_prices_and_taxes;
+
+  is($invoice->netamount,         9700,              "${title}: netamount");
+
+  is($invoice->amount,           11543,              "${title}: amount");
+
+  is($invoice->marge_total,       9700,              "${title}: marge_total");
+  is($invoice->marge_percent,      100,              "${title}: marge_percent");
+
+  is_deeply(\%data, {
+    allocated                                    => {},
+    amounts                                      => {
+      $buchungsgruppe->income_accno_id($taxzone) => {
+        amount                                   => 9700,
+        tax_id                                   => $tax->id,
+        taxkey                                   => 3,
+      },
+    },
+    amounts_cogs                                 => {},
+    assembly_items                               => [
+      [],
+    ],
+    exchangerate                                 => 1,
+    taxes_by_chart_id                            => {
+      $tax->chart_id                             => 1843,
+    },
+    taxes_by_tax_id                              => {
+      $tax->id                                   => 1843,
+    },
+    items                                        => [
+      { linetotal                                => 9700,
+        linetotal_cost                           => 0,
+        sellprice                                => 0.1,
+        tax_amount                               => 1843,
+        taxkey_id                                => $taxkeys{$item->parts_id}->id,
+      },
+    ],
+    rounding                                    =>  0,
+  }, "${title}: calculated data");
+}
+
+sub test_default_invoice_one_item_19_tax_not_included_rounding_discount_big_qty_low_sellprice() {
+  reset_state();
+
+  my $item   = new_item(qty => 10001, sellprice => 0.007, discount => 0.035);
+  my $invoice = new_invoice(
+    taxincluded  => 0,
+    invoiceitems => [ $item ],
+  );
+
+  my %taxkeys = map { ($_->id => $_->get_taxkey(date => $transdate, is_sales => 1, taxzone => $invoice->taxzone_id)) } uniq map { $_->part } ($item);
+
+  # item 1:
+  # discount = sellprice 0.007 * discount (0.035) = 0.000245; rounded 0.00
+  # sellprice = sellprice 0.007 - discount 0.00 = 0.007
+  # linetotal = sellprice 0.007 * qty 10001 * (1 - 0.035) = 67.556755; rounded 67.56
+  # 19%(67.56) = 12.8364; rounded = 12.84
+  # total rounded = 80.40
+
+  # lastcost 1.93 * qty 10001 = 19301.93; rounded 19301.93
+  # line marge_total = 67.56-19301.93 = -19234.37
+  # line marge_percent = 100*-19234.37/67.56 = -28470.0562462996
+
+  my $title = 'default invoice one item 19 tax not included rounding discount big qty low sellprice';
+  my %data  = $invoice->calculate_prices_and_taxes;
+
+  is($invoice->netamount,                 67.56,    "${title}: netamount");
+
+  is($invoice->amount,                    80.40,    "${title}: amount");
+
+  is($invoice->marge_total,           -19234.37,    "${title}: marge_total");
+  is($invoice->marge_percent, -28470.0562462996,    "${title}: marge_percent");
+
+  is_deeply(\%data, {
+    allocated                                    => {},
+    amounts                                      => {
+      $buchungsgruppe->income_accno_id($taxzone) => {
+        amount                                   => 67.56,
+        tax_id                                   => $tax->id,
+        taxkey                                   => 3,
+      },
+    },
+    amounts_cogs                                 => {},
+    assembly_items                               => [
+      [],
+    ],
+    exchangerate                                 => 1,
+    taxes_by_chart_id                            => {
+      $tax->chart_id                             => 12.84,
+    },
+    taxes_by_tax_id                              => {
+      $tax->id                                   => 12.8364,
+    },
+    items                                        => [
+      { linetotal                                => 67.56,
+        linetotal_cost                           => 19301.93,
+        sellprice                                => 0.007,
+        tax_amount                               => 12.8364,
+        taxkey_id                                => $taxkeys{$item->parts_id}->id,
+      },
+    ],
+    rounding                                    =>  0,