-
-
- # pay invoice or go to the next bank transaction if the amount is not sufficiently high
- if ($invoice->open_amount <= $amount_of_transaction && $n_invoices < $max_invoices) {
- my $open_amount = ($payment_type eq 'with_skonto_pt'?$invoice->amount_less_skonto:$invoice->open_amount);
- # first calculate new bank transaction amount ...
- if ($invoice->is_sales) {
- $amount_of_transaction -= $sign * $open_amount;
- $bank_transaction->invoice_amount($bank_transaction->invoice_amount + $open_amount);
- } else {
- $amount_of_transaction += $sign * $open_amount;
- $bank_transaction->invoice_amount($bank_transaction->invoice_amount - $open_amount);
- }
- # ... and then pay the invoice
- $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
- trans_id => $invoice->id,
- amount => $open_amount,
- payment_type => $payment_type,
- source => $source,
- memo => $memo,
- transdate => $bank_transaction->transdate->to_kivitendo);
- } elsif (( $invoice->is_sales && $invoice->invoice_type eq 'credit_note' ) ||
- (!$invoice->is_sales && $invoice->invoice_type eq 'ap_transaction' )) {
- # no check for overpayment/multiple payments
-
- # 1. $invoice->open_amount is arap.amount - ararp.paid (always positive!)
- # 2. $bank_transaction->amount is negative for outgoing transactions and positive for
- # incoming transactions.
- # 1. and 2. => we have to turn the sign for invoice_amount in bank_transactions
- # for verifying expected data, check t/bank/bank_transactions.t
- $bank_transaction->invoice_amount($invoice->open_amount * -1);
-
- $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
- trans_id => $invoice->id,
- amount => $invoice->open_amount,
- payment_type => $payment_type,
- source => $source,
- memo => $memo,
- transdate => $bank_transaction->transdate->to_kivitendo);
- } else { # use the whole amount of the bank transaction for the invoice, overpay the invoice if necessary
- my $overpaid_amount = $amount_of_transaction - $invoice->open_amount;
- $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
- trans_id => $invoice->id,
- amount => $amount_of_transaction,
- payment_type => $payment_type,
- source => $source,
- memo => $memo,
- transdate => $bank_transaction->transdate->to_kivitendo);
- $bank_transaction->invoice_amount($bank_transaction->amount);
- $amount_of_transaction = 0;
-
- if ($overpaid_amount >= 0.01) {
- push @warnings, {
- %data,
- result => 'warning',
- message => $::locale->text('Invoice #1 was overpaid by #2.', $invoice->invnumber, $::form->format_amount(\%::myconfig, $overpaid_amount, 2)),
- };
- }
- }
+ # pay invoice
+ # TODO rewrite this: really booked amount should be a return value of Payment.pm
+ # also this controller shouldnt care about how to calc skonto. we simply delegate the
+ # payment_type to the helper and get the corresponding bank_transaction values back
+ # 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);
+ $not_assigned_amount = abs($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;
+ # get the right direction for the bank transaction
+ $amount_for_booking *= $sign;
+
+ $bank_transaction->invoice_amount($bank_transaction->invoice_amount + $amount_for_booking);
+
+ # ... and then pay the invoice
+ my @acc_ids = $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
+ trans_id => $invoice->id,
+ amount => $amount_for_payment,
+ payment_type => $payment_type,
+ source => $source,
+ memo => $memo,
+ transdate => $bank_transaction->valutadate->to_kivitendo);
+ # ... and record the origin via BankTransactionAccTrans
+ if (scalar(@acc_ids) < 2) {
+ return {
+ %data,
+ result => 'error',
+ message => $::locale->text("Unable to book transactions for bank purpose #1", $bank_transaction->purpose),
+ };
+ }
+ foreach my $acc_trans_id (@acc_ids) {
+ my $id_type = $invoice->is_sales ? 'ar' : 'ap';
+ my %props_acc = (
+ acc_trans_id => $acc_trans_id,
+ bank_transaction_id => $bank_transaction->id,
+ $id_type => $invoice->id,
+ );
+ SL::DB::BankTransactionAccTrans->new(%props_acc)->save;
+ }