X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FBankTransaction.pm;h=9793b33c87086b74445d273ed4d090a160900edb;hb=bc89ebf428c7bde2205b31c5c9abb66879e673e4;hp=fe21db1b47e5a4653ddbf6a543f2d35f4d6d98e0;hpb=ac271c64e22976acedeb66e78902b3b34a2ee8c8;p=kivitendo-erp.git diff --git a/SL/Controller/BankTransaction.pm b/SL/Controller/BankTransaction.pm index fe21db1b4..9793b33c8 100644 --- a/SL/Controller/BankTransaction.pm +++ b/SL/Controller/BankTransaction.pm @@ -309,20 +309,16 @@ sub action_ajax_payment_suggestion { # create an HTML blob to be used by the js function add_invoices in templates/webpages/bank_transactions/list.html # and return encoded as JSON - my $bt = SL::DB::Manager::BankTransaction->find_by( id => $::form->{bt_id} ); - my $invoice = SL::DB::Manager::Invoice->find_by( id => $::form->{prop_id} ) || SL::DB::Manager::PurchaseInvoice->find_by( id => $::form->{prop_id} ); + croak("Need bt_id") unless $::form->{bt_id}; - die unless $bt and $invoice; + my $invoice = SL::DB::Manager::Invoice->find_by( id => $::form->{prop_id} ) || SL::DB::Manager::PurchaseInvoice->find_by( id => $::form->{prop_id} ); - my @select_options = $invoice->get_payment_select_options_for_bank_transaction($::form->{bt_id}); + croak("No valid invoice found") unless $invoice; - my $html; - $html = $self->render( + my $html = $self->render( 'bank_transactions/_payment_suggestion', { output => 0 }, bt_id => $::form->{bt_id}, - prop_id => $::form->{prop_id}, invoice => $invoice, - SELECT_OPTIONS => \@select_options, ); $self->render(\ SL::JSON::to_json( { 'html' => "$html" } ), { layout => 0, type => 'json', process => 0 }); @@ -574,7 +570,6 @@ sub save_single_bank_transaction { my $worker = sub { my $bt_id = $data{bank_transaction_id}; my $sign = $bank_transaction->amount < 0 ? -1 : 1; - my $not_assigned_amount = $bank_transaction->not_assigned_amount; my $payment_received = $bank_transaction->amount > 0; my $payment_sent = $bank_transaction->amount < 0; @@ -621,9 +616,11 @@ sub save_single_bank_transaction { my $memo = ($data{memos} // [])->[$n_invoices]; $n_invoices++ ; + # safety check invoice open + croak("Invoice closed. Cannot proceed.") unless ($invoice->open_amount); - - if (!$not_assigned_amount && $invoice->open_amount) { + if ( ($payment_sent && $bank_transaction->not_assigned_amount >= 0) + || ($payment_received && $bank_transaction->not_assigned_amount <= 0)) { return { %data, result => 'error', @@ -631,12 +628,25 @@ sub save_single_bank_transaction { }; } - my $payment_type; + my ($payment_type, $free_skonto_amount); if ( defined $::form->{invoice_skontos}->{"$bt_id"} ) { $payment_type = shift(@{ $::form->{invoice_skontos}->{"$bt_id"} }); } else { $payment_type = 'without_skonto'; - }; + } + + if ($payment_type eq 'free_skonto') { + # parse user input > 0 + if ($::form->parse_amount(\%::myconfig, $::form->{"free_skonto_amount"}->{"$bt_id"}{$invoice->id}) > 0) { + $free_skonto_amount = $::form->parse_amount(\%::myconfig, $::form->{"free_skonto_amount"}->{"$bt_id"}{$invoice->id}); + } else { + return { + %data, + result => 'error', + message => $::locale->text("Free skonto amount has to be a positive number."), + }; + } + } # 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 @@ -645,13 +655,15 @@ sub save_single_bank_transaction { # 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; + $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 $amount_for_booking *= $sign; @@ -659,12 +671,13 @@ sub save_single_bank_transaction { # ... 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); + trans_id => $invoice->id, + amount => $amount_for_payment, + payment_type => $payment_type, + source => $source, + memo => $memo, + skonto_amount => $free_skonto_amount, + transdate => $bank_transaction->valutadate->to_kivitendo); # ... and record the origin via BankTransactionAccTrans if (scalar(@acc_ids) < 2) { return {