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 #======================================================================
38 sub post_transaction {
39 $main::lxdebug->enter_sub();
41 my ($self, $myconfig, $form) = @_;
43 my ($null, $taxrate, $amount, $tax, $diff);
47 my $dbh = $form->dbconnect_noauto($myconfig);
49 if ($form->{currency} eq $form->{defaultcurrency}) {
50 $form->{exchangerate} = 1;
52 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
55 $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
60 $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2);
62 $form->{netamount} += $form->{"amount_$i"};
66 $form->{amount} = $form->{netamount};
69 # taxincluded doesn't make sense if there is no amount
71 $form->{taxincluded} = 0 if ($form->{amount} == 0);
73 $query = qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{taxkey}|;
74 $sth = $dbh->prepare($query);
75 $sth->execute || $form->dberror($query);
76 ($form->{AR}{tax}, $form->{taxrate}) = $sth->fetchrow_array;
79 $form->{tax} = $form->{amount_1} * $form->{taxrate};
80 $form->{tax} = $form->round_amount($form->{tax} * $form->{exchangerate}, 2);
81 $form->{total_tax} += $form->{tax};
83 # adjust paidaccounts if there is no date in the last row
84 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
87 for $i (1 .. $form->{paidaccounts}) {
88 $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
90 $form->{paid} += $form->{"paid_$i"};
91 $form->{datepaid} = $form->{"datepaid_$i"};
96 if ($form->{taxincluded} *= 1) {
98 $tax = $form->{"amount_$i"} - ( $form->{"amount_$i"} / ($form->{taxrate} + 1)) ;
99 $amount = $form->{"amount_$i"} - $tax;
100 $form->{"amount_$i"} = $form->round_amount($amount, 2);
101 $diff += $amount - $form->{"amount_$i"};
102 $form->{tax} = $form->round_amount($tax,2);
103 $form->{total_tax} = $form->{tax};
106 # deduct difference from amount_1
107 # $form->{amount_1} += $form->round_amount($diff, 2);
108 $form->{netamount} = $form->{amount_1};
112 $form->{amount} = $form->{netamount} + $form->{total_tax};
113 $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
116 my ($query, $sth, $null);
118 ($null, $form->{employee_id}) = split /--/, $form->{employee};
119 unless ($form->{employee_id}) {
120 $form->get_employee($dbh);
123 # if we have an id delete old records
126 # delete detail records
127 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
128 $dbh->do($query) || $form->dberror($query);
131 my $uid = rand().time;
133 $uid .= $form->{login};
135 $uid = substr($uid,2,75);
137 $query = qq|INSERT INTO ar (invnumber, employee_id)
138 VALUES ('$uid', $form->{employee_id})|;
139 $dbh->do($query) || $form->dberror($query);
141 $query = qq|SELECT a.id FROM ar a
142 WHERE a.invnumber = '$uid'|;
143 $sth = $dbh->prepare($query);
144 $sth->execute || $form->dberror($query);
146 ($form->{id}) = $sth->fetchrow_array;
153 ($null, $form->{department_id}) = split(/--/, $form->{department});
154 $form->{department_id} *= 1;
157 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
159 # record last payment date in ar table
160 $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
161 my $datepaid = ($form->{paid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
163 $query = qq|UPDATE ar set
164 invnumber = '$form->{invnumber}',
165 ordnumber = '$form->{ordnumber}',
166 transdate = '$form->{transdate}',
167 customer_id = $form->{customer_id},
168 taxincluded = '$form->{taxincluded}',
169 amount = $form->{amount},
170 duedate = '$form->{duedate}',
171 paid = $form->{paid},
172 datepaid = $datepaid,
173 netamount = $form->{netamount},
174 curr = '$form->{currency}',
175 notes = '$form->{notes}',
176 department_id = $form->{department_id},
177 employee_id = $form->{employee_id}
178 WHERE id = $form->{id}|;
179 $dbh->do($query) || $form->dberror($query);
182 # amount for AR account
183 $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
186 # update exchangerate
187 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
188 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
190 # add individual transactions for AR, amount and taxes
191 foreach my $item (keys %{ $form->{AR} }) {
192 if ($form->{$item} != 0) {
193 $project_id = 'NULL';
194 if ($item =~ /amount_/) {
195 if ($form->{"project_id_$'"} && $form->{"projectnumber_$'"}) {
196 $project_id = $form->{"project_id_$'"};
200 # insert detail records in acc_trans
201 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
203 VALUES ($form->{id}, (SELECT c.id FROM chart c
204 WHERE c.accno = '$form->{AR}{$item}'),
205 $form->{$item}, '$form->{transdate}', $project_id, '$form->{taxkey}')|;
206 $dbh->do($query) || $form->dberror($query);
211 # add paid transactions
212 for my $i (1 .. $form->{paidaccounts}) {
213 if ($form->{"paid_$i"} != 0) {
215 $form->{"AR_paid_$i"} =~ s/\"//g;
216 ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
217 $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
220 if ($form->{currency} eq $form->{defaultcurrency}) {
221 $form->{"exchangerate_$i"} = 1;
223 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
225 $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
229 # if there is no amount and invtotal is zero there is no exchangerate
230 if ($form->{amount} == 0 && $form->{netamount} == 0) {
231 $form->{exchangerate} = $form->{"exchangerate_$i"};
235 $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
237 if ($form->{receivables} != 0) {
239 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
242 (SELECT c.id FROM chart c
243 WHERE c.accno = '$form->{AR}{receivables}'),
244 $amount, '$form->{"datepaid_$i"}')|;
245 $dbh->do($query) || $form->dberror($query);
247 $form->{receivables} = $amount;
249 $form->{"memo_$i"} =~ s/\'/\'\'/g;
251 if ($form->{"paid_$i"} != 0) {
253 $amount = $form->{"paid_$i"} * -1;
254 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
255 transdate, source, memo)
257 (SELECT c.id FROM chart c
258 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
259 $amount, '$form->{"datepaid_$i"}',
260 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
261 $dbh->do($query) || $form->dberror($query);
264 # exchangerate difference for payment
265 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1, 2);
268 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
269 transdate, fx_transaction, cleared)
271 (SELECT c.id FROM chart c
272 WHERE c.accno = '$form->{AR}{"paid_$i"}'),
273 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
274 $dbh->do($query) || $form->dberror($query);
277 # exchangerate gain/loss
278 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 2);
281 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
282 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
283 transdate, fx_transaction, cleared)
284 VALUES ($form->{id}, (SELECT c.id FROM chart c
285 WHERE c.accno = '$accno'),
286 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
287 $dbh->do($query) || $form->dberror($query);
291 # update exchangerate record
292 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
293 $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
299 my $rc = $dbh->commit;
302 $main::lxdebug->leave_sub();
309 sub delete_transaction {
310 $main::lxdebug->enter_sub();
312 my ($self, $myconfig, $form) = @_;
314 # connect to database, turn AutoCommit off
315 my $dbh = $form->dbconnect_noauto($myconfig);
317 my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
318 $dbh->do($query) || $form->dberror($query);
320 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
321 $dbh->do($query) || $form->dberror($query);
324 my $rc = $dbh->commit;
327 $main::lxdebug->leave_sub();
334 sub ar_transactions {
335 $main::lxdebug->enter_sub();
337 my ($self, $myconfig, $form) = @_;
339 # connect to database
340 my $dbh = $form->dbconnect($myconfig);
342 my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
343 a.duedate, a.netamount, a.amount, a.paid, c.name,
344 a.invoice, a.datepaid, a.terms, a.notes, a.shipvia,
348 JOIN customer c ON (a.customer_id = c.id)
349 LEFT JOIN employee e ON (a.employee_id = e.id)|;
352 if ($form->{customer_id}) {
353 $where .= " AND a.customer_id = $form->{customer_id}";
355 if ($form->{customer}) {
356 my $customer = $form->like(lc $form->{customer});
357 $where .= " AND lower(c.name) LIKE '$customer'";
360 if ($form->{department}) {
361 my ($null, $department_id) = split /--/, $form->{department};
362 $where .= " AND a.department_id = $department_id";
364 if ($form->{invnumber}) {
365 my $invnumber = $form->like(lc $form->{invnumber});
366 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
368 if ($form->{ordnumber}) {
369 my $ordnumber = $form->like(lc $form->{ordnumber});
370 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
372 if ($form->{notes}) {
373 my $notes = $form->like(lc $form->{notes});
374 $where .= " AND lower(a.notes) LIKE '$notes'";
377 $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
378 $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
379 if ($form->{open} || $form->{closed}) {
380 unless ($form->{open} && $form->{closed}) {
381 $where .= " AND a.amount <> a.paid" if ($form->{open});
382 $where .= " AND a.amount = a.paid" if ($form->{closed});
386 my @a = (transdate, invnumber, name);
387 push @a, "employee" if $form->{l_employee};
388 my $sortorder = join ', ', $form->sort_columns(@a);
389 $sortorder = $form->{sort} unless $sortorder;
391 $query .= "WHERE $where
392 ORDER by $sortorder";
394 my $sth = $dbh->prepare($query);
395 $sth->execute || $form->dberror($query);
397 while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
398 push @{ $form->{AR} }, $ar;
404 $main::lxdebug->leave_sub();