1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  15 #  Contributors: Benjamin Lee <benjaminlee@consultant.com>
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # backend code for reports
 
  33 #======================================================================
 
  40   $main::lxdebug->enter_sub();
 
  42   my ($self, $myconfig, $form) = @_;
 
  45   my $dbh = $form->dbconnect($myconfig);
 
  48   my @categories  = qw(A C L Q);
 
  50   # if there are any dates construct a where
 
  51   if ($form->{asofdate}) {
 
  52     $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
 
  55   $form->{decimalplaces} *= 1;
 
  57   &get_accounts($dbh, $last_period, "", $form->{asofdate}, $form,
 
  60   # if there are any compare dates
 
  61   if ($form->{compareasofdate}) {
 
  64     &get_accounts($dbh, $last_period, "", $form->{compareasofdate},
 
  67     $form->{last_period} = conv_dateq($form->{compareasofdate});
 
  74   # now we got $form->{A}{accno}{ }    assets
 
  75   # and $form->{L}{accno}{ }           liabilities
 
  76   # and $form->{Q}{accno}{ }           equity
 
  77   # build asset accounts
 
  83                  'A' => { 'label'  => 'asset',
 
  87                  'L' => { 'label'  => 'liability',
 
  88                           'labels' => 'liabilities',
 
  91                  'Q' => { 'label'  => 'equity',
 
  96   foreach my $category (grep { !/C/ } @categories) {
 
  98     foreach $key (sort keys %{ $form->{$category} }) {
 
 100       $str = ($form->{l_heading}) ? $form->{padding} : "";
 
 102       if ($form->{$category}{$key}{charttype} eq "A") {
 
 105           ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
 
 106           : "$form->{$category}{$key}{description}";
 
 108       if ($form->{$category}{$key}{charttype} eq "H") {
 
 109         if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 111           push(@{ $form->{"$account{$category}{label}_account"} },
 
 112                "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
 
 114           push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 115                $form->format_amount(
 
 117                         $account{$category}{subthis} * $account{$category}{ml},
 
 118                         $form->{decimalplaces}, $dash
 
 122             push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 123                  $form->format_amount(
 
 125                         $account{$category}{sublast} * $account{$category}{ml},
 
 126                         $form->{decimalplaces}, $dash
 
 132           "$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
 
 134         $account{$category}{subthis}        = $form->{$category}{$key}{this};
 
 135         $account{$category}{sublast}        = $form->{$category}{$key}{last};
 
 136         $account{$category}{subdescription} =
 
 137           $form->{$category}{$key}{description};
 
 138         $account{$category}{subtotal} = 1;
 
 140         $form->{$category}{$key}{this} = 0;
 
 141         $form->{$category}{$key}{last} = 0;
 
 143         next unless $form->{l_heading};
 
 148       # push description onto array
 
 149       push(@{ $form->{"$account{$category}{label}_account"} }, $str);
 
 151       if ($form->{$category}{$key}{charttype} eq 'A') {
 
 152         $form->{"total_$account{$category}{labels}_this_period"} +=
 
 153           $form->{$category}{$key}{this} * $account{$category}{ml};
 
 157       push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 158            $form->format_amount(
 
 160                       $form->{$category}{$key}{this} * $account{$category}{ml},
 
 161                       $form->{decimalplaces}, $dash
 
 165         $form->{"total_$account{$category}{labels}_last_period"} +=
 
 166           $form->{$category}{$key}{last} * $account{$category}{ml};
 
 168         push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 169              $form->format_amount(
 
 171                       $form->{$category}{$key}{last} * $account{$category}{ml},
 
 172                       $form->{decimalplaces}, $dash
 
 177     $str = ($form->{l_heading}) ? $form->{padding} : "";
 
 178     if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 179       push(@{ $form->{"$account{$category}{label}_account"} },
 
 180            "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
 
 182       push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 183            $form->format_amount(
 
 185                         $account{$category}{subthis} * $account{$category}{ml},
 
 186                         $form->{decimalplaces}, $dash
 
 190         push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 191              $form->format_amount(
 
 193                         $account{$category}{sublast} * $account{$category}{ml},
 
 194                         $form->{decimalplaces}, $dash
 
 201   # totals for assets, liabilities
 
 202   $form->{total_assets_this_period} =
 
 203     $form->round_amount($form->{total_assets_this_period},
 
 204                         $form->{decimalplaces});
 
 205   $form->{total_liabilities_this_period} =
 
 206     $form->round_amount($form->{total_liabilities_this_period},
 
 207                         $form->{decimalplaces});
 
 208   $form->{total_equity_this_period} =
 
 209     $form->round_amount($form->{total_equity_this_period},
 
 210                         $form->{decimalplaces});
 
 213   $form->{earnings_this_period} =
 
 214     $form->{total_assets_this_period} -
 
 215     $form->{total_liabilities_this_period} - $form->{total_equity_this_period};
 
 217   push(@{ $form->{equity_this_period} },
 
 218        $form->format_amount($myconfig,
 
 219                             $form->{earnings_this_period},
 
 220                             $form->{decimalplaces}, "- "
 
 223   $form->{total_equity_this_period} =
 
 225              $form->{total_equity_this_period} + $form->{earnings_this_period},
 
 226              $form->{decimalplaces});
 
 228   # add liability + equity
 
 229   $form->{total_this_period} =
 
 230     $form->format_amount(
 
 232     $form->{total_liabilities_this_period} + $form->{total_equity_this_period},
 
 233     $form->{decimalplaces},
 
 238     # totals for assets, liabilities
 
 239     $form->{total_assets_last_period} =
 
 240       $form->round_amount($form->{total_assets_last_period},
 
 241                           $form->{decimalplaces});
 
 242     $form->{total_liabilities_last_period} =
 
 243       $form->round_amount($form->{total_liabilities_last_period},
 
 244                           $form->{decimalplaces});
 
 245     $form->{total_equity_last_period} =
 
 246       $form->round_amount($form->{total_equity_last_period},
 
 247                           $form->{decimalplaces});
 
 249     # calculate retained earnings
 
 250     $form->{earnings_last_period} =
 
 251       $form->{total_assets_last_period} -
 
 252       $form->{total_liabilities_last_period} -
 
 253       $form->{total_equity_last_period};
 
 255     push(@{ $form->{equity_last_period} },
 
 256          $form->format_amount($myconfig,
 
 257                               $form->{earnings_last_period},
 
 258                               $form->{decimalplaces}, "- "
 
 261     $form->{total_equity_last_period} =
 
 263              $form->{total_equity_last_period} + $form->{earnings_last_period},
 
 264              $form->{decimalplaces});
 
 266     # add liability + equity
 
 267     $form->{total_last_period} =
 
 268       $form->format_amount($myconfig,
 
 269                            $form->{total_liabilities_last_period} +
 
 270                              $form->{total_equity_last_period},
 
 271                            $form->{decimalplaces},
 
 276   $form->{total_liabilities_last_period} =
 
 277     $form->format_amount($myconfig,
 
 278                          $form->{total_liabilities_last_period},
 
 279                          $form->{decimalplaces}, "- ")
 
 280     if ($form->{total_liabilities_last_period} != 0);
 
 282   $form->{total_equity_last_period} =
 
 283     $form->format_amount($myconfig,
 
 284                          $form->{total_equity_last_period},
 
 285                          $form->{decimalplaces}, "- ")
 
 286     if ($form->{total_equity_last_period} != 0);
 
 288   $form->{total_assets_last_period} =
 
 289     $form->format_amount($myconfig,
 
 290                          $form->{total_assets_last_period},
 
 291                          $form->{decimalplaces}, "- ")
 
 292     if ($form->{total_assets_last_period} != 0);
 
 294   $form->{total_assets_this_period} =
 
 295     $form->format_amount($myconfig,
 
 296                          $form->{total_assets_this_period},
 
 297                          $form->{decimalplaces}, "- ");
 
 299   $form->{total_liabilities_this_period} =
 
 300     $form->format_amount($myconfig,
 
 301                          $form->{total_liabilities_this_period},
 
 302                          $form->{decimalplaces}, "- ");
 
 304   $form->{total_equity_this_period} =
 
 305     $form->format_amount($myconfig,
 
 306                          $form->{total_equity_this_period},
 
 307                          $form->{decimalplaces}, "- ");
 
 309   $main::lxdebug->leave_sub();
 
 313   $main::lxdebug->enter_sub();
 
 315   my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
 
 317   my ($null, $department_id) = split /--/, $form->{department};
 
 329   my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
 
 333     qq|SELECT c.accno, c.description, c.category
 
 335        WHERE (c.charttype = 'H')
 
 339   $sth = prepare_execute_query($form, $dbh, $query);
 
 341   my @headingaccounts = ();
 
 342   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 343     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 344       "$ref->{description}";
 
 345     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
 
 346     $form->{ $ref->{category} }{ $ref->{accno} }{accno}     = $ref->{accno};
 
 348     push @headingaccounts, $ref->{accno};
 
 354     $fromdate = conv_dateq($fromdate);
 
 355     if ($form->{method} eq 'cash') {
 
 356       $subwhere .= " AND (transdate >= $fromdate)";
 
 357       $glwhere = " AND (ac.transdate >= $fromdate)";
 
 359       $where .= " AND (ac.transdate >= $fromdate)";
 
 364     $todate = conv_dateq($todate);
 
 365     $where    .= " AND (ac.transdate <= $todate)";
 
 366     $subwhere .= " AND (transdate <= $todate)";
 
 369   if ($department_id) {
 
 370     $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
 
 371     $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
 
 374   if ($form->{project_id}) {
 
 375     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 378   if ($form->{method} eq 'cash') {
 
 380       qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 382          JOIN chart c ON (c.id = ac.chart_id)
 
 383          JOIN ar a ON (a.id = ac.trans_id)
 
 392                JOIN chart ON (chart_id = id)
 
 393                WHERE (link LIKE '%AR_paid%')
 
 397          GROUP BY c.accno, c.description, c.category
 
 401          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 403          JOIN chart c ON (c.id = ac.chart_id)
 
 404          JOIN ap a ON (a.id = ac.trans_id)
 
 413                JOIN chart ON (chart_id = id)
 
 414                WHERE (link LIKE '%AP_paid%')
 
 418          GROUP BY c.accno, c.description, c.category
 
 422          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 424          JOIN chart c ON (c.id = ac.chart_id)
 
 425          JOIN gl a ON (a.id = ac.trans_id)
 
 431              AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 433          GROUP BY c.accno, c.description, c.category |;
 
 435     if ($form->{project_id}) {
 
 440          SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 442          JOIN ar a ON (a.id = ac.trans_id)
 
 443          JOIN parts p ON (ac.parts_id = p.id)
 
 444          JOIN chart c on (p.income_accno_id = c.id)
 
 446          -- use transdate from subwhere
 
 447          WHERE (c.category = 'I')
 
 454                JOIN chart ON (chart_id = id)
 
 455                WHERE (link LIKE '%AR_paid%')
 
 459          GROUP BY c.accno, c.description, c.category
 
 463          SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
 
 465          JOIN ap a ON (a.id = ac.trans_id)
 
 466          JOIN parts p ON (ac.parts_id = p.id)
 
 467          JOIN chart c on (p.expense_accno_id = c.id)
 
 469          WHERE (c.category = 'E')
 
 476                JOIN chart ON (chart_id = id)
 
 477                WHERE link LIKE '%AP_paid%'
 
 481          GROUP BY c.accno, c.description, c.category |;
 
 484   } else {                      # if ($form->{method} eq 'cash')
 
 485     if ($department_id) {
 
 486       $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
 487       $dpt_where = qq| AND t.department_id = $department_id |;
 
 491       SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 493       JOIN chart c ON (c.id = ac.chart_id)
 
 499       GROUP BY c.accno, c.description, c.category |;
 
 501     if ($form->{project_id}) {
 
 505       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 507       JOIN ar a ON (a.id = ac.trans_id)
 
 508       JOIN parts p ON (ac.parts_id = p.id)
 
 509       JOIN chart c on (p.income_accno_id = c.id)
 
 511       -- use transdate from subwhere
 
 512       WHERE (c.category = 'I')
 
 516       GROUP BY c.accno, c.description, c.category
 
 520       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
 
 522       JOIN ap a ON (a.id = ac.trans_id)
 
 523       JOIN parts p ON (ac.parts_id = p.id)
 
 524       JOIN chart c on (p.expense_accno_id = c.id)
 
 526       WHERE (c.category = 'E')
 
 530       GROUP BY c.accno, c.description, c.category |;
 
 538   my $sth = prepare_execute_query($form, $dbh, $query);
 
 540   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 542     if ($ref->{category} eq 'C') {
 
 543       $ref->{category} = 'A';
 
 546     # get last heading account
 
 547     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
 551         $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
 
 553         $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
 
 557     $form->{ $ref->{category} }{ $ref->{accno} }{accno}       = $ref->{accno};
 
 558     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 560     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
 
 563       $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
 
 565       $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
 
 570   # remove accounts with zero balance
 
 571   foreach $category (@{$categories}) {
 
 572     foreach $accno (keys %{ $form->{$category} }) {
 
 573       $form->{$category}{$accno}{last} =
 
 574         $form->round_amount($form->{$category}{$accno}{last},
 
 575                             $form->{decimalplaces});
 
 576       $form->{$category}{$accno}{this} =
 
 577         $form->round_amount($form->{$category}{$accno}{this},
 
 578                             $form->{decimalplaces});
 
 580       delete $form->{$category}{$accno}
 
 581         if (   $form->{$category}{$accno}{this} == 0
 
 582             && $form->{$category}{$accno}{last} == 0);
 
 586   $main::lxdebug->leave_sub();
 
 590   $main::lxdebug->enter_sub();
 
 592   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 594   my ($null, $department_id) = split /--/, $form->{department};
 
 607     $fromdate = conv_dateq($fromdate);
 
 608     if ($form->{method} eq 'cash') {
 
 609       $subwhere .= " AND (transdate >= $fromdate)";
 
 610       $glwhere = " AND (ac.transdate >= $fromdate)";
 
 611       $prwhere = " AND (ar.transdate >= $fromdate)";
 
 613       $where .= " AND (ac.transdate >= $fromdate)";
 
 618     $todate = conv_dateq($todate);
 
 619     $where    .= " AND (ac.transdate <= $todate)";
 
 620     $subwhere .= " AND (transdate <= $todate)";
 
 621     $prwhere  .= " AND (ar.transdate <= $todate)";
 
 624   if ($department_id) {
 
 625     $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
 
 626     $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 629   if ($form->{project_id}) {
 
 630     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
 
 633   if ($form->{method} eq 'cash') {
 
 635       qq|SELECT sum(ac.amount) AS amount, c.$category
 
 637          JOIN chart c ON (c.id = ac.chart_id)
 
 638          JOIN ar a ON (a.id = ac.trans_id)
 
 646                JOIN chart ON (chart_id = id)
 
 647                WHERE (link LIKE '%AR_paid%')
 
 655          SELECT sum(ac.amount) AS amount, c.$category
 
 657          JOIN chart c ON (c.id = ac.chart_id)
 
 658          JOIN ap a ON (a.id = ac.trans_id)
 
 666                JOIN chart ON (chart_id = id)
 
 667                WHERE (link LIKE '%AP_paid%')
 
 675          SELECT sum(ac.amount) AS amount, c.$category
 
 677          JOIN chart c ON (c.id = ac.chart_id)
 
 678          JOIN gl a ON (a.id = ac.trans_id)
 
 683            AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 685          GROUP BY c.$category |;
 
 687     if ($form->{project_id}) {
 
 691          SELECT SUM(ac.sellprice * ac.qty) AS amount, c.$category
 
 693          JOIN ar a ON (a.id = ac.trans_id)
 
 694          JOIN parts p ON (ac.parts_id = p.id)
 
 695          JOIN chart c on (p.income_accno_id = c.id)
 
 697          WHERE (c.category = 'I')
 
 704                JOIN chart ON (chart_id = id)
 
 705                WHERE (link LIKE '%AR_paid%')
 
 713          SELECT SUM(ac.sellprice) AS amount, c.$category
 
 715          JOIN ap a ON (a.id = ac.trans_id)
 
 716          JOIN parts p ON (ac.parts_id = p.id)
 
 717          JOIN chart c on (p.expense_accno_id = c.id)
 
 719          WHERE (c.category = 'E') $prwhere
 
 725                JOIN chart ON (chart_id = id)
 
 726                WHERE (link LIKE '%AP_paid%')
 
 731          GROUP BY c.$category |;
 
 734   } else {                      # if ($form->{method} eq 'cash')
 
 735     if ($department_id) {
 
 736       $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
 737       $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 741         SELECT sum(ac.amount) AS amount, c.$category
 
 743         JOIN chart c ON (c.id = ac.chart_id)
 
 748         GROUP BY c.$category |;
 
 750     if ($form->{project_id}) {
 
 754         SELECT SUM(ac.sellprice * ac.qty) AS amount, c.$category
 
 756         JOIN ar a ON (a.id = ac.trans_id)
 
 757         JOIN parts p ON (ac.parts_id = p.id)
 
 758         JOIN chart c on (p.income_accno_id = c.id)
 
 760         WHERE (c.category = 'I')
 
 768         SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount, c.$category
 
 770         JOIN ap a ON (a.id = ac.trans_id)
 
 771         JOIN parts p ON (ac.parts_id = p.id)
 
 772         JOIN chart c on (p.expense_accno_id = c.id)
 
 774         WHERE (c.category = 'E')
 
 778         GROUP BY c.$category |;
 
 787   my $sth = prepare_execute_query($form, $dbh, $query);
 
 789   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 790     if ($ref->{amount} < 0) {
 
 791       $ref->{amount} *= -1;
 
 793     if ($category eq "pos_bwa") {
 
 795         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
 797         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
 800       $form->{ $ref->{$category} } += $ref->{amount};
 
 805   $main::lxdebug->leave_sub();
 
 809   $main::lxdebug->enter_sub();
 
 811   my ($self, $myconfig, $form) = @_;
 
 813   my $dbh = $form->dbconnect($myconfig);
 
 815   my ($query, $sth, $ref);
 
 818   my ($null, $department_id) = split /--/, $form->{department};
 
 819   my @headingaccounts = ();
 
 825   my $invwhere = $where;
 
 827   if ($department_id) {
 
 828     $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
 
 829     $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 832   # project_id only applies to getting transactions
 
 833   # it has nothing to do with a trial balance
 
 834   # but we use the same function to collect information
 
 836   if ($form->{project_id}) {
 
 837     $project = qq| AND ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 840   # get beginning balances
 
 841   if ($form->{fromdate}) {
 
 843       qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
 
 845          JOIN chart c ON (ac.chart_id = c.id)
 
 847          WHERE (ac.transdate < ?)
 
 850          GROUP BY c.accno, c.category, c.description |;
 
 852     $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
 
 854     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 855       $balance{ $ref->{accno} } = $ref->{amount};
 
 857       if ($ref->{amount} != 0 && $form->{all_accounts}) {
 
 858         $trb{ $ref->{accno} }{description} = $ref->{description};
 
 859         $trb{ $ref->{accno} }{charttype}   = 'A';
 
 860         $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 870     qq|SELECT c.accno, c.description, c.category
 
 872        WHERE c.charttype = 'H'
 
 875   $sth = prepare_execute_query($form, $dbh, $query);
 
 877   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 878     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 879     $trb{ $ref->{accno} }{charttype}   = 'H';
 
 880     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 882     push @headingaccounts, $ref->{accno};
 
 890   if ($form->{fromdate} || $form->{todate}) {
 
 891     if ($form->{fromdate}) {
 
 892       my $fromdate = conv_dateq($form->{fromdate});
 
 893       $tofrom   .= " AND (ac.transdate >= $fromdate)";
 
 894       $subwhere .= " AND (transdate >= $fromdate)";
 
 895       $invwhere .= " AND (a.transdate >= $fromdate)";
 
 896       $glwhere = " AND (ac.transdate >= $fromdate)";
 
 898     if ($form->{todate}) {
 
 899       my $todate = conv_dateq($form->{todate});
 
 900       $tofrom   .= " AND (ac.transdate <= $todate)";
 
 901       $invwhere .= " AND (a.transdate <= $todate)";
 
 902       $subwhere .= " AND (transdate <= $todate)";
 
 903       $glwhere  .= " AND (ac.transdate <= $todate)";
 
 909       qq| AND ((ac.trans_id IN (SELECT id from ar) AND
 
 914                     JOIN chart ON (chart_id = id)
 
 915                     WHERE (link LIKE '%AR_paid%')
 
 920                (ac.trans_id in (SELECT id from ap) AND
 
 925                     JOIN chart ON (chart_id = id)
 
 926                     WHERE (link LIKE '%AP_paid%')
 
 931                (ac.trans_id in (SELECT id from gl)
 
 939        SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
 
 941        JOIN chart c ON (c.id = ac.chart_id)
 
 946        GROUP BY c.accno, c.description, c.category |;
 
 948   if ($form->{project_id}) {
 
 950       -- add project transactions from invoice
 
 954       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
 
 956       JOIN ar a ON (ac.trans_id = a.id)
 
 957       JOIN parts p ON (ac.parts_id = p.id)
 
 958       JOIN chart c ON (p.income_accno_id = c.id)
 
 963       GROUP BY c.accno, c.description, c.category
 
 967       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
 
 969       JOIN ap a ON (ac.trans_id = a.id)
 
 970       JOIN parts p ON (ac.parts_id = p.id)
 
 971       JOIN chart c ON (p.expense_accno_id = c.id)
 
 976       GROUP BY c.accno, c.description, c.category
 
 980   $query .= qq| ORDER BY accno|;
 
 982   $sth = prepare_execute_query($form, $dbh, $query);
 
 984   # calculate the debit and credit in the period
 
 985   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 986     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 987     $trb{ $ref->{accno} }{charttype}   = 'A';
 
 988     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 989     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
 993   # prepare query for each account
 
 994   my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
 
 998          (SELECT SUM(ac.amount) * -1
 
1000           JOIN chart c ON (c.id = ac.chart_id)
 
1006           AND (c.accno = ?)) AS debit,
 
1008          (SELECT SUM(ac.amount)
 
1010           JOIN chart c ON (c.id = ac.chart_id)
 
1016           AND c.accno = ?) AS credit |;
 
1017   $drcr = prepare_query($form, $dbh, $q_drcr);
 
1019   if ($form->{project_id}) {
 
1020     # prepare query for each account
 
1023           (SELECT SUM(ac.sellprice * ac.qty) * -1
 
1025            JOIN parts p ON (ac.parts_id = p.id)
 
1026            JOIN ap a ON (ac.trans_id = a.id)
 
1027            JOIN chart c ON (p.expense_accno_id = c.id)
 
1032            AND c.accno = ?) AS debit,
 
1034           (SELECT SUM(ac.sellprice * ac.qty)
 
1036            JOIN parts p ON (ac.parts_id = p.id)
 
1037            JOIN ar a ON (ac.trans_id = a.id)
 
1038            JOIN chart c ON (p.income_accno_id = c.id)
 
1043            AND c.accno = ?) AS credit |;
 
1045     $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
 
1048   my ($debit, $credit);
 
1050   foreach my $accno (sort keys %trb) {
 
1053     $ref->{accno} = $accno;
 
1054     map { $ref->{$_} = $trb{$accno}{$_} }
 
1055       qw(description category charttype amount);
 
1057     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1059     if ($trb{$accno}{charttype} eq 'A') {
 
1062       do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno});
 
1064       ($debit, $credit) = (0, 0);
 
1065       while (($debit, $credit) = $drcr->fetchrow_array) {
 
1066         $ref->{debit}  += $debit;
 
1067         $ref->{credit} += $credit;
 
1071       if ($form->{project_id}) {
 
1074         do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno});
 
1076         ($debit, $credit) = (0, 0);
 
1077         while (($debit, $credit) = $project_drcr->fetchrow_array) {
 
1078           $ref->{debit}  += $debit;
 
1079           $ref->{credit} += $credit;
 
1081         $project_drcr->finish;
 
1084       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1085       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1090     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1091     $accno = pop @accno;
 
1093       $trb{$accno}{debit}  += $ref->{debit};
 
1094       $trb{$accno}{credit} += $ref->{credit};
 
1097     push @{ $form->{TB} }, $ref;
 
1103   # debits and credits for headings
 
1104   foreach $accno (@headingaccounts) {
 
1105     foreach $ref (@{ $form->{TB} }) {
 
1106       if ($accno eq $ref->{accno}) {
 
1107         $ref->{debit}  = $trb{$accno}{debit};
 
1108         $ref->{credit} = $trb{$accno}{credit};
 
1113   $main::lxdebug->leave_sub();
 
1117   $main::lxdebug->enter_sub();
 
1118   my ($self, $dbh, $form) = @_;
 
1119   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
1120   my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
 
1121   my $sth =  $dbh->prepare($query);
 
1122   while(my $ref = $sth->fetchrow_hashref()) {
 
1123     $ref->{invnumer} =~ s/Storno zu //g;
 
1124     $form->{storno}{$ref->{invnumber}} = 1;
 
1126   $main::lxdebug->leave_sub();
 
1130   $main::lxdebug->enter_sub();
 
1132   my ($self, $myconfig, $form) = @_;
 
1134   # connect to database
 
1135   my $dbh     = $form->dbconnect($myconfig);
 
1137   my ($invoice, $arap, $buysell, $ct, $ct_id);
 
1139   if ($form->{ct} eq "customer") {
 
1150   $ct_id = "${ct}_id";
 
1152   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
 
1153   my $todate = conv_dateq($form->{todate});
 
1155   my $where = " 1 = 1 ";
 
1158   if ($form->{$ct_id}) {
 
1159     $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
 
1160   } elsif ($form->{ $form->{ct} }) {
 
1161     $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
 
1165   if ($form->{department}) {
 
1166     ($null, $department_id) = split /--/, $form->{department};
 
1167     $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
 
1168     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
1172     -- between 0-30 days
 
1174     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1175       street, zipcode, city, country, contact, email,
 
1176       phone as customerphone, fax as customerfax, ${ct}number,
 
1177       "invnumber", "transdate",
 
1178       (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90",
 
1179       "duedate", invoice, ${arap}.id,
 
1182        WHERE (${arap}.curr = exchangerate.curr)
 
1183          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1185     WHERE (paid != amount)
 
1186       AND (${arap}.storno IS FALSE)
 
1187       AND (${arap}.${ct}_id = ${ct}.id)
 
1189       AND (transdate <= (date $todate - interval '0 days'))
 
1190       AND (transdate >= (date $todate - interval '30 days'))
 
1194     -- between 31-60 days
 
1196     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1197       street, zipcode, city, country, contact, email,
 
1198       phone as customerphone, fax as customerfax, ${ct}number,
 
1199       "invnumber", "transdate",
 
1200       0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90",
 
1201       "duedate", invoice, ${arap}.id,
 
1204        WHERE (${arap}.curr = exchangerate.curr)
 
1205          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1207     WHERE (paid != amount)
 
1208       AND (${arap}.storno IS FALSE)
 
1209       AND (${arap}.${ct}_id = ${ct}.id)
 
1211       AND (transdate < (date $todate - interval '30 days'))
 
1212       AND (transdate >= (date $todate - interval '60 days'))
 
1216     -- between 61-90 days
 
1218     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1219       street, zipcode, city, country, contact, email,
 
1220       phone as customerphone, fax as customerfax, ${ct}number,
 
1221       "invnumber", "transdate",
 
1222       0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90",
 
1223       "duedate", invoice, ${arap}.id,
 
1226        WHERE (${arap}.curr = exchangerate.curr)
 
1227          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1229     WHERE (paid != amount)
 
1230       AND (${arap}.storno IS FALSE)
 
1231       AND (${arap}.${ct}_id = ${ct}.id)
 
1233       AND (transdate < (date $todate - interval '60 days'))
 
1234       AND (transdate >= (date $todate - interval '90 days'))
 
1240     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1241       street, zipcode, city, country, contact, email,
 
1242       phone as customerphone, fax as customerfax, ${ct}number,
 
1243       "invnumber", "transdate",
 
1244       0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90",
 
1245       "duedate", invoice, ${arap}.id,
 
1248        WHERE (${arap}.curr = exchangerate.curr)
 
1249        AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1251     WHERE (paid != amount)
 
1252       AND (${arap}.storno IS FALSE)
 
1253       AND (${arap}.${ct}_id = ${ct}.id)
 
1255       AND (transdate < (date $todate - interval '90 days'))
 
1257     ORDER BY ctid, transdate, invnumber |;
 
1259   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1261   # select outstanding vendors or customers, depends on $ct
 
1263     qq|SELECT DISTINCT ct.id, ct.name
 
1264        FROM $ct ct, $arap a
 
1267          AND (a.${ct_id} = ct.id)
 
1268          AND (a.paid != a.amount)
 
1269          AND (a.transdate <= $todate)
 
1272   my $sth = prepare_execute_query($form, $dbh, $query);
 
1275   # for each company that has some stuff outstanding
 
1276   while (my ($id) = $sth->fetchrow_array) {
 
1277     do_statement($form, $sth_details, $q_details, $id, $id, $id, $id);
 
1279     while (my $ref = $sth_details->fetchrow_hashref(NAME_lc)) {
 
1280       $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
 
1281       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1282       push @{ $form->{AG} }, $ref;
 
1285     $sth_details->finish;
 
1294   $main::lxdebug->leave_sub();
 
1298   $main::lxdebug->enter_sub();
 
1300   my ($self, $myconfig, $form) = @_;
 
1302   # connect to database
 
1303   my $dbh = $form->dbconnect($myconfig);
 
1305   my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
 
1308     qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1311   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1312     selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
 
1315   $main::lxdebug->leave_sub();
 
1318 sub get_taxaccounts {
 
1319   $main::lxdebug->enter_sub();
 
1321   my ($self, $myconfig, $form) = @_;
 
1323   # connect to database
 
1324   my $dbh = $form->dbconnect($myconfig);
 
1328     qq|SELECT c.accno, c.description, t.rate
 
1330        WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
 
1332   $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
 
1336   $main::lxdebug->leave_sub();
 
1340   $main::lxdebug->enter_sub();
 
1342   my ($self, $myconfig, $form) = @_;
 
1344   # connect to database
 
1345   my $dbh = $form->dbconnect($myconfig);
 
1347   my ($null, $department_id) = split /--/, $form->{department};
 
1350   my $where = "1 = 1";
 
1352   if ($department_id) {
 
1353     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
1358   if ($form->{accno}) {
 
1359     $accno = $form->{accno};
 
1360     $rate  = $form->{"$form->{accno}_rate"};
 
1361     $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
 
1367   if ($form->{db} eq 'ar') {
 
1368     $table = "customer";
 
1375   my $arap = lc($ARAP);
 
1377   my $transdate = "a.transdate";
 
1379   if ($form->{method} eq 'cash') {
 
1380     $transdate = "a.datepaid";
 
1382     my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
 
1389           JOIN chart ON (chart_id = id)
 
1390           WHERE (link LIKE '%${ARAP}_paid%')
 
1391           AND (transdate <= $todate)
 
1396   # if there are any dates construct a where
 
1397   $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
 
1398   $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
 
1400   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1402   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1403   $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
 
1405   if ($form->{report} !~ /nontaxable/) {
 
1407       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
 
1408           ac.amount * $ml AS tax
 
1410          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1411          JOIN chart ch ON (ch.id = ac.chart_id)
 
1412          JOIN $table n ON (n.id = a.${table}_id)
 
1416            AND (a.invoice = '0')
 
1420          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1421            i.sellprice * i.qty * $rate * $ml AS tax
 
1423          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1424          JOIN chart ch ON (ch.id = ac.chart_id)
 
1425          JOIN $table n ON (n.id = a.${table}_id)
 
1426          JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1427          JOIN invoice i ON (i.trans_id = a.id)
 
1428          JOIN partstax p ON (p.parts_id = i.parts_id)
 
1432            AND (a.invoice = '1')
 
1433          ORDER BY $sortorder|;
 
1435     # only gather up non-taxable transactions
 
1437       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
 
1439          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1440          JOIN $table n ON (n.id = a.${table}_id)
 
1443            AND (a.invoice = '0')
 
1444            AND (a.netamount = a.amount)
 
1448          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
 
1450          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1451          JOIN $table n ON (n.id = a.${table}_id)
 
1452          JOIN invoice i ON (i.trans_id = a.id)
 
1455            AND (a.invoice = '1')
 
1457              a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
 
1459              i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
 
1461          GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
 
1462          ORDER by $sortorder|;
 
1465   $form->{TR} = selectall_hashref_query($form, $dbh, $query);
 
1469   $main::lxdebug->leave_sub();
 
1472 sub paymentaccounts {
 
1473   $main::lxdebug->enter_sub();
 
1475   my ($self, $myconfig, $form) = @_;
 
1477   # connect to database, turn AutoCommit off
 
1478   my $dbh = $form->dbconnect_noauto($myconfig);
 
1480   my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
 
1482   # get A(R|P)_paid accounts
 
1484     qq|SELECT accno, description
 
1486        WHERE link LIKE '%${ARAP}_paid%'|;
 
1487   $form->{PR} = selectall_hashref_query($form, $dbh, $query);
 
1491   $main::lxdebug->leave_sub();
 
1495   $main::lxdebug->enter_sub();
 
1497   my ($self, $myconfig, $form) = @_;
 
1499   # connect to database, turn AutoCommit off
 
1500   my $dbh = $form->dbconnect_noauto($myconfig);
 
1504   if ($form->{db} eq 'ar') {
 
1505     $table = 'customer';
 
1517   if ($form->{department_id}) {
 
1518     $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
1519     $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
 
1522   if ($form->{fromdate}) {
 
1523     $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
 
1525   if ($form->{todate}) {
 
1526     $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
 
1528   if (!$form->{fx_transaction}) {
 
1529     $where .= " AND ac.fx_transaction = '0'";
 
1534   if ($form->{reference}) {
 
1535     $reference = $dbh->quote('%' . $form->{reference} . '%');
 
1536     $invnumber = " AND (a.invnumber LIKE $reference)";
 
1537     $reference = " AND (g.reference LIKE $reference)";
 
1539   if ($form->{source}) {
 
1540     $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
 
1542   if ($form->{memo}) {
 
1543     $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
 
1546   my $sortorder = join(', ', qw(name invnumber ordnumber transdate source));
 
1547   $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(transdate invnumber name source memo)));
 
1549   $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
 
1550   my $sth = prepare_query($form, $dbh, $query);
 
1553       qq|SELECT c.name, a.invnumber, a.ordnumber,
 
1554            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1555            a.invoice, a.id, ac.memo, '${arap}' AS module
 
1557          JOIN $arap a ON (ac.trans_id = a.id)
 
1558          JOIN $table c ON (c.id = a.${table}_id)
 
1560          WHERE (ac.chart_id = ?)
 
1566          SELECT g.description, g.reference, NULL AS ordnumber,
 
1567            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1568            '0' as invoice, g.id, ac.memo, 'gl' AS module
 
1570          JOIN gl g ON (g.id = ac.trans_id)
 
1572          WHERE (ac.chart_id = ?)
 
1575            AND (ac.amount * $ml) > 0
 
1577          ORDER BY $sortorder|;
 
1578   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1582   # cycle through each id
 
1583   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
1584     do_statement($form, $sth, $query, $accno);
 
1585     my $ref = $sth->fetchrow_hashref();
 
1586     push(@{ $form->{PR} }, $ref);
 
1589     $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
 
1591     do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
 
1592     while (my $pr = $sth_details->fetchrow_hashref()) {
 
1593       push(@{ $form->{ $ref->{id} } }, $pr);
 
1595     $sth_details->finish();
 
1600   $main::lxdebug->leave_sub();
 
1604   $main::lxdebug->enter_sub();
 
1606   my ($self, $myconfig, $form) = @_;
 
1608   # connect to database
 
1609   my $dbh = $form->dbconnect($myconfig);
 
1611   my $last_period = 0;
 
1614     qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40);
 
1616   $form->{decimalplaces} *= 1;
 
1618   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
 
1620   # if there are any compare dates
 
1621   if ($form->{fromdate} || $form->{todate}) {
 
1623     if ($form->{fromdate}) {
 
1624       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1627       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1630     $kummfromdate = $form->{comparefromdate};
 
1631     $kummtodate   = $form->{comparetodate};
 
1632     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
 
1635   @periods        = qw(jetzt kumm);
 
1636   @gesamtleistung = qw(1 2 3);
 
1637   @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 19 20);
 
1639     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
1641   foreach $key (@periods) {
 
1642     $form->{ "$key" . "gesamtleistung" } = 0;
 
1643     $form->{ "$key" . "gesamtkosten" }   = 0;
 
1645     foreach $category (@categories) {
 
1647       if (defined($form->{$category}{$key})) {
 
1648         $form->{"$key$category"} =
 
1649           $form->format_amount($myconfig,
 
1650                                $form->round_amount($form->{$category}{$key}, 2
 
1652                                $form->{decimalplaces},
 
1656     foreach $item (@gesamtleistung) {
 
1657       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
1659     foreach $item (@gesamtkosten) {
 
1660       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
1662     $form->{ "$key" . "rohertrag" } =
 
1663       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
1664     $form->{ "$key" . "betriebrohertrag" } =
 
1665       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
1666     $form->{ "$key" . "betriebsergebnis" } =
 
1667       $form->{ "$key" . "betriebrohertrag" } -
 
1668       $form->{ "$key" . "gesamtkosten" };
 
1669     $form->{ "$key" . "neutraleraufwand" } =
 
1670       $form->{30}{$key} + $form->{31}{$key};
 
1671     $form->{ "$key" . "neutralertrag" } =
 
1672       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
1673     $form->{ "$key" . "ergebnisvorsteuern" } =
 
1674       $form->{ "$key" . "betriebsergebnis" } -
 
1675       $form->{ "$key" . "neutraleraufwand" } +
 
1676       $form->{ "$key" . "neutralertrag" };
 
1677     $form->{ "$key" . "ergebnis" } =
 
1678       $form->{ "$key" . "ergebnisvorsteuern" } + $form->{35}{$key};
 
1680     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
1681       foreach $category (@categories) {
 
1682         if (defined($form->{$category}{$key})) {
 
1683           $form->{ "$key" . "gl" . "$category" } =
 
1684             $form->format_amount(
 
1686                                $form->round_amount(
 
1687                                  ($form->{$category}{$key} /
 
1688                                     $form->{ "$key" . "gesamtleistung" } * 100
 
1690                                  $form->{decimalplaces}
 
1692                                $form->{decimalplaces},
 
1696       foreach $item (@ergebnisse) {
 
1697         $form->{ "$key" . "gl" . "$item" } =
 
1698           $form->format_amount($myconfig,
 
1699                                $form->round_amount(
 
1700                                  ( $form->{ "$key" . "$item" } /
 
1701                                      $form->{ "$key" . "gesamtleistung" } * 100
 
1703                                  $form->{decimalplaces}
 
1705                                $form->{decimalplaces},
 
1710     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
1711       foreach $category (@categories) {
 
1712         if (defined($form->{$category}{$key})) {
 
1713           $form->{ "$key" . "gk" . "$category" } =
 
1714             $form->format_amount($myconfig,
 
1715                                  $form->round_amount(
 
1716                                    ($form->{$category}{$key} /
 
1717                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1719                                    $form->{decimalplaces}
 
1721                                  $form->{decimalplaces},
 
1725       foreach $item (@ergebnisse) {
 
1726         $form->{ "$key" . "gk" . "$item" } =
 
1727           $form->format_amount($myconfig,
 
1728                                $form->round_amount(
 
1729                                    ($form->{ "$key" . "$item" } /
 
1730                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1732                                    $form->{decimalplaces}
 
1734                                $form->{decimalplaces},
 
1739     if ($form->{10}{$key} > 0) {
 
1740       foreach $category (@categories) {
 
1741         if (defined($form->{$category}{$key})) {
 
1742           $form->{ "$key" . "pk" . "$category" } =
 
1743             $form->format_amount(
 
1745                         $form->round_amount(
 
1746                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
1747                           $form->{decimalplaces}
 
1749                         $form->{decimalplaces},
 
1753       foreach $item (@ergebnisse) {
 
1754         $form->{ "$key" . "pk" . "$item" } =
 
1755           $form->format_amount($myconfig,
 
1756                                $form->round_amount(
 
1757                                                 ($form->{ "$key" . "$item" } /
 
1758                                                    $form->{10}{$key} * 100
 
1760                                                 $form->{decimalplaces}
 
1762                                $form->{decimalplaces},
 
1767     if ($form->{4}{$key} > 0) {
 
1768       foreach $category (@categories) {
 
1769         if (defined($form->{$category}{$key})) {
 
1770           $form->{ "$key" . "auf" . "$category" } =
 
1771             $form->format_amount(
 
1773                          $form->round_amount(
 
1774                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
1775                            $form->{decimalplaces}
 
1777                          $form->{decimalplaces},
 
1781       foreach $item (@ergebnisse) {
 
1782         $form->{ "$key" . "auf" . "$item" } =
 
1783           $form->format_amount($myconfig,
 
1784                                $form->round_amount(
 
1785                                                 ($form->{ "$key" . "$item" } /
 
1786                                                    $form->{4}{$key} * 100
 
1788                                                 $form->{decimalplaces}
 
1790                                $form->{decimalplaces},
 
1795     foreach $item (@ergebnisse) {
 
1796       $form->{ "$key" . "$item" } =
 
1797         $form->format_amount($myconfig,
 
1798                              $form->round_amount($form->{ "$key" . "$item" },
 
1799                                                  $form->{decimalplaces}
 
1801                              $form->{decimalplaces},
 
1808   $main::lxdebug->leave_sub();
 
1812   $main::lxdebug->enter_sub();
 
1814   my ($self, $myconfig, $form) = @_;
 
1816   # connect to database
 
1817   my $dbh = $form->dbconnect($myconfig);
 
1819   my $last_period     = 0;
 
1820   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
 
1821     96 66 43 45 53 62 65 67);
 
1822   my @categories_euro = qw(48 51 86 91 97 93 94);
 
1823   $form->{decimalplaces} *= 1;
 
1825   foreach $item (@categories_cent) {
 
1826     $form->{"$item"} = 0;
 
1828   foreach $item (@categories_euro) {
 
1829     $form->{"$item"} = 0;
 
1832   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
 
1834   #   foreach $item (@categories_cent) {
 
1835   #     if ($form->{$item}{"jetzt"} > 0) {
 
1836   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1837   #             delete $form->{$item}{"jetzt"};
 
1840   #   foreach $item (@categories_euro) {
 
1841   #     if ($form->{$item}{"jetzt"} > 0) {
 
1842   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1843   #             delete $form->{$item}{"jetzt"};
 
1844   #     }  foreach $item (@categories_cent) {
 
1845   #     if ($form->{$item}{"jetzt"} > 0) {
 
1846   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1847   #             delete $form->{$item}{"jetzt"};
 
1850   #   foreach $item (@categories_euro) {
 
1851   #     if ($form->{$item}{"jetzt"} > 0) {
 
1852   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1853   #             delete $form->{$item}{"jetzt"};
 
1860   # Berechnung der USTVA Formularfelder
 
1862   $form->{"51r"} = $form->{"511"};
 
1863   $form->{"86r"} = $form->{"861"};
 
1864   $form->{"97r"} = $form->{"971"};
 
1865   $form->{"93r"} = $form->{"931"};
 
1867   #$form->{"96"}  = $form->{"94"} * 0.16;
 
1869     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
 
1871   $form->{"45"} = $form->{"43"};
 
1872   $form->{"53"} = $form->{"43"};
 
1873   $form->{"62"} = $form->{"43"} - $form->{"66"};
 
1874   $form->{"65"} = $form->{"43"} - $form->{"66"};
 
1875   $form->{"67"} = $form->{"43"} - $form->{"66"};
 
1877   foreach $item (@categories_cent) {
 
1879       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
 
1883   foreach $item (@categories_euro) {
 
1885       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
 
1891   $main::lxdebug->leave_sub();
 
1894 sub income_statement {
 
1895   $main::lxdebug->enter_sub();
 
1897   my ($self, $myconfig, $form) = @_;
 
1899   # connect to database
 
1900   my $dbh = $form->dbconnect($myconfig);
 
1902   my $last_period          = 0;
 
1903   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
1904   my @categories_ausgaben  =
 
1905     qw(8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
 
1907   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
1909   $form->{decimalplaces} *= 1;
 
1911   foreach $item (@categories_einnahmen) {
 
1914   foreach $item (@categories_ausgaben) {
 
1918   foreach $item (@ergebnisse) {
 
1922   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
1925   foreach $item (@categories_einnahmen) {
 
1926     $form->{"eur${item}"} =
 
1927       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1928     $form->{"sumeura"} += $form->{$item};
 
1930   foreach $item (@categories_ausgaben) {
 
1931     $form->{"eur${item}"} =
 
1932       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1933     $form->{"sumeurb"} += $form->{$item};
 
1936   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
1938   foreach $item (@ergebnisse) {
 
1940       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1942   $main::lxdebug->leave_sub();