epic-s6g
[kivitendo-erp.git] / SL / Controller / Mebil.pm
1 package SL::Controller::Mebil;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 #use SL::Controller::Helper::ReportGenerator;
8 use SL::ReportGenerator;
9 use SL::DBUtils;
10
11 use SL::Locale::String;  # t8
12
13 #use Data::Dumper;
14 #use SL::ClientJS;
15
16 use SL::mebil::Mapping;
17
18 use Rose::Object::MakeMethods::Generic (
19   scalar                  => [ qw(report number_columns year current_year objects subtotals_per_quarter salesman_id) ],
20   'scalar --get_set_init' => [ qw(employees types data) ],
21 );
22
23 sub action_map {
24         $::lxdebug->enter_sub;
25         $::lxdebug->message(5, 'controller=mebil/action=map');
26         my ($self) = @_;
27         
28         $::form->header(no_layout => 1);
29         
30         print "<h1>Mebil running</h1>";
31
32         my $sql = "SELECT  chart_id,xbrl_tag from mebil_mapping";
33         my $result = SL::DBUtils::do_query($::form, $::form->get_standard_dbh, $sql);
34         $::lxdebug->message(5, "result= $result");
35         print "$result<br>\n";
36         
37         my @r = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
38         print ref($r[1])||"SCALAR";
39         print "<br>";
40         my $fst = $r[1];
41         while (my($k,$v) = each(%$fst)) {
42                 print $k, " : ", $v, "</br>\n";
43         }
44         print "<br>";
45         print scalar @r;
46         print "<br>";
47         print @r;
48         print "<p>Mebil ready</p>";
49         $::lxdebug->leave_sub;
50 }
51
52 sub action_showmap {
53         $::lxdebug->enter_sub;
54         my ($self) = @_;
55         
56         # call model -> diese Zeile ist fraglich, war ein Konflikt
57         #$self->{data} = DB::MebilMapping::getMappings($::form->get_standard_dbh);
58         
59         $::form->{title} = $::locale->text('Mebil Map');
60
61         my $mapping = new SL::mebil::Mapping($::form, $::form->get_standard_dbh);
62         $self->{data} = $mapping->get_mapping();
63
64         $self->prepare_report;
65         $self->list_data;
66
67         $::lxdebug->leave_sub;
68 }
69
70 sub action_calcmap {
71         $::lxdebug->enter_sub;
72         my ($self) = @_;
73         
74         $self->year($::form->{year} || DateTime->today->year - 1);
75         
76         $::form->{title} = $::locale->text('Mebil Map');
77         
78         my $mapping = new SL::mebil::Mapping($::form, $::form->get_standard_dbh);
79         
80         (my $fromacc, my $toacc) = $mapping->calc_mapping($self->{year});
81         
82         $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
83
84         my @columns = (qw(name amount));
85
86         my %column_defs          = (
87         name                        => { text => 'Konto', align => 'left'   },
88         amount                      => { text => 'Betrag'   , align => 'right'   },
89         );
90
91   $self->report->set_options(
92     std_column_visibility => 1,
93     controller_class      => 'Mebil',
94     output_format         => 'HTML',
95         raw_top_info_text     => $self->render('mebil/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->year - 10)..($self->year + 5)) ]),
96     title                 => t8('mebil - Mapping: values for #1', $self->year),
97 #    allow_pdf_export      => 1,
98 #    allow_csv_export      => 1,
99   );
100   $self->report->set_columns(%column_defs);
101   $self->report->set_column_order(@columns);
102 #  $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
103         $self->report->set_options_from_form;
104         $self->add_data_sorted($fromacc);
105         $self->add_data_sorted($toacc);
106  
107         $::lxdebug->leave_sub;
108         return $self->report->generate_with_headers;
109 }
110 sub add_data_sorted {
111         my $self = shift;
112         my $data = shift; # hash reference
113         
114         foreach my $key (sort keys %$data) {
115                 my %data = (
116                     name                     => { data => $key },
117                 amount                   => { data => $::form->format_amount(\%::myconfig, $data->{$key}, 2) },
118                 );
119                 $self->report->add_data(\%data);
120                 
121         }
122 }
123 sub prepare_report {
124   my ($self)      = @_;
125
126   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
127
128   my @columns = (qw(fromacc typ toacc));
129
130   #$self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
131
132   my %column_defs          = (
133     fromacc                  => { text => 'Quelle', align => 'left'   },
134     typ                      => { text => 'Typ'   , align => 'right'   },
135     toacc                    => { text => 'Ziel'  , align => 'left'   },
136   );
137
138   #$column_defs{$_}->{align} = 'right' for @columns;
139
140   $self->report->set_options(
141     std_column_visibility => 1,
142     controller_class      => 'Mebil',
143     output_format         => 'HTML',
144  #   raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->current_year - 10)..($self->current_year + 5)) ]),
145     title                 => 'mebil - Mapping',
146 #    allow_pdf_export      => 1,
147 #    allow_csv_export      => 1,
148   );
149   $self->report->set_columns(%column_defs);
150   $self->report->set_column_order(@columns);
151 #  $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
152   $self->report->set_options_from_form;
153 }
154 sub list_data {
155         my ($self)           = @_;
156
157 #  my @visible_columns  = $self->report->get_visible_columns;
158 #  my @type_columns     = @{ $self->types };
159 #  my @non_type_columns = grep { my $c = $_; none { $c eq $_ } @type_columns } @visible_columns;
160
161         foreach my $mapping (@{ $self->{data} }) {
162                 my %data = (
163                     fromacc                  => { data => $mapping->{fromacc} },
164                 typ                      => { data => $mapping->{typ}     },
165                 toacc                    => { data => $mapping->{toacc}   },
166                 );
167                 $self->report->add_data(\%data);
168         }
169  
170         return $self->report->generate_with_headers;
171 }
172
173 1;