+sub test_default_order_two_items_19_one_optional() {
+ reset_state();
+
+ my $item = new_order_item(qty => 2.5);
+ my $item_optional = new_order_item(qty => 2.5, optional => 1);
+
+ my $order = new_order(
+ taxincluded => 0,
+ orderitems => [ $item, $item_optional ],
+ );
+
+ my $taxkey = $item->part->get_taxkey(date => $transdate, is_sales => 1, taxzone => $order->taxzone_id);
+
+ # sellprice 2.34 * qty 2.5 = 5.85
+ # 19%(5.85) = 1.1115; rounded = 1.11
+ # total rounded = 6.96
+
+ # lastcost 1.93 * qty 2.5 = 4.825; rounded 4.83
+ # line marge_total = 1.02
+ # line marge_percent = 17.4358974358974
+
+ my $title = 'default order, two item, one item optional, 19% tax not included';
+ my %data = $order->calculate_prices_and_taxes;
+
+ is($item->marge_total, 1.02, "${title}: item marge_total");
+ is($item->marge_percent, 17.4358974358974, "${title}: item marge_percent");
+ is($item->marge_price_factor, 1, "${title}: item marge_price_factor");
+
+ # optional items have a linetotal and marge, but ...
+ is($item_optional->marge_total, 1.02, "${title}: item optional marge_total");
+ is($item_optional->marge_percent, 17.4358974358974, "${title}: item optional marge_percent");
+ is($item_optional->marge_price_factor, 1, "${title}: item optional marge_price_factor");
+
+ # ... should not be calculated for the record sum
+ is($order->netamount, 5.85, "${title}: netamount");
+ is($order->amount, 6.96, "${title}: amount");
+ is($order->marge_total, 1.02, "${title}: marge_total");
+ is($order->marge_percent, 17.4358974358974, "${title}: marge_percent");
+ is($order->orderitems->[1]->optional, 1, "${title}: second order item has attribute optional");
+ # diag explain $order->orderitems->[1]->optional;
+ # diag explain \%data;
+ is_deeply(\%data, {
+ allocated => {},
+ amounts => {
+ $buchungsgruppe->income_accno_id($taxzone) => {
+ amount => 5.85,
+ tax_id => $tax->id,
+ taxkey => 3,
+ },
+ },
+ amounts_cogs => {},
+ assembly_items => [
+ [],
+ [],
+ ],
+ exchangerate => 1,
+ taxes_by_chart_id => {
+ $tax->chart_id => 1.11,
+ },
+ taxes_by_tax_id => {
+ $tax->id => 1.1115,
+ },
+ items => [
+ { linetotal => 5.85,
+ linetotal_cost => 4.83,
+ sellprice => 2.34,
+ tax_amount => 1.1115,
+ taxkey_id => $taxkey->id,
+ },
+ { linetotal => 5.85,
+ linetotal_cost => 4.83,
+ sellprice => 2.34,
+ tax_amount => 1.1115,
+ taxkey_id => $taxkey->id,
+ },
+ ],
+ rounding => 0,
+ }, "${title}: calculated data");
+}