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 #======================================================================
40 sub post_transaction {
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
45 my ($null, $taxrate, $amount, $tax, $diff);
51 my $dbh = $form->dbconnect_noauto($myconfig);
53 if ($form->{currency} eq $form->{defaultcurrency}) {
54 $form->{exchangerate} = 1;
57 $form->check_exchangerate($myconfig, $form->{currency},
58 $form->{transdate}, 'buy');
60 for $i (1 .. $form->{rowcount}) {
61 $form->{AR_amounts}{"amount_$i"} =
62 (split(/--/, $form->{"AR_amount_$i"}))[0];
64 $form->{AR_amounts}{receivables} = $form->{ARselected};
65 $form->{AR}{receivables} = $form->{ARselected};
67 $form->{exchangerate} =
70 : $form->parse_amount($myconfig, $form->{exchangerate});
72 for $i (1 .. $form->{rowcount}) {
74 $form->{"amount_$i"} =
75 $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"})
76 * $form->{exchangerate},
79 $form->{netamount} += $form->{"amount_$i"};
81 # parse tax_$i for later
82 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"});
87 $form->{amount} = $form->{netamount};
90 $form->{netamount} = 0;
91 $form->{total_tax} = 0;
93 # taxincluded doesn't make sense if there is no amount
95 $form->{taxincluded} = 0 if ($form->{amount} == 0);
96 for $i (1 .. $form->{rowcount}) {
97 ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
100 qq|SELECT c.accno, t.taxkey, t.rate | .
101 qq|FROM tax t LEFT JOIN chart c on (c.id = t.chart_id) | .
102 qq|WHERE t.id = ? | .
103 qq|ORDER BY c.accno|;
105 ($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) =
106 selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"});
107 $form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
108 $form->{AR_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
110 if ($form->{taxincluded} *= 1) {
111 if (!$form->{"korrektur_$i"}) {
113 $form->{"amount_$i"} -
114 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
116 $tax = $form->{"tax_$i"};
118 $amount = $form->{"amount_$i"} - $tax;
119 $form->{"amount_$i"} = $form->round_amount($amount, 2);
120 $diff += $amount - $form->{"amount_$i"};
121 $form->{"tax_$i"} = $form->round_amount($tax, 2);
122 $form->{netamount} += $form->{"amount_$i"};
124 if (!$form->{"korrektur_$i"}) {
125 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
128 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
129 $form->{netamount} += $form->{"amount_$i"};
132 $form->{total_tax} += $form->{"tax_$i"};
135 # adjust paidaccounts if there is no date in the last row
136 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
140 for $i (1 .. $form->{paidaccounts}) {
142 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
145 $form->{paid} += $form->{"paid_$i"};
146 $form->{datepaid} = $form->{"datepaid_$i"};
150 $form->{amount} = $form->{netamount} + $form->{total_tax};
152 $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
154 my ($query, $sth, $null);
156 ($null, $form->{employee_id}) = split /--/, $form->{employee};
157 unless ($form->{employee_id}) {
158 $form->get_employee($dbh);
161 # if we have an id delete old records
163 # delete detail records
164 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
165 do_query($form, $dbh, $query, $form->{id});
168 $query = qq|SELECT nextval('glid')|;
169 ($form->{id}) = selectrow_query($form, $dbh, $query);
171 $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|;
172 do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
174 if (!$form->{invnumber}) {
175 $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh);
180 ($null, $form->{department_id}) = split(/--/, $form->{department});
181 $form->{department_id} *= 1;
183 # record last payment date in ar table
184 $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
185 my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef;
189 qq| invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, | .
190 qq| taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?, | .
191 qq| netamount = ?, curr = ?, notes = ?, department_id = ?, | .
192 qq| employee_id = ? | .
194 my @values = ($form->{invnumber}, $form->{ordnumber},
195 conv_date($form->{transdate}), conv_i($form->{customer_id}),
196 $form->{taxincluded} ? 't' : 'f', $form->{amount},
197 conv_date($form->{duedate}), $form->{paid},
198 conv_date($datepaid), $form->{netamount},
199 $form->{currency}, $form->{notes},
200 conv_i($form->{department_id}),
201 conv_i($form->{employee_id}),
202 conv_i($form->{id}));
203 do_query($form, $dbh, $query, @values);
205 # amount for AR account
206 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
208 # update exchangerate
209 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
210 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
211 $form->{exchangerate}, 0);
214 # add individual transactions for AR, amount and taxes
215 for $i (1 .. $form->{rowcount}) {
216 if ($form->{"amount_$i"} != 0) {
217 my $project_id = conv_i($form->{"project_id_$i"});
218 $taxkey = $form->{AR_amounts}{"amount_$i"}{taxkey};
220 # insert detail records in acc_trans
222 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
223 qq| project_id, taxkey) | .
224 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
225 @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}),
226 conv_i($form->{"amount_$i"}), conv_date($form->{transdate}),
227 $project_id, conv_i($taxkey));
228 do_query($form, $dbh, $query, @values);
230 if ($form->{"tax_$i"} != 0) {
231 # insert detail records in acc_trans
233 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
234 qq| project_id, taxkey) | .
235 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
237 @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}),
238 conv_i($form->{"tax_$i"}), conv_date($form->{transdate}),
239 $project_id, conv_i($taxkey));
240 do_query($form, $dbh, $query, @values);
247 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
248 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
249 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
250 @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables},
251 conv_i($form->{receivables}), conv_date($form->{transdate}),
252 $form->{AR_amounts}{receivables});
253 do_query($form, $dbh, $query, @values);
255 # add paid transactions
256 for my $i (1 .. $form->{paidaccounts}) {
257 if ($form->{"paid_$i"} != 0) {
258 my $project_id = conv_i($form->{"paid_project_id_$i"});
260 $form->{"AR_paid_$i"} =~ s/\"//g;
261 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
262 $form->{"datepaid_$i"} = $form->{transdate}
263 unless ($form->{"datepaid_$i"});
266 if ($form->{currency} eq $form->{defaultcurrency}) {
267 $form->{"exchangerate_$i"} = 1;
270 $form->check_exchangerate($myconfig, $form->{currency},
271 $form->{"datepaid_$i"}, 'buy');
273 $form->{"exchangerate_$i"} =
276 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
279 # if there is no amount and invtotal is zero there is no exchangerate
280 if ($form->{amount} == 0 && $form->{netamount} == 0) {
281 $form->{exchangerate} = $form->{"exchangerate_$i"};
286 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
288 if ($form->{receivables} != 0) {
291 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
292 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
293 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
294 @values = (conv_i($form->{id}), $form->{AR}{receivables},
295 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
296 $form->{AR}{receivables});
297 do_query($form, $dbh, $query, @values);
299 $form->{receivables} = $amount;
301 if ($form->{"paid_$i"} != 0) {
302 my $project_id = conv_i($form->{"paid_project_id_$i"});
304 $amount = $form->{"paid_$i"} * -1;
306 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
307 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
308 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
309 @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount,
310 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
311 $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"});
312 do_query($form, $dbh, $query, @values);
314 # exchangerate difference for payment
317 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1,
322 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
323 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
324 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
325 @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"},
326 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
327 $form->{AR}{"paid_$i"});
328 do_query($form, $dbh, $query, @values);
331 # exchangerate gain/loss
335 ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1,
340 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
342 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
343 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
344 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
345 @values = (conv_i($form->{id}), $accno, $amount,
346 conv_date($form->{"datepaid_$i"}), $project_id, $accno);
347 do_query($form, $dbh, $query, @values);
351 # update exchangerate record
352 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
353 $form->update_exchangerate($dbh, $form->{currency},
354 $form->{"datepaid_$i"},
355 $form->{"exchangerate_$i"}, 0);
360 my $rc = $dbh->commit;
363 $main::lxdebug->leave_sub();
369 $main::lxdebug->enter_sub();
371 my ($self, $myconfig, $form, $locale) = @_;
373 # connect to database, turn off autocommit
374 my $dbh = $form->dbconnect_noauto($myconfig);
376 $form->{datepaid} = $form->{transdate};
378 # total payments, don't move we need it here
379 for my $i (1 .. $form->{paidaccounts}) {
380 if ($form->{type} eq "credit_note") {
381 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
383 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
385 $form->{paid} += $form->{"paid_$i"};
386 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
389 $form->{exchangerate} =
390 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
393 my $accno_ar = $form->{ARselected};
395 # record payments and offsetting AR
396 for my $i (1 .. $form->{paidaccounts}) {
398 if ($form->{"paid_$i"} != 0) {
399 my $project_id = conv_i($form->{"paid_project_id_$i"});
401 my ($accno) = split /--/, $form->{"AR_paid_$i"};
402 $form->{"datepaid_$i"} = $form->{transdate}
403 unless ($form->{"datepaid_$i"});
404 $form->{datepaid} = $form->{"datepaid_$i"};
407 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
408 $form->{"exchangerate_$i"} = 1;
411 $form->check_exchangerate($myconfig, $form->{currency},
412 $form->{"datepaid_$i"}, 'buy');
414 $form->{"exchangerate_$i"} =
417 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
422 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
427 qq|DELETE FROM acc_trans | .
428 qq|WHERE trans_id = ? AND amount = ? AND transdate = ? AND | .
429 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)|;
430 @values = (conv_i($form->{id}), $amount,
431 conv_date($form->{"datepaid_$i"}), $accno_ar);
432 do_query($form, $dbh, $query, @values);
435 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
436 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
437 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
438 @values = (conv_i($form->{id}), $accno_ar, conv_i($amount),
439 conv_date($form->{"datepaid_$i"}), $project_id, $accno_ar);
440 do_query($form, $dbh, $query, @values);
443 $form->{"paid_$i"} *= -1;
446 qq|DELETE FROM acc_trans | .
447 qq|WHERE trans_id = ? AND | .
448 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
449 qq| amount = ? AND transdate = ? AND source = ? AND memo = ?|;
450 @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
451 conv_date($form->{"datepaid_$i"}),
452 $form->{"source_$i"}, $form->{"memo_$i"});
453 do_query($form, $dbh, $query, @values);
456 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
457 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
458 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
459 @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
460 conv_date($form->{"datepaid_$i"}),
461 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $accno);
462 do_query($form, $dbh, $query, @values);
466 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
467 $form->{"exchangerate_$i"};
469 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
472 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
478 # update exchange rate
479 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
480 $form->update_exchangerate($dbh, $form->{currency},
481 $form->{"datepaid_$i"},
482 $form->{"exchangerate_$i"}, 0);
487 # record exchange rate differences and gains/losses
488 foreach my $accno (keys %{ $form->{fx} }) {
489 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
491 ($form->{fx}{$accno}{$transdate} =
492 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
496 qq|DELETE FROM acc_trans | .
497 qq|WHERE trans_id = ? AND | .
498 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
499 qq| amount = ? AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
500 @values = (conv_i($form->{id}), $accno,
501 conv_i($form->{fx}{$accno}{$transdate}),
502 conv_date($transdate));
503 do_query($form, $dbh, $query, @values);
506 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey) | .
507 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 'f', 't', ?, | .
508 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
509 @values = (conv_i($form->{id}), $accno,
510 $form->{fx}{$accno}{$transdate},
511 conv_date($transdate), $project_id,
512 $form->{fx}{$accno}{$transdate});
513 do_query($form, $dbh, $query, @values);
517 my $datepaid = ($form->{paid}) ? $form->{datepaid} : "NULL";
521 qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|;
522 @values = (conv_i($form->{paid}), conv_date($datepaid), conv_i($form->{id}));
523 do_query($form, $dbh, $query, @values);
525 my $rc = $dbh->commit;
528 $main::lxdebug->leave_sub();
533 sub delete_transaction {
534 $main::lxdebug->enter_sub();
536 my ($self, $myconfig, $form) = @_;
538 # connect to database, turn AutoCommit off
539 my $dbh = $form->dbconnect_noauto($myconfig);
541 my $query = qq|DELETE FROM ar WHERE id = ?|;
542 do_query($form, $dbh, $query, $form->{id});
544 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
545 do_query($form, $dbh, $query, $form->{id});
548 my $rc = $dbh->commit;
551 $main::lxdebug->leave_sub();
556 sub ar_transactions {
557 $main::lxdebug->enter_sub();
559 my ($self, $myconfig, $form) = @_;
561 # connect to database
562 my $dbh = $form->dbconnect($myconfig);
567 qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate, | .
568 qq| a.duedate, a.netamount, a.amount, a.paid, | .
569 qq| a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | .
570 qq| a.shippingpoint, a.storno, a.globalproject_id, | .
571 qq| pr.projectnumber AS globalprojectnumber, | .
573 qq| e.name AS employee | .
575 qq|JOIN customer c ON (a.customer_id = c.id) | .
576 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
577 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)|;
580 if ($form->{customer_id}) {
581 $where .= " AND a.customer_id = ?";
582 push(@values, $form->{customer_id});
583 } elsif ($form->{customer}) {
584 $where .= " AND c.name ILIKE ?";
585 push(@values, $form->like($form->{customer}));
587 if ($form->{department}) {
588 my ($null, $department_id) = split /--/, $form->{department};
589 $where .= " AND a.department_id = ?";
590 push(@values, $department_id);
592 foreach my $column (qw(invnumber ordnumber notes)) {
593 if ($form->{$column}) {
594 $where .= " AND a.$column ILIKE ?";
595 push(@values, $form->like($form->{$column}));
598 if ($form->{"project_id"}) {
600 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
601 qq| (SELECT * FROM invoice i | .
602 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
603 push(@values, $form->{"project_id"}, $form->{"project_id"});
606 if ($form->{transdatefrom}) {
607 $where .= " AND a.transdate >= ?";
608 push(@values, $form->{transdatefrom});
610 if ($form->{transdateto}) {
611 $where .= " AND a.transdate <= ?";
612 push(@values, $form->{transdateto});
614 if ($form->{open} || $form->{closed}) {
615 unless ($form->{open} && $form->{closed}) {
616 $where .= " AND a.amount <> a.paid" if ($form->{open});
617 $where .= " AND a.amount = a.paid" if ($form->{closed});
621 my @a = (transdate, invnumber, name);
622 push @a, "employee" if $form->{l_employee};
623 my $sortorder = join(', ', @a);
625 if (grep({ $_ eq $form->{sort} }
626 qw(id transdate duedate invnumber ordnumber name
627 datepaid employee shippingpoint shipvia))) {
628 $sortorder = $form->{sort};
631 $query .= " WHERE $where ORDER by $sortorder";
633 my $sth = $dbh->prepare($query);
634 $sth->execute(@values) ||
635 $form->dberror($query . " (" . join(", ", @values) . ")");
638 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
639 push @{ $form->{AR} }, $ar;
645 $main::lxdebug->leave_sub();
649 $main::lxdebug->enter_sub();
651 my ($self, $myconfig, $form) = @_;
653 # connect to database
654 my $dbh = $form->dbconnect($myconfig);
658 " (SELECT transdate FROM ar WHERE id = " .
659 " (SELECT MAX(id) FROM ar) LIMIT 1), " .
661 ($form->{transdate}) = $dbh->selectrow_array($query);
665 $main::lxdebug->leave_sub();