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 Receivable module backend routines
33 #======================================================================
41 our (%myconfig, $form);
43 sub post_transaction {
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
48 my ($query, $sth, $null, $taxrate, $amount, $tax);
54 my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
55 $form->{defaultcurrency} = $form->get_default_currency($myconfig);
58 $form->{exchangerate} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 :
59 ( $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy') ||
60 $form->parse_amount($myconfig, $form->{exchangerate}) );
62 # get the charts selected
63 map { ($form->{AR_amounts}{"amount_$_"}) = split /--/, $form->{"AR_amount_$_"} } 1 .. $form->{rowcount};
65 $form->{AR_amounts}{receivables} = $form->{ARselected};
66 $form->{AR}{receivables} = $form->{ARselected};
69 for $i (1 .. $form->{rowcount}) {
70 $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2);
71 $form->{amount} += $form->{"amount_$i"};
72 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"});
77 $form->{netamount} = 0;
78 $form->{total_tax} = 0;
80 # taxincluded doesn't make sense if there is no amount
81 $form->{taxincluded} = 0 unless $form->{amount};
83 for $i (1 .. $form->{rowcount}) {
84 ($form->{"tax_id_$i"}) = split /--/, $form->{"taxchart_$i"};
86 $query = qq|SELECT c.accno, t.taxkey, t.rate FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id = ? ORDER BY c.accno|;
87 ($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"});
89 $form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
90 $form->{AR_amounts}{"amounts_$i"}{taxkey} = $form->{"taxkey_$i"};
92 if ($form->{taxincluded} *= 1) {
93 $tax = $form->{"korrektur_$i"}
95 : $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1)); # should be same as taxrate * amount / (taxrate + 1)
96 $form->{"amount_$i"} = $form->round_amount($form->{"amount_$i"} - $tax, 2);
97 $form->{"tax_$i"} = $form->round_amount($tax, 2);
99 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"} unless $form->{"korrektur_$i"};
100 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
102 $form->{netamount} += $form->{"amount_$i"};
103 $form->{total_tax} += $form->{"tax_$i"};
106 # adjust paidaccounts if there is no date in the last row
107 # this does not apply to stornos, where the paid field is set manually
108 unless ($form->{storno}) {
109 $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
113 for $i (1 .. $form->{paidaccounts}) {
114 $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
115 $form->{paid} += $form->{"paid_$i"};
116 $form->{datepaid} = $form->{"datepaid_$i"};
119 $form->{amount} = $form->{netamount} + $form->{total_tax};
121 $form->{paid} = $form->round_amount($form->{paid} * ($form->{exchangerate} || 1), 2);
123 ($null, $form->{employee_id}) = split /--/, $form->{employee};
125 $form->get_employee($dbh) unless $form->{employee_id};
127 # if we have an id delete old records else make one
128 if (!$payments_only) {
130 # delete detail records
131 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
132 do_query($form, $dbh, $query, $form->{id});
135 $query = qq|SELECT nextval('glid')|;
136 ($form->{id}) = selectrow_query($form, $dbh, $query);
137 $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|;
138 do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
139 $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber};
144 ($null, $form->{department_id}) = split(/--/, $form->{department});
145 $form->{department_id} *= 1;
147 # record last payment date in ar table
148 $form->{datepaid} ||= $form->{transdate} ;
149 my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef;
151 # amount for AR account
152 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
154 # update exchangerate
155 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0)
156 if ($form->{currency} ne $form->{defaultcurrency}) && !$form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
158 if (!$payments_only) {
161 invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?,
162 taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?,
163 netamount = ?, curr = ?, notes = ?, department_id = ?,
164 employee_id = ?, storno = ?, storno_id = ?
166 my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount},
167 conv_date($form->{duedate}), $form->{paid}, conv_date($datepaid), $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}),
168 conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{id}));
169 do_query($form, $dbh, $query, @values);
171 # add individual transactions for AR, amount and taxes
172 for $i (1 .. $form->{rowcount}) {
173 if ($form->{"amount_$i"} != 0) {
174 my $project_id = conv_i($form->{"project_id_$i"});
175 $taxkey = $form->{AR_amounts}{"amounts_$i"}{taxkey};
177 # insert detail records in acc_trans
178 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
179 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
180 @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}), conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey));
181 do_query($form, $dbh, $query, @values);
183 if ($form->{"tax_$i"} != 0) {
184 # insert detail records in acc_trans
185 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
186 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
187 @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}), conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id, conv_i($taxkey));
188 do_query($form, $dbh, $query, @values);
194 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey)
195 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
196 @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), $form->{AR_amounts}{receivables});
197 do_query($form, $dbh, $query, @values);
200 # add paid transactions
201 for my $i (1 .. $form->{paidaccounts}) {
202 if ($form->{"paid_$i"} != 0) {
203 my $project_id = conv_i($form->{"paid_project_id_$i"});
205 $form->{"AR_paid_$i"} =~ s/\"//g;
206 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
207 $form->{"datepaid_$i"} = $form->{transdate}
208 unless ($form->{"datepaid_$i"});
210 $form->{"exchangerate_$i"} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 :
211 ( $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy') ||
212 $form->parse_amount($myconfig, $form->{"exchangerate_$i"}) );
214 # if there is no amount and invtotal is zero there is no exchangerate
215 $form->{exchangerate} = $form->{"exchangerate_$i"}
216 if ($form->{amount} == 0 && $form->{netamount} == 0);
219 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
223 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
224 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
225 @values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables});
226 do_query($form, $dbh, $query, @values);
229 if ($form->{"paid_$i"} != 0) {
230 my $project_id = conv_i($form->{"paid_project_id_$i"});
232 $amount = $form->{"paid_$i"} * -1;
233 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey)
234 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
235 @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"});
236 do_query($form, $dbh, $query, @values);
238 # exchangerate difference for payment
239 $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1, 2);
242 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey)
243 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
244 @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{"paid_$i"});
245 do_query($form, $dbh, $query, @values);
248 # exchangerate gain/loss
249 $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 2);
252 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
253 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey)
254 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
255 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $accno);
256 do_query($form, $dbh, $query, @values);
260 # update exchangerate record
261 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0)
262 if ($form->{currency} ne $form->{defaultcurrency}) && !$form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
267 if (!$provided_dbh) {
268 $rc = $dbh->commit();
272 $main::lxdebug->leave_sub() and return $rc;
275 sub _delete_payments {
276 $main::lxdebug->enter_sub();
278 my ($self, $form, $dbh) = @_;
282 # Delete old payment entries from acc_trans.
286 WHERE (trans_id = ?) AND fx_transaction
292 LEFT JOIN chart c ON (at.chart_id = c.id)
293 WHERE (trans_id = ?) AND (c.link LIKE '%AR_paid%')|;
294 push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}), conv_i($form->{id}));
299 LEFT JOIN chart c ON (at.chart_id = c.id)
301 AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
304 push @delete_oids, selectall_array_query($form, $dbh, $query, conv_i($form->{id}));
307 $query = qq|DELETE FROM acc_trans WHERE oid IN (| . join(", ", @delete_oids) . qq|)|;
308 do_query($form, $dbh, $query);
311 $main::lxdebug->leave_sub();
315 $main::lxdebug->enter_sub();
317 my ($self, $myconfig, $form, $locale) = @_;
319 # connect to database, turn off autocommit
320 my $dbh = $form->dbconnect_noauto($myconfig);
322 my (%payments, $old_form, $row, $item, $query, %keep_vars);
324 $old_form = save_form();
326 # Delete all entries in acc_trans from prior payments.
327 $self->_delete_payments($form, $dbh);
329 # Save the new payments the user made before cleaning up $form.
330 my $payments_re = '^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AR_paid_\d+$|^paidaccounts$';
331 map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form };
333 # Clean up $form so that old content won't tamper the results.
334 %keep_vars = map { $_, 1 } qw(login password id);
335 map { delete $form->{$_} unless $keep_vars{$_} } keys %{ $form };
337 # Retrieve the invoice from the database.
338 $form->create_links('AR', $myconfig, 'customer', $dbh);
340 # Restore the payment options from the user input.
341 map { $form->{$_} = $payments{$_} } keys %payments;
343 # Set up the content of $form in the way that AR::post_transaction() expects.
345 $self->setup_form($form);
347 ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|);
348 $form->{defaultcurrency} = (split m/:/, $form->{defaultcurrency})[0];
350 $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate});
352 # Get the AR accno (which is normally done by Form::create_links()).
356 LEFT JOIN chart c ON (at.chart_id = c.id)
358 AND ((c.link = 'AR') OR (c.link LIKE '%:AR') OR (c.link LIKE 'AR:%'))
362 ($form->{ARselected}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id}));
364 # Post the new payments.
365 $self->post_transaction($myconfig, $form, $dbh, 1);
367 restore_form($old_form);
369 my $rc = $dbh->commit();
372 $main::lxdebug->leave_sub();
377 sub delete_transaction {
378 $main::lxdebug->enter_sub();
380 my ($self, $myconfig, $form) = @_;
382 # connect to database, turn AutoCommit off
383 my $dbh = $form->dbconnect_noauto($myconfig);
385 my $query = qq|DELETE FROM ar WHERE id = ?|;
386 do_query($form, $dbh, $query, $form->{id});
388 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
389 do_query($form, $dbh, $query, $form->{id});
392 my $rc = $dbh->commit;
395 $main::lxdebug->leave_sub();
400 sub ar_transactions {
401 $main::lxdebug->enter_sub();
403 my ($self, $myconfig, $form) = @_;
405 # connect to database
406 my $dbh = $form->dbconnect($myconfig);
411 qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate, | .
412 qq| a.duedate, a.netamount, a.amount, a.paid, | .
413 qq| a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | .
414 qq| a.shippingpoint, a.storno, a.globalproject_id, | .
415 qq| a.transaction_description, | .
416 qq| pr.projectnumber AS globalprojectnumber, | .
418 qq| e.name AS employee | .
420 qq|JOIN customer c ON (a.customer_id = c.id) | .
421 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
422 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)|;
425 if ($form->{customer_id}) {
426 $where .= " AND a.customer_id = ?";
427 push(@values, $form->{customer_id});
428 } elsif ($form->{customer}) {
429 $where .= " AND c.name ILIKE ?";
430 push(@values, $form->like($form->{customer}));
432 if ($form->{department}) {
433 my ($null, $department_id) = split /--/, $form->{department};
434 $where .= " AND a.department_id = ?";
435 push(@values, $department_id);
437 foreach my $column (qw(invnumber ordnumber notes transaction_description)) {
438 if ($form->{$column}) {
439 $where .= " AND a.$column ILIKE ?";
440 push(@values, $form->like($form->{$column}));
443 if ($form->{"project_id"}) {
445 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
446 qq| (SELECT * FROM invoice i | .
447 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
448 push(@values, $form->{"project_id"}, $form->{"project_id"});
451 if ($form->{transdatefrom}) {
452 $where .= " AND a.transdate >= ?";
453 push(@values, $form->{transdatefrom});
455 if ($form->{transdateto}) {
456 $where .= " AND a.transdate <= ?";
457 push(@values, $form->{transdateto});
459 if ($form->{open} || $form->{closed}) {
460 unless ($form->{open} && $form->{closed}) {
461 $where .= " AND a.amount <> a.paid" if ($form->{open});
462 $where .= " AND a.amount = a.paid" if ($form->{closed});
466 my @a = (transdate, invnumber, name);
467 push @a, "employee" if $form->{l_employee};
468 my $sortorder = join(', ', @a);
470 if (grep({ $_ eq $form->{sort} }
471 qw(id transdate duedate invnumber ordnumber name
472 datepaid employee shippingpoint shipvia))) {
473 $sortorder = $form->{sort};
476 $query .= " WHERE $where ORDER by $sortorder";
478 my $sth = $dbh->prepare($query);
479 $sth->execute(@values) ||
480 $form->dberror($query . " (" . join(", ", @values) . ")");
483 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
484 push @{ $form->{AR} }, $ar;
490 $main::lxdebug->leave_sub();
494 $main::lxdebug->enter_sub();
496 my ($self, $myconfig, $form) = @_;
498 # connect to database
499 my $dbh = $form->dbconnect($myconfig);
503 " (SELECT transdate FROM ar WHERE id = " .
504 " (SELECT MAX(id) FROM ar) LIMIT 1), " .
506 ($form->{transdate}) = $dbh->selectrow_array($query);
510 $main::lxdebug->leave_sub();
514 $main::lxdebug->enter_sub();
516 my ($self, $form) = @_;
518 my ($exchangerate, $key, $akey, $i, $j, $k, $index, $taxamount, $totaltax, $taxrate, $diff);
521 $form->{forex} = $form->{exchangerate};
522 $exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1;
524 foreach $key (keys %{ $form->{AR_links} }) {
525 # if there is a value we have an old entry
529 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
530 if ($key eq "AR_paid") {
532 $form->{"AR_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
535 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
536 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
537 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
538 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
539 $form->{"forex_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
540 $form->{"exchangerate_$i"} = $form->{"forex_$j"};
541 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
542 $form->{paidaccounts}++;
549 if ($key eq "AR_tax" || $key eq "AP_tax") {
550 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
551 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
553 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
554 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
555 $taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
558 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
559 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
562 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
563 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
564 $totaltax += $form->{"tax_$index"};
568 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
570 if ($akey eq 'amount') {
572 $totalamount += $form->{"${akey}_$i"};
574 $form->{"oldprojectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
575 $form->{"projectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
576 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
577 $form->{"project_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{project_id};
580 $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
583 $form->{ARselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
585 } elsif ($akey eq "amount") {
586 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
587 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
594 $form->{taxincluded} = $taxincluded if ($form->{id});
595 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
597 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
599 # add tax to amounts and invtotal
600 for $i (1 .. $form->{rowcount}) {
601 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
602 $tax = $form->round_amount($taxamount, 2);
603 $diff += ($taxamount - $tax);
604 $form->{"amount_$i"} += $form->{"tax_$i"};
606 $form->{amount_1} += $form->round_amount($diff, 2);
609 $taxamount = $form->round_amount($taxamount, 2);
610 $form->{tax} = $taxamount;
612 $form->{invtotal} = $totalamount + $totaltax;
614 $main::lxdebug->leave_sub();
618 $main::lxdebug->enter_sub();
620 my ($self, $form, $myconfig, $id) = @_;
622 my ($query, $new_id, $storno_row, $acc_trans_rows);
623 my $dbh = $form->get_standard_dbh($myconfig);
625 $query = qq|SELECT nextval('glid')|;
626 ($new_id) = selectrow_query($form, $dbh, $query);
628 $query = qq|SELECT * FROM ar WHERE id = ?|;
629 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
631 $storno_row->{id} = $new_id;
632 $storno_row->{storno_id} = $id;
633 $storno_row->{storno} = 't';
634 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
635 $storno_row->{amount} *= -1;
636 $storno_row->{netamount} *= -1;
637 $storno_row->{paid} = $storno_amount->{amount};
639 delete @$storno_row{qw(itime mtime)};
641 $query = sprintf 'INSERT INTO ar (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
642 do_query($form, $dbh, $query, (values %$storno_row));
644 $query = qq|UPDATE ar SET paid = amount + paid, storno = 't' WHERE id = ?|;
645 do_query($form, $dbh, $query, $id);
647 # now copy acc_trans entries
648 $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.oid|;
649 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
651 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
652 while ($rowref->[-1]{link} =~ /paid/) {
653 splice(@$rowref, -2);
656 for my $row (@$rowref) {
657 delete @$row{qw(itime mtime link)};
658 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
659 $row->{trans_id} = $new_id;
660 $row->{amount} *= -1;
661 do_query($form, $dbh, $query, (values %$row));
666 $main::lxdebug->leave_sub();