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 #======================================================================
39 sub post_transaction {
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form) = @_;
44 my ($null, $taxrate, $amount, $tax, $diff);
48 my $dbh = $form->dbconnect_noauto($myconfig);
50 if ($form->{currency} eq $form->{defaultcurrency}) {
51 $form->{exchangerate} = 1;
54 $form->check_exchangerate($myconfig, $form->{currency},
55 $form->{transdate}, 'buy');
57 for $i (1 .. $form->{rowcount}) {
58 $form->{AR_amounts}{"amount_$i"} =
59 (split(/--/, $form->{"AR_amount_$i"}))[0];
61 ($form->{AR_amounts}{receivables}) = split(/--/, $form->{ARselected});
62 ($form->{AR}{receivables}) = split(/--/, $form->{ARselected});
64 $form->{exchangerate} =
67 : $form->parse_amount($myconfig, $form->{exchangerate});
69 for $i (1 .. $form->{rowcount}) {
71 $form->{"amount_$i"} =
72 $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"})
73 * $form->{exchangerate},
76 $form->{netamount} += $form->{"amount_$i"};
81 $form->{amount} = $form->{netamount};
85 # taxincluded doesn't make sense if there is no amount
87 $form->{taxincluded} = 0 if ($form->{amount} == 0);
88 for $i (1 .. $form->{rowcount}) {
89 ($form->{"taxkey_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
92 qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{"taxkey_$i"}|;
93 $sth = $dbh->prepare($query);
94 $sth->execute || $form->dberror($query);
95 ($form->{AR_amounts}{"tax_$i"}, $form->{"taxrate_$i"}) =
97 $form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
98 $form->{AR_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
101 if (!$form->{"korrektur_$i"}) {
102 if ($form->{taxincluded} *= 1) {
104 $form->{"amount_$i"} -
105 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
106 $amount = $form->{"amount_$i"} - $tax;
107 $form->{"amount_$i"} = $form->round_amount($amount, 2);
108 $diff += $amount - $form->{"amount_$i"};
109 $form->{"tax_$i"} = $form->round_amount($tax, 2);
110 $form->{netamount} += $form->{"amount_$i"};
112 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
114 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
117 $form->{total_tax} += $form->{"tax_$i"};
120 # adjust paidaccounts if there is no date in the last row
121 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
125 for $i (1 .. $form->{paidaccounts}) {
127 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
130 $form->{paid} += $form->{"paid_$i"};
131 $form->{datepaid} = $form->{"datepaid_$i"};
135 $form->{amount} = $form->{netamount} + $form->{total_tax};
137 $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
139 my ($query, $sth, $null);
141 ($null, $form->{employee_id}) = split /--/, $form->{employee};
142 unless ($form->{employee_id}) {
143 $form->get_employee($dbh);
146 # if we have an id delete old records
149 # delete detail records
150 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
151 $dbh->do($query) || $form->dberror($query);
154 my $uid = rand() . time;
156 $uid .= $form->{login};
158 $uid = substr($uid, 2, 75);
160 $query = qq|INSERT INTO ar (invnumber, employee_id)
161 VALUES ('$uid', $form->{employee_id})|;
162 $dbh->do($query) || $form->dberror($query);
164 $query = qq|SELECT a.id FROM ar a
165 WHERE a.invnumber = '$uid'|;
166 $sth = $dbh->prepare($query);
167 $sth->execute || $form->dberror($query);
169 ($form->{id}) = $sth->fetchrow_array;
175 ($null, $form->{department_id}) = split(/--/, $form->{department});
176 $form->{department_id} *= 1;
179 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
181 # record last payment date in ar table
182 $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
183 my $datepaid = ($form->{paid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
185 $query = qq|UPDATE ar set
186 invnumber = '$form->{invnumber}',
187 ordnumber = '$form->{ordnumber}',
188 transdate = '$form->{transdate}',
189 customer_id = $form->{customer_id},
190 taxincluded = '$form->{taxincluded}',
191 amount = $form->{amount},
192 duedate = '$form->{duedate}',
193 paid = $form->{paid},
194 datepaid = $datepaid,
195 netamount = $form->{netamount},
196 curr = '$form->{currency}',
197 notes = '$form->{notes}',
198 department_id = $form->{department_id},
199 employee_id = $form->{employee_id}
200 WHERE id = $form->{id}|;
201 $dbh->do($query) || $form->dberror($query);
203 # amount for AR account
204 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
206 # update exchangerate
207 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
208 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
209 $form->{exchangerate}, 0);
212 # add individual transactions for AR, amount and taxes
213 for $i (1 .. $form->{rowcount}) {
214 if ($form->{"amount_$i"} != 0) {
215 $project_id = 'NULL';
216 if ("amount_$i" =~ /amount_/) {
217 if ($form->{"project_id_$i"} && $form->{"projectnumber_$i"}) {
218 $project_id = $form->{"project_id_$i"};
221 if ("amount_$i" =~ /amount/) {
222 $taxkey = $form->{AR_amounts}{"amount_$i"}{taxkey};
225 # insert detail records in acc_trans
226 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
228 VALUES ($form->{id}, (SELECT c.id FROM chart c
229 WHERE c.accno = '$form->{AR_amounts}{"amount_$i"}'),
230 $form->{"amount_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
231 $dbh->do($query) || $form->dberror($query);
232 if ($form->{"tax_$i"} != 0) {
234 # insert detail records in acc_trans
236 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
238 VALUES ($form->{id}, (SELECT c.id FROM chart c
239 WHERE c.accno = '$form->{AR_amounts}{"tax_$i"}'),
240 $form->{"tax_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
241 $dbh->do($query) || $form->dberror($query);
247 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
249 VALUES ($form->{id}, (SELECT c.id FROM chart c
250 WHERE c.accno = '$form->{AR_amounts}{receivables}'),
251 $form->{receivables}, '$form->{transdate}', $project_id)|;
252 $dbh->do($query) || $form->dberror($query);
254 # add paid transactions
255 for my $i (1 .. $form->{paidaccounts}) {
256 if ($form->{"paid_$i"} != 0) {
258 $form->{"AR_paid_$i"} =~ s/\"//g;
259 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
260 $form->{"datepaid_$i"} = $form->{transdate}
261 unless ($form->{"datepaid_$i"});
264 if ($form->{currency} eq $form->{defaultcurrency}) {
265 $form->{"exchangerate_$i"} = 1;
268 $form->check_exchangerate($myconfig, $form->{currency},
269 $form->{"datepaid_$i"}, 'buy');
271 $form->{"exchangerate_$i"} =
274 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
277 # if there is no amount and invtotal is zero there is no exchangerate
278 if ($form->{amount} == 0 && $form->{netamount} == 0) {
279 $form->{exchangerate} = $form->{"exchangerate_$i"};
284 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
286 if ($form->{receivables} != 0) {
289 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
292 (SELECT c.id FROM chart c
293 WHERE c.accno = '$form->{AR}{receivables}'),
294 $amount, '$form->{"datepaid_$i"}')|;
295 $dbh->do($query) || $form->dberror($query);
297 $form->{receivables} = $amount;
299 $form->{"memo_$i"} =~ s/\'/\'\'/g;
301 if ($form->{"paid_$i"} != 0) {
304 $amount = $form->{"paid_$i"} * -1;
305 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
306 transdate, source, memo)
308 (SELECT c.id FROM chart c
309 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
310 $amount, '$form->{"datepaid_$i"}',
311 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
312 $dbh->do($query) || $form->dberror($query);
314 # exchangerate difference for payment
317 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1,
321 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
322 transdate, fx_transaction, cleared)
324 (SELECT c.id FROM chart c
325 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
326 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
327 $dbh->do($query) || $form->dberror($query);
330 # exchangerate gain/loss
334 ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1,
339 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
340 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
341 transdate, fx_transaction, cleared)
342 VALUES ($form->{id}, (SELECT c.id FROM chart c
343 WHERE c.accno = '$accno'),
344 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
345 $dbh->do($query) || $form->dberror($query);
349 # update exchangerate record
350 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
351 $form->update_exchangerate($dbh, $form->{currency},
352 $form->{"datepaid_$i"},
353 $form->{"exchangerate_$i"}, 0);
358 my $rc = $dbh->commit;
361 $main::lxdebug->leave_sub();
366 sub delete_transaction {
367 $main::lxdebug->enter_sub();
369 my ($self, $myconfig, $form) = @_;
371 # connect to database, turn AutoCommit off
372 my $dbh = $form->dbconnect_noauto($myconfig);
374 my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
375 $dbh->do($query) || $form->dberror($query);
377 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
378 $dbh->do($query) || $form->dberror($query);
381 my $rc = $dbh->commit;
384 $main::lxdebug->leave_sub();
389 sub ar_transactions {
390 $main::lxdebug->enter_sub();
392 my ($self, $myconfig, $form) = @_;
394 # connect to database
395 my $dbh = $form->dbconnect($myconfig);
397 my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
398 a.duedate, a.netamount, a.amount, a.paid, c.name,
399 a.invoice, a.datepaid, a.terms, a.notes, a.shipvia,
403 JOIN customer c ON (a.customer_id = c.id)
404 LEFT JOIN employee e ON (a.employee_id = e.id)|;
407 if ($form->{customer_id}) {
408 $where .= " AND a.customer_id = $form->{customer_id}";
410 if ($form->{customer}) {
411 my $customer = $form->like(lc $form->{customer});
412 $where .= " AND lower(c.name) LIKE '$customer'";
415 if ($form->{department}) {
416 my ($null, $department_id) = split /--/, $form->{department};
417 $where .= " AND a.department_id = $department_id";
419 if ($form->{invnumber}) {
420 my $invnumber = $form->like(lc $form->{invnumber});
421 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
423 if ($form->{ordnumber}) {
424 my $ordnumber = $form->like(lc $form->{ordnumber});
425 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
427 if ($form->{notes}) {
428 my $notes = $form->like(lc $form->{notes});
429 $where .= " AND lower(a.notes) LIKE '$notes'";
432 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
433 if $form->{transdatefrom};
434 $where .= " AND a.transdate <= '$form->{transdateto}'"
435 if $form->{transdateto};
436 if ($form->{open} || $form->{closed}) {
437 unless ($form->{open} && $form->{closed}) {
438 $where .= " AND a.amount <> a.paid" if ($form->{open});
439 $where .= " AND a.amount = a.paid" if ($form->{closed});
443 my @a = (transdate, invnumber, name);
444 push @a, "employee" if $form->{l_employee};
445 my $sortorder = join ', ', $form->sort_columns(@a);
446 $sortorder = $form->{sort} if $form->{sort};
448 $query .= "WHERE $where
449 ORDER by $sortorder";
451 my $sth = $dbh->prepare($query);
452 $sth->execute || $form->dberror($query);
454 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
455 push @{ $form->{AR} }, $ar;
461 $main::lxdebug->leave_sub();