]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/BankTransaction.pm
locales freies Skonto
[mfinanz.git] / SL / Controller / BankTransaction.pm
index c7215b21c6a4127d195326be88916138f722c1e2..638da64c9fe797089d1c337f865e767d08408c4e 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::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);
@@ -308,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 });
@@ -630,12 +627,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
@@ -651,6 +661,7 @@ sub save_single_bank_transaction {
 
     # 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;
 
@@ -658,12 +669,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->transdate->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 {
@@ -765,8 +777,12 @@ sub action_unlink_bank_transaction {
         $_->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
@@ -843,8 +859,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           => { },