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.marge_total, a.marge_percent, | .
416 qq| a.transaction_description, | .
417 qq| pr.projectnumber AS globalprojectnumber, | .
419 qq| e.name AS employee | .
421 qq|JOIN customer c ON (a.customer_id = c.id) | .
422 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
423 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)|;
426 if ($form->{customer_id}) {
427 $where .= " AND a.customer_id = ?";
428 push(@values, $form->{customer_id});
429 } elsif ($form->{customer}) {
430 $where .= " AND c.name ILIKE ?";
431 push(@values, $form->like($form->{customer}));
433 if ($form->{department}) {
434 my ($null, $department_id) = split /--/, $form->{department};
435 $where .= " AND a.department_id = ?";
436 push(@values, $department_id);
438 foreach my $column (qw(invnumber ordnumber notes transaction_description)) {
439 if ($form->{$column}) {
440 $where .= " AND a.$column ILIKE ?";
441 push(@values, $form->like($form->{$column}));
444 if ($form->{"project_id"}) {
446 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
447 qq| (SELECT * FROM invoice i | .
448 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
449 push(@values, $form->{"project_id"}, $form->{"project_id"});
452 if ($form->{transdatefrom}) {
453 $where .= " AND a.transdate >= ?";
454 push(@values, $form->{transdatefrom});
456 if ($form->{transdateto}) {
457 $where .= " AND a.transdate <= ?";
458 push(@values, $form->{transdateto});
460 if ($form->{open} || $form->{closed}) {
461 unless ($form->{open} && $form->{closed}) {
462 $where .= " AND a.amount <> a.paid" if ($form->{open});
463 $where .= " AND a.amount = a.paid" if ($form->{closed});
467 my @a = (transdate, invnumber, name);
468 push @a, "employee" if $form->{l_employee};
469 my $sortorder = join(', ', @a);
471 if (grep({ $_ eq $form->{sort} }
472 qw(id transdate duedate invnumber ordnumber name
473 datepaid employee shippingpoint shipvia))) {
474 $sortorder = $form->{sort};
477 $query .= " WHERE $where ORDER by $sortorder";
479 my $sth = $dbh->prepare($query);
480 $sth->execute(@values) ||
481 $form->dberror($query . " (" . join(", ", @values) . ")");
484 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
485 push @{ $form->{AR} }, $ar;
491 $main::lxdebug->leave_sub();
495 $main::lxdebug->enter_sub();
497 my ($self, $myconfig, $form) = @_;
499 # connect to database
500 my $dbh = $form->dbconnect($myconfig);
504 " (SELECT transdate FROM ar WHERE id = " .
505 " (SELECT MAX(id) FROM ar) LIMIT 1), " .
507 ($form->{transdate}) = $dbh->selectrow_array($query);
511 $main::lxdebug->leave_sub();
515 $main::lxdebug->enter_sub();
517 my ($self, $form) = @_;
519 my ($exchangerate, $key, $akey, $i, $j, $k, $index, $taxamount, $totaltax, $taxrate, $diff);
522 $form->{forex} = $form->{exchangerate};
523 $exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1;
525 foreach $key (keys %{ $form->{AR_links} }) {
526 # if there is a value we have an old entry
530 for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
531 if ($key eq "AR_paid") {
533 $form->{"AR_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno};
536 $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
537 $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
538 $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
539 $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
540 $form->{"forex_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
541 $form->{"exchangerate_$i"} = $form->{"forex_$j"};
542 $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id};
543 $form->{paidaccounts}++;
550 if ($key eq "AR_tax" || $key eq "AP_tax") {
551 $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
552 $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
554 if ($form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"} > 0) {
555 $totaltax += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
556 $taxrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
559 $totalwithholding += $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"};
560 $withholdingrate += $form->{"$form->{acc_trans}{$key}->[$i-1]->{accno}_rate"};
563 $index = $form->{acc_trans}{$key}->[$i - 1]->{index};
564 $form->{"tax_$index"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
565 $totaltax += $form->{"tax_$index"};
569 $form->{"${akey}_$k"} = $form->round_amount($form->{acc_trans}{$key}->[$i - 1]->{amount} / $exchangerate, 2);
571 if ($akey eq 'amount') {
573 $totalamount += $form->{"${akey}_$i"};
575 $form->{"oldprojectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
576 $form->{"projectnumber_$k"} = $form->{acc_trans}{$key}->[$i-1]->{projectnumber};
577 $form->{taxrate} = $form->{acc_trans}{$key}->[$i - 1]->{rate};
578 $form->{"project_id_$k"} = $form->{acc_trans}{$key}->[$i-1]->{project_id};
581 $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
584 $form->{ARselected} = $form->{acc_trans}{$key}->[$i-1]->{accno};
586 } elsif ($akey eq "amount") {
587 $form->{"${key}_$k"} = $form->{acc_trans}{$key}->[$i-1]->{accno} . "--" . $form->{acc_trans}{$key}->[$i-1]->{id};
588 $form->{"taxchart_$k"} = $form->{acc_trans}{$key}->[$i-1]->{id} . "--" . $form->{acc_trans}{$key}->[$i-1]->{rate};
595 $form->{taxincluded} = $taxincluded if ($form->{id});
596 $form->{paidaccounts} = 1 if not defined $form->{paidaccounts};
598 if ($form->{taxincluded} && $form->{taxrate} && $totalamount) {
600 # add tax to amounts and invtotal
601 for $i (1 .. $form->{rowcount}) {
602 $taxamount = ($totaltax + $totalwithholding) * $form->{"amount_$i"} / $totalamount;
603 $tax = $form->round_amount($taxamount, 2);
604 $diff += ($taxamount - $tax);
605 $form->{"amount_$i"} += $form->{"tax_$i"};
607 $form->{amount_1} += $form->round_amount($diff, 2);
610 $taxamount = $form->round_amount($taxamount, 2);
611 $form->{tax} = $taxamount;
613 $form->{invtotal} = $totalamount + $totaltax;
615 $main::lxdebug->leave_sub();
619 $main::lxdebug->enter_sub();
621 my ($self, $form, $myconfig, $id) = @_;
623 my ($query, $new_id, $storno_row, $acc_trans_rows);
624 my $dbh = $form->get_standard_dbh($myconfig);
626 $query = qq|SELECT nextval('glid')|;
627 ($new_id) = selectrow_query($form, $dbh, $query);
629 $query = qq|SELECT * FROM ar WHERE id = ?|;
630 $storno_row = selectfirst_hashref_query($form, $dbh, $query, $id);
632 $storno_row->{id} = $new_id;
633 $storno_row->{storno_id} = $id;
634 $storno_row->{storno} = 't';
635 $storno_row->{invnumber} = 'Storno-' . $storno_row->{invnumber};
636 $storno_row->{amount} *= -1;
637 $storno_row->{netamount} *= -1;
638 $storno_row->{paid} = $storno_amount->{amount};
640 delete @$storno_row{qw(itime mtime)};
642 $query = sprintf 'INSERT INTO ar (%s) VALUES (%s)', join(', ', keys %$storno_row), join(', ', map '?', values %$storno_row);
643 do_query($form, $dbh, $query, (values %$storno_row));
645 $query = qq|UPDATE ar SET paid = amount + paid, storno = 't' WHERE id = ?|;
646 do_query($form, $dbh, $query, $id);
648 # now copy acc_trans entries
649 $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|;
650 my $rowref = selectall_hashref_query($form, $dbh, $query, $id);
652 # kill all entries containing payments, which are the last 2n rows, of which the last has link =~ /paid/
653 while ($rowref->[-1]{link} =~ /paid/) {
654 splice(@$rowref, -2);
657 for my $row (@$rowref) {
658 delete @$row{qw(itime mtime link)};
659 $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row);
660 $row->{trans_id} = $new_id;
661 $row->{amount} *= -1;
662 do_query($form, $dbh, $query, (values %$row));
667 $main::lxdebug->leave_sub();