-    
-      
-       
-	
-|;
+  my @columns = qw(
+    transdate      id               reference      description
+    notes          source           debit          debit_accno
+    credit         credit_accno     debit_tax      debit_tax_accno
+    credit_tax     credit_tax_accno projectnumbers balance
+  );
 
-  map { print "$column_header{$_}\n" } @column_index;
+  my @hidden_variables = qw(accno source reference department description notes project_id datefrom dateto category l_subtotal);
+  push @hidden_variables, map { "l_${_}" } @columns;
 
-  print "
-         
-        
-        
-        
-";
+  my (@options, @date_options);
+  push @options,      $locale->text('Account')     . " : $form->{accno} $form->{account_description}" if ($form->{accno});
+  push @options,      $locale->text('Source')      . " : $form->{source}"                             if ($form->{source});
+  push @options,      $locale->text('Reference')   . " : $form->{reference}"                          if ($form->{reference});
+  push @options,      $locale->text('Description') . " : $form->{description}"                        if ($form->{description});
+  push @options,      $locale->text('Notes')       . " : $form->{notes}"                              if ($form->{notes});
 
-  # add sort to callback
-  $form->{callback} = "$callback&sort=$form->{sort}";
-  $callback = $form->escape($form->{callback});
+  push @date_options, $locale->text('From'), $locale->date(\%myconfig, $form->{datefrom}, 1)          if ($form->{datefrom});
+  push @date_options, $locale->text('Bis'),  $locale->date(\%myconfig, $form->{dateto},   1)          if ($form->{dateto});
+  push @options,      join(' ', @date_options)                                                        if (scalar @date_options);
 
