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});
166 my $uid = rand() . time;
168 $uid .= $form->{login};
170 $uid = substr($uid, 2, 75);
173 qq|INSERT INTO ap (invnumber, employee_id,currency_id) | .
174 qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?),
175 (SELECT id FROM currencies WHERE name = ?) )|;
176 do_query($form, $dbh, $query, $uid, $form->{login}, $form->{currency});
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 currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?,
189 globalproject_id = ?, direct_debit = ?
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}),
199 $form->{direct_debit} ? 't' : 'f',
201 do_query($form, $dbh, $query, @values);
203 # add individual transactions
204 for my $i (1 .. $form->{rowcount}) {
205 if ($form->{"amount_$i"} != 0) {
207 $project_id = conv_i($form->{"project_id_$i"});
209 # insert detail records in acc_trans
211 qq|INSERT INTO acc_trans | .
212 qq| (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link)| .
213 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
214 qq| ?, ?, ?, ?, ?,| .
215 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
216 @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
217 $form->{"amount_$i"}, conv_date($form->{transdate}),
218 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
219 $form->{AP_amounts}{"amount_$i"});
220 do_query($form, $dbh, $query, @values);
222 if ($form->{"tax_$i"} != 0) {
223 # insert detail records in acc_trans
225 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
226 qq| project_id, taxkey, tax_id, chart_link) | .
227 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
228 qq| ?, ?, ?, ?, ?,| .
229 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
230 @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
231 $form->{"tax_$i"}, conv_date($form->{transdate}),
232 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
233 $form->{AP_amounts}{"tax_$i"});
234 do_query($form, $dbh, $query, @values);
242 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) | .
243 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
244 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
245 qq| (SELECT tax_id| .
247 qq| WHERE chart_id= (SELECT id | .
249 qq| WHERE accno = ?)| .
250 qq| AND startdate <= ?| .
251 qq| ORDER BY startdate DESC LIMIT 1),| .
252 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
253 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
254 conv_date($form->{transdate}), $form->{AP_amounts}{payables}, $form->{AP_amounts}{payables}, conv_date($form->{transdate}),
255 $form->{AP_amounts}{payables});
256 do_query($form, $dbh, $query, @values);
259 # if there is no amount but a payment record a payable
260 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
261 $form->{payables} = $form->{invpaid};
264 # add paid transactions
265 for my $i (1 .. $form->{paidaccounts}) {
267 if ($form->{"acc_trans_id_$i"} && $payments_only && (SL::DB::Default->get->payments_changeable == 0)) {
271 if ($form->{"paid_$i"} != 0) {
272 my $project_id = conv_i($form->{"paid_project_id_$i"});
275 if ($form->{currency} eq $form->{defaultcurrency}) {
276 $form->{"exchangerate_$i"} = 1;
278 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
279 $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
281 $form->{"AP_paid_$i"} =~ s/\"//g;
285 ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
286 $form->{"datepaid_$i"} = $form->{transdate}
287 unless ($form->{"datepaid_$i"});
289 # if there is no amount and invtotal is zero there is no exchangerate
290 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
291 $form->{exchangerate} = $form->{"exchangerate_$i"};
295 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
297 if ($form->{payables}) {
299 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) | .
300 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
301 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
302 qq| (SELECT tax_id| .
304 qq| WHERE chart_id= (SELECT id | .
306 qq| WHERE accno = ?)| .
307 qq| AND startdate <= ?| .
308 qq| ORDER BY startdate DESC LIMIT 1),| .
309 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
310 @values = ($form->{id}, $form->{AP_payables}, $amount,
311 conv_date($form->{"datepaid_$i"}), $project_id,
312 $form->{AP_payables}, $form->{AP_payables}, conv_date($form->{"datepaid_$i"}),
313 $form->{AP_payables});
314 do_query($form, $dbh, $query, @values);
316 $form->{payables} = $amount;
319 my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
321 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey, tax_id, chart_link) | .
322 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, | .
323 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
324 qq| (SELECT tax_id| .
326 qq| WHERE chart_id= (SELECT id | .
328 qq| WHERE accno = ?)| .
329 qq| AND startdate <= ?| .
330 qq| ORDER BY startdate DESC LIMIT 1),| .
331 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
332 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
333 conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"},
334 $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"},
335 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
336 $form->{"AP_paid_account_$i"});
337 do_query($form, $dbh, $query, @values);
339 # add exchange rate difference
341 $form->round_amount($form->{"paid_$i"} *
342 ($form->{"exchangerate_$i"} - 1), 2);
345 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
346 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
347 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
348 qq| (SELECT tax_id| .
350 qq| WHERE chart_id= (SELECT id | .
352 qq| WHERE accno = ?)| .
353 qq| AND startdate <= ?| .
354 qq| ORDER BY startdate DESC LIMIT 1),| .
355 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
356 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
357 conv_date($form->{"datepaid_$i"}), $project_id,
358 $form->{"AP_paid_account_$i"},
359 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
360 $form->{"AP_paid_account_$i"});
361 do_query($form, $dbh, $query, @values);
364 # exchangerate gain/loss
366 $form->round_amount($form->{"paid_$i"} *
367 ($form->{exchangerate} -
368 $form->{"exchangerate_$i"}), 2);
372 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
373 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
374 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
375 qq| (SELECT tax_id| .
377 qq| WHERE chart_id= (SELECT id | .
379 qq| WHERE accno = ?)| .
380 qq| AND startdate <= ?| .
381 qq| ORDER BY startdate DESC LIMIT 1),| .
382 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
383 @values = ($form->{id},
384 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
385 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
386 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
387 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno}, conv_date($form->{"datepaid_$i"}),
388 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno});
389 do_query($form, $dbh, $query, @values);
392 # update exchange rate record
393 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
394 $form->update_exchangerate($dbh, $form->{currency},
395 $form->{"datepaid_$i"},
396 0, $form->{"exchangerate_$i"});
401 if ($payments_only) {
402 $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
403 do_query($form, $dbh, $query, $form->{invpaid}, $form->{invpaid} ? conv_date($form->{datepaid}) : undef, conv_i($form->{id}));
406 IO->set_datepaid(table => 'ap', id => $form->{id}, dbh => $dbh);
408 # safety check datev export
409 if ($::instance_conf->get_datev_check_on_ap_transaction) {
410 my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
411 $transdate ||= DateTime->today;
413 my $datev = SL::DATEV->new(
414 exporttype => DATEV_ET_BUCHUNGEN,
415 format => DATEV_FORMAT_KNE,
419 trans_id => $form->{id},
424 if ($datev->errors) {
426 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
430 if (!$provided_dbh) {
435 $rc = 1; # Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
437 $main::lxdebug->leave_sub();
442 sub delete_transaction {
443 $main::lxdebug->enter_sub();
445 my ($self, $myconfig, $form) = @_;
447 # connect to database
448 my $dbh = $form->dbconnect_noauto($myconfig);
450 # acc_trans entries are deleted by database triggers.
451 my $query = qq|DELETE FROM ap WHERE id = ?|;
452 do_query($form, $dbh, $query, $form->{id});
454 my $rc = $dbh->commit;
457 $main::lxdebug->leave_sub();
462 sub ap_transactions {
463 $main::lxdebug->enter_sub();
465 my ($self, $myconfig, $form) = @_;
467 # connect to database
468 my $dbh = $form->get_standard_dbh($myconfig);
471 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
472 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
473 qq| a.globalproject_id, a.storno, a.storno_id, | .
474 qq| pr.projectnumber AS globalprojectnumber, | .
475 qq| e.name AS employee, | .
476 qq| v.vendornumber, v.country, v.ustid, | .
477 qq| tz.description AS taxzone, | .
478 qq| pt.description AS payment_terms, | .
479 qq{ ( SELECT ch.accno || ' -- ' || ch.description
481 LEFT JOIN chart ch ON ch.id = at.chart_id
482 WHERE ch.link ~ 'AP[[:>:]]'
483 AND at.trans_id = a.id
487 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
488 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
489 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
490 qq|LEFT JOIN tax_zones tz ON (tz.id = v.taxzone_id)| .
491 qq|LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)|;
495 unless ( $::auth->assert('show_ap_transactions', 1) ) {
496 $where .= " AND NOT invoice = 'f' "; # remove ap transactions from Sales -> Reports -> Invoices
501 if ($form->{vendor_id}) {
502 $where .= " AND a.vendor_id = ?";
503 push(@values, $form->{vendor_id});
504 } elsif ($form->{vendor}) {
505 $where .= " AND v.name ILIKE ?";
506 push(@values, $form->like($form->{vendor}));
508 if ($form->{department}) {
509 # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
510 # wird so nicht mehr als zeichenkette zusammengebaut
511 # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
512 #my ($null, $department_id) = split /--/, $form->{department};
513 $where .= " AND a.department_id = ?";
514 push(@values, $form->{department});
516 if ($form->{invnumber}) {
517 $where .= " AND a.invnumber ILIKE ?";
518 push(@values, $form->like($form->{invnumber}));
520 if ($form->{ordnumber}) {
521 $where .= " AND a.ordnumber ILIKE ?";
522 push(@values, $form->like($form->{ordnumber}));
524 if ($form->{notes}) {
525 $where .= " AND lower(a.notes) LIKE ?";
526 push(@values, $form->like($form->{notes}));
528 if ($form->{project_id}) {
530 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
531 qq| (SELECT * FROM invoice i | .
532 qq| WHERE i.project_id = ? AND i.trans_id = a.id) | .
534 qq| (SELECT * FROM acc_trans at | .
535 qq| WHERE at.project_id = ? AND at.trans_id = a.id)| .
537 push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id});
540 if ($form->{transdatefrom}) {
541 $where .= " AND a.transdate >= ?";
542 push(@values, $form->{transdatefrom});
544 if ($form->{transdateto}) {
545 $where .= " AND a.transdate <= ?";
546 push(@values, $form->{transdateto});
548 if ($form->{open} || $form->{closed}) {
549 unless ($form->{open} && $form->{closed}) {
550 $where .= " AND a.amount <> a.paid" if ($form->{open});
551 $where .= " AND a.amount = a.paid" if ($form->{closed});
556 substr($where, 0, 4, " WHERE ");
560 my @a = qw(transdate invnumber name);
561 push @a, "employee" if $form->{l_employee};
562 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
563 my $sortorder = join(', ', map { "$_ $sortdir" } @a);
565 if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
566 $sortorder = $form->{sort} . " $sortdir";
569 $query .= " ORDER BY $sortorder";
571 my @result = selectall_hashref_query($form, $dbh, $query, @values);
573 $form->{AP} = [ @result ];
575 $main::lxdebug->leave_sub();
579 $main::lxdebug->enter_sub();
581 my ($self, $myconfig, $form) = @_;
583 # connect to database
584 my $dbh = $form->dbconnect($myconfig);
588 " (SELECT transdate FROM ap WHERE id = " .
589 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
591 ($form->{transdate}) = $dbh->selectrow_array($query);
595 $main::lxdebug->leave_sub();
598 sub _delete_payments {
599 $main::lxdebug->enter_sub();
601 my ($self, $form, $dbh) = @_;
603 my @delete_acc_trans_ids;
605 # Delete old payment entries from acc_trans.
607 qq|SELECT acc_trans_id
609 WHERE (trans_id = ?) AND fx_transaction
613 SELECT at.acc_trans_id
615 LEFT JOIN chart c ON (at.chart_id = c.id)
616 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
617 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
620 qq|SELECT at.acc_trans_id
622 LEFT JOIN chart c ON (at.chart_id = c.id)
624 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
625 ORDER BY at.acc_trans_id
627 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
629 if (@delete_acc_trans_ids) {
630 $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
631 do_query($form, $dbh, $query);
634 $main::lxdebug->leave_sub();
638 $main::lxdebug->enter_sub();
640 my ($self, $myconfig, $form, $locale) = @_;
642 # connect to database, turn off autocommit
643 my $dbh = $form->dbconnect_noauto($myconfig);
645 my (%payments, $old_form, $row, $item, $query, %keep_vars);
647 $old_form = save_form();
649 # Delete all entries in acc_trans from prior payments.
650 if (SL::DB::Default->get->payments_changeable != 0) {
651 $self->_delete_payments($form, $dbh);
654 # Save the new payments the user made before cleaning up $form.
655 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$';
656 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
658 # Clean up $form so that old content won't tamper the results.
659 %keep_vars = map { $_, 1 } qw(login password id);
660 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
662 # Retrieve the invoice from the database.
663 $form->create_links('AP', $myconfig, 'vendor', $dbh);
665 # Restore the payment options from the user input.
666 map { $form->{$_} = $payments{$_} } keys %payments;
668 # Set up the content of $form in the way that AR::post_transaction() expects.
670 $self->setup_form($form, 1);
672 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
673 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
679 LEFT JOIN chart c ON (at.chart_id = c.id)
681 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
682 ORDER BY at.acc_trans_id
685 ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
687 # Post the new payments.
688 $self->post_transaction($myconfig, $form, $dbh, 1);
690 restore_form($old_form);
692 my $rc = $dbh->commit();
695 $main::lxdebug->leave_sub();
701 $main::lxdebug->enter_sub();
703 my ($self, $form, $for_post_payments) = @_;
705 my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
706 $taxincluded, $tax, $diff);
709 $form->{forex} = $form->{exchangerate};
710 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
712 foreach $key (keys %{ $form->{AP_links} }) {
713 foreach $ref (@{ $form->{AP_links}{$key} }) {
714 if ($key eq "AP_paid") {
715 $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
717 $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
721 $form->{$key} = $form->{"select$key"};
726 # if there is a value we have an old entry
727 next unless $form->{acc_trans}{$key};
729 # do not use old entries for payments. They come from the form
730 # even if they are not changeable (then they are in hiddens)
731 next if $for_post_payments && $key eq "AP_paid";
733 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
735 if ($key eq "AP_paid") {
737 $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
738 $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
739 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
740 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
741 $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
742 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
743 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
745 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
746 $form->{"forex_$j"} = $form->{"exchangerate_$i"};
747 $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
748 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
749 $form->{paidaccounts}++;
755 if (($key eq "AP_tax") || ($key eq "AR_tax")) {
756 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
757 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
759 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
760 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
762 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
763 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
766 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
767 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
768 $totaltax += $form->{"tax_$index"};
772 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
774 if ($akey eq 'amount') {
776 $form->{"${akey}_$i"} *= -1;
777 $totalamount += $form->{"${akey}_$i"};
778 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
780 $form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
781 $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
782 $form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
785 $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
787 my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
788 $form->{"select${key}"} =~
790 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
792 $form->{acc_trans}{$key}->[$i-1]->{accno}
796 $form->{"${key}_$k"} = $1;
799 $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
801 } elsif ($akey eq 'amount') {
802 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
803 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
810 $form->{taxincluded} = $taxincluded if ($form->{id});
811 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
813 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
814 # add tax to amounts and invtotal
815 for $i (1 .. $form->{rowcount}) {
816 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
817 $tax = $form->round_amount($taxamount, 2);
818 $diff += ($taxamount - $tax);
819 $form->{"amount_$i"} += $form->{"tax_$i"};
822 $form->{amount_1} += $form->round_amount($diff, 2);
825 $taxamount = $form->round_amount($taxamount, 2);
826 $form->{invtotal} = $totalamount + $totaltax;
828 $main::lxdebug->leave_sub();
832 $main::lxdebug->enter_sub();
834 my ($self, $form, $myconfig, $id) = @_;
836 my ($query, $new_id, $storno_row, $acc_trans_rows);
837 my $dbh = $form->get_standard_dbh($myconfig);
839 $query = qq|SELECT nextval('glid')|;
840 ($new_id) = selectrow_query($form, $dbh, $query);
842 $query = qq|SELECT * FROM ap WHERE id = ?|;
843 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
845 $storno_row->{id} = $new_id;
846 $storno_row->{storno_id} = $id;
847 $storno_row->{storno} = 't';
848 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
849 $storno_row->{amount} *= -1;
850 $storno_row->{netamount} *= -1;
851 $storno_row->{paid} = $storno_row->{amount};
853 delete @$storno_row{qw(itime mtime)};
855 $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
856 do_query($form, $dbh, $query, (values %$storno_row));
858 $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
859 do_query($form, $dbh, $query, $id);
861 # now copy acc_trans entries
862 $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|;
863 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
865 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
866 while ($rowref->[-1]{link} =~ /paid/) {
867 splice(@$rowref, -2);
870 for my $row (@$rowref) {
871 delete @$row{qw(itime mtime link acc_trans_id)};
872 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
873 $row->{trans_id} = $new_id;
874 $row->{amount} *= -1;
875 do_query($form, $dbh, $query, (values %$row));
878 map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
882 $main::lxdebug->leave_sub();