1 package SL::Controller::Mebil;
 
   5 use parent qw(SL::Controller::Base);
 
   7 #use SL::Controller::Helper::ReportGenerator;
 
   8 use SL::ReportGenerator;
 
  11 use SL::Locale::String;  # t8
 
  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) ],
 
  22         $::lxdebug->enter_sub;
 
  23         $::lxdebug->message(5, 'controller=mebil/action=map');
 
  26         $::form->header(no_layout => 1);
 
  28         print "<h1>Mebil running</h1>";
 
  30         my $sql = "SELECT  chart_id,xbrl_tag from mebil_mapping";
 
  31         my $result = SL::DBUtils::do_query($::form, $::form->get_standard_dbh, $sql);
 
  32         $::lxdebug->message(5, "result= $result");
 
  33         print "$result<br>\n";
 
  35         my @r = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
 
  36         print ref($r[1])||"SCALAR";
 
  39         while (my($k,$v) = each(%$fst)) {
 
  40                 print $k, " : ", $v, "</br>\n";
 
  46         print "<p>Mebil ready</p>";
 
  47         $::lxdebug->leave_sub;
 
  51         $::lxdebug->enter_sub;
 
  54         $::form->{title} = $::locale->text('Mebil Map');
 
  56         my $sql = "SELECT fromacc,typ,toacc from mebil_mapping order by ordering";
 
  57         $self->{data} = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
 
  59         $self->prepare_report;
 
  62         $::lxdebug->leave_sub;
 
  75         $::lxdebug->message(5, "part=$part");
 
  80         $::lxdebug->enter_sub;
 
  84         $::lxdebug->message(5, "rule=$rule");
 
  86         # a rule consists of key=value pairs seperated by colon. Possible keys are:
 
  87         # - ACC: account number
 
  88         # - VALUES=positive|negative: only positive or negative values are selected
 
  89         #     negative implies INVERT=true
 
  90         # - INVERT=true: Result is multiplied by -1
 
  91         # - START=YEAR: start year. Absolute ('2020') or relative ('YEAR/PY')
 
  92         # - END=YEAR: end year. Default: $year
 
  99         my $end = "AND (ac.transdate <= '31.12.$year')";
 
 103         (my $part,$rule) = extract_rule($rule);
 
 104         $::lxdebug->message(5, "part=$part");
 
 106                 $part =~ /(.*)=(.*)/;
 
 110                 elsif ($1 eq "VALUES") {
 
 111                         if ($2 eq "positive") {
 
 112                                 $values = "AND (ac.amount > 0)";
 
 114                         elsif ($2 eq "negative") {
 
 115                                 $values = "AND (ac.amount < 0)";
 
 119                                 die "invalid rule part: $part";
 
 122                 elsif ($1 eq "INVERT") {
 
 125                 elsif ($1 eq "START") {
 
 126                         $start = "AND (ac.transdate >= '01.01.$2')";
 
 127                         $start =~ s/YEAR/$year/;
 
 130                 elsif ($1 eq "END") {
 
 131                         $end = "AND (ac.transdate <= '31.12.$2')";
 
 132                         $end =~ s/YEAR/$year/;
 
 136                         die "invalid rule part: $part";
 
 138                 ($part,$rule) = extract_rule($rule);
 
 141         $::lxdebug->leave_sub;
 
 142         return "SELECT SUM(ac.amount) $invert AS saldo
 
 144                                                 JOIN chart c ON (c.id = ac.chart_id) 
 
 145                                                 WHERE (c.accno = '$acc') $values $start $end";
 
 149         $::lxdebug->enter_sub;
 
 152         $self->year($::form->{year} || DateTime->today->year - 1);
 
 154         $::form->{title} = $::locale->text('Mebil Map');
 
 156         my $sql = "SELECT  fromacc,typ,toacc from mebil_mapping order by ordering";
 
 157         $self->{data} = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
 
 161         my $year = $self->year;
 
 162         foreach my $mapping (@{ $self->{data} }) {
 
 163                 if ($mapping->{typ} eq 'H') {
 
 165                         $sql = "SELECT SUM(ac.amount) AS saldo
 
 167                                                 JOIN chart c ON (c.id = ac.chart_id) 
 
 168                                                 WHERE (ac.transdate <= '31.12.$year') 
 
 169                                                 AND (c.accno = '$mapping->{fromacc}') ";
 
 170                         my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);            
 
 171                         $fromacc{$mapping->{fromacc}} = $result->[0]->{saldo};
 
 172                         $toacc{$mapping->{toacc}} += $result->[0]->{saldo};
 
 174                 elsif ($mapping->{typ} eq 'S') {
 
 176                         $sql = "SELECT SUM(ac.amount)* -1 AS saldo
 
 178                                                 JOIN chart c ON (c.id = ac.chart_id) 
 
 179                                                 WHERE (ac.transdate <= '31.12.$year') 
 
 180                                                 AND (c.accno = '$mapping->{fromacc}') ";
 
 181                         my @result = SL::DBUtils::selectfirst_array_query($::form, $::form->get_standard_dbh, $sql);            
 
 182                         $fromacc{$mapping->{fromacc}} = $result[0];
 
 183                         $toacc{$mapping->{toacc}} += $result[0];
 
 185 #               elsif ($mapping->{typ} eq 'V') {
 
 187 #                       $fromacc{$mapping->{fromacc}} = 300.;
 
 188 #                       $toacc{$mapping->{toacc}} = 300.;
 
 190 #               elsif ($mapping->{typ} eq 'A') {
 
 192 #                       $fromacc{$mapping->{fromacc}} = 400.;
 
 193 #                       $toacc{$mapping->{toacc}} = 400.;
 
 195                 elsif ($mapping->{typ} eq 'X') {
 
 196                         # add to other account
 
 197                         $toacc{$mapping->{toacc}} += $toacc{$mapping->{fromacc}};
 
 199                 elsif ($mapping->{typ} eq 'Y') {
 
 200                         # substract from other account
 
 201                         $toacc{$mapping->{toacc}} -= $toacc{$mapping->{fromacc}};
 
 203                 elsif ($mapping->{typ} eq 'R') {
 
 205                         my $sql = rule2sql($mapping->{fromacc}, $year);
 
 206                         $::lxdebug->message(5, "sql=$sql");
 
 207                         my @result = SL::DBUtils::selectfirst_array_query($::form, $::form->get_standard_dbh, $sql);            
 
 208                         $toacc{$mapping->{toacc}} += $result[0];
 
 210                 elsif ($mapping->{typ} eq 'C') {
 
 215                         die "Error: Invalid mapping type: $mapping->{typ}\n";
 
 219         $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
 
 221         my @columns = (qw(name amount));
 
 224         name                        => { text => 'Konto', align => 'left'   },
 
 225         amount                      => { text => 'Betrag'   , align => 'right'   },
 
 228   $self->report->set_options(
 
 229     std_column_visibility => 1,
 
 230     controller_class      => 'Mebil',
 
 231     output_format         => 'HTML',
 
 232         raw_top_info_text     => $self->render('mebil/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->year - 10)..($self->year + 5)) ]),
 
 233     title                 => t8('mebil - Mapping: values for #1', $self->year),
 
 234 #    allow_pdf_export      => 1,
 
 235 #    allow_csv_export      => 1,
 
 237   $self->report->set_columns(%column_defs);
 
 238   $self->report->set_column_order(@columns);
 
 239 #  $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
 
 240         $self->report->set_options_from_form;
 
 241         $self->add_data_sorted(\%fromacc);
 
 242         $self->add_data_sorted(\%toacc);
 
 244         $::lxdebug->leave_sub;
 
 245         return $self->report->generate_with_headers;
 
 247 sub add_data_sorted {
 
 249         my $data = shift; # hash reference
 
 251         foreach my $key (sort keys %$data) {
 
 253                     name                     => { data => $key },
 
 254                 amount                   => { data => $::form->format_amount(\%::myconfig, $data->{$key}, 2) },
 
 256                 $self->report->add_data(\%data);
 
 263   $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
 
 265   my @columns = (qw(fromacc typ toacc));
 
 267   #$self->number_columns([ grep { !m/^(?:month|year|quarter)$/ } @columns ]);
 
 270     fromacc                  => { text => 'Quelle', align => 'left'   },
 
 271     typ                      => { text => 'Typ'   , align => 'right'   },
 
 272     toacc                    => { text => 'Ziel'  , align => 'left'   },
 
 275   #$column_defs{$_}->{align} = 'right' for @columns;
 
 277   $self->report->set_options(
 
 278     std_column_visibility => 1,
 
 279     controller_class      => 'Mebil',
 
 280     output_format         => 'HTML',
 
 281  #   raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->current_year - 10)..($self->current_year + 5)) ]),
 
 282     title                 => 'mebil - Mapping',
 
 283 #    allow_pdf_export      => 1,
 
 284 #    allow_csv_export      => 1,
 
 286   $self->report->set_columns(%column_defs);
 
 287   $self->report->set_column_order(@columns);
 
 288 #  $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
 
 289   $self->report->set_options_from_form;
 
 294 #  my @visible_columns  = $self->report->get_visible_columns;
 
 295 #  my @type_columns     = @{ $self->types };
 
 296 #  my @non_type_columns = grep { my $c = $_; none { $c eq $_ } @type_columns } @visible_columns;
 
 298         foreach my $mapping (@{ $self->{data} }) {
 
 300                     fromacc                  => { data => $mapping->{fromacc} },
 
 301                 typ                      => { data => $mapping->{typ}     },
 
 302                 toacc                    => { data => $mapping->{toacc}   },
 
 304                 $self->report->add_data(\%data);
 
 307         return $self->report->generate_with_headers;