1 package SL::Controller::FinancialOverview;
4 use parent qw(SL::Controller::Base);
6 use List::MoreUtils qw(none);
10 use SL::DB::PurchaseInvoice;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::Locale::String;
14 use Rose::Object::MakeMethods::Generic (
15 scalar => [ qw(report number_columns year current_year types objects data subtotals_per_quarter) ],
18 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
23 $self->subtotals_per_quarter($::form->{subtotals_per_quarter});
26 $self->calculate_data;
27 $self->prepare_report;
36 $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
38 my @columns = qw(year quarter month sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices);
40 $self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
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') },
54 map { $column_defs{$_}->{align} = 'right' } @columns;
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,
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;
74 $self->current_year(DateTime->today->year);
75 $self->year($::form->{year} || DateTime->today->year);
77 my $start = DateTime->new(year => $self->year, month => 1, day => 1);
78 my $end = DateTime->new(year => $self->year, month => 12, day => 31);
80 my @date_filter = (and => [ transdate => { ge => $start }, transdate => { le => $end } ]);
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),
95 $self->types([ qw(sales_quotations sales_orders sales_invoices requests_for_quotation purchase_orders purchase_invoices) ]);
98 year => [ ($self->year) x 12 ],
100 quarter => [ (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4) ],
103 months => [ (0) x 12 ],
104 quarters => [ (0) x 4 ],
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};
115 $tdata->{months}->[$month] += $object->netamount;
116 $tdata->{quarters}->[int($month / 3)] += $object->netamount;
117 $tdata->{year} += $object->netamount;
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;
131 for my $month (1..12) {
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 ),
137 $self->report->add_data(\%data);
139 if ($self->subtotals_per_quarter && (($month % 3) == 0)) {
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,
146 $subtotal{$_}->{class} = 'listsubtotal' for @visible_columns;
148 $self->report->add_data(\%subtotal);
153 year => { data => $self->year },
154 quarter => { data => $::locale->text('Total') },
155 map { ($_ => { data => $::form->format_amount(\%::myconfig, $self->data->{$_}->{year}, 2) }) } @type_columns,
158 $data{$_}->{class} = 'listtotal' for @visible_columns;
160 $self->report->add_data(\%data);
162 return $self->report->generate_with_headers;