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         # Diese Bedingung wird derzeit niemals wahr sein, da man in Bericht->Bilanz keine 
 
 219         # Projekte auswählen kann
 
 220     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 223   if ($form->{method} eq 'cash') {
 
 225       qq|SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 227          JOIN chart c ON (c.id = ac.chart_id)
 
 228          JOIN ar a ON (a.id = ac.trans_id)
 
 237                JOIN chart ON (chart_id = id)
 
 238                WHERE (link LIKE '%AR_paid%')
 
 242          GROUP BY c.accno, c.description, c.category
 
 246          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 248          JOIN chart c ON (c.id = ac.chart_id)
 
 249          JOIN ap a ON (a.id = ac.trans_id)
 
 258                JOIN chart ON (chart_id = id)
 
 259                WHERE (link LIKE '%AP_paid%')
 
 263          GROUP BY c.accno, c.description, c.category
 
 267          SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 269          JOIN chart c ON (c.id = ac.chart_id)
 
 270          JOIN gl a ON (a.id = ac.trans_id)
 
 276              AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 278          GROUP BY c.accno, c.description, c.category |;
 
 280     if ($form->{project_id}) {
 
 281                 # s.o. keine Projektauswahl in Bilanz
 
 286          SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 288          JOIN ar a ON (a.id = ac.trans_id)
 
 289          JOIN parts p ON (ac.parts_id = p.id)
 
 290          JOIN chart c on (p.income_accno_id = c.id)
 
 292          -- use transdate from subwhere
 
 293          WHERE (c.category = 'I')
 
 300                JOIN chart ON (chart_id = id)
 
 301                WHERE (link LIKE '%AR_paid%')
 
 305          GROUP BY c.accno, c.description, c.category
 
 309          SELECT c.accno AS accno, SUM(ac.sellprice) AS amount, c.description AS description, c.category
 
 311          JOIN ap a ON (a.id = ac.trans_id)
 
 312          JOIN parts p ON (ac.parts_id = p.id)
 
 313          JOIN chart c on (p.expense_accno_id = c.id)
 
 315          WHERE (c.category = 'E')
 
 322                JOIN chart ON (chart_id = id)
 
 323                WHERE link LIKE '%AP_paid%'
 
 327          GROUP BY c.accno, c.description, c.category |;
 
 330   } else {                      # if ($form->{method} eq 'cash')
 
 331         # ich sehe keinen sinn das nochmal explizit ohne conv_i aufzurufen
 
 332         # bitte prüfen und löschen jan 15.11.2009
 
 333   #  if ($department_id) {
 
 334   #    $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
 335   #    $dpt_where = qq| AND t.department_id = $department_id |;
 
 339       SELECT c.accno, sum(ac.amount) AS amount, c.description, c.category
 
 341       JOIN chart c ON (c.id = ac.chart_id)
 
 347       GROUP BY c.accno, c.description, c.category |;
 
 349     if ($form->{project_id}) {
 
 350                 # s.o. keine Projektauswahl in Bilanz
 
 354       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) AS amount, c.description AS description, c.category
 
 356       JOIN ar a ON (a.id = ac.trans_id)
 
 357       JOIN parts p ON (ac.parts_id = p.id)
 
 358       JOIN chart c on (p.income_accno_id = c.id)
 
 360       -- use transdate from subwhere
 
 361       WHERE (c.category = 'I')
 
 365       GROUP BY c.accno, c.description, c.category
 
 369       SELECT c.accno AS accno, SUM(ac.sellprice * ac.qty) * -1 AS amount, c.description AS description, c.category
 
 371       JOIN ap a ON (a.id = ac.trans_id)
 
 372       JOIN parts p ON (ac.parts_id = p.id)
 
 373       JOIN chart c on (p.expense_accno_id = c.id)
 
 375       WHERE (c.category = 'E')
 
 379       GROUP BY c.accno, c.description, c.category |;
 
 387   $sth = prepare_execute_query($form, $dbh, $query);
 
 389   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 391     if ($ref->{category} eq 'C') {
 
 392       $ref->{category} = 'A';
 
 395     # get last heading account
 
 396     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
 400         $form->{ $ref->{category} }{$accno}{last} += $ref->{amount};
 
 402         $form->{ $ref->{category} }{$accno}{this} += $ref->{amount};
 
 406     $form->{ $ref->{category} }{ $ref->{accno} }{accno}       = $ref->{accno};
 
 407     $form->{ $ref->{category} }{ $ref->{accno} }{description} = $ref->{description};
 
 408     $form->{ $ref->{category} }{ $ref->{accno} }{charttype} = "A";
 
 411       $form->{ $ref->{category} }{ $ref->{accno} }{last} += $ref->{amount};
 
 413       $form->{ $ref->{category} }{ $ref->{accno} }{this} += $ref->{amount};
 
 418   # remove accounts with zero balance
 
 419   foreach $category (@{$categories}) {
 
 420     foreach $accno (keys %{ $form->{$category} }) {
 
 421       $form->{$category}{$accno}{last} = $form->round_amount($form->{$category}{$accno}{last}, $dec);
 
 422       $form->{$category}{$accno}{this} = $form->round_amount($form->{$category}{$accno}{this}, $dec);
 
 424       delete $form->{$category}{$accno}
 
 425         if (   $form->{$category}{$accno}{this} == 0
 
 426             && $form->{$category}{$accno}{last} == 0);
 
 430   $main::lxdebug->leave_sub();
 
 434   $main::lxdebug->enter_sub();
 
 436   my ($dbh, $last_period, $fromdate, $todate, $form, $category) = @_;
 
 438   my ($null, $department_id) = split /--/, $form->{department};
 
 452     $fromdate = conv_dateq($fromdate);
 
 453     if ($form->{method} eq 'cash') {
 
 454       $subwhere .= " AND (transdate    >= $fromdate)";
 
 455       $glwhere   = " AND (ac.transdate >= $fromdate)";
 
 456       $prwhere   = " AND (a.transdate  >= $fromdate)";
 
 457       $inwhere   = " AND (acc.transdate >= $fromdate)";
 
 459       $where    .= " AND (ac.transdate >= $fromdate)";
 
 464     $todate = conv_dateq($todate);
 
 465     $subwhere   .= " AND (transdate    <= $todate)";
 
 466     $where      .= " AND (ac.transdate <= $todate)";
 
 467     $prwhere    .= " AND (a.transdate  <= $todate)";
 
 468     $inwhere    .= " AND (acc.transdate <= $todate)";
 
 471   if ($department_id) {
 
 472     $dpt_join = qq| JOIN department t ON (a.department_id = t.id) |;
 
 473     $dpt_where = qq| AND (t.id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 476   if ($form->{project_id}) {
 
 477     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}) . qq|) |;
 
 481 # GUV patch by Ronny Rentner (Bug 1190)
 
 483 # GUV IST-Versteuerung
 
 485 # Alle tatsaechlichen _Zahlungseingaenge_
 
 486 # im Zeitraum erfassen
 
 487 # (Teilzahlungen werden prozentual auf verschiedene Steuern aufgeteilt)
 
 491   if ($form->{method} eq 'cash') {
 
 494        SELECT SUM( ac.amount *
 
 495                     (SELECT SUM(acc.amount) * -1
 
 497                      INNER JOIN chart c ON (acc.chart_id = c.id AND c.link LIKE '%AR_paid%')
 
 498                      WHERE 1=1 $inwhere AND acc.trans_id = ac.trans_id)
 
 499                   / (SELECT amount FROM ar WHERE id = ac.trans_id)
 
 500                 ) AS amount, c.pos_eur
 
 502        LEFT JOIN chart c ON (c.id  = ac.chart_id)
 
 503        LEFT JOIN ar      ON (ar.id = ac.trans_id)
 
 504        LEFT JOIN taxkeys tk ON (tk.id = (
 
 505                                   SELECT id FROM taxkeys
 
 506                                   WHERE chart_id = ac.chart_id
 
 507                                   AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
 
 508                                   ORDER BY startdate DESC LIMIT 1
 
 511       WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
 
 516        SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 518          JOIN chart c ON (c.id = ac.chart_id)
 
 519          JOIN ar a ON (a.id = ac.trans_id)
 
 521          WHERE $where $dpt_where
 
 522            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
 
 528          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 530          JOIN chart c ON (c.id = ac.chart_id)
 
 531          JOIN ap a ON (a.id = ac.trans_id)
 
 533          WHERE $where $dpt_where
 
 534            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
 
 540          SELECT SUM(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 542          JOIN chart c ON (c.id = ac.chart_id)
 
 543          JOIN gl a ON (a.id = ac.trans_id)
 
 545          WHERE $where $dpt_where $glwhere
 
 546            AND NOT ((c.link = 'AR') OR (c.link = 'AP'))
 
 551     if ($form->{project_id}) {
 
 555          SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 557          JOIN ar a ON (a.id = ac.trans_id)
 
 558          JOIN parts p ON (ac.parts_id = p.id)
 
 559          JOIN chart c on (p.income_accno_id = c.id)
 
 561          WHERE (c.category = 'I') $prwhere $dpt_where
 
 562            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AR_paid%') $subwhere)
 
 568          SELECT SUM(ac.sellprice * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 570          JOIN ap a ON (a.id = ac.trans_id)
 
 571          JOIN parts p ON (ac.parts_id = p.id)
 
 572          JOIN chart c on (p.expense_accno_id = c.id)
 
 574          WHERE (c.category = 'E') $prwhere $dpt_where
 
 575            AND ac.trans_id IN ( SELECT trans_id FROM acc_trans JOIN chart ON (chart_id = id) WHERE (link LIKE '%AP_paid%') $subwhere)
 
 581   } else {                      # if ($form->{method} eq 'cash')
 
 582                 # s.o. jan 15.11.2009
 
 583    # if ($department_id) {
 
 584          #              ($dpt_join, $dpt_where) = sql_department($department_id);
 
 588         SELECT sum(ac.amount * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 590         JOIN chart c ON (c.id = ac.chart_id)
 
 595         GROUP BY c.$category |;
 
 597     if ($form->{project_id}) {
 
 601         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 603         JOIN ar a ON (a.id = ac.trans_id)
 
 604         JOIN parts p ON (ac.parts_id = p.id)
 
 605         JOIN chart c on (p.income_accno_id = c.id)
 
 607         WHERE (c.category = 'I')
 
 615         SELECT SUM(ac.sellprice * ac.qty * chart_category_to_sgn(c.category)) AS amount, c.$category
 
 617         JOIN ap a ON (a.id = ac.trans_id)
 
 618         JOIN parts p ON (ac.parts_id = p.id)
 
 619         JOIN chart c on (p.expense_accno_id = c.id)
 
 621         WHERE (c.category = 'E')
 
 625         GROUP BY c.$category |;
 
 633   foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
 
 634     if ($category eq "pos_bwa") {
 
 636         $form->{ $ref->{$category} }{kumm} += $ref->{amount};
 
 638         $form->{ $ref->{$category} }{jetzt} += $ref->{amount};
 
 641       $form->{ $ref->{$category} } += $ref->{amount};
 
 645   $main::lxdebug->leave_sub();
 
 649   $main::lxdebug->enter_sub();
 
 651   my ($self, $myconfig, $form, %options) = @_;
 
 653   my $dbh = $form->dbconnect($myconfig);
 
 655   my ($query, $sth, $ref);
 
 658   my ($null, $department_id) = split /--/, $form->{department};
 
 659   my @headingaccounts = ();
 
 665   my $invwhere = $where;
 
 667   if ($department_id) {
 
 668     $dpt_join = qq| JOIN dpt_trans t ON (ac.trans_id = t.trans_id) |;
 
 669     $dpt_where = qq| AND (t.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
 672   # project_id only applies to getting transactions
 
 673   # it has nothing to do with a trial balance
 
 674   # but we use the same function to collect information
 
 676   if ($form->{project_id}) {
 
 677     $project = qq| AND (ac.project_id = | . conv_i($form->{project_id}, 'NULL') . qq|) |;
 
 680   my $acc_cash_where = "";
 
 681 #  my $ar_cash_where = "";
 
 682 #  my $ap_cash_where = "";
 
 685   if ($form->{method} eq "cash") {
 
 687       qq| AND (ac.trans_id IN (
 
 690             WHERE datepaid >= '$form->{fromdate}'
 
 691               AND datepaid <= '$form->{todate}'
 
 697             WHERE datepaid >= '$form->{fromdate}'
 
 698               AND datepaid <= '$form->{todate}'
 
 704             WHERE transdate >= '$form->{fromdate}'
 
 705               AND transdate <= '$form->{todate}'
 
 707 #    $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
 
 710   if ($options{beginning_balances}) {
 
 711     foreach my $prefix (qw(from to)) {
 
 712       next if ($form->{"${prefix}date"});
 
 714       my $min_max = $prefix eq 'from' ? 'min' : 'max';
 
 715       $query      = qq|SELECT ${min_max}(transdate)
 
 721       ($form->{"${prefix}date"}) = selectfirst_array_query($form, $dbh, $query);
 
 724     # get beginning balances
 
 726       qq|SELECT c.accno, c.category, SUM(ac.amount) AS amount, c.description
 
 728           LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 730           WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
 
 733           GROUP BY c.accno, c.category, c.description |;
 
 735     $sth = prepare_execute_query($form, $dbh, $query, $form->{fromdate});
 
 737     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 739       if ($ref->{amount} != 0 || $form->{all_accounts}) {
 
 740         $trb{ $ref->{accno} }{description} = $ref->{description};
 
 741         $trb{ $ref->{accno} }{charttype}   = 'A';
 
 742         $trb{ $ref->{accno} }{beginning_balance} = $ref->{amount};
 
 744         if ($ref->{amount} > 0) {
 
 745           $trb{ $ref->{accno} }{haben_eb}   = $ref->{amount};
 
 747           $trb{ $ref->{accno} }{soll_eb}   = $ref->{amount} * -1;
 
 749         $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 758     qq|SELECT c.accno, c.description, c.category
 
 760        WHERE c.charttype = 'H'
 
 763   $sth = prepare_execute_query($form, $dbh, $query);
 
 765   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 766     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 767     $trb{ $ref->{accno} }{charttype}   = 'H';
 
 768     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 770     push @headingaccounts, $ref->{accno};
 
 776   my $saldowhere    = " 1 = 1 ";
 
 777   my $sumwhere      = " 1 = 1 ";
 
 779   my $sumsubwhere   = '';
 
 780   my $saldosubwhere = '';
 
 781   my $glsaldowhere  = '';
 
 786   my ($fromdate, $todate);
 
 788   if ($form->{fromdate} || $form->{todate}) {
 
 789     if ($form->{fromdate}) {
 
 790       $fromdate = conv_dateq($form->{fromdate});
 
 791       $tofrom        .= " AND (ac.transdate >= $fromdate)";
 
 792       $subwhere      .= " AND (ac.transdate >= $fromdate)";
 
 793       $sumsubwhere   .= " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
 
 794       $saldosubwhere .= " AND (ac,transdate>=(select date_trunc('year', date $fromdate)))  ";
 
 795       $invwhere      .= " AND (a.transdate >= $fromdate)";
 
 796       $glsaldowhere  .= " AND ac.transdate>=(select date_trunc('year', date $fromdate)) ";
 
 797       $glwhere        = " AND (ac.transdate >= $fromdate)";
 
 798       $glsumwhere     = " AND (ac.transdate >= (select date_trunc('year', date $fromdate))) ";
 
 800     if ($form->{todate}) {
 
 801       $todate = conv_dateq($form->{todate});
 
 802       $tofrom        .= " AND (ac.transdate <= $todate)";
 
 803       $invwhere      .= " AND (a.transdate <= $todate)";
 
 804       $saldosubwhere .= " AND (ac.transdate <= $todate)";
 
 805       $sumsubwhere   .= " AND (ac.transdate <= $todate)";
 
 806       $subwhere      .= " AND (ac.transdate <= $todate)";
 
 807       $glwhere       .= " AND (ac.transdate <= $todate)";
 
 808       $glsumwhere    .= " AND (ac.transdate <= $todate) ";
 
 809       $glsaldowhere  .= " AND (ac.transdate <= $todate) ";
 
 813   if ($form->{method} eq "cash") {
 
 815       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) |;
 
 816     $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) |;
 
 818     $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) |;
 
 820     $where .= $tofrom . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 821     $saldowhere .= $glsaldowhere . " AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 822     $sumwhere .= $glsumwhere . " AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND (NOT ac.cb_transaction OR ac.cb_transaction IS NULL)";
 
 826        SELECT c.accno, c.description, c.category, SUM(ac.amount) AS amount
 
 828        JOIN chart c ON (c.id = ac.chart_id)
 
 833        GROUP BY c.accno, c.description, c.category |;
 
 835   if ($form->{project_id}) {
 
 837       -- add project transactions from invoice
 
 841       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) AS amount
 
 843       JOIN ar a ON (ac.trans_id = a.id)
 
 844       JOIN parts p ON (ac.parts_id = p.id)
 
 845       JOIN chart c ON (p.income_accno_id = c.id)
 
 850       GROUP BY c.accno, c.description, c.category
 
 854       SELECT c.accno, c.description, c.category, SUM(ac.sellprice * ac.qty) * -1 AS amount
 
 856       JOIN ap a ON (ac.trans_id = a.id)
 
 857       JOIN parts p ON (ac.parts_id = p.id)
 
 858       JOIN chart c ON (p.expense_accno_id = c.id)
 
 863       GROUP BY c.accno, c.description, c.category
 
 867   $query .= qq| ORDER BY accno|;
 
 869   $sth = prepare_execute_query($form, $dbh, $query);
 
 871   # calculate the debit and credit in the period
 
 872   while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 873     $trb{ $ref->{accno} }{description} = $ref->{description};
 
 874     $trb{ $ref->{accno} }{charttype}   = 'A';
 
 875     $trb{ $ref->{accno} }{category}    = $ref->{category};
 
 876     $trb{ $ref->{accno} }{amount} += $ref->{amount};
 
 880   # prepare query for each account
 
 881   my ($q_drcr, $drcr, $q_project_drcr, $project_drcr);
 
 885          (SELECT SUM(ac.amount) * -1
 
 887           JOIN chart c ON (c.id = ac.chart_id)
 
 893           AND (c.accno = ?)) AS debit,
 
 895          (SELECT SUM(ac.amount)
 
 897           JOIN chart c ON (c.id = ac.chart_id)
 
 903           AND c.accno = ?) AS credit,
 
 904         (SELECT SUM(ac.amount)
 
 906          JOIN chart c ON (ac.chart_id = c.id)
 
 911          AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
 
 913         (SELECT SUM(ac.amount)
 
 915          JOIN chart c ON (ac.chart_id = c.id)
 
 921          AND c.accno = ?) AS sum_credit,
 
 923         (SELECT SUM(ac.amount)
 
 925          JOIN chart c ON (ac.chart_id = c.id)
 
 931          AND c.accno = ?) AS sum_debit,
 
 933         (SELECT max(ac.transdate) FROM acc_trans ac
 
 934         JOIN chart c ON (ac.chart_id = c.id)
 
 939         AND c.accno = ?) AS last_transaction
 
 944   $drcr = prepare_query($form, $dbh, $q_drcr);
 
 946   if ($form->{project_id}) {
 
 947     # prepare query for each account
 
 950           (SELECT SUM(ac.sellprice * ac.qty) * -1
 
 952            JOIN parts p ON (ac.parts_id = p.id)
 
 953            JOIN ap a ON (ac.trans_id = a.id)
 
 954            JOIN chart c ON (p.expense_accno_id = c.id)
 
 959            AND c.accno = ?) AS debit,
 
 961           (SELECT SUM(ac.sellprice * ac.qty)
 
 963            JOIN parts p ON (ac.parts_id = p.id)
 
 964            JOIN ar a ON (ac.trans_id = a.id)
 
 965            JOIN chart c ON (p.income_accno_id = c.id)
 
 970            AND c.accno = ?) AS credit,
 
 972         (SELECT SUM(ac.amount)
 
 974          JOIN chart c ON (ac.chart_id = c.id)
 
 979          AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)) AS saldo,
 
 981         (SELECT SUM(ac.amount)
 
 983          JOIN chart c ON (ac.chart_id = c.id)
 
 989          AND c.accno = ?) AS sum_credit,
 
 991         (SELECT SUM(ac.amount)
 
 993          JOIN chart c ON (ac.chart_id = c.id)
 
 999          AND c.accno = ?) AS sum_debit,
 
1002         (SELECT max(ac.transdate) FROM acc_trans ac
 
1003         JOIN chart c ON (ac.chart_id = c.id)
 
1008         AND c.accno = ?) AS last_transaction
 
1011     $project_drcr = prepare_query($form, $dbh, $q_project_drcr);
 
1015   my ($debit, $credit, $saldo, $soll_saldo, $haben_saldo,$soll_kummuliert, $haben_kummuliert, $last_transaction);
 
1017   foreach my $accno (sort keys %trb) {
 
1020     $ref->{accno} = $accno;
 
1021     map { $ref->{$_} = $trb{$accno}{$_} }
 
1022       qw(description category charttype amount soll_eb haben_eb beginning_balance);
 
1024     $ref->{balance} = $form->round_amount($balance{ $ref->{accno} }, 2);
 
1026     if ($trb{$accno}{charttype} eq 'A') {
 
1029       do_statement($form, $drcr, $q_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1031       ($debit, $credit, $saldo, $haben_saldo, $soll_saldo) = (0, 0, 0, 0, 0);
 
1032       my ($soll_kumuliert, $haben_kumuliert) = (0, 0);
 
1033       $last_transaction = "";
 
1034       while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $drcr->fetchrow_array) {
 
1035         $ref->{debit}  += $debit;
 
1036         $ref->{credit} += $credit;
 
1038           $ref->{haben_saldo} += $saldo;
 
1040           $ref->{soll_saldo} += $saldo * -1;
 
1042         $ref->{last_transaction} = $last_transaction;
 
1043         $ref->{soll_kumuliert} = $soll_kumuliert * -1;
 
1044         $ref->{haben_kumuliert} = $haben_kumuliert;
 
1048       if ($form->{project_id}) {
 
1051         do_statement($form, $project_drcr, $q_project_drcr, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno}, $ref->{accno});
 
1053         ($debit, $credit) = (0, 0);
 
1054         while (($debit, $credit, $saldo, $haben_kumuliert, $soll_kumuliert, $last_transaction) = $project_drcr->fetchrow_array) {
 
1055           $ref->{debit}  += $debit;
 
1056           $ref->{credit} += $credit;
 
1058             $ref->{haben_saldo} += $saldo;
 
1060             $ref->{soll_saldo} += $saldo * -1;
 
1062           $ref->{soll_kumuliert} += $soll_kumuliert * -1;
 
1063           $ref->{haben_kumuliert} += $haben_kumuliert;
 
1065         $project_drcr->finish;
 
1068       $ref->{debit}  = $form->round_amount($ref->{debit},  2);
 
1069       $ref->{credit} = $form->round_amount($ref->{credit}, 2);
 
1071       if ($ref->{haben_saldo} != 0) {
 
1072         $ref->{haben_saldo}  = $ref->{haben_saldo} + $ref->{beginning_balance};
 
1073         if ($ref->{haben_saldo} < 0) {
 
1074           $ref->{soll_saldo} = $form->round_amount(($ref->{haben_saldo} *- 1), 2);
 
1075           $ref->{haben_saldo} = 0;
 
1078         $ref->{soll_saldo} = $ref->{soll_saldo} - $ref->{beginning_balance};
 
1079         if ($ref->{soll_saldo} < 0) {
 
1080           $ref->{haben_saldo} = $form->round_amount(($ref->{soll_saldo} * -1), 2);
 
1081           $ref->{soll_saldo} = 0;
 
1084       $ref->{haben_saldo} = $form->round_amount($ref->{haben_saldo}, 2);
 
1085       $ref->{soll_saldo} = $form->round_amount($ref->{soll_saldo}, 2);
 
1086       $ref->{haben_kumuliert}  = $form->round_amount($ref->{haben_kumuliert},  2);
 
1087       $ref->{soll_kumuliert} = $form->round_amount($ref->{soll_kumuliert}, 2);
 
1092     @accno = grep { $_ le "$ref->{accno}" } @headingaccounts;
 
1093     $accno = pop @accno;
 
1095       $trb{$accno}{debit}  += $ref->{debit};
 
1096       $trb{$accno}{credit} += $ref->{credit};
 
1097       $trb{$accno}{soll_saldo}  += $ref->{soll_saldo};
 
1098       $trb{$accno}{haben_saldo} += $ref->{haben_saldo};
 
1099       $trb{$accno}{soll_kumuliert}  += $ref->{soll_kumuliert};
 
1100       $trb{$accno}{haben_kumuliert} += $ref->{haben_kumuliert};
 
1103     push @{ $form->{TB} }, $ref;
 
1109   # debits and credits for headings
 
1110   foreach my $accno (@headingaccounts) {
 
1111     foreach $ref (@{ $form->{TB} }) {
 
1112       if ($accno eq $ref->{accno}) {
 
1113         $ref->{debit}           = $trb{$accno}{debit};
 
1114         $ref->{credit}          = $trb{$accno}{credit};
 
1115         $ref->{soll_saldo}      = $trb{$accno}{soll_saldo};
 
1116         $ref->{haben_saldo}     = $trb{$accno}{haben_saldo};
 
1117         $ref->{soll_kumuliert}  = $trb{$accno}{soll_kumuliert};
 
1118         $ref->{haben_kumuliert} = $trb{$accno}{haben_kumuliert};
 
1123   $main::lxdebug->leave_sub();
 
1127   $main::lxdebug->enter_sub();
 
1128   my ($self, $dbh, $form) = @_;
 
1129   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
1130   my $query = qq|SELECT invnumber FROM $arap WHERE invnumber LIKE "Storno zu "|;
 
1131   my $sth =  $dbh->prepare($query);
 
1132   while(my $ref = $sth->fetchrow_hashref()) {
 
1133     $ref->{invnumer} =~ s/Storno zu //g;
 
1134     $form->{storno}{$ref->{invnumber}} = 1;
 
1136   $main::lxdebug->leave_sub();
 
1140   $main::lxdebug->enter_sub();
 
1142   my ($self, $myconfig, $form) = @_;
 
1144   # connect to database
 
1145   my $dbh     = $form->dbconnect($myconfig);
 
1147   my ($invoice, $arap, $buysell, $ct, $ct_id, $ml);
 
1149   if ($form->{ct} eq "customer") {
 
1162   $ct_id = "${ct}_id";
 
1164   $form->{todate} = $form->current_date($myconfig) unless ($form->{todate});
 
1165   my $todate = conv_dateq($form->{todate});
 
1166   my $fromdate = conv_dateq($form->{fromdate});
 
1168   my $fromwhere = ($form->{fromdate} ne "") ? " AND (transdate >= (date $fromdate)) " : "";
 
1170   my $where = " 1 = 1 ";
 
1173   if ($form->{$ct_id}) {
 
1174     $where .= qq| AND (ct.id = | . conv_i($form->{$ct_id}) . qq|)|;
 
1175   } elsif ($form->{ $form->{ct} }) {
 
1176     $where .= qq| AND (ct.name ILIKE | . $dbh->quote('%' . $form->{$ct} . '%') . qq|)|;
 
1180   if ($form->{department}) {
 
1181     my ($null, $department_id) = split /--/, $form->{department};
 
1182     $dpt_join = qq| JOIN department d ON (a.department_id = d.id) |;
 
1183     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|)|;
 
1187     -- between 0-30 days
 
1189     SELECT ${ct}.id AS ctid, ${ct}.name,
 
1190       street, zipcode, city, country, contact, email,
 
1191       phone as customerphone, fax as customerfax, ${ct}number,
 
1192       "invnumber", "transdate",
 
1193       (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",
 
1194       "duedate", invoice, ${arap}.id,
 
1197        WHERE (${arap}.curr = exchangerate.curr)
 
1198          AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate
 
1200     WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null))
 
1201       AND (${arap}.storno IS FALSE)
 
1202       AND (${arap}.${ct}_id = ${ct}.id)
 
1204       AND (transdate <= (date $todate) $fromwhere )
 
1206     ORDER BY ctid, transdate, invnumber |;
 
1208   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1210   # select outstanding vendors or customers, depends on $ct
 
1212     qq|SELECT DISTINCT ct.id, ct.name
 
1213        FROM $ct ct, $arap a
 
1216          AND (a.${ct_id} = ct.id)
 
1217          AND ((a.paid != a.amount) OR ((a.datepaid > $todate) AND (datepaid is NOT NULL)))
 
1218          AND (a.transdate <= $todate $fromwhere)
 
1221   my $sth = prepare_execute_query($form, $dbh, $query);
 
1224   # for each company that has some stuff outstanding
 
1225   while (my ($id) = $sth->fetchrow_array) {
 
1226     do_statement($form, $sth_details, $q_details, $id);
 
1228     while (my $ref = $sth_details->fetchrow_hashref("NAME_lc")) {
 
1229       $ref->{module} = ($ref->{invoice}) ? $invoice : $arap;
 
1230       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
1231       push @{ $form->{AG} }, $ref;
 
1234     $sth_details->finish;
 
1243   $main::lxdebug->leave_sub();
 
1247   $main::lxdebug->enter_sub();
 
1249   my ($self, $myconfig, $form) = @_;
 
1251   # connect to database
 
1252   my $dbh = $form->dbconnect($myconfig);
 
1254   my $ct = $form->{ct} eq "customer" ? "customer" : "vendor";
 
1257     qq|SELECT ct.name, ct.email, ct.cc, ct.bcc
 
1260   ($form->{ $form->{ct} }, $form->{email}, $form->{cc}, $form->{bcc}) =
 
1261     selectrow_query($form, $dbh, $query, $form->{"${ct}_id"});
 
1264   $main::lxdebug->leave_sub();
 
1267 sub get_taxaccounts {
 
1268   $main::lxdebug->enter_sub();
 
1270   my ($self, $myconfig, $form) = @_;
 
1272   # connect to database
 
1273   my $dbh = $form->dbconnect($myconfig);
 
1277     qq|SELECT c.accno, c.description, t.rate
 
1279        WHERE (c.link LIKE '%CT_tax%') AND (c.id = t.chart_id)
 
1281   $form->{taxaccounts} = selectall_hashref_quert($form, $dbh, $query);
 
1285   $main::lxdebug->leave_sub();
 
1289   $main::lxdebug->enter_sub();
 
1291   my ($self, $myconfig, $form) = @_;
 
1293   # connect to database
 
1294   my $dbh = $form->dbconnect($myconfig);
 
1296   my ($null, $department_id) = split /--/, $form->{department};
 
1299   my $where = "1 = 1";
 
1301   if ($department_id) {
 
1302     $where .= qq| AND (a.department_id = | . conv_i($department_id, 'NULL') . qq|) |;
 
1307   if ($form->{accno}) {
 
1308     $accno = $form->{accno};
 
1309     $rate  = $form->{"$form->{accno}_rate"};
 
1310     $accno = qq| AND (ch.accno = | . $dbh->quote($accno) . qq|)|;
 
1316   if ($form->{db} eq 'ar') {
 
1317     $table = "customer";
 
1324   my $arap = lc($ARAP);
 
1326   my $transdate = "a.transdate";
 
1328   if ($form->{method} eq 'cash') {
 
1329     $transdate = "a.datepaid";
 
1331     my $todate = conv_dateq($form->{todate} ? $form->{todate} : $form->current_date($myconfig));
 
1338           JOIN chart ON (chart_id = id)
 
1339           WHERE (link LIKE '%${ARAP}_paid%')
 
1340           AND (transdate <= $todate)
 
1345   # if there are any dates construct a where
 
1346   $where .= " AND ($transdate >= " . conv_dateq($form->{fromdate}) . ") " if ($form->{fromdate});
 
1347   $where .= " AND ($transdate <= " . conv_dateq($form->{todate}) . ") " if ($form->{todate});
 
1349   my $ml = ($form->{db} eq 'ar') ? 1 : -1;
 
1351   my $sortorder = join ', ', $form->sort_columns(qw(transdate invnumber name));
 
1352   $sortorder = $form->{sort} if ($form->{sort} && grep({ $_ eq $form->{sort} } qw(id transdate invnumber name netamount tax)));
 
1355   if ($form->{report} !~ /nontaxable/) {
 
1357       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount,
 
1358           ac.amount * $ml AS tax
 
1360          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1361          JOIN chart ch ON (ch.id = ac.chart_id)
 
1362          JOIN $table n ON (n.id = a.${table}_id)
 
1366            AND (a.invoice = '0')
 
1370          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount,
 
1371            i.sellprice * i.qty * $rate * $ml AS tax
 
1373          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1374          JOIN chart ch ON (ch.id = ac.chart_id)
 
1375          JOIN $table n ON (n.id = a.${table}_id)
 
1376          JOIN ${table}tax t ON (t.${table}_id = n.id)
 
1377          JOIN invoice i ON (i.trans_id = a.id)
 
1378          JOIN partstax p ON (p.parts_id = i.parts_id)
 
1382            AND (a.invoice = '1')
 
1383          ORDER BY $sortorder|;
 
1385     # only gather up non-taxable transactions
 
1387       qq|SELECT a.id, '0' AS invoice, $transdate AS transdate, a.invnumber, n.name, a.netamount
 
1389          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1390          JOIN $table n ON (n.id = a.${table}_id)
 
1393            AND (a.invoice = '0')
 
1394            AND (a.netamount = a.amount)
 
1398          SELECT a.id, '1' AS invoice, $transdate AS transdate, a.invnumber, n.name, i.sellprice * i.qty AS netamount
 
1400          JOIN ${arap} a ON (a.id = ac.trans_id)
 
1401          JOIN $table n ON (n.id = a.${table}_id)
 
1402          JOIN invoice i ON (i.trans_id = a.id)
 
1405            AND (a.invoice = '1')
 
1407              a.${table}_id NOT IN (SELECT ${table}_id FROM ${table}tax t (${table}_id))
 
1409              i.parts_id NOT IN (SELECT parts_id FROM partstax p (parts_id))
 
1411          GROUP BY a.id, a.invnumber, $transdate, n.name, i.sellprice, i.qty
 
1412          ORDER by $sortorder|;
 
1415   $form->{TR} = selectall_hashref_query($form, $dbh, $query);
 
1419   $main::lxdebug->leave_sub();
 
1422 sub paymentaccounts {
 
1423   $main::lxdebug->enter_sub();
 
1425   my ($self, $myconfig, $form) = @_;
 
1427   # connect to database, turn AutoCommit off
 
1428   my $dbh = $form->dbconnect_noauto($myconfig);
 
1430   my $ARAP = $form->{db} eq "ar" ? "AR" : "AP";
 
1432   # get A(R|P)_paid accounts
 
1434     qq|SELECT accno, description
 
1436        WHERE link LIKE '%${ARAP}_paid%'|;
 
1437   $form->{PR} = selectall_hashref_query($form, $dbh, $query);
 
1441   $main::lxdebug->leave_sub();
 
1445   $main::lxdebug->enter_sub();
 
1447   my ($self, $myconfig, $form) = @_;
 
1449   # connect to database, turn AutoCommit off
 
1450   my $dbh = $form->dbconnect_noauto($myconfig);
 
1455   if ($form->{db} eq 'ar') {
 
1456     $table = 'customer';
 
1468   if ($form->{department_id}) {
 
1469     $dpt_join = qq| JOIN dpt_trans t ON (t.trans_id = ac.trans_id) |;
 
1470     $where = qq| AND (t.department_id = | . conv_i($form->{department_id}, 'NULL') . qq|) |;
 
1473   if ($form->{fromdate}) {
 
1474     $where .= " AND (ac.transdate >= " . $dbh->quote($form->{fromdate}) . ") ";
 
1476   if ($form->{todate}) {
 
1477     $where .= " AND (ac.transdate <= " . $dbh->quote($form->{todate}) . ") ";
 
1479   if (!$form->{fx_transaction}) {
 
1480     $where .= " AND ac.fx_transaction = '0'";
 
1485   if ($form->{reference}) {
 
1486     $reference = $dbh->quote('%' . $form->{reference} . '%');
 
1487     $invnumber = " AND (a.invnumber LIKE $reference)";
 
1488     $reference = " AND (g.reference LIKE $reference)";
 
1490   if ($form->{source}) {
 
1491     $where .= " AND (ac.source ILIKE " . $dbh->quote('%' . $form->{source} . '%') . ") ";
 
1493   if ($form->{memo}) {
 
1494     $where .= " AND (ac.memo ILIKE " . $dbh->quote('%' . $form->{memo} . '%') . ") ";
 
1497   my %sort_columns =  (
 
1498     'transdate'    => [ qw(transdate lower_invnumber lower_name) ],
 
1499     'invnumber'    => [ qw(lower_invnumber lower_name transdate) ],
 
1500     'name'         => [ qw(lower_name transdate)                 ],
 
1501     'source'       => [ qw(lower_source)                         ],
 
1502     'memo'         => [ qw(lower_memo)                           ],
 
1504   my %lowered_columns =  (
 
1505     'invnumber'       => { 'gl' => 'g.reference',   'arap' => 'a.invnumber', },
 
1506     'memo'            => { 'gl' => 'ac.memo',       'arap' => 'ac.memo',     },
 
1507     'source'          => { 'gl' => 'ac.source',     'arap' => 'ac.source',   },
 
1508     'name'            => { 'gl' => 'g.description', 'arap' => 'c.name',      },
 
1511   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
1512   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : 'transdate';
 
1513   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
1516   my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
 
1517   foreach my $spec (@{ $sort_columns{$sortkey} }) {
 
1518     next if ($spec !~ m/^lower_(.*)$/);
 
1521     map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
 
1524   $query = qq|SELECT id, accno, description FROM chart WHERE accno = ?|;
 
1525   $sth = prepare_query($form, $dbh, $query);
 
1528       qq|SELECT c.name, a.invnumber, a.ordnumber,
 
1529            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1530            a.invoice, a.id, ac.memo, '${arap}' AS module
 
1531            $columns_for_sorting{arap}
 
1533          JOIN $arap a ON (ac.trans_id = a.id)
 
1534          JOIN $table c ON (c.id = a.${table}_id)
 
1536          WHERE (ac.chart_id = ?)
 
1542          SELECT g.description, g.reference, NULL AS ordnumber,
 
1543            ac.transdate, ac.amount * $ml AS paid, ac.source,
 
1544            '0' as invoice, g.id, ac.memo, 'gl' AS module
 
1545            $columns_for_sorting{gl}
 
1547          JOIN gl g ON (g.id = ac.trans_id)
 
1549          WHERE (ac.chart_id = ?)
 
1552            AND (ac.amount * $ml) > 0
 
1554          ORDER BY $sortorder|;
 
1555   my $sth_details = prepare_query($form, $dbh, $q_details);
 
1559   # cycle through each id
 
1560   foreach my $accno (split(/ /, $form->{paymentaccounts})) {
 
1561     do_statement($form, $sth, $query, $accno);
 
1562     my $ref = $sth->fetchrow_hashref();
 
1563     push(@{ $form->{PR} }, $ref);
 
1566     $form->{ $ref->{id} } = [] unless ($form->{ $ref->{id} });
 
1568     do_statement($form, $sth_details, $q_details, $ref->{id}, $ref->{id});
 
1569     while (my $pr = $sth_details->fetchrow_hashref()) {
 
1570       push(@{ $form->{ $ref->{id} } }, $pr);
 
1572     $sth_details->finish();
 
1577   $main::lxdebug->leave_sub();
 
1581   $main::lxdebug->enter_sub();
 
1583   my ($self, $myconfig, $form) = @_;
 
1585   # connect to database
 
1586   my $dbh = $form->dbconnect($myconfig);
 
1588   my $last_period = 0;
 
1591     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);
 
1593   $form->{decimalplaces} *= 1;
 
1595   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_bwa");
 
1597   # if there are any compare dates
 
1599   if ($form->{fromdate} || $form->{todate}) {
 
1601     if ($form->{fromdate}) {
 
1602       $form->{fromdate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1605       $form->{todate} =~ /[0-9]*\.[0-9]*\.([0-9]*)/;
 
1608     my $kummfromdate = $form->{comparefromdate};
 
1609     my $kummtodate   = $form->{comparetodate};
 
1610     &get_accounts_g($dbh, $last_period, $kummfromdate, $kummtodate, $form, "pos_bwa");
 
1613   my @periods        = qw(jetzt kumm);
 
1614   my @gesamtleistung = qw(1 3);
 
1615   my @gesamtkosten   = qw (10 11 12 13 14 15 16 17 18 19 20);
 
1617     qw (rohertrag betriebrohertrag betriebsergebnis neutraleraufwand neutralerertrag ergebnisvorsteuern ergebnis gesamtleistung gesamtkosten);
 
1619   foreach my $key (@periods) {
 
1620     $form->{ "$key" . "gesamtleistung" } = 0;
 
1621     $form->{ "$key" . "gesamtkosten" }   = 0;
 
1623     foreach $category (@categories) {
 
1625       if (defined($form->{$category}{$key})) {
 
1626         $form->{"$key$category"} =
 
1627           $form->format_amount($myconfig,
 
1628                                $form->round_amount($form->{$category}{$key}, 2
 
1630                                $form->{decimalplaces},
 
1634     foreach my $item (@gesamtleistung) {
 
1635       $form->{ "$key" . "gesamtleistung" } += $form->{$item}{$key};
 
1637     $form->{ "$key" . "gesamtleistung" } -= $form->{2}{$key};
 
1639     foreach my $item (@gesamtkosten) {
 
1640       $form->{ "$key" . "gesamtkosten" } += $form->{$item}{$key};
 
1642     $form->{ "$key" . "rohertrag" } =
 
1643       $form->{ "$key" . "gesamtleistung" } - $form->{4}{$key};
 
1644     $form->{ "$key" . "betriebrohertrag" } =
 
1645       $form->{ "$key" . "rohertrag" } + $form->{5}{$key};
 
1646     $form->{ "$key" . "betriebsergebnis" } =
 
1647       $form->{ "$key" . "betriebrohertrag" } -
 
1648       $form->{ "$key" . "gesamtkosten" };
 
1649     $form->{ "$key" . "neutraleraufwand" } =
 
1650       $form->{30}{$key} + $form->{31}{$key};
 
1651     $form->{ "$key" . "neutralertrag" } =
 
1652       $form->{32}{$key} + $form->{33}{$key} + $form->{34}{$key};
 
1653     $form->{ "$key" . "ergebnisvorsteuern" } =
 
1654       $form->{ "$key" . "betriebsergebnis" } -
 
1655       $form->{ "$key" . "neutraleraufwand" } +
 
1656       $form->{ "$key" . "neutralertrag" };
 
1657     $form->{ "$key" . "ergebnis" } =
 
1658       $form->{ "$key" . "ergebnisvorsteuern" } - $form->{35}{$key};
 
1660     if ($form->{ "$key" . "gesamtleistung" } > 0) {
 
1661       foreach $category (@categories) {
 
1662         if (defined($form->{$category}{$key})) {
 
1663           $form->{ "$key" . "gl" . "$category" } =
 
1664             $form->format_amount(
 
1666                                $form->round_amount(
 
1667                                  ($form->{$category}{$key} /
 
1668                                     $form->{ "$key" . "gesamtleistung" } * 100
 
1670                                  $form->{decimalplaces}
 
1672                                $form->{decimalplaces},
 
1676       foreach my $item (@ergebnisse) {
 
1677         $form->{ "$key" . "gl" . "$item" } =
 
1678           $form->format_amount($myconfig,
 
1679                                $form->round_amount(
 
1680                                  ( $form->{ "$key" . "$item" } /
 
1681                                      $form->{ "$key" . "gesamtleistung" } * 100
 
1683                                  $form->{decimalplaces}
 
1685                                $form->{decimalplaces},
 
1690     if ($form->{ "$key" . "gesamtkosten" } > 0) {
 
1691       foreach $category (@categories) {
 
1692         if (defined($form->{$category}{$key})) {
 
1693           $form->{ "$key" . "gk" . "$category" } =
 
1694             $form->format_amount($myconfig,
 
1695                                  $form->round_amount(
 
1696                                    ($form->{$category}{$key} /
 
1697                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1699                                    $form->{decimalplaces}
 
1701                                  $form->{decimalplaces},
 
1705       foreach my $item (@ergebnisse) {
 
1706         $form->{ "$key" . "gk" . "$item" } =
 
1707           $form->format_amount($myconfig,
 
1708                                $form->round_amount(
 
1709                                    ($form->{ "$key" . "$item" } /
 
1710                                       $form->{ "$key" . "gesamtkosten" } * 100
 
1712                                    $form->{decimalplaces}
 
1714                                $form->{decimalplaces},
 
1719     if ($form->{10}{$key} > 0) {
 
1720       foreach $category (@categories) {
 
1721         if (defined($form->{$category}{$key})) {
 
1722           $form->{ "$key" . "pk" . "$category" } =
 
1723             $form->format_amount(
 
1725                         $form->round_amount(
 
1726                           ($form->{$category}{$key} / $form->{10}{$key} * 100),
 
1727                           $form->{decimalplaces}
 
1729                         $form->{decimalplaces},
 
1733       foreach my $item (@ergebnisse) {
 
1734         $form->{ "$key" . "pk" . "$item" } =
 
1735           $form->format_amount($myconfig,
 
1736                                $form->round_amount(
 
1737                                                 ($form->{ "$key" . "$item" } /
 
1738                                                    $form->{10}{$key} * 100
 
1740                                                 $form->{decimalplaces}
 
1742                                $form->{decimalplaces},
 
1747     if ($form->{4}{$key} > 0) {
 
1748       foreach $category (@categories) {
 
1749         if (defined($form->{$category}{$key})) {
 
1750           $form->{ "$key" . "auf" . "$category" } =
 
1751             $form->format_amount(
 
1753                          $form->round_amount(
 
1754                            ($form->{$category}{$key} / $form->{4}{$key} * 100),
 
1755                            $form->{decimalplaces}
 
1757                          $form->{decimalplaces},
 
1761       foreach my $item (@ergebnisse) {
 
1762         $form->{ "$key" . "auf" . "$item" } =
 
1763           $form->format_amount($myconfig,
 
1764                                $form->round_amount(
 
1765                                                 ($form->{ "$key" . "$item" } /
 
1766                                                    $form->{4}{$key} * 100
 
1768                                                 $form->{decimalplaces}
 
1770                                $form->{decimalplaces},
 
1775     foreach my $item (@ergebnisse) {
 
1776       $form->{ "$key" . "$item" } =
 
1777         $form->format_amount($myconfig,
 
1778                              $form->round_amount($form->{ "$key" . "$item" },
 
1779                                                  $form->{decimalplaces}
 
1781                              $form->{decimalplaces},
 
1788   $main::lxdebug->leave_sub();
 
1792   $main::lxdebug->enter_sub();
 
1794   my ($self, $myconfig, $form) = @_;
 
1796   # connect to database
 
1797   my $dbh = $form->dbconnect($myconfig);
 
1799   my $last_period     = 0;
 
1800   my @categories_cent = qw(51r 511 86r 861 97r 971 93r 931
 
1801     96 66 43 45 53 62 65 67);
 
1802   my @categories_euro = qw(48 51 86 91 97 93 94);
 
1803   $form->{decimalplaces} *= 1;
 
1805   foreach my $item (@categories_cent) {
 
1806     $form->{"$item"} = 0;
 
1808   foreach my $item (@categories_euro) {
 
1809     $form->{"$item"} = 0;
 
1812   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate}, $form, "pos_ustva");
 
1814   #   foreach $item (@categories_cent) {
 
1815   #     if ($form->{$item}{"jetzt"} > 0) {
 
1816   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1817   #             delete $form->{$item}{"jetzt"};
 
1820   #   foreach $item (@categories_euro) {
 
1821   #     if ($form->{$item}{"jetzt"} > 0) {
 
1822   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1823   #             delete $form->{$item}{"jetzt"};
 
1824   #     }  foreach $item (@categories_cent) {
 
1825   #     if ($form->{$item}{"jetzt"} > 0) {
 
1826   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1827   #             delete $form->{$item}{"jetzt"};
 
1830   #   foreach $item (@categories_euro) {
 
1831   #     if ($form->{$item}{"jetzt"} > 0) {
 
1832   #             $form->{$item} = $form->{$item}{"jetzt"};
 
1833   #             delete $form->{$item}{"jetzt"};
 
1840   # Berechnung der USTVA Formularfelder
 
1842   $form->{"51r"} = $form->{"511"};
 
1843   $form->{"86r"} = $form->{"861"};
 
1844   $form->{"97r"} = $form->{"971"};
 
1845   $form->{"93r"} = $form->{"931"};
 
1847   #$form->{"96"}  = $form->{"94"} * 0.16;
 
1849     $form->{"51r"} + $form->{"86r"} + $form->{"97r"} + $form->{"93r"} +
 
1851   $form->{"45"} = $form->{"43"};
 
1852   $form->{"53"} = $form->{"43"};
 
1853   $form->{"62"} = $form->{"43"} - $form->{"66"};
 
1854   $form->{"65"} = $form->{"43"} - $form->{"66"};
 
1855   $form->{"67"} = $form->{"43"} - $form->{"66"};
 
1857   foreach my $item (@categories_cent) {
 
1859       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),
 
1863   foreach my $item (@categories_euro) {
 
1865       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 0),
 
1871   $main::lxdebug->leave_sub();
 
1874 sub income_statement {
 
1875   $main::lxdebug->enter_sub();
 
1877   my ($self, $myconfig, $form) = @_;
 
1879   # connect to database
 
1880   my $dbh = $form->dbconnect($myconfig);
 
1882   my $last_period          = 0;
 
1883   my @categories_einnahmen = qw(1 2 3 4 5 6 7);
 
1884   my @categories_ausgaben  =
 
1885     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);
 
1887   my @ergebnisse = qw(sumeura sumeurb guvsumme);
 
1889   $form->{decimalplaces} *= 1;
 
1893   &get_accounts_g($dbh, $last_period, $form->{fromdate}, $form->{todate},
 
1897   foreach my $item (@categories_einnahmen) {
 
1898     $form->{"eur${item}"} =
 
1899       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1900     $form->{"sumeura"} += $form->{$item};
 
1902   foreach my $item (@categories_ausgaben) {
 
1903     $form->{"eur${item}"} =
 
1904       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1905     $form->{"sumeurb"} += $form->{$item};
 
1908   $form->{"guvsumme"} = $form->{"sumeura"} - $form->{"sumeurb"};
 
1910   foreach my $item (@ergebnisse) {
 
1912       $form->format_amount($myconfig, $form->round_amount($form->{$item}, 2),2);
 
1914   $main::lxdebug->leave_sub();