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 sub get_balance_starting_date {
 
  55   # determine date from which the balance is calculated. The method is
 
  56   # configured in the client configuration.
 
  59   return unless $asofdate;
 
  61   $asofdate = $::locale->parse_date_to_object($asofdate);
 
  63   my $form = $main::form;
 
  64   my $dbh  = $::form->get_standard_dbh;
 
  66   my $startdate_method = $::instance_conf->get_balance_startdate_method;
 
  68   # We could use the following objects to determine the starting date for
 
  69   # calculating the balance from asofdate (the reference date for the balance):
 
  70   # * start_of_year - 1.1., no deviating fiscal year supported
 
  71   # * closed_to - all transactions since the books were last closed
 
  72   # * last_ob - all transactions since last opening balance transaction (usually 1.1.)
 
  73   # * mindate - all transactions in database
 
  75   my $start_of_year = $asofdate->clone();
 
  76   $start_of_year->set_day(1);
 
  77   $start_of_year->set_month(1);
 
  79   # closedto assumes that we only close the books at the end of a fiscal year,
 
  80   # never during the fiscal year. If this assumption is valid closedto should
 
  81   # also work for deviating fiscal years. But as the trial balance (SuSa)
 
  82   # doesn't yet deal with deviating fiscal years, and is useful to also close
 
  83   # the books after a month has been exported via DATEV, so this method of
 
  84   # determining the starting date isn't recommended and has been removed as
 
  86   my ($closedto) = selectfirst_array_query($form, $dbh, 'SELECT closedto FROM defaults');
 
  88     $closedto = $::locale->parse_date_to_object($closedto);
 
  89     $closedto->subtract(years => 1) while ($asofdate - $closedto)->is_negative;
 
  90     $closedto->add(days => 1);
 
  93   my ($query, $startdate, $last_ob, $mindate);
 
  94   $query = qq|select max(transdate) from acc_trans where ob_transaction is true and transdate <= ?|;
 
  95   ($last_ob) = selectrow_query($::form, $dbh, $query, $::locale->format_date(\%::myconfig, $asofdate));
 
  96   $last_ob = $::locale->parse_date_to_object($last_ob) if $last_ob;
 
  98   $query = qq|select min(transdate) from acc_trans|;
 
  99   ($mindate) = selectrow_query($::form, $dbh, $query);
 
 100   $mindate = $::locale->parse_date_to_object($mindate);
 
 102   # the default method is to use all transactions ($mindate)
 
 104   if ( $startdate_method eq 'closed_to' and $closedto ) {
 
 105     # if no closedto is configured use default
 
 106     return $::locale->format_date(\%::myconfig, $closedto);
 
 108   } elsif ( $startdate_method eq 'start_of_year' ) {
 
 110     return $::locale->format_date(\%::myconfig, $start_of_year);
 
 112   } elsif ( $startdate_method eq 'all_transactions' ) {
 
 114     return $::locale->format_date(\%::myconfig, $mindate);
 
 116   } elsif ( $startdate_method eq 'last_ob_or_all_transactions' and $last_ob ) {
 
 117     # use default if there are no ob transactions
 
 119     return $::locale->format_date(\%::myconfig, $last_ob);
 
 121   } elsif ( $startdate_method eq 'last_ob_or_start_of_year' ) {
 
 124       return $::locale->format_date(\%::myconfig, $last_ob);
 
 126       return $::locale->format_date(\%::myconfig, $start_of_year);
 
 130     # default action, also used for closedto and last_ob_or_all_transactions if
 
 131     # there are no valid dates
 
 133     return $::locale->format_date(\%::myconfig, $mindate);
 
 139   $main::lxdebug->enter_sub();
 
 141   my $myconfig = \%main::myconfig;
 
 142   my $form     = $main::form;
 
 143   my $dbh      = $::form->get_standard_dbh;
 
 146   my @categories  = qw(A C L Q);
 
 148   # if there are any dates construct a where
 
 149   if ($form->{asofdate}) {
 
 150     $form->{period} = $form->{this_period} = conv_dateq($form->{asofdate});
 
 153   # get starting date for calculating balance
 
 154   $form->{this_startdate} = get_balance_starting_date($form->{asofdate});
 
 156   get_accounts($dbh, $last_period, $form->{this_startdate}, $form->{asofdate}, $form, \@categories);
 
 158   # if there are any compare dates
 
 159   if ($form->{compareasofdate}) {
 
 162     $form->{last_startdate} = get_balance_starting_date($form->{compareasofdate});
 
 164     get_accounts($dbh, $last_period, $form->{last_startdate} , $form->{compareasofdate}, $form, \@categories);
 
 165     $form->{last_period} = conv_dateq($form->{compareasofdate});
 
 168   # now we got $form->{A}{accno}{ }    assets
 
 169   # and $form->{L}{accno}{ }           liabilities
 
 170   # and $form->{Q}{accno}{ }           equity
 
 171   # build asset accounts
 
 173   my %account = ('A' => { 'ml'     => -1 },
 
 174                  'L' => { 'ml'     =>  1 },
 
 175                  'Q' => { 'ml'     =>  1 });
 
 179   foreach my $category (grep { !/C/ } @categories) {
 
 181     $TMPL_DATA->{$category} = [];
 
 182     my $ml  = $account{$category}{ml};
 
 184     foreach my $key (sort keys %{ $form->{$category} }) {
 
 186       my $row = { %{ $form->{$category}{$key} } };
 
 188       # if charttype "heading" - calculate this entry, start a new batch of charts belonging to this heading and skip the rest bo the loop
 
 189       # header charts are not real charts. start a sub aggregation with them, but don't calculate anything with them
 
 190       if ($row->{charttype} eq "H") {
 
 191         if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 192           $row->{subdescription} = $account{$category}{subdescription};
 
 193           $row->{this}           = $account{$category}{subthis} * $ml;                   # format: $dec, $dash
 
 194           $row->{last}           = $account{$category}{sublast} * $ml if $last_period;   # format: $dec, $dash
 
 197         $row->{subheader} = 1;
 
 198         $account{$category}{subthis}        = $row->{this};
 
 199         $account{$category}{sublast}        = $row->{last};
 
 200         $account{$category}{subdescription} = $row->{description};
 
 201         $account{$category}{subtotal} = 1;
 
 206         next unless $form->{l_heading};
 
 209       for my $period (qw(this last)) {
 
 210         next if ($period eq 'last' && !$last_period);
 
 212         $row->{$period}                    *= $ml;
 
 215       push @{ $TMPL_DATA->{$category} }, $row;
 
 218     # resolve heading/subtotal
 
 219     if ($account{$category}{subtotal} && $form->{l_subtotal}) {
 
 220       $TMPL_DATA->{$category}[-1]{subdescription} = $account{$category}{subdescription};
 
 221       $TMPL_DATA->{$category}[-1]{this}           = $account{$category}{subthis} * $ml;                   # format: $dec, $dash
 
 222       $TMPL_DATA->{$category}[-1]{last}           = $account{$category}{sublast} * $ml if $last_period;   # format: $dec, $dash
 
 225     $TMPL_DATA->{total}{$category}{this} = sum map { $_->{this} } @{ $TMPL_DATA->{$category} };
 
 226     $TMPL_DATA->{total}{$category}{last} = sum map { $_->{last} } @{ $TMPL_DATA->{$category} };
 
 229   for my $period (qw(this last)) {
 
 230     next if ($period eq 'last' && !$last_period);
 
 232     $form->{E}{$period}             = $TMPL_DATA->{total}{A}{$period} - $TMPL_DATA->{total}{L}{$period} - $TMPL_DATA->{total}{Q}{$period};
 
 233     $TMPL_DATA->{total}{Q}{$period}     += $form->{E}{$period};
 
 234     $TMPL_DATA->{total}{$period}    = $TMPL_DATA->{total}{L}{$period} + $TMPL_DATA->{total}{Q}{$period};
 
 236     $form->{E}{description}='nicht verbuchter Gewinn/Verlust';
 
 237   push @{ $TMPL_DATA->{Q} }, $form->{E};
 
 239   $main::lxdebug->leave_sub();
 
 245   $main::lxdebug->enter_sub();
 
 247   my ($dbh, $last_period, $fromdate, $todate, $form, $categories) = @_;
 
 249   my ($null, $department_id) = split /--/, $form->{department};
 
 253   my $dpt_where_without_arapgl = '';
 
 260   my $dec = $form->{decimalplaces};
 
 262   my $category = qq| AND (| . join(" OR ", map({ "(c.category = " . $dbh->quote($_) . ")" } @{$categories})) . qq|) |;
 
 266     qq|SELECT c.accno, c.description, c.category
 
 268        WHERE (c.charttype = 'H')
 
 272   $sth = prepare_execute_query($form, $dbh, $query);
 
 274   my @headingaccounts = ();
 
 275   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 276     $form->{ $ref->{category} }{ $ref->{accno} }{description} =
 
 277       "$ref->{description}";
 
 278     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "H";
 
 279     $form->{ $ref->{category} }{ $ref->{accno} }{accno}     = $ref->{accno};
 
 281     push @headingaccounts, $ref->{accno};
 
 286   # filter for opening and closing bookings
 
 287   # if l_ob is selected l_cb is always ignored
 
 288   if ( $last_period ) {
 
 289     # ob/cb-settings for "compared to" balance
 
 290     if ( $form->{l_ob_compared} ) {
 
 291       $where .= ' AND ac.ob_transaction is true  '
 
 292     } elsif ( not $form->{l_cb_compared} ) {
 
 293       $where .= ' AND ac.cb_transaction is false ';
 
 296     # ob/cb-settings for "as of" balance
 
 297     if ( $form->{l_ob} ) {
 
 298       $where .= ' AND ac.ob_transaction is true  '
 
 299     } elsif ( not $form->{l_cb} ) {
 
 300       $where .= ' AND ac.cb_transaction is false ';
 
 306     $fromdate = conv_dateq($fromdate);
 
 307     if ($form->{method} eq 'cash') {
 
 308       $subwhere .= " AND (transdate >= $fromdate)";
 
 309       $glwhere = " AND (ac.transdate >= $fromdate)";
 
 311       $where .= " AND (ac.transdate >= $fromdate)";
 
 316     $todate = conv_dateq($todate);
 
 317     $where    .= " AND (ac.transdate <= $todate)";
 
 318     $subwhere .= " AND (transdate <= $todate)";
 
 321   if ($department_id) {
 
 322     $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
 325   if ($form->{project_id}) {
 
 326     # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine
 
 327     # Projekte auswählen kann
 
 328     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 331   if ($form->{method} eq 'cash') {
 
 333       qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 335          JOIN chart c ON (c.id = ac.chart_id)
 
 336          JOIN ar a ON (a.id = ac.trans_id)
 
 344                WHERE (a.chart_link LIKE '%AR_paid%')
 
 348          GROUP BY c.accno, c.description, c.category
 
 352          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 354          JOIN chart c ON (c.id = ac.chart_id)
 
 355          JOIN ap a ON (a.id = ac.trans_id)
 
 363                WHERE (a.chart_link LIKE '%AP_paid%')
 
 367          GROUP BY c.accno, c.description, c.category
 
 371          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 373          JOIN chart c ON (c.id = ac.chart_id)
 
 374          JOIN gl a ON (a.id = ac.trans_id)
 
 379              AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
 
 381          GROUP BY c.accno, c.description, c.category |;
 
 383     if ($form->{project_id}) {
 
 384       # s.o. keine Projektauswahl in Bilanz
 
 389          SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 391          JOIN ar a ON (a.id = ac.trans_id)
 
 392          JOIN parts p ON (ac.parts_id = p.id)
 
 393          JOIN chart c on (p.income_accno_id = c.id)
 
 394          -- use transdate from subwhere
 
 395          WHERE (c.category = 'I')
 
 402                WHERE (a.chart_link LIKE '%AR_paid%')
 
 406          GROUP BY c.accno, c.description, c.category
 
 410          SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
 
 412          JOIN ap a ON (a.id = ac.trans_id)
 
 413          JOIN parts p ON (ac.parts_id = p.id)
 
 414          JOIN chart c on (p.expense_accno_id = c.id)
 
 415          WHERE (c.category = 'E')
 
 422                WHERE a.chart_link LIKE '%AP_paid%'
 
 426          GROUP BY c.accno, c.description, c.category |;
 
 429   } else {                      # if ($form->{method} eq 'cash')
 
 430     if ($department_id) {
 
 431       $dpt_where = qq| AND a.department_id = | . conv_i($department_id);
 
 432       $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
 
 433                                                    (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
 
 434                                                    (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
 
 438       SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 440       JOIN chart c ON (c.id = ac.chart_id)
 
 442         $dpt_where_without_arapgl
 
 445       GROUP BY c.accno, c.description, c.category |;
 
 447     if ($form->{project_id}) {
 
 448       # s.o. keine Projektauswahl in Bilanz
 
 452       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 454       JOIN ar a ON (a.id = ac.trans_id)
 
 455       JOIN parts p ON (ac.parts_id = p.id)
 
 456       JOIN chart c on (p.income_accno_id = c.id)
 
 457       -- use transdate from subwhere
 
 458       WHERE (c.category = 'I')
 
 462       GROUP BY c.accno, c.description, c.category
 
 466       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
 
 468       JOIN ap a ON (a.id = ac.trans_id)
 
 469       JOIN parts p ON (ac.parts_id = p.id)
 
 470       JOIN chart c on (p.expense_accno_id = c.id)
 
 471       WHERE (c.category = 'E')
 
 475       GROUP BY c.accno, c.description, c.category |;
 
 483   $sth = prepare_execute_query($form, $dbh, $query);
 
 485   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 487     if ($ref->{category} eq 'C') {
 
 488       $ref->{category} = 'A';
 
 491     # get last heading account
 
 492     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
 496         $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
 
 498         $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
 
 502     $form->{ $ref->{category} }{ $ref->{accno} }{accno}       = $ref->{accno};
 
 503     $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
 
 504     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
 
 507       $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
 
 509       $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
 
 514   # remove accounts with zero balance
 
 515   foreach $category (@{$categories}) {
 
 516     foreach $accno (keys %{ $form->{$category} }) {
 
 517       $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
 
 518       $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
 
 520       delete $form->{$category}{$accno}
 
 521         if (   $form->{$category}{$accno}{this} == 0
 
 522             && $form->{$category}{$accno}{last} == 0);
 
 526   $main::lxdebug->leave_sub();
 
 530   $main::lxdebug->enter_sub();
 
 532   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 534   my ($null, $department_id) = split /--/, $form->{department};
 
 538   my $dpt_where_without_arapgl;
 
 547   $where .= ' AND ac.cb_transaction is false ' unless $form->{l_cb};
 
 550     $fromdate = conv_dateq($fromdate);
 
 551     if ($form->{method} eq 'cash') {
 
 552       $subwhere .= " AND (transdate    >= $fromdate)";
 
 553       $glwhere   = " AND (ac.transdate >= $fromdate)";
 
 554       $prwhere   = " AND (a.transdate  >= $fromdate)";
 
 555       $inwhere   = " AND (acc.transdate >= $fromdate)";
 
 557       $where    .= " AND (ac.transdate >= $fromdate)";
 
 562     $todate = conv_dateq($todate);
 
 563     $subwhere   .= " AND (transdate    <= $todate)";
 
 564     $where      .= " AND (ac.transdate <= $todate)";
 
 565     $prwhere    .= " AND (a.transdate  <= $todate)";
 
 566     $inwhere    .= " AND (acc.transdate <= $todate)";
 
 569   if ($department_id) {
 
 570     $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 573   if ($form->{project_id}) {
 
 574     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
 
 578 # GUV patch by Ronny Rentner (Bug 1190)
 
 580 # GUV IST-Versteuerung
 
 582 # Alle tatsaechlichen _Zahlungseingaenge_
 
 583 # im Zeitraum erfassen
 
 584 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
 
 588   if ($form->{method} eq 'cash') {
 
 591        SELECT SUM( ac.amount * CASE WHEN COALESCE((SELECT amount FROM ar a WHERE id = ac.trans_id $dpt_where), 0) != 0 THEN
 
 592             /* ar amount is not zero, so we can divide by amount   */
 
 593                     (SELECT SUM(acc.amount) * -1
 
 596                      AND acc.trans_id = ac.trans_id
 
 597                      AND acc.chart_link LIKE '%AR_paid%')
 
 598                   / (SELECT amount FROM ar WHERE id = ac.trans_id)
 
 600             /* ar amount is zero, or we are checking with a non-ar-transaction, so we return 0 in both cases as multiplicator of ac.amount */
 
 602                 ) AS amount, c.$category
 
 604        LEFT JOIN chart c ON (c.id  = ac.chart_id)
 
 605        LEFT JOIN ar      ON (ar.id = ac.trans_id)
 
 606       WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
 
 611        SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 613          JOIN chart c ON (c.id = ac.chart_id)
 
 614          JOIN ar a ON (a.id = ac.trans_id)
 
 615          WHERE $where $dpt_where
 
 616            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
 
 622          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 624          JOIN chart c ON (c.id = ac.chart_id)
 
 625          JOIN ap a ON (a.id = ac.trans_id)
 
 626          WHERE $where $dpt_where
 
 627            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
 
 633          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 635          JOIN chart c ON (c.id = ac.chart_id)
 
 636          JOIN gl a ON (a.id = ac.trans_id)
 
 637          WHERE $where $dpt_where $glwhere
 
 638            AND NOT ((ac.chart_link = 'AR') OR (ac.chart_link = 'AP'))
 
 643     if ($form->{project_id}) {
 
 647          SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 649          JOIN ar a ON (a.id = ac.trans_id)
 
 650          JOIN parts p ON (ac.parts_id = p.id)
 
 651          JOIN chart c on (p.income_accno_id = c.id)
 
 652          WHERE (c.category = 'I') $prwhere $dpt_where
 
 653            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AR_paid%') $subwhere)
 
 659          SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 661          JOIN ap a ON (a.id = ac.trans_id)
 
 662          JOIN parts p ON (ac.parts_id = p.id)
 
 663          JOIN chart c on (p.expense_accno_id = c.id)
 
 664          WHERE (c.category = 'E') $prwhere $dpt_where
 
 665            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans a WHERE (a.chart_link LIKE '%AP_paid%') $subwhere)
 
 671   } else {                      # if ($form->{method} eq 'cash')
 
 672     if ($department_id) {
 
 673       $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 674       $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
 
 675                                                    (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
 
 676                                                    (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
 
 680         SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 682         JOIN chart c ON (c.id = ac.chart_id)
 
 684           $dpt_where_without_arapgl
 
 686         GROUP BY c.$category |;
 
 688     if ($form->{project_id}) {
 
 692         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 694         JOIN ar a ON (a.id = ac.trans_id)
 
 695         JOIN parts p ON (ac.parts_id = p.id)
 
 696         JOIN chart c on (p.income_accno_id = c.id)
 
 697         WHERE (c.category = 'I')
 
 705         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 707         JOIN ap a ON (a.id = ac.trans_id)
 
 708         JOIN parts p ON (ac.parts_id = p.id)
 
 709         JOIN chart c on (p.expense_accno_id = c.id)
 
 710         WHERE (c.category = 'E')
 
 714         GROUP BY c.$category |;
 
 722   foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
 
 723     if ($category eq "pos_bwa") {
 
 725         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
 727         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
 730       $form->{ $ref->{$category} } += $ref->{amount};
 
 734   $main::lxdebug->leave_sub();
 
 738   $main::lxdebug->enter_sub();
 
 740   my ($self, $myconfig, $form, %options) = @_;
 
 742   my $dbh = $form->dbconnect($myconfig);
 
 744   my ($query, $sth, $ref);
 
 747   my ($null, $department_id) = split /--/, $form->{department};
 
 748   my @headingaccounts = ();
 
 750   my $dpt_where_without_arapgl;
 
 751   my ($customer_where, $customer_join, $customer_no_union);
 
 755   my $invwhere = $where;
 
 757   if ($department_id) {
 
 758     $dpt_where = qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 759     $dpt_where_without_arapgl = qq| AND COALESCE((SELECT department_id FROM ar WHERE ar.id=ac.trans_id),
 
 760                                                  (SELECT department_id FROM gl WHERE gl.id=ac.trans_id),
 
 761                                                  (SELECT department_id FROM ap WHERE ap.id=ac.trans_id)) = | . conv_i($department_id);
 
 763   if ($form->{customer_id}) {
 
 764     $customer_join     = qq| JOIN ar a ON (ac.trans_id = a.id) |;
 
 765     $customer_where    = qq| AND (a.customer_id = | . conv_i($form->{customer_id}, 'NULL') . qq|) |;
 
 766     $customer_no_union = qq| AND 1=0 |;
 
 769   # project_id only applies to getting transactions
 
 770   # it has nothing to do with a trial balance
 
 771   # but we use the same function to collect information
 
 773   if ($form->{project_id}) {
 
 774     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 777   my $acc_cash_where = "";
 
 778 #  my $ar_cash_where = "";
 
 779 #  my $ap_cash_where = "";
 
 782   if ($form->{method} eq "cash") {
 
 784       qq| AND (ac.trans_id IN (
 
 787             WHERE datepaid >= '$form->{fromdate}'
 
 788               AND datepaid <= '$form->{todate}'
 
 794             WHERE datepaid >= '$form->{fromdate}'
 
 795               AND datepaid <= '$form->{todate}'
 
 801             WHERE transdate >= '$form->{fromdate}'
 
 802               AND transdate <= '$form->{todate}'
 
 804 #    $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
 
 807   if ($options{beginning_balances}) {
 
 808     foreach my $prefix (qw(from to)) {
 
 809       next if ($form->{"${prefix}date"});
 
 811       my $min_max = $prefix eq 'from' ? 'min' : 'max';
 
 812       $query      = qq|SELECT ${min_max}(transdate)
 
 816                          $dpt_where_without_arapgl
 
 820       ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
 
 823     # get beginning balances
 
 825       qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
 
 827           LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 829           WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
 
 830             $dpt_where_without_arapgl
 
 834           GROUP BY c.accno, c.category, c.description |;
 
 836     $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
 
 838     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 840       if ($ref->{amount} != 0 || $form->{all_accounts}) {
 
 841         $trb{ $ref->{accno} }{description} = $ref->{description};
 
 842         $trb{ $ref->{accno} }{charttype}   = 'A';
 
 843         $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
 
 845         if ($ref->{amount} > 0) {
 
 846           $trb{ $ref->{accno} }{haben_eb}   = $ref->{amount};
 
 848           $trb{ $ref->{accno} }{soll_eb}   = $ref->{amount} * -1;
 
 850         $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 859     qq|SELECT c.accno, c.description, c.category
 
 861        WHERE c.charttype = 'H'
 
 864   $sth = prepare_execute_query($form, $dbh, $query);
 
 866   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 867     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 868     $trb{ $ref->{accno} }{charttype}   = 'H';
 
 869     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 871     push @headingaccounts, $ref->{accno};
 
 877   my $saldowhere    = " 1 = 1 ";
 
 878   my $sumwhere      = " 1 = 1 ";
 
 880   my $sumsubwhere   = '';
 
 881   my $saldosubwhere = '';
 
 882   my $glsaldowhere  = '';
 
 887   my ($fromdate, $todate);
 
 889   if ($form->{fromdate} || $form->{todate}) {
 
 890     if ($form->{fromdate}) {
 
 891       $fromdate = conv_dateq($form->{fromdate});
 
 892       $tofrom        .= " AND (ac.transdate >= $fromdate)";
 
 893       $subwhere      .= " AND (ac.transdate >= $fromdate)";
 
 894       $sumsubwhere   .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
 
 895       $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate)))  ";
 
 896       $invwhere      .= " AND (a.transdate >= $fromdate)";
 
 897       $glsaldowhere  .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
 
 898       $glwhere        = " AND (ac.transdate >= $fromdate)";
 
 899       $glsumwhere     = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
 
 901     if ($form->{todate}) {
 
 902       $todate = conv_dateq($form->{todate});
 
 903       $tofrom        .= " AND (ac.transdate <= $todate)";
 
 904       $invwhere      .= " AND (a.transdate <= $todate)";
 
 905       $saldosubwhere .= " AND (ac.transdate <= $todate)";
 
 906       $sumsubwhere   .= " AND (ac.transdate <= $todate)";
 
 907       $subwhere      .= " AND (ac.transdate <= $todate)";
 
 908       $glwhere       .= " AND (ac.transdate <= $todate)";
 
 909       $glsumwhere    .= " AND (ac.transdate <= $todate) ";
 
 910       $glsaldowhere  .= " AND (ac.transdate <= $todate) ";
 
 914   if ($form->{method} eq "cash") {
 
 916       qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
 
 917     $saldowhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate))  AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
 
 919     $sumwhere .= qq| AND(ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM ap WHERE datepaid>= $fromdate AND datepaid<= $todate UNION SELECT id FROM gl WHERE transdate>= $fromdate AND transdate<= $todate)) AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL) |;
 
 921     $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 922     $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 923     $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 927        SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
 
 929        JOIN chart c ON (c.id = ac.chart_id)
 
 932          $dpt_where_without_arapgl
 
 934        GROUP BY c.accno, c.description, c.category |;
 
 936   if ($form->{project_id}) {
 
 938       -- add project transactions from invoice
 
 942       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
 
 944       JOIN ar a ON (ac.trans_id = a.id)
 
 945       JOIN parts p ON (ac.parts_id = p.id)
 
 946       JOIN chart c ON (p.income_accno_id = c.id)
 
 951       GROUP BY c.accno, c.description, c.category
 
 955       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
 
 957       JOIN ap a ON (ac.trans_id = a.id)
 
 958       JOIN parts p ON (ac.parts_id = p.id)
 
 959       JOIN chart c ON (p.expense_accno_id = c.id)
 
 964       GROUP BY c.accno, c.description, c.category
 
 968   $query .= qq| ORDER BY accno|;
 
 970   $sth = prepare_execute_query($form, $dbh, $query);
 
 972   # calculate the debit and credit in the period
 
 973   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 974     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 975     $trb{ $ref->{accno} }{charttype}   = 'A';
 
 976     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 977     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
 981   # prepare query for each account
 
 982   my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
 
 986          (SELECT SUM(ac.amount) * -1
 
 988           JOIN chart c ON (c.id = ac.chart_id)
 
 991             $dpt_where_without_arapgl
 
 996           AND (c.accno = ?)) AS debit,
 
 998          (SELECT SUM(ac.amount)
 
1000           JOIN chart c ON (c.id = ac.chart_id)
 
1003             $dpt_where_without_arapgl
 
1008           AND c.accno = ?) AS credit,
 
1009         (SELECT SUM(ac.amount)
 
1011          JOIN chart c ON (ac.chart_id = c.id)
 
1014            $dpt_where_without_arapgl
 
1018          AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
 
1020         (SELECT SUM(ac.amount)
 
1022          JOIN chart c ON (ac.chart_id = c.id)
 
1025            $dpt_where_without_arapgl
 
1030          AND c.accno = ?) AS sum_credit,
 
1032         (SELECT SUM(ac.amount)
 
1034          JOIN chart c ON (ac.chart_id = c.id)
 
1037            $dpt_where_without_arapgl
 
1042          AND c.accno = ?) AS sum_debit,
 
1044         (SELECT max(ac.transdate) FROM acc_trans ac
 
1045         JOIN chart c ON (ac.chart_id = c.id)
 
1048           $dpt_where_without_arapgl
 
1052         AND c.accno = ?) AS last_transaction
 
1057   $drcr = prepare_query($form, $dbh, $q_drcr);
 
1059   if ($form->{project_id}) {
 
1060     # prepare query for each account
 
1063           (SELECT SUM(ac.sellprice * ac.qty) * -1
 
1065            JOIN parts p ON (ac.parts_id = p.id)
 
1066            JOIN ap a ON (ac.trans_id = a.id)
 
1067            JOIN chart c ON (p.expense_accno_id = c.id)
 
1072            AND c.accno = ?) AS debit,
 
1074           (SELECT SUM(ac.sellprice * ac.qty)
 
1076            JOIN parts p ON (ac.parts_id = p.id)
 
1077            JOIN ar a ON (ac.trans_id = a.id)
 
1078            JOIN chart c ON (p.income_accno_id = c.id)
 
1083            AND c.accno = ?) AS credit,
 
1085         (SELECT SUM(ac.amount)
 
1087          JOIN chart c ON (ac.chart_id = c.id)
 
1090            $dpt_where_without_arapgl
 
1094          AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
 
1096         (SELECT SUM(ac.amount)
 
1098          JOIN chart c ON (ac.chart_id = c.id)
 
1101            $dpt_where_without_arapgl
 
1106          AND c.accno = ?) AS sum_credit,
 
1108         (SELECT SUM(ac.amount)
 
1110          JOIN chart c ON (ac.chart_id = c.id)
 
1114            $dpt_where_without_arapgl
 
1118          AND c.accno = ?) AS sum_debit,
 
1121         (SELECT max(ac.transdate) FROM acc_trans ac
 
1122         JOIN chart c ON (ac.chart_id = c.id)
 
1125           $dpt_where_without_arapgl
 
1129         AND c.accno = ?) AS last_transaction
 
1132     $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
 
1136   my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
 
1138   foreach my $accno (sort keys %trb) {
 
1141     $ref->{accno} = $accno;
 
1142     map { $ref->{$_} = $trb{$accno}{$_} }
 
1143       qw(description category charttype amount soll_eb haben_eb beginning_balance);
 
1145     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1147     if ($trb{$accno}{charttype} eq 'A') {
 
1150       do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1152       ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
 
1153       my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
 
1154       $last_transaction = "";
 
1155       while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
 
1156         $ref->{debit}  += $debit;
 
1157         $ref->{credit} += $credit;
 
1159           $ref->{haben_saldo} += $saldo;
 
1161           $ref->{soll_saldo} += $saldo * -1;
 
1163         $ref->{last_transaction} = $last_transaction;
 
1164         $ref->{soll_kumuliert} = $soll_kumuliert * -1;
 
1165         $ref->{haben_kumuliert} = $haben_kumuliert;
 
1169       if ($form->{project_id}) {
 
1172         do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1174         ($debit, $credit) = (0, 0);
 
1175         while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
 
1176           $ref->{debit}  += $debit;
 
1177           $ref->{credit} += $credit;
 
1179             $ref->{haben_saldo} += $saldo;
 
1181             $ref->{soll_saldo} += $saldo * -1;
 
1183           $ref->{soll_kumuliert} += $soll_kumuliert * -1;
 
1184           $ref->{haben_kumuliert} += $haben_kumuliert;
 
1186         $project_drcr->finish;
 
1189       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1190       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1192       if ($ref->{haben_saldo} != 0) {
 
1193         $ref->{haben_saldo}  = $ref->{haben_saldo} + $ref->{beginning_balance};
 
1194         if ($ref->{haben_saldo} < 0) {
 
1195           $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
 
1196           $ref->{haben_saldo} = 0;
 
1199         $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
 
1200         if ($ref->{soll_saldo} < 0) {
 
1201           $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
 
1202           $ref->{soll_saldo} = 0;
 
1205       $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
 
1206       $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
 
1207       $ref->{haben_kumuliert}  = $form->round_amount($ref->{haben_kumuliert},  2);
 
1208       $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
 
1213     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1214     $accno = pop @accno;
 
1216       $trb{$accno}{debit}  += $ref->{debit};
 
1217       $trb{$accno}{credit} += $ref->{credit};
 
1218       $trb{$accno}{soll_saldo}  += $ref->{soll_saldo};
 
1219       $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
 
1220       $trb{$accno}{soll_kumuliert}  += $ref->{soll_kumuliert};
 
1221       $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
 
1224     push @{ $form->{TB} }, $ref;
 
1230   # debits and credits for headings
 
1231   foreach my $accno (@headingaccounts) {
 
1232     foreach $ref (@{ $form->{TB} }) {
 
1233       if ($accno eq $ref->{accno}) {
 
1234         $ref->{debit}           = $trb{$accno}{debit};
 
1235         $ref->{credit}          = $trb{$accno}{credit};
 
1236         $ref->{soll_saldo}      = $trb{$accno}{soll_saldo};
 
1237         $ref->{haben_saldo}     = $trb{$accno}{haben_saldo};
 
1238         $ref->{soll_kumuliert}  = $trb{$accno}{soll_kumuliert};
 
1239         $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
 
1244   $main::lxdebug->leave_sub();
 
1248   $main::lxdebug->enter_sub();
 
1249   my ($self, $dbh, $form) = @_;
 
1250   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
1251   my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
 
1252   my $sth =  $dbh->prepare($query);
 
1253   while(my $ref = $sth->fetchrow_hashref()) {
 
1254     $ref->{invnumer} =~ s/Storno zu //g;
 
1255     $form->{storno}{$ref->{invnumber}} = 1;
 
1257   $main::lxdebug->leave_sub();
 
1261   $main::lxdebug->enter_sub();
 
1263   my ($self, $myconfig, $form) = @_;
 
1265   # connect to database
 
1266   my $dbh     = $form->dbconnect($myconfig);
 
1268   my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
 
1270   # falls customer ziehen wir die offene forderungsliste
 
1271   # anderfalls für die lieferanten die offenen verbindlichkeitne
 
1272   if ($form->{ct} eq "customer") {
 
1285   $ct_id = "${ct}_id";
 
1287   # erweiterung um einen freien zeitraum oder einen stichtag
 
1288   # mit entsprechender altersstrukturliste (s.a. Bug 1842)
 
1289   # eine neue variable an der oberfläche eingeführt, somit ist
 
1290   # todate == freier zeitrau und fordate == stichtag
 
1291   # duedate_where == nur fällige rechnungen anzeigen
 
1293   my ($review_of_aging_list, $todate, $fromdate, $fromwhere, $fordate,
 
1296   if ($form->{reporttype} eq 'custom') {  # altersstrukturliste, nur fällige
 
1298     # explizit rausschmeissen was man für diesen bericht nicht braucht
 
1299     delete $form->{fromdate};
 
1300     delete $form->{todate};
 
1302     # an der oberfläche ist das tagesaktuelle datum vorausgewählt
 
1303     # falls es dennoch per Benutzereingabe gelöscht wird, lieber wieder vorbelegen
 
1304     # ferner muss für die spätere DB-Abfrage muss todate gesetzt sein.
 
1305     $form->{fordate}  = $form->current_date($myconfig) unless ($form->{fordate});
 
1306     $fordate          = conv_dateq($form->{fordate});
 
1309     if ($form->{review_of_aging_list}) { # falls die liste leer ist, alles anzeigen
 
1310       if ($form->{review_of_aging_list} =~ m "-") {             # ..  periode von bis
 
1311         my @period = split(/-/, $form->{review_of_aging_list}); # ... von periode bis periode
 
1312         $review_of_aging_list = " AND $period[0] <  (date $fordate) - duedate
 
1313                                   AND (date $fordate) - duedate  < $period[1]";
 
1315         $form->{review_of_aging_list} =~ s/[^0-9]//g;   # größer 120 das substitute ist nur für das '>' zeichen
 
1316         $review_of_aging_list = " AND $form->{review_of_aging_list} < (date $fordate) - duedate";
 
1319     $duedate_where = " AND (date $fordate) - duedate >= 0 ";
 
1320   } else {  # freier zeitraum, nur rechnungsdatum und OHNE review_of_aging_list
 
1321     $form->{todate}  = $form->current_date($myconfig) unless ($form->{todate});
 
1322     $todate = conv_dateq($form->{todate});
 
1323     $fromdate = conv_dateq($form->{fromdate});
 
1324     $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
 
1326   my $where = " 1 = 1 ";
 
1329   if ($form->{$ct_id}) {
 
1330     $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
 
1331   } elsif ($form->{ $form->{ct} }) {
 
1332     $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
 
1337   if ($form->{department}) {
 
1338     my ($null, $department_id) = split /--/, $form->{department};
 
1339     $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
 
1340     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
1341     $where_dpt = qq| AND (${arap}.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
1345     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1346       street, zipcode, city, country, contact, email,
 
1347       phone as customerphone, fax as customerfax, ${ct}number,
 
1348       "invnumber", "transdate",
 
1349       (amount - COALESCE((SELECT sum(amount)*$ml FROM acc_trans WHERE chart_link ilike '%paid%' AND acc_trans.trans_id=${arap}.id AND acc_trans.transdate <= (date $todate)),0)) as "open", "amount",
 
1350       "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays,
 
1353        WHERE (${arap}.currency_id = exchangerate.currency_id)
 
1354          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1356     WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
 
1357       AND NOT COALESCE (${arap}.storno, 'f')
 
1358       AND (${arap}.${ct}_id = ${ct}.id)
 
1361       AND (transdate <= (date $todate) $fromwhere )
 
1362       $review_of_aging_list
 
1364     ORDER BY ctid, transdate, invnumber |;
 
1366   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1368   # select outstanding vendors or customers, depends on $ct
 
1370     qq|SELECT DISTINCT ct.id, ct.name
 
1371        FROM $ct ct, $arap a
 
1374          AND (a.${ct_id} = ct.id)
 
1375          AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
 
1376          AND (a.transdate <= $todate $fromwhere)
 
1379   my $sth = prepare_execute_query($form, $dbh, $query);
 
1382   # for each company that has some stuff outstanding
 
1383   while (my ($id) = $sth->fetchrow_array) {
 
1384     do_statement($form, $sth_details, $q_details, $id);
 
1386     while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
 
1387       $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
 
1388       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1389       push @{ $form->{AG} }, $ref;
 
1392     $sth_details->finish;
 
1401   $main::lxdebug->leave_sub();
 
1405   $main::lxdebug->enter_sub();
 
1407   my ($self, $myconfig, $form) = @_;
 
1409   # connect to database
 
1410   my $dbh = $form->dbconnect($myconfig);
 
1412   my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
 
1415     qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1418   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1419     selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
 
1422   $main::lxdebug->leave_sub();
 
1426   $main::lxdebug->enter_sub();
 
1428   my ($self, $myconfig, $form) = @_;
 
1430   # connect to database
 
1431   my $dbh = $form->dbconnect($myconfig);
 
1433   my ($null, $department_id) = split /--/, $form->{department};
 
1436   my $where = "1 = 1";
 
1438   if ($department_id) {
 
1439     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
1444   if ($form->{accno}) {
 
1445     $accno = $form->{accno};
 
1446     $rate  = $form->{"$form->{accno}_rate"};
 
1447     $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
 
1453   if ($form->{db} eq 'ar') {
 
1454     $table = "customer";
 
1461   my $arap = lc($ARAP);
 
1463   my $transdate = "a.transdate";
 
1465   if ($form->{method} eq 'cash') {
 
1466     $transdate = "a.datepaid";
 
1468     my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
 
1475           WHERE (a.chart_link LIKE '%${ARAP}_paid%')
 
1476           AND (transdate <= $todate)
 
1481   # if there are any dates construct a where
 
1482   $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
 
1483   $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
 
1485   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1487   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1488   $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
 
1491       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
 
1492           ac.amount * $ml AS tax
 
1494          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1495          JOIN chart ch ON (ch.id = ac.chart_id)
 
1496          JOIN $table n ON (n.id = a.${table}_id)
 
1500            AND (a.invoice = '0')
 
1504          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1505            i.sellprice * i.qty * $rate * $ml AS tax
 
1507          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1508          JOIN chart ch ON (ch.id = ac.chart_id)
 
1509          JOIN $table n ON (n.id = a.${table}_id)
 
1510          JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1511          JOIN invoice i ON (i.trans_id = a.id)
 
1515            AND (a.invoice = '1')
 
1516          ORDER BY $sortorder|;
 
1518   $form->{TR} = selectall_hashref_query($form, $dbh, $query);
 
1522   $main::lxdebug->leave_sub();
 
1525 sub paymentaccounts {
 
1526   $main::lxdebug->enter_sub();
 
1528   my ($self, $myconfig, $form) = @_;
 
1530   # connect to database, turn AutoCommit off
 
1531   my $dbh = $form->dbconnect_noauto($myconfig);
 
1533   my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
 
1535   # get A(R|P)_paid accounts
 
1537     qq|SELECT accno, description
 
1539        WHERE link LIKE '%${ARAP}_paid%'|;
 
1540   $form->{PR} = selectall_hashref_query($form, $dbh, $query);
 
1544   $main::lxdebug->leave_sub();
 
1548   $main::lxdebug->enter_sub();
 
1550   my ($self, $myconfig, $form) = @_;
 
1552   # connect to database, turn AutoCommit off
 
1553   my $dbh = $form->dbconnect_noauto($myconfig);
 
1558   if ($form->{db} eq 'ar') {
 
1559     $table = 'customer';
 
1570   if ($form->{department_id}) {
 
1571     $where = qq| AND (a.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
 
1574   if ($form->{fromdate}) {
 
1575     $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
 
1577   if ($form->{todate}) {
 
1578     $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
 
1580   if (!$form->{fx_transaction}) {
 
1581     $where .= " AND ac.fx_transaction = '0'";
 
1586   if ($form->{reference}) {
 
1587     $reference = $dbh->quote('%' . $form->{reference} . '%');
 
1588     $invnumber = " AND (a.invnumber LIKE $reference)";
 
1589     $reference = " AND (a.reference LIKE $reference)";
 
1591   if ($form->{source}) {
 
1592     $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
 
1594   if ($form->{memo}) {
 
1595     $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
 
1598   my %sort_columns =  (
 
1599     'transdate'    => [ qw(transdate lower_invnumber lower_name) ],
 
1600     'invnumber'    => [ qw(lower_invnumber lower_name transdate) ],
 
1601     'name'         => [ qw(lower_name transdate)                 ],
 
1602     'source'       => [ qw(lower_source)                         ],
 
1603     'memo'         => [ qw(lower_memo)                           ],
 
1605   my %lowered_columns =  (
 
1606     'invnumber'       => { 'gl' => 'a.reference',   'arap' => 'a.invnumber', },
 
1607     'memo'            => { 'gl' => 'ac.memo',       'arap' => 'ac.memo',     },
 
1608     'source'          => { 'gl' => 'ac.source',     'arap' => 'ac.source',   },
 
1609     'name'            => { 'gl' => 'a.description', 'arap' => 'c.name',      },
 
1612   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
1613   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
 
1614   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
1617   my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
 
1618   foreach my $spec (@{ $sort_columns{$sortkey} }) {
 
1619     next if ($spec !~ m/^lower_(.*)$/);
 
1622     map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
 
1625   $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
 
1626   $sth = prepare_query($form, $dbh, $query);
 
1629       qq|SELECT c.name, a.invnumber, a.ordnumber,
 
1630            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1631            a.invoice, a.id, ac.memo, '${arap}' AS module
 
1632            $columns_for_sorting{arap}
 
1634          JOIN $arap a ON (ac.trans_id = a.id)
 
1635          JOIN $table c ON (c.id = a.${table}_id)
 
1636          WHERE (ac.chart_id = ?)
 
1642          SELECT a.description, a.reference, NULL AS ordnumber,
 
1643            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1644            '0' as invoice, a.id, ac.memo, 'gl' AS module
 
1645            $columns_for_sorting{gl}
 
1647          JOIN gl a ON (a.id = ac.trans_id)
 
1648          WHERE (ac.chart_id = ?)
 
1651            AND (ac.amount * $ml) > 0
 
1653          ORDER BY $sortorder|;
 
1654   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1658   # cycle through each id
 
1659   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
1660     do_statement($form, $sth, $query, $accno);
 
1661     my $ref = $sth->fetchrow_hashref();
 
1662     push(@{ $form->{PR} }, $ref);
 
1665     $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
 
1667     do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
 
1668     while (my $pr = $sth_details->fetchrow_hashref()) {
 
1669       push(@{ $form->{ $ref->{id} } }, $pr);
 
1671     $sth_details->finish();
 
1676   $main::lxdebug->leave_sub();
 
1680   $main::lxdebug->enter_sub();
 
1682   my ($self, $myconfig, $form) = @_;
 
1684   # connect to database
 
1685   my $dbh = $form->dbconnect($myconfig);
 
1687   my $last_period = 0;
 
1690     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);
 
1692   $form->{decimalplaces} *= 1;
 
1694   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
 
1696   # if there are any compare dates
 
1698   if ($form->{fromdate} || $form->{todate}) {
 
1700     if ($form->{fromdate}) {
 
1701       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1704       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1707     my $kummfromdate = $form->{comparefromdate};
 
1708     my $kummtodate   = $form->{comparetodate};
 
1709     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
 
1712   my @periods        = qw(jetzt kumm);
 
1713   my @gesamtleistung = qw(1 3);
 
1714   my @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 20);
 
1716     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
1718   foreach my $key (@periods) {
 
1719     $form->{ "$key" . "gesamtleistung" } = 0;
 
1720     $form->{ "$key" . "gesamtkosten" }   = 0;
 
1722     foreach $category (@categories) {
 
1724       if (defined($form->{$category}{$key})) {
 
1725         $form->{"$key$category"} =
 
1726           $form->format_amount($myconfig,
 
1727                                $form->round_amount($form->{$category}{$key}, 2
 
1729                                $form->{decimalplaces},
 
1733     foreach my $item (@gesamtleistung) {
 
1734       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
1736     $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
 
1738     foreach my $item (@gesamtkosten) {
 
1739       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
1741     $form->{ "$key" . "rohertrag" } =
 
1742       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
1743     $form->{ "$key" . "betriebrohertrag" } =
 
1744       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
1745     $form->{ "$key" . "betriebsergebnis" } =
 
1746       $form->{ "$key" . "betriebrohertrag" } -
 
1747       $form->{ "$key" . "gesamtkosten" };
 
1748     $form->{ "$key" . "neutraleraufwand" } =
 
1749       $form->{19}{$key} + $form->{30}{$key} + $form->{31}{$key};
 
1750     $form->{ "$key" . "neutralerertrag" } =
 
1751       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
1752     $form->{ "$key" . "ergebnisvorsteuern" } =
 
1753       $form->{ "$key" . "betriebsergebnis" } -
 
1754       $form->{ "$key" . "neutraleraufwand" } +
 
1755       $form->{ "$key" . "neutralerertrag" };
 
1756     $form->{ "$key" . "ergebnis" } =
 
1757       $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
 
1759     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
1760       foreach $category (@categories) {
 
1761         if (defined($form->{$category}{$key})) {
 
1762           $form->{ "$key" . "gl" . "$category" } =
 
1763             $form->format_amount(
 
1765                                $form->round_amount(
 
1766                                  ($form->{$category}{$key} /
 
1767                                     $form->{ "$key" . "gesamtleistung" } * 100
 
1769                                  $form->{decimalplaces}
 
1771                                $form->{decimalplaces},
 
1775       foreach my $item (@ergebnisse) {
 
1776         $form->{ "$key" . "gl" . "$item" } =
 
1777           $form->format_amount($myconfig,
 
1778                                $form->round_amount(
 
1779                                  ( $form->{ "$key" . "$item" } /
 
1780                                      $form->{ "$key" . "gesamtleistung" } * 100
 
1782                                  $form->{decimalplaces}
 
1784                                $form->{decimalplaces},
 
1789     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
1790       foreach $category (@categories) {
 
1791         if (defined($form->{$category}{$key})) {
 
1792           $form->{ "$key" . "gk" . "$category" } =
 
1793             $form->format_amount($myconfig,
 
1794                                  $form->round_amount(
 
1795                                    ($form->{$category}{$key} /
 
1796                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1798                                    $form->{decimalplaces}
 
1800                                  $form->{decimalplaces},
 
1804       foreach my $item (@ergebnisse) {
 
1805         $form->{ "$key" . "gk" . "$item" } =
 
1806           $form->format_amount($myconfig,
 
1807                                $form->round_amount(
 
1808                                    ($form->{ "$key" . "$item" } /
 
1809                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1811                                    $form->{decimalplaces}
 
1813                                $form->{decimalplaces},
 
1818     if ($form->{10}{$key} > 0) {
 
1819       foreach $category (@categories) {
 
1820         if (defined($form->{$category}{$key})) {
 
1821           $form->{ "$key" . "pk" . "$category" } =
 
1822             $form->format_amount(
 
1824                         $form->round_amount(
 
1825                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
1826                           $form->{decimalplaces}
 
1828                         $form->{decimalplaces},
 
1832       foreach my $item (@ergebnisse) {
 
1833         $form->{ "$key" . "pk" . "$item" } =
 
1834           $form->format_amount($myconfig,
 
1835                                $form->round_amount(
 
1836                                                 ($form->{ "$key" . "$item" } /
 
1837                                                    $form->{10}{$key} * 100
 
1839                                                 $form->{decimalplaces}
 
1841                                $form->{decimalplaces},
 
1846     if ($form->{4}{$key} > 0) {
 
1847       foreach $category (@categories) {
 
1848         if (defined($form->{$category}{$key})) {
 
1849           $form->{ "$key" . "auf" . "$category" } =
 
1850             $form->format_amount(
 
1852                          $form->round_amount(
 
1853                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
1854                            $form->{decimalplaces}
 
1856                          $form->{decimalplaces},
 
1860       foreach my $item (@ergebnisse) {
 
1861         $form->{ "$key" . "auf" . "$item" } =
 
1862           $form->format_amount($myconfig,
 
1863                                $form->round_amount(
 
1864                                                 ($form->{ "$key" . "$item" } /
 
1865                                                    $form->{4}{$key} * 100
 
1867                                                 $form->{decimalplaces}
 
1869                                $form->{decimalplaces},
 
1874     foreach my $item (@ergebnisse) {
 
1875       $form->{ "$key" . "$item" } =
 
1876         $form->format_amount($myconfig,
 
1877                              $form->round_amount($form->{ "$key" . "$item" },
 
1878                                                  $form->{decimalplaces}
 
1880                              $form->{decimalplaces},
 
1887   $main::lxdebug->leave_sub();
 
1890 sub income_statement {
 
1891   $main::lxdebug->enter_sub();
 
1893   my ($self, $myconfig, $form) = @_;
 
1895   # connect to database
 
1896   my $dbh = $form->dbconnect($myconfig);
 
1898   my $last_period          = 0;
 
1899   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
1900   my @categories_ausgaben  =
 
1901     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);
 
1903   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
1905   $form->{decimalplaces} *= 1;
 
1909   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
1913   foreach my $item (@categories_einnahmen) {
 
1914     $form->{"eur${item}"} =
 
1915       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1916     $form->{"sumeura"} += $form->{$item};
 
1918   foreach my $item (@categories_ausgaben) {
 
1919     $form->{"eur${item}"} =
 
1920       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1921     $form->{"sumeurb"} += $form->{$item};
 
1924   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
1926   foreach my $item (@ergebnisse) {
 
1928       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1930   $main::lxdebug->leave_sub();