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 #======================================================================
 
  42 use SL::DATEV qw(:CONSTANTS);
 
  44 use SL::Util qw(trim);
 
  48 sub delete_transaction {
 
  49   my ($self, $myconfig, $form) = @_;
 
  50   $main::lxdebug->enter_sub();
 
  53   my $dbh = $form->dbconnect_noauto($myconfig);
 
  55   # acc_trans entries are deleted by database triggers.
 
  56   do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, conv_i($form->{id}));
 
  59   my $rc = $dbh->commit;
 
  61   $main::lxdebug->leave_sub();
 
  67 sub post_transaction {
 
  68   my ($self, $myconfig, $form) = @_;
 
  69   $main::lxdebug->enter_sub();
 
  71   my ($debit, $credit) = (0, 0);
 
  76   # connect to database, turn off AutoCommit
 
  77   my $dbh = $form->dbconnect_noauto($myconfig);
 
  79   # post the transaction
 
  80   # make up a unique handle and store in reference field
 
  81   # then retrieve the record based on the unique handle to get the id
 
  82   # replace the reference field with the actual variable
 
  83   # add records to acc_trans
 
  85   # if there is a $form->{id} replace the old transaction
 
  86   # delete all acc_trans entries and add the new ones
 
  88   if (!$form->{taxincluded}) {
 
  89     $form->{taxincluded} = 0;
 
  92   my ($query, $sth, @values, $taxkey, $rate, $posted);
 
  96     # delete individual transactions
 
  97     $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
 
  98     @values = (conv_i($form->{id}));
 
  99     do_query($form, $dbh, $query, @values);
 
 102     $query = qq|SELECT nextval('glid')|;
 
 103     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 106       qq|INSERT INTO gl (id, employee_id) | .
 
 107       qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
 
 108     @values = ($form->{id}, $::myconfig{login});
 
 109     do_query($form, $dbh, $query, @values);
 
 112   my ($null, $department_id) = split(/--/, $form->{department});
 
 114   $form->{ob_transaction} *= 1;
 
 115   $form->{cb_transaction} *= 1;
 
 119          reference = ?, description = ?, notes = ?,
 
 120          transdate = ?, department_id = ?, taxincluded = ?,
 
 121          storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
 
 124   @values = ($form->{reference}, $form->{description}, $form->{notes},
 
 125              conv_date($form->{transdate}), conv_i($department_id), $form->{taxincluded} ? 't' : 'f',
 
 126              $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
 
 127              conv_i($form->{id}));
 
 128   do_query($form, $dbh, $query, @values);
 
 130   # insert acc_trans transactions
 
 131   for $i (1 .. $form->{rowcount}) {
 
 133     my ($accno) = split(/--/, $form->{"accno_$i"});
 
 134     ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
 
 135     if ($form->{"tax_id_$i"} ne "") {
 
 136       $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
 
 137       ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
 
 141     my $debit  = $form->{"debit_$i"};
 
 142     my $credit = $form->{"credit_$i"};
 
 143     my $tax    = $form->{"tax_$i"};
 
 150       $amount = $debit * -1;
 
 155     $project_id = conv_i($form->{"project_id_$i"});
 
 157     # if there is an amount, add the record
 
 160         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 161                                   source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id, chart_link)
 
 162            VALUES (?, (SELECT id FROM chart WHERE accno = ?),
 
 163                    ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE accno = ?))|;
 
 164       @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
 
 165                  $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}), $accno);
 
 166       do_query($form, $dbh, $query, @values);
 
 172         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 173                                   source, memo, project_id, taxkey, tax_id, chart_link)
 
 174            VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
 
 175                    ?, ?, ?, ?, ?, ?, ?, (SELECT link
 
 177                                          WHERE id = (SELECT chart_id
 
 180       @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
 
 181                  $tax, conv_date($form->{transdate}), $form->{"source_$i"},
 
 182                  $form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}), conv_i($form->{"tax_id_$i"}));
 
 183       do_query($form, $dbh, $query, @values);
 
 187   if ($form->{storno} && $form->{storno_id}) {
 
 188     do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
 
 191   # safety check datev export
 
 192   if ($::instance_conf->get_datev_check_on_gl_transaction) {
 
 193     my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
 
 194     $transdate  ||= DateTime->today;
 
 196     my $datev = SL::DATEV->new(
 
 197       exporttype => DATEV_ET_BUCHUNGEN,
 
 198       format     => DATEV_FORMAT_KNE,
 
 200       trans_id   => $form->{id},
 
 205     if ($datev->errors) {
 
 207       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
 
 211   # commit and redirect
 
 212   my $rc = $dbh->commit;
 
 214   $main::lxdebug->leave_sub();
 
 219 sub all_transactions {
 
 220   my ($self, $myconfig, $form) = @_;
 
 221   $main::lxdebug->enter_sub();
 
 223   # connect to database
 
 224   my $dbh = $form->dbconnect($myconfig);
 
 225   my ($query, $sth, $source, $null, $space);
 
 227   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
 
 228   my (@glvalues, @arvalues, @apvalues);
 
 230   if ($form->{reference}) {
 
 231     $glwhere .= qq| AND g.reference ILIKE ?|;
 
 232     $arwhere .= qq| AND a.invnumber ILIKE ?|;
 
 233     $apwhere .= qq| AND a.invnumber ILIKE ?|;
 
 234     push(@glvalues, like($form->{reference}));
 
 235     push(@arvalues, like($form->{reference}));
 
 236     push(@apvalues, like($form->{reference}));
 
 239   if ($form->{department}) {
 
 240     my ($null, $department) = split /--/, $form->{department};
 
 241     $glwhere .= qq| AND g.department_id = ?|;
 
 242     $arwhere .= qq| AND a.department_id = ?|;
 
 243     $apwhere .= qq| AND a.department_id = ?|;
 
 244     push(@glvalues, $department);
 
 245     push(@arvalues, $department);
 
 246     push(@apvalues, $department);
 
 249   if ($form->{source}) {
 
 250     $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 251     $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 252     $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 253     push(@glvalues, like($form->{source}));
 
 254     push(@arvalues, like($form->{source}));
 
 255     push(@apvalues, like($form->{source}));
 
 258   # default Datumseinschränkung falls nicht oder falsch übergeben (sollte nie passieren)
 
 259   $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
 
 261   if (trim($form->{datefrom})) {
 
 262     $glwhere .= " AND ac.$form->{datesort} >= ?";
 
 263     $arwhere .= " AND ac.$form->{datesort} >= ?";
 
 264     $apwhere .= " AND ac.$form->{datesort} >= ?";
 
 265     push(@glvalues, trim($form->{datefrom}));
 
 266     push(@arvalues, trim($form->{datefrom}));
 
 267     push(@apvalues, trim($form->{datefrom}));
 
 270   if (trim($form->{dateto})) {
 
 271     $glwhere .= " AND ac.$form->{datesort} <= ?";
 
 272     $arwhere .= " AND ac.$form->{datesort} <= ?";
 
 273     $apwhere .= " AND ac.$form->{datesort} <= ?";
 
 274     push(@glvalues, trim($form->{dateto}));
 
 275     push(@arvalues, trim($form->{dateto}));
 
 276     push(@apvalues, trim($form->{dateto}));
 
 279   if (trim($form->{description})) {
 
 280     $glwhere .= " AND g.description ILIKE ?";
 
 281     $arwhere .= " AND ct.name ILIKE ?";
 
 282     $apwhere .= " AND ct.name ILIKE ?";
 
 283     push(@glvalues, like($form->{description}));
 
 284     push(@arvalues, like($form->{description}));
 
 285     push(@apvalues, like($form->{description}));
 
 288   if ($form->{employee_id}) {
 
 289     $glwhere .= " AND g.employee_id = ? ";
 
 290     $arwhere .= " AND a.employee_id = ? ";
 
 291     $apwhere .= " AND a.employee_id = ? ";
 
 292     push(@glvalues, conv_i($form->{employee_id}));
 
 293     push(@arvalues, conv_i($form->{employee_id}));
 
 294     push(@apvalues, conv_i($form->{employee_id}));
 
 297   if (trim($form->{notes})) {
 
 298     $glwhere .= " AND g.notes ILIKE ?";
 
 299     $arwhere .= " AND a.notes ILIKE ?";
 
 300     $apwhere .= " AND a.notes ILIKE ?";
 
 301     push(@glvalues, like($form->{notes}));
 
 302     push(@arvalues, like($form->{notes}));
 
 303     push(@apvalues, like($form->{notes}));
 
 306   if ($form->{accno}) {
 
 307     $glwhere .= " AND c.accno = '$form->{accno}'";
 
 308     $arwhere .= " AND c.accno = '$form->{accno}'";
 
 309     $apwhere .= " AND c.accno = '$form->{accno}'";
 
 312   if ($form->{category} ne 'X') {
 
 313     $glwhere .= qq| AND g.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
 
 314     $arwhere .= qq| AND a.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
 
 315     $apwhere .= qq| AND a.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = ?))|;
 
 316     push(@glvalues, $form->{category});
 
 317     push(@arvalues, $form->{category});
 
 318     push(@apvalues, $form->{category});
 
 321   if ($form->{project_id}) {
 
 322     $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
 
 324       qq| AND ((a.globalproject_id = ?) OR
 
 325                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 327       qq| AND ((a.globalproject_id = ?) OR
 
 328                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 329     my $project_id = conv_i($form->{project_id});
 
 330     push(@glvalues, $project_id);
 
 331     push(@arvalues, $project_id, $project_id);
 
 332     push(@apvalues, $project_id, $project_id);
 
 335   my ($project_columns, $project_join);
 
 336   if ($form->{"l_projectnumbers"}) {
 
 337     $project_columns = qq|, ac.project_id, pr.projectnumber|;
 
 338     $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
 
 341   if ($form->{accno}) {
 
 342     # get category for account
 
 343     $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 344     ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 346     if ($form->{datefrom}) {
 
 348         qq|SELECT SUM(ac.amount)
 
 350            LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 351            WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
 
 352       ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
 
 358     'transdate'    => [ qw(transdate id)         ],
 
 359     'gldate'       => [ qw(gldate id)         ],
 
 360     'reference'    => [ qw(lower_reference id)   ],
 
 361     'description'  => [ qw(lower_description id) ],
 
 362     'accno'        => [ qw(accno transdate id)   ],
 
 364   my %lowered_columns =  (
 
 365     'reference'       => { 'gl' => 'g.reference',   'arap' => 'a.invnumber', },
 
 366     'source'          => { 'gl' => 'ac.source',     'arap' => 'ac.source',   },
 
 367     'description'     => { 'gl' => 'g.description', 'arap' => 'ct.name',     },
 
 370   # sortdir = sort direction (ascending or descending)
 
 371   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 372   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort};  # default used to be transdate
 
 373   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
 375   my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
 
 376   foreach my $spec (@{ $sort_columns{$sortkey} }) {
 
 377     next if ($spec !~ m/^lower_(.*)$/);
 
 380     map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
 
 385         ac.acc_trans_id, g.id, 'gl' AS type, FALSE AS invoice, g.reference, ac.taxkey, c.link,
 
 386         g.description, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 387         ac.amount, c.accno, g.notes, t.chart_id,
 
 388         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 390         $columns_for_sorting{gl}
 
 392       LEFT JOIN employee e ON (g.employee_id = e.id),
 
 393       acc_trans ac $project_join, chart c
 
 394       LEFT JOIN tax t ON (t.chart_id = c.id)
 
 396         AND (ac.chart_id = c.id)
 
 397         AND (g.id = ac.trans_id)
 
 401       SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 402         ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 403         ac.amount, c.accno, a.notes, t.chart_id,
 
 404         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 406         $columns_for_sorting{arap}
 
 408       LEFT JOIN employee e ON (a.employee_id = e.id),
 
 409       acc_trans ac $project_join, customer ct, chart c
 
 410       LEFT JOIN tax t ON (t.chart_id=c.id)
 
 412         AND (ac.chart_id = c.id)
 
 413         AND (a.customer_id = ct.id)
 
 414         AND (a.id = ac.trans_id)
 
 418       SELECT ac.acc_trans_id, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 419         ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 420         ac.amount, c.accno, a.notes, t.chart_id,
 
 421         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 423         $columns_for_sorting{arap}
 
 425       LEFT JOIN employee e ON (a.employee_id = e.id),
 
 426       acc_trans ac $project_join, vendor ct, chart c
 
 427       LEFT JOIN tax t ON (t.chart_id=c.id)
 
 429         AND (ac.chart_id = c.id)
 
 430         AND (a.vendor_id = ct.id)
 
 431         AND (a.id = ac.trans_id)
 
 433       ORDER BY $sortorder, acc_trans_id $sortdir|;
 
 434 #      ORDER BY gldate DESC, id DESC, acc_trans_id DESC
 
 436   my @values = (@glvalues, @arvalues, @apvalues);
 
 438   # Show all $query in Debuglevel LXDebug::QUERY
 
 439   my $callingdetails = (caller (0))[3];
 
 440   dump_query(LXDebug->QUERY(), "$callingdetails", $query, @values);
 
 442   $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 447   my ($i, $j, $k, $l, $ref, $ref2);
 
 450   while (my $ref0 = $sth->fetchrow_hashref("NAME_lc")) {
 
 452     $trans_id = $ref0->{id};
 
 454     my $source = $ref0->{source};
 
 455     undef($ref0->{source});
 
 457     if ($trans_id != $trans_id2) { # first line of a booking
 
 460         push(@{ $form->{GL} }, $ref);
 
 465       $trans_id2 = $ref->{id};
 
 468       if ($ref->{type} eq "gl") {
 
 469         $ref->{module} = "gl";
 
 473       if ($ref->{type} eq "ap") {
 
 474         if ($ref->{invoice}) {
 
 475           $ref->{module} = "ir";
 
 477           $ref->{module} = "ap";
 
 482       if ($ref->{type} eq "ar") {
 
 483         if ($ref->{invoice}) {
 
 484           $ref->{module} = "is";
 
 486           $ref->{module} = "ar";
 
 490       $ref->{"projectnumbers"} = {};
 
 491       $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
 
 493       $balance = $ref->{amount};
 
 495       # Linenumbers of General Ledger
 
 496       $k       = 0; # Debit      # AP      # Soll
 
 497       $l       = 0; # Credit     # AR      # Haben
 
 498       $i       = 0; # Debit Tax  # AP_tax  # VSt
 
 499       $j       = 0; # Credit Tax # AR_tax  # USt
 
 501       if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
 
 502         if ($ref->{amount} < 0) {
 
 503           if ($ref->{link} =~ /AR_tax/) {
 
 504             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 505             $ref->{credit_tax_accno}{$j} = $ref->{accno};
 
 507           if ($ref->{link} =~ /AP_tax/) {
 
 508             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 509             $ref->{debit_tax_accno}{$i} = $ref->{accno};
 
 512           if ($ref->{link} =~ /AR_tax/) {
 
 513             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 514             $ref->{credit_tax_accno}{$j} = $ref->{accno};
 
 516           if ($ref->{link} =~ /AP_tax/) {
 
 517             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 518             $ref->{debit_tax_accno}{$i} = $ref->{accno};
 
 521       } else { #all other accounts first line
 
 523         if ($ref->{amount} < 0) {
 
 524           $ref->{debit}{$k}        = $ref->{amount} * -1;
 
 525           $ref->{debit_accno}{$k}  = $ref->{accno};
 
 526           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
 
 527           $ref->{ac_transdate}{$k} = $ref->{transdate};
 
 528           $ref->{source}{$k}       = $source;
 
 530           $ref->{credit}{$l}        = $ref->{amount} * 1;
 
 531           $ref->{credit_accno}{$l}  = $ref->{accno};
 
 532           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
 
 533           $ref->{ac_transdate}{$l}  = $ref->{transdate};
 
 534           $ref->{source}{$l}        = $source;
 
 538     } else { # following lines of a booking, line increasing
 
 541 #      $trans_old = $trans_id2;   # doesn't seem to be used anymore
 
 542       $trans_id2 = $ref2->{id};
 
 545         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
 
 547       $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
 
 549       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
 
 550         if ($ref2->{amount} < 0) {
 
 551           if ($ref2->{link} =~ /AR_tax/) {
 
 552             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 555             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 556             $ref->{credit_tax_accno}{$j} = $ref2->{accno};
 
 558           if ($ref2->{link} =~ /AP_tax/) {
 
 559             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 562             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 563             $ref->{debit_tax_accno}{$i} = $ref2->{accno};
 
 566           if ($ref2->{link} =~ /AR_tax/) {
 
 567             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 570             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 571             $ref->{credit_tax_accno}{$j} = $ref2->{accno};
 
 573           if ($ref2->{link} =~ /AP_tax/) {
 
 574             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 577             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 578             $ref->{debit_tax_accno}{$i} = $ref2->{accno};
 
 581       } else { # all other accounts, following lines
 
 582         if ($ref2->{amount} < 0) {
 
 583           if ($ref->{debit_accno}{$k} ne "") {
 
 586           if ($ref->{source}{$k} ne "") {
 
 591           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
 
 592           $ref->{debit_accno}{$k}  = $ref2->{accno};
 
 593           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
 
 594           $ref->{ac_transdate}{$k} = $ref2->{transdate};
 
 595           $ref->{source}{$k}       = $source . $space . $ref->{source}{$k};
 
 597           if ($ref->{credit_accno}{$l} ne "") {
 
 600           if ($ref->{source}{$l} ne "") {
 
 605           $ref->{credit}{$l}        = $ref2->{amount};
 
 606           $ref->{credit_accno}{$l}  = $ref2->{accno};
 
 607           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
 
 608           $ref->{ac_transdate}{$l}  = $ref2->{transdate};
 
 609           $ref->{source}{$l}        = $ref->{source}{$l} . $space . $source;
 
 615   push @{ $form->{GL} }, $ref;
 
 618   if ($form->{accno}) {
 
 619     $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
 
 620     ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 625   $main::lxdebug->leave_sub();
 
 629   my ($self, $myconfig, $form) = @_;
 
 630   $main::lxdebug->enter_sub();
 
 632   my ($query, $sth, $ref, @values);
 
 634   # connect to database
 
 635   my $dbh = $form->dbconnect($myconfig);
 
 637   $query = qq|SELECT closedto, revtrans FROM defaults|;
 
 638   ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
 
 640   $query = qq|SELECT id, gldate
 
 642               WHERE id = (SELECT max(id) FROM gl)|;
 
 643   ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
 
 647       qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
 
 648            d.description AS department, e.name AS employee, g.taxincluded, g.gldate,
 
 649          g.ob_transaction, g.cb_transaction
 
 651          LEFT JOIN department d ON (d.id = g.department_id)
 
 652          LEFT JOIN employee e ON (e.id = g.employee_id)
 
 654     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 655     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 657     # retrieve individual rows
 
 659       qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
 
 660            a.transdate, a.cleared, a.project_id, p.projectnumber,
 
 661            a.taxkey, t.rate AS taxrate, t.id,
 
 663             FROM chart c1, tax t1
 
 664             WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
 
 667             WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
 
 668             ORDER BY tk.startdate desc LIMIT 1) AS tax_id
 
 670          JOIN chart c ON (c.id = a.chart_id)
 
 671          LEFT JOIN project p ON (p.id = a.project_id)
 
 672          LEFT JOIN tax t ON (t.id = a.tax_id)
 
 673          WHERE (a.trans_id = ?)
 
 674            AND (a.fx_transaction = '0')
 
 675          ORDER BY a.acc_trans_id, a.transdate|;
 
 676     $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 683             WHERE id = (SELECT MAX(id) FROM gl)
 
 686     ($form->{transdate}) = selectrow_query($form, $dbh, $query);
 
 689   # get tax description
 
 690   $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
 691   $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
 
 693   # get chart of accounts
 
 695     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
 
 697        LEFT JOIN taxkeys tk ON (tk.id =
 
 700           WHERE (taxkeys.chart_id = c.id)
 
 702           ORDER BY startdate DESC
 
 705   $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
 
 709   $main::lxdebug->leave_sub();
 
 713   $main::lxdebug->enter_sub();
 
 715   my ($self, $form, $myconfig, $id) = @_;
 
 717   my ($query, $new_id, $storno_row, $acc_trans_rows);
 
 718   my $dbh = $form->get_standard_dbh($myconfig);
 
 720   $query = qq|SELECT nextval('glid')|;
 
 721   ($new_id) = selectrow_query($form, $dbh, $query);
 
 723   $query = qq|SELECT * FROM gl WHERE id = ?|;
 
 724   $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
 
 726   $storno_row->{id}          = $new_id;
 
 727   $storno_row->{storno_id}   = $id;
 
 728   $storno_row->{storno}      = 't';
 
 729   $storno_row->{reference}   = 'Storno-' . $storno_row->{reference};
 
 731   $query = qq|SELECT id FROM employee WHERE login = ?|;
 
 732   my ($employee_id) = selectrow_query($form, $dbh, $query, $::myconfig{login});
 
 733   $storno_row->{employee_id} = $employee_id;
 
 735   delete @$storno_row{qw(itime mtime gldate)};
 
 737   $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
 
 738   do_query($form, $dbh, $query, (values %$storno_row));
 
 740   $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
 
 741   do_query($form, $dbh, $query, $id);
 
 743   # now copy acc_trans entries
 
 744   $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
 
 745   my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
 
 747   for my $row (@$rowref) {
 
 748     delete @$row{qw(itime mtime acc_trans_id gldate)};
 
 749     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
 
 750     $row->{trans_id}   = $new_id;
 
 751     $row->{amount}    *= -1;
 
 752     do_query($form, $dbh, $query, (values %$row));
 
 757   $main::lxdebug->leave_sub();
 
 760 sub get_chart_balances {
 
 761   $main::lxdebug->enter_sub();
 
 766   Common::check_params(\%params, qw(charts));
 
 768   my $myconfig = \%main::myconfig;
 
 769   my $form     = $main::form;
 
 771   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 773   my @ids      = map { $_->{id} } @{ $params{charts} };
 
 776     $main::lxdebug->leave_sub();
 
 780   my $query = qq|SELECT chart_id, SUM(amount) AS sum
 
 782                  WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
 
 785   my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
 
 787   foreach my $chart (@{ $params{charts} }) {
 
 788     $chart->{balance} = $balances{ $chart->{id} } || 0;
 
 791   $main::lxdebug->leave_sub();
 
 794 sub get_tax_dropdown {
 
 795   my ($self, $accno) = @_;
 
 797   my $myconfig = \%main::myconfig;
 
 798   my $form = $main::form;
 
 800   my $dbh = $form->get_standard_dbh($myconfig);
 
 802   my $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 803   my ($category) = selectrow_query($form, $dbh, $query, $accno);
 
 805   $query = qq|SELECT * FROM tax WHERE chart_categories like '%$category%' order by taxkey, rate|;
 
 807   my $sth = prepare_execute_query($form, $dbh, $query);
 
 809   my @tax_accounts = ();
 
 810   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 811     push(@tax_accounts, $ref);
 
 814   return @tax_accounts;