epic-s6g
[kivitendo-erp.git] / SL / Controller / Mebil.pm
index fd77ce5..b8d02a4 100644 (file)
@@ -13,6 +13,8 @@ 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) ],
@@ -51,10 +53,13 @@ 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;
@@ -70,61 +75,9 @@ sub action_calcmap {
        
        $::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 %fromacc = ();
-       my %toacc   = ();
-       my $year = $self->year;
-       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.$year') 
-                                               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
-                       $sql = "SELECT SUM(ac.amount)* -1 AS saldo
-                                               FROM acc_trans ac 
-                                               JOIN chart c ON (c.id = ac.chart_id) 
-                                               WHERE (ac.transdate <= '31.12.$year') 
-                                               AND (c.accno = '$mapping->{fromacc}') ";
-                       my @result = SL::DBUtils::selectfirst_array_query($::form, $::form->get_standard_dbh, $sql);            
-                       $fromacc{$mapping->{fromacc}} = $result[0];
-                       $toacc{$mapping->{toacc}} += $result[0];
-               }
-#              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') {
-                       # add to other account
-                       $toacc{$mapping->{toacc}} += $toacc{$mapping->{fromacc}};
-               }
-               elsif ($mapping->{typ} eq 'Y') {
-                       # substract from other account
-                       $toacc{$mapping->{toacc}} -= $toacc{$mapping->{fromacc}};
-               }
-               elsif ($mapping->{typ} eq 'C') {
-                       # constant value
-                       ; # do nothing here
-               }
-               else {
-                       die "Error: Invalid mapping type: $mapping->{typ}\n";
-               }
-       }
+       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));
 
@@ -148,11 +101,11 @@ sub action_calcmap {
   $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;
-       $self->add_data_sorted(\%fromacc);
-       $self->add_data_sorted(\%toacc);
+       $self->add_data_sorted($fromacc);
+       $self->add_data_sorted($toacc);
  
-       return $self->report->generate_with_headers;
        $::lxdebug->leave_sub;
+       return $self->report->generate_with_headers;
 }
 sub add_data_sorted {
        my $self = shift;