Wiederkehrende Rechnungen: Auftragswerts-Periodizität setzen können
[kivitendo-erp.git] / SL / Controller / FinancialOverview.pm
1 package SL::Controller::FinancialOverview;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use List::MoreUtils qw(none);
7
8 use SL::DB::Employee;
9 use SL::DB::Invoice;
10 use SL::DB::Order;
11 use SL::DB::PeriodicInvoicesConfig;
12 use SL::DB::PurchaseInvoice;
13 use SL::Controller::Helper::ReportGenerator;
14 use SL::Locale::String;
15
16 use Rose::Object::MakeMethods::Generic (
17   scalar                  => [ qw(report number_columns year current_year objects subtotals_per_quarter salesman_id) ],
18   'scalar --get_set_init' => [ qw(employees types data) ],
19 );
20
21 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
22
23 sub action_list {
24   my ($self) = @_;
25
26   $self->$_($::form->{$_}) for qw(subtotals_per_quarter salesman_id);
27
28   $self->get_objects;
29   $self->calculate_one_time_data;
30   $self->calculate_periodic_invoices;
31   $self->prepare_report;
32   $self->list_data;
33 }
34
35 # private functions
36
37 sub prepare_report {
38   my ($self)      = @_;
39
40   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
41
42   my @columns = qw(year quarter month sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices);
43
44   $self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
45
46   my %column_defs          = (
47     month                  => { text => t8('Month')                  },
48     year                   => { text => t8('Year')                   },
49     quarter                => { text => t8('Quarter')                },
50     sales_quotations       => { text => t8('Sales Quotations')       },
51     sales_orders           => { text => t8('Sales Orders')           },
52     sales_invoices         => { text => t8('Invoices')               },
53     requests_for_quotation => { text => t8('Requests for Quotation') },
54     purchase_orders        => { text => t8('Purchase Orders')        },
55     purchase_invoices      => { text => t8('Purchase Invoices')      },
56   );
57
58   map { $column_defs{$_}->{align} = 'right' } @columns;
59
60   $self->report->set_options(
61     std_column_visibility => 1,
62     controller_class      => 'FinancialOverview',
63     output_format         => 'HTML',
64     raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->current_year - 10)..($self->current_year + 5)) ]),
65     title                 => t8('Financial overview for #1', $self->year),
66     allow_pdf_export      => 1,
67     allow_csv_export      => 1,
68   );
69   $self->report->set_columns(%column_defs);
70   $self->report->set_column_order(@columns);
71   $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
72   $self->report->set_options_from_form;
73 }
74
75 sub get_objects {
76   my ($self) = @_;
77
78   $self->current_year(DateTime->today->year);
79   $self->year($::form->{year} || DateTime->today->year);
80
81   my $start       = DateTime->new(year => $self->year, month => 1, day => 1);
82   my $end         = DateTime->new(year => $self->year, month => 12, day => 31);
83
84   my @f_date      = (transdate => { ge => $start }, transdate => { le => $end });
85   my @f_salesman  = $self->salesman_id ? (salesman_id => $self->salesman_id) : ();
86
87   $self->objects({
88     sales_quotations       => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('sales_quotation')   ]]),
89     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) ]),
90     requests_for_quotation => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('request_quotation') ]]),
91     purchase_orders        => SL::DB::Manager::Order->get_all(          where => [ and => [ @f_date, @f_salesman, SL::DB::Manager::Order->type_filter('purchase_order')    ]]),
92     sales_invoices         => SL::DB::Manager::Invoice->get_all(        where => [ and => [ @f_date, @f_salesman, ]]),
93     purchase_invoices      => SL::DB::Manager::PurchaseInvoice->get_all(where => [ and =>  \@f_date ]),
94     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) ]),
95   });
96
97   $self->objects->{sales_orders} = [ grep { !$_->periodic_invoices_config || !$_->periodic_invoices_config->active } @{ $self->objects->{sales_orders} } ];
98 }
99
100 sub init_types { [ qw(sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices) ] }
101
102 sub init_data {
103   my ($self) = @_;
104
105   my %data  = (
106     year    => [ ($self->year) x 12                   ],
107     month   => [ (1..12)                              ],
108     quarter => [ (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4) ],
109     map {
110       $_ => {
111         months   => [ (0) x 12 ],
112         quarters => [ (0) x  4 ],
113         year     => 0,
114       }
115     } @{ $self->types },
116   );
117
118   return \%data;
119 }
120
121 sub calculate_one_time_data {
122   my ($self) = @_;
123
124   foreach my $type (@{ $self->types }) {
125     foreach my $object (@{ $self->objects->{ $type } }) {
126       my $month                              = $object->transdate->month - 1;
127       my $tdata                              = $self->data->{$type};
128
129       $tdata->{months}->[$month]            += $object->netamount;
130       $tdata->{quarters}->[int($month / 3)] += $object->netamount;
131       $tdata->{year}                        += $object->netamount;
132     }
133   }
134 }
135
136 sub calculate_periodic_invoices {
137   my ($self)     = @_;
138
139   my $start_date = DateTime->new(year => $self->year, month =>  1, day =>  1, time_zone => $::locale->get_local_time_zone);
140   my $end_date   = DateTime->new(year => $self->year, month => 12, day => 31, time_zone => $::locale->get_local_time_zone);
141
142   $self->calculate_one_periodic_invoice(config => $_, start_date => $start_date, end_date => $end_date) for @{ $self->objects->{periodic_invoices_cfg} };
143 }
144
145 sub calculate_one_periodic_invoice {
146   my ($self, %params) = @_;
147
148   return if $params{config}->start_date > $params{end_date};
149
150   my $first_date = $params{config}->start_date->clone->set_year($self->year);
151   my $net        = $params{config}->order->netamount * (12 / $params{config}->get_billing_period_length);
152   my $sord       = $self->data->{sales_orders};
153
154   $sord->{months  }->[ $first_date->month   - 1 ] += $net;
155   $sord->{quarters}->[ $first_date->quarter - 1 ] += $net;
156   $sord->{year}                                   += $net;
157 }
158
159 sub list_data {
160   my ($self)           = @_;
161
162   my @visible_columns  = $self->report->get_visible_columns;
163   my @type_columns     = @{ $self->types };
164   my @non_type_columns = grep { my $c = $_; none { $c eq $_ } @type_columns } @visible_columns;
165
166   for my $month (1..12) {
167     my %data  = (
168       map({ ($_ => { data => $self->data->{$_}->[$month - 1]                                                    }) } @non_type_columns),
169       map({ ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{months}->[$month - 1], 2) }) } @type_columns    ),
170     );
171
172     $self->report->add_data(\%data);
173
174     if ($self->subtotals_per_quarter && (($month % 3) == 0)) {
175       my %subtotal =  (
176         year       => { data => $self->year },
177         month      => { data => $::locale->text('Total') },
178         map { ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{quarters}->[int(($month - 1) / 3)], 2) }) } @type_columns,
179       );
180
181       $subtotal{$_}->{class} = 'listsubtotal' for @visible_columns;
182
183       $self->report->add_data(\%subtotal);
184     }
185   }
186
187   my %data  =  (
188     year    => { data => $self->year },
189     quarter => { data => $::locale->text('Total') },
190     map { ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{year}, 2) }) } @type_columns,
191   );
192
193   $data{$_}->{class} = 'listtotal' for @visible_columns;
194
195   $self->report->add_data(\%data);
196
197   return $self->report->generate_with_headers;
198 }
199
200 sub init_employees { SL::DB::Manager::Employee->get_all_sorted }
201
202 1;