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 #======================================================================
 
  39 use List::Util qw(sum);
 
  44 # new implementation of balance sheet
 
  47 # stuff missing from the original implementation:
 
  50 # - proper testing for heading charts
 
  51 # - transmission from $form to TMPL realm is not as clear as i'd like
 
  53   $main::lxdebug->enter_sub();
 
  55   my $myconfig = \%main::myconfig;
 
  56   my $form     = $main::form;
 
  57   my $dbh      = $form->get_standard_dbh($myconfig);
 
  60   my @categories  = qw(A C L Q);
 
  62   # if there are any dates construct a where
 
  63   if ($form->{asofdate}) {
 
  64     $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
 
  67   get_accounts($dbh, $last_period, "", $form->{asofdate}, $form, \@categories);
 
  69   # if there are any compare dates
 
  70   if ($form->{compareasofdate}) {
 
  72     get_accounts($dbh, $last_period, "", $form->{compareasofdate}, $form, \@categories);
 
  73     $form->{last_period} = conv_dateq($form->{compareasofdate});
 
  76   # now we got $form->{A}{accno}{ }    assets
 
  77   # and $form->{L}{accno}{ }           liabilities
 
  78   # and $form->{Q}{accno}{ }           equity
 
  79   # build asset accounts
 
  81   my %account = ('A' => { 'ml'     => -1 },
 
  83                  'Q' => { 'ml'     =>  1 });
 
  87   foreach my $category (grep { !/C/ } @categories) {
 
  89     $TMPL_DATA->{$category} = [];
 
  90     my $ml  = $account{$category}{ml};
 
  92     foreach my $key (sort keys %{ $form->{$category} }) {
 
  94       my $row = { %{ $form->{$category}{$key} } };
 
  96       # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
 
  97       # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
 
  98       if ($row->{charttype} eq "H") {
 
  99         if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 100           $row->{subdescription} = $account{$category}{subdescription};
 
 101           $row->{this}           = $account{$category}{subthis} * $ml;                   # format: $dec, $dash
 
 102           $row->{last}           = $account{$category}{sublast} * $ml if $last_period;   # format: $dec, $dash
 
 105         $row->{subheader} = 1;
 
 106         $account{$category}{subthis}        = $row->{this};
 
 107         $account{$category}{sublast}        = $row->{last};
 
 108         $account{$category}{subdescription} = $row->{description};
 
 109         $account{$category}{subtotal} = 1;
 
 114         next unless $form->{l_heading};
 
 117       for my $period (qw(this last)) {
 
 118         next if ($period eq 'last' && !$last_period);
 
 120         $row->{$period}                    *= $ml;
 
 121         $form->{total}{$category}{$period} += $row->{$period};      #      if ($row->{charttype} eq 'A') {   # why??
 
 124       push @{ $TMPL_DATA->{$category} }, $row;
 
 127     # resolve heading/subtotal
 
 128     if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 129       $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
 
 130       $TMPL_DATA->{$category}[-1]{this}           = $account{$category}{subthis} * $ml;                   # format: $dec, $dash
 
 131       $TMPL_DATA->{$category}[-1]{last}           = $account{$category}{sublast} * $ml if $last_period;   # format: $dec, $dash
 
 134     $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
 
 135     $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
 
 138   for my $period (qw(this last)) {
 
 139     next if ($period eq 'last' && !$last_period);
 
 141     $form->{E}{$period}             = $form->{total}{A}{$period} - $form->{total}{L}{$period} - $form->{total}{Q}{$period};
 
 142     $form->{total}{Q}{$period}     += $form->{E}{$period};
 
 143     $TMPL_DATA->{total}{Q}{$period} = $form->{total}{Q}{$period};
 
 144     $TMPL_DATA->{total}{$period}    = $form->{total}{L}{$period} + $form->{total}{Q}{$period};
 
 147   push @{ $TMPL_DATA->{Q} }, $form->{E};
 
 149   $main::lxdebug->leave_sub();
 
 155   $main::lxdebug->enter_sub();
 
 157   my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
 
 159   my ($null, $department_id) = split /--/, $form->{department};
 
 170   my $dec = $form->{decimalplaces};
 
 172   my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
 
 176     qq|SELECT c.accno, c.description, c.category
 
 178        WHERE (c.charttype = 'H')
 
 182   $sth = prepare_execute_query($form, $dbh, $query);
 
 184   my @headingaccounts = ();
 
 185   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 186     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 187       "$ref->{description}";
 
 188     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
 
 189     $form->{ $ref->{category} }{ $ref->{accno} }{accno}     = $ref->{accno};
 
 191     push @headingaccounts, $ref->{accno};
 
 197     $fromdate = conv_dateq($fromdate);
 
 198     if ($form->{method} eq 'cash') {
 
 199       $subwhere .= " AND (transdate >= $fromdate)";
 
 200       $glwhere = " AND (ac.transdate >= $fromdate)";
 
 202       $where .= " AND (ac.transdate >= $fromdate)";
 
 207     $todate = conv_dateq($todate);
 
 208     $where    .= " AND (ac.transdate <= $todate)";
 
 209     $subwhere .= " AND (transdate <= $todate)";
 
 212   if ($department_id) {
 
 213     $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
 
 214     $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|)|;
 
 217   if ($form->{project_id}) {
 
 218     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 221   if ($form->{method} eq 'cash') {
 
 223       qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 225          JOIN chart c ON (c.id = ac.chart_id)
 
 226          JOIN ar a ON (a.id = ac.trans_id)
 
 235                JOIN chart ON (chart_id = id)
 
 236                WHERE (link LIKE '%AR_paid%')
 
 240          GROUP BY c.accno, c.description, c.category
 
 244          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 246          JOIN chart c ON (c.id = ac.chart_id)
 
 247          JOIN ap a ON (a.id = ac.trans_id)
 
 256                JOIN chart ON (chart_id = id)
 
 257                WHERE (link LIKE '%AP_paid%')
 
 261          GROUP BY c.accno, c.description, c.category
 
 265          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 267          JOIN chart c ON (c.id = ac.chart_id)
 
 268          JOIN gl a ON (a.id = ac.trans_id)
 
 274              AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 276          GROUP BY c.accno, c.description, c.category |;
 
 278     if ($form->{project_id}) {
 
 283          SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 285          JOIN ar a ON (a.id = ac.trans_id)
 
 286          JOIN parts p ON (ac.parts_id = p.id)
 
 287          JOIN chart c on (p.income_accno_id = c.id)
 
 289          -- use transdate from subwhere
 
 290          WHERE (c.category = 'I')
 
 297                JOIN chart ON (chart_id = id)
 
 298                WHERE (link LIKE '%AR_paid%')
 
 302          GROUP BY c.accno, c.description, c.category
 
 306          SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
 
 308          JOIN ap a ON (a.id = ac.trans_id)
 
 309          JOIN parts p ON (ac.parts_id = p.id)
 
 310          JOIN chart c on (p.expense_accno_id = c.id)
 
 312          WHERE (c.category = 'E')
 
 319                JOIN chart ON (chart_id = id)
 
 320                WHERE link LIKE '%AP_paid%'
 
 324          GROUP BY c.accno, c.description, c.category |;
 
 327   } else {                      # if ($form->{method} eq 'cash')
 
 328     if ($department_id) {
 
 329       $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
 330       $dpt_where = qq| AND t.department_id = $department_id |;
 
 334       SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 336       JOIN chart c ON (c.id = ac.chart_id)
 
 342       GROUP BY c.accno, c.description, c.category |;
 
 344     if ($form->{project_id}) {
 
 348       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 350       JOIN ar a ON (a.id = ac.trans_id)
 
 351       JOIN parts p ON (ac.parts_id = p.id)
 
 352       JOIN chart c on (p.income_accno_id = c.id)
 
 354       -- use transdate from subwhere
 
 355       WHERE (c.category = 'I')
 
 359       GROUP BY c.accno, c.description, c.category
 
 363       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
 
 365       JOIN ap a ON (a.id = ac.trans_id)
 
 366       JOIN parts p ON (ac.parts_id = p.id)
 
 367       JOIN chart c on (p.expense_accno_id = c.id)
 
 369       WHERE (c.category = 'E')
 
 373       GROUP BY c.accno, c.description, c.category |;
 
 381   $sth = prepare_execute_query($form, $dbh, $query);
 
 383   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 385     if ($ref->{category} eq 'C') {
 
 386       $ref->{category} = 'A';
 
 389     # get last heading account
 
 390     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
 394         $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
 
 396         $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
 
 400     $form->{ $ref->{category} }{ $ref->{accno} }{accno}       = $ref->{accno};
 
 401     $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
 
 402     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
 
 405       $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
 
 407       $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
 
 412   # remove accounts with zero balance
 
 413   foreach $category (@{$categories}) {
 
 414     foreach $accno (keys %{ $form->{$category} }) {
 
 415       $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
 
 416       $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
 
 418       delete $form->{$category}{$accno}
 
 419         if (   $form->{$category}{$accno}{this} == 0
 
 420             && $form->{$category}{$accno}{last} == 0);
 
 424   $main::lxdebug->leave_sub();
 
 428   $main::lxdebug->enter_sub();
 
 430   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 432   my ($null, $department_id) = split /--/, $form->{department};
 
 445     $fromdate = conv_dateq($fromdate);
 
 446     if ($form->{method} eq 'cash') {
 
 447       $subwhere .= " AND (transdate    >= $fromdate)";
 
 448       $glwhere   = " AND (ac.transdate >= $fromdate)";
 
 449       $prwhere   = " AND (a.transdate  >= $fromdate)";
 
 451       $where    .= " AND (ac.transdate >= $fromdate)";
 
 456     $todate = conv_dateq($todate);
 
 457     $subwhere   .= " AND (transdate    <= $todate)";
 
 458     $where      .= " AND (ac.transdate <= $todate)";
 
 459     $prwhere    .= " AND (a.transdate  <= $todate)";
 
 462   if ($department_id) {
 
 463     $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
 
 464     $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 467   if ($form->{project_id}) {
 
 468     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
 
 471   if ($form->{method} eq 'cash') {
 
 474        SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 476          JOIN chart c ON (c.id = ac.chart_id)
 
 477          JOIN ar a ON (a.id = ac.trans_id)
 
 479          WHERE $where $dpt_where
 
 480            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
 
 486          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 488          JOIN chart c ON (c.id = ac.chart_id)
 
 489          JOIN ap a ON (a.id = ac.trans_id)
 
 491          WHERE $where $dpt_where
 
 492            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
 
 498          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 500          JOIN chart c ON (c.id = ac.chart_id)
 
 501          JOIN gl a ON (a.id = ac.trans_id)
 
 503          WHERE $where $dpt_where $glwhere
 
 504            AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 509     if ($form->{project_id}) {
 
 513          SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 515          JOIN ar a ON (a.id = ac.trans_id)
 
 516          JOIN parts p ON (ac.parts_id = p.id)
 
 517          JOIN chart c on (p.income_accno_id = c.id)
 
 519          WHERE (c.category = 'I') $prwhere $dpt_where
 
 520            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
 
 526          SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 528          JOIN ap a ON (a.id = ac.trans_id)
 
 529          JOIN parts p ON (ac.parts_id = p.id)
 
 530          JOIN chart c on (p.expense_accno_id = c.id)
 
 532          WHERE (c.category = 'E') $prwhere $dpt_where
 
 533            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
 
 539   } else {                      # if ($form->{method} eq 'cash')
 
 540     if ($department_id) {
 
 541       $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
 542       $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 546         SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 548         JOIN chart c ON (c.id = ac.chart_id)
 
 553         GROUP BY c.$category |;
 
 555     if ($form->{project_id}) {
 
 559         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 561         JOIN ar a ON (a.id = ac.trans_id)
 
 562         JOIN parts p ON (ac.parts_id = p.id)
 
 563         JOIN chart c on (p.income_accno_id = c.id)
 
 565         WHERE (c.category = 'I')
 
 573         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 575         JOIN ap a ON (a.id = ac.trans_id)
 
 576         JOIN parts p ON (ac.parts_id = p.id)
 
 577         JOIN chart c on (p.expense_accno_id = c.id)
 
 579         WHERE (c.category = 'E')
 
 583         GROUP BY c.$category |;
 
 591   my $sth = prepare_execute_query($form, $dbh, $query);
 
 593   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 594     if ($category eq "pos_bwa") {
 
 596         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
 598         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
 601       $form->{ $ref->{$category} } += $ref->{amount};
 
 606   $main::lxdebug->leave_sub();
 
 610   $main::lxdebug->enter_sub();
 
 612   my ($self, $myconfig, $form) = @_;
 
 614   my $dbh = $form->dbconnect($myconfig);
 
 616   my ($query, $sth, $ref);
 
 619   my ($null, $department_id) = split /--/, $form->{department};
 
 620   my @headingaccounts = ();
 
 626   my $invwhere = $where;
 
 628   if ($department_id) {
 
 629     $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
 
 630     $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 633   # project_id only applies to getting transactions
 
 634   # it has nothing to do with a trial balance
 
 635   # but we use the same function to collect information
 
 637   if ($form->{project_id}) {
 
 638     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 641   my $acc_cash_where = "";
 
 642 #  my $ar_cash_where = "";
 
 643 #  my $ap_cash_where = "";
 
 646   if ($form->{method} eq "cash") {
 
 647     $acc_cash_where = qq| AND (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM ap WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM gl WHERE transdate>='$form->{fromdate}' AND transdate<='$form->{todate}')) |;
 
 648 #    $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
 
 651   # get beginning balances
 
 653     qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
 
 655         LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 657         WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction $acc_cash_where
 
 660         GROUP BY c.accno, c.category, c.description |;
 
 662   $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
 
 664   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 666     if ($ref->{amount} != 0 || $form->{all_accounts}) {
 
 667       $trb{ $ref->{accno} }{description} = $ref->{description};
 
 668       $trb{ $ref->{accno} }{charttype}   = 'A';
 
 670       if ($ref->{amount} > 0) {
 
 671         $trb{ $ref->{accno} }{haben_eb}   = $ref->{amount};
 
 673         $trb{ $ref->{accno} }{soll_eb}   = $ref->{amount} * -1;
 
 675       $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 684     qq|SELECT c.accno, c.description, c.category
 
 686        WHERE c.charttype = 'H'
 
 689   $sth = prepare_execute_query($form, $dbh, $query);
 
 691   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 692     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 693     $trb{ $ref->{accno} }{charttype}   = 'H';
 
 694     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 696     push @headingaccounts, $ref->{accno};
 
 702   my $saldowhere    = " 1 = 1 ";
 
 703   my $sumwhere      = " 1 = 1 ";
 
 705   my $sumsubwhere   = '';
 
 706   my $saldosubwhere = '';
 
 707   my $glsaldowhere  = '';
 
 713   if ($form->{fromdate} || $form->{todate}) {
 
 714     if ($form->{fromdate}) {
 
 715       my $fromdate = conv_dateq($form->{fromdate});
 
 716       $tofrom        .= " AND (ac.transdate >= $fromdate)";
 
 717       $subwhere      .= " AND (transdate >= $fromdate)";
 
 718       $sumsubwhere   .= " AND (transdate >= (select date_trunc('year', date $fromdate))) ";
 
 719       $saldosubwhere .= " AND transdate>=(select date_trunc('year', date $fromdate))  ";
 
 720       $invwhere      .= " AND (a.transdate >= $fromdate)";
 
 721       $glsaldowhere  .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
 
 722       $glwhere        = " AND (ac.transdate >= $fromdate)";
 
 723       $glsumwhere     = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
 
 725     if ($form->{todate}) {
 
 726       my $todate = conv_dateq($form->{todate});
 
 727       $tofrom        .= " AND (ac.transdate <= $todate)";
 
 728       $invwhere      .= " AND (a.transdate <= $todate)";
 
 729       $saldosubwhere .= " AND (transdate <= $todate)";
 
 730       $sumsubwhere   .= " AND (transdate <= $todate)";
 
 731       $subwhere      .= " AND (transdate <= $todate)";
 
 732       $glwhere       .= " AND (ac.transdate <= $todate)";
 
 733       $glsumwhere    .= " AND (ac.transdate <= $todate) ";
 
 734       $glsaldowhere  .= " AND (ac.transdate <= $todate) ";
 
 738   if ($form->{method} eq "cash") {
 
 740       qq| AND ((ac.trans_id IN (SELECT id from ar) AND
 
 745                     JOIN chart ON (chart_id = id)
 
 746                     WHERE (link LIKE '%AR_paid%')
 
 751                (ac.trans_id in (SELECT id from ap) AND
 
 756                     JOIN chart ON (chart_id = id)
 
 757                     WHERE (link LIKE '%AP_paid%')
 
 762                (ac.trans_id in (SELECT id from gl)
 
 766 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
 
 771                     JOIN chart ON (chart_id = id)
 
 772                     WHERE (link LIKE '%AR_paid%')
 
 777                (ac.trans_id in (SELECT id from ap) AND
 
 782                     JOIN chart ON (chart_id = id)
 
 783                     WHERE (link LIKE '%AP_paid%')
 
 788                (ac.trans_id in (SELECT id from gl)
 
 792 qq| AND ((ac.trans_id IN (SELECT id from ar) AND
 
 797                     JOIN chart ON (chart_id = id)
 
 798                     WHERE (link LIKE '%AR_paid%')
 
 803                (ac.trans_id in (SELECT id from ap) AND
 
 808                     JOIN chart ON (chart_id = id)
 
 809                     WHERE (link LIKE '%AP_paid%')
 
 814                (ac.trans_id in (SELECT id from gl)
 
 819     $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 820     $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 821     $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 825        SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
 
 827        JOIN chart c ON (c.id = ac.chart_id)
 
 832        GROUP BY c.accno, c.description, c.category |;
 
 834   if ($form->{project_id}) {
 
 836       -- add project transactions from invoice
 
 840       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
 
 842       JOIN ar a ON (ac.trans_id = a.id)
 
 843       JOIN parts p ON (ac.parts_id = p.id)
 
 844       JOIN chart c ON (p.income_accno_id = c.id)
 
 849       GROUP BY c.accno, c.description, c.category
 
 853       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
 
 855       JOIN ap a ON (ac.trans_id = a.id)
 
 856       JOIN parts p ON (ac.parts_id = p.id)
 
 857       JOIN chart c ON (p.expense_accno_id = c.id)
 
 862       GROUP BY c.accno, c.description, c.category
 
 866   $query .= qq| ORDER BY accno|;
 
 868   $sth = prepare_execute_query($form, $dbh, $query);
 
 870   # calculate the debit and credit in the period
 
 871   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 872     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 873     $trb{ $ref->{accno} }{charttype}   = 'A';
 
 874     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 875     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
 879   # prepare query for each account
 
 880   my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
 
 884          (SELECT SUM(ac.amount) * -1
 
 886           JOIN chart c ON (c.id = ac.chart_id)
 
 892           AND (c.accno = ?)) AS debit,
 
 894          (SELECT SUM(ac.amount)
 
 896           JOIN chart c ON (c.id = ac.chart_id)
 
 902           AND c.accno = ?) AS credit,
 
 903         (SELECT SUM(ac.amount)
 
 905          JOIN chart c ON (ac.chart_id = c.id)
 
 910          AND c.accno = ?) AS saldo,
 
 912         (SELECT SUM(ac.amount)
 
 914          JOIN chart c ON (ac.chart_id = c.id)
 
 920          AND c.accno = ?) AS sum_credit,
 
 922         (SELECT SUM(ac.amount)
 
 924          JOIN chart c ON (ac.chart_id = c.id)
 
 930          AND c.accno = ?) AS sum_debit,
 
 932         (SELECT max(ac.transdate) FROM acc_trans ac
 
 933         JOIN chart c ON (ac.chart_id = c.id)
 
 938         AND c.accno = ?) AS last_transaction
 
 943   $drcr = prepare_query($form, $dbh, $q_drcr);
 
 945   if ($form->{project_id}) {
 
 946     # prepare query for each account
 
 949           (SELECT SUM(ac.sellprice * ac.qty) * -1
 
 951            JOIN parts p ON (ac.parts_id = p.id)
 
 952            JOIN ap a ON (ac.trans_id = a.id)
 
 953            JOIN chart c ON (p.expense_accno_id = c.id)
 
 958            AND c.accno = ?) AS debit,
 
 960           (SELECT SUM(ac.sellprice * ac.qty)
 
 962            JOIN parts p ON (ac.parts_id = p.id)
 
 963            JOIN ar a ON (ac.trans_id = a.id)
 
 964            JOIN chart c ON (p.income_accno_id = c.id)
 
 969            AND c.accno = ?) AS credit,
 
 971         (SELECT SUM(ac.amount)
 
 973          JOIN chart c ON (ac.chart_id = c.id)
 
 978          AND c.accno = ?) AS saldo,
 
 980         (SELECT SUM(ac.amount)
 
 982          JOIN chart c ON (ac.chart_id = c.id)
 
 988          AND c.accno = ?) AS sum_credit,
 
 990         (SELECT SUM(ac.amount)
 
 992          JOIN chart c ON (ac.chart_id = c.id)
 
 998          AND c.accno = ?) AS sum_debit,
 
1001         (SELECT max(ac.transdate) FROM acc_trans ac
 
1002         JOIN chart c ON (ac.chart_id = c.id)
 
1007         AND c.accno = ?) AS last_transaction
 
1010     $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
 
1014   my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
 
1016   foreach my $accno (sort keys %trb) {
 
1019     $ref->{accno} = $accno;
 
1020     map { $ref->{$_} = $trb{$accno}{$_} }
 
1021       qw(description category charttype amount soll_eb haben_eb);
 
1023     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1025     if ($trb{$accno}{charttype} eq 'A') {
 
1028       do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1030       ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
 
1031       my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
 
1032       $last_transaction = "";
 
1033       while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
 
1034         $ref->{debit}  += $debit;
 
1035         $ref->{credit} += $credit;
 
1037           $ref->{haben_saldo} += $saldo;
 
1039           $ref->{soll_saldo} += $saldo * -1;
 
1041         $ref->{last_transaction} = $last_transaction;
 
1042         $ref->{soll_kumuliert} = $soll_kumuliert * -1;
 
1043         $ref->{haben_kumuliert} = $haben_kumuliert;
 
1047       if ($form->{project_id}) {
 
1050         do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1052         ($debit, $credit) = (0, 0);
 
1053         while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
 
1054           $ref->{debit}  += $debit;
 
1055           $ref->{credit} += $credit;
 
1057             $ref->{haben_saldo} += $saldo;
 
1059             $ref->{soll_saldo} += $saldo * -1;
 
1061           $ref->{soll_kumuliert} += $soll_kumuliert * -1;
 
1062           $ref->{haben_kumuliert} += $haben_kumuliert;
 
1064         $project_drcr->finish;
 
1067       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1068       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1069       $ref->{haben_saldo}  = $form->round_amount($ref->{haben_saldo},  2);
 
1070       $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
 
1071       $ref->{haben_kumuliert}  = $form->round_amount($ref->{haben_kumuliert},  2);
 
1072       $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
 
1077     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1078     $accno = pop @accno;
 
1080       $trb{$accno}{debit}  += $ref->{debit};
 
1081       $trb{$accno}{credit} += $ref->{credit};
 
1082       $trb{$accno}{soll_saldo}  += $ref->{soll_saldo};
 
1083       $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
 
1084       $trb{$accno}{soll_kumuliert}  += $ref->{soll_kumuliert};
 
1085       $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
 
1088     push @{ $form->{TB} }, $ref;
 
1094   # debits and credits for headings
 
1095   foreach my $accno (@headingaccounts) {
 
1096     foreach $ref (@{ $form->{TB} }) {
 
1097       if ($accno eq $ref->{accno}) {
 
1098         $ref->{debit}  = $trb{$accno}{debit};
 
1099         $ref->{credit} = $trb{$accno}{credit};
 
1100         $ref->{soll_saldo}  = $trb{$accno}{soll_saldo};
 
1101         $ref->{haben_saldo} = $trb{$accno}{haben_saldo};
 
1102         $ref->{soll_kumuliert}  = $trb{$accno}{soll_kumuliert};
 
1103         $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};      }
 
1107   $main::lxdebug->leave_sub();
 
1111   $main::lxdebug->enter_sub();
 
1112   my ($self, $dbh, $form) = @_;
 
1113   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
1114   my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
 
1115   my $sth =  $dbh->prepare($query);
 
1116   while(my $ref = $sth->fetchrow_hashref()) {
 
1117     $ref->{invnumer} =~ s/Storno zu //g;
 
1118     $form->{storno}{$ref->{invnumber}} = 1;
 
1120   $main::lxdebug->leave_sub();
 
1124   $main::lxdebug->enter_sub();
 
1126   my ($self, $myconfig, $form) = @_;
 
1128   # connect to database
 
1129   my $dbh     = $form->dbconnect($myconfig);
 
1131   my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
 
1133   if ($form->{ct} eq "customer") {
 
1146   $ct_id = "${ct}_id";
 
1148   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
 
1149   my $todate = conv_dateq($form->{todate});
 
1150   my $fromdate = conv_dateq($form->{fromdate});
 
1152   my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
 
1154   my $where = " 1 = 1 ";
 
1157   if ($form->{$ct_id}) {
 
1158     $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
 
1159   } elsif ($form->{ $form->{ct} }) {
 
1160     $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
 
1164   if ($form->{department}) {
 
1165     my ($null, $department_id) = split /--/, $form->{department};
 
1166     $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
 
1167     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
1171     -- between 0-30 days
 
1173     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1174       street, zipcode, city, country, contact, email,
 
1175       phone as customerphone, fax as customerfax, ${ct}number,
 
1176       "invnumber", "transdate",
 
1177       (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans LEFT JOIN chart ON (acc_trans.chart_id=chart.id) WHERE link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
 
1178       "duedate", invoice, ${arap}.id,
 
1181        WHERE (${arap}.curr = exchangerate.curr)
 
1182          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1184     WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
 
1185       AND (${arap}.storno IS FALSE)
 
1186       AND (${arap}.${ct}_id = ${ct}.id)
 
1188       AND (transdate <= (date $todate) $fromwhere )
 
1190     ORDER BY ctid, transdate, invnumber |;
 
1192   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1194   # select outstanding vendors or customers, depends on $ct
 
1196     qq|SELECT DISTINCT ct.id, ct.name
 
1197        FROM $ct ct, $arap a
 
1200          AND (a.${ct_id} = ct.id)
 
1201          AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
 
1202          AND (a.transdate <= $todate $fromwhere)
 
1205   my $sth = prepare_execute_query($form, $dbh, $query);
 
1208   # for each company that has some stuff outstanding
 
1209   while (my ($id) = $sth->fetchrow_array) {
 
1210     do_statement($form, $sth_details, $q_details, $id);
 
1212     while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
 
1213       $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
 
1214       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1215       push @{ $form->{AG} }, $ref;
 
1218     $sth_details->finish;
 
1227   $main::lxdebug->leave_sub();
 
1231   $main::lxdebug->enter_sub();
 
1233   my ($self, $myconfig, $form) = @_;
 
1235   # connect to database
 
1236   my $dbh = $form->dbconnect($myconfig);
 
1238   my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
 
1241     qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1244   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1245     selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
 
1248   $main::lxdebug->leave_sub();
 
1251 sub get_taxaccounts {
 
1252   $main::lxdebug->enter_sub();
 
1254   my ($self, $myconfig, $form) = @_;
 
1256   # connect to database
 
1257   my $dbh = $form->dbconnect($myconfig);
 
1261     qq|SELECT c.accno, c.description, t.rate
 
1263        WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
 
1265   $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
 
1269   $main::lxdebug->leave_sub();
 
1273   $main::lxdebug->enter_sub();
 
1275   my ($self, $myconfig, $form) = @_;
 
1277   # connect to database
 
1278   my $dbh = $form->dbconnect($myconfig);
 
1280   my ($null, $department_id) = split /--/, $form->{department};
 
1283   my $where = "1 = 1";
 
1285   if ($department_id) {
 
1286     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
1291   if ($form->{accno}) {
 
1292     $accno = $form->{accno};
 
1293     $rate  = $form->{"$form->{accno}_rate"};
 
1294     $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
 
1300   if ($form->{db} eq 'ar') {
 
1301     $table = "customer";
 
1308   my $arap = lc($ARAP);
 
1310   my $transdate = "a.transdate";
 
1312   if ($form->{method} eq 'cash') {
 
1313     $transdate = "a.datepaid";
 
1315     my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
 
1322           JOIN chart ON (chart_id = id)
 
1323           WHERE (link LIKE '%${ARAP}_paid%')
 
1324           AND (transdate <= $todate)
 
1329   # if there are any dates construct a where
 
1330   $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
 
1331   $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
 
1333   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1335   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1336   $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
 
1339   if ($form->{report} !~ /nontaxable/) {
 
1341       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
 
1342           ac.amount * $ml AS tax
 
1344          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1345          JOIN chart ch ON (ch.id = ac.chart_id)
 
1346          JOIN $table n ON (n.id = a.${table}_id)
 
1350            AND (a.invoice = '0')
 
1354          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1355            i.sellprice * i.qty * $rate * $ml AS tax
 
1357          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1358          JOIN chart ch ON (ch.id = ac.chart_id)
 
1359          JOIN $table n ON (n.id = a.${table}_id)
 
1360          JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1361          JOIN invoice i ON (i.trans_id = a.id)
 
1362          JOIN partstax p ON (p.parts_id = i.parts_id)
 
1366            AND (a.invoice = '1')
 
1367          ORDER BY $sortorder|;
 
1369     # only gather up non-taxable transactions
 
1371       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
 
1373          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1374          JOIN $table n ON (n.id = a.${table}_id)
 
1377            AND (a.invoice = '0')
 
1378            AND (a.netamount = a.amount)
 
1382          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
 
1384          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1385          JOIN $table n ON (n.id = a.${table}_id)
 
1386          JOIN invoice i ON (i.trans_id = a.id)
 
1389            AND (a.invoice = '1')
 
1391              a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
 
1393              i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
 
1395          GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
 
1396          ORDER by $sortorder|;
 
1399   $form->{TR} = selectall_hashref_query($form, $dbh, $query);
 
1403   $main::lxdebug->leave_sub();
 
1406 sub paymentaccounts {
 
1407   $main::lxdebug->enter_sub();
 
1409   my ($self, $myconfig, $form) = @_;
 
1411   # connect to database, turn AutoCommit off
 
1412   my $dbh = $form->dbconnect_noauto($myconfig);
 
1414   my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
 
1416   # get A(R|P)_paid accounts
 
1418     qq|SELECT accno, description
 
1420        WHERE link LIKE '%${ARAP}_paid%'|;
 
1421   $form->{PR} = selectall_hashref_query($form, $dbh, $query);
 
1425   $main::lxdebug->leave_sub();
 
1429   $main::lxdebug->enter_sub();
 
1431   my ($self, $myconfig, $form) = @_;
 
1433   # connect to database, turn AutoCommit off
 
1434   my $dbh = $form->dbconnect_noauto($myconfig);
 
1439   if ($form->{db} eq 'ar') {
 
1440     $table = 'customer';
 
1452   if ($form->{department_id}) {
 
1453     $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
1454     $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
 
1457   if ($form->{fromdate}) {
 
1458     $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
 
1460   if ($form->{todate}) {
 
1461     $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
 
1463   if (!$form->{fx_transaction}) {
 
1464     $where .= " AND ac.fx_transaction = '0'";
 
1469   if ($form->{reference}) {
 
1470     $reference = $dbh->quote('%' . $form->{reference} . '%');
 
1471     $invnumber = " AND (a.invnumber LIKE $reference)";
 
1472     $reference = " AND (g.reference LIKE $reference)";
 
1474   if ($form->{source}) {
 
1475     $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
 
1477   if ($form->{memo}) {
 
1478     $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
 
1481   my %sort_columns =  (
 
1482     'transdate'    => [ qw(transdate lower_invnumber lower_name) ],
 
1483     'invnumber'    => [ qw(lower_invnumber lower_name transdate) ],
 
1484     'name'         => [ qw(lower_name transdate)                 ],
 
1485     'source'       => [ qw(lower_source)                         ],
 
1486     'memo'         => [ qw(lower_memo)                           ],
 
1488   my %lowered_columns =  (
 
1489     'invnumber'       => { 'gl' => 'g.reference',   'arap' => 'a.invnumber', },
 
1490     'memo'            => { 'gl' => 'ac.memo',       'arap' => 'ac.memo',     },
 
1491     'source'          => { 'gl' => 'ac.source',     'arap' => 'ac.source',   },
 
1492     'name'            => { 'gl' => 'g.description', 'arap' => 'c.name',      },
 
1495   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
1496   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
 
1497   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
1500   my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
 
1501   foreach my $spec (@{ $sort_columns{$sortkey} }) {
 
1502     next if ($spec !~ m/^lower_(.*)$/);
 
1505     map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
 
1508   $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
 
1509   $sth = prepare_query($form, $dbh, $query);
 
1512       qq|SELECT c.name, a.invnumber, a.ordnumber,
 
1513            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1514            a.invoice, a.id, ac.memo, '${arap}' AS module
 
1515            $columns_for_sorting{arap}
 
1517          JOIN $arap a ON (ac.trans_id = a.id)
 
1518          JOIN $table c ON (c.id = a.${table}_id)
 
1520          WHERE (ac.chart_id = ?)
 
1526          SELECT g.description, g.reference, NULL AS ordnumber,
 
1527            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1528            '0' as invoice, g.id, ac.memo, 'gl' AS module
 
1529            $columns_for_sorting{gl}
 
1531          JOIN gl g ON (g.id = ac.trans_id)
 
1533          WHERE (ac.chart_id = ?)
 
1536            AND (ac.amount * $ml) > 0
 
1538          ORDER BY $sortorder|;
 
1539   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1543   # cycle through each id
 
1544   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
1545     do_statement($form, $sth, $query, $accno);
 
1546     my $ref = $sth->fetchrow_hashref();
 
1547     push(@{ $form->{PR} }, $ref);
 
1550     $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
 
1552     do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
 
1553     while (my $pr = $sth_details->fetchrow_hashref()) {
 
1554       push(@{ $form->{ $ref->{id} } }, $pr);
 
1556     $sth_details->finish();
 
1561   $main::lxdebug->leave_sub();
 
1565   $main::lxdebug->enter_sub();
 
1567   my ($self, $myconfig, $form) = @_;
 
1569   # connect to database
 
1570   my $dbh = $form->dbconnect($myconfig);
 
1572   my $last_period = 0;
 
1575     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);
 
1577   $form->{decimalplaces} *= 1;
 
1579   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
 
1581   # if there are any compare dates
 
1583   if ($form->{fromdate} || $form->{todate}) {
 
1585     if ($form->{fromdate}) {
 
1586       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1589       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1592     my $kummfromdate = $form->{comparefromdate};
 
1593     my $kummtodate   = $form->{comparetodate};
 
1594     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
 
1597   my @periods        = qw(jetzt kumm);
 
1598   my @gesamtleistung = qw(1 2 3);
 
1599   my @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 19 20);
 
1601     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
1603   foreach my $key (@periods) {
 
1604     $form->{ "$key" . "gesamtleistung" } = 0;
 
1605     $form->{ "$key" . "gesamtkosten" }   = 0;
 
1607     foreach $category (@categories) {
 
1609       if (defined($form->{$category}{$key})) {
 
1610         $form->{"$key$category"} =
 
1611           $form->format_amount($myconfig,
 
1612                                $form->round_amount($form->{$category}{$key}, 2
 
1614                                $form->{decimalplaces},
 
1618     foreach my $item (@gesamtleistung) {
 
1619       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
1621     foreach my $item (@gesamtkosten) {
 
1622       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
1624     $form->{ "$key" . "rohertrag" } =
 
1625       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
1626     $form->{ "$key" . "betriebrohertrag" } =
 
1627       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
1628     $form->{ "$key" . "betriebsergebnis" } =
 
1629       $form->{ "$key" . "betriebrohertrag" } -
 
1630       $form->{ "$key" . "gesamtkosten" };
 
1631     $form->{ "$key" . "neutraleraufwand" } =
 
1632       $form->{30}{$key} + $form->{31}{$key};
 
1633     $form->{ "$key" . "neutralertrag" } =
 
1634       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
1635     $form->{ "$key" . "ergebnisvorsteuern" } =
 
1636       $form->{ "$key" . "betriebsergebnis" } -
 
1637       $form->{ "$key" . "neutraleraufwand" } +
 
1638       $form->{ "$key" . "neutralertrag" };
 
1639     $form->{ "$key" . "ergebnis" } =
 
1640       $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
 
1642     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
1643       foreach $category (@categories) {
 
1644         if (defined($form->{$category}{$key})) {
 
1645           $form->{ "$key" . "gl" . "$category" } =
 
1646             $form->format_amount(
 
1648                                $form->round_amount(
 
1649                                  ($form->{$category}{$key} /
 
1650                                     $form->{ "$key" . "gesamtleistung" } * 100
 
1652                                  $form->{decimalplaces}
 
1654                                $form->{decimalplaces},
 
1658       foreach my $item (@ergebnisse) {
 
1659         $form->{ "$key" . "gl" . "$item" } =
 
1660           $form->format_amount($myconfig,
 
1661                                $form->round_amount(
 
1662                                  ( $form->{ "$key" . "$item" } /
 
1663                                      $form->{ "$key" . "gesamtleistung" } * 100
 
1665                                  $form->{decimalplaces}
 
1667                                $form->{decimalplaces},
 
1672     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
1673       foreach $category (@categories) {
 
1674         if (defined($form->{$category}{$key})) {
 
1675           $form->{ "$key" . "gk" . "$category" } =
 
1676             $form->format_amount($myconfig,
 
1677                                  $form->round_amount(
 
1678                                    ($form->{$category}{$key} /
 
1679                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1681                                    $form->{decimalplaces}
 
1683                                  $form->{decimalplaces},
 
1687       foreach my $item (@ergebnisse) {
 
1688         $form->{ "$key" . "gk" . "$item" } =
 
1689           $form->format_amount($myconfig,
 
1690                                $form->round_amount(
 
1691                                    ($form->{ "$key" . "$item" } /
 
1692                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1694                                    $form->{decimalplaces}
 
1696                                $form->{decimalplaces},
 
1701     if ($form->{10}{$key} > 0) {
 
1702       foreach $category (@categories) {
 
1703         if (defined($form->{$category}{$key})) {
 
1704           $form->{ "$key" . "pk" . "$category" } =
 
1705             $form->format_amount(
 
1707                         $form->round_amount(
 
1708                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
1709                           $form->{decimalplaces}
 
1711                         $form->{decimalplaces},
 
1715       foreach my $item (@ergebnisse) {
 
1716         $form->{ "$key" . "pk" . "$item" } =
 
1717           $form->format_amount($myconfig,
 
1718                                $form->round_amount(
 
1719                                                 ($form->{ "$key" . "$item" } /
 
1720                                                    $form->{10}{$key} * 100
 
1722                                                 $form->{decimalplaces}
 
1724                                $form->{decimalplaces},
 
1729     if ($form->{4}{$key} > 0) {
 
1730       foreach $category (@categories) {
 
1731         if (defined($form->{$category}{$key})) {
 
1732           $form->{ "$key" . "auf" . "$category" } =
 
1733             $form->format_amount(
 
1735                          $form->round_amount(
 
1736                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
1737                            $form->{decimalplaces}
 
1739                          $form->{decimalplaces},
 
1743       foreach my $item (@ergebnisse) {
 
1744         $form->{ "$key" . "auf" . "$item" } =
 
1745           $form->format_amount($myconfig,
 
1746                                $form->round_amount(
 
1747                                                 ($form->{ "$key" . "$item" } /
 
1748                                                    $form->{4}{$key} * 100
 
1750                                                 $form->{decimalplaces}
 
1752                                $form->{decimalplaces},
 
1757     foreach my $item (@ergebnisse) {
 
1758       $form->{ "$key" . "$item" } =
 
1759         $form->format_amount($myconfig,
 
1760                              $form->round_amount($form->{ "$key" . "$item" },
 
1761                                                  $form->{decimalplaces}
 
1763                              $form->{decimalplaces},
 
1770   $main::lxdebug->leave_sub();
 
1774   $main::lxdebug->enter_sub();
 
1776   my ($self, $myconfig, $form) = @_;
 
1778   # connect to database
 
1779   my $dbh = $form->dbconnect($myconfig);
 
1781   my $last_period     = 0;
 
1782   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
 
1783     96 66 43 45 53 62 65 67);
 
1784   my @categories_euro = qw(48 51 86 91 97 93 94);
 
1785   $form->{decimalplaces} *= 1;
 
1787   foreach my $item (@categories_cent) {
 
1788     $form->{"$item"} = 0;
 
1790   foreach my $item (@categories_euro) {
 
1791     $form->{"$item"} = 0;
 
1794   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
 
1796   #   foreach $item (@categories_cent) {
 
1797   #     if ($form->{$item}{"jetzt"} > 0) {
 
1798   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1799   #             delete $form->{$item}{"jetzt"};
 
1802   #   foreach $item (@categories_euro) {
 
1803   #     if ($form->{$item}{"jetzt"} > 0) {
 
1804   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1805   #             delete $form->{$item}{"jetzt"};
 
1806   #     }  foreach $item (@categories_cent) {
 
1807   #     if ($form->{$item}{"jetzt"} > 0) {
 
1808   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1809   #             delete $form->{$item}{"jetzt"};
 
1812   #   foreach $item (@categories_euro) {
 
1813   #     if ($form->{$item}{"jetzt"} > 0) {
 
1814   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1815   #             delete $form->{$item}{"jetzt"};
 
1822   # Berechnung der USTVA Formularfelder
 
1824   $form->{"51r"} = $form->{"511"};
 
1825   $form->{"86r"} = $form->{"861"};
 
1826   $form->{"97r"} = $form->{"971"};
 
1827   $form->{"93r"} = $form->{"931"};
 
1829   #$form->{"96"}  = $form->{"94"} * 0.16;
 
1831     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
 
1833   $form->{"45"} = $form->{"43"};
 
1834   $form->{"53"} = $form->{"43"};
 
1835   $form->{"62"} = $form->{"43"} - $form->{"66"};
 
1836   $form->{"65"} = $form->{"43"} - $form->{"66"};
 
1837   $form->{"67"} = $form->{"43"} - $form->{"66"};
 
1839   foreach my $item (@categories_cent) {
 
1841       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
 
1845   foreach my $item (@categories_euro) {
 
1847       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
 
1853   $main::lxdebug->leave_sub();
 
1856 sub income_statement {
 
1857   $main::lxdebug->enter_sub();
 
1859   my ($self, $myconfig, $form) = @_;
 
1861   # connect to database
 
1862   my $dbh = $form->dbconnect($myconfig);
 
1864   my $last_period          = 0;
 
1865   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
1866   my @categories_ausgaben  =
 
1867     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);
 
1869   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
1871   $form->{decimalplaces} *= 1;
 
1873   foreach my $item (@categories_einnahmen) {
 
1876   foreach my $item (@categories_ausgaben) {
 
1880   foreach my $item (@ergebnisse) {
 
1884   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
1887   foreach my $item (@categories_einnahmen) {
 
1888     $form->{"eur${item}"} =
 
1889       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1890     $form->{"sumeura"} += $form->{$item};
 
1892   foreach my $item (@categories_ausgaben) {
 
1893     $form->{"eur${item}"} =
 
1894       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1895     $form->{"sumeurb"} += $form->{$item};
 
1898   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
1900   foreach my $item (@ergebnisse) {
 
1902       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2));
 
1904   $main::lxdebug->leave_sub();