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}, $::myconfig{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,
365 trans_id => $form->{id},
370 if ($datev->errors) {
372 die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
376 if (!$provided_dbh) {
381 $rc = 1; # Den return-code auf true setzen, aber nur falls beim commit alles i.O. ist
383 $main::lxdebug->leave_sub();
388 sub delete_transaction {
389 $main::lxdebug->enter_sub();
391 my ($self, $myconfig, $form) = @_;
393 # connect to database
394 my $dbh = $form->dbconnect_noauto($myconfig);
396 # acc_trans entries are deleted by database triggers.
397 my $query = qq|DELETE FROM ap WHERE id = ?|;
398 do_query($form, $dbh, $query, $form->{id});
400 my $rc = $dbh->commit;
403 $main::lxdebug->leave_sub();
408 sub ap_transactions {
409 $main::lxdebug->enter_sub();
411 my ($self, $myconfig, $form) = @_;
413 # connect to database
414 my $dbh = $form->get_standard_dbh($myconfig);
417 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
418 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
419 qq| a.globalproject_id, a.storno, a.storno_id, | .
420 qq| pr.projectnumber AS globalprojectnumber, | .
421 qq| e.name AS employee, | .
422 qq| v.vendornumber, v.country, v.ustid, | .
423 qq| tz.description AS taxzone, | .
424 qq| pt.description AS payment_terms, | .
425 qq{ ( SELECT ch.accno || ' -- ' || ch.description
427 LEFT JOIN chart ch ON ch.id = at.chart_id
428 WHERE ch.link ~ 'AP[[:>:]]'
429 AND at.trans_id = a.id
433 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
434 qq|LEFT JOIN contacts cp ON (a.cp_id = cp.cp_id) | .
435 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
436 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) | .
437 qq|LEFT JOIN tax_zones tz ON (tz.id = a.taxzone_id)| .
438 qq|LEFT JOIN payment_terms pt ON (pt.id = a.payment_id)|;
442 unless ( $::auth->assert('show_ap_transactions', 1) ) {
443 $where .= " AND NOT invoice = 'f' "; # remove ap transactions from Sales -> Reports -> Invoices
448 if ($form->{vendor_id}) {
449 $where .= " AND a.vendor_id = ?";
450 push(@values, $form->{vendor_id});
451 } elsif ($form->{vendor}) {
452 $where .= " AND v.name ILIKE ?";
453 push(@values, $form->like($form->{vendor}));
455 if ($form->{"cp_name"}) {
456 $where .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
457 push(@values, ('%' . $form->{"cp_name"} . '%')x2);
459 if ($form->{department}) {
460 # ähnlich wie commit 0bbfb33b6aa8e38bb6c81d1684ab7d08e5b5c5af abteilung
461 # wird so nicht mehr als zeichenkette zusammengebaut
462 # hätte zu ee9f9f9aa4c3b9d5d20ab10a45c12bcaa6aa78d0 auffallen können ;-) jan
463 #my ($null, $department_id) = split /--/, $form->{department};
464 $where .= " AND a.department_id = ?";
465 push(@values, $form->{department});
467 if ($form->{invnumber}) {
468 $where .= " AND a.invnumber ILIKE ?";
469 push(@values, $form->like($form->{invnumber}));
471 if ($form->{ordnumber}) {
472 $where .= " AND a.ordnumber ILIKE ?";
473 push(@values, $form->like($form->{ordnumber}));
475 if ($form->{notes}) {
476 $where .= " AND lower(a.notes) LIKE ?";
477 push(@values, $form->like($form->{notes}));
479 if ($form->{project_id}) {
481 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
482 qq| (SELECT * FROM invoice i | .
483 qq| WHERE i.project_id = ? AND i.trans_id = a.id) | .
485 qq| (SELECT * FROM acc_trans at | .
486 qq| WHERE at.project_id = ? AND at.trans_id = a.id)| .
488 push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id});
491 if ($form->{transdatefrom}) {
492 $where .= " AND a.transdate >= ?";
493 push(@values, $form->{transdatefrom});
495 if ($form->{transdateto}) {
496 $where .= " AND a.transdate <= ?";
497 push(@values, $form->{transdateto});
499 if ($form->{open} || $form->{closed}) {
500 unless ($form->{open} && $form->{closed}) {
501 $where .= " AND a.amount <> a.paid" if ($form->{open});
502 $where .= " AND a.amount = a.paid" if ($form->{closed});
507 substr($where, 0, 4, " WHERE ");
511 my @a = qw(transdate invnumber name);
512 push @a, "employee" if $form->{l_employee};
513 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
514 my $sortorder = join(', ', map { "$_ $sortdir" } @a);
516 if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description))) {
517 $sortorder = $form->{sort} . " $sortdir";
520 $query .= " ORDER BY $sortorder";
522 my @result = selectall_hashref_query($form, $dbh, $query, @values);
524 $form->{AP} = [ @result ];
526 $main::lxdebug->leave_sub();
530 $main::lxdebug->enter_sub();
532 my ($self, $myconfig, $form) = @_;
534 # connect to database
535 my $dbh = $form->dbconnect($myconfig);
539 " (SELECT transdate FROM ap WHERE id = " .
540 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
542 ($form->{transdate}) = $dbh->selectrow_array($query);
546 $main::lxdebug->leave_sub();
549 sub _delete_payments {
550 $main::lxdebug->enter_sub();
552 my ($self, $form, $dbh) = @_;
554 my @delete_acc_trans_ids;
556 # Delete old payment entries from acc_trans.
558 qq|SELECT acc_trans_id
560 WHERE (trans_id = ?) AND fx_transaction
564 SELECT at.acc_trans_id
566 LEFT JOIN chart c ON (at.chart_id = c.id)
567 WHERE (trans_id = ?) AND (c.link LIKE '%AP_paid%')|;
568 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
571 qq|SELECT at.acc_trans_id
573 LEFT JOIN chart c ON (at.chart_id = c.id)
575 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
576 ORDER BY at.acc_trans_id
578 push @delete_acc_trans_ids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
580 if (@delete_acc_trans_ids) {
581 $query = qq|DELETE FROM acc_trans WHERE acc_trans_id IN (| . join(", ", @delete_acc_trans_ids) . qq|)|;
582 do_query($form, $dbh, $query);
585 $main::lxdebug->leave_sub();
589 $main::lxdebug->enter_sub();
591 my ($self, $myconfig, $form, $locale) = @_;
593 # connect to database, turn off autocommit
594 my $dbh = $form->dbconnect_noauto($myconfig);
596 my (%payments, $old_form, $row, $item, $query, %keep_vars);
598 $old_form = save_form();
600 # Delete all entries in acc_trans from prior payments.
601 if (SL::DB::Default->get->payments_changeable != 0) {
602 $self->_delete_payments($form, $dbh);
605 # Save the new payments the user made before cleaning up $form.
606 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$';
607 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
609 # Clean up $form so that old content won't tamper the results.
610 %keep_vars = map { $_, 1 } qw(login password id);
611 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
613 # Retrieve the invoice from the database.
614 $form->create_links('AP', $myconfig, 'vendor', $dbh);
616 # Restore the payment options from the user input.
617 map { $form->{$_} = $payments{$_} } keys %payments;
619 # Set up the content of $form in the way that AR::post_transaction() expects.
621 $self->setup_form($form, 1);
623 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
624 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
630 LEFT JOIN chart c ON (at.chart_id = c.id)
632 AND ((c.link = 'AP') OR (c.link LIKE '%:AP') OR (c.link LIKE 'AP:%'))
633 ORDER BY at.acc_trans_id
636 ($form->{APselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
638 # Post the new payments.
639 $self->post_transaction($myconfig, $form, $dbh, 1);
641 restore_form($old_form);
643 my $rc = $dbh->commit();
646 $main::lxdebug->leave_sub();
652 $main::lxdebug->enter_sub();
654 my ($self, $form, $for_post_payments) = @_;
656 my ($exchangerate, $i, $j, $k, $key, $akey, $ref, $index, $taxamount, $totalamount, $totaltax, $totalwithholding, $withholdingrate,
660 $form->{forex} = $form->{exchangerate};
661 $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
663 foreach $key (keys %{ $form->{AP_links} }) {
664 foreach $ref (@{ $form->{AP_links}{$key} }) {
665 if ($key eq "AP_paid") {
666 $form->{"select$key"} .= "<option value=\"$ref->{accno}\">$ref->{accno}--$ref->{description}</option>\n";
668 $form->{"select$key"} .= "<option value=\"$ref->{accno}--$ref->{tax_id}\">$ref->{accno}--$ref->{description}</option>\n";
672 $form->{$key} = $form->{"select$key"};
677 # if there is a value we have an old entry
678 next unless $form->{acc_trans}{$key};
680 # do not use old entries for payments. They come from the form
681 # even if they are not changeable (then they are in hiddens)
682 next if $for_post_payments && $key eq "AP_paid";
684 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
686 if ($key eq "AP_paid") {
688 $form->{"AP_paid_$j"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
689 $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id};
690 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
691 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
692 $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate};
693 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
694 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
696 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
697 $form->{"forex_$j"} = $form->{"exchangerate_$i"};
698 $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
699 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
700 $form->{paidaccounts}++;
706 if (($key eq "AP_tax") || ($key eq "AR_tax")) {
707 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
708 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
710 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
711 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
713 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
714 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
717 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
718 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
719 $totaltax += $form->{"tax_$index"};
723 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
725 if ($akey eq 'amount') {
727 $form->{"${akey}_$i"} *= -1;
728 $totalamount += $form->{"${akey}_$i"};
729 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
731 $form->{"projectnumber_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}";
732 $form->{"oldprojectnumber_$k"} = $form->{"projectnumber_$k"};
733 $form->{"project_id_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{project_id}";
736 $form->{"${key}_$k"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
738 my $q_description = quotemeta($form->{acc_trans}{$key}->[$i-1]->{description});
739 $form->{"select${key}"} =~
741 ($form->{acc_trans}{$key}->[$i-1]->{accno}--[^\"]*)
743 $form->{acc_trans}{$key}->[$i-1]->{accno}
747 $form->{"${key}_$k"} = $1;
750 $form->{APselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
752 } elsif ($akey eq 'amount') {
753 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
754 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
761 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
763 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
764 # add tax to amounts and invtotal
765 for $i (1 .. $form->{rowcount}) {
766 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
767 $tax = $form->round_amount($taxamount, 2);
768 $diff += ($taxamount - $tax);
769 $form->{"amount_$i"} += $form->{"tax_$i"};
772 $form->{amount_1} += $form->round_amount($diff, 2);
775 $taxamount = $form->round_amount($taxamount, 2);
776 $form->{invtotal} = $totalamount + $totaltax;
778 $main::lxdebug->leave_sub();
782 $main::lxdebug->enter_sub();
784 my ($self, $form, $myconfig, $id) = @_;
786 my ($query, $new_id, $storno_row, $acc_trans_rows);
787 my $dbh = $form->get_standard_dbh($myconfig);
789 $query = qq|SELECT nextval('glid')|;
790 ($new_id) = selectrow_query($form, $dbh, $query);
792 $query = qq|SELECT * FROM ap WHERE id = ?|;
793 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
795 $storno_row->{id} = $new_id;
796 $storno_row->{storno_id} = $id;
797 $storno_row->{storno} = 't';
798 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
799 $storno_row->{amount} *= -1;
800 $storno_row->{netamount} *= -1;
801 $storno_row->{paid} = $storno_row->{amount};
803 delete @$storno_row{qw(itime mtime)};
805 $query = sprintf 'INSERT INTO ap (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
806 do_query($form, $dbh, $query, (values %$storno_row));
808 $query = qq|UPDATE ap SET paid = amount + paid, storno = 't' WHERE id = ?|;
809 do_query($form, $dbh, $query, $id);
811 # now copy acc_trans entries
812 $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|;
813 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
815 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
816 while ($rowref->[-1]{link} =~ /paid/) {
817 splice(@$rowref, -2);
820 for my $row (@$rowref) {
821 delete @$row{qw(itime mtime link acc_trans_id)};
822 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
823 $row->{trans_id} = $new_id;
824 $row->{amount} *= -1;
825 do_query($form, $dbh, $query, (values %$row));
828 map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id);
832 $main::lxdebug->leave_sub();