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"} = (split(/--/, $form->{"AR_amount_$i"}))[0];
60 ($form->{AR_amounts}{receivables}) = split(/--/, $form->{ARselected});
61 ($form->{AR}{receivables}) = split(/--/, $form->{ARselected});
63 $form->{exchangerate} =
66 : $form->parse_amount($myconfig, $form->{exchangerate});
68 for $i (1 .. $form->{rowcount}) {
70 $form->{"amount_$i"} =
71 $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"})
72 * $form->{exchangerate},
75 $form->{netamount} += $form->{"amount_$i"};
80 $form->{amount} = $form->{netamount};
84 # taxincluded doesn't make sense if there is no amount
86 $form->{taxincluded} = 0 if ($form->{amount} == 0);
87 for $i (1 .. $form->{rowcount}) {
88 ($form->{"taxkey_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
91 qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{"taxkey_$i"}|;
92 $sth = $dbh->prepare($query);
93 $sth->execute || $form->dberror($query);
94 ($form->{AR_amounts}{"tax_$i"}, $form->{"taxrate_$i"}) = $sth->fetchrow_array;
95 $form->{AR_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
96 $form->{AR_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
99 if (!$form->{"korrektur_$i"}) {
100 if ($form->{taxincluded} *= 1) {
101 $tax = $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
102 $amount = $form->{"amount_$i"} - $tax;
103 $form->{"amount_$i"} = $form->round_amount($amount, 2);
104 $diff += $amount - $form->{"amount_$i"};
105 $form->{"tax_$i"} = $form->round_amount($tax, 2);
106 $form->{netamount} += $form->{"amount_$i"};
108 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
109 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
111 $form->{total_tax} += $form->{"tax_$i"};
114 # adjust paidaccounts if there is no date in the last row
115 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
119 for $i (1 .. $form->{paidaccounts}) {
121 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
124 $form->{paid} += $form->{"paid_$i"};
125 $form->{datepaid} = $form->{"datepaid_$i"};
129 $form->{amount} = $form->{netamount} + $form->{total_tax};
131 $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
133 my ($query, $sth, $null);
135 ($null, $form->{employee_id}) = split /--/, $form->{employee};
136 unless ($form->{employee_id}) {
137 $form->get_employee($dbh);
140 # if we have an id delete old records
143 # delete detail records
144 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
145 $dbh->do($query) || $form->dberror($query);
148 my $uid = rand() . time;
150 $uid .= $form->{login};
152 $uid = substr($uid, 2, 75);
154 $query = qq|INSERT INTO ar (invnumber, employee_id)
155 VALUES ('$uid', $form->{employee_id})|;
156 $dbh->do($query) || $form->dberror($query);
158 $query = qq|SELECT a.id FROM ar a
159 WHERE a.invnumber = '$uid'|;
160 $sth = $dbh->prepare($query);
161 $sth->execute || $form->dberror($query);
163 ($form->{id}) = $sth->fetchrow_array;
169 ($null, $form->{department_id}) = split(/--/, $form->{department});
170 $form->{department_id} *= 1;
173 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
175 # record last payment date in ar table
176 $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
177 my $datepaid = ($form->{paid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
179 $query = qq|UPDATE ar set
180 invnumber = '$form->{invnumber}',
181 ordnumber = '$form->{ordnumber}',
182 transdate = '$form->{transdate}',
183 customer_id = $form->{customer_id},
184 taxincluded = '$form->{taxincluded}',
185 amount = $form->{amount},
186 duedate = '$form->{duedate}',
187 paid = $form->{paid},
188 datepaid = $datepaid,
189 netamount = $form->{netamount},
190 curr = '$form->{currency}',
191 notes = '$form->{notes}',
192 department_id = $form->{department_id},
193 employee_id = $form->{employee_id}
194 WHERE id = $form->{id}|;
195 $dbh->do($query) || $form->dberror($query);
197 # amount for AR account
198 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
200 # update exchangerate
201 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
202 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
203 $form->{exchangerate}, 0);
206 # add individual transactions for AR, amount and taxes
207 for $i (1 .. $form->{rowcount}) {
208 if ($form->{"amount_$i"} != 0) {
209 $project_id = 'NULL';
210 if ("amount_$i" =~ /amount_/) {
211 if ($form->{"project_id_$i"} && $form->{"projectnumber_$i"}) {
212 $project_id = $form->{"project_id_$i"};
215 if ("amount_$i" =~ /amount/) {
216 $taxkey = $form->{AR_amounts}{"amount_$i"}{taxkey};
219 # insert detail records in acc_trans
220 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
222 VALUES ($form->{id}, (SELECT c.id FROM chart c
223 WHERE c.accno = '$form->{AR_amounts}{"amount_$i"}'),
224 $form->{"amount_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
225 $dbh->do($query) || $form->dberror($query);
226 if ($form->{"tax_$i"} !=0) {
227 # insert detail records in acc_trans
228 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
230 VALUES ($form->{id}, (SELECT c.id FROM chart c
231 WHERE c.accno = '$form->{AR_amounts}{"tax_$i"}'),
232 $form->{"tax_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
233 $dbh->do($query) || $form->dberror($query);
238 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
240 VALUES ($form->{id}, (SELECT c.id FROM chart c
241 WHERE c.accno = '$form->{AR_amounts}{receivables}'),
242 $form->{receivables}, '$form->{transdate}', $project_id)|;
243 $dbh->do($query) || $form->dberror($query);
246 # add paid transactions
247 for my $i (1 .. $form->{paidaccounts}) {
248 if ($form->{"paid_$i"} != 0) {
250 $form->{"AR_paid_$i"} =~ s/\"//g;
251 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
252 $form->{"datepaid_$i"} = $form->{transdate}
253 unless ($form->{"datepaid_$i"});
256 if ($form->{currency} eq $form->{defaultcurrency}) {
257 $form->{"exchangerate_$i"} = 1;
260 $form->check_exchangerate($myconfig, $form->{currency},
261 $form->{"datepaid_$i"}, 'buy');
263 $form->{"exchangerate_$i"} =
266 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
269 # if there is no amount and invtotal is zero there is no exchangerate
270 if ($form->{amount} == 0 && $form->{netamount} == 0) {
271 $form->{exchangerate} = $form->{"exchangerate_$i"};
276 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
278 if ($form->{receivables} != 0) {
281 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
284 (SELECT c.id FROM chart c
285 WHERE c.accno = '$form->{AR}{receivables}'),
286 $amount, '$form->{"datepaid_$i"}')|;
287 $dbh->do($query) || $form->dberror($query);
289 $form->{receivables} = $amount;
291 $form->{"memo_$i"} =~ s/\'/\'\'/g;
293 if ($form->{"paid_$i"} != 0) {
296 $amount = $form->{"paid_$i"} * -1;
297 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
298 transdate, source, memo)
300 (SELECT c.id FROM chart c
301 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
302 $amount, '$form->{"datepaid_$i"}',
303 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
304 $dbh->do($query) || $form->dberror($query);
306 # exchangerate difference for payment
309 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1,
313 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
314 transdate, fx_transaction, cleared)
316 (SELECT c.id FROM chart c
317 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
318 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
319 $dbh->do($query) || $form->dberror($query);
322 # exchangerate gain/loss
326 ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1,
331 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
332 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
333 transdate, fx_transaction, cleared)
334 VALUES ($form->{id}, (SELECT c.id FROM chart c
335 WHERE c.accno = '$accno'),
336 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
337 $dbh->do($query) || $form->dberror($query);
341 # update exchangerate record
342 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
343 $form->update_exchangerate($dbh, $form->{currency},
344 $form->{"datepaid_$i"},
345 $form->{"exchangerate_$i"}, 0);
350 my $rc = $dbh->commit;
353 $main::lxdebug->leave_sub();
358 sub delete_transaction {
359 $main::lxdebug->enter_sub();
361 my ($self, $myconfig, $form) = @_;
363 # connect to database, turn AutoCommit off
364 my $dbh = $form->dbconnect_noauto($myconfig);
366 my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
367 $dbh->do($query) || $form->dberror($query);
369 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
370 $dbh->do($query) || $form->dberror($query);
373 my $rc = $dbh->commit;
376 $main::lxdebug->leave_sub();
381 sub ar_transactions {
382 $main::lxdebug->enter_sub();
384 my ($self, $myconfig, $form) = @_;
386 # connect to database
387 my $dbh = $form->dbconnect($myconfig);
389 my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
390 a.duedate, a.netamount, a.amount, a.paid, c.name,
391 a.invoice, a.datepaid, a.terms, a.notes, a.shipvia,
395 JOIN customer c ON (a.customer_id = c.id)
396 LEFT JOIN employee e ON (a.employee_id = e.id)|;
399 if ($form->{customer_id}) {
400 $where .= " AND a.customer_id = $form->{customer_id}";
402 if ($form->{customer}) {
403 my $customer = $form->like(lc $form->{customer});
404 $where .= " AND lower(c.name) LIKE '$customer'";
407 if ($form->{department}) {
408 my ($null, $department_id) = split /--/, $form->{department};
409 $where .= " AND a.department_id = $department_id";
411 if ($form->{invnumber}) {
412 my $invnumber = $form->like(lc $form->{invnumber});
413 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
415 if ($form->{ordnumber}) {
416 my $ordnumber = $form->like(lc $form->{ordnumber});
417 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
419 if ($form->{notes}) {
420 my $notes = $form->like(lc $form->{notes});
421 $where .= " AND lower(a.notes) LIKE '$notes'";
424 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
425 if $form->{transdatefrom};
426 $where .= " AND a.transdate <= '$form->{transdateto}'"
427 if $form->{transdateto};
428 if ($form->{open} || $form->{closed}) {
429 unless ($form->{open} && $form->{closed}) {
430 $where .= " AND a.amount <> a.paid" if ($form->{open});
431 $where .= " AND a.amount = a.paid" if ($form->{closed});
435 my @a = (transdate, invnumber, name);
436 push @a, "employee" if $form->{l_employee};
437 my $sortorder = join ', ', $form->sort_columns(@a);
438 $sortorder = $form->{sort} unless $sortorder;
440 $query .= "WHERE $where
441 ORDER by $sortorder";
443 my $sth = $dbh->prepare($query);
444 $sth->execute || $form->dberror($query);
446 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
447 push @{ $form->{AR} }, $ar;
453 $main::lxdebug->leave_sub();