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};
 
 925     if ($form->{method} eq 'cash') {
 
 926       $subwhere .= " AND transdate >= '$fromdate'";
 
 927       $glwhere = " AND ac.transdate >= '$fromdate'";
 
 929       $where .= " AND ac.transdate >= '$fromdate'";
 
 934     $where    .= " AND ac.transdate <= '$todate'";
 
 935     $subwhere .= " AND transdate <= '$todate'";
 
 938   if ($department_id) {
 
 940                JOIN department t ON (a.department_id = t.id)
 
 943                AND t.id = $department_id
 
 947   if ($form->{project_id}) {
 
 949                  AND ac.project_id = $form->{project_id}
 
 953   if ($form->{method} eq 'cash') {
 
 957                  SELECT sum(ac.amount) AS amount,
 
 960                  JOIN chart c ON (c.id = ac.chart_id)
 
 961                  JOIN ar a ON (a.id = ac.trans_id)
 
 969                      JOIN chart ON (chart_id = id)
 
 970                      WHERE link LIKE '%AR_paid%'
 
 979                  SELECT sum(ac.amount) AS amount,
 
 982                  JOIN chart c ON (c.id = ac.chart_id)
 
 983                  JOIN ap a ON (a.id = ac.trans_id)
 
 991                      JOIN chart ON (chart_id = id)
 
 992                      WHERE link LIKE '%AP_paid%'
 
1001                  SELECT sum(ac.amount) AS amount,
 
1004                  JOIN chart c ON (c.id = ac.chart_id)
 
1005                  JOIN gl a ON (a.id = ac.trans_id)
 
1010                  AND NOT (c.link = 'AR' OR c.link = 'AP')
 
1012                  GROUP BY c.$category
 
1015     if ($form->{project_id}) {
 
1021                  SELECT SUM(ac.sellprice * ac.qty) AS amount,
 
1024                  JOIN ar a ON (a.id = ac.trans_id)
 
1025                  JOIN parts p ON (ac.parts_id = p.id)
 
1026                  JOIN chart c on (p.income_accno_id = c.id)
 
1028         -- use transdate from subwhere
 
1029                  WHERE 1 = 1 $subwhere
 
1030                  AND c.category = 'I'
 
1036                      JOIN chart ON (chart_id = id)
 
1037                      WHERE link LIKE '%AR_paid%'
 
1042                  GROUP BY c.$category
 
1046                  SELECT SUM(ac.sellprice) AS amount,
 
1049                  JOIN ap a ON (a.id = ac.trans_id)
 
1050                  JOIN parts p ON (ac.parts_id = p.id)
 
1051                  JOIN chart c on (p.expense_accno_id = c.id)
 
1053                  WHERE 1 = 1 $subwhere
 
1054                  AND c.category = 'E'
 
1060                      JOIN chart ON (chart_id = id)
 
1061                      WHERE link LIKE '%AP_paid%'
 
1066                  GROUP BY c.$category
 
1072     if ($department_id) {
 
1074               JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
1077                AND t.department_id = $department_id
 
1083                  SELECT sum(ac.amount) AS amount,
 
1086                  JOIN chart c ON (c.id = ac.chart_id)
 
1091                  GROUP BY c.$category
 
1094     if ($form->{project_id}) {
 
1100                  SELECT SUM(ac.sellprice * ac.qty) AS amount,
 
1103                  JOIN ar a ON (a.id = ac.trans_id)
 
1104                  JOIN parts p ON (ac.parts_id = p.id)
 
1105                  JOIN chart c on (p.income_accno_id = c.id)
 
1107         -- use transdate from subwhere
 
1108                  WHERE 1 = 1 $subwhere
 
1109                  AND c.category = 'I'
 
1112                  GROUP BY c.$category
 
1116                  SELECT SUM(ac.sellprice * ac.qty) * -1 AS amount,
 
1119                  JOIN ap a ON (a.id = ac.trans_id)
 
1120                  JOIN parts p ON (ac.parts_id = p.id)
 
1121                  JOIN chart c on (p.expense_accno_id = c.id)
 
1123                  WHERE 1 = 1 $subwhere
 
1124                  AND c.category = 'E'
 
1127                  GROUP BY c.$category
 
1138   my $sth = $dbh->prepare($query);
 
1139   $sth->execute || $form->dberror($query);
 
1141   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1142     if ($ref->{amount} < 0) {
 
1143       $ref->{amount} *= -1;
 
1145     if ($category eq "pos_bwa") {
 
1147         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
1149         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
1152       $form->{ $ref->{$category} } += $ref->{amount};
 
1157   $main::lxdebug->leave_sub();
 
1161   $main::lxdebug->enter_sub();
 
1163   my ($self, $myconfig, $form) = @_;
 
1165   my $dbh = $form->dbconnect($myconfig);
 
1167   my ($query, $sth, $ref);
 
1170   my ($null, $department_id) = split /--/, $form->{department};
 
1171   my @headingaccounts = ();
 
1176   my $where    = "1 = 1";
 
1177   my $invwhere = $where;
 
1179   if ($department_id) {
 
1181                 JOIN dpt_trans t ON (ac.trans_id = t.trans_id)
 
1184                 AND t.department_id = $department_id
 
1188   # project_id only applies to getting transactions
 
1189   # it has nothing to do with a trial balance
 
1190   # but we use the same function to collect information
 
1192   if ($form->{project_id}) {
 
1194                 AND ac.project_id = $form->{project_id}
 
1198   # get beginning balances
 
1199   if ($form->{fromdate}) {
 
1201     if ($form->{accounttype} eq 'gifi') {
 
1203       $query = qq|SELECT g.accno, c.category, SUM(ac.amount) AS amount,
 
1206                   JOIN chart c ON (ac.chart_id = c.id)
 
1207                   JOIN gifi g ON (c.gifi_accno = g.accno)
 
1209                   WHERE ac.transdate < '$form->{fromdate}'
 
1212                   GROUP BY g.accno, c.category, g.description
 
1217       $query = qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount,
 
1220                   JOIN chart c ON (ac.chart_id = c.id)
 
1222                   WHERE ac.transdate < '$form->{fromdate}'
 
1225                   GROUP BY c.accno, c.category, c.description
 
1230     $sth = $dbh->prepare($query);
 
1231     $sth->execute || $form->dberror($query);
 
1233     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1234       $balance{ $ref->{accno} } = $ref->{amount};
 
1236       if ($ref->{amount} != 0 && $form->{all_accounts}) {
 
1237         $trb{ $ref->{accno} }{description} = $ref->{description};
 
1238         $trb{ $ref->{accno} }{charttype}   = 'A';
 
1239         $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1248   $query = qq|SELECT c.accno, c.description, c.category
 
1250               WHERE c.charttype = 'H'
 
1253   if ($form->{accounttype} eq 'gifi') {
 
1254     $query = qq|SELECT g.accno, g.description, c.category
 
1256                 JOIN chart c ON (c.gifi_accno = g.accno)
 
1257                 WHERE c.charttype = 'H'
 
1261   $sth = $dbh->prepare($query);
 
1262   $sth->execute || $form->dberror($query);
 
1264   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1265     $trb{ $ref->{accno} }{description} = $ref->{description};
 
1266     $trb{ $ref->{accno} }{charttype}   = 'H';
 
1267     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1269     push @headingaccounts, $ref->{accno};
 
1276   if ($form->{fromdate} || $form->{todate}) {
 
1277     if ($form->{fromdate}) {
 
1278       $tofrom   .= " AND ac.transdate >= '$form->{fromdate}'";
 
1279       $subwhere .= " AND transdate >= '$form->{fromdate}'";
 
1280       $invwhere .= " AND a.transdate >= '$form->{fromdate}'";
 
1281       $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
 
1283     if ($form->{todate}) {
 
1284       $tofrom   .= " AND ac.transdate <= '$form->{todate}'";
 
1285       $invwhere .= " AND a.transdate <= '$form->{todate}'";
 
1286       $subwhere .= " AND transdate <= '$form->{todate}'";
 
1287       $glwhere  .= " AND ac.transdate <= '$form->{todate}'";
 
1291     $where .= qq| AND ((ac.trans_id in (SELECT id from ar)
 
1296                      JOIN chart ON (chart_id = id)
 
1297                      WHERE link LIKE '%AR_paid%'
 
1299                    )) OR (ac.trans_id in (SELECT id from ap)
 
1304                      JOIN chart ON (chart_id = id)
 
1305                      WHERE link LIKE '%AP_paid%'
 
1307                    )) OR (ac.trans_id in (SELECT id from gl)
 
1313   if ($form->{accounttype} eq 'gifi') {
 
1315     $query = qq|SELECT g.accno, g.description, c.category,
 
1316                 SUM(ac.amount) AS amount
 
1318                 JOIN chart c ON (c.id = ac.chart_id)
 
1319                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1324                 GROUP BY g.accno, g.description, c.category
 
1327     if ($form->{project_id}) {
 
1331         -- add project transactions from invoice
 
1335                 SELECT g.accno, g.description, c.category,
 
1336                 SUM(ac.sellprice * ac.qty) AS amount
 
1338                 JOIN ar a ON (ac.trans_id = a.id)
 
1339                 JOIN parts p ON (ac.parts_id = p.id)
 
1340                 JOIN chart c ON (p.income_accno_id = c.id)
 
1341                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1346                 GROUP BY g.accno, g.description, c.category
 
1350                 SELECT g.accno, g.description, c.category,
 
1351                 SUM(ac.sellprice * ac.qty) * -1 AS amount
 
1353                 JOIN ap a ON (ac.trans_id = a.id)
 
1354                 JOIN parts p ON (ac.parts_id = p.id)
 
1355                 JOIN chart c ON (p.expense_accno_id = c.id)
 
1356                 JOIN gifi g ON (c.gifi_accno = g.accno)
 
1361                 GROUP BY g.accno, g.description, c.category
 
1370     $query = qq|SELECT c.accno, c.description, c.category,
 
1371                 SUM(ac.amount) AS amount
 
1373                 JOIN chart c ON (c.id = ac.chart_id)
 
1378                 GROUP BY c.accno, c.description, c.category
 
1381     if ($form->{project_id}) {
 
1385         -- add project transactions from invoice
 
1389                 SELECT c.accno, c.description, c.category,
 
1390                 SUM(ac.sellprice * ac.qty) AS amount
 
1392                 JOIN ar a ON (ac.trans_id = a.id)
 
1393                 JOIN parts p ON (ac.parts_id = p.id)
 
1394                 JOIN chart c ON (p.income_accno_id = c.id)
 
1399                 GROUP BY c.accno, c.description, c.category
 
1403                 SELECT c.accno, c.description, c.category,
 
1404                 SUM(ac.sellprice * ac.qty) * -1 AS amount
 
1406                 JOIN ap a ON (ac.trans_id = a.id)
 
1407                 JOIN parts p ON (ac.parts_id = p.id)
 
1408                 JOIN chart c ON (p.expense_accno_id = c.id)
 
1413                 GROUP BY c.accno, c.description, c.category
 
1422   $sth = $dbh->prepare($query);
 
1423   $sth->execute || $form->dberror($query);
 
1425   # prepare query for each account
 
1426   $query = qq|SELECT (SELECT SUM(ac.amount) * -1
 
1428               JOIN chart c ON (c.id = ac.chart_id)
 
1434               AND c.accno = ?) AS debit,
 
1436              (SELECT SUM(ac.amount)
 
1438               JOIN chart c ON (c.id = ac.chart_id)
 
1444               AND c.accno = ?) AS credit
 
1447   if ($form->{accounttype} eq 'gifi') {
 
1449     $query = qq|SELECT (SELECT SUM(ac.amount) * -1
 
1451                 JOIN chart c ON (c.id = ac.chart_id)
 
1457                 AND c.gifi_accno = ?) AS debit,
 
1459                (SELECT SUM(ac.amount)
 
1461                 JOIN chart c ON (c.id = ac.chart_id)
 
1467                 AND c.gifi_accno = ?) AS credit|;
 
1471   $drcr = $dbh->prepare($query);
 
1473   if ($form->{project_id}) {
 
1475     # prepare query for each account
 
1476     $query = qq|SELECT (SELECT SUM(ac.sellprice * ac.qty) * -1
 
1478               JOIN parts p ON (ac.parts_id = p.id)
 
1479               JOIN ap a ON (ac.trans_id = a.id)
 
1480               JOIN chart c ON (p.expense_accno_id = c.id)
 
1485               AND c.accno = ?) AS debit,
 
1487              (SELECT SUM(ac.sellprice * ac.qty)
 
1489               JOIN parts p ON (ac.parts_id = p.id)
 
1490               JOIN ar a ON (ac.trans_id = a.id)
 
1491               JOIN chart c ON (p.income_accno_id = c.id)
 
1496               AND c.accno = ?) AS credit
 
1499     $project_drcr = $dbh->prepare($query);
 
1503   # calculate the debit and credit in the period
 
1504   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1505     $trb{ $ref->{accno} }{description} = $ref->{description};
 
1506     $trb{ $ref->{accno} }{charttype}   = 'A';
 
1507     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
1508     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
1512   my ($debit, $credit);
 
1514   foreach my $accno (sort keys %trb) {
 
1517     $ref->{accno} = $accno;
 
1518     map { $ref->{$_} = $trb{$accno}{$_} }
 
1519       qw(description category charttype amount);
 
1521     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1523     if ($trb{$accno}{charttype} eq 'A') {
 
1526       $drcr->execute($ref->{accno}, $ref->{accno}) || $form->dberror($query);
 
1528       ($debit, $credit) = (0, 0);
 
1529       while (($debit, $credit) = $drcr->fetchrow_array) {
 
1530         $ref->{debit}  += $debit;
 
1531         $ref->{credit} += $credit;
 
1535       if ($form->{project_id}) {
 
1538         $project_drcr->execute($ref->{accno}, $ref->{accno})
 
1539           || $form->dberror($query);
 
1541         ($debit, $credit) = (0, 0);
 
1542         while (($debit, $credit) = $project_drcr->fetchrow_array) {
 
1543           $ref->{debit}  += $debit;
 
1544           $ref->{credit} += $credit;
 
1546         $project_drcr->finish;
 
1549       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1550       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1555     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1556     $accno = pop @accno;
 
1558       $trb{$accno}{debit}  += $ref->{debit};
 
1559       $trb{$accno}{credit} += $ref->{credit};
 
1562     push @{ $form->{TB} }, $ref;
 
1568   # debits and credits for headings
 
1569   foreach $accno (@headingaccounts) {
 
1570     foreach $ref (@{ $form->{TB} }) {
 
1571       if ($accno eq $ref->{accno}) {
 
1572         $ref->{debit}  = $trb{$accno}{debit};
 
1573         $ref->{credit} = $trb{$accno}{credit};
 
1578   $main::lxdebug->leave_sub();
 
1582   $main::lxdebug->enter_sub();
 
1584   my ($self, $myconfig, $form) = @_;
 
1586   # connect to database
 
1587   my $dbh     = $form->dbconnect($myconfig);
 
1588   my $invoice = ($form->{arap} eq 'ar') ? 'is' : 'ir';
 
1590   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
 
1592   my $where = "1 = 1";
 
1595   if ($form->{"$form->{ct}_id"}) {
 
1596     $where .= qq| AND ct.id = $form->{"$form->{ct}_id"}|;
 
1598     if ($form->{ $form->{ct} }) {
 
1599       $name = $form->like(lc $form->{ $form->{ct} });
 
1600       $where .= qq| AND lower(ct.name) LIKE '$name'| if $form->{ $form->{ct} };
 
1605   if ($form->{department}) {
 
1606     ($null, $department_id) = split /--/, $form->{department};
 
1608                JOIN department d ON (a.department_id = d.id)
 
1611     $where .= qq| AND a.department_id = $department_id|;
 
1614   # select outstanding vendors or customers, depends on $ct
 
1615   my $query = qq|SELECT DISTINCT ct.id, ct.name
 
1616                  FROM $form->{ct} ct, $form->{arap} a
 
1619                  AND a.$form->{ct}_id = ct.id
 
1620                  AND a.paid != a.amount
 
1621                  AND (a.transdate <= '$form->{todate}')
 
1624   my $sth = $dbh->prepare($query);
 
1625   $sth->execute || $form->dberror;
 
1627   my $buysell = ($form->{arap} eq 'ar') ? 'buy' : 'sell';
 
1629   # for each company that has some stuff outstanding
 
1630   while (my ($id) = $sth->fetchrow_array) {
 
1634 -- between 0-30 days
 
1636         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1637         street, zipcode, city, country, contact, email,
 
1638         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1639         "invnumber", "transdate",
 
1640         (amount - paid) as "c0", 0.00 as "c30", 0.00 as "c60", 0.00 as "c90",
 
1641         "duedate", invoice, $form->{arap}.id,
 
1642           (SELECT $buysell FROM exchangerate
 
1643            WHERE $form->{arap}.curr = exchangerate.curr
 
1644            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1645   FROM $form->{arap}, $form->{ct}
 
1646         WHERE paid != amount
 
1647         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1648         AND $form->{ct}.id = $id
 
1650                 transdate <= (date '$form->{todate}' - interval '0 days')
 
1651                 AND transdate >= (date '$form->{todate}' - interval '30 days')
 
1656 -- between 31-60 days
 
1658         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1659         street, zipcode, city, country, contact, email,
 
1660         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1661         "invnumber", "transdate",
 
1662         0.00 as "c0", (amount - paid) as "c30", 0.00 as "c60", 0.00 as "c90",
 
1663         "duedate", invoice, $form->{arap}.id,
 
1664           (SELECT $buysell FROM exchangerate
 
1665            WHERE $form->{arap}.curr = exchangerate.curr
 
1666            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1667   FROM $form->{arap}, $form->{ct}
 
1668         WHERE paid != amount
 
1669         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1670         AND $form->{ct}.id = $id
 
1672                 transdate < (date '$form->{todate}' - interval '30 days')
 
1673                 AND transdate >= (date '$form->{todate}' - interval '60 days')
 
1678 -- between 61-90 days
 
1680         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1681         street, zipcode, city, country, contact, email,
 
1682         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1683         "invnumber", "transdate",
 
1684         0.00 as "c0", 0.00 as "c30", (amount - paid) as "c60", 0.00 as "c90",
 
1685         "duedate", invoice, $form->{arap}.id,
 
1686           (SELECT $buysell FROM exchangerate
 
1687            WHERE $form->{arap}.curr = exchangerate.curr
 
1688            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1689         FROM $form->{arap}, $form->{ct}
 
1690         WHERE paid != amount
 
1691         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1692         AND $form->{ct}.id = $id
 
1694                 transdate < (date '$form->{todate}' - interval '60 days')
 
1695                 AND transdate >= (date '$form->{todate}' - interval '90 days')
 
1702         SELECT $form->{ct}.id AS ctid, $form->{ct}.name,
 
1703         street, zipcode, city, country, contact, email,
 
1704         phone as customerphone, fax as customerfax, $form->{ct}number,
 
1705         "invnumber", "transdate",
 
1706         0.00 as "c0", 0.00 as "c30", 0.00 as "c60", (amount - paid) as "c90",
 
1707         "duedate", invoice, $form->{arap}.id,
 
1708           (SELECT $buysell FROM exchangerate
 
1709            WHERE $form->{arap}.curr = exchangerate.curr
 
1710            AND exchangerate.transdate = $form->{arap}.transdate) AS exchangerate
 
1711         FROM $form->{arap}, $form->{ct}
 
1712         WHERE paid != amount
 
1713         AND $form->{arap}.$form->{ct}_id = $form->{ct}.id
 
1714         AND $form->{ct}.id = $id
 
1715         AND transdate < (date '$form->{todate}' - interval '90 days')
 
1719   ctid, transdate, invnumber
 
1723     my $sth = $dbh->prepare($query);
 
1724     $sth->execute || $form->dberror;
 
1726     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1727       $ref->{module} = ($ref->{invoice}) ? $invoice : $form->{arap};
 
1728       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1729       push @{ $form->{AG} }, $ref;
 
1741   $main::lxdebug->leave_sub();
 
1745   $main::lxdebug->enter_sub();
 
1747   my ($self, $myconfig, $form) = @_;
 
1749   # connect to database
 
1750   my $dbh = $form->dbconnect($myconfig);
 
1752   my $query = qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1754                  WHERE ct.id = $form->{"$form->{ct}_id"}|;
 
1755   my $sth = $dbh->prepare($query);
 
1756   $sth->execute || $form->dberror;
 
1758   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1759     $sth->fetchrow_array;
 
1763   $main::lxdebug->leave_sub();
 
1766 sub get_taxaccounts {
 
1767   $main::lxdebug->enter_sub();
 
1769   my ($self, $myconfig, $form) = @_;
 
1771   # connect to database
 
1772   my $dbh = $form->dbconnect($myconfig);
 
1775   my $query = qq|SELECT c.accno, c.description, t.rate
 
1777                  WHERE c.link LIKE '%CT_tax%'
 
1778                  AND c.id = t.chart_id
 
1780   my $sth = $dbh->prepare($query);
 
1781   $sth->execute || $form->dberror;
 
1784   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1785     push @{ $form->{taxaccounts} }, $ref;
 
1789   # get gifi tax accounts
 
1790   $query = qq|SELECT DISTINCT ON (g.accno) g.accno, g.description,
 
1792                  FROM gifi g, chart c, tax t
 
1793                  WHERE g.accno = c.gifi_accno
 
1794                  AND c.id = t.chart_id
 
1795                  AND c.link LIKE '%CT_tax%'
 
1796                  GROUP BY g.accno, g.description
 
1798   $sth = $dbh->prepare($query);
 
1799   $sth->execute || $form->dberror;
 
1801   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1802     push @{ $form->{gifi_taxaccounts} }, $ref;
 
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 ($null, $department_id) = split /--/, $form->{department};
 
1822   my $where = "1 = 1";
 
1824   if ($department_id) {
 
1826                  AND a.department_id = $department_id
 
1832   if ($form->{accno}) {
 
1833     if ($form->{accno} =~ /^gifi_/) {
 
1834       ($null, $accno) = split /_/, $form->{accno};
 
1835       $rate  = $form->{"$form->{accno}_rate"};
 
1836       $accno = qq| AND ch.gifi_accno = '$accno'|;
 
1838       $accno = $form->{accno};
 
1839       $rate  = $form->{"$form->{accno}_rate"};
 
1840       $accno = qq| AND ch.accno = '$accno'|;
 
1847   if ($form->{db} eq 'ar') {
 
1848     $table = "customer";
 
1851   if ($form->{db} eq 'ap') {
 
1856   my $transdate = "a.transdate";
 
1858   if ($form->{method} eq 'cash') {
 
1859     $transdate = "a.datepaid";
 
1862       ($form->{todate}) ? $form->{todate} : $form->current_date($myconfig);
 
1869                      JOIN chart ON (chart_id = id)
 
1870                      WHERE link LIKE '%${ARAP}_paid%'
 
1871                      AND transdate <= '$todate'
 
1876   # if there are any dates construct a where
 
1877   if ($form->{fromdate} || $form->{todate}) {
 
1878     if ($form->{fromdate}) {
 
1879       $where .= " AND $transdate >= '$form->{fromdate}'";
 
1881     if ($form->{todate}) {
 
1882       $where .= " AND $transdate <= '$form->{todate}'";
 
1886   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1888   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1889   $sortorder = $form->{sort} if $form->{sort};
 
1891   $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
 
1892               a.invnumber, n.name, a.netamount,
 
1893               ac.amount * $ml AS tax
 
1895             JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1896             JOIN chart ch ON (ch.id = ac.chart_id)
 
1897             JOIN $table n ON (n.id = a.${table}_id)
 
1902               SELECT a.id, '1' AS invoice, $transdate AS transdate,
 
1903               a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1904               i.sellprice * i.qty * $rate * $ml AS tax
 
1906             JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1907             JOIN chart ch ON (ch.id = ac.chart_id)
 
1908             JOIN $table n ON (n.id = a.${table}_id)
 
1909             JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1910             JOIN invoice i ON (i.trans_id = a.id)
 
1911             JOIN partstax p ON (p.parts_id = i.parts_id)
 
1915               ORDER by $sortorder|;
 
1917   if ($form->{report} =~ /nontaxable/) {
 
1919     # only gather up non-taxable transactions
 
1920     $query = qq|SELECT a.id, '0' AS invoice, $transdate AS transdate,
 
1921                 a.invnumber, n.name, a.netamount
 
1923               JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1924               JOIN $table n ON (n.id = a.${table}_id)
 
1927                 AND a.netamount = a.amount
 
1929                 SELECT a.id, '1' AS invoice, $transdate AS transdate,
 
1930                 a.invnumber, n.name, i.sellprice * i.qty AS netamount
 
1932               JOIN $form->{db} a ON (a.id = ac.trans_id)
 
1933               JOIN $table n ON (n.id = a.${table}_id)
 
1934               JOIN invoice i ON (i.trans_id = a.id)
 
1938                   a.${table}_id NOT IN (
 
1939                         SELECT ${table}_id FROM ${table}tax t (${table}_id)
 
1942                         SELECT parts_id FROM partstax p (parts_id)
 
1945                 GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
 
1946                 ORDER by $sortorder|;
 
1949   my $sth = $dbh->prepare($query);
 
1950   $sth->execute || $form->dberror($query);
 
1952   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1953     push @{ $form->{TR} }, $ref;
 
1959   $main::lxdebug->leave_sub();
 
1962 sub paymentaccounts {
 
1963   $main::lxdebug->enter_sub();
 
1965   my ($self, $myconfig, $form) = @_;
 
1967   # connect to database, turn AutoCommit off
 
1968   my $dbh = $form->dbconnect_noauto($myconfig);
 
1970   my $ARAP = uc $form->{db};
 
1972   # get A(R|P)_paid accounts
 
1973   my $query = qq|SELECT c.accno, c.description
 
1975                  WHERE c.link LIKE '%${ARAP}_paid%'|;
 
1976   my $sth = $dbh->prepare($query);
 
1977   $sth->execute || $form->dberror($query);
 
1979   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
1980     push @{ $form->{PR} }, $ref;
 
1986   $main::lxdebug->leave_sub();
 
1990   $main::lxdebug->enter_sub();
 
1992   my ($self, $myconfig, $form) = @_;
 
1994   # connect to database, turn AutoCommit off
 
1995   my $dbh = $form->dbconnect_noauto($myconfig);
 
1998   if ($form->{db} eq 'ar') {
 
1999     $table = 'customer';
 
2002   if ($form->{db} eq 'ap') {
 
2010   if ($form->{department_id}) {
 
2012                  JOIN dpt_trans t ON (t.trans_id = ac.trans_id)
 
2016                  AND t.department_id = $form->{department_id}
 
2020   if ($form->{fromdate}) {
 
2021     $where .= " AND ac.transdate >= '$form->{fromdate}'";
 
2023   if ($form->{todate}) {
 
2024     $where .= " AND ac.transdate <= '$form->{todate}'";
 
2026   if (!$form->{fx_transaction}) {
 
2027     $where .= " AND ac.fx_transaction = '0'";
 
2032   if ($form->{reference}) {
 
2033     $reference = $form->like(lc $form->{reference});
 
2034     $invnumber = " AND lower(a.invnumber) LIKE '$reference'";
 
2035     $reference = " AND lower(g.reference) LIKE '$reference'";
 
2037   if ($form->{source}) {
 
2038     my $source = $form->like(lc $form->{source});
 
2039     $where .= " AND lower(ac.source) LIKE '$source'";
 
2041   if ($form->{memo}) {
 
2042     my $memo = $form->like(lc $form->{memo});
 
2043     $where .= " AND lower(ac.memo) LIKE '$memo'";
 
2046   my $sortorder = join ', ',
 
2047     $form->sort_columns(qw(name invnumber ordnumber transdate source));
 
2048   $sortorder = $form->{sort} if $form->{sort};
 
2050   # cycle through each id
 
2051   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
2053     $query = qq|SELECT c.id, c.accno, c.description
 
2055                 WHERE c.accno = '$accno'|;
 
2056     $sth = $dbh->prepare($query);
 
2057     $sth->execute || $form->dberror($query);
 
2059     my $ref = $sth->fetchrow_hashref(NAME_lc);
 
2060     push @{ $form->{PR} }, $ref;
 
2063     $query = qq|SELECT c.name, a.invnumber, a.ordnumber,
 
2064                 ac.transdate, ac.amount * $ml AS paid, ac.source,
 
2065                 a.invoice, a.id, ac.memo, '$form->{db}' AS module
 
2067                 JOIN $form->{db} a ON (ac.trans_id = a.id)
 
2068                 JOIN $table c ON (c.id = a.${table}_id)
 
2070                 WHERE ac.chart_id = $ref->{id}
 
2075                 SELECT g.description, g.reference, NULL AS ordnumber,
 
2076                 ac.transdate, ac.amount * $ml AS paid, ac.source,
 
2077                 '0' as invoice, g.id, ac.memo, 'gl' AS module
 
2079                 JOIN gl g ON (g.id = ac.trans_id)
 
2081                 WHERE ac.chart_id = $ref->{id}
 
2084                 AND (ac.amount * $ml) > 0
 
2085                 ORDER BY $sortorder|;
 
2087     $sth = $dbh->prepare($query);
 
2088     $sth->execute || $form->dberror($query);
 
2090     while (my $pr = $sth->fetchrow_hashref(NAME_lc)) {
 
2091       push @{ $form->{ $ref->{id} } }, $pr;
 
2099   $main::lxdebug->leave_sub();
 
2103   $main::lxdebug->enter_sub();
 
2105   my ($self, $myconfig, $form) = @_;
 
2107   # connect to database
 
2108   my $dbh = $form->dbconnect($myconfig);
 
2110   my $last_period = 0;
 
2111   my $category    = "pos_bwa";
 
2113     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);
 
2115   $form->{decimalplaces} *= 1;
 
2117   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2120   # if there are any compare dates
 
2121   if ($form->{fromdate} || $form->{todate}) {
 
2123     if ($form->{fromdate}) {
 
2124       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
2127       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
2130     $kummfromdate = $form->{comparefromdate};
 
2131     $kummtodate   = $form->{comparetodate};
 
2132     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form,
 
2136   @periods        = qw(jetzt kumm);
 
2137   @gesamtleistung = qw(1 2 3);
 
2138   @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 19 20);
 
2140     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
2142   foreach $key (@periods) {
 
2143     $form->{ "$key" . "gesamtleistung" } = 0;
 
2144     $form->{ "$key" . "gesamtkosten" }   = 0;
 
2146     foreach $category (@categories) {
 
2148       if (defined($form->{$category}{$key})) {
 
2149         $form->{"$key$category"} =
 
2150           $form->format_amount($myconfig,
 
2151                                $form->round_amount($form->{$category}{$key}, 2
 
2153                                $form->{decimalplaces},
 
2157     foreach $item (@gesamtleistung) {
 
2158       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
2160     foreach $item (@gesamtkosten) {
 
2161       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
2163     $form->{ "$key" . "rohertrag" } =
 
2164       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
2165     $form->{ "$key" . "betriebrohertrag" } =
 
2166       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
2167     $form->{ "$key" . "betriebsergebnis" } =
 
2168       $form->{ "$key" . "betriebrohertrag" } -
 
2169       $form->{ "$key" . "gesamtkosten" };
 
2170     $form->{ "$key" . "neutraleraufwand" } =
 
2171       $form->{30}{$key} + $form->{31}{$key};
 
2172     $form->{ "$key" . "neutralertrag" } =
 
2173       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
2174     $form->{ "$key" . "ergebnisvorsteuern" } =
 
2175       $form->{ "$key" . "betriebsergebnis" } -
 
2176       $form->{ "$key" . "neutraleraufwand" } +
 
2177       $form->{ "$key" . "neutralertrag" };
 
2178     $form->{ "$key" . "ergebnis" } =
 
2179       $form->{ "$key" . "ergebnisvorsteuern" } + $form->{35}{$key};
 
2181     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
2182       foreach $category (@categories) {
 
2183         if (defined($form->{$category}{$key})) {
 
2184           $form->{ "$key" . "gl" . "$category" } =
 
2185             $form->format_amount(
 
2187                                $form->round_amount(
 
2188                                  ($form->{$category}{$key} /
 
2189                                     $form->{ "$key" . "gesamtleistung" } * 100
 
2191                                  $form->{decimalplaces}
 
2193                                $form->{decimalplaces},
 
2197       foreach $item (@ergebnisse) {
 
2198         $form->{ "$key" . "gl" . "$item" } =
 
2199           $form->format_amount($myconfig,
 
2200                                $form->round_amount(
 
2201                                  ( $form->{ "$key" . "$item" } /
 
2202                                      $form->{ "$key" . "gesamtleistung" } * 100
 
2204                                  $form->{decimalplaces}
 
2206                                $form->{decimalplaces},
 
2211     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
2212       foreach $category (@categories) {
 
2213         if (defined($form->{$category}{$key})) {
 
2214           $form->{ "$key" . "gk" . "$category" } =
 
2215             $form->format_amount($myconfig,
 
2216                                  $form->round_amount(
 
2217                                    ($form->{$category}{$key} /
 
2218                                       $form->{ "$key" . "gesamtkosten" } * 100
 
2220                                    $form->{decimalplaces}
 
2222                                  $form->{decimalplaces},
 
2226       foreach $item (@ergebnisse) {
 
2227         $form->{ "$key" . "gk" . "$item" } =
 
2228           $form->format_amount($myconfig,
 
2229                                $form->round_amount(
 
2230                                    ($form->{ "$key" . "$item" } /
 
2231                                       $form->{ "$key" . "gesamtkosten" } * 100
 
2233                                    $form->{decimalplaces}
 
2235                                $form->{decimalplaces},
 
2240     if ($form->{10}{$key} > 0) {
 
2241       foreach $category (@categories) {
 
2242         if (defined($form->{$category}{$key})) {
 
2243           $form->{ "$key" . "pk" . "$category" } =
 
2244             $form->format_amount(
 
2246                         $form->round_amount(
 
2247                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
2248                           $form->{decimalplaces}
 
2250                         $form->{decimalplaces},
 
2254       foreach $item (@ergebnisse) {
 
2255         $form->{ "$key" . "pk" . "$item" } =
 
2256           $form->format_amount($myconfig,
 
2257                                $form->round_amount(
 
2258                                                 ($form->{ "$key" . "$item" } /
 
2259                                                    $form->{10}{$key} * 100
 
2261                                                 $form->{decimalplaces}
 
2263                                $form->{decimalplaces},
 
2268     if ($form->{4}{$key} > 0) {
 
2269       foreach $category (@categories) {
 
2270         if (defined($form->{$category}{$key})) {
 
2271           $form->{ "$key" . "auf" . "$category" } =
 
2272             $form->format_amount(
 
2274                          $form->round_amount(
 
2275                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
2276                            $form->{decimalplaces}
 
2278                          $form->{decimalplaces},
 
2282       foreach $item (@ergebnisse) {
 
2283         $form->{ "$key" . "auf" . "$item" } =
 
2284           $form->format_amount($myconfig,
 
2285                                $form->round_amount(
 
2286                                                 ($form->{ "$key" . "$item" } /
 
2287                                                    $form->{4}{$key} * 100
 
2289                                                 $form->{decimalplaces}
 
2291                                $form->{decimalplaces},
 
2296     foreach $item (@ergebnisse) {
 
2297       $form->{ "$key" . "$item" } =
 
2298         $form->format_amount($myconfig,
 
2299                              $form->round_amount($form->{ "$key" . "$item" },
 
2300                                                  $form->{decimalplaces}
 
2302                              $form->{decimalplaces},
 
2309   $main::lxdebug->leave_sub();
 
2313   $main::lxdebug->enter_sub();
 
2315   my ($self, $myconfig, $form) = @_;
 
2317   # connect to database
 
2318   my $dbh = $form->dbconnect($myconfig);
 
2320   my $last_period     = 0;
 
2321   my $category        = "pos_ustva";
 
2322   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
 
2323     96 66 43 45 53 62 65 67);
 
2324   my @categories_euro = qw(48 51 86 91 97 93 94);
 
2325   $form->{decimalplaces} *= 1;
 
2327   foreach $item (@categories_cent) {
 
2328     $form->{"$item"} = 0;
 
2330   foreach $item (@categories_euro) {
 
2331     $form->{"$item"} = 0;
 
2334   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2337   #   foreach $item (@categories_cent) {
 
2338   #     if ($form->{$item}{"jetzt"} > 0) {
 
2339   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2340   #             delete $form->{$item}{"jetzt"};
 
2343   #   foreach $item (@categories_euro) {
 
2344   #     if ($form->{$item}{"jetzt"} > 0) {
 
2345   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2346   #             delete $form->{$item}{"jetzt"};
 
2347   #     }  foreach $item (@categories_cent) {
 
2348   #     if ($form->{$item}{"jetzt"} > 0) {
 
2349   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2350   #             delete $form->{$item}{"jetzt"};
 
2353   #   foreach $item (@categories_euro) {
 
2354   #     if ($form->{$item}{"jetzt"} > 0) {
 
2355   #             $form->{$item} = $form->{$item}{"jetzt"};
 
2356   #             delete $form->{$item}{"jetzt"};
 
2363   # Berechnung der USTVA Formularfelder
 
2365   $form->{"51r"} = $form->{"511"};
 
2366   $form->{"86r"} = $form->{"861"};
 
2367   $form->{"97r"} = $form->{"971"};
 
2368   $form->{"93r"} = $form->{"931"};
 
2370   #$form->{"96"}  = $form->{"94"} * 0.16;
 
2372     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
 
2374   $form->{"45"} = $form->{"43"};
 
2375   $form->{"53"} = $form->{"43"};
 
2376   $form->{"62"} = $form->{"43"} - $form->{"66"};
 
2377   $form->{"65"} = $form->{"43"} - $form->{"66"};
 
2378   $form->{"67"} = $form->{"43"} - $form->{"66"};
 
2380   foreach $item (@categories_cent) {
 
2382       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
 
2386   foreach $item (@categories_euro) {
 
2388       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
 
2394   $main::lxdebug->leave_sub();
 
2397 sub income_statement {
 
2398   $main::lxdebug->enter_sub();
 
2400   my ($self, $myconfig, $form) = @_;
 
2402   # connect to database
 
2403   my $dbh = $form->dbconnect($myconfig);
 
2405   my $last_period          = 0;
 
2406   my $category             = "pos_eur";
 
2407   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
2408   my @categories_ausgaben  =
 
2409     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);
 
2411   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
2413   $form->{decimalplaces} *= 1;
 
2415   foreach $item (@categories_einnahmen) {
 
2418   foreach $item (@categories_ausgaben) {
 
2422   foreach $item (@ergebnisse) {
 
2426   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
2429   foreach $item (@categories_einnahmen) {
 
2430     $form->{"eur${item}"} =
 
2431       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2432     $form->{"sumeura"} += $form->{$item};
 
2434   foreach $item (@categories_ausgaben) {
 
2435     $form->{"eur${item}"} =
 
2436       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2437     $form->{"sumeurb"} += $form->{$item};
 
2440   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
2442   foreach $item (@ergebnisse) {
 
2444       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
2446   $main::lxdebug->leave_sub();