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 # Accounts Payables database backend routines
 
  33 #======================================================================
 
  45 sub post_transaction {
 
  46   $main::lxdebug->enter_sub();
 
  48   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
 
  49   my $rc = 0; # return code auf false setzen
 
  51   my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
 
  53   my ($null, $taxrate, $amount);
 
  56   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
 
  57   delete $form->{currency} unless $form->{defaultcurrency};
 
  59   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
  60   $form->{department_id} *= 1;
 
  62   if ($form->{currency} eq $form->{defaultcurrency}) {
 
  63     $form->{exchangerate} = 1;
 
  65     $exchangerate         = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
 
  66     $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
 
  69   for my $i (1 .. $form->{rowcount}) {
 
  70     $form->{AP_amounts}{"amount_$i"} =
 
  71       (split(/--/, $form->{"AP_amount_$i"}))[0];
 
  73   ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
 
  74   ($form->{AP_payables})          = split(/--/, $form->{APselected});
 
  76   # reverse and parse amounts
 
  77   for my $i (1 .. $form->{rowcount}) {
 
  78     $form->{"amount_$i"} =
 
  80                          $form->parse_amount($myconfig, $form->{"amount_$i"}) *
 
  81                            $form->{exchangerate} * -1,
 
  83     $amount += ($form->{"amount_$i"} * -1);
 
  85     # parse tax_$i for later
 
  86     $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"}) * -1;
 
  90   $form->{amount} = $amount;
 
  92   # taxincluded doesn't make sense if there is no amount
 
  93   $form->{taxincluded} = 0 if ($form->{amount} == 0);
 
  95   for my $i (1 .. $form->{rowcount}) {
 
  96     ($form->{"tax_id_$i"}, undef) = split /--/, $form->{"taxchart_$i"};
 
  99       qq|SELECT c.accno, t.taxkey, t.rate | .
 
 100       qq|FROM tax t LEFT JOIN chart c on (c.id=t.chart_id) | .
 
 101       qq|WHERE t.id = ? | .
 
 102       qq|ORDER BY c.accno|;
 
 103     my $sth = $dbh->prepare($query);
 
 104     $sth->execute($form->{"tax_id_$i"}) || $form->dberror($query . " (" . $form->{"tax_id_$i"} . ")");
 
 105     ($form->{AP_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = $sth->fetchrow_array();
 
 110     if ($form->{taxincluded} *= 1) {
 
 111       $tax = $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
 
 112       $amount = $form->{"amount_$i"} - $tax;
 
 113       $form->{"amount_$i"} = $form->round_amount($amount, 2);
 
 114       $diff += $amount - $form->{"amount_$i"};
 
 115       $form->{"tax_$i"} = $form->round_amount($tax, 2);
 
 116       $form->{netamount} += $form->{"amount_$i"};
 
 118       $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
 
 119       $form->{netamount} += $form->{"amount_$i"};
 
 121     $form->{total_tax} += $form->{"tax_$i"} * -1;
 
 124   # adjust paidaccounts if there is no date in the last row
 
 125   $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
 
 127   $form->{invpaid} = 0;
 
 128   $form->{netamount} *= -1;
 
 131   for my $i (1 .. $form->{paidaccounts}) {
 
 133       $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
 
 136     $form->{invpaid} += $form->{"paid_$i"};
 
 137     $form->{datepaid} = $form->{"datepaid_$i"};
 
 142     $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
 
 144   # store invoice total, this goes into ap table
 
 145   $form->{invtotal} = $form->{netamount} + $form->{total_tax};
 
 147   # amount for total AP
 
 148   $form->{payables} = $form->{invtotal};
 
 150   # update exchangerate
 
 151   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 152     $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
 
 153                                $form->{exchangerate});
 
 156   my ($query, $sth, @values);
 
 158   if (!$payments_only) {
 
 159     # if we have an id delete old records
 
 162       # delete detail records
 
 163       $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
 
 164       do_query($form, $dbh, $query, $form->{id});
 
 167       my $uid = rand() . time;
 
 169       $uid .= $form->{login};
 
 171       $uid = substr($uid, 2, 75);
 
 174         qq|INSERT INTO ap (invnumber, employee_id) | .
 
 175         qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?))|;
 
 176       do_query($form, $dbh, $query, $uid, $form->{login});
 
 178       $query = qq|SELECT a.id FROM ap a
 
 179                   WHERE a.invnumber = ?|;
 
 180       ($form->{id}) = selectrow_query($form, $dbh, $query, $uid);
 
 183     $form->{invnumber} = $form->{id} unless $form->{invnumber};
 
 185     $query = qq|UPDATE ap SET
 
 186                 invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
 
 187                 amount = ?, duedate = ?, paid = ?, netamount = ?,
 
 188                 curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?,
 
 191     @values = ($form->{invnumber}, conv_date($form->{transdate}),
 
 192                   $form->{ordnumber}, conv_i($form->{vendor_id}),
 
 193                   $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
 
 194                   conv_date($form->{duedate}), $form->{invpaid},
 
 196                   $form->{currency}, $form->{notes},
 
 197                   conv_i($form->{department_id}), $form->{storno},
 
 198                   $form->{storno_id}, conv_i($form->{globalproject_id}),
 
 200     do_query($form, $dbh, $query, @values);
 
 202     # add individual transactions
 
 203     for my $i (1 .. $form->{rowcount}) {
 
 204       if ($form->{"amount_$i"} != 0) {
 
 206         $project_id = conv_i($form->{"project_id_$i"});
 
 208         # insert detail records in acc_trans
 
 210           qq|INSERT INTO acc_trans | .
 
 211           qq|  (trans_id, chart_id, amount, transdate, project_id, taxkey)| .
 
 212           qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
 
 214         @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
 
 215                    $form->{"amount_$i"}, conv_date($form->{transdate}),
 
 216                    $project_id, $form->{"taxkey_$i"});
 
 217         do_query($form, $dbh, $query, @values);
 
 219         if ($form->{"tax_$i"} != 0) {
 
 220           # insert detail records in acc_trans
 
 222             qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
 
 223             qq|  project_id, taxkey) | .
 
 224             qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
 
 226           @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
 
 227                      $form->{"tax_$i"}, conv_date($form->{transdate}),
 
 228                      $project_id, $form->{"taxkey_$i"});
 
 229           do_query($form, $dbh, $query, @values);
 
 237       qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
 
 238       qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
 
 239       qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
 
 240     @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
 
 241                conv_date($form->{transdate}), $form->{AP_amounts}{payables});
 
 242     do_query($form, $dbh, $query, @values);
 
 245   # if there is no amount but a payment record a payable
 
 246   if ($form->{amount} == 0 && $form->{invtotal} == 0) {
 
 247     $form->{payables} = $form->{invpaid};
 
 250   # add paid transactions
 
 251   for my $i (1 .. $form->{paidaccounts}) {
 
 253     if ($form->{"acc_trans_id_$i"} && $payments_only && ($::lx_office_conf{features}->{payments_changeable} == 0)) {
 
 257     if ($form->{"paid_$i"} != 0) {
 
 258       my $project_id = conv_i($form->{"paid_project_id_$i"});
 
 261       if ($form->{currency} eq $form->{defaultcurrency}) {
 
 262         $form->{"exchangerate_$i"} = 1;
 
 264         $exchangerate              = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
 
 265         $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
 
 267       $form->{"AP_paid_$i"} =~ s/\"//g;
 
 271       ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
 
 272       $form->{"datepaid_$i"} = $form->{transdate}
 
 273         unless ($form->{"datepaid_$i"});
 
 275       # if there is no amount and invtotal is zero there is no exchangerate
 
 276       if ($form->{amount} == 0 && $form->{invtotal} == 0) {
 
 277         $form->{exchangerate} = $form->{"exchangerate_$i"};
 
 281         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
 
 283       if ($form->{payables}) {
 
 285           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
 
 286           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
 
 287           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
 
 288         @values = ($form->{id}, $form->{AP_payables}, $amount,
 
 289                    conv_date($form->{"datepaid_$i"}), $project_id,
 
 290                    $form->{AP_payables});
 
 291         do_query($form, $dbh, $query, @values);
 
 293       $form->{payables} = $amount;
 
 296       my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
 
 298         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey) | .
 
 299         qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, | .
 
 300         qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
 
 301       @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
 
 302                  conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"},
 
 303                  $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"});
 
 304       do_query($form, $dbh, $query, @values);
 
 306       # add exchange rate difference
 
 308         $form->round_amount($form->{"paid_$i"} *
 
 309                             ($form->{"exchangerate_$i"} - 1), 2);
 
 312           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
 
 313           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
 
 314           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
 
 315         @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
 
 316                    conv_date($form->{"datepaid_$i"}), $project_id,
 
 317                    $form->{"AP_paid_account_$i"});
 
 318         do_query($form, $dbh, $query, @values);
 
 321       # exchangerate gain/loss
 
 323         $form->round_amount($form->{"paid_$i"} *
 
 324                             ($form->{exchangerate} -
 
 325                              $form->{"exchangerate_$i"}), 2);
 
 329           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
 
 330           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
 
 331           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
 
 332         @values = ($form->{id}, ($amount > 0) ?
 
 333                    $form->{fxgain_accno} : $form->{fxloss_accno},
 
 334                    $amount, conv_date($form->{"datepaid_$i"}), $project_id,
 
 336                    $form->{fxgain_accno} : $form->{fxloss_accno});
 
 337         do_query($form, $dbh, $query, @values);
 
 340       # update exchange rate record
 
 341       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
 
 342         $form->update_exchangerate($dbh, $form->{currency},
 
 343                                    $form->{"datepaid_$i"},
 
 344                                    0, $form->{"exchangerate_$i"});
 
 349   if ($payments_only) {
 
 350     $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
 
 351     do_query($form, $dbh, $query,  $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
 
 354   IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
 
 356   if (!$provided_dbh) {
 
 361   $rc = 1; #  Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
 
 363   $main::lxdebug->leave_sub();
 
 368 sub delete_transaction {
 
 369   $main::lxdebug->enter_sub();
 
 371   my ($self, $myconfig, $form) = @_;
 
 373   # connect to database
 
 374   my $dbh = $form->dbconnect_noauto($myconfig);
 
 376   my $query = qq|DELETE FROM ap WHERE id = ?|;
 
 377   do_query($form, $dbh, $query, $form->{id});
 
 379   $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
 
 380   do_query($form, $dbh, $query, $form->{id});
 
 382   # commit and redirect
 
 383   my $rc = $dbh->commit;
 
 386   $main::lxdebug->leave_sub();
 
 391 sub ap_transactions {
 
 392   $main::lxdebug->enter_sub();
 
 394   my ($self, $myconfig, $form) = @_;
 
 396   # connect to database
 
 397   my $dbh = $form->get_standard_dbh($myconfig);
 
 400     qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
 
 401     qq|  a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
 
 402     qq|  a.globalproject_id, a.storno, a.storno_id, | .
 
 403     qq|  pr.projectnumber AS globalprojectnumber, | .
 
 404     qq|  e.name AS employee, | .
 
 405     qq|  v.vendornumber, v.country, v.ustid, | .
 
 406     qq|  tz.description AS taxzone, | .
 
 407     qq|  pt.description AS payment_terms, | .
 
 408     qq{  ( SELECT ch.accno || ' -- ' || ch.description
 
 410            LEFT JOIN chart ch ON ch.id = at.chart_id
 
 411            WHERE ch.link ~ 'AP[[:>:]]'
 
 412             AND at.trans_id = a.id
 
 416     qq|JOIN vendor v ON (a.vendor_id = v.id) | .
 
 417     qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
 
 418     qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
 
 419     qq|LEFT JOIN tax_zones tz ON (tz.id = v.taxzone_id)| .
 
 420     qq|LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)|;
 
 425   if ($form->{vendor_id}) {
 
 426     $where .= " AND a.vendor_id = ?";
 
 427     push(@values, $form->{vendor_id});
 
 428   } elsif ($form->{vendor}) {
 
 429     $where .= " AND v.name ILIKE ?";
 
 430     push(@values, $form->like($form->{vendor}));
 
 432   if ($form->{department}) {
 
 433     # Ã¤hnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
 
 434     # wird so nicht mehr als zeichenkette zusammengebaut
 
 435     # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
 
 436     #my ($null, $department_id) = split /--/, $form->{department};
 
 437     $where .= " AND a.department_id = ?";
 
 438     push(@values, $form->{department});
 
 440   if ($form->{invnumber}) {
 
 441     $where .= " AND a.invnumber ILIKE ?";
 
 442     push(@values, $form->like($form->{invnumber}));
 
 444   if ($form->{ordnumber}) {
 
 445     $where .= " AND a.ordnumber ILIKE ?";
 
 446     push(@values, $form->like($form->{ordnumber}));
 
 448   if ($form->{notes}) {
 
 449     $where .= " AND lower(a.notes) LIKE ?";
 
 450     push(@values, $form->like($form->{notes}));
 
 452   if ($form->{project_id}) {
 
 454       qq|AND ((a.globalproject_id = ?) OR EXISTS | .
 
 455       qq|  (SELECT * FROM invoice i | .
 
 456       qq|   WHERE i.project_id = ? AND i.trans_id = a.id) | .
 
 458       qq|  (SELECT * FROM acc_trans at | .
 
 459       qq|   WHERE at.project_id = ? AND at.trans_id = a.id)| .
 
 461     push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id});
 
 464   if ($form->{transdatefrom}) {
 
 465     $where .= " AND a.transdate >= ?";
 
 466     push(@values, $form->{transdatefrom});
 
 468   if ($form->{transdateto}) {
 
 469     $where .= " AND a.transdate <= ?";
 
 470     push(@values, $form->{transdateto});
 
 472   if ($form->{open} || $form->{closed}) {
 
 473     unless ($form->{open} && $form->{closed}) {
 
 474       $where .= " AND a.amount <> a.paid" if ($form->{open});
 
 475       $where .= " AND a.amount = a.paid"  if ($form->{closed});
 
 480     substr($where, 0, 4, " WHERE ");
 
 484   my @a = qw(transdate invnumber name);
 
 485   push @a, "employee" if $form->{l_employee};
 
 486   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
 
 487   my $sortorder = join(', ', map { "$_ $sortdir" } @a);
 
 489   if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
 
 490     $sortorder = $form->{sort} . " $sortdir";
 
 493   $query .= " ORDER BY $sortorder";
 
 495   my @result = selectall_hashref_query($form, $dbh, $query, @values);
 
 497   $form->{AP} = [ @result ];
 
 499   $main::lxdebug->leave_sub();
 
 503   $main::lxdebug->enter_sub();
 
 505   my ($self, $myconfig, $form) = @_;
 
 507   # connect to database
 
 508   my $dbh = $form->dbconnect($myconfig);
 
 512     "  (SELECT transdate FROM ap WHERE id = " .
 
 513     "    (SELECT MAX(id) FROM ap) LIMIT 1), " .
 
 515   ($form->{transdate}) = $dbh->selectrow_array($query);
 
 519   $main::lxdebug->leave_sub();
 
 522 sub _delete_payments {
 
 523   $main::lxdebug->enter_sub();
 
 525   my ($self, $form, $dbh) = @_;
 
 527   my @delete_acc_trans_ids;
 
 529   # Delete old payment entries from acc_trans.
 
 531     qq|SELECT acc_trans_id
 
 533        WHERE (trans_id = ?) AND fx_transaction
 
 537        SELECT at.acc_trans_id
 
 539        LEFT JOIN chart c ON (at.chart_id = c.id)
 
 540        WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
 
 541   push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
 
 544     qq|SELECT at.acc_trans_id
 
 546        LEFT JOIN chart c ON (at.chart_id = c.id)
 
 548          AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
 
 549        ORDER BY at.acc_trans_id
 
 551   push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
 
 553   if (@delete_acc_trans_ids) {
 
 554     $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
 
 555     do_query($form, $dbh, $query);
 
 558   $main::lxdebug->leave_sub();
 
 562   $main::lxdebug->enter_sub();
 
 564   my ($self, $myconfig, $form, $locale) = @_;
 
 566   # connect to database, turn off autocommit
 
 567   my $dbh = $form->dbconnect_noauto($myconfig);
 
 569   my (%payments, $old_form, $row, $item, $query, %keep_vars);
 
 571   $old_form = save_form();
 
 573   # Delete all entries in acc_trans from prior payments.
 
 574   if ($::lx_office_conf{features}->{payments_changeable} != 0) {
 
 575     $self->_delete_payments($form, $dbh);
 
 578   # Save the new payments the user made before cleaning up $form.
 
 579   my $payments_re = '^datepaid_\d+$|^gldate_\d+$|^acc_trans_id_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AP_paid_\d+$|^paidaccounts$';
 
 580   map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
 
 582   # Clean up $form so that old content won't tamper the results.
 
 583   %keep_vars = map { $_, 1 } qw(login password id);
 
 584   map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
 
 586   # Retrieve the invoice from the database.
 
 587   $form->create_links('AP', $myconfig, 'vendor', $dbh);
 
 589   # Restore the payment options from the user input.
 
 590   map { $form->{$_} = $payments{$_} } keys %payments;
 
 592   # Set up the content of $form in the way that AR::post_transaction() expects.
 
 594   $self->setup_form($form, 1);
 
 596   $form->{exchangerate}    = $form->format_amount($myconfig, $form->{exchangerate});
 
 597   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
 
 598   delete $form->{currency} unless $form->{defaultcurrency};
 
 604        LEFT JOIN chart c ON (at.chart_id = c.id)
 
 606          AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
 
 607        ORDER BY at.acc_trans_id
 
 610   ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
 
 612   # Post the new payments.
 
 613   $self->post_transaction($myconfig, $form, $dbh, 1);
 
 615   restore_form($old_form);
 
 617   my $rc = $dbh->commit();
 
 620   $main::lxdebug->leave_sub();
 
 626   $main::lxdebug->enter_sub();
 
 628   my ($self, $form, $for_post_payments) = @_;
 
 630   my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
 
 631       $taxincluded, $tax, $diff);
 
 634   $form->{forex} = $form->{exchangerate};
 
 635   $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
 
 637   foreach $key (keys %{ $form->{AP_links} }) {
 
 638     foreach $ref (@{ $form->{AP_links}{$key} }) {
 
 639       if ($key eq "AP_paid") {
 
 640         $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
 
 642         $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
 
 646     $form->{$key} = $form->{"select$key"};
 
 651     # if there is a value we have an old entry
 
 652     next unless $form->{acc_trans}{$key};
 
 654     # do not use old entries for payments. They come from the form
 
 655     # even if they are not changeable (then they are in hiddens)
 
 656     next if $for_post_payments && $key eq "AP_paid";
 
 658     for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
 
 660       if ($key eq "AP_paid") {
 
 662         $form->{"AP_paid_$j"}         = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 663         $form->{"acc_trans_id_$j"}    = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
 
 664         $form->{"paid_$j"}            = $form->{acc_trans}{$key}->[$i - 1]->{amount};
 
 665         $form->{"datepaid_$j"}        = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
 
 666         $form->{"gldate_$j"}          = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
 
 667         $form->{"source_$j"}          = $form->{acc_trans}{$key}->[$i - 1]->{source};
 
 668         $form->{"memo_$j"}            = $form->{acc_trans}{$key}->[$i - 1]->{memo};
 
 670         $form->{"exchangerate_$i"}    = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
 
 671         $form->{"forex_$j"}           = $form->{"exchangerate_$i"};
 
 672         $form->{"AP_paid_$j"}         = $form->{acc_trans}{$key}->[$i-1]->{accno};
 
 673         $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
 
 674         $form->{paidaccounts}++;
 
 680         if (($key eq "AP_tax") || ($key eq "AR_tax")) {
 
 681           $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"}  = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 682           $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
 
 684           if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
 
 685             $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
 
 687             $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
 
 688             $withholdingrate  += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
 
 691           $index                 = $form->{acc_trans}{$key}->[$i - 1]->{index};
 
 692           $form->{"tax_$index"}  = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
 
 693           $totaltax             += $form->{"tax_$index"};
 
 697           $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
 
 699           if ($akey eq 'amount') {
 
 701             $form->{"${akey}_$i"} *= -1;
 
 702             $totalamount          += $form->{"${akey}_$i"};
 
 703             $form->{taxrate}       = $form->{acc_trans}{$key}->[$i - 1]->{rate};
 
 705             $form->{"projectnumber_$k"}    = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
 
 706             $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
 
 707             $form->{"project_id_$k"}       = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
 
 710           $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
 
 712           my $q_description    = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
 
 713           $form->{"select${key}"} =~
 
 715                 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
 
 717               $form->{acc_trans}{$key}->[$i-1]->{accno}
 
 721           $form->{"${key}_$k"} = $1;
 
 724             $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
 
 726           } elsif ($akey eq 'amount') {
 
 727             $form->{"${key}_$k"}   = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
 
 728             $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id}    . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
 
 735   $form->{taxincluded}  = $taxincluded if ($form->{id});
 
 736   $form->{paidaccounts} = 1            if not defined $form->{paidaccounts};
 
 738   if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
 
 739     # add tax to amounts and invtotal
 
 740     for $i (1 .. $form->{rowcount}) {
 
 741       $taxamount            = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
 
 742       $tax                  = $form->round_amount($taxamount, 2);
 
 743       $diff                += ($taxamount - $tax);
 
 744       $form->{"amount_$i"} += $form->{"tax_$i"};
 
 747     $form->{amount_1} += $form->round_amount($diff, 2);
 
 750   $taxamount        = $form->round_amount($taxamount, 2);
 
 751   $form->{invtotal} = $totalamount + $totaltax;
 
 753   $main::lxdebug->leave_sub();
 
 757   $main::lxdebug->enter_sub();
 
 759   my ($self, $form, $myconfig, $id) = @_;
 
 761   my ($query, $new_id, $storno_row, $acc_trans_rows);
 
 762   my $dbh = $form->get_standard_dbh($myconfig);
 
 764   $query = qq|SELECT nextval('glid')|;
 
 765   ($new_id) = selectrow_query($form, $dbh, $query);
 
 767   $query = qq|SELECT * FROM ap WHERE id = ?|;
 
 768   $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
 
 770   $storno_row->{id}         = $new_id;
 
 771   $storno_row->{storno_id}  = $id;
 
 772   $storno_row->{storno}     = 't';
 
 773   $storno_row->{invnumber}  = 'Storno-' . $storno_row->{invnumber};
 
 774   $storno_row->{amount}    *= -1;
 
 775   $storno_row->{netamount} *= -1;
 
 776   $storno_row->{paid}       = $storno_row->{amount};
 
 778   delete @$storno_row{qw(itime mtime)};
 
 780   $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
 
 781   do_query($form, $dbh, $query, (values %$storno_row));
 
 783   $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
 
 784   do_query($form, $dbh, $query, $id);
 
 786   # now copy acc_trans entries
 
 787   $query = qq|SELECT a.*, c.link FROM acc_trans a LEFT JOIN chart c ON a.chart_id = c.id WHERE a.trans_id = ? ORDER BY a.acc_trans_id|;
 
 788   my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
 
 790   # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
 
 791   while ($rowref->[-1]{link} =~ /paid/) {
 
 792     splice(@$rowref, -2);
 
 795   for my $row (@$rowref) {
 
 796     delete @$row{qw(itime mtime link acc_trans_id)};
 
 797     $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
 
 798     $row->{trans_id}   = $new_id;
 
 799     $row->{amount}    *= -1;
 
 800     do_query($form, $dbh, $query, (values %$row));
 
 803   map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
 
 807   $main::lxdebug->leave_sub();