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);
 
  47 sub delete_transaction {
 
  48   my ($self, $myconfig, $form) = @_;
 
  49   $main::lxdebug->enter_sub();
 
  52   my $dbh = $form->dbconnect_noauto($myconfig);
 
  54   # acc_trans entries are deleted by database triggers.
 
  55   do_query($form, $dbh, qq|DELETE FROM gl WHERE id = ?|, conv_i($form->{id}));
 
  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   # connect to database, turn off AutoCommit
 
  76   my $dbh = $form->dbconnect_noauto($myconfig);
 
  78   # post the transaction
 
  79   # make up a unique handle and store in reference field
 
  80   # then retrieve the record based on the unique handle to get the id
 
  81   # replace the reference field with the actual variable
 
  82   # add records to acc_trans
 
  84   # if there is a $form->{id} replace the old transaction
 
  85   # delete all acc_trans entries and add the new ones
 
  87   if (!$form->{taxincluded}) {
 
  88     $form->{taxincluded} = 0;
 
  91   my ($query, $sth, @values, $taxkey, $rate, $posted);
 
  95     # delete individual transactions
 
  96     $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
 
  97     @values = (conv_i($form->{id}));
 
  98     do_query($form, $dbh, $query, @values);
 
 101     $query = qq|SELECT nextval('glid')|;
 
 102     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 105       qq|INSERT INTO gl (id, employee_id) | .
 
 106       qq|VALUES (?, (SELECT id FROM employee WHERE login = ?))|;
 
 107     @values = ($form->{id}, $form->{login});
 
 108     do_query($form, $dbh, $query, @values);
 
 111   my ($null, $department_id) = split(/--/, $form->{department});
 
 113   $form->{ob_transaction} *= 1;
 
 114   $form->{cb_transaction} *= 1;
 
 118          reference = ?, description = ?, notes = ?,
 
 119          transdate = ?, department_id = ?, taxincluded = ?,
 
 120          storno = ?, storno_id = ?, ob_transaction = ?, cb_transaction = ?
 
 123   @values = ($form->{reference}, $form->{description}, $form->{notes},
 
 124              conv_date($form->{transdate}), conv_i($department_id), $form->{taxincluded} ? 't' : 'f',
 
 125              $form->{storno} ? 't' : 'f', conv_i($form->{storno_id}), $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f',
 
 126              conv_i($form->{id}));
 
 127   do_query($form, $dbh, $query, @values);
 
 129   # insert acc_trans transactions
 
 130   for $i (1 .. $form->{rowcount}) {
 
 132     my ($accno) = split(/--/, $form->{"accno_$i"});
 
 133     ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
 
 134     if ($form->{"tax_id_$i"} ne "") {
 
 135       $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
 
 136       ($taxkey, $rate) = selectrow_query($form, $dbh, $query, conv_i($form->{"tax_id_$i"}));
 
 140     my $debit  = $form->{"debit_$i"};
 
 141     my $credit = $form->{"credit_$i"};
 
 142     my $tax    = $form->{"tax_$i"};
 
 149       $amount = $debit * -1;
 
 154     $project_id = conv_i($form->{"project_id_$i"});
 
 156     # if there is an amount, add the record
 
 159         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 160                                   source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id)
 
 161            VALUES (?, (SELECT id FROM chart WHERE accno = ?),
 
 162                    ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
 
 163       @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
 
 164                  $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"}));
 
 165       do_query($form, $dbh, $query, @values);
 
 171         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
 
 172                                   source, memo, project_id, taxkey, tax_id)
 
 173            VALUES (?, (SELECT chart_id FROM tax WHERE id = ?),
 
 174                    ?, ?, ?, ?, ?, ?, ?)|;
 
 175       @values = (conv_i($form->{id}), conv_i($form->{"tax_id_$i"}),
 
 176                  $tax, conv_date($form->{transdate}), $form->{"source_$i"},
 
 177                  $form->{"memo_$i"}, $project_id, $taxkey, conv_i($form->{"tax_id_$i"}));
 
 178       do_query($form, $dbh, $query, @values);
 
 182   if ($form->{storno} && $form->{storno_id}) {
 
 183     do_query($form, $dbh, qq|UPDATE gl SET storno = 't' WHERE id = ?|, conv_i($form->{storno_id}));
 
 186   # safety check datev export
 
 187   if ($::instance_conf->get_datev_check_on_gl_transaction) {
 
 188     my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
 
 189     $transdate  ||= DateTime->today;
 
 191     my $datev = SL::DATEV->new(
 
 192       exporttype => DATEV_ET_BUCHUNGEN,
 
 193       format     => DATEV_FORMAT_KNE,
 
 201     if ($datev->errors) {
 
 203       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
 
 207   # commit and redirect
 
 208   my $rc = $dbh->commit;
 
 210   $main::lxdebug->leave_sub();
 
 215 sub all_transactions {
 
 216   my ($self, $myconfig, $form) = @_;
 
 217   $main::lxdebug->enter_sub();
 
 219   # connect to database
 
 220   my $dbh = $form->dbconnect($myconfig);
 
 221   my ($query, $sth, $source, $null, $space);
 
 223   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
 
 224   my (@glvalues, @arvalues, @apvalues);
 
 226   if ($form->{reference}) {
 
 227     $glwhere .= qq| AND g.reference ILIKE ?|;
 
 228     $arwhere .= qq| AND a.invnumber ILIKE ?|;
 
 229     $apwhere .= qq| AND a.invnumber ILIKE ?|;
 
 230     push(@glvalues, '%' . $form->{reference} . '%');
 
 231     push(@arvalues, '%' . $form->{reference} . '%');
 
 232     push(@apvalues, '%' . $form->{reference} . '%');
 
 235   if ($form->{department}) {
 
 236     my ($null, $department) = split /--/, $form->{department};
 
 237     $glwhere .= qq| AND g.department_id = ?|;
 
 238     $arwhere .= qq| AND a.department_id = ?|;
 
 239     $apwhere .= qq| AND a.department_id = ?|;
 
 240     push(@glvalues, $department);
 
 241     push(@arvalues, $department);
 
 242     push(@apvalues, $department);
 
 245   if ($form->{source}) {
 
 246     $glwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 247     $arwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 248     $apwhere .= " AND ac.trans_id IN (SELECT trans_id from acc_trans WHERE source ILIKE ?)";
 
 249     push(@glvalues, '%' . $form->{source} . '%');
 
 250     push(@arvalues, '%' . $form->{source} . '%');
 
 251     push(@apvalues, '%' . $form->{source} . '%');
 
 254   # default Datumseinschränkung falls nicht oder falsch übergeben (sollte nie passieren)
 
 255   $form->{datesort} = 'transdate' unless $form->{datesort} =~ /^(transdate|gldate)$/;
 
 257   if ($form->{datefrom}) {
 
 258     $glwhere .= " AND ac.$form->{datesort} >= ?";
 
 259     $arwhere .= " AND ac.$form->{datesort} >= ?";
 
 260     $apwhere .= " AND ac.$form->{datesort} >= ?";
 
 261     push(@glvalues, $form->{datefrom});
 
 262     push(@arvalues, $form->{datefrom});
 
 263     push(@apvalues, $form->{datefrom});
 
 266   if ($form->{dateto}) {
 
 267     $glwhere .= " AND ac.$form->{datesort} <= ?";
 
 268     $arwhere .= " AND ac.$form->{datesort} <= ?";
 
 269     $apwhere .= " AND ac.$form->{datesort} <= ?";
 
 270     push(@glvalues, $form->{dateto});
 
 271     push(@arvalues, $form->{dateto});
 
 272     push(@apvalues, $form->{dateto});
 
 275   if ($form->{description}) {
 
 276     $glwhere .= " AND g.description ILIKE ?";
 
 277     $arwhere .= " AND ct.name ILIKE ?";
 
 278     $apwhere .= " AND ct.name ILIKE ?";
 
 279     push(@glvalues, '%' . $form->{description} . '%');
 
 280     push(@arvalues, '%' . $form->{description} . '%');
 
 281     push(@apvalues, '%' . $form->{description} . '%');
 
 284   if ($form->{employee} =~ /--/) {
 
 285     ($form->{employee_id},$form->{employee_name}) = split(/--/,$form->{employee});
 
 286   #if ($form->{employee_id}) {
 
 287     $glwhere .= " AND g.employee_id = ? ";
 
 288     $arwhere .= " AND a.employee_id = ? ";
 
 289     $apwhere .= " AND a.employee_id = ? ";
 
 290     push(@glvalues, conv_i($form->{employee_id}));
 
 291     push(@arvalues, conv_i($form->{employee_id}));
 
 292     push(@apvalues, conv_i($form->{employee_id}));
 
 295   if ($form->{notes}) {
 
 296     $glwhere .= " AND g.notes ILIKE ?";
 
 297     $arwhere .= " AND a.notes ILIKE ?";
 
 298     $apwhere .= " AND a.notes ILIKE ?";
 
 299     push(@glvalues, '%' . $form->{notes} . '%');
 
 300     push(@arvalues, '%' . $form->{notes} . '%');
 
 301     push(@apvalues, '%' . $form->{notes} . '%');
 
 304   if ($form->{accno}) {
 
 305     $glwhere .= " AND c.accno = '$form->{accno}'";
 
 306     $arwhere .= " AND c.accno = '$form->{accno}'";
 
 307     $apwhere .= " AND c.accno = '$form->{accno}'";
 
 310   if ($form->{category} ne 'X') {
 
 311     $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 = ?))|;
 
 312     $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 = ?))|;
 
 313     $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 = ?))|;
 
 314     push(@glvalues, $form->{category});
 
 315     push(@arvalues, $form->{category});
 
 316     push(@apvalues, $form->{category});
 
 319   if ($form->{project_id}) {
 
 320     $glwhere .= qq| AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)|;
 
 322       qq| AND ((a.globalproject_id = ?) OR
 
 323                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 325       qq| AND ((a.globalproject_id = ?) OR
 
 326                (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = ?)))|;
 
 327     my $project_id = conv_i($form->{project_id});
 
 328     push(@glvalues, $project_id);
 
 329     push(@arvalues, $project_id, $project_id);
 
 330     push(@apvalues, $project_id, $project_id);
 
 333   my ($project_columns, $project_join);
 
 334   if ($form->{"l_projectnumbers"}) {
 
 335     $project_columns = qq|, ac.project_id, pr.projectnumber|;
 
 336     $project_join = qq|LEFT JOIN project pr ON (ac.project_id = pr.id)|;
 
 339   if ($form->{accno}) {
 
 340     # get category for account
 
 341     $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 342     ($form->{ml}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 344     if ($form->{datefrom}) {
 
 346         qq|SELECT SUM(ac.amount)
 
 348            LEFT JOIN chart c ON (ac.chart_id = c.id)
 
 349            WHERE (c.accno = ?) AND (ac.$form->{datesort} < ?)|;
 
 350       ($form->{balance}) = selectrow_query($form, $dbh, $query, $form->{accno}, conv_date($form->{datefrom}));
 
 354   my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
 
 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)
 
 676               WHERE (tk.taxkey_id = a.taxkey) AND
 
 677                 ((CASE WHEN a.chart_id IN
 
 678                     (SELECT chart_id FROM taxkeys WHERE taxkey_id = a.taxkey)
 
 679                   THEN tk.chart_id = a.chart_id
 
 682                  OR (c.link LIKE '%tax%'))
 
 683                 AND (startdate <= a.transdate)
 
 684               ORDER BY startdate DESC LIMIT 1))
 
 685          WHERE (a.trans_id = ?)
 
 686            AND (a.fx_transaction = '0')
 
 687          ORDER BY a.acc_trans_id, a.transdate|;
 
 688     $form->{GL} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 695             WHERE id = (SELECT MAX(id) FROM gl)
 
 698     ($form->{transdate}) = selectrow_query($form, $dbh, $query);
 
 701   # get tax description
 
 702   $query = qq|SELECT * FROM tax ORDER BY taxkey|;
 
 703   $form->{TAX} = selectall_hashref_query($form, $dbh, $query);
 
 705   # get chart of accounts
 
 707     qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
 
 709        LEFT JOIN taxkeys tk ON (tk.id =
 
 712           WHERE (taxkeys.chart_id = c.id)
 
 714           ORDER BY startdate DESC
 
 717   $form->{chart} = selectall_hashref_query($form, $dbh, $query, conv_date($form->{transdate}));
 
 721   $main::lxdebug->leave_sub();
 
 725   $main::lxdebug->enter_sub();
 
 727   my ($self, $form, $myconfig, $id) = @_;
 
 729   my ($query, $new_id, $storno_row, $acc_trans_rows);
 
 730   my $dbh = $form->get_standard_dbh($myconfig);
 
 732   $query = qq|SELECT nextval('glid')|;
 
 733   ($new_id) = selectrow_query($form, $dbh, $query);
 
 735   $query = qq|SELECT * FROM gl WHERE id = ?|;
 
 736   $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
 
 738   $storno_row->{id}        = $new_id;
 
 739   $storno_row->{storno_id} = $id;
 
 740   $storno_row->{storno}    = 't';
 
 741   $storno_row->{reference} = 'Storno-' . $storno_row->{reference};
 
 743   delete @$storno_row{qw(itime mtime gldate)};
 
 745   $query = sprintf 'INSERT INTO gl (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
 
 746   do_query($form, $dbh, $query, (values %$storno_row));
 
 748   $query = qq|UPDATE gl SET storno = 't' WHERE id = ?|;
 
 749   do_query($form, $dbh, $query, $id);
 
 751   # now copy acc_trans entries
 
 752   $query = qq|SELECT * FROM acc_trans WHERE trans_id = ?|;
 
 753   my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
 
 755   for my $row (@$rowref) {
 
 756     delete @$row{qw(itime mtime acc_trans_id gldate)};
 
 757     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
 
 758     $row->{trans_id}   = $new_id;
 
 759     $row->{amount}    *= -1;
 
 760     do_query($form, $dbh, $query, (values %$row));
 
 765   $main::lxdebug->leave_sub();
 
 768 sub get_chart_balances {
 
 769   $main::lxdebug->enter_sub();
 
 774   Common::check_params(\%params, qw(charts));
 
 776   my $myconfig = \%main::myconfig;
 
 777   my $form     = $main::form;
 
 779   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 781   my @ids      = map { $_->{id} } @{ $params{charts} };
 
 784     $main::lxdebug->leave_sub();
 
 788   my $query = qq|SELECT chart_id, SUM(amount) AS sum
 
 790                  WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
 
 793   my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
 
 795   foreach my $chart (@{ $params{charts} }) {
 
 796     $chart->{balance} = $balances{ $chart->{id} } || 0;
 
 799   $main::lxdebug->leave_sub();