-
- my $open_amount = ($payment_type eq 'with_skonto_pt' ? $invoice->amount_less_skonto : $invoice->open_amount);
- my $amount_for_booking = abs(($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount);
+ # hotfix to get the signs right - compare absolute values and later set the signs
+ # should be better done elsewhere - changing not_assigned_amount to abs feels seriously bogus
+
+ my $open_amount = $payment_type eq 'with_skonto_pt' ? $invoice->amount_less_skonto : $invoice->open_amount;
+ $open_amount = abs($open_amount);
+ $open_amount -= $free_skonto_amount if ($payment_type eq 'free_skonto');
+ my $not_assigned_amount = abs($bank_transaction->not_assigned_amount);
+ my $amount_for_booking = ($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount;
+ my $amount_for_payment = $amount_for_booking;
+
+ # get the right direction for the payment bookings (all amounts < 0 are stornos, credit notes or negative ap)
+ $amount_for_payment *= -1 if $invoice->amount < 0;
+ $free_skonto_amount *= -1 if ($free_skonto_amount && $invoice->amount < 0);
+ # get the right direction for the bank transaction