epic-s6g
[kivitendo-erp.git] / SL / Controller / Mebil.pm
index fa80bca..b8d02a4 100644 (file)
@@ -8,9 +8,13 @@ use parent qw(SL::Controller::Base);
 use SL::ReportGenerator;
 use SL::DBUtils;
 
+use SL::Locale::String;  # t8
+
 #use Data::Dumper;
 #use SL::ClientJS;
 
+use SL::mebil::Mapping;
+
 use Rose::Object::MakeMethods::Generic (
   scalar                  => [ qw(report number_columns year current_year objects subtotals_per_quarter salesman_id) ],
   'scalar --get_set_init' => [ qw(employees types data) ],
@@ -48,9 +52,14 @@ sub action_map {
 sub action_showmap {
        $::lxdebug->enter_sub;
        my ($self) = @_;
+       
+       # call model -> diese Zeile ist fraglich, war ein Konflikt
+       #$self->{data} = DB::MebilMapping::getMappings($::form->get_standard_dbh);
+       
+       $::form->{title} = $::locale->text('Mebil Map');
 
-       my $sql = "SELECT  fromacc,typ,toacc from mebil_mapping order by ordering";
-       $self->{data} = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
+       my $mapping = new SL::mebil::Mapping($::form, $::form->get_standard_dbh);
+       $self->{data} = $mapping->get_mapping();
 
        $self->prepare_report;
        $self->list_data;
@@ -61,48 +70,14 @@ sub action_showmap {
 sub action_calcmap {
        $::lxdebug->enter_sub;
        my ($self) = @_;
-
-       my $sql = "SELECT  fromacc,typ,toacc from mebil_mapping order by ordering";
-       $self->{data} = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);
-
-       my %fromacc = ();
-       my %toacc   = ();
-       foreach my $mapping (@{ $self->{data} }) {
-               if ($mapping->{typ} eq 'H') {
-                       # process Haben
-                       $sql = "SELECT SUM(ac.amount) AS saldo
-                                               FROM acc_trans ac 
-                                               JOIN chart c ON (c.id = ac.chart_id) 
-                                               WHERE (ac.transdate <= '31.12.2020') 
-                                               AND (c.accno = '$mapping->{fromacc}') ";
-                       my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $sql);            
-                       $fromacc{$mapping->{fromacc}} = $result->[0]->{saldo};
-                       $toacc{$mapping->{toacc}} += $result->[0]->{saldo};
-               }
-               elsif ($mapping->{typ} eq 'S') {
-                       # process Soll
-                       $fromacc{$mapping->{fromacc}} = 200.;
-                       $toacc{$mapping->{toacc}} = 200.;
-               }
-               elsif ($mapping->{typ} eq 'V') {
-                       # process Vorjahr
-                       $fromacc{$mapping->{fromacc}} = 300.;
-                       $toacc{$mapping->{toacc}} = 300.;
-               }
-               elsif ($mapping->{typ} eq 'A') {
-                       # process Aktjahr
-                       $fromacc{$mapping->{fromacc}} = 400.;
-                       $toacc{$mapping->{toacc}} = 400.;
-               }
-               elsif ($mapping->{typ} eq 'X') {
-                       # process Soll
-                       $fromacc{$mapping->{fromacc}} += $toacc{$mapping->{fromacc}};
-                       $toacc{$mapping->{toacc}} += $toacc{$mapping->{fromacc}};
-               }
-               else {
-                       die "Error: Invalid mapping type\n";
-               }
-       }
+       
+       $self->year($::form->{year} || DateTime->today->year - 1);
+       
+       $::form->{title} = $::locale->text('Mebil Map');
+       
+       my $mapping = new SL::mebil::Mapping($::form, $::form->get_standard_dbh);
+       
+       (my $fromacc, my $toacc) = $mapping->calc_mapping($self->{year});
        
        $self->report(SL::ReportGenerator->new(\%::myconfig, $::form));
 
@@ -113,44 +88,38 @@ sub action_calcmap {
        amount                      => { text => 'Betrag'   , align => 'right'   },
        );
 
-  #$column_defs{$_}->{align} = 'right' for @columns;
-
   $self->report->set_options(
     std_column_visibility => 1,
     controller_class      => 'Mebil',
     output_format         => 'HTML',
#   raw_top_info_text     => $self->render('financial_overview/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->current_year - 10)..($self->current_year + 5)) ]),
-    title                 => 'mebil - Mapping',
      raw_top_info_text     => $self->render('mebil/report_top', { output => 0 }, YEARS_TO_LIST => [ reverse(($self->year - 10)..($self->year + 5)) ]),
+    title                 => t8('mebil - Mapping: values for #1', $self->year),
 #    allow_pdf_export      => 1,
 #    allow_csv_export      => 1,
   );
   $self->report->set_columns(%column_defs);
   $self->report->set_column_order(@columns);
 #  $self->report->set_export_options(qw(list year subtotals_per_quarter salesman_id));
-  $self->report->set_options_from_form;
-
-       while (my($a,$v) = each(%fromacc)) {
-               if ($a =~ /\d+/) {
-                       # list only kivitendo accounts
-                       my %data = (
-                           name                     => { data => $a },
-                       amount                   => { data => $v },
-                       );
-                       $self->report->add_data(\%data);
-               }
-       }
-       while (my($a,$v) = each(%toacc)) {
+       $self->report->set_options_from_form;
+       $self->add_data_sorted($fromacc);
+       $self->add_data_sorted($toacc);
+       $::lxdebug->leave_sub;
+       return $self->report->generate_with_headers;
+}
+sub add_data_sorted {
+       my $self = shift;
+       my $data = shift; # hash reference
+       
+       foreach my $key (sort keys %$data) {
                my %data = (
-                   name                     => { data => $a },
-               amount                   => { data => $v },
+                   name                     => { data => $key },
+               amount                   => { data => $::form->format_amount(\%::myconfig, $data->{$key}, 2) },
                );
                $self->report->add_data(\%data);
+               
        }
-       return $self->report->generate_with_headers;
-       $::lxdebug->leave_sub;
 }
-
 sub prepare_report {
   my ($self)      = @_;