X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/9af3e1927c6fe3c631b63310888f1909039f24b6..ac271c64e22976acedeb66e78902b3b34a2ee8c8:/SL/Controller/BankTransaction.pm diff --git a/SL/Controller/BankTransaction.pm b/SL/Controller/BankTransaction.pm index 69bc63114..fe21db1b4 100644 --- a/SL/Controller/BankTransaction.pm +++ b/SL/Controller/BankTransaction.pm @@ -24,11 +24,12 @@ use SL::DB::AccTransaction; use SL::DB::BankTransactionAccTrans; use SL::DB::Tax; use SL::DB::BankAccount; +use SL::DB::GLTransaction; use SL::DB::RecordTemplate; use SL::DB::SepaExportItem; use SL::DBUtils qw(like do_query); -use SL::Presenter::Tag qw(checkbox_tag); +use SL::Presenter::Tag qw(checkbox_tag html_tag); use Carp; use List::UtilsBy qw(partition_by); use List::MoreUtils qw(any); @@ -561,6 +562,13 @@ sub save_single_bank_transaction { my $bank_transaction = $data{bank_transaction}; + if ($bank_transaction->closed_period) { + return { + %data, + result => 'error', + message => $::locale->text('Cannot post payment for a closed period!'), + }; + } my (@warnings); my $worker = sub { @@ -633,22 +641,32 @@ sub save_single_bank_transaction { # 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 - - 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); + $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 => ($open_amount < $not_assigned_amount) ? $open_amount : $not_assigned_amount, + amount => $amount_for_payment, payment_type => $payment_type, source => $source, memo => $memo, - transdate => $bank_transaction->transdate->to_kivitendo); + transdate => $bank_transaction->valutadate->to_kivitendo); # ... and record the origin via BankTransactionAccTrans - if (scalar(@acc_ids) != 2) { + if (scalar(@acc_ids) < 2) { return { %data, result => 'error', @@ -715,17 +733,17 @@ sub save_single_bank_transaction { return grep { $_ } ($error, @warnings); } sub action_unlink_bank_transaction { - my ($self) = @_; + my ($self, %params) = @_; croak("No bank transaction ids") unless scalar @{ $::form->{ids}} > 0; - my $closedto = $::locale->parse_date_to_object($::instance_conf->get_closedto); my $success_count; foreach my $bt_id (@{ $::form->{ids}} ) { my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id); croak("No valid bank transaction found") unless (ref($bank_transaction) eq 'SL::DB::BankTransaction'); + croak t8('Cannot unlink payment for a closed period!') if $bank_transaction->closed_period; # everything in one transaction my $rez = $bank_transaction->db->with_transaction(sub { @@ -737,22 +755,23 @@ sub action_unlink_bank_transaction { foreach my $acc_trans_id_entry (@{ SL::DB::Manager::BankTransactionAccTrans->get_all(where => [bank_transaction_id => $bt_id ] )}) { my $acc_trans = SL::DB::Manager::AccTransaction->get_all(where => [acc_trans_id => $acc_trans_id_entry->acc_trans_id]); - # check closedto for acc trans entries - croak t8('Cannot unlink payment for a closed period!') if (ref $closedto && grep { $_->transdate < $closedto } @{ $acc_trans } ); # save trans_id and type die "no type" unless ($acc_trans_id_entry->ar_id || $acc_trans_id_entry->ap_id || $acc_trans_id_entry->gl_id); $trans_ids{$acc_trans_id_entry->ar_id} = 'ar' if $acc_trans_id_entry->ar_id; $trans_ids{$acc_trans_id_entry->ap_id} = 'ap' if $acc_trans_id_entry->ap_id; $trans_ids{$acc_trans_id_entry->gl_id} = 'gl' if $acc_trans_id_entry->gl_id; - # 2. all good -> ready to delete acc_trans and bt_acc link $acc_trans_id_entry->delete; $_->delete for @{ $acc_trans }; } # 3. update arap.paid (may not be 0, yet) + # or in case of gl, delete whole entry while (my ($trans_id, $type) = each %trans_ids) { - next if $type eq 'gl'; + if ($type eq 'gl') { + SL::DB::Manager::GLTransaction->delete_all(where => [ id => $trans_id ]); + next; + } die ("invalid type") unless $type =~ m/^(ar|ap)$/; # recalc and set paid via database query @@ -779,7 +798,7 @@ sub action_unlink_bank_transaction { } flash('ok', t8('#1 bank transaction bookings undone.', $success_count)); - $self->action_list_all(); + $self->action_list_all() unless $params{testcase}; } # # filters @@ -829,8 +848,13 @@ sub prepare_report { my %column_defs = ( ids => { raw_header_data => checkbox_tag("", id => "check_all", checkall => "[data-checkall=1]"), 'align' => 'center', - raw_data => sub { if (@{ $_[0]->linked_invoices } && !(grep {ref ($_) eq 'SL::DB::GLTransaction' } @{ $_[0]->linked_invoices })) { - checkbox_tag("ids[]", value => $_[0]->id, "data-checkall" => 1); } } }, + raw_data => sub { if (@{ $_[0]->linked_invoices }) { + if ($_[0]->closed_period) { + html_tag('text', "X"); #, tooltip => t8('Bank Transaction is in a closed period.')), + } else { + checkbox_tag("ids[]", value => $_[0]->id, "data-checkall" => 1); + } + } } }, transdate => { sub => sub { $_[0]->transdate_as_date } }, valutadate => { sub => sub { $_[0]->valutadate_as_date } }, remote_name => { },