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);
58 $form->{taxincluded} = 0 unless $form->{taxincluded};
60 ($null, $form->{department_id}) = split(/--/, $form->{department});
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];
74 ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
75 ($form->{AP_payables}) = split(/--/, $form->{APselected});
77 # calculate the totals while calculating and reformatting the $amount_$i and $tax_$i
78 ($form->{netamount},$form->{total_tax},$form->{invtotal}) = $form->calculate_arap('buy',$form->{taxincluded}, $form->{exchangerate});
80 # adjust paidaccounts if there is no date in the last row
81 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
86 for my $i (1 .. $form->{paidaccounts}) {
88 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
91 $form->{invpaid} += $form->{"paid_$i"};
92 $form->{datepaid} = $form->{"datepaid_$i"};
97 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
99 # # store invoice total, this goes into ap table
100 # $form->{invtotal} = $form->{netamount} + $form->{total_tax};
102 # amount for total AP
103 $form->{payables} = $form->{invtotal};
105 # update exchangerate
106 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
107 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
108 $form->{exchangerate});
111 my ($query, $sth, @values);
113 if (!$payments_only) {
114 # if we have an id delete old records
117 # delete detail records
118 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
119 do_query($form, $dbh, $query, $form->{id});
123 ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|);
126 qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | .
127 qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?),
128 (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|;
129 do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $form->{login}, $form->{currency}, $form->{vendor_id});
133 $query = qq|UPDATE ap SET invnumber = ?,
134 transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?,
135 amount = ?, duedate = ?, paid = ?, netamount = ?,
136 currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?,
137 globalproject_id = ?, direct_debit = ?
139 @values = ($form->{invnumber}, conv_date($form->{transdate}),
140 $form->{ordnumber}, conv_i($form->{vendor_id}),
141 $form->{taxincluded} ? 't' : 'f', $form->{invtotal},
142 conv_date($form->{duedate}), $form->{invpaid},
144 $form->{currency}, $form->{notes},
145 conv_i($form->{department_id}), $form->{storno},
146 $form->{storno_id}, conv_i($form->{globalproject_id}),
147 $form->{direct_debit} ? 't' : 'f',
149 do_query($form, $dbh, $query, @values);
151 # add individual transactions
152 for my $i (1 .. $form->{rowcount}) {
153 if ($form->{"amount_$i"} != 0) {
155 $project_id = conv_i($form->{"project_id_$i"});
157 # insert detail records in acc_trans
159 qq|INSERT INTO acc_trans | .
160 qq| (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link)| .
161 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
162 qq| ?, ?, ?, ?, ?,| .
163 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
164 @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
165 $form->{"amount_$i"}, conv_date($form->{transdate}),
166 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
167 $form->{AP_amounts}{"amount_$i"});
168 do_query($form, $dbh, $query, @values);
170 if ($form->{"tax_$i"} != 0) {
171 # insert detail records in acc_trans
173 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
174 qq| project_id, taxkey, tax_id, chart_link) | .
175 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
176 qq| ?, ?, ?, ?, ?,| .
177 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
178 @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
179 $form->{"tax_$i"}, conv_date($form->{transdate}),
180 $project_id, $form->{"taxkey_$i"}, conv_i($form->{"tax_id_$i"}),
181 $form->{AP_amounts}{"tax_$i"});
182 do_query($form, $dbh, $query, @values);
190 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) | .
191 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
192 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
193 qq| (SELECT tax_id| .
195 qq| WHERE chart_id= (SELECT id | .
197 qq| WHERE accno = ?)| .
198 qq| AND startdate <= ?| .
199 qq| ORDER BY startdate DESC LIMIT 1),| .
200 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
201 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
202 conv_date($form->{transdate}), $form->{AP_amounts}{payables}, $form->{AP_amounts}{payables}, conv_date($form->{transdate}),
203 $form->{AP_amounts}{payables});
204 do_query($form, $dbh, $query, @values);
207 # if there is no amount but a payment record a payable
208 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
209 $form->{payables} = $form->{invpaid};
212 # add paid transactions
213 for my $i (1 .. $form->{paidaccounts}) {
215 if ($form->{"acc_trans_id_$i"} && $payments_only && (SL::DB::Default->get->payments_changeable == 0)) {
219 if ($form->{"paid_$i"} != 0) {
220 my $project_id = conv_i($form->{"paid_project_id_$i"});
223 if ($form->{currency} eq $form->{defaultcurrency}) {
224 $form->{"exchangerate_$i"} = 1;
226 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
227 $form->{"exchangerate_$i"} = $exchangerate || $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
229 $form->{"AP_paid_$i"} =~ s/\"//g;
233 ($form->{"AP_paid_account_$i"}) = split(/--/, $form->{"AP_paid_$i"});
234 $form->{"datepaid_$i"} = $form->{transdate}
235 unless ($form->{"datepaid_$i"});
237 # if there is no amount and invtotal is zero there is no exchangerate
238 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
239 $form->{exchangerate} = $form->{"exchangerate_$i"};
243 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
245 if ($form->{payables}) {
247 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) | .
248 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
249 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
250 qq| (SELECT tax_id| .
252 qq| WHERE chart_id= (SELECT id | .
254 qq| WHERE accno = ?)| .
255 qq| AND startdate <= ?| .
256 qq| ORDER BY startdate DESC LIMIT 1),| .
257 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
258 @values = ($form->{id}, $form->{AP_payables}, $amount,
259 conv_date($form->{"datepaid_$i"}), $project_id,
260 $form->{AP_payables}, $form->{AP_payables}, conv_date($form->{"datepaid_$i"}),
261 $form->{AP_payables});
262 do_query($form, $dbh, $query, @values);
264 $form->{payables} = $amount;
267 my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig));
269 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey, tax_id, chart_link) | .
270 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, | .
271 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
272 qq| (SELECT tax_id| .
274 qq| WHERE chart_id= (SELECT id | .
276 qq| WHERE accno = ?)| .
277 qq| AND startdate <= ?| .
278 qq| ORDER BY startdate DESC LIMIT 1),| .
279 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
280 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $form->{"paid_$i"},
281 conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"},
282 $form->{"memo_$i"}, $project_id, $form->{"AP_paid_account_$i"},
283 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
284 $form->{"AP_paid_account_$i"});
285 do_query($form, $dbh, $query, @values);
287 # add exchange rate difference
289 $form->round_amount($form->{"paid_$i"} *
290 ($form->{"exchangerate_$i"} - 1), 2);
293 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
294 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
295 qq| (SELECT taxkey_id FROM chart WHERE accno = ?), | .
296 qq| (SELECT tax_id| .
298 qq| WHERE chart_id= (SELECT id | .
300 qq| WHERE accno = ?)| .
301 qq| AND startdate <= ?| .
302 qq| ORDER BY startdate DESC LIMIT 1),| .
303 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
304 @values = ($form->{id}, $form->{"AP_paid_account_$i"}, $amount,
305 conv_date($form->{"datepaid_$i"}), $project_id,
306 $form->{"AP_paid_account_$i"},
307 $form->{"AP_paid_account_$i"}, conv_date($form->{"datepaid_$i"}),
308 $form->{"AP_paid_account_$i"});
309 do_query($form, $dbh, $query, @values);
312 # exchangerate gain/loss
314 $form->round_amount($form->{"paid_$i"} *
315 ($form->{exchangerate} -
316 $form->{"exchangerate_$i"}), 2);
320 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) | .
321 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
322 qq| (SELECT taxkey_id FROM chart WHERE accno = ?),| .
323 qq| (SELECT tax_id| .
325 qq| WHERE chart_id= (SELECT id | .
327 qq| WHERE accno = ?)| .
328 qq| AND startdate <= ?| .
329 qq| ORDER BY startdate DESC LIMIT 1),| .
330 qq| (SELECT c.link FROM chart c WHERE c.accno = ?))|;
331 @values = ($form->{id},
332 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
333 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
334 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno},
335 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno}, conv_date($form->{"datepaid_$i"}),
336 ($amount > 0) ? $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 # safety check datev export
357 if ($::instance_conf->get_datev_check_on_ap_transaction) {
358 my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef;
359 $transdate ||= DateTime->today;
361 my $datev = SL::DATEV->new(
362 exporttype => DATEV_ET_BUCHUNGEN,
363 format => DATEV_FORMAT_KNE,
367 trans_id => $form->{id},
372 if ($datev->errors) {
374 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
378 if (!$provided_dbh) {
383 $rc = 1; # Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
385 $main::lxdebug->leave_sub();
390 sub delete_transaction {
391 $main::lxdebug->enter_sub();
393 my ($self, $myconfig, $form) = @_;
395 # connect to database
396 my $dbh = $form->dbconnect_noauto($myconfig);
398 # acc_trans entries are deleted by database triggers.
399 my $query = qq|DELETE FROM ap WHERE id = ?|;
400 do_query($form, $dbh, $query, $form->{id});
402 my $rc = $dbh->commit;
405 $main::lxdebug->leave_sub();
410 sub ap_transactions {
411 $main::lxdebug->enter_sub();
413 my ($self, $myconfig, $form) = @_;
415 # connect to database
416 my $dbh = $form->get_standard_dbh($myconfig);
419 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
420 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
421 qq| a.globalproject_id, a.storno, a.storno_id, | .
422 qq| pr.projectnumber AS globalprojectnumber, | .
423 qq| e.name AS employee, | .
424 qq| v.vendornumber, v.country, v.ustid, | .
425 qq| tz.description AS taxzone, | .
426 qq| pt.description AS payment_terms, | .
427 qq{ ( SELECT ch.accno || ' -- ' || ch.description
429 LEFT JOIN chart ch ON ch.id = at.chart_id
430 WHERE ch.link ~ 'AP[[:>:]]'
431 AND at.trans_id = a.id
435 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
436 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
437 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
438 qq|LEFT JOIN tax_zones tz ON (tz.id = v.taxzone_id)| .
439 qq|LEFT JOIN payment_terms pt ON (pt.id = v.payment_id)|;
443 unless ( $::auth->assert('show_ap_transactions', 1) ) {
444 $where .= " AND NOT invoice = 'f' "; # remove ap transactions from Sales -> Reports -> Invoices
449 if ($form->{vendor_id}) {
450 $where .= " AND a.vendor_id = ?";
451 push(@values, $form->{vendor_id});
452 } elsif ($form->{vendor}) {
453 $where .= " AND v.name ILIKE ?";
454 push(@values, $form->like($form->{vendor}));
456 if ($form->{department}) {
457 # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
458 # wird so nicht mehr als zeichenkette zusammengebaut
459 # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
460 #my ($null, $department_id) = split /--/, $form->{department};
461 $where .= " AND a.department_id = ?";
462 push(@values, $form->{department});
464 if ($form->{invnumber}) {
465 $where .= " AND a.invnumber ILIKE ?";
466 push(@values, $form->like($form->{invnumber}));
468 if ($form->{ordnumber}) {
469 $where .= " AND a.ordnumber ILIKE ?";
470 push(@values, $form->like($form->{ordnumber}));
472 if ($form->{notes}) {
473 $where .= " AND lower(a.notes) LIKE ?";
474 push(@values, $form->like($form->{notes}));
476 if ($form->{project_id}) {
478 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
479 qq| (SELECT * FROM invoice i | .
480 qq| WHERE i.project_id = ? AND i.trans_id = a.id) | .
482 qq| (SELECT * FROM acc_trans at | .
483 qq| WHERE at.project_id = ? AND at.trans_id = a.id)| .
485 push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id});
488 if ($form->{transdatefrom}) {
489 $where .= " AND a.transdate >= ?";
490 push(@values, $form->{transdatefrom});
492 if ($form->{transdateto}) {
493 $where .= " AND a.transdate <= ?";
494 push(@values, $form->{transdateto});
496 if ($form->{open} || $form->{closed}) {
497 unless ($form->{open} && $form->{closed}) {
498 $where .= " AND a.amount <> a.paid" if ($form->{open});
499 $where .= " AND a.amount = a.paid" if ($form->{closed});
504 substr($where, 0, 4, " WHERE ");
508 my @a = qw(transdate invnumber name);
509 push @a, "employee" if $form->{l_employee};
510 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
511 my $sortorder = join(', ', map { "$_ $sortdir" } @a);
513 if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
514 $sortorder = $form->{sort} . " $sortdir";
517 $query .= " ORDER BY $sortorder";
519 my @result = selectall_hashref_query($form, $dbh, $query, @values);
521 $form->{AP} = [ @result ];
523 $main::lxdebug->leave_sub();
527 $main::lxdebug->enter_sub();
529 my ($self, $myconfig, $form) = @_;
531 # connect to database
532 my $dbh = $form->dbconnect($myconfig);
536 " (SELECT transdate FROM ap WHERE id = " .
537 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
539 ($form->{transdate}) = $dbh->selectrow_array($query);
543 $main::lxdebug->leave_sub();
546 sub _delete_payments {
547 $main::lxdebug->enter_sub();
549 my ($self, $form, $dbh) = @_;
551 my @delete_acc_trans_ids;
553 # Delete old payment entries from acc_trans.
555 qq|SELECT acc_trans_id
557 WHERE (trans_id = ?) AND fx_transaction
561 SELECT at.acc_trans_id
563 LEFT JOIN chart c ON (at.chart_id = c.id)
564 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
565 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
568 qq|SELECT at.acc_trans_id
570 LEFT JOIN chart c ON (at.chart_id = c.id)
572 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
573 ORDER BY at.acc_trans_id
575 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
577 if (@delete_acc_trans_ids) {
578 $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
579 do_query($form, $dbh, $query);
582 $main::lxdebug->leave_sub();
586 $main::lxdebug->enter_sub();
588 my ($self, $myconfig, $form, $locale) = @_;
590 # connect to database, turn off autocommit
591 my $dbh = $form->dbconnect_noauto($myconfig);
593 my (%payments, $old_form, $row, $item, $query, %keep_vars);
595 $old_form = save_form();
597 # Delete all entries in acc_trans from prior payments.
598 if (SL::DB::Default->get->payments_changeable != 0) {
599 $self->_delete_payments($form, $dbh);
602 # Save the new payments the user made before cleaning up $form.
603 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$';
604 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
606 # Clean up $form so that old content won't tamper the results.
607 %keep_vars = map { $_, 1 } qw(login password id);
608 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
610 # Retrieve the invoice from the database.
611 $form->create_links('AP', $myconfig, 'vendor', $dbh);
613 # Restore the payment options from the user input.
614 map { $form->{$_} = $payments{$_} } keys %payments;
616 # Set up the content of $form in the way that AR::post_transaction() expects.
618 $self->setup_form($form, 1);
620 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
621 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
627 LEFT JOIN chart c ON (at.chart_id = c.id)
629 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
630 ORDER BY at.acc_trans_id
633 ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
635 # Post the new payments.
636 $self->post_transaction($myconfig, $form, $dbh, 1);
638 restore_form($old_form);
640 my $rc = $dbh->commit();
643 $main::lxdebug->leave_sub();
649 $main::lxdebug->enter_sub();
651 my ($self, $form, $for_post_payments) = @_;
653 my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
657 $form->{forex} = $form->{exchangerate};
658 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
660 foreach $key (keys %{ $form->{AP_links} }) {
661 foreach $ref (@{ $form->{AP_links}{$key} }) {
662 if ($key eq "AP_paid") {
663 $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
665 $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
669 $form->{$key} = $form->{"select$key"};
674 # if there is a value we have an old entry
675 next unless $form->{acc_trans}{$key};
677 # do not use old entries for payments. They come from the form
678 # even if they are not changeable (then they are in hiddens)
679 next if $for_post_payments && $key eq "AP_paid";
681 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
683 if ($key eq "AP_paid") {
685 $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
686 $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
687 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
688 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
689 $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
690 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
691 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
693 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
694 $form->{"forex_$j"} = $form->{"exchangerate_$i"};
695 $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
696 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
697 $form->{paidaccounts}++;
703 if (($key eq "AP_tax") || ($key eq "AR_tax")) {
704 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
705 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
707 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
708 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
710 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
711 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
714 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
715 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
716 $totaltax += $form->{"tax_$index"};
720 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
722 if ($akey eq 'amount') {
724 $form->{"${akey}_$i"} *= -1;
725 $totalamount += $form->{"${akey}_$i"};
726 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
728 $form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
729 $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
730 $form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
733 $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
735 my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
736 $form->{"select${key}"} =~
738 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
740 $form->{acc_trans}{$key}->[$i-1]->{accno}
744 $form->{"${key}_$k"} = $1;
747 $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
749 } elsif ($akey eq 'amount') {
750 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
751 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
758 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
760 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
761 # add tax to amounts and invtotal
762 for $i (1 .. $form->{rowcount}) {
763 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
764 $tax = $form->round_amount($taxamount, 2);
765 $diff += ($taxamount - $tax);
766 $form->{"amount_$i"} += $form->{"tax_$i"};
769 $form->{amount_1} += $form->round_amount($diff, 2);
772 $taxamount = $form->round_amount($taxamount, 2);
773 $form->{invtotal} = $totalamount + $totaltax;
775 $main::lxdebug->leave_sub();
779 $main::lxdebug->enter_sub();
781 my ($self, $form, $myconfig, $id) = @_;
783 my ($query, $new_id, $storno_row, $acc_trans_rows);
784 my $dbh = $form->get_standard_dbh($myconfig);
786 $query = qq|SELECT nextval('glid')|;
787 ($new_id) = selectrow_query($form, $dbh, $query);
789 $query = qq|SELECT * FROM ap WHERE id = ?|;
790 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
792 $storno_row->{id} = $new_id;
793 $storno_row->{storno_id} = $id;
794 $storno_row->{storno} = 't';
795 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
796 $storno_row->{amount} *= -1;
797 $storno_row->{netamount} *= -1;
798 $storno_row->{paid} = $storno_row->{amount};
800 delete @$storno_row{qw(itime mtime)};
802 $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
803 do_query($form, $dbh, $query, (values %$storno_row));
805 $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
806 do_query($form, $dbh, $query, $id);
808 # now copy acc_trans entries
809 $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|;
810 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
812 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
813 while ($rowref->[-1]{link} =~ /paid/) {
814 splice(@$rowref, -2);
817 for my $row (@$rowref) {
818 delete @$row{qw(itime mtime link acc_trans_id)};
819 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
820 $row->{trans_id} = $new_id;
821 $row->{amount} *= -1;
822 do_query($form, $dbh, $query, (values %$row));
825 map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
829 $main::lxdebug->leave_sub();