-  # initial item for subtotals
-  if (@{ $form->{GL} }) {
-    $sameitem = $form->{GL}->[0]->{ $form->{sort} };
+  if ($form->{department}) {
+    my ($department) = split /--/, $form->{department};
+    push @options, $locale->text('Department') . " : $department";
   }
 
-  if (($form->{accno} || $form->{gifi_accno}) && $form->{balance}) {
-
-    map { $column_data{$_} = "  | " } @column_index;
-    $column_data{balance} =
-        ""
-      . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
-      . " | ";
 
-    $i++;
-    $i %= 2;
-    print qq|
-        
-|;
-    map { print "$column_data{$_}\n" } @column_index;
+  my $callback = build_std_url('action=generate_report', grep { $form->{$_} } @hidden_variables);
+
+  $form->{l_credit_accno}     = 'Y';
+  $form->{l_debit_accno}      = 'Y';
+  $form->{l_credit_tax}       = 'Y';
+  $form->{l_debit_tax}        = 'Y';
+  $form->{l_credit_tax_accno} = 'Y';
+  $form->{l_debit_tax_accno}  = 'Y';
+  $form->{l_balance}          = $form->{accno} ? 'Y' : '';
+
+  my %column_defs = (
+    'id'               => { 'text' => $locale->text('ID'), },
+    'transdate'        => { 'text' => $locale->text('Date'), },
+    'reference'        => { 'text' => $locale->text('Reference'), },
+    'source'           => { 'text' => $locale->text('Source'), },
+    'description'      => { 'text' => $locale->text('Description'), },
+    'notes'            => { 'text' => $locale->text('Notes'), },
+    'debit'            => { 'text' => $locale->text('Debit'), },
+    'debit_accno'      => { 'text' => $locale->text('Debit Account'), },
+    'credit'           => { 'text' => $locale->text('Credit'), },
+    'credit_accno'     => { 'text' => $locale->text('Credit Account'), },
+    'debit_tax'        => { 'text' => $locale->text('Debit Tax'), },
+    'debit_tax_accno'  => { 'text' => $locale->text('Debit Tax Account'), },
+    'credit_tax'       => { 'text' => $locale->text('Credit Tax'), },
+    'credit_tax_accno' => { 'text' => $locale->text('Credit Tax Account'), },
+    'balance'          => { 'text' => $locale->text('Balance'), },
+    'projectnumbers'   => { 'text' => $locale->text('Project Numbers'), },
+  );
 
-    print qq|
-         
-|;
+  foreach my $name (qw(id transdate reference description debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
+    my $sortname                = $name =~ m/accno/ ? 'accno' : $name;
+    my $sortdir                 = $sortname eq $form->{sort} ? 1 - $form->{sortdir} : $form->{sortdir};
+    $column_defs{$name}->{link} = $callback . "&sort=$sortname&sortdir=$sortdir";
   }
-  $form->{balance} *= $ml;
-  foreach $ref (@{ $form->{GL} }) {
-    $form->{balance} *= $ml;
-
-    # if item ne sort print subtotal
-    if ($form->{l_subtotal} eq 'Y') {
-      if ($sameitem ne $ref->{ $form->{sort} }) {
-        &gl_subtotal;
-      }
-    }
 
-    #foreach $key (sort keys(%{ $ref->{amount} })) {
-    #  $form->{balance} += $ref->{amount}{$key};
-    #}
+  map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
+  map { $column_defs{$_}->{visible} = 0 } qw(debit_accno credit_accno debit_tax_accno credit_tax_accno) if $form->{accno};
 
-    $debit = "";
-    foreach $key (sort keys(%{ $ref->{debit} })) {
-      $subtotaldebit += $ref->{debit}{$key};
-      $totaldebit    += $ref->{debit}{$key};
-      if ($key == 0) {
-        $debit = $form->format_amount(\%myconfig, $ref->{debit}{$key}, 2, 0);
-      } else {
-        $debit .=
-          " " . $form->format_amount(\%myconfig, $ref->{debit}{$key}, 2, 0);
-      }
-      $form->{balance} = abs($form->{balance}) - abs($ref->{debit}{$key});
-    }
+  my %column_alignment;
+  map { $column_alignment{$_}     = 'right'  } qw(balance id debit credit debit_tax credit_tax balance);
+  map { $column_alignment{$_}     = 'center' } qw(reference debit_accno credit_accno debit_tax_accno credit_tax_accno);
+  map { $column_alignment{$_}     = 'left' } qw(description source notes);
+  map { $column_defs{$_}->{align} = $column_alignment{$_} } keys %column_alignment;
 
-    $credit = "";
-    foreach $key (sort keys(%{ $ref->{credit} })) {
-      $subtotalcredit += $ref->{credit}{$key};
-      $totalcredit    += $ref->{credit}{$key};
-      if ($key == 0) {
-        $credit = $form->format_amount(\%myconfig, $ref->{credit}{$key}, 2, 0);
-      } else {
-        $credit .= " "
-          . $form->format_amount(\%myconfig, $ref->{credit}{$key}, 2, 0);
-      }
-      $form->{balance} = abs($form->{balance}) - abs($ref->{credit}{$key});
-    }
+  my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
-    $debittax = "";
-    foreach $key (sort keys(%{ $ref->{debit_tax} })) {
-      $subtotaldebittax += $ref->{debit_tax}{$key};
-      $totaldebittax    += $ref->{debit_tax}{$key};
-      if ($key == 0) {
-        $debittax =
-          $form->format_amount(\%myconfig, $ref->{debit_tax}{$key}, 2, 0);
-      } else {
-        $debittax .= " "
-          . $form->format_amount(\%myconfig, $ref->{debit_tax}{$key}, 2, 0);
-      }
-      $form->{balance} = abs($form->{balance}) - abs($ref->{debit_tax}{$key});
-    }
+  $report->set_columns(%column_defs);
+  $report->set_column_order(@columns);
 
-    $credittax = "";
-    foreach $key (sort keys(%{ $ref->{credit_tax} })) {
-      $subtotalcredittax += $ref->{credit_tax}{$key};
-      $totalcredittax    += $ref->{credit_tax}{$key};
-      if ($key == 0) {
-        $credittax =
-          $form->format_amount(\%myconfig, $ref->{credit_tax}{$key}, 2, 0);
-      } else {
-        $credittax .= " "
-          . $form->format_amount(\%myconfig, $ref->{credit_tax}{$key}, 2, 0);
-      }
-      $form->{balance} = abs($form->{balance}) - abs($ref->{credit_tax}{$key});
-    }
+  $report->set_export_options('generate_report', @hidden_variables, qw(sort sortdir));
 
-    $debitaccno  = "";
-    $debittaxkey = "";
-    $taxaccno    = "";
-    foreach $key (sort keys(%{ $ref->{debit_accno} })) {
-      if ($key == 0) {
-        $debitaccno =
-          "{debit_accno}{$key}&callback=$callback>$ref->{debit_accno}{$key}";
-      } else {
-        $debitaccno .=
-          " {debit_accno}{$key}&callback=$callback>$ref->{debit_accno}{$key}";
-      }
+  $report->set_sort_indicator($form->{sort} eq 'accno' ? 'debit_accno' : $form->{sort}, $form->{sortdir});
 
-      #       if ($ref->{debit_taxkey}{$key} eq $debittaxkey) {
-      #         $ref->{debit_tax_accno}{$key} = $taxaccno;
-      #       }
-      $taxaccno    = $ref->{debit_tax_accno}{$key};
-      $debittaxkey = $ref->{debit_taxkey}{$key};
-    }
+  $report->set_options('top_info_text'        => join("\n", @options),
+                       'output_format'        => 'HTML',
+                       'title'                => $form->{title},
+                       'attachment_basename'  => $locale->text('general_ledger_list') . strftime('_%Y%m%d', localtime time),
+    );
+  $report->set_options_from_form();
 
-    $creditaccno  = "";
-    $credittaxkey = "";
-    $taxaccno     = "";
-    foreach $key (sort keys(%{ $ref->{credit_accno} })) {
-      if ($key == 0) {
-        $creditaccno =
-          "{credit_accno}{$key}&callback=$callback>$ref->{credit_accno}{$key}";
-      } else {
-        $creditaccno .=
-          " {credit_accno}{$key}&callback=$callback>$ref->{credit_accno}{$key}";
-      }
+  # add sort to callback
+  $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
 
-      #       if ($ref->{credit_taxkey}{$key} eq $credittaxkey) {
-      #         $ref->{credit_tax_accno}{$key} = $taxaccno;
-      #       }
-      $taxaccno     = $ref->{credit_tax_accno}{$key};
-      $credittaxkey = $ref->{credit_taxkey}{$key};
-    }
 
-    $debittaxaccno = "";
-    foreach $key (sort keys(%{ $ref->{debit_tax_accno} })) {
-      if ($key == 0) {
-        $debittaxaccno =
-          "{debit_tax_accno}{$key}&callback=$callback>$ref->{debit_tax_accno}{$key}";
-      } else {
-        $debittaxaccno .=
-          " {debit_tax_accno}{$key}&callback=$callback>$ref->{debit_tax_accno}{$key}";
-      }
-    }
+  my @totals_columns = qw(debit credit debit_tax credit_tax);
+  my %subtotals      = map { $_ => 0 } @totals_columns;
+  my %totals         = map { $_ => 0 } @totals_columns;
+  my $idx            = 0;
 
-    $credittaxaccno = "";
-    foreach $key (sort keys(%{ $ref->{credit_tax_accno} })) {
-      if ($key == 0) {
-        $credittaxaccno =
-          "{credit_tax_accno}{$key}&callback=$callback>$ref->{credit_tax_accno}{$key}";
-      } else {
-        $credittaxaccno .=
-          " {credit_tax_accno}{$key}&callback=$callback>$ref->{credit_tax_accno}{$key}";
+  foreach my $ref (@{ $form->{GL} }) {
+
+    my %rows;
+
+    foreach my $key (qw(debit credit debit_tax credit_tax)) {
+      $rows{$key} = [];
+      foreach my $idx (sort keys(%{ $ref->{$key} })) {
+        my $value         = $ref->{$key}->{$idx};
+        $subtotals{$key} += $value;
+        $totals{$key}    += $value;
+        if ($key =~ /debit.*/) {
+          $ml = -1;
+        } else {
+          $ml = 1;
+        }
+        $form->{balance}  = $form->{balance} + $value * $ml;
+        push @{ $rows{$key} }, $form->format_amount(\%myconfig, $value, 2);
       }
     }
 
-    $transdate = "";
-    foreach $key (sort keys(%{ $ref->{transdate} })) {
-      if ($key == 0) {
-        $transdate = "$ref->{transdate}{$key}";
-      } else {
-        $transdate .= " $ref->{transdate}{$key}";
-      }
+    foreach my $key (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno ac_transdate source)) {
+      my $col = $key eq 'ac_transdate' ? 'transdate' : $key;
+      $rows{$col} = [ map { $ref->{$key}->{$_} } sort keys(%{ $ref->{$key} }) ];
     }
 
-    #    $ref->{debit} = $form->format_amount(\%myconfig, $ref->{debit}, 2, " ");
-    #    $ref->{credit} = $form->format_amount(\%myconfig, $ref->{credit}, 2, " ");
-
-    $column_data{id}        = " $ref->{id}  | ";
-    $column_data{transdate}    = "$transdate | ";
-    $column_data{reference} =
-      "{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&password=$form->{password}&callback=$callback>$ref->{reference} | ";
-    $column_data{description}  = "$ref->{description}  | ";
-    $column_data{source}       = "$ref->{source}  | ";
-    $column_data{notes}        = "$ref->{notes}  | ";
-    $column_data{debit}        = "$debit | ";
-    $column_data{debit_accno}  = "$debitaccno | ";
-    $column_data{credit}       = "$credit | ";
-    $column_data{credit_accno} = "$creditaccno | ";
-    $column_data{debit_tax}    =
-      ($ref->{debit_tax_accno} ne "")
-      ? "$debittax | "
-      : " | ";
-    $column_data{debit_tax_accno} = "$debittaxaccno | ";
-    $column_data{gifi_accno}      =
-      "{gifi_accno}&callback=$callback>$ref->{gifi_accno}  | ";
-    $column_data{credit_tax} =
-      ($ref->{credit_tax_accno} ne "")
-      ? "$credittax | "
-      : " | ";
-    $column_data{credit_tax_accno} = "$credittaxaccno | ";
-    $column_data{gifi_accno}       =
-      "{gifi_accno}&callback=$callback>$ref->{gifi_accno}  | ";
-    $column_data{balance} =
-      ""
-      . $form->format_amount(\%myconfig, $form->{balance}, 2, 0) . " | ";
-    $column_data{projectnumbers} =
-      "" . join(", ", sort({ lc($a) cmp lc($b) } keys(%{ $ref->{projectnumbers} }))) . " | ";
-
-    $i++;
-    $i %= 2;
-    print "
-        ";
-    map { print "$column_data{$_}\n" } @column_index;
-    print " ";
+    my $row = { };
+    map { $row->{$_} = { 'data' => '', 'align' => $column_alignment{$_} } } @columns;
 
-  }
+    my $sh = "";
+    if ($form->{balance} < 0) {
+      $sh = " S";
+      $ml = -1;
+    } elsif ($form->{balance} > 0) {
+      $sh = " H";
+      $ml = 1;
+    }
+    my $data = $form->format_amount(\%myconfig, ($form->{balance} * $ml), 2);
+    $data .= $sh;
 
-  &gl_subtotal if ($form->{l_subtotal} eq 'Y');
-
-  map { $column_data{$_} = "  | " } @column_index;
-
-  my $balanced_ledger = $totaldebit 
-                      + $totaldebittax 
-                      - $totalcredit 
-                      - $totalcredittax;
-                    # = 0 for balanced ledger
-                    
-  $column_data{debit} =
-    ""
-    . $form->format_amount(\%myconfig, $totaldebit, 2, " ") . " | ";
-  $column_data{credit} =
-    ""
-    . $form->format_amount(\%myconfig, $totalcredit, 2, " ") . " | ";
-  $column_data{debit_tax} =
-    ""
-    . $form->format_amount(\%myconfig, $totaldebittax, 2, " ") . " | ";
-  $column_data{credit_tax} =
-    ""
-    . $form->format_amount(\%myconfig, $totalcredittax, 2, " ") . " | ";
-  $column_data{balance} =
-    ""
-    . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0) . " | ";
+    $row->{balance}->{data}        = $data;
+    $row->{projectnumbers}->{data} = join ", ", sort { lc($a) cmp lc($b) } keys %{ $ref->{projectnumbers} };
 
-  print qq|
-	
-|;
+    map { $row->{$_}->{data} = $ref->{$_} } qw(id reference description notes);
 
-  map { print "$column_data{$_}\n" } @column_index;
+    map { $row->{$_}->{data} = \@{ $rows{$_} }; } qw(transdate debit credit debit_accno credit_accno debit_tax_accno credit_tax_accno source);
 
-  print qq|
-         
-        |;
+    foreach my $col (qw(debit_accno credit_accno debit_tax_accno credit_tax_accno)) {
+      $row->{$col}->{link} = [ map { "${callback}&accno=" . E($_) } @{ $rows{$col} } ];
+    }
 
+    map { $row->{$_}->{data} = \@{ $rows{$_} } if ($ref->{"${_}_accno"} ne "") } qw(debit_tax credit_tax);
 
-  if ( abs($balanced_ledger) >  0.001 ) {
+    $row->{reference}->{link} = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{id}), 'callback');
 
-    print qq|| |
-        . $locale->text('Unbalanced Ledger') 
-        . ": " 
-        . $form->format_amount(\%myconfig, $balanced_ledger, 3, " ")
+    my $row_set = [ $row ];
 
-  } elsif ( abs($balanced_ledger) <= 0.001 ) {
+    if (($form->{l_subtotal} eq 'Y')
+        && (($idx == (scalar @{ $form->{GL} } - 1))
+            || ($ref->{ $form->{sort} } ne $form->{GL}->[$idx + 1]->{ $form->{sort} }))) {
+      push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, [ qw(debit credit) ], 'listsubtotal');
+    }
 
-    print qq| | |
-          . $locale->text('Balanced Ledger') 
+    $report->add_data($row_set);
 
+    $idx++;
   }
 
-  
-  print qq|
-          | 
-         
-        
-       
-     | 
-  
-