- foreach my $bt_id (@{ $::form->{proposal_ids} }) {
- #mark bt as booked
- my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
- $bt->invoice_amount($bt->amount);
- $bt->save;
-
- #pay invoice
- my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
- $arap = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
- $arap->pay_invoice(chart_id => $bt->local_bank_account->chart_id,
- trans_id => $arap->id,
- amount => $arap->amount,
- transdate => $bt->transdate);
- $arap->save;
-
- #create record link
- my @props = (
+
+ # 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);
+ } else {
+ # use the whole amount of the bank transaction for the invoice, overpay the invoice if necessary
+
+ # $invoice->open_amount is negative for credit_notes
+ # $bank_transaction->amount is negative for outgoing transactions
+ # so $amount_of_transaction is negative but needs positive
+ # $invoice->open_amount may be negative for ap_transaction but may be positiv for negative ap_transaction
+ # if $invoice->open_amount is negative $bank_transaction->amount is positve
+ # if $invoice->open_amount is positive $bank_transaction->amount is negative
+ # but amount of transaction is for both positive
+
+ $amount_of_transaction *= -1 if ($invoice->amount < 0);
+
+ # if we have a skonto case - the last invoice needs skonto
+ $amount_of_transaction = $invoice->amount_less_skonto if ($payment_type eq 'with_skonto_pt');
+
+
+ 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)),
+ };
+ }
+ }
+ # Record a record link from the bank transaction to the invoice
+ my %props = (