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 Payables database backend routines
33 #======================================================================
37 sub post_transaction {
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form) = @_;
43 my $dbh = $form->dbconnect_noauto($myconfig);
45 my ($null, $taxrate, $amount);
48 ($null, $form->{department_id}) = split(/--/, $form->{department});
49 $form->{department_id} *= 1;
51 if ($form->{currency} eq $form->{defaultcurrency}) {
52 $form->{exchangerate} = 1;
55 $form->check_exchangerate($myconfig, $form->{currency},
56 $form->{transdate}, 'sell');
58 $form->{exchangerate} =
61 : $form->parse_amount($myconfig, $form->{exchangerate});
64 for $i (1 .. $form->{rowcount}) {
65 $form->{AP_amounts}{"amount_$i"} =
66 (split(/--/, $form->{"AP_amount_$i"}))[0];
68 ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
69 ($form->{AP}{payables}) = split(/--/, $form->{APselected});
71 # reverse and parse amounts
72 for my $i (1 .. $form->{rowcount}) {
73 $form->{"amount_$i"} =
75 $form->parse_amount($myconfig, $form->{"amount_$i"}) *
76 $form->{exchangerate} * -1,
78 $amount += ($form->{"amount_$i"} * -1);
82 $form->{amount} = $amount;
84 # taxincluded doesn't make sense if there is no amount
85 $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->{AP_amounts}{"tax_$i"}, $form->{"taxrate_$i"}) =
96 $form->{AP_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
97 $form->{AP_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
100 if (!$form->{"korrektur_$i"}) {
101 if ($form->{taxincluded} *= 1) {
103 $form->{"amount_$i"} -
104 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
105 $amount = $form->{"amount_$i"} - $tax;
106 $form->{"amount_$i"} = $form->round_amount($amount, 2);
107 $diff += $amount - $form->{"amount_$i"};
108 $form->{"tax_$i"} = $form->round_amount($tax, 2);
109 $form->{netamount} += $form->{"amount_$i"};
111 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
113 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
114 $form->{netamount} += $form->{"amount_$i"};
117 $form->{total_tax} += $form->{"tax_$i"} * -1;
120 # adjust paidaccounts if there is no date in the last row
121 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
123 $form->{invpaid} = 0;
124 $form->{netamount} *= -1;
127 for my $i (1 .. $form->{paidaccounts}) {
129 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
132 $form->{invpaid} += $form->{"paid_$i"};
133 $form->{datepaid} = $form->{"datepaid_$i"};
138 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
140 # store invoice total, this goes into ap table
141 $form->{invtotal} = $form->{netamount} + $form->{total_tax};
143 # amount for total AP
144 $form->{payables} = $form->{invtotal};
145 print(STDERR $form->{payables},
155 # if we have an id delete old records
158 # delete detail records
159 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
161 $dbh->do($query) || $form->dberror($query);
164 my $uid = rand() . time;
166 $uid .= $form->{login};
168 $uid = substr($uid, 2, 75);
170 $query = qq|INSERT INTO ap (invnumber, employee_id)
171 VALUES ('$uid', (SELECT e.id FROM employee e
172 WHERE e.login = '$form->{login}') )|;
173 $dbh->do($query) || $form->dberror($query);
175 $query = qq|SELECT a.id FROM ap a
176 WHERE a.invnumber = '$uid'|;
177 $sth = $dbh->prepare($query);
178 $sth->execute || $form->dberror($query);
180 ($form->{id}) = $sth->fetchrow_array;
185 $form->{invnumber} = $form->{id} unless $form->{invnumber};
188 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
190 $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
191 my $datepaid = ($form->{invpaid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
193 $query = qq|UPDATE ap SET
194 invnumber = '$form->{invnumber}',
195 transdate = '$form->{transdate}',
196 ordnumber = '$form->{ordnumber}',
197 vendor_id = $form->{vendor_id},
198 taxincluded = '$form->{taxincluded}',
199 amount = $form->{invtotal},
200 duedate = '$form->{duedate}',
201 paid = $form->{invpaid},
202 datepaid = $datepaid,
203 netamount = $form->{netamount},
204 curr = '$form->{currency}',
205 notes = '$form->{notes}',
206 department_id = $form->{department_id}
207 WHERE id = $form->{id}
209 $dbh->do($query) || $form->dberror($query);
211 # update exchangerate
212 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
213 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
214 $form->{exchangerate});
217 # add individual transactions
218 for $i (1 .. $form->{rowcount}) {
219 if ($form->{"amount_$i"} != 0) {
220 $project_id = 'NULL';
221 if ("amount_$i" =~ /amount_/) {
222 if ($form->{"project_id_$i"} && $form->{"projectnumber_$i"}) {
223 $project_id = $form->{"project_id_$i"};
226 if ("amount_$i" =~ /amount/) {
227 $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
230 # insert detail records in acc_trans
231 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
233 VALUES ($form->{id}, (SELECT c.id FROM chart c
234 WHERE c.accno = '$form->{AP_amounts}{"amount_$i"}'),
235 $form->{"amount_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
236 $dbh->do($query) || $form->dberror($query);
238 if ($form->{"tax_$i"} != 0) {
240 # insert detail records in acc_trans
242 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
244 VALUES ($form->{id}, (SELECT c.id FROM chart c
245 WHERE c.accno = '$form->{AP_amounts}{"tax_$i"}'),
246 $form->{"tax_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
247 $dbh->do($query) || $form->dberror($query);
254 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
256 VALUES ($form->{id}, (SELECT c.id FROM chart c
257 WHERE c.accno = '$form->{AP_amounts}{payables}'),
258 $form->{payables}, '$form->{transdate}', $project_id)|;
259 $dbh->do($query) || $form->dberror($query);
261 # if there is no amount but a payment record a payable
262 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
263 $form->{payables} = $form->{invpaid};
266 # add paid transactions
267 for my $i (1 .. $form->{paidaccounts}) {
268 if ($form->{"paid_$i"} != 0) {
271 if ($form->{currency} eq $form->{defaultcurrency}) {
272 $form->{"exchangerate_$i"} = 1;
275 $form->check_exchangerate($myconfig, $form->{currency},
276 $form->{"datepaid_$i"}, 'sell');
278 $form->{"exchangerate_$i"} =
281 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
283 $form->{"AP_paid_$i"} =~ s/\"//g;
287 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
288 $form->{"datepaid_$i"} = $form->{transdate}
289 unless ($form->{"datepaid_$i"});
291 # if there is no amount and invtotal is zero there is no exchangerate
292 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
293 $form->{exchangerate} = $form->{"exchangerate_$i"};
297 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
299 if ($form->{payables}) {
300 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
303 (SELECT c.id FROM chart c
304 WHERE c.accno = '$form->{AP}{payables}'),
305 $amount, '$form->{"datepaid_$i"}')|;
306 $dbh->do($query) || $form->dberror($query);
308 $form->{payables} = $amount;
310 $form->{"memo_$i"} =~ s/\'/\'\'/g;
313 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
314 transdate, source, memo)
316 (SELECT c.id FROM chart c
317 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
318 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
319 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
320 $dbh->do($query) || $form->dberror($query);
322 # add exchange rate difference
325 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1),
328 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
329 transdate, fx_transaction, cleared)
331 (SELECT c.id FROM chart c
332 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
333 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
335 $dbh->do($query) || $form->dberror($query);
338 # exchangerate gain/loss
342 ($form->{exchangerate} - $form->{"exchangerate_$i"}),
346 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
347 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
348 transdate, fx_transaction, cleared)
349 VALUES ($form->{id}, (SELECT c.id FROM chart c
350 WHERE c.accno = '$accno'),
351 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
352 $dbh->do($query) || $form->dberror($query);
355 # update exchange rate record
356 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
357 $form->update_exchangerate($dbh, $form->{currency},
358 $form->{"datepaid_$i"},
359 0, $form->{"exchangerate_$i"});
364 my $rc = $dbh->commit;
367 $main::lxdebug->leave_sub();
372 sub delete_transaction {
373 $main::lxdebug->enter_sub();
375 my ($self, $myconfig, $form, $spool) = @_;
377 # connect to database
378 my $dbh = $form->dbconnect_noauto($myconfig);
380 my $query = qq|DELETE FROM ap WHERE id = $form->{id}|;
381 $dbh->do($query) || $form->dberror($query);
383 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
384 $dbh->do($query) || $form->dberror($query);
386 # commit and redirect
387 my $rc = $dbh->commit;
390 $main::lxdebug->leave_sub();
395 sub ap_transactions {
396 $main::lxdebug->enter_sub();
398 my ($self, $myconfig, $form) = @_;
400 # connect to database
401 my $dbh = $form->dbconnect($myconfig);
403 my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate,
404 a.amount, a.paid, a.ordnumber, v.name, a.invoice,
405 a.netamount, a.datepaid, a.notes, e.name AS employee
407 JOIN vendor v ON (a.vendor_id = v.id)
408 LEFT JOIN employee e ON (a.employee_id = e.id)|;
412 if ($form->{vendor_id}) {
413 $where .= " AND a.vendor_id = $form->{vendor_id}";
415 if ($form->{vendor}) {
416 my $vendor = $form->like(lc $form->{vendor});
417 $where .= " AND lower(v.name) LIKE '$vendor'";
420 if ($form->{department}) {
421 my ($null, $department_id) = split /--/, $form->{department};
422 $where .= " AND a.department_id = $department_id";
424 if ($form->{invnumber}) {
425 my $invnumber = $form->like(lc $form->{invnumber});
426 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
428 if ($form->{ordnumber}) {
429 my $ordnumber = $form->like(lc $form->{ordnumber});
430 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
432 if ($form->{notes}) {
433 my $notes = $form->like(lc $form->{notes});
434 $where .= " AND lower(a.notes) LIKE '$notes'";
437 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
438 if $form->{transdatefrom};
439 $where .= " AND a.transdate <= '$form->{transdateto}'"
440 if $form->{transdateto};
441 if ($form->{open} || $form->{closed}) {
442 unless ($form->{open} && $form->{closed}) {
443 $where .= " AND a.amount <> a.paid" if ($form->{open});
444 $where .= " AND a.amount = a.paid" if ($form->{closed});
448 my @a = (transdate, invnumber, name);
449 push @a, "employee" if $self->{l_employee};
450 my $sortorder = join ', ', $form->sort_columns(@a);
451 $sortorder = $form->{sort} if $form->{sort};
453 $query .= "WHERE $where
454 ORDER by $sortorder";
456 my $sth = $dbh->prepare($query);
457 $sth->execute || $form->dberror($query);
459 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
460 push @{ $form->{AP} }, $ap;
466 $main::lxdebug->leave_sub();