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 #======================================================================
37 sub post_transaction {
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form) = @_;
42 my ($null, $taxrate, $amount, $tax, $diff);
46 my $dbh = $form->dbconnect_noauto($myconfig);
48 if ($form->{currency} eq $form->{defaultcurrency}) {
49 $form->{exchangerate} = 1;
52 $form->check_exchangerate($myconfig, $form->{currency},
53 $form->{transdate}, 'buy');
56 $form->{exchangerate} =
59 : $form->parse_amount($myconfig, $form->{exchangerate});
63 $form->{"amount_$i"} =
64 $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"})
65 * $form->{exchangerate},
68 $form->{netamount} += $form->{"amount_$i"};
73 $form->{amount} = $form->{netamount};
77 # taxincluded doesn't make sense if there is no amount
79 $form->{taxincluded} = 0 if ($form->{amount} == 0);
82 qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{taxkey}|;
83 $sth = $dbh->prepare($query);
84 $sth->execute || $form->dberror($query);
85 ($form->{AR}{tax}, $form->{taxrate}) = $sth->fetchrow_array;
88 $form->{tax} = $form->{amount_1} * $form->{taxrate};
89 $form->{tax} = $form->round_amount($form->{tax} * $form->{exchangerate}, 2);
90 $form->{total_tax} += $form->{tax};
92 # adjust paidaccounts if there is no date in the last row
93 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
97 for $i (1 .. $form->{paidaccounts}) {
99 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
102 $form->{paid} += $form->{"paid_$i"};
103 $form->{datepaid} = $form->{"datepaid_$i"};
107 if ($form->{taxincluded} *= 1) {
110 $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{taxrate} + 1));
111 $amount = $form->{"amount_$i"} - $tax;
112 $form->{"amount_$i"} = $form->round_amount($amount, 2);
113 $diff += $amount - $form->{"amount_$i"};
114 $form->{tax} = $form->round_amount($tax, 2);
115 $form->{total_tax} = $form->{tax};
118 # deduct difference from amount_1
119 # $form->{amount_1} += $form->round_amount($diff, 2);
120 $form->{netamount} = $form->{amount_1};
124 $form->{amount} = $form->{netamount} + $form->{total_tax};
126 $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
128 my ($query, $sth, $null);
130 ($null, $form->{employee_id}) = split /--/, $form->{employee};
131 unless ($form->{employee_id}) {
132 $form->get_employee($dbh);
135 # if we have an id delete old records
138 # delete detail records
139 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
140 $dbh->do($query) || $form->dberror($query);
143 my $uid = rand() . time;
145 $uid .= $form->{login};
147 $uid = substr($uid, 2, 75);
149 $query = qq|INSERT INTO ar (invnumber, employee_id)
150 VALUES ('$uid', $form->{employee_id})|;
151 $dbh->do($query) || $form->dberror($query);
153 $query = qq|SELECT a.id FROM ar a
154 WHERE a.invnumber = '$uid'|;
155 $sth = $dbh->prepare($query);
156 $sth->execute || $form->dberror($query);
158 ($form->{id}) = $sth->fetchrow_array;
164 ($null, $form->{department_id}) = split(/--/, $form->{department});
165 $form->{department_id} *= 1;
168 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
170 # record last payment date in ar table
171 $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
172 my $datepaid = ($form->{paid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
174 $query = qq|UPDATE ar set
175 invnumber = '$form->{invnumber}',
176 ordnumber = '$form->{ordnumber}',
177 transdate = '$form->{transdate}',
178 customer_id = $form->{customer_id},
179 taxincluded = '$form->{taxincluded}',
180 amount = $form->{amount},
181 duedate = '$form->{duedate}',
182 paid = $form->{paid},
183 datepaid = $datepaid,
184 netamount = $form->{netamount},
185 curr = '$form->{currency}',
186 notes = '$form->{notes}',
187 department_id = $form->{department_id},
188 employee_id = $form->{employee_id}
189 WHERE id = $form->{id}|;
190 $dbh->do($query) || $form->dberror($query);
192 # amount for AR account
193 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
195 # update exchangerate
196 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
197 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
198 $form->{exchangerate}, 0);
201 # add individual transactions for AR, amount and taxes
202 foreach my $item (keys %{ $form->{AR} }) {
203 if ($form->{$item} != 0) {
204 $project_id = 'NULL';
205 if ($item =~ /amount_/) {
206 if ($form->{"project_id_$'"} && $form->{"projectnumber_$'"}) {
207 $project_id = $form->{"project_id_$'"};
211 # insert detail records in acc_trans
212 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
214 VALUES ($form->{id}, (SELECT c.id FROM chart c
215 WHERE c.accno = '$form->{AR}{$item}'),
216 $form->{$item}, '$form->{transdate}', $project_id, '$form->{taxkey}')|;
217 $dbh->do($query) || $form->dberror($query);
221 # add paid transactions
222 for my $i (1 .. $form->{paidaccounts}) {
223 if ($form->{"paid_$i"} != 0) {
225 $form->{"AR_paid_$i"} =~ s/\"//g;
226 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
227 $form->{"datepaid_$i"} = $form->{transdate}
228 unless ($form->{"datepaid_$i"});
231 if ($form->{currency} eq $form->{defaultcurrency}) {
232 $form->{"exchangerate_$i"} = 1;
235 $form->check_exchangerate($myconfig, $form->{currency},
236 $form->{"datepaid_$i"}, 'buy');
238 $form->{"exchangerate_$i"} =
241 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
244 # if there is no amount and invtotal is zero there is no exchangerate
245 if ($form->{amount} == 0 && $form->{netamount} == 0) {
246 $form->{exchangerate} = $form->{"exchangerate_$i"};
251 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
253 if ($form->{receivables} != 0) {
256 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
259 (SELECT c.id FROM chart c
260 WHERE c.accno = '$form->{AR}{receivables}'),
261 $amount, '$form->{"datepaid_$i"}')|;
262 $dbh->do($query) || $form->dberror($query);
264 $form->{receivables} = $amount;
266 $form->{"memo_$i"} =~ s/\'/\'\'/g;
268 if ($form->{"paid_$i"} != 0) {
271 $amount = $form->{"paid_$i"} * -1;
272 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
273 transdate, source, memo)
275 (SELECT c.id FROM chart c
276 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
277 $amount, '$form->{"datepaid_$i"}',
278 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
279 $dbh->do($query) || $form->dberror($query);
281 # exchangerate difference for payment
284 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1,
288 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
289 transdate, fx_transaction, cleared)
291 (SELECT c.id FROM chart c
292 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
293 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
294 $dbh->do($query) || $form->dberror($query);
297 # exchangerate gain/loss
301 ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1,
306 ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
307 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
308 transdate, fx_transaction, cleared)
309 VALUES ($form->{id}, (SELECT c.id FROM chart c
310 WHERE c.accno = '$accno'),
311 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
312 $dbh->do($query) || $form->dberror($query);
316 # update exchangerate record
317 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
318 $form->update_exchangerate($dbh, $form->{currency},
319 $form->{"datepaid_$i"},
320 $form->{"exchangerate_$i"}, 0);
325 my $rc = $dbh->commit;
328 $main::lxdebug->leave_sub();
333 sub delete_transaction {
334 $main::lxdebug->enter_sub();
336 my ($self, $myconfig, $form) = @_;
338 # connect to database, turn AutoCommit off
339 my $dbh = $form->dbconnect_noauto($myconfig);
341 my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
342 $dbh->do($query) || $form->dberror($query);
344 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
345 $dbh->do($query) || $form->dberror($query);
348 my $rc = $dbh->commit;
351 $main::lxdebug->leave_sub();
356 sub ar_transactions {
357 $main::lxdebug->enter_sub();
359 my ($self, $myconfig, $form) = @_;
361 # connect to database
362 my $dbh = $form->dbconnect($myconfig);
364 my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
365 a.duedate, a.netamount, a.amount, a.paid, c.name,
366 a.invoice, a.datepaid, a.terms, a.notes, a.shipvia,
370 JOIN customer c ON (a.customer_id = c.id)
371 LEFT JOIN employee e ON (a.employee_id = e.id)|;
374 if ($form->{customer_id}) {
375 $where .= " AND a.customer_id = $form->{customer_id}";
377 if ($form->{customer}) {
378 my $customer = $form->like(lc $form->{customer});
379 $where .= " AND lower(c.name) LIKE '$customer'";
382 if ($form->{department}) {
383 my ($null, $department_id) = split /--/, $form->{department};
384 $where .= " AND a.department_id = $department_id";
386 if ($form->{invnumber}) {
387 my $invnumber = $form->like(lc $form->{invnumber});
388 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
390 if ($form->{ordnumber}) {
391 my $ordnumber = $form->like(lc $form->{ordnumber});
392 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
394 if ($form->{notes}) {
395 my $notes = $form->like(lc $form->{notes});
396 $where .= " AND lower(a.notes) LIKE '$notes'";
399 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
400 if $form->{transdatefrom};
401 $where .= " AND a.transdate <= '$form->{transdateto}'"
402 if $form->{transdateto};
403 if ($form->{open} || $form->{closed}) {
404 unless ($form->{open} && $form->{closed}) {
405 $where .= " AND a.amount <> a.paid" if ($form->{open});
406 $where .= " AND a.amount = a.paid" if ($form->{closed});
410 my @a = (transdate, invnumber, name);
411 push @a, "employee" if $form->{l_employee};
412 my $sortorder = join ', ', $form->sort_columns(@a);
413 $sortorder = $form->{sort} unless $sortorder;
415 $query .= "WHERE $where
416 ORDER by $sortorder";
418 my $sth = $dbh->prepare($query);
419 $sth->execute || $form->dberror($query);
421 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
422 push @{ $form->{AR} }, $ar;
428 $main::lxdebug->leave_sub();