+ } else {
+ my $unrounded = $trans->[$j]->{'amount'} * (1 + $tax_rate) * -1; # + $rounding_error;
+ my $rounded = $form->round_amount($unrounded, 2);
+ $rounding_error += $unrounded - $rounded;
+ $new_trans{'amount'} = $rounded;
+ $new_trans{'umsatz'} = abs($form->round_amount(($trans->[$j]->{'amount'} * (1 + $tax_rate)), 2)) * $ml;
+ $trans->[$j]->{'umsatz'} = abs($form->round_amount(($trans->[$j]->{'amount'} * (1 + $tax_rate)), 2)) * $ml;
+ $absumsatz += $form->round_amount($trans->[$j]->{'amount'} + $trans->[$j]->{'amount'} * $tax_rate, 2);
+ }
+
+ push @{ $form->{DATEV} }, [ \%new_trans, $trans->[$j] ];
+ }
+ }
+
+ my $idx = 0;
+ my $correction = 0;
+ while (abs($absumsatz) >= 0.01) {
+ if ($idx >= scalar @taxed) {
+ last if (!$correction);
+
+ $correction = 0;
+ $idx = 0;
+ }
+
+ my $transaction = $taxed[$idx]->[0];
+
+ my $old_amount = $transaction->{amount};
+ my $old_correction = $correction;
+ my @possible_diffs;
+
+ if (!$transaction->{diff}) {
+ @possible_diffs = (0.01, -0.01);
+ } else {
+ @possible_diffs = ($transaction->{diff});
+ }
+
+ foreach my $diff (@possible_diffs) {
+ my $net_amount = $form->round_amount(($transaction->{amount} + $diff) / $transaction->{tax_rate}, 2);
+ next if ($net_amount != $transaction->{net_amount});
+
+ $transaction->{diff} = $diff;
+ $transaction->{amount} += $diff;
+ $transaction->{umsatz} += $diff;
+ $absumsatz -= $diff;
+ $correction = 1;
+
+ last;