]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/FinancialOverview.pm
JavaScript-Test-Framework auf Basis von QUnit
[mfinanz.git] / SL / Controller / FinancialOverview.pm
index fec091572a9851e0b172acff0044670b033dc896..cd153d23f42eefcca85ca6d62c59ccf61d85a022 100644 (file)
@@ -8,13 +8,14 @@ use List::MoreUtils qw(none);
 use SL::DB::Employee;
 use SL::DB::Invoice;
 use SL::DB::Order;
 use SL::DB::Employee;
 use SL::DB::Invoice;
 use SL::DB::Order;
+use SL::DB::PeriodicInvoicesConfig;
 use SL::DB::PurchaseInvoice;
 use SL::Controller::Helper::ReportGenerator;
 use SL::Locale::String;
 
 use Rose::Object::MakeMethods::Generic (
 use SL::DB::PurchaseInvoice;
 use SL::Controller::Helper::ReportGenerator;
 use SL::Locale::String;
 
 use Rose::Object::MakeMethods::Generic (
-  scalar                  => [ qw(report number_columns year current_year types objects data subtotals_per_quarter salesman_id) ],
-  'scalar --get_set_init' => [ qw(employees) ],
+  scalar                  => [ qw(report number_columns year current_year objects subtotals_per_quarter salesman_id) ],
+  'scalar --get_set_init' => [ qw(employees types data) ],
 );
 
 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
 );
 
 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
@@ -25,7 +26,8 @@ sub action_list {
   $self->$_($::form->{$_}) for qw(subtotals_per_quarter salesman_id);
 
   $self->get_objects;
   $self->$_($::form->{$_}) for qw(subtotals_per_quarter salesman_id);
 
   $self->get_objects;
-  $self->calculate_data;
+  $self->calculate_one_time_data;
+  $self->calculate_periodic_invoices;
   $self->prepare_report;
   $self->list_data;
 }
   $self->prepare_report;
   $self->list_data;
 }
@@ -59,7 +61,7 @@ sub prepare_report {
     std_column_visibility => 1,
     controller_class      => 'FinancialOverview',
     output_format         => 'HTML',
     std_column_visibility => 1,
     controller_class      => 'FinancialOverview',
     output_format         => 'HTML',
-    raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(2000..$self->current_year) ]),
+    raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->current_year - 10)..($self->current_year + 5)) ]),
     title                 => t8('Financial overview for #1', $self->year),
     allow_pdf_export      => 1,
     allow_csv_export      => 1,
     title                 => t8('Financial overview for #1', $self->year),
     allow_pdf_export      => 1,
     allow_csv_export      => 1,
@@ -84,18 +86,21 @@ sub get_objects {
 
   $self->objects({
     sales_quotations       => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('sales_quotation')   ]]),
 
   $self->objects({
     sales_quotations       => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('sales_quotation')   ]]),
-    sales_orders           => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('sales_order')       ]]),
+    sales_orders           => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('sales_order')       ]], with_objects => [ qw(periodic_invoices_config) ]),
     requests_for_quotation => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('request_quotation') ]]),
     purchase_orders        => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('purchase_order')    ]]),
     sales_invoices         => SL::DB::Manager::Invoice->get_all(        where => [ and => [ @f_date, @f_salesman, ]]),
     purchase_invoices      => SL::DB::Manager::PurchaseInvoice->get_all(where => [ and =>  \@f_date ]),
     requests_for_quotation => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('request_quotation') ]]),
     purchase_orders        => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('purchase_order')    ]]),
     sales_invoices         => SL::DB::Manager::Invoice->get_all(        where => [ and => [ @f_date, @f_salesman, ]]),
     purchase_invoices      => SL::DB::Manager::PurchaseInvoice->get_all(where => [ and =>  \@f_date ]),
+    periodic_invoices_cfg  => SL::DB::Manager::PeriodicInvoicesConfig->get_all(where => [ active => 1, $self->salesman_id ? ('order.salesman_id' => $self->salesman_id) : () ], with_objects => [ qw(order) ]),
   });
   });
+
+  $self->objects->{sales_orders} = [ grep { !$_->periodic_invoices_config || !$_->periodic_invoices_config->active } @{ $self->objects->{sales_orders} } ];
 }
 
 }
 
-sub calculate_data {
-  my ($self) = @_;
+sub init_types { [ qw(sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices) ] }
 
 
-  $self->types([ qw(sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices) ]);
+sub init_data {
+  my ($self) = @_;
 
   my %data  = (
     year    => [ ($self->year) x 12                   ],
 
   my %data  = (
     year    => [ ($self->year) x 12                   ],
@@ -110,18 +115,45 @@ sub calculate_data {
     } @{ $self->types },
   );
 
     } @{ $self->types },
   );
 
-  foreach my $type (keys %{ $self->objects }) {
+  return \%data;
+}
+
+sub calculate_one_time_data {
+  my ($self) = @_;
+
+  foreach my $type (@{ $self->types }) {
     foreach my $object (@{ $self->objects->{ $type } }) {
       my $month                              = $object->transdate->month - 1;
     foreach my $object (@{ $self->objects->{ $type } }) {
       my $month                              = $object->transdate->month - 1;
-      my $tdata                              = $data{$type};
+      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;
       $tdata->{quarters}->[int($month / 3)] += $object->netamount;
       $tdata->{year}                        += $object->netamount;
     }
   }
+}
+
+sub calculate_periodic_invoices {
+  my ($self)     = @_;
+
+  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} };
+}
+
+sub calculate_one_periodic_invoice {
+  my ($self, %params) = @_;
+
+  return if $params{config}->start_date > $params{end_date};
+
+  my $first_date = $params{config}->start_date->clone->set_year($self->year);
+  my $net        = $params{config}->order->netamount * (12 / $params{config}->get_period_length);
+  my $sord       = $self->data->{sales_orders};
 
 
-  $self->data(\%data);
+  $sord->{months  }->[ $first_date->month   - 1 ] += $net;
+  $sord->{quarters}->[ $first_date->quarter - 1 ] += $net;
+  $sord->{year}                                   += $net;
 }
 
 sub list_data {
 }
 
 sub list_data {