Refactoring
[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::Invoice;
9 use SL::DB::Order;
10 use SL::DB::PurchaseInvoice;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::Locale::String;
13
14 use Rose::Object::MakeMethods::Generic (
15   scalar => [ qw(report number_columns year current_year types objects data subtotals_per_quarter) ],
16 );
17
18 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
19
20 sub action_list {
21   my ($self) = @_;
22
23   $self->subtotals_per_quarter($::form->{subtotals_per_quarter});
24
25   $self->get_objects;
26   $self->calculate_data;
27   $self->prepare_report;
28   $self->list_data;
29 }
30
31 # private functions
32
33 sub prepare_report {
34   my ($self)      = @_;
35
36   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
37
38   my @columns = qw(year quarter month sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices);
39
40   $self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
41
42   my %column_defs          = (
43     month                  => { text => t8('Month')                  },
44     year                   => { text => t8('Year')                   },
45     quarter                => { text => t8('Quarter')                },
46     sales_quotations       => { text => t8('Sales Quotations')       },
47     sales_orders           => { text => t8('Sales Orders')           },
48     sales_invoices         => { text => t8('Invoices')               },
49     requests_for_quotation => { text => t8('Requests for Quotation') },
50     purchase_orders        => { text => t8('Purchase Orders')        },
51     purchase_invoices      => { text => t8('Purchase Invoices')      },
52   );
53
54   map { $column_defs{$_}->{align} = 'right' } @columns;
55
56   $self->report->set_options(
57     std_column_visibility => 1,
58     controller_class      => 'FinancialOverview',
59     output_format         => 'HTML',
60     raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(2000..$self->current_year) ]),
61     title                 => t8('Financial overview for #1', $self->year),
62     allow_pdf_export      => 1,
63     allow_csv_export      => 1,
64   );
65   $self->report->set_columns(%column_defs);
66   $self->report->set_column_order(@columns);
67   $self->report->set_export_options(qw(list year subtotals_per_quarter));
68   $self->report->set_options_from_form;
69 }
70
71 sub get_objects {
72   my ($self) = @_;
73
74   $self->current_year(DateTime->today->year);
75   $self->year($::form->{year} || DateTime->today->year);
76
77   my $start       = DateTime->new(year => $self->year, month => 1, day => 1);
78   my $end         = DateTime->new(year => $self->year, month => 12, day => 31);
79
80   my @date_filter = (and => [ transdate => { ge => $start }, transdate => { le => $end } ]);
81
82   $self->objects({
83     sales_quotations       => SL::DB::Manager::Order->get_all(          where => [ and => [ @date_filter, SL::DB::Manager::Order->type_filter('sales_quotation')   ]]),
84     sales_orders           => SL::DB::Manager::Order->get_all(          where => [ and => [ @date_filter, SL::DB::Manager::Order->type_filter('sales_order')       ]]),
85     requests_for_quotation => SL::DB::Manager::Order->get_all(          where => [ and => [ @date_filter, SL::DB::Manager::Order->type_filter('request_quotation') ]]),
86     purchase_orders        => SL::DB::Manager::Order->get_all(          where => [ and => [ @date_filter, SL::DB::Manager::Order->type_filter('purchase_order')    ]]),
87     sales_invoices         => SL::DB::Manager::Invoice->get_all(        where => \@date_filter),
88     purchase_invoices      => SL::DB::Manager::PurchaseInvoice->get_all(where => \@date_filter),
89   });
90 }
91
92 sub calculate_data {
93   my ($self) = @_;
94
95   $self->types([ qw(sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices) ]);
96
97   my %data  = (
98     year    => [ ($self->year) x 12                   ],
99     month   => [ (1..12)                              ],
100     quarter => [ (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4) ],
101     map {
102       $_ => {
103         months   => [ (0) x 12 ],
104         quarters => [ (0) x  4 ],
105         year     => 0,
106       }
107     } @{ $self->types },
108   );
109
110   foreach my $type (keys %{ $self->objects }) {
111     foreach my $object (@{ $self->objects->{ $type } }) {
112       my $month                              = $object->transdate->month - 1;
113       my $tdata                              = $data{$type};
114
115       $tdata->{months}->[$month]            += $object->netamount;
116       $tdata->{quarters}->[int($month / 3)] += $object->netamount;
117       $tdata->{year}                        += $object->netamount;
118     }
119   }
120
121   $self->data(\%data);
122 }
123
124 sub list_data {
125   my ($self)           = @_;
126
127   my @visible_columns  = $self->report->get_visible_columns;
128   my @type_columns     = @{ $self->types };
129   my @non_type_columns = grep { my $c = $_; none { $c eq $_ } @type_columns } @visible_columns;
130
131   for my $month (1..12) {
132     my %data  = (
133       map({ ($_ => { data => $self->data->{$_}->[$month - 1]                                                    }) } @non_type_columns),
134       map({ ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{months}->[$month - 1], 2) }) } @type_columns    ),
135     );
136
137     $self->report->add_data(\%data);
138
139     if ($self->subtotals_per_quarter && (($month % 3) == 0)) {
140       my %subtotal =  (
141         year       => { data => $self->year },
142         month      => { data => $::locale->text('Total') },
143         map { ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{quarters}->[int(($month - 1) / 3)], 2) }) } @type_columns,
144       );
145
146       $subtotal{$_}->{class} = 'listsubtotal' for @visible_columns;
147
148       $self->report->add_data(\%subtotal);
149     }
150   }
151
152   my %data  =  (
153     year    => { data => $self->year },
154     quarter => { data => $::locale->text('Total') },
155     map { ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{year}, 2) }) } @type_columns,
156   );
157
158   $data{$_}->{class} = 'listtotal' for @visible_columns;
159
160   $self->report->add_data(\%data);
161
162   return $self->report->generate_with_headers;
163 }
164
165 1;