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., 51 Franklin Street, Fifth Floor, Boston,
 
  30 #======================================================================
 
  32 # General ledger backend code
 
  35 #   DS. 2000-07-04  Created
 
  36 #   DS. 2001-06-12  Changed relations from accno to chart_id
 
  38 #======================================================================
 
  42 use List::Util qw(first);
 
  45 use SL::DATEV qw(:CONSTANTS);
 
  49 use SL::Util qw(trim);
 
  54 sub delete_transaction {
 
  55   my ($self, $myconfig, $form) = @_;
 
  56   $main::lxdebug->enter_sub();
 
  58   SL::DB->client->with_transaction(sub {
 
  59     do_query($form, SL::DB->client->dbh, qq|DELETE FROM gl WHERE id = ?|, conv_i($form->{id}));
 
  61   }) or do { die SL::DB->client->error };
 
  63   $main::lxdebug->leave_sub();
 
  66 sub post_transaction {
 
  67   my ($self, $myconfig, $form) = @_;
 
  68   $main::lxdebug->enter_sub();
 
  70   my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form);
 
  72   $::lxdebug->leave_sub;
 
  76 sub _post_transaction {
 
  77   my ($self, $myconfig, $form) = @_;
 
  78   $main::lxdebug->enter_sub();
 
  80   my ($debit, $credit) = (0, 0);
 
  85   my $dbh = SL::DB->client->dbh;
 
  87   # post the transaction
 
  88   # make up a unique handle and store in reference field
 
  89   # then retrieve the record based on the unique handle to get the id
 
  90   # replace the reference field with the actual variable
 
  91   # add records to acc_trans
 
  93   # if there is a $form->{id} replace the old transaction
 
  94   # delete all acc_trans entries and add the new ones
 
  96   if (!$form->{taxincluded}) {
 
  97     $form->{taxincluded} = 0;
 
 100   my ($query, $sth, @values, $taxkey, $rate, $posted);
 
 104     # delete individual transactions
 
 105     $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
 
 106     @values = (conv_i($form->{id}));
 
 107     do_query($form, $dbh, $query, @values);
 
 110     $query = qq|SELECT nextval('glid')|;
 
 111     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 114       qq|INSERT INTO gl (id, employee_id) | .
 
 115       qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
 
 116     @values = ($form->{id}, $::myconfig{login});
 
 117     do_query($form, $dbh, $query, @values);
 
 120   $form->{ob_transaction} *= 1;
 
 121   $form->{cb_transaction} *= 1;
 
 125          reference = ?, description = ?, notes = ?,
 
 126          transdate = ?, department_id = ?, taxincluded = ?,
 
 127          storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
 
 130   @values = ($form->{reference}, $form->{description}, $form->{notes},
 
 131              conv_date($form->{transdate}), conv_i($form->{department_id}), $form->{taxincluded} ? 't' : 'f',
 
 132              $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
 
 133              conv_i($form->{id}));
 
 134   do_query($form, $dbh, $query, @values);
 
 136   # insert acc_trans transactions
 
 137   for $i (1 .. $form->{rowcount}) {
 
 138     ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
 
 139     if ($form->{"tax_id_$i"} ne "") {
 
 140       $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
 
 141       ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
 
 145     my $debit  = $form->{"debit_$i"};
 
 146     my $credit = $form->{"credit_$i"};
 
 147     my $tax    = $form->{"tax_$i"};
 
 154       $amount = $debit * -1;
 
 159     $project_id = conv_i($form->{"project_id_$i"});
 
 161     # if there is an amount, add the record
 
 164         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 165                                   source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id, chart_link)
 
 166            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE id = ?))|;
 
 167       @values = (conv_i($form->{id}), $form->{"accno_id_$i"}, $amount, conv_date($form->{transdate}),
 
 168                  $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"}), $form->{"accno_id_$i"});
 
 169       do_query($form, $dbh, $query, @values);
 
 175         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 176                                   source, memo, project_id, taxkey, tax_id, chart_link)
 
 177            VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
 
 178                    ?, ?, ?, ?, ?, ?, ?, (SELECT link
 
 180                                          WHERE id = (SELECT chart_id
 
 183       @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
 
 184                  $tax, conv_date($form->{transdate}), $form->{"source_$i"},
 
 185                  $form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}), conv_i($form->{"tax_id_$i"}));
 
 186       do_query($form, $dbh, $query, @values);
 
 190   if ($form->{storno} && $form->{storno_id}) {
 
 191     do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
 
 194   if ($form->{draft_id}) {
 
 195     SL::DB::Manager::Draft->delete_all(where => [ id => delete($form->{draft_id}) ]);
 
 198   # safety check datev export
 
 199   if ($::instance_conf->get_datev_check_on_gl_transaction) {
 
 200     my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
 
 201     $transdate  ||= DateTime->today;
 
 203     my $datev = SL::DATEV->new(
 
 204       exporttype => DATEV_ET_BUCHUNGEN,
 
 205       format     => DATEV_FORMAT_KNE,
 
 207       trans_id   => $form->{id},
 
 212     if ($datev->errors) {
 
 213       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
 
 220 sub all_transactions {
 
 221   my ($self, $myconfig, $form) = @_;
 
 222   $main::lxdebug->enter_sub();
 
 224   my $dbh = SL::DB->client->dbh;
 
 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_id}) {
 
 240     $glwhere .= qq| AND g.department_id = ?|;
 
 241     $arwhere .= qq| AND a.department_id = ?|;
 
 242     $apwhere .= qq| AND a.department_id = ?|;
 
 243     push(@glvalues, $form->{department_id});
 
 244     push(@arvalues, $form->{department_id});
 
 245     push(@apvalues, $form->{department_id});
 
 248   if ($form->{source}) {
 
 249     $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 250     $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 251     $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 252     push(@glvalues, like($form->{source}));
 
 253     push(@arvalues, like($form->{source}));
 
 254     push(@apvalues, like($form->{source}));
 
 257   # default Datumseinschränkung falls nicht oder falsch übergeben (sollte nie passieren)
 
 258   $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
 
 260   if (trim($form->{datefrom})) {
 
 261     $glwhere .= " AND ac.$form->{datesort} >= ?";
 
 262     $arwhere .= " AND ac.$form->{datesort} >= ?";
 
 263     $apwhere .= " AND ac.$form->{datesort} >= ?";
 
 264     push(@glvalues, trim($form->{datefrom}));
 
 265     push(@arvalues, trim($form->{datefrom}));
 
 266     push(@apvalues, trim($form->{datefrom}));
 
 269   if (trim($form->{dateto})) {
 
 270     $glwhere .= " AND ac.$form->{datesort} <= ?";
 
 271     $arwhere .= " AND ac.$form->{datesort} <= ?";
 
 272     $apwhere .= " AND ac.$form->{datesort} <= ?";
 
 273     push(@glvalues, trim($form->{dateto}));
 
 274     push(@arvalues, trim($form->{dateto}));
 
 275     push(@apvalues, trim($form->{dateto}));
 
 278   if (trim($form->{description})) {
 
 279     $glwhere .= " AND g.description ILIKE ?";
 
 280     $arwhere .= " AND ct.name ILIKE ?";
 
 281     $apwhere .= " AND ct.name ILIKE ?";
 
 282     push(@glvalues, like($form->{description}));
 
 283     push(@arvalues, like($form->{description}));
 
 284     push(@apvalues, like($form->{description}));
 
 287   if ($form->{employee_id}) {
 
 288     $glwhere .= " AND g.employee_id = ? ";
 
 289     $arwhere .= " AND a.employee_id = ? ";
 
 290     $apwhere .= " AND a.employee_id = ? ";
 
 291     push(@glvalues, conv_i($form->{employee_id}));
 
 292     push(@arvalues, conv_i($form->{employee_id}));
 
 293     push(@apvalues, conv_i($form->{employee_id}));
 
 296   if (trim($form->{notes})) {
 
 297     $glwhere .= " AND g.notes ILIKE ?";
 
 298     $arwhere .= " AND a.notes ILIKE ?";
 
 299     $apwhere .= " AND a.notes ILIKE ?";
 
 300     push(@glvalues, like($form->{notes}));
 
 301     push(@arvalues, like($form->{notes}));
 
 302     push(@apvalues, like($form->{notes}));
 
 305   if ($form->{accno}) {
 
 306     $glwhere .= " AND c.accno = '$form->{accno}'";
 
 307     $arwhere .= " AND c.accno = '$form->{accno}'";
 
 308     $apwhere .= " AND c.accno = '$form->{accno}'";
 
 311   if ($form->{category} ne 'X') {
 
 312     $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 = ?))|;
 
 313     $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 = ?))|;
 
 314     $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 = ?))|;
 
 315     push(@glvalues, $form->{category});
 
 316     push(@arvalues, $form->{category});
 
 317     push(@apvalues, $form->{category});
 
 320   if ($form->{project_id}) {
 
 321     $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
 
 323       qq| AND ((a.globalproject_id = ?) OR
 
 324                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 326       qq| AND ((a.globalproject_id = ?) OR
 
 327                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 328     my $project_id = conv_i($form->{project_id});
 
 329     push(@glvalues, $project_id);
 
 330     push(@arvalues, $project_id, $project_id);
 
 331     push(@apvalues, $project_id, $project_id);
 
 334   my ($project_columns, $project_join);
 
 335   if ($form->{"l_projectnumbers"}) {
 
 336     $project_columns = qq|, ac.project_id, pr.projectnumber|;
 
 337     $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
 
 340   if ($form->{accno}) {
 
 341     # get category for account
 
 342     $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 343     ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 345     if ($form->{datefrom}) {
 
 347         qq|SELECT SUM(ac.amount)
 
 349            LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 350            WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
 
 351       ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
 
 357     'transdate'    => [ qw(transdate id)         ],
 
 358     'gldate'       => [ qw(gldate id)         ],
 
 359     'reference'    => [ qw(lower_reference id)   ],
 
 360     'description'  => [ qw(lower_description id) ],
 
 361     'accno'        => [ qw(accno transdate id)   ],
 
 363   my %lowered_columns =  (
 
 364     'reference'       => { 'gl' => 'g.reference',   'arap' => 'a.invnumber', },
 
 365     'source'          => { 'gl' => 'ac.source',     'arap' => 'ac.source',   },
 
 366     'description'     => { 'gl' => 'g.description', 'arap' => 'ct.name',     },
 
 369   # sortdir = sort direction (ascending or descending)
 
 370   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 371   my $sortkey   = $sort_columns{$form->{sort}} ? $form->{sort} : $form->{datesort};  # default used to be transdate
 
 372   my $sortorder = join ', ', map { "$_ $sortdir" } @{ $sort_columns{$sortkey} };
 
 374   my %columns_for_sorting = ( 'gl' => '', 'arap' => '', );
 
 375   foreach my $spec (@{ $sort_columns{$sortkey} }) {
 
 376     next if ($spec !~ m/^lower_(.*)$/);
 
 379     map { $columns_for_sorting{$_} .= sprintf(', lower(%s) AS lower_%s', $lowered_columns{$column}->{$_}, $column) } qw(gl arap);
 
 384         ac.acc_trans_id, g.id, 'gl' AS type, FALSE AS invoice, g.reference, ac.taxkey, c.link,
 
 385         g.description, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 386         ac.amount, c.accno, g.notes, t.chart_id,
 
 387         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 389         $columns_for_sorting{gl}
 
 391       LEFT JOIN employee e ON (g.employee_id = e.id),
 
 392       acc_trans ac $project_join, chart c
 
 393       LEFT JOIN tax t ON (t.chart_id = c.id)
 
 395         AND (ac.chart_id = c.id)
 
 396         AND (g.id = ac.trans_id)
 
 400       SELECT ac.acc_trans_id, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 401         ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 402         ac.amount, c.accno, a.notes, t.chart_id,
 
 403         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 405         $columns_for_sorting{arap}
 
 407       LEFT JOIN employee e ON (a.employee_id = e.id),
 
 408       acc_trans ac $project_join, customer ct, chart c
 
 409       LEFT JOIN tax t ON (t.chart_id=c.id)
 
 411         AND (ac.chart_id = c.id)
 
 412         AND (a.customer_id = ct.id)
 
 413         AND (a.id = ac.trans_id)
 
 417       SELECT ac.acc_trans_id, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
 
 418         ct.name, ac.transdate, ac.gldate, ac.source, ac.trans_id,
 
 419         ac.amount, c.accno, a.notes, t.chart_id,
 
 420         CASE WHEN (COALESCE(e.name, '') = '') THEN e.login ELSE e.name END AS employee
 
 422         $columns_for_sorting{arap}
 
 424       LEFT JOIN employee e ON (a.employee_id = e.id),
 
 425       acc_trans ac $project_join, vendor ct, chart c
 
 426       LEFT JOIN tax t ON (t.chart_id=c.id)
 
 428         AND (ac.chart_id = c.id)
 
 429         AND (a.vendor_id = ct.id)
 
 430         AND (a.id = ac.trans_id)
 
 432       ORDER BY $sortorder, acc_trans_id $sortdir|;
 
 433 #      ORDER BY gldate DESC, id DESC, acc_trans_id DESC
 
 435   my @values = (@glvalues, @arvalues, @apvalues);
 
 437   # Show all $query in Debuglevel LXDebug::QUERY
 
 438   my $callingdetails = (caller (0))[3];
 
 439   dump_query(LXDebug->QUERY(), "$callingdetails", $query, @values);
 
 441   $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 446   my ($i, $j, $k, $l, $ref, $ref2);
 
 449   while (my $ref0 = $sth->fetchrow_hashref("NAME_lc")) {
 
 451     $trans_id = $ref0->{id};
 
 453     my $source = $ref0->{source};
 
 454     undef($ref0->{source});
 
 456     if ($trans_id != $trans_id2) { # first line of a booking
 
 459         push(@{ $form->{GL} }, $ref);
 
 464       $trans_id2 = $ref->{id};
 
 467       if ($ref->{type} eq "gl") {
 
 468         $ref->{module} = "gl";
 
 472       if ($ref->{type} eq "ap") {
 
 473         if ($ref->{invoice}) {
 
 474           $ref->{module} = "ir";
 
 476           $ref->{module} = "ap";
 
 481       if ($ref->{type} eq "ar") {
 
 482         if ($ref->{invoice}) {
 
 483           $ref->{module} = "is";
 
 485           $ref->{module} = "ar";
 
 489       $ref->{"projectnumbers"} = {};
 
 490       $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
 
 492       $balance = $ref->{amount};
 
 494       # Linenumbers of General Ledger
 
 495       $k       = 0; # Debit      # AP      # Soll
 
 496       $l       = 0; # Credit     # AR      # Haben
 
 497       $i       = 0; # Debit Tax  # AP_tax  # VSt
 
 498       $j       = 0; # Credit Tax # AR_tax  # USt
 
 500       if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
 
 501         if ($ref->{amount} < 0) {
 
 502           if ($ref->{link} =~ /AR_tax/) {
 
 503             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 504             $ref->{credit_tax_accno}{$j} = $ref->{accno};
 
 506           if ($ref->{link} =~ /AP_tax/) {
 
 507             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 508             $ref->{debit_tax_accno}{$i} = $ref->{accno};
 
 511           if ($ref->{link} =~ /AR_tax/) {
 
 512             $ref->{credit_tax}{$j}       = $ref->{amount};
 
 513             $ref->{credit_tax_accno}{$j} = $ref->{accno};
 
 515           if ($ref->{link} =~ /AP_tax/) {
 
 516             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
 
 517             $ref->{debit_tax_accno}{$i} = $ref->{accno};
 
 520       } else { #all other accounts first line
 
 522         if ($ref->{amount} < 0) {
 
 523           $ref->{debit}{$k}        = $ref->{amount} * -1;
 
 524           $ref->{debit_accno}{$k}  = $ref->{accno};
 
 525           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
 
 526           $ref->{ac_transdate}{$k} = $ref->{transdate};
 
 527           $ref->{source}{$k}       = $source;
 
 529           $ref->{credit}{$l}        = $ref->{amount} * 1;
 
 530           $ref->{credit_accno}{$l}  = $ref->{accno};
 
 531           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
 
 532           $ref->{ac_transdate}{$l}  = $ref->{transdate};
 
 533           $ref->{source}{$l}        = $source;
 
 537     } else { # following lines of a booking, line increasing
 
 540 #      $trans_old = $trans_id2;   # doesn't seem to be used anymore
 
 541       $trans_id2 = $ref2->{id};
 
 544         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
 
 546       $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
 
 548       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
 
 549         if ($ref2->{amount} < 0) {
 
 550           if ($ref2->{link} =~ /AR_tax/) {
 
 551             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 554             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 555             $ref->{credit_tax_accno}{$j} = $ref2->{accno};
 
 557           if ($ref2->{link} =~ /AP_tax/) {
 
 558             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 561             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 562             $ref->{debit_tax_accno}{$i} = $ref2->{accno};
 
 565           if ($ref2->{link} =~ /AR_tax/) {
 
 566             if ($ref->{credit_tax_accno}{$j} ne "") {
 
 569             $ref->{credit_tax}{$j}       = $ref2->{amount};
 
 570             $ref->{credit_tax_accno}{$j} = $ref2->{accno};
 
 572           if ($ref2->{link} =~ /AP_tax/) {
 
 573             if ($ref->{debit_tax_accno}{$i} ne "") {
 
 576             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
 
 577             $ref->{debit_tax_accno}{$i} = $ref2->{accno};
 
 580       } else { # all other accounts, following lines
 
 581         if ($ref2->{amount} < 0) {
 
 582           if ($ref->{debit_accno}{$k} ne "") {
 
 585           if ($ref->{source}{$k} ne "") {
 
 590           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
 
 591           $ref->{debit_accno}{$k}  = $ref2->{accno};
 
 592           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
 
 593           $ref->{ac_transdate}{$k} = $ref2->{transdate};
 
 594           $ref->{source}{$k}       = $source . $space . $ref->{source}{$k};
 
 596           if ($ref->{credit_accno}{$l} ne "") {
 
 599           if ($ref->{source}{$l} ne "") {
 
 604           $ref->{credit}{$l}        = $ref2->{amount};
 
 605           $ref->{credit_accno}{$l}  = $ref2->{accno};
 
 606           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
 
 607           $ref->{ac_transdate}{$l}  = $ref2->{transdate};
 
 608           $ref->{source}{$l}        = $ref->{source}{$l} . $space . $source;
 
 614   push @{ $form->{GL} }, $ref;
 
 617   if ($form->{accno}) {
 
 618     $query = qq|SELECT c.description FROM chart c WHERE c.accno = ?|;
 
 619     ($form->{account_description}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 622   $main::lxdebug->leave_sub();
 
 626   my ($self, $myconfig, $form) = @_;
 
 627   $main::lxdebug->enter_sub();
 
 629   my ($query, $sth, $ref, @values);
 
 631   my $dbh = SL::DB->client->dbh;
 
 633   $query = qq|SELECT closedto, revtrans FROM defaults|;
 
 634   ($form->{closedto}, $form->{revtrans}) = selectrow_query($form, $dbh, $query);
 
 636   $query = qq|SELECT id, gldate
 
 638               WHERE id = (SELECT max(id) FROM gl)|;
 
 639   ($form->{previous_id}, $form->{previous_gldate}) = selectrow_query($form, $dbh, $query);
 
 643       qq|SELECT g.reference, g.description, g.notes, g.transdate, g.storno, g.storno_id,
 
 644            g.department_id, d.description AS department,
 
 645            e.name AS employee, g.taxincluded, g.gldate,
 
 646          g.ob_transaction, g.cb_transaction
 
 648          LEFT JOIN department d ON (d.id = g.department_id)
 
 649          LEFT JOIN employee e ON (e.id = g.employee_id)
 
 651     $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 652     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 654     # retrieve individual rows
 
 656       qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
 
 657            a.transdate, a.cleared, a.project_id, p.projectnumber,
 
 658            a.taxkey, t.rate AS taxrate, t.id, a.chart_id,
 
 660             FROM chart c1, tax t1
 
 661             WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
 
 664             WHERE (tk.chart_id = a.chart_id) AND (tk.startdate <= a.transdate)
 
 665             ORDER BY tk.startdate desc LIMIT 1) AS tax_id
 
 667          JOIN chart c ON (c.id = a.chart_id)
 
 668          LEFT JOIN project p ON (p.id = a.project_id)
 
 669          LEFT JOIN tax t ON (t.id = a.tax_id)
 
 670          WHERE (a.trans_id = ?)
 
 671            AND (a.fx_transaction = '0')
 
 672          ORDER BY a.acc_trans_id, a.transdate|;
 
 673     $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 680             WHERE id = (SELECT MAX(id) FROM gl)
 
 683     ($form->{transdate}) = selectrow_query($form, $dbh, $query);
 
 686   # get tax description
 
 687   $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
 688   $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
 
 690   # get chart of accounts
 
 692     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
 
 694        LEFT JOIN taxkeys tk ON (tk.id =
 
 697           WHERE (taxkeys.chart_id = c.id)
 
 699           ORDER BY startdate DESC
 
 702   $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
 
 704   $main::lxdebug->leave_sub();
 
 708   my ($self, $form, $myconfig, $id) = @_;
 
 709   $main::lxdebug->enter_sub();
 
 711   my $rc = SL::DB->client->with_transaction(\&_storno, $self, $form, $myconfig, $id);
 
 713   $::lxdebug->leave_sub;
 
 718   my ($self, $form, $myconfig, $id) = @_;
 
 720   my ($query, $new_id, $storno_row, $acc_trans_rows);
 
 721   my $dbh = SL::DB->client->dbh;
 
 723   $query = qq|SELECT nextval('glid')|;
 
 724   ($new_id) = selectrow_query($form, $dbh, $query);
 
 726   $query = qq|SELECT * FROM gl WHERE id = ?|;
 
 727   $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
 
 729   $storno_row->{id}          = $new_id;
 
 730   $storno_row->{storno_id}   = $id;
 
 731   $storno_row->{storno}      = 't';
 
 732   $storno_row->{reference}   = 'Storno-' . $storno_row->{reference};
 
 734   $query = qq|SELECT id FROM employee WHERE login = ?|;
 
 735   my ($employee_id) = selectrow_query($form, $dbh, $query, $::myconfig{login});
 
 736   $storno_row->{employee_id} = $employee_id;
 
 738   delete @$storno_row{qw(itime mtime gldate)};
 
 740   $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
 
 741   do_query($form, $dbh, $query, (values %$storno_row));
 
 743   $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
 
 744   do_query($form, $dbh, $query, $id);
 
 746   # now copy acc_trans entries
 
 747   $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
 
 748   my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
 
 750   for my $row (@$rowref) {
 
 751     delete @$row{qw(itime mtime acc_trans_id gldate)};
 
 752     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
 
 753     $row->{trans_id}   = $new_id;
 
 754     $row->{amount}    *= -1;
 
 755     do_query($form, $dbh, $query, (values %$row));
 
 761 sub get_chart_balances {
 
 762   my ($self, @chart_ids) = @_;
 
 764   return () unless @chart_ids;
 
 766   my $placeholders = join ', ', ('?') x scalar(@chart_ids);
 
 767   my $query = qq|SELECT chart_id, SUM(amount) AS sum
 
 769                  WHERE chart_id IN (${placeholders})
 
 772   my %balances = selectall_as_map($::form, $::form->get_standard_dbh(\%::myconfig), $query, 'chart_id', 'sum', @chart_ids);
 
 777 sub get_active_taxes_for_chart {
 
 778   my ($self, $chart_id, $transdate) = @_;
 
 780   my $chart         = SL::DB::Chart->new(id => $chart_id)->load;
 
 781   my $active_taxkey = $chart->get_active_taxkey($transdate);
 
 782   my $taxes         = SL::DB::Manager::Tax->get_all(
 
 783     where   => [ chart_categories => { like => '%' . $chart->category . '%' }],
 
 784     sort_by => 'taxkey, rate',
 
 787   my $default_tax            = first { $active_taxkey->tax_id == $_->id } @{ $taxes };
 
 788   $default_tax->{is_default} = 1 if $default_tax;