]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/BankTransaction.pm
Kontoauszug verbuchen rückgängig machen. Closedto und GL
[mfinanz.git] / SL / Controller / BankTransaction.pm
index 69bc631140028975f770016b21d4121ce457fad5..fe21db1b47e5a4653ddbf6a543f2d35f4d6d98e0 100644 (file)
@@ -24,11 +24,12 @@ use SL::DB::AccTransaction;
 use SL::DB::BankTransactionAccTrans;
 use SL::DB::Tax;
 use SL::DB::BankAccount;
 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::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);
 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};
 
 
   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 {
   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
     # 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;
     $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,
     $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,
                           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
     # ... and record the origin via BankTransactionAccTrans
-    if (scalar(@acc_ids) != 2) {
+    if (scalar(@acc_ids) < 2) {
       return {
         %data,
         result  => 'error',
       return {
         %data,
         result  => 'error',
@@ -715,17 +733,17 @@ sub save_single_bank_transaction {
   return grep { $_ } ($error, @warnings);
 }
 sub action_unlink_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;
 
 
   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');
   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 {
 
     # 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]);
       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;
 
         # 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)
         # 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) {
       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
         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));
   }
 
   flash('ok', t8('#1 bank transaction bookings undone.', $success_count));
-  $self->action_list_all();
+  $self->action_list_all() unless $params{testcase};
 }
 #
 # filters
 }
 #
 # 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',
   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           => { },
     transdate             => { sub   => sub { $_[0]->transdate_as_date } },
     valutadate            => { sub   => sub { $_[0]->valutadate_as_date } },
     remote_name           => { },