]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/FinancialOverview.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / Controller / FinancialOverview.pm
index c0c7d183ce806aad5e35508a45707c3f341e9f6e..edfe33da4eefcb7424c8859f43ffa3202ecb74f2 100644 (file)
@@ -41,7 +41,7 @@ sub prepare_report {
   my ($self)      = @_;
 
   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
-
+  $self->report->{title} =t8('Financial Overview');
   my @columns = (qw(year quarter month), @{ $self->types });
 
   $self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
@@ -141,9 +141,9 @@ sub calculate_one_time_data {
       my $month                              = $object->transdate->month - 1;
       my $tdata                              = $self->data->{$type};
 
-      $tdata->{months}->[$month]            += $object->netamount;
-      $tdata->{quarters}->[int($month / 3)] += $object->netamount;
-      $tdata->{year}                        += $object->netamount;
+      $tdata->{months}->[$month]            += $object->netamount_base_currency;
+      $tdata->{quarters}->[int($month / 3)] += $object->netamount_base_currency;
+      $tdata->{year}                        += $object->netamount_base_currency;
     }
   }
 }
@@ -151,23 +151,52 @@ sub calculate_one_time_data {
 sub calculate_periodic_invoices {
   my ($self)     = @_;
 
+  my %billed_once_item_ids;
   my $start_date = DateTime->new(year => $self->year, month =>  1, day =>  1, time_zone => $::locale->get_local_time_zone);
   my $end_date   = DateTime->new(year => $self->year, month => 12, day => 31, time_zone => $::locale->get_local_time_zone);
 
-  $self->calculate_one_periodic_invoice(config => $_, start_date => $start_date, end_date => $end_date) for @{ $self->objects->{periodic_invoices_cfg} };
+  foreach my $config (@{ $self->objects->{periodic_invoices_cfg} }) {
+    $self->calculate_one_periodic_invoice(
+      config               => $config,
+      start_date           => $start_date,
+      end_date             => $end_date,
+      billed_once_item_ids => \%billed_once_item_ids,
+    );
+  }
 }
 
 sub calculate_one_periodic_invoice {
   my ($self, %params) = @_;
 
   # Calculate sales order advance
-  my $net  = $params{config}->order->netamount * $params{config}->get_billing_period_length / $params{config}->get_order_value_period_length;
   my $sord = $self->data->{sales_orders};
 
+  my ($net, $net_once) = (0, 0);
+
+  foreach my $item (@{ $params{config}->order->orderitems }) {
+    next if $item->recurring_billing_mode eq 'never';
+
+    my $item_net = $item->qty * (1 - $item->discount) * $item->sellprice;
+
+    if ($item->recurring_billing_mode eq 'once') {
+      next if $item->recurring_billing_invoice_id || $params{billed_once_invoice_id}->{$item->id};
+
+      $params{billed_once_invoice_id}->{$item->id} = 1;
+      $net_once                                   += $item_net;
+
+    } else {
+      $net += $item_net;
+    }
+  }
+
+  $net = $net * $params{config}->get_billing_period_length / $params{config}->get_order_value_period_length;
+
   foreach my $date ($params{config}->calculate_invoice_dates(start_date => $params{start_date}, end_date => $params{end_date}, past_dates => 1)) {
-    $sord->{months  }->[ $date->month   - 1 ] += $net;
-    $sord->{quarters}->[ $date->quarter - 1 ] += $net;
-    $sord->{year}                             += $net;
+    $sord->{months  }->[ $date->month   - 1 ] += $net + $net_once;
+    $sord->{quarters}->[ $date->quarter - 1 ] += $net + $net_once;
+    $sord->{year}                             += $net + $net_once;
+
+    $net_once = 0;
   }
 
   # Calculate total sales order value