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) = @_;
51 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
53 my ($null, $taxrate, $amount);
56 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
58 ($null, $form->{department_id}) = split(/--/, $form->{department});
59 $form->{department_id} *= 1;
61 if ($form->{currency} eq $form->{defaultcurrency}) {
62 $form->{exchangerate} = 1;
64 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell');
65 $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate});
68 for my $i (1 .. $form->{rowcount}) {
69 $form->{AP_amounts}{"amount_$i"} =
70 (split(/--/, $form->{"AP_amount_$i"}))[0];
72 ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
73 ($form->{AP_payables}) = split(/--/, $form->{APselected});
75 # reverse and parse amounts
76 for my $i (1 .. $form->{rowcount}) {
77 $form->{"amount_$i"} =
79 $form->parse_amount($myconfig, $form->{"amount_$i"}) *
80 $form->{exchangerate} * -1,
82 $amount += ($form->{"amount_$i"} * -1);
84 # parse tax_$i for later
85 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"}) * -1;
89 $form->{amount} = $amount;
91 # taxincluded doesn't make sense if there is no amount
92 $form->{taxincluded} = 0 if ($form->{amount} == 0);
94 for my $i (1 .. $form->{rowcount}) {
95 ($form->{"tax_id_$i"}, undef) = split /--/, $form->{"taxchart_$i"};
98 qq|SELECT c.accno, t.taxkey, t.rate | .
99 qq|FROM tax t LEFT JOIN chart c on (c.id=t.chart_id) | .
100 qq|WHERE t.id = ? | .
101 qq|ORDER BY c.accno|;
102 my $sth = $dbh->prepare($query);
103 $sth->execute($form->{"tax_id_$i"}) || $form->dberror($query . " (" . $form->{"tax_id_$i"} . ")");
104 ($form->{AP_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = $sth->fetchrow_array();
109 if ($form->{taxincluded} *= 1) {
110 if (!$form->{"korrektur_$i"}) {
112 $form->{"amount_$i"} -
113 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
115 $tax = $form->{"tax_$i"};
117 $amount = $form->{"amount_$i"} - $tax;
118 $form->{"amount_$i"} = $form->round_amount($amount, 2);
119 $diff += $amount - $form->{"amount_$i"};
120 $form->{"tax_$i"} = $form->round_amount($tax, 2);
121 $form->{netamount} += $form->{"amount_$i"};
123 if (!$form->{"korrektur_$i"}) {
124 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
126 $tax = $form->{"tax_$i"};
129 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
130 $form->{netamount} += $form->{"amount_$i"};
132 $form->{total_tax} += $form->{"tax_$i"} * -1;
135 # adjust paidaccounts if there is no date in the last row
136 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
138 $form->{invpaid} = 0;
139 $form->{netamount} *= -1;
142 for my $i (1 .. $form->{paidaccounts}) {
144 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
147 $form->{invpaid} += $form->{"paid_$i"};
148 $form->{datepaid} = $form->{"datepaid_$i"};
153 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
155 # store invoice total, this goes into ap table
156 $form->{invtotal} = $form->{netamount} + $form->{total_tax};
158 # amount for total AP
159 $form->{payables} = $form->{invtotal};
161 # update exchangerate
162 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
163 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
164 $form->{exchangerate});
167 my ($query, $sth, @values);
169 if (!$payments_only) {
170 # if we have an id delete old records
173 # delete detail records
174 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
175 do_query($form, $dbh, $query, $form->{id});
178 my $uid = rand() . time;
180 $uid .= $form->{login};
182 $uid = substr($uid, 2, 75);
185 qq|INSERT INTO ap (invnumber, employee_id) | .
186 qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?))|;
187 do_query($form, $dbh, $query, $uid, $form->{login});
189 $query = qq|SELECT a.id FROM ap a
190 WHERE a.invnumber = ?|;
191 ($form->{id}) = selectrow_query($form, $dbh, $query, $uid);
194 $form->{invnumber} = $form->{id} unless $form->{invnumber};
196 $query = qq|UPDATE ap SET
197 invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
198 amount = ?, duedate = ?, paid = ?, netamount = ?,
199 curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?
201 @values = ($form->{invnumber}, conv_date($form->{transdate}),
202 $form->{ordnumber}, conv_i($form->{vendor_id}),
203 $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
204 conv_date($form->{duedate}), $form->{invpaid},
206 $form->{currency}, $form->{notes},
207 conv_i($form->{department_id}), $form->{storno},
208 $form->{storno_id}, $form->{id});
209 do_query($form, $dbh, $query, @values);
211 # add individual transactions
212 for my $i (1 .. $form->{rowcount}) {
213 if ($form->{"amount_$i"} != 0) {
215 $project_id = conv_i($form->{"project_id_$i"});
217 # insert detail records in acc_trans
219 qq|INSERT INTO acc_trans | .
220 qq| (trans_id, chart_id, amount, transdate, project_id, taxkey)| .
221 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
223 @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
224 $form->{"amount_$i"}, conv_date($form->{transdate}),
225 $project_id, $form->{"taxkey_$i"});
226 do_query($form, $dbh, $query, @values);
228 if ($form->{"tax_$i"} != 0) {
229 # insert detail records in acc_trans
231 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
232 qq| project_id, taxkey) | .
233 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
235 @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
236 $form->{"tax_$i"}, conv_date($form->{transdate}),
237 $project_id, $form->{"taxkey_$i"});
238 do_query($form, $dbh, $query, @values);
246 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
247 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
248 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
249 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
250 conv_date($form->{transdate}), $form->{AP_amounts}{payables});
251 do_query($form, $dbh, $query, @values);
254 # if there is no amount but a payment record a payable
255 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
256 $form->{payables} = $form->{invpaid};
259 # add paid transactions
260 for my $i (1 .. $form->{paidaccounts}) {
261 if ($form->{"paid_$i"} != 0) {
262 my $project_id = conv_i($form->{"paid_project_id_$i"});
265 if ($form->{currency} eq $form->{defaultcurrency}) {
266 $form->{"exchangerate_$i"} = 1;
268 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
269 $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
271 $form->{"AP_paid_$i"} =~ s/\"//g;
275 ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
276 $form->{"datepaid_$i"} = $form->{transdate}
277 unless ($form->{"datepaid_$i"});
279 # if there is no amount and invtotal is zero there is no exchangerate
280 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
281 $form->{exchangerate} = $form->{"exchangerate_$i"};
285 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
287 if ($form->{payables}) {
289 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
290 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
291 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
292 @values = ($form->{id}, $form->{AP_payables}, $amount,
293 conv_date($form->{"datepaid_$i"}), $project_id,
294 $form->{AP_payables});
295 do_query($form, $dbh, $query, @values);
297 $form->{payables} = $amount;
301 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
302 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
303 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
304 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
305 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
306 $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"});
307 do_query($form, $dbh, $query, @values);
309 # add exchange rate difference
311 $form->round_amount($form->{"paid_$i"} *
312 ($form->{"exchangerate_$i"} - 1), 2);
315 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
316 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
317 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
318 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
319 conv_date($form->{"datepaid_$i"}), $project_id,
320 $form->{"AP_paid_account_$i"});
321 do_query($form, $dbh, $query, @values);
324 # exchangerate gain/loss
326 $form->round_amount($form->{"paid_$i"} *
327 ($form->{exchangerate} -
328 $form->{"exchangerate_$i"}), 2);
332 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
333 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
334 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
335 @values = ($form->{id}, ($amount > 0) ?
336 $form->{fxgain_accno} : $form->{fxloss_accno},
337 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
339 $form->{fxgain_accno} : $form->{fxloss_accno});
340 do_query($form, $dbh, $query, @values);
343 # update exchange rate record
344 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
345 $form->update_exchangerate($dbh, $form->{currency},
346 $form->{"datepaid_$i"},
347 0, $form->{"exchangerate_$i"});
352 if ($payments_only) {
353 $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
354 do_query($form, $dbh, $query, $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
357 IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
360 if (!$provided_dbh) {
365 $main::lxdebug->leave_sub();
370 sub delete_transaction {
371 $main::lxdebug->enter_sub();
373 my ($self, $myconfig, $form, $spool) = @_;
375 # connect to database
376 my $dbh = $form->dbconnect_noauto($myconfig);
378 my $query = qq|DELETE FROM ap WHERE id = ?|;
379 do_query($form, $dbh, $query, $form->{id});
381 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
382 do_query($form, $dbh, $query, $form->{id});
384 # commit and redirect
385 my $rc = $dbh->commit;
388 $main::lxdebug->leave_sub();
393 sub ap_transactions {
394 $main::lxdebug->enter_sub();
396 my ($self, $myconfig, $form) = @_;
398 # connect to database
399 my $dbh = $form->get_standard_dbh($myconfig);
402 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
403 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
404 qq| a.globalproject_id, a.storno, a.storno_id, | .
405 qq| pr.projectnumber AS globalprojectnumber, | .
406 qq| e.name AS employee, | .
407 qq| v.vendornumber, v.country, v.ustid, | .
408 qq| tz.description AS taxzone, | .
409 qq| pt.description AS payment_terms, | .
410 qq{ ( SELECT ch.accno || ' -- ' || ch.description
412 LEFT JOIN chart ch ON ch.id = at.chart_id
413 WHERE ch.link ~ 'AP[[:>:]]'
414 AND at.trans_id = a.id
418 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
419 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
420 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
421 qq|LEFT JOIN tax_zones tz ON (tz.id = v.taxzone_id)| .
422 qq|LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)|;
427 if ($form->{vendor_id}) {
428 $where .= " AND a.vendor_id = ?";
429 push(@values, $form->{vendor_id});
430 } elsif ($form->{vendor}) {
431 $where .= " AND v.name ILIKE ?";
432 push(@values, $form->like($form->{vendor}));
434 if ($form->{department}) {
435 my ($null, $department_id) = split /--/, $form->{department};
436 $where .= " AND a.department_id = ?";
437 push(@values, $department_id);
439 if ($form->{invnumber}) {
440 $where .= " AND a.invnumber ILIKE ?";
441 push(@values, $form->like($form->{invnumber}));
443 if ($form->{ordnumber}) {
444 $where .= " AND a.ordnumber ILIKE ?";
445 push(@values, $form->like($form->{ordnumber}));
447 if ($form->{notes}) {
448 $where .= " AND lower(a.notes) LIKE ?";
449 push(@values, $form->like($form->{notes}));
451 if ($form->{project_id}) {
453 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
454 qq| (SELECT * FROM invoice i | .
455 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
456 push(@values, $form->{project_id}, $form->{project_id});
459 if ($form->{transdatefrom}) {
460 $where .= " AND a.transdate >= ?";
461 push(@values, $form->{transdatefrom});
463 if ($form->{transdateto}) {
464 $where .= " AND a.transdate <= ?";
465 push(@values, $form->{transdateto});
467 if ($form->{open} || $form->{closed}) {
468 unless ($form->{open} && $form->{closed}) {
469 $where .= " AND a.amount <> a.paid" if ($form->{open});
470 $where .= " AND a.amount = a.paid" if ($form->{closed});
475 substr($where, 0, 4, " WHERE ");
479 my @a = qw(transdate invnumber name);
480 push @a, "employee" if $form->{l_employee};
481 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
482 my $sortorder = join(', ', map { "$_ $sortdir" } @a);
484 if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
485 $sortorder = $form->{sort} . " $sortdir";
488 $query .= " ORDER BY $sortorder";
490 my @result = selectall_hashref_query($form, $dbh, $query, @values);
492 $form->{AP} = [ @result ];
494 $main::lxdebug->leave_sub();
498 $main::lxdebug->enter_sub();
500 my ($self, $myconfig, $form) = @_;
502 # connect to database
503 my $dbh = $form->dbconnect($myconfig);
507 " (SELECT transdate FROM ap WHERE id = " .
508 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
510 ($form->{transdate}) = $dbh->selectrow_array($query);
514 $main::lxdebug->leave_sub();
517 sub _delete_payments {
518 $main::lxdebug->enter_sub();
520 my ($self, $form, $dbh) = @_;
522 my @delete_acc_trans_ids;
524 # Delete old payment entries from acc_trans.
526 qq|SELECT acc_trans_id
528 WHERE (trans_id = ?) AND fx_transaction
532 SELECT at.acc_trans_id
534 LEFT JOIN chart c ON (at.chart_id = c.id)
535 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
536 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
539 qq|SELECT at.acc_trans_id
541 LEFT JOIN chart c ON (at.chart_id = c.id)
543 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
544 ORDER BY at.acc_trans_id
546 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
548 if (@delete_acc_trans_ids) {
549 $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
550 do_query($form, $dbh, $query);
553 $main::lxdebug->leave_sub();
557 $main::lxdebug->enter_sub();
559 my ($self, $myconfig, $form, $locale) = @_;
561 # connect to database, turn off autocommit
562 my $dbh = $form->dbconnect_noauto($myconfig);
564 my (%payments, $old_form, $row, $item, $query, %keep_vars);
566 $old_form = save_form();
568 # Delete all entries in acc_trans from prior payments.
569 $self->_delete_payments($form, $dbh);
571 # Save the new payments the user made before cleaning up $form.
572 my $payments_re = '^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AP_paid_\d+$|^paidaccounts$';
573 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
575 # Clean up $form so that old content won't tamper the results.
576 %keep_vars = map { $_, 1 } qw(login password id);
577 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
579 # Retrieve the invoice from the database.
580 $form->create_links('AP', $myconfig, 'vendor', $dbh);
582 # Restore the payment options from the user input.
583 map { $form->{$_} = $payments{$_} } keys %payments;
585 # Set up the content of $form in the way that AR::post_transaction() expects.
587 $self->setup_form($form);
589 ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|);
590 $form->{defaultcurrency} = (split m/:/, $form->{defaultcurrency})[0];
591 $form->{currency} = $form->{defaultcurrency} if ($form->{defaultcurrency} && ($form->{currency} =~ m/^\s*$/));
593 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
599 LEFT JOIN chart c ON (at.chart_id = c.id)
601 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
602 ORDER BY at.acc_trans_id
605 ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
607 # Post the new payments.
608 $self->post_transaction($myconfig, $form, $dbh, 1);
610 restore_form($old_form);
612 my $rc = $dbh->commit();
615 $main::lxdebug->leave_sub();
621 $main::lxdebug->enter_sub();
623 my ($self, $form) = @_;
625 my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
626 $taxincluded, $tax, $diff);
629 $form->{forex} = $form->{exchangerate};
630 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
632 foreach $key (keys %{ $form->{AP_links} }) {
633 foreach $ref (@{ $form->{AP_links}{$key} }) {
634 if ($key eq "AP_paid") {
635 $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
637 $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
641 $form->{$key} = $form->{"select$key"};
643 # if there is a value we have an old entry
647 next unless $form->{acc_trans}{$key};
648 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
650 if ($key eq "AP_paid") {
652 $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
653 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
654 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
655 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
656 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
658 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
659 $form->{"forex_$j"} = $form->{"exchangerate_$i"};
660 $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
661 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
662 $form->{paidaccounts}++;
668 if (($key eq "AP_tax") || ($key eq "AR_tax")) {
669 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
670 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
672 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
673 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
675 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
676 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
679 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
680 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
681 $totaltax += $form->{"tax_$index"};
685 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
687 if ($akey eq 'amount') {
689 $form->{"${akey}_$i"} *= -1;
690 $totalamount += $form->{"${akey}_$i"};
691 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
693 $form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
694 $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
695 $form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
698 $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
700 my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
701 $form->{"select${key}"} =~
703 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
705 $form->{acc_trans}{$key}->[$i-1]->{accno}
709 $form->{"${key}_$k"} = $1;
712 $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
714 } elsif ($akey eq 'amount') {
715 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
716 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
723 $form->{taxincluded} = $taxincluded if ($form->{id});
724 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
726 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
727 # add tax to amounts and invtotal
728 for $i (1 .. $form->{rowcount}) {
729 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
730 $tax = $form->round_amount($taxamount, 2);
731 $diff += ($taxamount - $tax);
732 $form->{"amount_$i"} += $form->{"tax_$i"};
735 $form->{amount_1} += $form->round_amount($diff, 2);
738 $taxamount = $form->round_amount($taxamount, 2);
739 $form->{invtotal} = $totalamount + $totaltax;
741 $main::lxdebug->leave_sub();
745 $main::lxdebug->enter_sub();
747 my ($self, $form, $myconfig, $id) = @_;
749 my ($query, $new_id, $storno_row, $acc_trans_rows);
750 my $dbh = $form->get_standard_dbh($myconfig);
752 $query = qq|SELECT nextval('glid')|;
753 ($new_id) = selectrow_query($form, $dbh, $query);
755 $query = qq|SELECT * FROM ap WHERE id = ?|;
756 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
758 $storno_row->{id} = $new_id;
759 $storno_row->{storno_id} = $id;
760 $storno_row->{storno} = 't';
761 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
762 $storno_row->{amount} *= -1;
763 $storno_row->{netamount} *= -1;
764 $storno_row->{paid} = $storno_row->{amount};
766 delete @$storno_row{qw(itime mtime)};
768 $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
769 do_query($form, $dbh, $query, (values %$storno_row));
771 $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
772 do_query($form, $dbh, $query, $id);
774 # now copy acc_trans entries
775 $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|;
776 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
778 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
779 while ($rowref->[-1]{link} =~ /paid/) {
780 splice(@$rowref, -2);
783 for my $row (@$rowref) {
784 delete @$row{qw(itime mtime link)};
785 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
786 $row->{trans_id} = $new_id;
787 $row->{amount} *= -1;
788 do_query($form, $dbh, $query, (values %$row));
791 map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
795 $main::lxdebug->leave_sub();