1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  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 # General ledger backend code
 
  34 #   DS. 2000-07-04  Created
 
  35 #   DS. 2001-06-12  Changed relations from accno to chart_id
 
  37 #======================================================================
 
  44 sub delete_transaction {
 
  45   my ($self, $myconfig, $form) = @_;
 
  46   $main::lxdebug->enter_sub();
 
  49   my $dbh = $form->dbconnect_noauto($myconfig);
 
  51   my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
 
  52   $dbh->do($query) || $form->dberror($query);
 
  54   $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
 
  55   $dbh->do($query) || $form->dberror($query);
 
  58   my $rc = $dbh->commit;
 
  60   $main::lxdebug->leave_sub();
 
  66 sub post_transaction {
 
  67   my ($self, $myconfig, $form) = @_;
 
  68   $main::lxdebug->enter_sub();
 
  70   my ($debit, $credit) = (0, 0);
 
  75   # check if debit and credit balances
 
  77   if ($form->{storno}) {
 
  79     $credit              = $credit * -1;
 
  81     $form->{reference}   = "Storno-" . $form->{reference};
 
  82     $form->{description} = "Storno-" . $form->{description};
 
  85   # connect to database, turn off AutoCommit
 
  86   my $dbh = $form->dbconnect_noauto($myconfig);
 
  88   # post the transaction
 
  89   # make up a unique handle and store in reference field
 
  90   # then retrieve the record based on the unique handle to get the id
 
  91   # replace the reference field with the actual variable
 
  92   # add records to acc_trans
 
  94   # if there is a $form->{id} replace the old transaction
 
  95   # delete all acc_trans entries and add the new ones
 
  98   map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
 
 100   if (!$form->{taxincluded}) {
 
 101     $form->{taxincluded} = 0;
 
 108     # delete individual transactions
 
 109     $query = qq|DELETE FROM acc_trans 
 
 110                 WHERE trans_id = $form->{id}|;
 
 111     $dbh->do($query) || $form->dberror($query);
 
 115     $uid .= $form->{login};
 
 117     $query = qq|INSERT INTO gl (reference, employee_id)
 
 118                 VALUES ('$uid', (SELECT e.id FROM employee e
 
 119                                  WHERE e.login = '$form->{login}'))|;
 
 120     $dbh->do($query) || $form->dberror($query);
 
 122     $query = qq|SELECT g.id FROM gl g
 
 123                 WHERE g.reference = '$uid'|;
 
 124     $sth = $dbh->prepare($query);
 
 125     $sth->execute || $form->dberror($query);
 
 127     ($form->{id}) = $sth->fetchrow_array;
 
 132   my ($null, $department_id) = split /--/, $form->{department};
 
 135   $query = qq|UPDATE gl SET 
 
 136               reference = '$form->{reference}',
 
 137               description = '$form->{description}',
 
 138               notes = '$form->{notes}',
 
 139               transdate = '$form->{transdate}',
 
 140               department_id = $department_id,
 
 141               taxincluded = '$form->{taxincluded}'
 
 142               WHERE id = $form->{id}|;
 
 144   $dbh->do($query) || $form->dberror($query);
 
 145   ($taxkey, $rate) = split(/--/, $form->{taxkey});
 
 147   # insert acc_trans transactions
 
 148   for $i (1 .. $form->{rowcount}) {
 
 152     print(STDERR $form->{"taxchart_$i"}, "TAXCHART\n");
 
 153     my ($accno) = split(/--/, $form->{"accno_$i"});
 
 154     my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
 
 155     ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
 
 156     if ($form->{"tax_id_$i"} ne "") {
 
 157       $query = qq|SELECT t.taxkey, t.rate
 
 159               WHERE t.id=$form->{"tax_id_$i"}|;
 
 161       $sth = $dbh->prepare($query);
 
 162       $sth->execute || $form->dberror($query);
 
 164         $sth->fetchrow_array;
 
 169     my $debit  = $form->{"debit_$i"};
 
 170     my $credit = $form->{"credit_$i"};
 
 171     my $tax    = $form->{"tax_$i"};
 
 178       $amount = $debit * -1;
 
 183     $project_id = conv_i($form->{"project_id_$i"});
 
 185     # if there is an amount, add the record
 
 187       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 188                   source, memo, project_id, taxkey)
 
 190                   ($form->{id}, (SELECT c.id
 
 192                                  WHERE c.accno = '$accno'),
 
 193                    $amount, '$form->{transdate}', |
 
 194         . $dbh->quote($form->{"source_$i"}) . qq|, |
 
 195         . $dbh->quote($form->{"memo_$i"}) . qq|,
 
 198       do_query($form, $dbh, $query, $project_id);
 
 203       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 204                   source, memo, project_id, taxkey)
 
 206                   ($form->{id}, (SELECT t.chart_id
 
 208                   WHERE t.id = $form->{"tax_id_$i"}),
 
 209                   $tax, '$form->{transdate}', |
 
 210         . $dbh->quote($form->{"source_$i"}) . qq|, |
 
 211         . $dbh->quote($form->{"memo_$i"}) . qq|, ?, $taxkey)|;
 
 213       do_query($form, $dbh, $query, $project_id);
 
 217   my %audittrail = (tablename => 'gl',
 
 218                     reference => $form->{reference},
 
 219                     formname  => 'transaction',
 
 223   # $form->audittrail($dbh, "", \%audittrail);
 
 225   # commit and redirect
 
 226   my $rc = $dbh->commit;
 
 228   $main::lxdebug->leave_sub();
 
 234 sub all_transactions {
 
 235   my ($self, $myconfig, $form) = @_;
 
 236   $main::lxdebug->enter_sub();
 
 238   # connect to database
 
 239   my $dbh = $form->dbconnect($myconfig);
 
 240   my ($query, $sth, $source, $null);
 
 242   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
 
 244   if ($form->{reference}) {
 
 245     $source = $form->like(lc $form->{reference});
 
 246     $glwhere .= " AND lower(g.reference) LIKE '$source'";
 
 247     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
 
 248     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
 
 250   if ($form->{department}) {
 
 251     ($null, $source) = split /--/, $form->{department};
 
 252     $glwhere .= " AND g.department_id = $source";
 
 253     $arwhere .= " AND a.department_id = $source";
 
 254     $apwhere .= " AND a.department_id = $source";
 
 257   if ($form->{source}) {
 
 258     $source = $form->like(lc $form->{source});
 
 259     $glwhere .= " AND lower(ac.source) LIKE '$source'";
 
 260     $arwhere .= " AND lower(ac.source) LIKE '$source'";
 
 261     $apwhere .= " AND lower(ac.source) LIKE '$source'";
 
 263   if ($form->{datefrom}) {
 
 264     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
 
 265     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
 
 266     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
 
 268   if ($form->{dateto}) {
 
 269     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
 
 270     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
 
 271     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
 
 273   if ($form->{description}) {
 
 274     my $description = $form->like(lc $form->{description});
 
 275     $glwhere .= " AND lower(g.description) LIKE '$description'";
 
 276     $arwhere .= " AND lower(ct.name) LIKE '$description'";
 
 277     $apwhere .= " AND lower(ct.name) LIKE '$description'";
 
 279   if ($form->{notes}) {
 
 280     my $notes = $form->like(lc $form->{notes});
 
 281     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
 
 282     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
 
 283     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
 
 285   if ($form->{accno}) {
 
 286     $glwhere .= " AND c.accno = '$form->{accno}'";
 
 287     $arwhere .= " AND c.accno = '$form->{accno}'";
 
 288     $apwhere .= " AND c.accno = '$form->{accno}'";
 
 290   if ($form->{gifi_accno}) {
 
 291     $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
 
 292     $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
 
 293     $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
 
 295   if ($form->{category} ne 'X') {
 
 297       " AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
 
 299       " AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
 
 301       " AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
 
 303   if ($form->{project_id}) {
 
 304     $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
 
 306       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
 
 307       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
 
 309       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
 
 310       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
 
 313   my ($project_columns, %project_join);
 
 314   if ($form->{"l_projectnumbers"}) {
 
 315     $project_columns = ", ac.project_id, pr.projectnumber";
 
 316     $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
 
 319   if ($form->{accno}) {
 
 321     # get category for account
 
 322     $query = qq|SELECT c.category
 
 324                 WHERE c.accno = '$form->{accno}'|;
 
 325     $sth = $dbh->prepare($query);
 
 327     $sth->execute || $form->dberror($query);
 
 328     ($form->{ml}) = $sth->fetchrow_array;
 
 331     if ($form->{datefrom}) {
 
 332       $query = qq|SELECT SUM(ac.amount)
 
 333                   FROM acc_trans ac, chart c
 
 334                   WHERE ac.chart_id = c.id
 
 335                   AND c.accno = '$form->{accno}'
 
 336                   AND ac.transdate < date '$form->{datefrom}'
 
 338       $sth = $dbh->prepare($query);
 
 339       $sth->execute || $form->dberror($query);
 
 341       ($form->{balance}) = $sth->fetchrow_array;
 
 346   if ($form->{gifi_accno}) {
 
 348     # get category for account
 
 349     $query = qq|SELECT c.category
 
 351                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
 
 352     $sth = $dbh->prepare($query);
 
 354     $sth->execute || $form->dberror($query);
 
 355     ($form->{ml}) = $sth->fetchrow_array;
 
 358     if ($form->{datefrom}) {
 
 359       $query = qq|SELECT SUM(ac.amount)
 
 360                   FROM acc_trans ac, chart c
 
 361                   WHERE ac.chart_id = c.id
 
 362                   AND c.gifi_accno = '$form->{gifi_accno}'
 
 363                   AND ac.transdate < date '$form->{datefrom}'
 
 365       $sth = $dbh->prepare($query);
 
 366       $sth->execute || $form->dberror($query);
 
 368       ($form->{balance}) = $sth->fetchrow_array;
 
 373   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
 
 375   my $sortorder = join ', ',
 
 376     $form->sort_columns(qw(transdate reference source description accno));
 
 377   my %ordinal = (transdate   => 6,
 
 381   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
 
 384     $sortorder = $form->{sort} . ",";
 
 390     qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
 
 391                  g.description, ac.transdate, ac.source, ac.trans_id,
 
 392                  ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
 
 394                  FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
 
 397                  AND ac.chart_id = c.id
 
 398                  AND g.id = ac.trans_id
 
 400                  SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 401                  ct.name, ac.transdate, ac.source, ac.trans_id,
 
 402                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
 
 404                  FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
 
 407                  AND ac.chart_id = c.id
 
 408                  AND a.customer_id = ct.id
 
 409                  AND a.id = ac.trans_id
 
 411                  SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 412                  ct.name, ac.transdate, ac.source, ac.trans_id,
 
 413                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
 
 415                  FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
 
 418                  AND ac.chart_id = c.id
 
 419                  AND a.vendor_id = ct.id
 
 420                  AND a.id = ac.trans_id
 
 421                  ORDER BY $sortorder transdate,acoid, trans_id, taxkey DESC|;
 
 423   # Show all $query in Debuglevel LXDebug::QUERY
 
 424   $callingdetails = (caller (0))[3];
 
 425   $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
 
 427   my $sth = $dbh->prepare($query);
 
 428   $sth->execute || $form->dberror($query);
 
 432   while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
 
 434     $trans_id = $ref0->{id};
 
 436     if ($trans_id != $trans_id2) { # first line of a booking
 
 439         push @{ $form->{GL} }, $ref;
 
 444       $trans_id2 = $ref->{id};
 
 447       if ($ref->{type} eq "gl") {
 
 448         $ref->{module} = "gl";
 
 452       if ($ref->{type} eq "ap") {
 
 453         if ($ref->{invoice}) {
 
 454           $ref->{module} = "ir";
 
 456           $ref->{module} = "ap";
 
 461       if ($ref->{type} eq "ar") {
 
 462         if ($ref->{invoice}) {
 
 463           $ref->{module} = "is";
 
 465           $ref->{module} = "ar";
 
 469       $ref->{"projectnumbers"} = {};
 
 470       $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
 
 472       $balance = $ref->{amount};
 
 474       # Linenumbers of General Ledger  
 
 475       $k       = 0; # Debit      # AP      # Soll
 
 476       $l       = 0; # Credit     # AR      # Haben
 
 477       $i       = 0; # Debit Tax  # AP_tax  # VSt
 
 478       $j       = 0; # Credit Tax # AR_tax  # USt
 
 481       if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
 
 482         if ($ref->{amount} < 0) {
 
 483           if ($ref->{link} =~ /AR_tax/) {
 
 484             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 485             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
 
 487           if ($ref->{link} =~ /AP_tax/) {
 
 488             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 489             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
 
 492           if ($ref->{link} =~ /AR_tax/) {
 
 493             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 494             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
 
 496           if ($ref->{link} =~ /AP_tax/) {
 
 497             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 498             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
 
 501       } else { #all other accounts first line
 
 502         if ($ref->{amount} < 0) {
 
 503           $ref->{debit}{$k}        = $ref->{amount} * -1;
 
 504           $ref->{debit_accno}{$k}  = $ref->{accno};
 
 505           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
 
 506           $ref->{transdate}{$k}    = $ref->{transdate};
 
 509           $ref->{credit}{$l}        = $ref->{amount} * 1;
 
 510           $ref->{credit_accno}{$l}  = $ref->{accno};
 
 511           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
 
 512           $ref->{transdate}{$l}     = $ref->{transdate};
 
 518     } else { # following lines of a booking, line increasing
 
 521       $trans_old  =$trans_id2;
 
 522       $trans_id2 = $ref2->{id};
 
 525         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
 
 527       $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
 
 529       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
 
 530         if ($ref2->{amount} < 0) {
 
 531           if ($ref2->{link} =~ /AR_tax/) {
 
 532             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 535             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 536             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
 
 538           if ($ref2->{link} =~ /AP_tax/) {
 
 539             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 542             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 543             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
 
 546           if ($ref2->{link} =~ /AR_tax/) {
 
 547             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 550             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 551             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
 
 553           if ($ref2->{link} =~ /AP_tax/) {
 
 554             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 557             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 558             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
 
 561       } else { # all other accounts, following lines
 
 562         if ($ref2->{amount} < 0) {
 
 563           if ($ref->{debit_accno}{$k} ne "") {
 
 566           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
 
 567           $ref->{debit_accno}{$k}  = $ref2->{accno};
 
 568           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
 
 569           $ref->{transdate}{$k}    = $ref2->{transdate};
 
 571           if ($ref->{credit_accno}{$l} ne "") {
 
 574           $ref->{credit}{$l}        = $ref2->{amount};
 
 575           $ref->{credit_accno}{$l}  = $ref2->{accno};
 
 576           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
 
 577           $ref->{transdate}{$l}     = $ref2->{transdate};
 
 582   push @{ $form->{GL} }, $ref;
 
 585   if ($form->{accno}) {
 
 587       qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
 
 588     $sth = $dbh->prepare($query);
 
 589     $sth->execute || $form->dberror($query);
 
 591     ($form->{account_description}) = $sth->fetchrow_array;
 
 594   if ($form->{gifi_accno}) {
 
 596       qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
 
 597     $sth = $dbh->prepare($query);
 
 598     $sth->execute || $form->dberror($query);
 
 600     ($form->{gifi_account_description}) = $sth->fetchrow_array;
 
 603   $main::lxdebug->leave_sub();
 
 610   my ($self, $myconfig, $form) = @_;
 
 611   $main::lxdebug->enter_sub();
 
 613   my ($query, $sth, $ref);
 
 615   # connect to database
 
 616   my $dbh = $form->dbconnect($myconfig);
 
 619     $query = "SELECT closedto, revtrans
 
 621     $sth = $dbh->prepare($query);
 
 622     $sth->execute || $form->dberror($query);
 
 624     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
 
 627     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
 
 628               d.description AS department, e.name as employee, g.taxincluded, g.gldate
 
 630             LEFT JOIN department d ON (d.id = g.department_id)  
 
 631             LEFT JOIN employee e ON (e.id = g.employee_id)  
 
 632             WHERE g.id = $form->{id}";
 
 633     $sth = $dbh->prepare($query);
 
 634     $sth->execute || $form->dberror($query);
 
 635     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 636     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 639     # retrieve individual rows
 
 640     $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
 
 641                 a.transdate, a.cleared, a.project_id, p.projectnumber,(SELECT p.projectnumber FROM project p
 
 642                  WHERE a.project_id = p.id) AS projectnumber, a.taxkey, t.rate AS taxrate, t.id, (SELECT c1.accno FROM chart c1, tax t1 WHERE t1.id=t.id AND c1.id=t.chart_id) AS taxaccno, (SELECT tk.tax_id FROM taxkeys tk WHERE tk.chart_id =a.chart_id AND tk.startdate<=a.transdate ORDER BY tk.startdate desc LIMIT 1) AS tax_id
 
 644                 JOIN chart c ON (c.id = a.chart_id)
 
 645                 LEFT JOIN project p ON (p.id = a.project_id)
 
 646                 LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link LIKE '%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1)) 
 
 647                 WHERE a.trans_id = $form->{id}
 
 648                 AND a.fx_transaction = '0'
 
 649                 ORDER BY a.oid,a.transdate|;
 
 651     $sth = $dbh->prepare($query);
 
 652     $sth->execute || $form->dberror($query);
 
 655     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 656       push @{ $form->{GL} }, $ref;
 
 659     # get tax description
 
 660     $query = qq| SELECT * FROM tax t order by t.taxkey|;
 
 661     $sth   = $dbh->prepare($query);
 
 662     $sth->execute || $form->dberror($query);
 
 664     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 665       push @{ $form->{TAX} }, $ref;
 
 670     $query = "SELECT closedto, revtrans FROM defaults";
 
 671     ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
 
 674       "  (SELECT transdate FROM gl WHERE id = " .
 
 675       "    (SELECT MAX(id) FROM gl) LIMIT 1), " .
 
 677     ($form->{transdate}) = $dbh->selectrow_array($query);
 
 679     # get tax description
 
 680     $query = qq| SELECT * FROM tax t order by t.taxkey|;
 
 681     $sth   = $dbh->prepare($query);
 
 682     $sth->execute || $form->dberror($query);
 
 684     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 685       push @{ $form->{TAX} }, $ref;
 
 690   my $transdate = "current_date";
 
 691   if ($form->{transdate}) {
 
 692     $transdate = qq|'$form->{transdate}'|;
 
 694   # get chart of accounts
 
 695   $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
 
 697                 LEFT JOIN taxkeys tk ON (tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1))
 
 699   $sth = $dbh->prepare($query);
 
 700   $sth->execute || $form->dberror($query);
 
 702   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 703     push @{ $form->{chart} }, $ref;
 
 708   $main::lxdebug->leave_sub();