]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/ChartOfAccounts.pm
kivitendo 3.9.2-0.2
[mfinanz.git] / SL / Controller / ChartOfAccounts.pm
1 package SL::Controller::ChartOfAccounts;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use POSIX qw(strftime);
7
8 use SL::CA;
9
10 use SL::ReportGenerator;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::Locale::String;
13
14 use Rose::Object::MakeMethods::Generic (
15   scalar                  => [ qw(report) ],
16 );
17
18 __PACKAGE__->run_before(sub { $::auth->assert('report'); });
19
20 sub action_list {
21   my ($self) = @_;
22
23   if ( $::instance_conf->get_accounting_method eq 'cash' ) {
24     $::form->{method} = "cash";
25   }
26
27   $self->prepare_report;
28   $self->set_report_data;
29   $self->report->generate_with_headers;
30 }
31
32 # private functions
33
34 sub prepare_report {
35   my ($self) = @_;
36
37   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
38
39   $self->report->{title} = t8 ('Chart of Accounts');
40   my @columns     = qw(accno description debit credit);
41   my %column_defs = (
42     accno       => { text => t8('Account') },
43     description => { text => t8('Description') },
44     debit       => { text => t8('Debit') },
45     credit      => { text => t8('Credit') },
46   );
47
48   $self->report->set_options(
49     std_column_visibility => 1,
50     controller_class      => 'ChartOfAccounts',
51     output_format         => 'HTML',
52     title                 => t8('Chart of Accounts'),
53     allow_pdf_export      => 1,
54     allow_csv_export      => 1,
55     attachment_basename   => t8('chart_of_accounts') . strftime('_%Y%m%d', localtime time),
56   );
57   $self->report->set_columns(%column_defs);
58   $self->report->set_column_order(@columns);
59
60   $self->report->set_export_options(qw(list));
61   $self->report->set_options_from_form;
62   $self->report->set_sort_indicator($::form->{sort}, 1);
63 }
64
65 sub set_report_data {
66   my ($self) = @_;
67
68   my $debit_sum = 0.;
69   my $credit_sum = 0.;
70
71   # i tried to use the get_balance function from SL::DB::Manager::Chart here,
72   # but the results i got were different (numbers and defined balance/amount),
73   # the database queries in CA are more sophisticated, therefore i'm still using these for now,
74   # also performance wise they seem faster
75   CA->all_accounts(\%::myconfig, \%$::form);
76
77   my $formatted_zero = $::form->format_amount(\%::myconfig, 0., 2);
78
79   for my $chart (@{ $::form->{CA} }) {
80     my $balance = $chart->{amount};
81
82     my $link = "controller.pl?action=ListTransactions%2freport_settings&accno=$chart->{accno}&link=1";
83     if (defined($balance)) {
84       my %data = (
85         accno       => { data => $chart->{accno}, link => $link },
86         description => { data => $chart->{description} },
87         debit       => { data => $balance < 0 ? $::form->format_amount(\%::myconfig, $balance * -1., 2) : ''},
88         credit      => { data => $balance >= 0 ? $::form->format_amount(\%::myconfig, $balance, 2) : ''},
89       );
90       $data{$_}->{align} = 'right' for qw(debit credit);
91       map { $data{$_}->{class} = 'listheading' } keys %data if ($chart->{charttype} eq "H") ;
92       $self->report->add_data(\%data);
93
94       if ($balance < 0) {
95         $debit_sum += $balance;
96       } else {
97         $credit_sum += $balance;
98       }
99     }
100   }
101   my %data_total = (
102     accno       => { data => t8('Total') },
103     description => { data => '' },
104     debit       => { data => $::form->format_amount(\%::myconfig, $debit_sum * -1., 2)},
105     credit      => { data => $::form->format_amount(\%::myconfig, $credit_sum, 2)},
106   );
107   $data_total{$_}->{align} = 'right' for qw(debit credit);
108   $data_total{$_}->{class} = 'listtotal' for keys %data_total;
109
110   $self->report->add_data(\%data_total);
111 }
112
113 1;
114
115 __END__
116
117 =encoding utf-8
118
119 =head1 NAME
120
121 SL::Controller::ChartOfAccounts - Controller for the chart of accounts report
122
123 =head1 SYNOPSIS
124
125 New controller for Reports -> Chart of Accounts.
126
127 This replaces the old bin/mozilla/ca.pl chart_of_accounts sub.
128
129 The rest of the functions from ca.pl are separated into the new ListTransactions.pm
130 controller.
131
132 =head1 DESCRIPTION
133
134 Displays a list of all accounts with their balance.
135
136 Clicking on an account number will open the form for Reports -> List Transactions, with
137 the account number preselected.
138
139 Export to PDF, CSV and Chart is possible.
140
141 =head1 CAVEATS / TODO
142
143 Database queries are still from SL::CA.
144
145 I tried to use the get_balance function from SL::DB::Manager::Chart here,
146 but the results i got were different (numbers and defined balance/amount).
147 The database queries in CA are more sophisticated, therefore i'm still using these for now.
148 Also performance wise they seem faster.
149
150 =head1 BUGS
151
152 None yet.
153
154 =head1 AUTHOR
155
156 Cem Aydin E<lt>cem.aydin@revamp-it.chE<gt>
157
158 =cut