]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/FinancialControllingReport.pm
Finanzcontrolling: Auftragswertperiodizität berücksichtigen
[mfinanz.git] / SL / Controller / FinancialControllingReport.pm
index 50760a5f7ad7c63b159a96b909c3286157e0a9a5..ea11e863ec77f32d4ac6b5daa1e3b39c65e4244e 100644 (file)
@@ -3,7 +3,7 @@ package SL::Controller::FinancialControllingReport;
 use strict;
 use parent qw(SL::Controller::Base);
 
 use strict;
 use parent qw(SL::Controller::Base);
 
-use List::Util qw(sum);
+use List::Util qw(max min sum);
 
 use SL::DB::Order;
 use SL::DB::ProjectType;
 
 use SL::DB::Order;
 use SL::DB::ProjectType;
@@ -121,8 +121,7 @@ sub calculate_data {
     $order->{billable_amount}   = $order->{delivered_amount} - $order->{billed_amount};
 
     if ($order->periodic_invoices_config) {
     $order->{billable_amount}   = $order->{delivered_amount} - $order->{billed_amount};
 
     if ($order->periodic_invoices_config) {
-      my @dates = $order->periodic_invoices_config->calculate_invoice_dates(past_dates => 1, end_date => DateTime->today_local);
-      $order->{net_amount} = $order->netamount * scalar(@dates);
+      $order->{net_amount} = $self->calculate_periodic_invoices_order_netamount($order);
 
     } else {
       $order->{net_amount} = $order->netamount;
 
     } else {
       $order->{net_amount} = $order->netamount;
@@ -134,6 +133,29 @@ sub calculate_data {
   }
 }
 
   }
 }
 
+sub calculate_periodic_invoices_order_netamount {
+  my ($self, $order) = @_;
+
+  my $year       = DateTime->today_local->year;
+  my $year_start = DateTime->new_local(day =>  1, month =>  1, year => $year);
+  my $year_end   = DateTime->new_local(day => 31, month => 12, year => $year);
+
+  my $cfg        = $order->periodic_invoices_config;
+  my $period_len = $cfg->get_billing_period_length;
+  my $num_months = 0;
+  my $cur_date   = $cfg->start_date->clone;
+  my $end_date   = $cfg->terminated ? $cfg->end_date : undef;
+  $end_date    //= $year_end;
+  $end_date      = min $end_date, $year_end;
+
+  while ($cur_date <= $end_date) {
+    $num_months += $period_len if $cur_date >= $year_start;
+    $cur_date->add(months => $period_len);
+  }
+
+  return $num_months * $order->netamount / $order->periodic_invoices_config->get_order_value_period_length;
+}
+
 sub sum_items {
   my ($self, %params) = @_;
 
 sub sum_items {
   my ($self, %params) = @_;