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 #======================================================================
37 use SL::DATEV qw(:CONSTANTS);
46 sub post_transaction {
47 $main::lxdebug->enter_sub();
49 my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
50 my $rc = 0; # return code auf false setzen
52 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
54 my ($null, $taxrate, $amount);
57 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
59 ($null, $form->{department_id}) = split(/--/, $form->{department});
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 $tax = $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
111 $amount = $form->{"amount_$i"} - $tax;
112 $form->{"amount_$i"} = $form->round_amount($amount, 2);
113 $diff += $amount - $form->{"amount_$i"};
114 $form->{"tax_$i"} = $form->round_amount($tax, 2);
115 $form->{netamount} += $form->{"amount_$i"};
117 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
118 $form->{netamount} += $form->{"amount_$i"};
120 $form->{total_tax} += $form->{"tax_$i"} * -1;
123 # adjust paidaccounts if there is no date in the last row
124 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
126 $form->{invpaid} = 0;
127 $form->{netamount} *= -1;
130 for my $i (1 .. $form->{paidaccounts}) {
132 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
135 $form->{invpaid} += $form->{"paid_$i"};
136 $form->{datepaid} = $form->{"datepaid_$i"};
141 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
143 # store invoice total, this goes into ap table
144 $form->{invtotal} = $form->{netamount} + $form->{total_tax};
146 # amount for total AP
147 $form->{payables} = $form->{invtotal};
149 # update exchangerate
150 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
151 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
152 $form->{exchangerate});
155 my ($query, $sth, @values);
157 if (!$payments_only) {
158 # if we have an id delete old records
161 # delete detail records
162 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
163 do_query($form, $dbh, $query, $form->{id});
167 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
170 qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | .
171 qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?),
172 (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|;
173 do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $form->{login}, $form->{currency}, $form->{vendor_id});
177 $query = qq|UPDATE ap SET invnumber = ?,
178 transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
179 amount = ?, duedate = ?, paid = ?, netamount = ?,
180 currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?,
181 globalproject_id = ?, direct_debit = ?
183 @values = ($form->{invnumber}, conv_date($form->{transdate}),
184 $form->{ordnumber}, conv_i($form->{vendor_id}),
185 $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
186 conv_date($form->{duedate}), $form->{invpaid},
188 $form->{currency}, $form->{notes},
189 conv_i($form->{department_id}), $form->{storno},
190 $form->{storno_id}, conv_i($form->{globalproject_id}),
191 $form->{direct_debit} ? 't' : 'f',
193 do_query($form, $dbh, $query, @values);
195 # add individual transactions
196 for my $i (1 .. $form->{rowcount}) {
197 if ($form->{"amount_$i"} != 0) {
199 $project_id = conv_i($form->{"project_id_$i"});
201 # insert detail records in acc_trans
203 qq|INSERT INTO acc_trans | .
204 qq| (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link)| .
205 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
206 qq| ?, ?, ?, ?, ?,| .
207 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
208 @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
209 $form->{"amount_$i"}, conv_date($form->{transdate}),
210 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
211 $form->{AP_amounts}{"amount_$i"});
212 do_query($form, $dbh, $query, @values);
214 if ($form->{"tax_$i"} != 0) {
215 # insert detail records in acc_trans
217 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
218 qq| project_id, taxkey, tax_id, chart_link) | .
219 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
220 qq| ?, ?, ?, ?, ?,| .
221 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
222 @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
223 $form->{"tax_$i"}, conv_date($form->{transdate}),
224 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
225 $form->{AP_amounts}{"tax_$i"});
226 do_query($form, $dbh, $query, @values);
234 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) | .
235 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
236 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
237 qq| (SELECT tax_id| .
239 qq| WHERE chart_id= (SELECT id | .
241 qq| WHERE accno = ?)| .
242 qq| AND startdate <= ?| .
243 qq| ORDER BY startdate DESC LIMIT 1),| .
244 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
245 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
246 conv_date($form->{transdate}), $form->{AP_amounts}{payables}, $form->{AP_amounts}{payables}, conv_date($form->{transdate}),
247 $form->{AP_amounts}{payables});
248 do_query($form, $dbh, $query, @values);
251 # if there is no amount but a payment record a payable
252 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
253 $form->{payables} = $form->{invpaid};
256 # add paid transactions
257 for my $i (1 .. $form->{paidaccounts}) {
259 if ($form->{"acc_trans_id_$i"} && $payments_only && (SL::DB::Default->get->payments_changeable == 0)) {
263 if ($form->{"paid_$i"} != 0) {
264 my $project_id = conv_i($form->{"paid_project_id_$i"});
267 if ($form->{currency} eq $form->{defaultcurrency}) {
268 $form->{"exchangerate_$i"} = 1;
270 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
271 $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
273 $form->{"AP_paid_$i"} =~ s/\"//g;
277 ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
278 $form->{"datepaid_$i"} = $form->{transdate}
279 unless ($form->{"datepaid_$i"});
281 # if there is no amount and invtotal is zero there is no exchangerate
282 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
283 $form->{exchangerate} = $form->{"exchangerate_$i"};
287 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
289 if ($form->{payables}) {
291 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) | .
292 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
293 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
294 qq| (SELECT tax_id| .
296 qq| WHERE chart_id= (SELECT id | .
298 qq| WHERE accno = ?)| .
299 qq| AND startdate <= ?| .
300 qq| ORDER BY startdate DESC LIMIT 1),| .
301 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
302 @values = ($form->{id}, $form->{AP_payables}, $amount,
303 conv_date($form->{"datepaid_$i"}), $project_id,
304 $form->{AP_payables}, $form->{AP_payables}, conv_date($form->{"datepaid_$i"}),
305 $form->{AP_payables});
306 do_query($form, $dbh, $query, @values);
308 $form->{payables} = $amount;
311 my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
313 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey, tax_id, chart_link) | .
314 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, | .
315 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
316 qq| (SELECT tax_id| .
318 qq| WHERE chart_id= (SELECT id | .
320 qq| WHERE accno = ?)| .
321 qq| AND startdate <= ?| .
322 qq| ORDER BY startdate DESC LIMIT 1),| .
323 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
324 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
325 conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"},
326 $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"},
327 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
328 $form->{"AP_paid_account_$i"});
329 do_query($form, $dbh, $query, @values);
331 # add exchange rate difference
333 $form->round_amount($form->{"paid_$i"} *
334 ($form->{"exchangerate_$i"} - 1), 2);
337 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
338 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
339 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
340 qq| (SELECT tax_id| .
342 qq| WHERE chart_id= (SELECT id | .
344 qq| WHERE accno = ?)| .
345 qq| AND startdate <= ?| .
346 qq| ORDER BY startdate DESC LIMIT 1),| .
347 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
348 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
349 conv_date($form->{"datepaid_$i"}), $project_id,
350 $form->{"AP_paid_account_$i"},
351 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
352 $form->{"AP_paid_account_$i"});
353 do_query($form, $dbh, $query, @values);
356 # exchangerate gain/loss
358 $form->round_amount($form->{"paid_$i"} *
359 ($form->{exchangerate} -
360 $form->{"exchangerate_$i"}), 2);
364 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
365 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
366 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
367 qq| (SELECT tax_id| .
369 qq| WHERE chart_id= (SELECT id | .
371 qq| WHERE accno = ?)| .
372 qq| AND startdate <= ?| .
373 qq| ORDER BY startdate DESC LIMIT 1),| .
374 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
375 @values = ($form->{id},
376 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
377 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
378 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
379 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno}, conv_date($form->{"datepaid_$i"}),
380 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno});
381 do_query($form, $dbh, $query, @values);
384 # update exchange rate record
385 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
386 $form->update_exchangerate($dbh, $form->{currency},
387 $form->{"datepaid_$i"},
388 0, $form->{"exchangerate_$i"});
393 if ($payments_only) {
394 $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
395 do_query($form, $dbh, $query, $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
398 IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
400 # safety check datev export
401 if ($::instance_conf->get_datev_check_on_ap_transaction) {
402 my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
403 $transdate ||= DateTime->today;
405 my $datev = SL::DATEV->new(
406 exporttype => DATEV_ET_BUCHUNGEN,
407 format => DATEV_FORMAT_KNE,
411 trans_id => $form->{id},
416 if ($datev->errors) {
418 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
422 if (!$provided_dbh) {
427 $rc = 1; # Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
429 $main::lxdebug->leave_sub();
434 sub delete_transaction {
435 $main::lxdebug->enter_sub();
437 my ($self, $myconfig, $form) = @_;
439 # connect to database
440 my $dbh = $form->dbconnect_noauto($myconfig);
442 # acc_trans entries are deleted by database triggers.
443 my $query = qq|DELETE FROM ap WHERE id = ?|;
444 do_query($form, $dbh, $query, $form->{id});
446 my $rc = $dbh->commit;
449 $main::lxdebug->leave_sub();
454 sub ap_transactions {
455 $main::lxdebug->enter_sub();
457 my ($self, $myconfig, $form) = @_;
459 # connect to database
460 my $dbh = $form->get_standard_dbh($myconfig);
463 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
464 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
465 qq| a.globalproject_id, a.storno, a.storno_id, | .
466 qq| pr.projectnumber AS globalprojectnumber, | .
467 qq| e.name AS employee, | .
468 qq| v.vendornumber, v.country, v.ustid, | .
469 qq| tz.description AS taxzone, | .
470 qq| pt.description AS payment_terms, | .
471 qq{ ( SELECT ch.accno || ' -- ' || ch.description
473 LEFT JOIN chart ch ON ch.id = at.chart_id
474 WHERE ch.link ~ 'AP[[:>:]]'
475 AND at.trans_id = a.id
479 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
480 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
481 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
482 qq|LEFT JOIN tax_zones tz ON (tz.id = v.taxzone_id)| .
483 qq|LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)|;
487 unless ( $::auth->assert('show_ap_transactions', 1) ) {
488 $where .= " AND NOT invoice = 'f' "; # remove ap transactions from Sales -> Reports -> Invoices
493 if ($form->{vendor_id}) {
494 $where .= " AND a.vendor_id = ?";
495 push(@values, $form->{vendor_id});
496 } elsif ($form->{vendor}) {
497 $where .= " AND v.name ILIKE ?";
498 push(@values, $form->like($form->{vendor}));
500 if ($form->{department}) {
501 # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
502 # wird so nicht mehr als zeichenkette zusammengebaut
503 # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
504 #my ($null, $department_id) = split /--/, $form->{department};
505 $where .= " AND a.department_id = ?";
506 push(@values, $form->{department});
508 if ($form->{invnumber}) {
509 $where .= " AND a.invnumber ILIKE ?";
510 push(@values, $form->like($form->{invnumber}));
512 if ($form->{ordnumber}) {
513 $where .= " AND a.ordnumber ILIKE ?";
514 push(@values, $form->like($form->{ordnumber}));
516 if ($form->{notes}) {
517 $where .= " AND lower(a.notes) LIKE ?";
518 push(@values, $form->like($form->{notes}));
520 if ($form->{project_id}) {
522 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
523 qq| (SELECT * FROM invoice i | .
524 qq| WHERE i.project_id = ? AND i.trans_id = a.id) | .
526 qq| (SELECT * FROM acc_trans at | .
527 qq| WHERE at.project_id = ? AND at.trans_id = a.id)| .
529 push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id});
532 if ($form->{transdatefrom}) {
533 $where .= " AND a.transdate >= ?";
534 push(@values, $form->{transdatefrom});
536 if ($form->{transdateto}) {
537 $where .= " AND a.transdate <= ?";
538 push(@values, $form->{transdateto});
540 if ($form->{open} || $form->{closed}) {
541 unless ($form->{open} && $form->{closed}) {
542 $where .= " AND a.amount <> a.paid" if ($form->{open});
543 $where .= " AND a.amount = a.paid" if ($form->{closed});
548 substr($where, 0, 4, " WHERE ");
552 my @a = qw(transdate invnumber name);
553 push @a, "employee" if $form->{l_employee};
554 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
555 my $sortorder = join(', ', map { "$_ $sortdir" } @a);
557 if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
558 $sortorder = $form->{sort} . " $sortdir";
561 $query .= " ORDER BY $sortorder";
563 my @result = selectall_hashref_query($form, $dbh, $query, @values);
565 $form->{AP} = [ @result ];
567 $main::lxdebug->leave_sub();
571 $main::lxdebug->enter_sub();
573 my ($self, $myconfig, $form) = @_;
575 # connect to database
576 my $dbh = $form->dbconnect($myconfig);
580 " (SELECT transdate FROM ap WHERE id = " .
581 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
583 ($form->{transdate}) = $dbh->selectrow_array($query);
587 $main::lxdebug->leave_sub();
590 sub _delete_payments {
591 $main::lxdebug->enter_sub();
593 my ($self, $form, $dbh) = @_;
595 my @delete_acc_trans_ids;
597 # Delete old payment entries from acc_trans.
599 qq|SELECT acc_trans_id
601 WHERE (trans_id = ?) AND fx_transaction
605 SELECT at.acc_trans_id
607 LEFT JOIN chart c ON (at.chart_id = c.id)
608 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
609 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
612 qq|SELECT at.acc_trans_id
614 LEFT JOIN chart c ON (at.chart_id = c.id)
616 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
617 ORDER BY at.acc_trans_id
619 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
621 if (@delete_acc_trans_ids) {
622 $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
623 do_query($form, $dbh, $query);
626 $main::lxdebug->leave_sub();
630 $main::lxdebug->enter_sub();
632 my ($self, $myconfig, $form, $locale) = @_;
634 # connect to database, turn off autocommit
635 my $dbh = $form->dbconnect_noauto($myconfig);
637 my (%payments, $old_form, $row, $item, $query, %keep_vars);
639 $old_form = save_form();
641 # Delete all entries in acc_trans from prior payments.
642 if (SL::DB::Default->get->payments_changeable != 0) {
643 $self->_delete_payments($form, $dbh);
646 # Save the new payments the user made before cleaning up $form.
647 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$';
648 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
650 # Clean up $form so that old content won't tamper the results.
651 %keep_vars = map { $_, 1 } qw(login password id);
652 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
654 # Retrieve the invoice from the database.
655 $form->create_links('AP', $myconfig, 'vendor', $dbh);
657 # Restore the payment options from the user input.
658 map { $form->{$_} = $payments{$_} } keys %payments;
660 # Set up the content of $form in the way that AR::post_transaction() expects.
662 $self->setup_form($form, 1);
664 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
665 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
671 LEFT JOIN chart c ON (at.chart_id = c.id)
673 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
674 ORDER BY at.acc_trans_id
677 ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
679 # Post the new payments.
680 $self->post_transaction($myconfig, $form, $dbh, 1);
682 restore_form($old_form);
684 my $rc = $dbh->commit();
687 $main::lxdebug->leave_sub();
693 $main::lxdebug->enter_sub();
695 my ($self, $form, $for_post_payments) = @_;
697 my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
701 $form->{forex} = $form->{exchangerate};
702 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
704 foreach $key (keys %{ $form->{AP_links} }) {
705 foreach $ref (@{ $form->{AP_links}{$key} }) {
706 if ($key eq "AP_paid") {
707 $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
709 $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
713 $form->{$key} = $form->{"select$key"};
718 # if there is a value we have an old entry
719 next unless $form->{acc_trans}{$key};
721 # do not use old entries for payments. They come from the form
722 # even if they are not changeable (then they are in hiddens)
723 next if $for_post_payments && $key eq "AP_paid";
725 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
727 if ($key eq "AP_paid") {
729 $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
730 $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
731 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
732 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
733 $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
734 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
735 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
737 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
738 $form->{"forex_$j"} = $form->{"exchangerate_$i"};
739 $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
740 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
741 $form->{paidaccounts}++;
747 if (($key eq "AP_tax") || ($key eq "AR_tax")) {
748 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
749 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
751 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
752 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
754 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
755 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
758 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
759 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
760 $totaltax += $form->{"tax_$index"};
764 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
766 if ($akey eq 'amount') {
768 $form->{"${akey}_$i"} *= -1;
769 $totalamount += $form->{"${akey}_$i"};
770 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
772 $form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
773 $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
774 $form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
777 $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
779 my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
780 $form->{"select${key}"} =~
782 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
784 $form->{acc_trans}{$key}->[$i-1]->{accno}
788 $form->{"${key}_$k"} = $1;
791 $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
793 } elsif ($akey eq 'amount') {
794 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
795 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
802 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
804 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
805 # add tax to amounts and invtotal
806 for $i (1 .. $form->{rowcount}) {
807 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
808 $tax = $form->round_amount($taxamount, 2);
809 $diff += ($taxamount - $tax);
810 $form->{"amount_$i"} += $form->{"tax_$i"};
813 $form->{amount_1} += $form->round_amount($diff, 2);
816 $taxamount = $form->round_amount($taxamount, 2);
817 $form->{invtotal} = $totalamount + $totaltax;
819 $main::lxdebug->leave_sub();
823 $main::lxdebug->enter_sub();
825 my ($self, $form, $myconfig, $id) = @_;
827 my ($query, $new_id, $storno_row, $acc_trans_rows);
828 my $dbh = $form->get_standard_dbh($myconfig);
830 $query = qq|SELECT nextval('glid')|;
831 ($new_id) = selectrow_query($form, $dbh, $query);
833 $query = qq|SELECT * FROM ap WHERE id = ?|;
834 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
836 $storno_row->{id} = $new_id;
837 $storno_row->{storno_id} = $id;
838 $storno_row->{storno} = 't';
839 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
840 $storno_row->{amount} *= -1;
841 $storno_row->{netamount} *= -1;
842 $storno_row->{paid} = $storno_row->{amount};
844 delete @$storno_row{qw(itime mtime)};
846 $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
847 do_query($form, $dbh, $query, (values %$storno_row));
849 $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
850 do_query($form, $dbh, $query, $id);
852 # now copy acc_trans entries
853 $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|;
854 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
856 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
857 while ($rowref->[-1]{link} =~ /paid/) {
858 splice(@$rowref, -2);
861 for my $row (@$rowref) {
862 delete @$row{qw(itime mtime link acc_trans_id)};
863 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
864 $row->{trans_id} = $new_id;
865 $row->{amount} *= -1;
866 do_query($form, $dbh, $query, (values %$row));
869 map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
873 $main::lxdebug->leave_sub();