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 ($query, $sth, $null, $taxrate, $amount, $tax);
51 my $dbh = $form->dbconnect_noauto($myconfig);
54 $form->{exchangerate} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 :
55 ( $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy') ||
56 $form->parse_amount($myconfig, $form->{exchangerate}) );
58 # get the charts selected
59 map { ($form->{AR_amounts}{"amount_$_"}) = split /--/, $form->{"AR_amount_$_"} } 1 .. $form->{rowcount};
61 $form->{AR_amounts}{receivables} = $form->{ARselected};
62 $form->{AR}{receivables} = $form->{ARselected};
65 for $i (1 .. $form->{rowcount}) {
66 $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2);
67 $form->{amount} += $form->{"amount_$i"};
68 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"});
73 $form->{netamount} = 0;
74 $form->{total_tax} = 0;
76 # taxincluded doesn't make sense if there is no amount
77 $form->{taxincluded} = 0 unless $form->{amount};
79 for $i (1 .. $form->{rowcount}) {
80 ($form->{"tax_id_$i"}) = split /--/, $form->{"taxchart_$i"};
82 $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|;
83 ($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"});
85 $form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
86 $form->{AR_amounts}{"amounts_$i"}{taxkey} = $form->{"taxkey_$i"};
88 if ($form->{taxincluded} *= 1) {
89 $tax = $form->{"korrektur_$i"}
91 : $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1)); # should be same as taxrate * amount / (taxrate + 1)
92 $form->{"amount_$i"} = $form->round_amount($form->{"amount_$i"} - $tax, 2);
93 $form->{"tax_$i"} = $form->round_amount($tax, 2);
95 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"} unless $form->{"korrektur_$i"};
96 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
98 $form->{netamount} += $form->{"amount_$i"};
99 $form->{total_tax} += $form->{"tax_$i"};
102 # adjust paidaccounts if there is no date in the last row
103 $form->{paidaccounts}-- unless $form->{"datepaid_$form->{paidaccounts}"};
107 for $i (1 .. $form->{paidaccounts}) {
108 $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
109 $form->{paid} += $form->{"paid_$i"};
110 $form->{datepaid} = $form->{"datepaid_$i"};
113 $form->{amount} = $form->{netamount} + $form->{total_tax};
114 $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
116 ($null, $form->{employee_id}) = split /--/, $form->{employee};
118 $form->get_employee($dbh) unless $form->{employee_id};
120 # if we have an id delete old records else make one
122 # delete detail records
123 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
124 do_query($form, $dbh, $query, $form->{id});
126 $query = qq|SELECT nextval('glid')|;
127 ($form->{id}) = selectrow_query($form, $dbh, $query);
128 $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|;
129 do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
130 $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber};
134 ($null, $form->{department_id}) = split(/--/, $form->{department});
135 $form->{department_id} *= 1;
137 # record last payment date in ar table
138 $form->{datepaid} ||= $form->{transdate} ;
139 my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef;
143 invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?,
144 taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?,
145 netamount = ?, curr = ?, notes = ?, department_id = ?,
148 my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount},
149 conv_date($form->{duedate}), $form->{paid}, conv_date($datepaid), $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}),
150 conv_i($form->{employee_id}), conv_i($form->{id}));
151 do_query($form, $dbh, $query, @values);
153 # amount for AR account
154 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
156 # update exchangerate
157 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0)
158 if ($form->{currency} ne $form->{defaultcurrency}) && $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
161 # add individual transactions for AR, amount and taxes
162 for $i (1 .. $form->{rowcount}) {
163 if ($form->{"amount_$i"} != 0) {
164 my $project_id = conv_i($form->{"project_id_$i"});
165 $taxkey = $form->{AR_amounts}{"amounts_$i"}{taxkey};
167 # insert detail records in acc_trans
168 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
169 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
170 @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));
171 do_query($form, $dbh, $query, @values);
173 if ($form->{"tax_$i"} != 0) {
174 # insert detail records in acc_trans
175 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
176 VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
177 @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));
178 do_query($form, $dbh, $query, @values);
184 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey)
185 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
186 @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), $form->{AR_amounts}{receivables});
187 do_query($form, $dbh, $query, @values);
189 # add paid transactions
190 for my $i (1 .. $form->{paidaccounts}) {
191 if ($form->{"paid_$i"} != 0) {
192 my $project_id = conv_i($form->{"paid_project_id_$i"});
194 $form->{"AR_paid_$i"} =~ s/\"//g;
195 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
196 $form->{"datepaid_$i"} = $form->{transdate}
197 unless ($form->{"datepaid_$i"});
199 $form->{"exchangerate_$i"} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 :
200 ( $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy') ||
201 $form->parse_amount($myconfig, $form->{"exchangerate_$i"}) );
203 # if there is no amount and invtotal is zero there is no exchangerate
204 $form->{exchangerate} = $form->{"exchangerate_$i"}
205 if ($form->{amount} == 0 && $form->{netamount} == 0);
208 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
210 if ($form->{receivables} != 0) {
212 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey)
213 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
214 @values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables});
215 do_query($form, $dbh, $query, @values);
217 $form->{receivables} = $amount;
219 if ($form->{"paid_$i"} != 0) {
220 my $project_id = conv_i($form->{"paid_project_id_$i"});
222 $amount = $form->{"paid_$i"} * -1;
223 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, 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}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"});
226 do_query($form, $dbh, $query, @values);
228 # exchangerate difference for payment
229 $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1, 2);
232 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey)
233 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
234 @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{"paid_$i"});
235 do_query($form, $dbh, $query, @values);
238 # exchangerate gain/loss
239 $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 2);
242 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
243 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey)
244 VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|;
245 @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $accno);
246 do_query($form, $dbh, $query, @values);
250 # update exchangerate record
251 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0)
252 if ($form->{currency} ne $form->{defaultcurrency}) && !$form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
256 my $rc = $dbh->commit;
259 $main::lxdebug->leave_sub() and return $rc;
263 $main::lxdebug->enter_sub();
265 my ($self, $myconfig, $form, $locale) = @_;
267 # connect to database, turn off autocommit
268 my $dbh = $form->dbconnect_noauto($myconfig);
270 $form->{datepaid} = $form->{transdate};
272 # total payments, don't move we need it here
273 for my $i (1 .. $form->{paidaccounts}) {
274 if ($form->{type} eq "credit_note") {
275 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
277 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
279 $form->{paid} += $form->{"paid_$i"};
280 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
283 $form->{exchangerate} =
284 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
287 my $accno_ar = $form->{ARselected};
289 # record payments and offsetting AR
290 for my $i (1 .. $form->{paidaccounts}) {
292 if ($form->{"paid_$i"} != 0) {
293 my $project_id = conv_i($form->{"paid_project_id_$i"});
295 my ($accno) = split /--/, $form->{"AR_paid_$i"};
296 $form->{"datepaid_$i"} = $form->{transdate}
297 unless ($form->{"datepaid_$i"});
298 $form->{datepaid} = $form->{"datepaid_$i"};
301 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
302 $form->{"exchangerate_$i"} = 1;
305 $form->check_exchangerate($myconfig, $form->{currency},
306 $form->{"datepaid_$i"}, 'buy');
308 $form->{"exchangerate_$i"} =
311 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
316 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
321 qq|DELETE FROM acc_trans | .
322 qq|WHERE trans_id = ? AND amount = ? AND transdate = ? AND | .
323 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)|;
324 @values = (conv_i($form->{id}), $amount,
325 conv_date($form->{"datepaid_$i"}), $accno_ar);
326 do_query($form, $dbh, $query, @values);
329 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
330 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
331 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
332 @values = (conv_i($form->{id}), $accno_ar, conv_i($amount),
333 conv_date($form->{"datepaid_$i"}), $project_id, $accno_ar);
334 do_query($form, $dbh, $query, @values);
337 $form->{"paid_$i"} *= -1;
340 qq|DELETE FROM acc_trans | .
341 qq|WHERE trans_id = ? AND | .
342 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
343 qq| amount = ? AND transdate = ? AND source = ? AND memo = ?|;
344 @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
345 conv_date($form->{"datepaid_$i"}),
346 $form->{"source_$i"}, $form->{"memo_$i"});
347 do_query($form, $dbh, $query, @values);
350 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
351 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
352 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
353 @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
354 conv_date($form->{"datepaid_$i"}),
355 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $accno);
356 do_query($form, $dbh, $query, @values);
360 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
361 $form->{"exchangerate_$i"};
363 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
366 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
370 # update exchange rate
371 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
372 $form->update_exchangerate($dbh, $form->{currency},
373 $form->{"datepaid_$i"},
374 $form->{"exchangerate_$i"}, 0);
379 # record exchange rate differences and gains/losses
380 foreach my $accno (keys %{ $form->{fx} }) {
381 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
383 ($form->{fx}{$accno}{$transdate} =
384 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
388 qq|DELETE FROM acc_trans | .
389 qq|WHERE trans_id = ? AND | .
390 qq| chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
391 qq| amount = ? AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
392 @values = (conv_i($form->{id}), $accno,
393 conv_i($form->{fx}{$accno}{$transdate}),
394 conv_date($transdate));
395 do_query($form, $dbh, $query, @values);
398 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey) | .
399 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 'f', 't', ?, | .
400 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
401 @values = (conv_i($form->{id}), $accno,
402 $form->{fx}{$accno}{$transdate},
403 conv_date($transdate), $project_id,
404 $form->{fx}{$accno}{$transdate});
405 do_query($form, $dbh, $query, @values);
409 my $datepaid = ($form->{paid}) ? $form->{datepaid} : "NULL";
413 qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|;
414 @values = (conv_i($form->{paid}), conv_date($datepaid), conv_i($form->{id}));
415 do_query($form, $dbh, $query, @values);
417 my $rc = $dbh->commit;
420 $main::lxdebug->leave_sub();
425 sub delete_transaction {
426 $main::lxdebug->enter_sub();
428 my ($self, $myconfig, $form) = @_;
430 # connect to database, turn AutoCommit off
431 my $dbh = $form->dbconnect_noauto($myconfig);
433 my $query = qq|DELETE FROM ar WHERE id = ?|;
434 do_query($form, $dbh, $query, $form->{id});
436 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
437 do_query($form, $dbh, $query, $form->{id});
440 my $rc = $dbh->commit;
443 $main::lxdebug->leave_sub();
448 sub ar_transactions {
449 $main::lxdebug->enter_sub();
451 my ($self, $myconfig, $form) = @_;
453 # connect to database
454 my $dbh = $form->dbconnect($myconfig);
459 qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate, | .
460 qq| a.duedate, a.netamount, a.amount, a.paid, | .
461 qq| a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | .
462 qq| a.shippingpoint, a.storno, a.globalproject_id, | .
463 qq| a.transaction_description, | .
464 qq| pr.projectnumber AS globalprojectnumber, | .
466 qq| e.name AS employee | .
468 qq|JOIN customer c ON (a.customer_id = c.id) | .
469 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
470 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)|;
473 if ($form->{customer_id}) {
474 $where .= " AND a.customer_id = ?";
475 push(@values, $form->{customer_id});
476 } elsif ($form->{customer}) {
477 $where .= " AND c.name ILIKE ?";
478 push(@values, $form->like($form->{customer}));
480 if ($form->{department}) {
481 my ($null, $department_id) = split /--/, $form->{department};
482 $where .= " AND a.department_id = ?";
483 push(@values, $department_id);
485 foreach my $column (qw(invnumber ordnumber notes transaction_description)) {
486 if ($form->{$column}) {
487 $where .= " AND a.$column ILIKE ?";
488 push(@values, $form->like($form->{$column}));
491 if ($form->{"project_id"}) {
493 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
494 qq| (SELECT * FROM invoice i | .
495 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
496 push(@values, $form->{"project_id"}, $form->{"project_id"});
499 if ($form->{transdatefrom}) {
500 $where .= " AND a.transdate >= ?";
501 push(@values, $form->{transdatefrom});
503 if ($form->{transdateto}) {
504 $where .= " AND a.transdate <= ?";
505 push(@values, $form->{transdateto});
507 if ($form->{open} || $form->{closed}) {
508 unless ($form->{open} && $form->{closed}) {
509 $where .= " AND a.amount <> a.paid" if ($form->{open});
510 $where .= " AND a.amount = a.paid" if ($form->{closed});
514 my @a = (transdate, invnumber, name);
515 push @a, "employee" if $form->{l_employee};
516 my $sortorder = join(', ', @a);
518 if (grep({ $_ eq $form->{sort} }
519 qw(id transdate duedate invnumber ordnumber name
520 datepaid employee shippingpoint shipvia))) {
521 $sortorder = $form->{sort};
524 $query .= " WHERE $where ORDER by $sortorder";
526 my $sth = $dbh->prepare($query);
527 $sth->execute(@values) ||
528 $form->dberror($query . " (" . join(", ", @values) . ")");
531 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
532 push @{ $form->{AR} }, $ar;
538 $main::lxdebug->leave_sub();
542 $main::lxdebug->enter_sub();
544 my ($self, $myconfig, $form) = @_;
546 # connect to database
547 my $dbh = $form->dbconnect($myconfig);
551 " (SELECT transdate FROM ar WHERE id = " .
552 " (SELECT MAX(id) FROM ar) LIMIT 1), " .
554 ($form->{transdate}) = $dbh->selectrow_array($query);
558 $main::lxdebug->leave_sub();