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 #======================================================================
 
  38   $main::lxdebug->enter_sub();
 
  40   my ($self, $myconfig, $form) = @_;
 
  43   my $dbh = $form->dbconnect($myconfig);
 
  46   my @categories  = qw(A C L Q);
 
  48   # if there are any dates construct a where
 
  49   if ($form->{asofdate}) {
 
  51     $form->{this_period} = "$form->{asofdate}";
 
  52     $form->{period}      = "$form->{asofdate}";
 
  56   $form->{decimalplaces} *= 1;
 
  58   &get_accounts($dbh, $last_period, "", $form->{asofdate}, $form,
 
  61   # if there are any compare dates
 
  62   if ($form->{compareasofdate}) {
 
  65     &get_accounts($dbh, $last_period, "", $form->{compareasofdate},
 
  68     $form->{last_period} = "$form->{compareasofdate}";
 
  75   # now we got $form->{A}{accno}{ }    assets
 
  76   # and $form->{L}{accno}{ }           liabilities
 
  77   # and $form->{Q}{accno}{ }           equity
 
  78   # build asset accounts
 
  84                  'A' => { 'label'  => 'asset',
 
  88                  'L' => { 'label'  => 'liability',
 
  89                           'labels' => 'liabilities',
 
  92                  'Q' => { 'label'  => 'equity',
 
  97   foreach $category (grep { !/C/ } @categories) {
 
  99     foreach $key (sort keys %{ $form->{$category} }) {
 
 101       $str = ($form->{l_heading}) ? $form->{padding} : "";
 
 103       if ($form->{$category}{$key}{charttype} eq "A") {
 
 106           ? "$form->{$category}{$key}{accno} - $form->{$category}{$key}{description}"
 
 107           : "$form->{$category}{$key}{description}";
 
 109       if ($form->{$category}{$key}{charttype} eq "H") {
 
 110         if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 112           push(@{ $form->{"$account{$category}{label}_account"} },
 
 113                "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
 
 115           push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 116                $form->format_amount(
 
 118                         $account{$category}{subthis} * $account{$category}{ml},
 
 119                         $form->{decimalplaces}, $dash
 
 123             push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 124                  $form->format_amount(
 
 126                         $account{$category}{sublast} * $account{$category}{ml},
 
 127                         $form->{decimalplaces}, $dash
 
 133           "$form->{bold}$form->{$category}{$key}{description}$form->{endbold}";
 
 135         $account{$category}{subthis}        = $form->{$category}{$key}{this};
 
 136         $account{$category}{sublast}        = $form->{$category}{$key}{last};
 
 137         $account{$category}{subdescription} =
 
 138           $form->{$category}{$key}{description};
 
 139         $account{$category}{subtotal} = 1;
 
 141         $form->{$category}{$key}{this} = 0;
 
 142         $form->{$category}{$key}{last} = 0;
 
 144         next unless $form->{l_heading};
 
 149       # push description onto array
 
 150       push(@{ $form->{"$account{$category}{label}_account"} }, $str);
 
 152       if ($form->{$category}{$key}{charttype} eq 'A') {
 
 153         $form->{"total_$account{$category}{labels}_this_period"} +=
 
 154           $form->{$category}{$key}{this} * $account{$category}{ml};
 
 158       push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 159            $form->format_amount(
 
 161                       $form->{$category}{$key}{this} * $account{$category}{ml},
 
 162                       $form->{decimalplaces}, $dash
 
 166         $form->{"total_$account{$category}{labels}_last_period"} +=
 
 167           $form->{$category}{$key}{last} * $account{$category}{ml};
 
 169         push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 170              $form->format_amount(
 
 172                       $form->{$category}{$key}{last} * $account{$category}{ml},
 
 173                       $form->{decimalplaces}, $dash
 
 178     $str = ($form->{l_heading}) ? $form->{padding} : "";
 
 179     if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 180       push(@{ $form->{"$account{$category}{label}_account"} },
 
 181            "$str$form->{bold}$account{$category}{subdescription}$form->{endbold}"
 
 183       push(@{ $form->{"$account{$category}{label}_this_period"} },
 
 184            $form->format_amount(
 
 186                         $account{$category}{subthis} * $account{$category}{ml},
 
 187                         $form->{decimalplaces}, $dash
 
 191         push(@{ $form->{"$account{$category}{label}_last_period"} },
 
 192              $form->format_amount(
 
 194                         $account{$category}{sublast} * $account{$category}{ml},
 
 195                         $form->{decimalplaces}, $dash
 
 202   # totals for assets, liabilities
 
 203   $form->{total_assets_this_period} =
 
 204     $form->round_amount($form->{total_assets_this_period},
 
 205                         $form->{decimalplaces});
 
 206   $form->{total_liabilities_this_period} =
 
 207     $form->round_amount($form->{total_liabilities_this_period},
 
 208                         $form->{decimalplaces});
 
 209   $form->{total_equity_this_period} =
 
 210     $form->round_amount($form->{total_equity_this_period},
 
 211                         $form->{decimalplaces});
 
 214   $form->{earnings_this_period} =
 
 215     $form->{total_assets_this_period} -
 
 216     $form->{total_liabilities_this_period} - $form->{total_equity_this_period};
 
 218   push(@{ $form->{equity_this_period} },
 
 219        $form->format_amount($myconfig,
 
 220                             $form->{earnings_this_period},
 
 221                             $form->{decimalplaces}, "- "
 
 224   $form->{total_equity_this_period} =
 
 226              $form->{total_equity_this_period} + $form->{earnings_this_period},
 
 227              $form->{decimalplaces});
 
 229   # add liability + equity
 
 230   $form->{total_this_period} =
 
 231     $form->format_amount(
 
 233     $form->{total_liabilities_this_period} + $form->{total_equity_this_period},
 
 234     $form->{decimalplaces},
 
 239     # totals for assets, liabilities
 
 240     $form->{total_assets_last_period} =
 
 241       $form->round_amount($form->{total_assets_last_period},
 
 242                           $form->{decimalplaces});
 
 243     $form->{total_liabilities_last_period} =
 
 244       $form->round_amount($form->{total_liabilities_last_period},
 
 245                           $form->{decimalplaces});
 
 246     $form->{total_equity_last_period} =
 
 247       $form->round_amount($form->{total_equity_last_period},
 
 248                           $form->{decimalplaces});
 
 250     # calculate retained earnings
 
 251     $form->{earnings_last_period} =
 
 252       $form->{total_assets_last_period} -
 
 253       $form->{total_liabilities_last_period} -
 
 254       $form->{total_equity_last_period};
 
 256     push(@{ $form->{equity_last_period} },
 
 257          $form->format_amount($myconfig,
 
 258                               $form->{earnings_last_period},
 
 259                               $form->{decimalplaces}, "- "
 
 262     $form->{total_equity_last_period} =
 
 264              $form->{total_equity_last_period} + $form->{earnings_last_period},
 
 265              $form->{decimalplaces});
 
 267     # add liability + equity
 
 268     $form->{total_last_period} =
 
 269       $form->format_amount($myconfig,
 
 270                            $form->{total_liabilities_last_period} +
 
 271                              $form->{total_equity_last_period},
 
 272                            $form->{decimalplaces},
 
 277   $form->{total_liabilities_last_period} =
 
 278     $form->format_amount($myconfig,
 
 279                          $form->{total_liabilities_last_period},
 
 280                          $form->{decimalplaces}, "- ")
 
 281     if ($form->{total_liabilities_last_period} != 0);
 
 283   $form->{total_equity_last_period} =
 
 284     $form->format_amount($myconfig,
 
 285                          $form->{total_equity_last_period},
 
 286                          $form->{decimalplaces}, "- ")
 
 287     if ($form->{total_equity_last_period} != 0);
 
 289   $form->{total_assets_last_period} =
 
 290     $form->format_amount($myconfig,
 
 291                          $form->{total_assets_last_period},
 
 292                          $form->{decimalplaces}, "- ")
 
 293     if ($form->{total_assets_last_period} != 0);
 
 295   $form->{total_assets_this_period} =
 
 296     $form->format_amount($myconfig,
 
 297                          $form->{total_assets_this_period},
 
 298                          $form->{decimalplaces}, "- ");
 
 300   $form->{total_liabilities_this_period} =
 
 301     $form->format_amount($myconfig,
 
 302                          $form->{total_liabilities_this_period},
 
 303                          $form->{decimalplaces}, "- ");
 
 305   $form->{total_equity_this_period} =
 
 306     $form->format_amount($myconfig,
 
 307                          $form->{total_equity_this_period},
 
 308                          $form->{decimalplaces}, "- ");
 
 310   $main::lxdebug->leave_sub();
 
 314   $main::lxdebug->enter_sub();
 
 316   my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
 
 318   my ($null, $department_id) = split /--/, $form->{department};
 
 329   my $category = "AND (";
 
 330   foreach $item (@{$categories}) {
 
 331     $category .= qq|c.category = '$item' OR |;
 
 333   $category =~ s/OR $/\)/;
 
 336   $query = qq|SELECT c.accno, c.description, c.category
 
 338               WHERE c.charttype = 'H'
 
 342   if ($form->{accounttype} eq 'gifi') {
 
 343     $query = qq|SELECT g.accno, g.description, c.category
 
 345                 JOIN chart c ON (c.gifi_accno = g.accno)
 
 346                 WHERE c.charttype = 'H'
 
 351   $sth = $dbh->prepare($query);
 
 352   $sth->execute || $form->dberror($query);
 
 354   my @headingaccounts = ();
 
 355   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 356     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 357       "$ref->{description}";
 
 358     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
 
 359     $form->{ $ref->{category} }{ $ref->{accno} }{accno}     = $ref->{accno};
 
 361     push @headingaccounts, $ref->{accno};
 
 367     if ($form->{method} eq 'cash') {
 
 368       $subwhere .= " AND transdate >= '$fromdate'";
 
 369       $glwhere = " AND ac.transdate >= '$fromdate'";
 
 371       $where .= " AND ac.transdate >= '$fromdate'";
 
 376     $where    .= " AND ac.transdate <= '$todate'";
 
 377     $subwhere .= " AND transdate <= '$todate'";
 
 380   if ($department_id) {
 
 382                JOIN department t ON (a.department_id = t.id)
 
 385                AND t.id = $department_id
 
 389   if ($form->{project_id}) {
 
 391                  AND ac.project_id = $form->{project_id}
 
 395   if ($form->{accounttype} eq 'gifi') {
 
 397     if ($form->{method} eq 'cash') {
 
 401                  SELECT g.accno, sum(ac.amount) AS amount,
 
 402                  g.description, c.category
 
 404                  JOIN chart c ON (c.id = ac.chart_id)
 
 405                  JOIN ar a ON (a.id = ac.trans_id)
 
 406                  JOIN gifi g ON (g.accno = c.gifi_accno)
 
 415                      JOIN chart ON (chart_id = id)
 
 416                      WHERE link LIKE '%AR_paid%'
 
 420                  GROUP BY g.accno, g.description, c.category
 
 424                  SELECT '' AS accno, SUM(ac.amount) AS amount,
 
 425                  '' AS description, c.category
 
 427                  JOIN chart c ON (c.id = ac.chart_id)
 
 428                  JOIN ar a ON (a.id = ac.trans_id)
 
 433                  AND c.gifi_accno = ''
 
 438                      JOIN chart ON (chart_id = id)
 
 439                      WHERE link LIKE '%AR_paid%'
 
 447                  SELECT g.accno, sum(ac.amount) AS amount,
 
 448                  g.description, c.category
 
 450                  JOIN chart c ON (c.id = ac.chart_id)
 
 451                  JOIN ap a ON (a.id = ac.trans_id)
 
 452                  JOIN gifi g ON (g.accno = c.gifi_accno)
 
 461                      JOIN chart ON (chart_id = id)
 
 462                      WHERE link LIKE '%AP_paid%'
 
 466                  GROUP BY g.accno, g.description, c.category
 
 470                  SELECT '' AS accno, SUM(ac.amount) AS amount,
 
 471                  '' AS description, c.category
 
 473                  JOIN chart c ON (c.id = ac.chart_id)
 
 474                  JOIN ap a ON (a.id = ac.trans_id)
 
 479                  AND c.gifi_accno = ''
 
 484                      JOIN chart ON (chart_id = id)
 
 485                      WHERE link LIKE '%AP_paid%'
 
 495                  SELECT g.accno, sum(ac.amount) AS amount,
 
 496                  g.description, c.category
 
 498                  JOIN chart c ON (c.id = ac.chart_id)
 
 499                  JOIN gifi g ON (g.accno = c.gifi_accno)
 
 500                  JOIN gl a ON (a.id = ac.trans_id)
 
 506                  AND NOT (c.link = 'AR' OR c.link = 'AP')
 
 508                  GROUP BY g.accno, g.description, c.category
 
 512                  SELECT '' AS accno, SUM(ac.amount) AS amount,
 
 513                  '' AS description, c.category
 
 515                  JOIN chart c ON (c.id = ac.chart_id)
 
 516                  JOIN gl a ON (a.id = ac.trans_id)
 
 522                  AND c.gifi_accno = ''
 
 523                  AND NOT (c.link = 'AR' OR c.link = 'AP')
 
 528       if ($form->{project_id}) {
 
 534                  SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
 
 535                  g.description AS description, c.category
 
 537                  JOIN ar a ON (a.id = ac.trans_id)
 
 538                  JOIN parts p ON (ac.parts_id = p.id)
 
 539                  JOIN chart c on (p.income_accno_id = c.id)
 
 540                  JOIN gifi g ON (g.accno = c.gifi_accno)
 
 542         -- use transdate from subwhere
 
 543                  WHERE 1 = 1 $subwhere
 
 550                      JOIN chart ON (chart_id = id)
 
 551                      WHERE link LIKE '%AR_paid%'
 
 555                  GROUP BY g.accno, g.description, c.category
 
 559                  SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
 
 560                  g.description AS description, c.category
 
 562                  JOIN ap a ON (a.id = ac.trans_id)
 
 563                  JOIN parts p ON (ac.parts_id = p.id)
 
 564                  JOIN chart c on (p.expense_accno_id = c.id)
 
 565                  JOIN gifi g ON (g.accno = c.gifi_accno)
 
 567                  WHERE 1 = 1 $subwhere
 
 574                      JOIN chart ON (chart_id = id)
 
 575                      WHERE link LIKE '%AP_paid%'
 
 579                  GROUP BY g.accno, g.description, c.category
 
 585       if ($department_id) {
 
 587               JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
 590                AND t.department_id = $department_id
 
 597               SELECT g.accno, SUM(ac.amount) AS amount,
 
 598               g.description, c.category
 
 600               JOIN chart c ON (c.id = ac.chart_id)
 
 601               JOIN gifi g ON (c.gifi_accno = g.accno)
 
 607               GROUP BY g.accno, g.description, c.category
 
 611               SELECT '' AS accno, SUM(ac.amount) AS amount,
 
 612               '' AS description, c.category
 
 614               JOIN chart c ON (c.id = ac.chart_id)
 
 619               AND c.gifi_accno = ''
 
 624       if ($form->{project_id}) {
 
 630                  SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
 
 631                  g.description AS description, c.category
 
 633                  JOIN ar a ON (a.id = ac.trans_id)
 
 634                  JOIN parts p ON (ac.parts_id = p.id)
 
 635                  JOIN chart c on (p.income_accno_id = c.id)
 
 636                  JOIN gifi g ON (c.gifi_accno = g.accno)
 
 638         -- use transdate from subwhere
 
 639                  WHERE 1 = 1 $subwhere
 
 643                  GROUP BY g.accno, g.description, c.category
 
 647                  SELECT g.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
 
 648                  g.description AS description, c.category
 
 650                  JOIN ap a ON (a.id = ac.trans_id)
 
 651                  JOIN parts p ON (ac.parts_id = p.id)
 
 652                  JOIN chart c on (p.expense_accno_id = c.id)
 
 653                  JOIN gifi g ON (c.gifi_accno = g.accno)
 
 655                  WHERE 1 = 1 $subwhere
 
 659                  GROUP BY g.accno, g.description, c.category
 
 665   } else {    # standard account
 
 667     if ($form->{method} eq 'cash') {
 
 671                  SELECT c.accno, sum(ac.amount) AS amount,
 
 672                  c.description, c.category
 
 674                  JOIN chart c ON (c.id = ac.chart_id)
 
 675                  JOIN ar a ON (a.id = ac.trans_id)
 
 684                      JOIN chart ON (chart_id = id)
 
 685                      WHERE link LIKE '%AR_paid%'
 
 690                  GROUP BY c.accno, c.description, c.category
 
 694                  SELECT c.accno, sum(ac.amount) AS amount,
 
 695                  c.description, c.category
 
 697                  JOIN chart c ON (c.id = ac.chart_id)
 
 698                  JOIN ap a ON (a.id = ac.trans_id)
 
 707                      JOIN chart ON (chart_id = id)
 
 708                      WHERE link LIKE '%AP_paid%'
 
 713                  GROUP BY c.accno, c.description, c.category
 
 717                  SELECT c.accno, sum(ac.amount) AS amount,
 
 718                  c.description, c.category
 
 720                  JOIN chart c ON (c.id = ac.chart_id)
 
 721                  JOIN gl a ON (a.id = ac.trans_id)
 
 727                  AND NOT (c.link = 'AR' OR c.link = 'AP')
 
 729                  GROUP BY c.accno, c.description, c.category
 
 732       if ($form->{project_id}) {
 
 738                  SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
 
 739                  c.description AS description, c.category
 
 741                  JOIN ar a ON (a.id = ac.trans_id)
 
 742                  JOIN parts p ON (ac.parts_id = p.id)
 
 743                  JOIN chart c on (p.income_accno_id = c.id)
 
 745         -- use transdate from subwhere
 
 746                  WHERE 1 = 1 $subwhere
 
 753                      JOIN chart ON (chart_id = id)
 
 754                      WHERE link LIKE '%AR_paid%'
 
 759                  GROUP BY c.accno, c.description, c.category
 
 763                  SELECT c.accno AS accno, SUM(ac.sellprice) AS amount,
 
 764                  c.description AS description, c.category
 
 766                  JOIN ap a ON (a.id = ac.trans_id)
 
 767                  JOIN parts p ON (ac.parts_id = p.id)
 
 768                  JOIN chart c on (p.expense_accno_id = c.id)
 
 770                  WHERE 1 = 1 $subwhere
 
 777                      JOIN chart ON (chart_id = id)
 
 778                      WHERE link LIKE '%AP_paid%'
 
 783                  GROUP BY c.accno, c.description, c.category
 
 789       if ($department_id) {
 
 791               JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
 794                AND t.department_id = $department_id
 
 800                  SELECT c.accno, sum(ac.amount) AS amount,
 
 801                  c.description, c.category
 
 803                  JOIN chart c ON (c.id = ac.chart_id)
 
 809                  GROUP BY c.accno, c.description, c.category
 
 812       if ($form->{project_id}) {
 
 818                  SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount,
 
 819                  c.description AS description, c.category
 
 821                  JOIN ar a ON (a.id = ac.trans_id)
 
 822                  JOIN parts p ON (ac.parts_id = p.id)
 
 823                  JOIN chart c on (p.income_accno_id = c.id)
 
 825         -- use transdate from subwhere
 
 826                  WHERE 1 = 1 $subwhere
 
 830                  GROUP BY c.accno, c.description, c.category
 
 834                  SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount,
 
 835                  c.description AS description, c.category
 
 837                  JOIN ap a ON (a.id = ac.trans_id)
 
 838                  JOIN parts p ON (ac.parts_id = p.id)
 
 839                  JOIN chart c on (p.expense_accno_id = c.id)
 
 841                  WHERE 1 = 1 $subwhere
 
 845                  GROUP BY c.accno, c.description, c.category
 
 856   my $sth = $dbh->prepare($query);
 
 857   $sth->execute || $form->dberror($query);
 
 859   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 861     if ($ref->{category} eq 'C') {
 
 862       $ref->{category} = 'A';
 
 865     # get last heading account
 
 866     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
 870         $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
 
 872         $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
 
 876     $form->{ $ref->{category} }{ $ref->{accno} }{accno}       = $ref->{accno};
 
 877     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 879     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
 
 882       $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
 
 884       $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
 
 889   # remove accounts with zero balance
 
 890   foreach $category (@{$categories}) {
 
 891     foreach $accno (keys %{ $form->{$category} }) {
 
 892       $form->{$category}{$accno}{last} =
 
 893         $form->round_amount($form->{$category}{$accno}{last},
 
 894                             $form->{decimalplaces});
 
 895       $form->{$category}{$accno}{this} =
 
 896         $form->round_amount($form->{$category}{$accno}{this},
 
 897                             $form->{decimalplaces});
 
 899       delete $form->{$category}{$accno}
 
 900         if (   $form->{$category}{$accno}{this} == 0
 
 901             && $form->{$category}{$accno}{last} == 0);
 
 905   $main::lxdebug->leave_sub();
 
 909   $main::lxdebug->enter_sub();
 
 911   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 913   my ($null, $department_id) = split /--/, $form->{department};
 
 926     if ($form->{method} eq 'cash') {
 
 927       $subwhere .= " AND transdate >= '$fromdate'";
 
 928       $glwhere = " AND ac.transdate >= '$fromdate'";
 
 929       $prwhere = " AND ar.transdate >= '$fromdate'";
 
 931       $where .= " AND ac.transdate >= '$fromdate'";
 
 936     $where    .= " AND ac.transdate <= '$todate'";
 
 937     $subwhere .= " AND transdate <= '$todate'";
 
 938     $prwhere  .= " AND ar.transdate <= '$todate'";
 
 941   if ($department_id) {
 
 943                JOIN department t ON (a.department_id = t.id)
 
 946                AND t.id = $department_id
 
 950   if ($form->{project_id}) {
 
 952                  AND ac.project_id = $form->{project_id}
 
 956   if ($form->{method} eq 'cash') {
 
 960                  SELECT sum(ac.amount) AS amount,
 
 963                  JOIN chart c ON (c.id = ac.chart_id)
 
 964                  JOIN ar a ON (a.id = ac.trans_id)
 
 972                      JOIN chart ON (chart_id = id)
 
 973                      WHERE link LIKE '%AR_paid%'
 
 982                  SELECT sum(ac.amount) AS amount,
 
 985                  JOIN chart c ON (c.id = ac.chart_id)
 
 986                  JOIN ap a ON (a.id = ac.trans_id)
 
 994                      JOIN chart ON (chart_id = id)
 
 995                      WHERE link LIKE '%AP_paid%'
 
1000                  GROUP BY c.$category
 
1004                  SELECT sum(ac.amount) AS amount,
 
1007                  JOIN chart c ON (c.id = ac.chart_id)
 
1008                  JOIN gl a ON (a.id = ac.trans_id)
 
1013                  AND NOT (c.link = 'AR' OR c.link = 'AP')
 
1015                  GROUP BY c.$category
 
1018     if ($form->{project_id}) {
 
1024                  SELECT SUM(ac.sellprice * ac.qty) AS amount,
 
1027                  JOIN ar a ON (a.id = ac.trans_id)
 
1028                  JOIN parts p ON (ac.parts_id = p.id)
 
1029                  JOIN chart c on (p.income_accno_id = c.id)
 
1031                  WHERE 1 = 1 $prwhere
 
1032                  AND c.category = 'I'
 
1038                      JOIN chart ON (chart_id = id)
 
1039                      WHERE link LIKE '%AR_paid%'
 
1044                  GROUP BY c.$category
 
1048                  SELECT SUM(ac.sellprice) AS amount,
 
1051                  JOIN ap a ON (a.id = ac.trans_id)
 
1052                  JOIN parts p ON (ac.parts_id = p.id)
 
1053                  JOIN chart c on (p.expense_accno_id = c.id)
 
1055                  WHERE 1 = 1 $prwhere
 
1056                  AND c.category = 'E'
 
1062                      JOIN chart ON (chart_id = id)
 
1063                      WHERE link LIKE '%AP_paid%'
 
1068                  GROUP BY c.$category
 
1074     if ($department_id) {
 
1076               JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
1079                AND t.department_id = $department_id
 
1085                  SELECT sum(ac.amount) AS amount,
 
1088                  JOIN chart c ON (c.id = ac.chart_id)
 
1093                  GROUP BY c.$category
 
1096     if ($form->{project_id}) {
 
1102                  SELECT SUM(ac.sellprice * ac.qty) AS amount,
 
1105                  JOIN ar a ON (a.id = ac.trans_id)
 
1106                  JOIN parts p ON (ac.parts_id = p.id)
 
1107                  JOIN chart c on (p.income_accno_id = c.id)
 
1109                  WHERE 1 = 1 $prwhere
 
1110                  AND c.category = 'I'
 
1113                  GROUP BY c.$category
 
1117                  SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount,
 
1120                  JOIN ap a ON (a.id = ac.trans_id)
 
1121                  JOIN parts p ON (ac.parts_id = p.id)
 
1122                  JOIN chart c on (p.expense_accno_id = c.id)
 
1124                  WHERE 1 = 1 $prwhere
 
1125                  AND c.category = 'E'
 
1128                  GROUP BY c.$category
 
1139   my $sth = $dbh->prepare($query);
 
1140   $sth->execute || $form->dberror($query);
 
1142   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1143     if ($ref->{amount} < 0) {
 
1144       $ref->{amount} *= -1;
 
1146     if ($category eq "pos_bwa") {
 
1148         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
1150         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
1153       $form->{ $ref->{$category} } += $ref->{amount};
 
1158   $main::lxdebug->leave_sub();
 
1162   $main::lxdebug->enter_sub();
 
1164   my ($self, $myconfig, $form) = @_;
 
1166   my $dbh = $form->dbconnect($myconfig);
 
1168   my ($query, $sth, $ref);
 
1171   my ($null, $department_id) = split /--/, $form->{department};
 
1172   my @headingaccounts = ();
 
1177   my $where    = "1 = 1";
 
1178   my $invwhere = $where;
 
1180   if ($department_id) {
 
1182                 JOIN dpt_trans t ON (ac.trans_id = t.trans_id)
 
1185                 AND t.department_id = $department_id
 
1189   # project_id only applies to getting transactions
 
1190   # it has nothing to do with a trial balance
 
1191   # but we use the same function to collect information
 
1193   if ($form->{project_id}) {
 
1195                 AND ac.project_id = $form->{project_id}
 
1199   # get beginning balances
 
1200   if ($form->{fromdate}) {
 
1202     if ($form->{accounttype} eq 'gifi') {
 
1204       $query = qq|SELECT g.accno, c.category, SUM(ac.amount) AS amount,
 
1207                   JOIN chart c ON (ac.chart_id = c.id)
 
1208                   JOIN gifi g ON (c.gifi_accno = g.accno)
 
1210                   WHERE ac.transdate < '$form->{fromdate}'
 
1213                   GROUP BY g.accno, c.category, g.description
 
1218       $query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount,
 
1221                   JOIN chart c ON (ac.chart_id = c.id)
 
1223                   WHERE ac.transdate < '$form->{fromdate}'
 
1226                   GROUP BY c.accno, c.category, c.description
 
1231     $sth = $dbh->prepare($query);
 
1232     $sth->execute || $form->dberror($query);
 
1234     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1235       $balance{ $ref->{accno} } = $ref->{amount};
 
1237       if ($ref->{amount} != 0 && $form->{all_accounts}) {
 
1238         $trb{ $ref->{accno} }{description} = $ref->{description};
 
1239         $trb{ $ref->{accno} }{charttype}   = 'A';
 
1240         $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1249   $query = qq|SELECT c.accno, c.description, c.category
 
1251               WHERE c.charttype = 'H'
 
1254   if ($form->{accounttype} eq 'gifi') {
 
1255     $query = qq|SELECT g.accno, g.description, c.category
 
1257                 JOIN chart c ON (c.gifi_accno = g.accno)
 
1258                 WHERE c.charttype = 'H'
 
1262   $sth = $dbh->prepare($query);
 
1263   $sth->execute || $form->dberror($query);
 
1265   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1266     $trb{ $ref->{accno} }{description} = $ref->{description};
 
1267     $trb{ $ref->{accno} }{charttype}   = 'H';
 
1268     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1270     push @headingaccounts, $ref->{accno};
 
1277   if ($form->{fromdate} || $form->{todate}) {
 
1278     if ($form->{fromdate}) {
 
1279       $tofrom   .= " AND ac.transdate >= '$form->{fromdate}'";
 
1280       $subwhere .= " AND transdate >= '$form->{fromdate}'";
 
1281       $invwhere .= " AND a.transdate >= '$form->{fromdate}'";
 
1282       $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
 
1284     if ($form->{todate}) {
 
1285       $tofrom   .= " AND ac.transdate <= '$form->{todate}'";
 
1286       $invwhere .= " AND a.transdate <= '$form->{todate}'";
 
1287       $subwhere .= " AND transdate <= '$form->{todate}'";
 
1288       $glwhere  .= " AND ac.transdate <= '$form->{todate}'";
 
1292     $where .= qq| AND ((ac.trans_id in (SELECT id from ar)
 
1297                      JOIN chart ON (chart_id = id)
 
1298                      WHERE link LIKE '%AR_paid%'
 
1300                    )) OR (ac.trans_id in (SELECT id from ap)
 
1305                      JOIN chart ON (chart_id = id)
 
1306                      WHERE link LIKE '%AP_paid%'
 
1308                    )) OR (ac.trans_id in (SELECT id from gl)
 
1314   if ($form->{accounttype} eq 'gifi') {
 
1316     $query = qq|SELECT g.accno, g.description, c.category,
 
1317                 SUM(ac.amount) AS amount
 
1319                 JOIN chart c ON (c.id = ac.chart_id)
 
1320                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1325                 GROUP BY g.accno, g.description, c.category
 
1328     if ($form->{project_id}) {
 
1332         -- add project transactions from invoice
 
1336                 SELECT g.accno, g.description, c.category,
 
1337                 SUM(ac.sellprice * ac.qty) AS amount
 
1339                 JOIN ar a ON (ac.trans_id = a.id)
 
1340                 JOIN parts p ON (ac.parts_id = p.id)
 
1341                 JOIN chart c ON (p.income_accno_id = c.id)
 
1342                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1347                 GROUP BY g.accno, g.description, c.category
 
1351                 SELECT g.accno, g.description, c.category,
 
1352                 SUM(ac.sellprice * ac.qty) * -1 AS amount
 
1354                 JOIN ap a ON (ac.trans_id = a.id)
 
1355                 JOIN parts p ON (ac.parts_id = p.id)
 
1356                 JOIN chart c ON (p.expense_accno_id = c.id)
 
1357                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1362                 GROUP BY g.accno, g.description, c.category
 
1371     $query = qq|SELECT c.accno, c.description, c.category,
 
1372                 SUM(ac.amount) AS amount
 
1374                 JOIN chart c ON (c.id = ac.chart_id)
 
1379                 GROUP BY c.accno, c.description, c.category
 
1382     if ($form->{project_id}) {
 
1386         -- add project transactions from invoice
 
1390                 SELECT c.accno, c.description, c.category,
 
1391                 SUM(ac.sellprice * ac.qty) AS amount
 
1393                 JOIN ar a ON (ac.trans_id = a.id)
 
1394                 JOIN parts p ON (ac.parts_id = p.id)
 
1395                 JOIN chart c ON (p.income_accno_id = c.id)
 
1400                 GROUP BY c.accno, c.description, c.category
 
1404                 SELECT c.accno, c.description, c.category,
 
1405                 SUM(ac.sellprice * ac.qty) * -1 AS amount
 
1407                 JOIN ap a ON (ac.trans_id = a.id)
 
1408                 JOIN parts p ON (ac.parts_id = p.id)
 
1409                 JOIN chart c ON (p.expense_accno_id = c.id)
 
1414                 GROUP BY c.accno, c.description, c.category
 
1423   $sth = $dbh->prepare($query);
 
1424   $sth->execute || $form->dberror($query);
 
1426   # prepare query for each account
 
1427   $query = qq|SELECT (SELECT SUM(ac.amount) * -1
 
1429               JOIN chart c ON (c.id = ac.chart_id)
 
1435               AND c.accno = ?) AS debit,
 
1437              (SELECT SUM(ac.amount)
 
1439               JOIN chart c ON (c.id = ac.chart_id)
 
1445               AND c.accno = ?) AS credit
 
1448   if ($form->{accounttype} eq 'gifi') {
 
1450     $query = qq|SELECT (SELECT SUM(ac.amount) * -1
 
1452                 JOIN chart c ON (c.id = ac.chart_id)
 
1458                 AND c.gifi_accno = ?) AS debit,
 
1460                (SELECT SUM(ac.amount)
 
1462                 JOIN chart c ON (c.id = ac.chart_id)
 
1468                 AND c.gifi_accno = ?) AS credit|;
 
1472   $drcr = $dbh->prepare($query);
 
1474   if ($form->{project_id}) {
 
1476     # prepare query for each account
 
1477     $query = qq|SELECT (SELECT SUM(ac.sellprice * ac.qty) * -1
 
1479               JOIN parts p ON (ac.parts_id = p.id)
 
1480               JOIN ap a ON (ac.trans_id = a.id)
 
1481               JOIN chart c ON (p.expense_accno_id = c.id)
 
1486               AND c.accno = ?) AS debit,
 
1488              (SELECT SUM(ac.sellprice * ac.qty)
 
1490               JOIN parts p ON (ac.parts_id = p.id)
 
1491               JOIN ar a ON (ac.trans_id = a.id)
 
1492               JOIN chart c ON (p.income_accno_id = c.id)
 
1497               AND c.accno = ?) AS credit
 
1500     $project_drcr = $dbh->prepare($query);
 
1504   # calculate the debit and credit in the period
 
1505   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1506     $trb{ $ref->{accno} }{description} = $ref->{description};
 
1507     $trb{ $ref->{accno} }{charttype}   = 'A';
 
1508     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1509     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
1513   my ($debit, $credit);
 
1515   foreach my $accno (sort keys %trb) {
 
1518     $ref->{accno} = $accno;
 
1519     map { $ref->{$_} = $trb{$accno}{$_} }
 
1520       qw(description category charttype amount);
 
1522     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1524     if ($trb{$accno}{charttype} eq 'A') {
 
1527       $drcr->execute($ref->{accno}, $ref->{accno}) || $form->dberror($query);
 
1529       ($debit, $credit) = (0, 0);
 
1530       while (($debit, $credit) = $drcr->fetchrow_array) {
 
1531         $ref->{debit}  += $debit;
 
1532         $ref->{credit} += $credit;
 
1536       if ($form->{project_id}) {
 
1539         $project_drcr->execute($ref->{accno}, $ref->{accno})
 
1540           || $form->dberror($query);
 
1542         ($debit, $credit) = (0, 0);
 
1543         while (($debit, $credit) = $project_drcr->fetchrow_array) {
 
1544           $ref->{debit}  += $debit;
 
1545           $ref->{credit} += $credit;
 
1547         $project_drcr->finish;
 
1550       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1551       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1556     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1557     $accno = pop @accno;
 
1559       $trb{$accno}{debit}  += $ref->{debit};
 
1560       $trb{$accno}{credit} += $ref->{credit};
 
1563     push @{ $form->{TB} }, $ref;
 
1569   # debits and credits for headings
 
1570   foreach $accno (@headingaccounts) {
 
1571     foreach $ref (@{ $form->{TB} }) {
 
1572       if ($accno eq $ref->{accno}) {
 
1573         $ref->{debit}  = $trb{$accno}{debit};
 
1574         $ref->{credit} = $trb{$accno}{credit};
 
1579   $main::lxdebug->leave_sub();
 
1583   $main::lxdebug->enter_sub();
 
1585   my ($self, $myconfig, $form) = @_;
 
1587   # connect to database
 
1588   my $dbh     = $form->dbconnect($myconfig);
 
1589   my $invoice = ($form->{arap} eq 'ar') ? 'is' : 'ir';
 
1591   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
 
1593   my $where = "1 = 1";
 
1596   if ($form->{"$form->{ct}_id"}) {
 
1597     $where .= qq| AND ct.id = $form->{"$form->{ct}_id"}|;
 
1599     if ($form->{ $form->{ct} }) {
 
1600       $name = $form->like(lc $form->{ $form->{ct} });
 
1601       $where .= qq| AND lower(ct.name) LIKE '$name'| if $form->{ $form->{ct} };
 
1606   if ($form->{department}) {
 
1607     ($null, $department_id) = split /--/, $form->{department};
 
1609                JOIN department d ON (a.department_id = d.id)
 
1612     $where .= qq| AND a.department_id = $department_id|;
 
1615   # select outstanding vendors or customers, depends on $ct
 
1616   my $query = qq|SELECT DISTINCT ct.id, ct.name
 
1617                  FROM $form->{ct} ct, $form->{arap} a
 
1620                  AND a.$form->{ct}_id = ct.id
 
1621                  AND a.paid != a.amount
 
1622                  AND (a.transdate <= '$form->{todate}')
 
1625   my $sth = $dbh->prepare($query);
 
1626   $sth->execute || $form->dberror;
 
1628   my $buysell = ($form->{arap} eq 'ar') ? 'buy' : 'sell';
 
1630   # for each company that has some stuff outstanding
 
1631   while (my ($id) = $sth->fetchrow_array) {
 
1635 -- between 0-30 days
 
1637         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1638         street, zipcode, city, country, contact, email,
 
1639         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1640         "invnumber", "transdate",
 
1641         (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90",
 
1642         "duedate", invoice, $form->{arap}.id,
 
1643           (SELECT $buysell FROM exchangerate
 
1644            WHERE $form->{arap}.curr = exchangerate.curr
 
1645            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1646   FROM $form->{arap}, $form->{ct}
 
1647         WHERE paid != amount
 
1648         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1649         AND $form->{ct}.id = $id
 
1651                 transdate <= (date '$form->{todate}' - interval '0 days')
 
1652                 AND transdate >= (date '$form->{todate}' - interval '30 days')
 
1657 -- between 31-60 days
 
1659         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1660         street, zipcode, city, country, contact, email,
 
1661         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1662         "invnumber", "transdate",
 
1663         0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90",
 
1664         "duedate", invoice, $form->{arap}.id,
 
1665           (SELECT $buysell FROM exchangerate
 
1666            WHERE $form->{arap}.curr = exchangerate.curr
 
1667            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1668   FROM $form->{arap}, $form->{ct}
 
1669         WHERE paid != amount
 
1670         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1671         AND $form->{ct}.id = $id
 
1673                 transdate < (date '$form->{todate}' - interval '30 days')
 
1674                 AND transdate >= (date '$form->{todate}' - interval '60 days')
 
1679 -- between 61-90 days
 
1681         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1682         street, zipcode, city, country, contact, email,
 
1683         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1684         "invnumber", "transdate",
 
1685         0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90",
 
1686         "duedate", invoice, $form->{arap}.id,
 
1687           (SELECT $buysell FROM exchangerate
 
1688            WHERE $form->{arap}.curr = exchangerate.curr
 
1689            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1690         FROM $form->{arap}, $form->{ct}
 
1691         WHERE paid != amount
 
1692         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1693         AND $form->{ct}.id = $id
 
1695                 transdate < (date '$form->{todate}' - interval '60 days')
 
1696                 AND transdate >= (date '$form->{todate}' - interval '90 days')
 
1703         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1704         street, zipcode, city, country, contact, email,
 
1705         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1706         "invnumber", "transdate",
 
1707         0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90",
 
1708         "duedate", invoice, $form->{arap}.id,
 
1709           (SELECT $buysell FROM exchangerate
 
1710            WHERE $form->{arap}.curr = exchangerate.curr
 
1711            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1712         FROM $form->{arap}, $form->{ct}
 
1713         WHERE paid != amount
 
1714         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1715         AND $form->{ct}.id = $id
 
1716         AND transdate < (date '$form->{todate}' - interval '90 days')
 
1720   ctid, transdate, invnumber
 
1724     my $sth = $dbh->prepare($query);
 
1725     $sth->execute || $form->dberror;
 
1727     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1728       $ref->{module} = ($ref->{invoice}) ? $invoice : $form->{arap};
 
1729       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1730       push @{ $form->{AG} }, $ref;
 
1742   $main::lxdebug->leave_sub();
 
1746   $main::lxdebug->enter_sub();
 
1748   my ($self, $myconfig, $form) = @_;
 
1750   # connect to database
 
1751   my $dbh = $form->dbconnect($myconfig);
 
1753   my $query = qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1755                  WHERE ct.id = $form->{"$form->{ct}_id"}|;
 
1756   my $sth = $dbh->prepare($query);
 
1757   $sth->execute || $form->dberror;
 
1759   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1760     $sth->fetchrow_array;
 
1764   $main::lxdebug->leave_sub();
 
1767 sub get_taxaccounts {
 
1768   $main::lxdebug->enter_sub();
 
1770   my ($self, $myconfig, $form) = @_;
 
1772   # connect to database
 
1773   my $dbh = $form->dbconnect($myconfig);
 
1776   my $query = qq|SELECT c.accno, c.description, t.rate
 
1778                  WHERE c.link LIKE '%CT_tax%'
 
1779                  AND c.id = t.chart_id
 
1781   my $sth = $dbh->prepare($query);
 
1782   $sth->execute || $form->dberror;
 
1785   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1786     push @{ $form->{taxaccounts} }, $ref;
 
1790   # get gifi tax accounts
 
1791   $query = qq|SELECT DISTINCT ON (g.accno) g.accno, g.description,
 
1793                  FROM gifi g, chart c, tax t
 
1794                  WHERE g.accno = c.gifi_accno
 
1795                  AND c.id = t.chart_id
 
1796                  AND c.link LIKE '%CT_tax%'
 
1797                  GROUP BY g.accno, g.description
 
1799   $sth = $dbh->prepare($query);
 
1800   $sth->execute || $form->dberror;
 
1802   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1803     push @{ $form->{gifi_taxaccounts} }, $ref;
 
1809   $main::lxdebug->leave_sub();
 
1813   $main::lxdebug->enter_sub();
 
1815   my ($self, $myconfig, $form) = @_;
 
1817   # connect to database
 
1818   my $dbh = $form->dbconnect($myconfig);
 
1820   my ($null, $department_id) = split /--/, $form->{department};
 
1823   my $where = "1 = 1";
 
1825   if ($department_id) {
 
1827                  AND a.department_id = $department_id
 
1833   if ($form->{accno}) {
 
1834     if ($form->{accno} =~ /^gifi_/) {
 
1835       ($null, $accno) = split /_/, $form->{accno};
 
1836       $rate  = $form->{"$form->{accno}_rate"};
 
1837       $accno = qq| AND ch.gifi_accno = '$accno'|;
 
1839       $accno = $form->{accno};
 
1840       $rate  = $form->{"$form->{accno}_rate"};
 
1841       $accno = qq| AND ch.accno = '$accno'|;
 
1848   if ($form->{db} eq 'ar') {
 
1849     $table = "customer";
 
1852   if ($form->{db} eq 'ap') {
 
1857   my $transdate = "a.transdate";
 
1859   if ($form->{method} eq 'cash') {
 
1860     $transdate = "a.datepaid";
 
1863       ($form->{todate}) ? $form->{todate} : $form->current_date($myconfig);
 
1870                      JOIN chart ON (chart_id = id)
 
1871                      WHERE link LIKE '%${ARAP}_paid%'
 
1872                      AND transdate <= '$todate'
 
1877   # if there are any dates construct a where
 
1878   if ($form->{fromdate} || $form->{todate}) {
 
1879     if ($form->{fromdate}) {
 
1880       $where .= " AND $transdate >= '$form->{fromdate}'";
 
1882     if ($form->{todate}) {
 
1883       $where .= " AND $transdate <= '$form->{todate}'";
 
1887   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1889   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1890   $sortorder = $form->{sort} if $form->{sort};
 
1892   $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
 
1893               a.invnumber, n.name, a.netamount,
 
1894               ac.amount * $ml AS tax
 
1896             JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1897             JOIN chart ch ON (ch.id = ac.chart_id)
 
1898             JOIN $table n ON (n.id = a.${table}_id)
 
1903               SELECT a.id, '1' AS invoice, $transdate AS transdate,
 
1904               a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1905               i.sellprice * i.qty * $rate * $ml AS tax
 
1907             JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1908             JOIN chart ch ON (ch.id = ac.chart_id)
 
1909             JOIN $table n ON (n.id = a.${table}_id)
 
1910             JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1911             JOIN invoice i ON (i.trans_id = a.id)
 
1912             JOIN partstax p ON (p.parts_id = i.parts_id)
 
1916               ORDER by $sortorder|;
 
1918   if ($form->{report} =~ /nontaxable/) {
 
1920     # only gather up non-taxable transactions
 
1921     $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
 
1922                 a.invnumber, n.name, a.netamount
 
1924               JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1925               JOIN $table n ON (n.id = a.${table}_id)
 
1928                 AND a.netamount = a.amount
 
1930                 SELECT a.id, '1' AS invoice, $transdate AS transdate,
 
1931                 a.invnumber, n.name, i.sellprice * i.qty AS netamount
 
1933               JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1934               JOIN $table n ON (n.id = a.${table}_id)
 
1935               JOIN invoice i ON (i.trans_id = a.id)
 
1939                   a.${table}_id NOT IN (
 
1940                         SELECT ${table}_id FROM ${table}tax t (${table}_id)
 
1943                         SELECT parts_id FROM partstax p (parts_id)
 
1946                 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
 
1947                 ORDER by $sortorder|;
 
1950   my $sth = $dbh->prepare($query);
 
1951   $sth->execute || $form->dberror($query);
 
1953   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1954     push @{ $form->{TR} }, $ref;
 
1960   $main::lxdebug->leave_sub();
 
1963 sub paymentaccounts {
 
1964   $main::lxdebug->enter_sub();
 
1966   my ($self, $myconfig, $form) = @_;
 
1968   # connect to database, turn AutoCommit off
 
1969   my $dbh = $form->dbconnect_noauto($myconfig);
 
1971   my $ARAP = uc $form->{db};
 
1973   # get A(R|P)_paid accounts
 
1974   my $query = qq|SELECT c.accno, c.description
 
1976                  WHERE c.link LIKE '%${ARAP}_paid%'|;
 
1977   my $sth = $dbh->prepare($query);
 
1978   $sth->execute || $form->dberror($query);
 
1980   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1981     push @{ $form->{PR} }, $ref;
 
1987   $main::lxdebug->leave_sub();
 
1991   $main::lxdebug->enter_sub();
 
1993   my ($self, $myconfig, $form) = @_;
 
1995   # connect to database, turn AutoCommit off
 
1996   my $dbh = $form->dbconnect_noauto($myconfig);
 
1999   if ($form->{db} eq 'ar') {
 
2000     $table = 'customer';
 
2003   if ($form->{db} eq 'ap') {
 
2011   if ($form->{department_id}) {
 
2013                  JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
2017                  AND t.department_id = $form->{department_id}
 
2021   if ($form->{fromdate}) {
 
2022     $where .= " AND ac.transdate >= '$form->{fromdate}'";
 
2024   if ($form->{todate}) {
 
2025     $where .= " AND ac.transdate <= '$form->{todate}'";
 
2027   if (!$form->{fx_transaction}) {
 
2028     $where .= " AND ac.fx_transaction = '0'";
 
2033   if ($form->{reference}) {
 
2034     $reference = $form->like(lc $form->{reference});
 
2035     $invnumber = " AND lower(a.invnumber) LIKE '$reference'";
 
2036     $reference = " AND lower(g.reference) LIKE '$reference'";
 
2038   if ($form->{source}) {
 
2039     my $source = $form->like(lc $form->{source});
 
2040     $where .= " AND lower(ac.source) LIKE '$source'";
 
2042   if ($form->{memo}) {
 
2043     my $memo = $form->like(lc $form->{memo});
 
2044     $where .= " AND lower(ac.memo) LIKE '$memo'";
 
2047   my $sortorder = join ', ',
 
2048     $form->sort_columns(qw(name invnumber ordnumber transdate source));
 
2049   $sortorder = $form->{sort} if $form->{sort};
 
2051   # cycle through each id
 
2052   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
2054     $query = qq|SELECT c.id, c.accno, c.description
 
2056                 WHERE c.accno = '$accno'|;
 
2057     $sth = $dbh->prepare($query);
 
2058     $sth->execute || $form->dberror($query);
 
2060     my $ref = $sth->fetchrow_hashref(NAME_lc);
 
2061     push @{ $form->{PR} }, $ref;
 
2064     $query = qq|SELECT c.name, a.invnumber, a.ordnumber,
 
2065                 ac.transdate, ac.amount * $ml AS paid, ac.source,
 
2066                 a.invoice, a.id, ac.memo, '$form->{db}' AS module
 
2068                 JOIN $form->{db} a ON (ac.trans_id = a.id)
 
2069                 JOIN $table c ON (c.id = a.${table}_id)
 
2071                 WHERE ac.chart_id = $ref->{id}
 
2076                 SELECT g.description, g.reference, NULL AS ordnumber,
 
2077                 ac.transdate, ac.amount * $ml AS paid, ac.source,
 
2078                 '0' as invoice, g.id, ac.memo, 'gl' AS module
 
2080                 JOIN gl g ON (g.id = ac.trans_id)
 
2082                 WHERE ac.chart_id = $ref->{id}
 
2085                 AND (ac.amount * $ml) > 0
 
2086                 ORDER BY $sortorder|;
 
2088     $sth = $dbh->prepare($query);
 
2089     $sth->execute || $form->dberror($query);
 
2091     while (my $pr = $sth->fetchrow_hashref(NAME_lc)) {
 
2092       push @{ $form->{ $ref->{id} } }, $pr;
 
2100   $main::lxdebug->leave_sub();
 
2104   $main::lxdebug->enter_sub();
 
2106   my ($self, $myconfig, $form) = @_;
 
2108   # connect to database
 
2109   my $dbh = $form->dbconnect($myconfig);
 
2111   my $last_period = 0;
 
2112   my $category    = "pos_bwa";
 
2114     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);
 
2116   $form->{decimalplaces} *= 1;
 
2118   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2121   # if there are any compare dates
 
2122   if ($form->{fromdate} || $form->{todate}) {
 
2124     if ($form->{fromdate}) {
 
2125       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
2128       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
2131     $kummfromdate = $form->{comparefromdate};
 
2132     $kummtodate   = $form->{comparetodate};
 
2133     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form,
 
2137   @periods        = qw(jetzt kumm);
 
2138   @gesamtleistung = qw(1 2 3);
 
2139   @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 19 20);
 
2141     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
2143   foreach $key (@periods) {
 
2144     $form->{ "$key" . "gesamtleistung" } = 0;
 
2145     $form->{ "$key" . "gesamtkosten" }   = 0;
 
2147     foreach $category (@categories) {
 
2149       if (defined($form->{$category}{$key})) {
 
2150         $form->{"$key$category"} =
 
2151           $form->format_amount($myconfig,
 
2152                                $form->round_amount($form->{$category}{$key}, 2
 
2154                                $form->{decimalplaces},
 
2158     foreach $item (@gesamtleistung) {
 
2159       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
2161     foreach $item (@gesamtkosten) {
 
2162       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
2164     $form->{ "$key" . "rohertrag" } =
 
2165       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
2166     $form->{ "$key" . "betriebrohertrag" } =
 
2167       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
2168     $form->{ "$key" . "betriebsergebnis" } =
 
2169       $form->{ "$key" . "betriebrohertrag" } -
 
2170       $form->{ "$key" . "gesamtkosten" };
 
2171     $form->{ "$key" . "neutraleraufwand" } =
 
2172       $form->{30}{$key} + $form->{31}{$key};
 
2173     $form->{ "$key" . "neutralertrag" } =
 
2174       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
2175     $form->{ "$key" . "ergebnisvorsteuern" } =
 
2176       $form->{ "$key" . "betriebsergebnis" } -
 
2177       $form->{ "$key" . "neutraleraufwand" } +
 
2178       $form->{ "$key" . "neutralertrag" };
 
2179     $form->{ "$key" . "ergebnis" } =
 
2180       $form->{ "$key" . "ergebnisvorsteuern" } + $form->{35}{$key};
 
2182     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
2183       foreach $category (@categories) {
 
2184         if (defined($form->{$category}{$key})) {
 
2185           $form->{ "$key" . "gl" . "$category" } =
 
2186             $form->format_amount(
 
2188                                $form->round_amount(
 
2189                                  ($form->{$category}{$key} /
 
2190                                     $form->{ "$key" . "gesamtleistung" } * 100
 
2192                                  $form->{decimalplaces}
 
2194                                $form->{decimalplaces},
 
2198       foreach $item (@ergebnisse) {
 
2199         $form->{ "$key" . "gl" . "$item" } =
 
2200           $form->format_amount($myconfig,
 
2201                                $form->round_amount(
 
2202                                  ( $form->{ "$key" . "$item" } /
 
2203                                      $form->{ "$key" . "gesamtleistung" } * 100
 
2205                                  $form->{decimalplaces}
 
2207                                $form->{decimalplaces},
 
2212     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
2213       foreach $category (@categories) {
 
2214         if (defined($form->{$category}{$key})) {
 
2215           $form->{ "$key" . "gk" . "$category" } =
 
2216             $form->format_amount($myconfig,
 
2217                                  $form->round_amount(
 
2218                                    ($form->{$category}{$key} /
 
2219                                       $form->{ "$key" . "gesamtkosten" } * 100
 
2221                                    $form->{decimalplaces}
 
2223                                  $form->{decimalplaces},
 
2227       foreach $item (@ergebnisse) {
 
2228         $form->{ "$key" . "gk" . "$item" } =
 
2229           $form->format_amount($myconfig,
 
2230                                $form->round_amount(
 
2231                                    ($form->{ "$key" . "$item" } /
 
2232                                       $form->{ "$key" . "gesamtkosten" } * 100
 
2234                                    $form->{decimalplaces}
 
2236                                $form->{decimalplaces},
 
2241     if ($form->{10}{$key} > 0) {
 
2242       foreach $category (@categories) {
 
2243         if (defined($form->{$category}{$key})) {
 
2244           $form->{ "$key" . "pk" . "$category" } =
 
2245             $form->format_amount(
 
2247                         $form->round_amount(
 
2248                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
2249                           $form->{decimalplaces}
 
2251                         $form->{decimalplaces},
 
2255       foreach $item (@ergebnisse) {
 
2256         $form->{ "$key" . "pk" . "$item" } =
 
2257           $form->format_amount($myconfig,
 
2258                                $form->round_amount(
 
2259                                                 ($form->{ "$key" . "$item" } /
 
2260                                                    $form->{10}{$key} * 100
 
2262                                                 $form->{decimalplaces}
 
2264                                $form->{decimalplaces},
 
2269     if ($form->{4}{$key} > 0) {
 
2270       foreach $category (@categories) {
 
2271         if (defined($form->{$category}{$key})) {
 
2272           $form->{ "$key" . "auf" . "$category" } =
 
2273             $form->format_amount(
 
2275                          $form->round_amount(
 
2276                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
2277                            $form->{decimalplaces}
 
2279                          $form->{decimalplaces},
 
2283       foreach $item (@ergebnisse) {
 
2284         $form->{ "$key" . "auf" . "$item" } =
 
2285           $form->format_amount($myconfig,
 
2286                                $form->round_amount(
 
2287                                                 ($form->{ "$key" . "$item" } /
 
2288                                                    $form->{4}{$key} * 100
 
2290                                                 $form->{decimalplaces}
 
2292                                $form->{decimalplaces},
 
2297     foreach $item (@ergebnisse) {
 
2298       $form->{ "$key" . "$item" } =
 
2299         $form->format_amount($myconfig,
 
2300                              $form->round_amount($form->{ "$key" . "$item" },
 
2301                                                  $form->{decimalplaces}
 
2303                              $form->{decimalplaces},
 
2310   $main::lxdebug->leave_sub();
 
2314   $main::lxdebug->enter_sub();
 
2316   my ($self, $myconfig, $form) = @_;
 
2318   # connect to database
 
2319   my $dbh = $form->dbconnect($myconfig);
 
2321   my $last_period     = 0;
 
2322   my $category        = "pos_ustva";
 
2323   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
 
2324     96 66 43 45 53 62 65 67);
 
2325   my @categories_euro = qw(48 51 86 91 97 93 94);
 
2326   $form->{decimalplaces} *= 1;
 
2328   foreach $item (@categories_cent) {
 
2329     $form->{"$item"} = 0;
 
2331   foreach $item (@categories_euro) {
 
2332     $form->{"$item"} = 0;
 
2335   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2338   #   foreach $item (@categories_cent) {
 
2339   #     if ($form->{$item}{"jetzt"} > 0) {
 
2340   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2341   #             delete $form->{$item}{"jetzt"};
 
2344   #   foreach $item (@categories_euro) {
 
2345   #     if ($form->{$item}{"jetzt"} > 0) {
 
2346   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2347   #             delete $form->{$item}{"jetzt"};
 
2348   #     }  foreach $item (@categories_cent) {
 
2349   #     if ($form->{$item}{"jetzt"} > 0) {
 
2350   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2351   #             delete $form->{$item}{"jetzt"};
 
2354   #   foreach $item (@categories_euro) {
 
2355   #     if ($form->{$item}{"jetzt"} > 0) {
 
2356   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2357   #             delete $form->{$item}{"jetzt"};
 
2364   # Berechnung der USTVA Formularfelder
 
2366   $form->{"51r"} = $form->{"511"};
 
2367   $form->{"86r"} = $form->{"861"};
 
2368   $form->{"97r"} = $form->{"971"};
 
2369   $form->{"93r"} = $form->{"931"};
 
2371   #$form->{"96"}  = $form->{"94"} * 0.16;
 
2373     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
 
2375   $form->{"45"} = $form->{"43"};
 
2376   $form->{"53"} = $form->{"43"};
 
2377   $form->{"62"} = $form->{"43"} - $form->{"66"};
 
2378   $form->{"65"} = $form->{"43"} - $form->{"66"};
 
2379   $form->{"67"} = $form->{"43"} - $form->{"66"};
 
2381   foreach $item (@categories_cent) {
 
2383       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
 
2387   foreach $item (@categories_euro) {
 
2389       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
 
2395   $main::lxdebug->leave_sub();
 
2398 sub income_statement {
 
2399   $main::lxdebug->enter_sub();
 
2401   my ($self, $myconfig, $form) = @_;
 
2403   # connect to database
 
2404   my $dbh = $form->dbconnect($myconfig);
 
2406   my $last_period          = 0;
 
2407   my $category             = "pos_eur";
 
2408   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
2409   my @categories_ausgaben  =
 
2410     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);
 
2412   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
2414   $form->{decimalplaces} *= 1;
 
2416   foreach $item (@categories_einnahmen) {
 
2419   foreach $item (@categories_ausgaben) {
 
2423   foreach $item (@ergebnisse) {
 
2427   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2430   foreach $item (@categories_einnahmen) {
 
2431     $form->{"eur${item}"} =
 
2432       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2433     $form->{"sumeura"} += $form->{$item};
 
2435   foreach $item (@categories_ausgaben) {
 
2436     $form->{"eur${item}"} =
 
2437       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2438     $form->{"sumeurb"} += $form->{$item};
 
2441   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
2443   foreach $item (@ergebnisse) {
 
2445       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2447   $main::lxdebug->leave_sub();