Einen Filter für den Kunden-/Lieferantentypen bei Berichten über Kunden-/Lieferantens...
[kivitendo-erp.git] / SL / RP.pm
index 6c32832..bb212e0 100644 (file)
--- a/SL/RP.pm
+++ b/SL/RP.pm
 
 package RP;
 
-sub income_statement {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  my $last_period = 0;
-  my @categories  = qw(I E);
-  my $category;
-
-  $form->{decimalplaces} *= 1;
-
-  &get_accounts($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form,
-                \@categories);
-
-  # if there are any compare dates
-  if ($form->{comparefromdate} || $form->{comparetodate}) {
-    $last_period = 1;
-
-    &get_accounts($dbh, $last_period,
-                  $form->{comparefromdate},
-                  $form->{comparetodate},
-                  $form, \@categories);
-  }
-
-  # disconnect
-  $dbh->disconnect;
-
-  # now we got $form->{I}{accno}{ }
-  # and $form->{E}{accno}{  }
-
-  my %account = (
-                 'I' => { 'label'  => 'income',
-                          'labels' => 'income',
-                          'ml'     => 1
-                 },
-                 'E' => { 'label'  => 'expense',
-                          'labels' => 'expenses',
-                          'ml'     => -1
-                 });
-
-  my $str;
-
-  foreach $category (@categories) {
-
-    foreach $key (sort keys %{ $form->{$category} }) {
-
-      # push description onto array
-
-      $str = ($form->{l_heading}) ? $form->{padding} : "";
-
-      if ($form->{$category}{$key}{charttype} eq "A") {
-        $str .=
-          ($form->{l_accno})
-          ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
-          : "$form->{$category}{$key}{description}";
-      }
-      if ($form->{$category}{$key}{charttype} eq "H") {
-        if ($account{$category}{subtotal} && $form->{l_subtotal}) {
-          $dash = "- ";
-          push(@{ $form->{"$account{$category}{label}_account"} },
-               "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
-          );
-          push(@{ $form->{"$account{$category}{labels}_this_period"} },
-               $form->format_amount(
-                        $myconfig,
-                        $account{$category}{subthis} * $account{$category}{ml},
-                        $form->{decimalplaces}, $dash
-               ));
-
-          if ($last_period) {
-            push(@{ $form->{"$account{$category}{labels}_last_period"} },
-                 $form->format_amount(
-                        $myconfig,
-                        $account{$category}{sublast} * $account{$category}{ml},
-                        $form->{decimalplaces}, $dash
-                 ));
-          }
-
-        }
-
-        $str =
-          "$form->{br}$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
-
-        $account{$category}{subthis}        = $form->{$category}{$key}{this};
-        $account{$category}{sublast}        = $form->{$category}{$key}{last};
-        $account{$category}{subdescription} =
-          $form->{$category}{$key}{description};
-        $account{$category}{subtotal} = 1;
-
-        $form->{$category}{$key}{this} = 0;
-        $form->{$category}{$key}{last} = 0;
-
-        next unless $form->{l_heading};
-
-        $dash = " ";
-      }
-
-      push(@{ $form->{"$account{$category}{label}_account"} }, $str);
-
-      if ($form->{$category}{$key}{charttype} eq 'A') {
-        $form->{"total_$account{$category}{labels}_this_period"} +=
-          $form->{$category}{$key}{this} * $account{$category}{ml};
-        $dash = "- ";
-      }
-
-      push(@{ $form->{"$account{$category}{labels}_this_period"} },
-           $form->format_amount(
-                      $myconfig,
-                      $form->{$category}{$key}{this} * $account{$category}{ml},
-                      $form->{decimalplaces}, $dash
-           ));
-
-      # add amount or - for last period
-      if ($last_period) {
-        $form->{"total_$account{$category}{labels}_last_period"} +=
-          $form->{$category}{$key}{last} * $account{$category}{ml};
-
-        push(@{ $form->{"$account{$category}{labels}_last_period"} },
-             $form->format_amount(
-                      $myconfig,
-                      $form->{$category}{$key}{last} * $account{$category}{ml},
-                      $form->{decimalplaces}, $dash
-             ));
-      }
-    }
-
-    $str = ($form->{l_heading}) ? $form->{padding} : "";
-    if ($account{$category}{subtotal} && $form->{l_subtotal}) {
-      push(@{ $form->{"$account{$category}{label}_account"} },
-           "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
-      );
-      push(@{ $form->{"$account{$category}{labels}_this_period"} },
-           $form->format_amount(
-                        $myconfig,
-                        $account{$category}{subthis} * $account{$category}{ml},
-                        $form->{decimalplaces}, $dash
-           ));
-
-      if ($last_period) {
-        push(@{ $form->{"$account{$category}{labels}_last_period"} },
-             $form->format_amount(
-                        $myconfig,
-                        $account{$category}{sublast} * $account{$category}{ml},
-                        $form->{decimalplaces}, $dash
-             ));
-      }
-    }
-
-  }
-
-  # totals for income and expenses
-  $form->{total_income_this_period} =
-    $form->round_amount($form->{total_income_this_period},
-                        $form->{decimalplaces});
-  $form->{total_expenses_this_period} =
-    $form->round_amount($form->{total_expenses_this_period},
-                        $form->{decimalplaces});
-
-  # total for income/loss
-  $form->{total_this_period} =
-    $form->{total_income_this_period} - $form->{total_expenses_this_period};
-
-  if ($last_period) {
-
-    # total for income/loss
-    $form->{total_last_period} =
-      $form->format_amount(
-       $myconfig,
-       $form->{total_income_last_period} - $form->{total_expenses_last_period},
-       $form->{decimalplaces},
-       "- ");
-
-    # totals for income and expenses for last_period
-    $form->{total_income_last_period} =
-      $form->format_amount($myconfig,
-                           $form->{total_income_last_period},
-                           $form->{decimalplaces}, "- ");
-    $form->{total_expenses_last_period} =
-      $form->format_amount($myconfig,
-                           $form->{total_expenses_last_period},
-                           $form->{decimalplaces}, "- ");
-
-  }
-
-  $form->{total_income_this_period} =
-    $form->format_amount($myconfig,
-                         $form->{total_income_this_period},
-                         $form->{decimalplaces}, "- ");
-  $form->{total_expenses_this_period} =
-    $form->format_amount($myconfig,
-                         $form->{total_expenses_this_period},
-                         $form->{decimalplaces}, "- ");
-  $form->{total_this_period} =
-    $form->format_amount($myconfig,
-                         $form->{total_this_period},
-                         $form->{decimalplaces}, "- ");
-
-  $main::lxdebug->leave_sub();
-}
-
 sub balance_sheet {
   $main::lxdebug->enter_sub();
 
@@ -542,15 +339,6 @@ sub get_accounts {
              $category
              ORDER by c.accno|;
 
-  if ($form->{accounttype} eq 'gifi') {
-    $query = qq|SELECT g.accno, g.description, c.category
-               FROM gifi g
-               JOIN chart c ON (c.gifi_accno = g.accno)
-               WHERE c.charttype = 'H'
-               $category
-               ORDER BY g.accno|;
-  }
-
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
@@ -595,277 +383,7 @@ sub get_accounts {
                 |;
   }
 
-  if ($form->{accounttype} eq 'gifi') {
-
-    if ($form->{method} eq 'cash') {
-
-      $query = qq|
-
-                SELECT g.accno, sum(ac.amount) AS amount,
-                g.description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN ar a ON (a.id = ac.trans_id)
-                JOIN gifi g ON (g.accno = c.gifi_accno)
-                $dpt_join
-                WHERE $where
-                $dpt_where
-                $category
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AR_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-                SELECT '' AS accno, SUM(ac.amount) AS amount,
-                '' AS description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN ar a ON (a.id = ac.trans_id)
-                $dpt_join
-                WHERE $where
-                $dpt_where
-                $category
-                AND c.gifi_accno = ''
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AR_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY c.category
-
-       UNION ALL
-
-                        SELECT g.accno, sum(ac.amount) AS amount,
-                g.description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN ap a ON (a.id = ac.trans_id)
-                JOIN gifi g ON (g.accno = c.gifi_accno)
-                $dpt_join
-                WHERE $where
-                $dpt_where
-                $category
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AP_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-                SELECT '' AS accno, SUM(ac.amount) AS amount,
-                '' AS description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN ap a ON (a.id = ac.trans_id)
-                $dpt_join
-                WHERE $where
-                $dpt_where
-                $category
-                AND c.gifi_accno = ''
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AP_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY c.category
-
-       UNION ALL
-
--- add gl
-
-                SELECT g.accno, sum(ac.amount) AS amount,
-                g.description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN gifi g ON (g.accno = c.gifi_accno)
-                JOIN gl a ON (a.id = ac.trans_id)
-                $dpt_join
-                WHERE $where
-                $glwhere
-                $dpt_where
-                $category
-                AND NOT (c.link = 'AR' OR c.link = 'AP')
-                $project
-                GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-                SELECT '' AS accno, SUM(ac.amount) AS amount,
-                '' AS description, c.category
-                FROM acc_trans ac
-                JOIN chart c ON (c.id = ac.chart_id)
-                JOIN gl a ON (a.id = ac.trans_id)
-                $dpt_join
-                WHERE $where
-                $glwhere
-                $dpt_where
-                $category
-                AND c.gifi_accno = ''
-                AND NOT (c.link = 'AR' OR c.link = 'AP')
-                $project
-                GROUP BY c.category
-                |;
-
-      if ($form->{project_id}) {
-
-        $query .= qq|
-
-       UNION ALL
-
-                SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
-                g.description AS description, c.category
-                FROM invoice ac
-                JOIN ar a ON (a.id = ac.trans_id)
-                JOIN parts p ON (ac.parts_id = p.id)
-                JOIN chart c on (p.income_accno_id = c.id)
-                JOIN gifi g ON (g.accno = c.gifi_accno)
-                $dpt_join
-       -- use transdate from subwhere
-                WHERE 1 = 1 $subwhere
-                AND c.category = 'I'
-                $dpt_where
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AR_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-                SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
-                g.description AS description, c.category
-                FROM invoice ac
-                JOIN ap a ON (a.id = ac.trans_id)
-                JOIN parts p ON (ac.parts_id = p.id)
-                JOIN chart c on (p.expense_accno_id = c.id)
-                JOIN gifi g ON (g.accno = c.gifi_accno)
-                $dpt_join
-                WHERE 1 = 1 $subwhere
-                AND c.category = 'E'
-                $dpt_where
-                AND ac.trans_id IN
-                  (
-                    SELECT trans_id
-                    FROM acc_trans
-                    JOIN chart ON (chart_id = id)
-                    WHERE link LIKE '%AP_paid%'
-                    $subwhere
-                  )
-                $project
-                GROUP BY g.accno, g.description, c.category
-                |;
-      }
-
-    } else {
-
-      if ($department_id) {
-        $dpt_join = qq|
-             JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
-             |;
-        $dpt_where = qq|
-               AND t.department_id = $department_id
-             |;
-
-      }
-
-      $query = qq|
-
-             SELECT g.accno, SUM(ac.amount) AS amount,
-             g.description, c.category
-             FROM acc_trans ac
-             JOIN chart c ON (c.id = ac.chart_id)
-             JOIN gifi g ON (c.gifi_accno = g.accno)
-             $dpt_join
-             WHERE $where
-             $dpt_from
-             $category
-             $project
-             GROUP BY g.accno, g.description, c.category
-
-          UNION ALL
-
-             SELECT '' AS accno, SUM(ac.amount) AS amount,
-             '' AS description, c.category
-             FROM acc_trans ac
-             JOIN chart c ON (c.id = ac.chart_id)
-             $dpt_join
-             WHERE $where
-             $dpt_from
-             $category
-             AND c.gifi_accno = ''
-             $project
-             GROUP BY c.category
-             |;
-
-      if ($form->{project_id}) {
-
-        $query .= qq|
-
-        UNION ALL
-
-                SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
-                g.description AS description, c.category
-                FROM invoice ac
-                JOIN ar a ON (a.id = ac.trans_id)
-                JOIN parts p ON (ac.parts_id = p.id)
-                JOIN chart c on (p.income_accno_id = c.id)
-                JOIN gifi g ON (c.gifi_accno = g.accno)
-                $dpt_join
-       -- use transdate from subwhere
-                WHERE 1 = 1 $subwhere
-                AND c.category = 'I'
-                $dpt_where
-                $project
-                GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-                SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
-                g.description AS description, c.category
-                FROM invoice ac
-                JOIN ap a ON (a.id = ac.trans_id)
-                JOIN parts p ON (ac.parts_id = p.id)
-                JOIN chart c on (p.expense_accno_id = c.id)
-                JOIN gifi g ON (c.gifi_accno = g.accno)
-                $dpt_join
-                WHERE 1 = 1 $subwhere
-                AND c.category = 'E'
-                $dpt_where
-                $project
-                GROUP BY g.accno, g.description, c.category
-                |;
-      }
-
-    }
-
-  } else {    # standard account
+  {    # standard account
 
     if ($form->{method} eq 'cash') {
 
@@ -1121,6 +639,7 @@ sub get_accounts_g {
   my $project;
   my $where    = "1 = 1";
   my $glwhere  = "";
+  my $prwhere  = "";
   my $subwhere = "";
   my $item;
 
@@ -1128,6 +647,7 @@ sub get_accounts_g {
     if ($form->{method} eq 'cash') {
       $subwhere .= " AND transdate >= '$fromdate'";
       $glwhere = " AND ac.transdate >= '$fromdate'";
+      $prwhere = " AND ar.transdate >= '$fromdate'";
     } else {
       $where .= " AND ac.transdate >= '$fromdate'";
     }
@@ -1136,6 +656,7 @@ sub get_accounts_g {
   if ($todate) {
     $where    .= " AND ac.transdate <= '$todate'";
     $subwhere .= " AND transdate <= '$todate'";
+    $prwhere  .= " AND ar.transdate <= '$todate'";
   }
 
   if ($department_id) {
@@ -1228,8 +749,7 @@ sub get_accounts_g {
                 JOIN parts p ON (ac.parts_id = p.id)
                 JOIN chart c on (p.income_accno_id = c.id)
                 $dpt_join
-       -- use transdate from subwhere
-                WHERE 1 = 1 $subwhere
+                WHERE 1 = 1 $prwhere
                 AND c.category = 'I'
                 $dpt_where
                 AND ac.trans_id IN
@@ -1253,7 +773,7 @@ sub get_accounts_g {
                 JOIN parts p ON (ac.parts_id = p.id)
                 JOIN chart c on (p.expense_accno_id = c.id)
                 $dpt_join
-                WHERE 1 = 1 $subwhere
+                WHERE 1 = 1 $prwhere
                 AND c.category = 'E'
                 $dpt_where
                 AND ac.trans_id IN
@@ -1307,8 +827,7 @@ sub get_accounts_g {
                 JOIN parts p ON (ac.parts_id = p.id)
                 JOIN chart c on (p.income_accno_id = c.id)
                 $dpt_join
-       -- use transdate from subwhere
-                WHERE 1 = 1 $subwhere
+                WHERE 1 = 1 $prwhere
                 AND c.category = 'I'
                 $dpt_where
                 $project
@@ -1323,7 +842,7 @@ sub get_accounts_g {
                 JOIN parts p ON (ac.parts_id = p.id)
                 JOIN chart c on (p.expense_accno_id = c.id)
                 $dpt_join
-                WHERE 1 = 1 $subwhere
+                WHERE 1 = 1 $prwhere
                 AND c.category = 'E'
                 $dpt_where
                 $project
@@ -1336,6 +855,7 @@ sub get_accounts_g {
   my @accno;
   my $accno;
   my $ref;
+
   #print $query;
   my $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
@@ -1400,23 +920,7 @@ sub trial_balance {
   # get beginning balances
   if ($form->{fromdate}) {
 
-    if ($form->{accounttype} eq 'gifi') {
-
-      $query = qq|SELECT g.accno, c.category, SUM(ac.amount) AS amount,
-                  g.description
-                 FROM acc_trans ac
-                 JOIN chart c ON (ac.chart_id = c.id)
-                 JOIN gifi g ON (c.gifi_accno = g.accno)
-                 $dpt_join
-                 WHERE ac.transdate < '$form->{fromdate}'
-                 $dpt_where
-                 $project
-                 GROUP BY g.accno, c.category, g.description
-                 |;
-
-    } else {
-
-      $query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount,
+    $query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount,
                   c.description
                  FROM acc_trans ac
                  JOIN chart c ON (ac.chart_id = c.id)
@@ -1427,8 +931,6 @@ sub trial_balance {
                  GROUP BY c.accno, c.category, c.description
                  |;
 
-    }
-
     $sth = $dbh->prepare($query);
     $sth->execute || $form->dberror($query);
 
@@ -1452,14 +954,6 @@ sub trial_balance {
              WHERE c.charttype = 'H'
              ORDER by c.accno|;
 
-  if ($form->{accounttype} eq 'gifi') {
-    $query = qq|SELECT g.accno, g.description, c.category
-               FROM gifi g
-               JOIN chart c ON (c.gifi_accno = g.accno)
-               WHERE c.charttype = 'H'
-               ORDER BY g.accno|;
-  }
-
   $sth = $dbh->prepare($query);
   $sth->execute || $form->dberror($query);
 
@@ -1512,62 +1006,7 @@ sub trial_balance {
     $where .= $tofrom;
   }
 
-  if ($form->{accounttype} eq 'gifi') {
-
-    $query = qq|SELECT g.accno, g.description, c.category,
-                SUM(ac.amount) AS amount
-               FROM acc_trans ac
-               JOIN chart c ON (c.id = ac.chart_id)
-               JOIN gifi g ON (c.gifi_accno = g.accno)
-               $dpt_join
-               WHERE $where
-               $dpt_where
-               $project
-               GROUP BY g.accno, g.description, c.category
-               |;
-
-    if ($form->{project_id}) {
-
-      $query .= qq|
-
-       -- add project transactions from invoice
-
-       UNION ALL
-
-               SELECT g.accno, g.description, c.category,
-               SUM(ac.sellprice * ac.qty) AS amount
-               FROM invoice ac
-               JOIN ar a ON (ac.trans_id = a.id)
-               JOIN parts p ON (ac.parts_id = p.id)
-               JOIN chart c ON (p.income_accno_id = c.id)
-               JOIN gifi g ON (c.gifi_accno = g.accno)
-               $dpt_join
-               WHERE $invwhere
-               $dpt_where
-               $project
-               GROUP BY g.accno, g.description, c.category
-
-       UNION ALL
-
-               SELECT g.accno, g.description, c.category,
-               SUM(ac.sellprice * ac.qty) * -1 AS amount
-               FROM invoice ac
-               JOIN ap a ON (ac.trans_id = a.id)
-               JOIN parts p ON (ac.parts_id = p.id)
-               JOIN chart c ON (p.expense_accno_id = c.id)
-               JOIN gifi g ON (c.gifi_accno = g.accno)
-               $dpt_join
-               WHERE $invwhere
-               $dpt_where
-               $project
-               GROUP BY g.accno, g.description, c.category
-               |;
-    }
-
-    $query .= qq|
-               ORDER BY accno|;
-
-  } else {
+  {
 
     $query = qq|SELECT c.accno, c.description, c.category,
                 SUM(ac.amount) AS amount
@@ -1646,30 +1085,6 @@ sub trial_balance {
              AND c.accno = ?) AS credit
              |;
 
-  if ($form->{accounttype} eq 'gifi') {
-
-    $query = qq|SELECT (SELECT SUM(ac.amount) * -1
-               FROM acc_trans ac
-               JOIN chart c ON (c.id = ac.chart_id)
-               $dpt_join
-               WHERE $where
-               $dpt_where
-               $project
-               AND ac.amount < 0
-               AND c.gifi_accno = ?) AS debit,
-
-              (SELECT SUM(ac.amount)
-               FROM acc_trans ac
-               JOIN chart c ON (c.id = ac.chart_id)
-               $dpt_join
-               WHERE $where
-               $dpt_where
-               $project
-               AND ac.amount > 0
-               AND c.gifi_accno = ?) AS credit|;
-
-  }
-
   $drcr = $dbh->prepare($query);
 
   if ($form->{project_id}) {
@@ -1988,23 +1403,6 @@ sub get_taxaccounts {
   }
   $sth->finish;
 
-  # get gifi tax accounts
-  my $query = qq|SELECT DISTINCT ON (g.accno) g.accno, g.description,
-                 sum(t.rate) AS rate
-                 FROM gifi g, chart c, tax t
-                WHERE g.accno = c.gifi_accno
-                AND c.id = t.chart_id
-                AND c.link LIKE '%CT_tax%'
-                GROUP BY g.accno, g.description
-                 ORDER BY accno|;
-  my $sth = $dbh->prepare($query);
-  $sth->execute || $form->dberror;
-
-  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
-    push @{ $form->{gifi_taxaccounts} }, $ref;
-  }
-  $sth->finish;
-
   $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
@@ -2032,15 +1430,9 @@ sub tax_report {
   my ($accno, $rate);
 
   if ($form->{accno}) {
-    if ($form->{accno} =~ /^gifi_/) {
-      ($null, $accno) = split /_/, $form->{accno};
-      $rate  = $form->{"$form->{accno}_rate"};
-      $accno = qq| AND ch.gifi_accno = '$accno'|;
-    } else {
-      $accno = $form->{accno};
-      $rate  = $form->{"$form->{accno}_rate"};
-      $accno = qq| AND ch.accno = '$accno'|;
-    }
+    $accno = $form->{accno};
+    $rate  = $form->{"$form->{accno}_rate"};
+    $accno = qq| AND ch.accno = '$accno'|;
   }
   $rate *= 1;
 
@@ -2088,7 +1480,7 @@ sub tax_report {
   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
-  $sortorder = $form->{sort} unless $sortorder;
+  $sortorder = $form->{sort} if $form->{sort};
 
   $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
               a.invnumber, n.name, a.netamount,
@@ -2247,6 +1639,7 @@ sub payments {
 
   my $sortorder = join ', ',
     $form->sort_columns(qw(name invnumber ordnumber transdate source));
+  $sortorder = $form->{sort} if $form->{sort};
 
   # cycle through each id
   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
@@ -2343,11 +1736,6 @@ sub bwa {
   foreach $key (@periods) {
     $form->{ "$key" . "gesamtleistung" } = 0;
     $form->{ "$key" . "gesamtkosten" }   = 0;
-    foreach $kosten (@gesamtkosten) {
-      $form->{$kosten}{$key} *= -1;
-    }
-    
-    $form->{4}{$key} *= -1;
 
     foreach $category (@categories) {
 
@@ -2355,14 +1743,15 @@ sub bwa {
         $form->{"$key$category"} =
           $form->format_amount($myconfig,
                                $form->round_amount($form->{$category}{$key}, 2
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
       }
     }
     foreach $item (@gesamtleistung) {
       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
     }
     foreach $item (@gesamtkosten) {
-      $form->{$item}{$key} *= -1;
       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
     }
     $form->{ "$key" . "rohertrag" } =
@@ -2378,8 +1767,8 @@ sub bwa {
       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
     $form->{ "$key" . "ergebnisvorsteuern" } =
       $form->{ "$key" . "betriebsergebnis" } -
-      ($form->{ "$key" . "neutraleraufwand" } +
-       $form->{ "$key" . "neutralertrag" });
+      $form->{ "$key" . "neutraleraufwand" } +
+      $form->{ "$key" . "neutralertrag" };
     $form->{ "$key" . "ergebnis" } =
       $form->{ "$key" . "ergebnisvorsteuern" } + $form->{35}{$key};
 
@@ -2394,7 +1783,9 @@ sub bwa {
                                     $form->{ "$key" . "gesamtleistung" } * 100
                                  ),
                                  $form->{decimalplaces}
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
         }
       }
       foreach $item (@ergebnisse) {
@@ -2405,7 +1796,9 @@ sub bwa {
                                      $form->{ "$key" . "gesamtleistung" } * 100
                                  ),
                                  $form->{decimalplaces}
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
       }
     }
 
@@ -2419,7 +1812,9 @@ sub bwa {
                                       $form->{ "$key" . "gesamtkosten" } * 100
                                    ),
                                    $form->{decimalplaces}
-                                 ), $form->{decimalplaces}, '0');
+                                 ),
+                                 $form->{decimalplaces},
+                                 '0');
         }
       }
       foreach $item (@ergebnisse) {
@@ -2430,7 +1825,9 @@ sub bwa {
                                       $form->{ "$key" . "gesamtkosten" } * 100
                                    ),
                                    $form->{decimalplaces}
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
       }
     }
 
@@ -2439,11 +1836,13 @@ sub bwa {
         if (defined($form->{$category}{$key})) {
           $form->{ "$key" . "pk" . "$category" } =
             $form->format_amount(
-                      $myconfig,
-                      $form->round_amount(
-                        ($form->{$category}{$key} / $form->{10}{$key} * 100), 
-                        $form->{decimalplaces}
-                      ), $form->{decimalplaces}, '0');
+                        $myconfig,
+                        $form->round_amount(
+                          ($form->{$category}{$key} / $form->{10}{$key} * 100),
+                          $form->{decimalplaces}
+                        ),
+                        $form->{decimalplaces},
+                        '0');
         }
       }
       foreach $item (@ergebnisse) {
@@ -2454,7 +1853,9 @@ sub bwa {
                                                    $form->{10}{$key} * 100
                                                 ),
                                                 $form->{decimalplaces}
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
       }
     }
 
@@ -2463,11 +1864,13 @@ sub bwa {
         if (defined($form->{$category}{$key})) {
           $form->{ "$key" . "auf" . "$category" } =
             $form->format_amount(
-                       $myconfig,
-                       $form->round_amount(
-                         ($form->{$category}{$key} / $form->{4}{$key} * 100), 
-                         $form->{decimalplaces}
-                       ), $form->{decimalplaces}, '0');
+                         $myconfig,
+                         $form->round_amount(
+                           ($form->{$category}{$key} / $form->{4}{$key} * 100),
+                           $form->{decimalplaces}
+                         ),
+                         $form->{decimalplaces},
+                         '0');
         }
       }
       foreach $item (@ergebnisse) {
@@ -2478,16 +1881,20 @@ sub bwa {
                                                    $form->{4}{$key} * 100
                                                 ),
                                                 $form->{decimalplaces}
-                               ), $form->{decimalplaces}, '0');
+                               ),
+                               $form->{decimalplaces},
+                               '0');
       }
     }
 
     foreach $item (@ergebnisse) {
       $form->{ "$key" . "$item" } =
         $form->format_amount($myconfig,
-                             $form->round_amount($form->{ "$key" . "$item" }, 
-                             $form->{decimalplaces}
-                             ), $form->{decimalplaces}, '0');
+                             $form->round_amount($form->{ "$key" . "$item" },
+                                                 $form->{decimalplaces}
+                             ),
+                             $form->{decimalplaces},
+                             '0');
     }
 
   }
@@ -2507,7 +1914,7 @@ sub ustva {
   my $last_period     = 0;
   my $category        = "pos_ustva";
   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
-                           96 66 43 45 53 62 65 67);
+    96 66 43 45 53 62 65 67);
   my @categories_euro = qw(48 51 86 91 97 93 94);
   $form->{decimalplaces} *= 1;
 
@@ -2545,7 +1952,7 @@ sub ustva {
   #   }
   #
   #    }
-  
+
   #
   # Berechnung der USTVA Formularfelder
   #
@@ -2553,8 +1960,9 @@ sub ustva {
   $form->{"86r"} = $form->{"861"};
   $form->{"97r"} = $form->{"971"};
   $form->{"93r"} = $form->{"931"};
+
   #$form->{"96"}  = $form->{"94"} * 0.16;
-  $form->{"43"}  =
+  $form->{"43"} =
     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
     $form->{"96"};
   $form->{"45"} = $form->{"43"};
@@ -2565,12 +1973,14 @@ sub ustva {
 
   foreach $item (@categories_cent) {
     $form->{$item} =
-      $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2), 2, '0');
+      $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
+                           2, '0');
   }
 
   foreach $item (@categories_euro) {
     $form->{$item} =
-      $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0), 0, '0');
+      $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
+                           0, '0');
   }
 
   $dbh->disconnect;