HTML::Entities in InstallationCheck aufgenommen
[kivitendo-erp.git] / SL / Controller / FinancialControllingReport.pm
index bedc762..c10c09d 100644 (file)
@@ -3,7 +3,7 @@ package SL::Controller::FinancialControllingReport;
 use strict;
 use parent qw(SL::Controller::Base);
 
-use List::Util qw(sum);
+use List::Util qw(min sum);
 
 use SL::DB::Order;
 use SL::DB::ProjectType;
@@ -24,7 +24,6 @@ my %sort_columns = (
   transaction_description => t8('Transaction description'),
   globalprojectnumber     => t8('Project'),
   globalproject_type      => t8('Project Type'),
-  netamount               => t8('Order amount'),
 );
 
 sub action_list {
@@ -51,13 +50,13 @@ sub prepare_report {
   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
   $self->{report} = $report;
 
-  my @columns     = qw(customer globalprojectnumber globalproject_type ordnumber netamount delivered_amount delivered_amount_p billed_amount billed_amount_p paid_amount paid_amount_p
+  my @columns     = qw(customer globalprojectnumber globalproject_type transaction_description ordnumber net_amount delivered_amount delivered_amount_p billed_amount billed_amount_p paid_amount paid_amount_p
                        billable_amount billable_amount_p other_amount);
-  my @sortable    = qw(ordnumber transdate customer netamount globalprojectnumber globalproject_type);
-  $self->{number_columns} = [ qw(netamount billed_amount billed_amount_p delivered_amount delivered_amount_p paid_amount paid_amount_p other_amount billable_amount billable_amount_p) ];
+  my @sortable    = qw(ordnumber transdate customer globalprojectnumber globalproject_type transaction_description );
+  $self->{number_columns} = [ qw(net_amount billed_amount billed_amount_p delivered_amount delivered_amount_p paid_amount paid_amount_p other_amount billable_amount billable_amount_p) ];
 
   my %column_defs           = (
-    netamount               => {                                                                                         },
+    net_amount              => { text     => $::locale->text('Order amount')                                             },
     billed_amount           => { text     => $::locale->text('Billed amount')                                            },
     billed_amount_p         => { text     => $::locale->text('%')                                                        },
     delivered_amount        => { text     => $::locale->text('Delivered amount')                                         },
@@ -67,6 +66,7 @@ sub prepare_report {
     billable_amount         => { text     => $::locale->text('Billable amount')                                          },
     billable_amount_p       => { text     => $::locale->text('%')                                                        },
     other_amount            => { text     => $::locale->text('Billed extra expenses')                                    },
+    transaction_description => { text     => $::locale->text('Transaction description')                                  },
     ordnumber               => { obj_link => sub { $self->link_to($_[0])                                              }  },
     customer                => {      sub => sub { $_[0]->customer->name                                              },
                                  obj_link => sub { $self->link_to($_[0]->customer)                                    }  },
@@ -76,8 +76,8 @@ sub prepare_report {
                                  sub      => sub { $_[0]->globalproject_id ? $_[0]->globalproject->project_type->description : '' }  },
   );
 
-  map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
-  map { $column_defs{$_}->{align} = 'right' } @{ $self->{number_columns} };
+  $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) for keys %column_defs;
+  $column_defs{$_}->{align}  = 'right'                                                        for @{ $self->{number_columns} };
 
   $report->set_options(
     std_column_visibility => 1,
@@ -120,12 +120,37 @@ sub calculate_data {
     $order->{other_amount}      = $billed_amount             - $order->{billed_amount};
     $order->{billable_amount}   = $order->{delivered_amount} - $order->{billed_amount};
 
+    if ($order->periodic_invoices_config) {
+      $order->{net_amount} = $self->calculate_periodic_invoices_order_netamount($order);
+
+    } else {
+      $order->{net_amount} = $order->netamount;
+    }
+
     foreach (qw(delivered billed paid billable)) {
-      $order->{"${_}_amount_p"} = $order->netamount * 1 ? $order->{"${_}_amount"} * 100 / $order->netamount : undef;
+      $order->{"${_}_amount_p"} = $order->{net_amount} * 1 ? $order->{"${_}_amount"} * 100 / $order->{net_amount} : undef;
     }
   }
 }
 
+sub calculate_periodic_invoices_order_netamount {
+  my ($self, $order) = @_;
+
+  my $cfg        = $order->periodic_invoices_config;
+  my $num_years  = 0;
+  my $cur_date   = $cfg->start_date->clone;
+  my $end_date   = $cfg->terminated ? $self->end_date : undef;
+  $end_date    //= DateTime->today_local;
+  $end_date      = min($end_date, DateTime->today_local);
+
+  while ($cur_date <= $end_date) {
+    $num_years++;
+    $cur_date->add(years => 1);
+  }
+
+  return $num_years * $order->netamount * (12 / $order->periodic_invoices_config->get_period_length);
+}
+
 sub sum_items {
   my ($self, %params) = @_;
 
@@ -173,8 +198,8 @@ sub list_objects {
   my ($self)      = @_;
   my $modify_data = sub {
     my ($data) = @_;
-    map { $data->{$_}->{data} = defined $data->{$_}->{data} ? int($data->{$_}->{data}) : ''  } grep {  m/_p$/ } @{ $self->{number_columns} };
-    map { $data->{$_}->{data} = $::form->format_amount(\%::myconfig, $data->{$_}->{data}, 2) } grep { !m/_p$/ } @{ $self->{number_columns} };
+    $data->{$_}->{data} = defined $data->{$_}->{data} ? int($data->{$_}->{data}) : ''  for grep {  m/_p$/ } @{ $self->{number_columns} };
+    $data->{$_}->{data} = $::form->format_amount(\%::myconfig, $data->{$_}->{data}, 2) for grep { !m/_p$/ } @{ $self->{number_columns} };
   };
 
   return $self->report_generator_list_objects(report => $self->{report}, objects => $self->{orders}, data_callback => $modify_data);
@@ -219,8 +244,20 @@ sub init_models {
           'globalproject.active' => 1,
           'globalproject.valid'  => 1,
         ]],
+      # keine WR
+      # oder aber (WR aktiv und (kein enddatum oder enddatum noch nicht überschritten))
+      or => [
+        'periodic_invoices_config.id' => undef,
+        # and => [
+          'periodic_invoices_config.active' => 1,
+        #   or => [
+        #     'periodic_invoices_config.end_date' => undef,
+        #     'periodic_invoices_config.end_date' => { le => DateTime->today_local },
+        # ]
+        # ]
+      ],
     ],
-    with_objects => [ 'customer', 'globalproject', 'globalproject.project_type' ],
+    with_objects => [ 'customer', 'globalproject', 'globalproject.project_type', 'periodic_invoices_config' ],
   );
 }
 
@@ -238,7 +275,7 @@ sub link_to {
   }
   if ($object->isa('SL::DB::Customer')) {
     my $id     = $object->id;
-    return "ct.pl?action=$action&id=$id&db=customer";
+    return "controller.pl?action=CustomerVendor/$action&id=$id";
   }
   if ($object->isa('SL::DB::Project')) {
     my $id     = $object->id;