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);
80 # parse tax_$i for later
81 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"}) * -1;
85 $form->{amount} = $amount;
87 # taxincluded doesn't make sense if there is no amount
88 $form->{taxincluded} = 0 if ($form->{amount} == 0);
90 for $i (1 .. $form->{rowcount}) {
91 ($form->{"taxkey_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
94 qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{"taxkey_$i"}|;
95 $sth = $dbh->prepare($query);
96 $sth->execute || $form->dberror($query);
97 ($form->{AP_amounts}{"tax_$i"}, $form->{"taxrate_$i"}) =
99 $form->{AP_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
100 $form->{AP_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
103 if ($form->{taxincluded} *= 1) {
104 if (!$form->{"korrektur_$i"}) {
106 $form->{"amount_$i"} -
107 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
109 $tax = $form->{"tax_$i"};
111 $amount = $form->{"amount_$i"} - $tax;
112 $form->{"amount_$i"} = $form->round_amount($amount, 2);
113 $diff += $amount - $form->{"amount_$i"};
114 $form->{"tax_$i"} = $form->round_amount($tax, 2);
115 $form->{netamount} += $form->{"amount_$i"};
117 if (!$form->{"korrektur_$i"}) {
118 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
120 $tax = $form->{"tax_$i"};
123 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
124 $form->{netamount} += $form->{"amount_$i"};
126 $form->{total_tax} += $form->{"tax_$i"} * -1;
129 # adjust paidaccounts if there is no date in the last row
130 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
132 $form->{invpaid} = 0;
133 $form->{netamount} *= -1;
136 for my $i (1 .. $form->{paidaccounts}) {
138 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
141 $form->{invpaid} += $form->{"paid_$i"};
142 $form->{datepaid} = $form->{"datepaid_$i"};
147 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
149 # store invoice total, this goes into ap table
150 $form->{invtotal} = $form->{netamount} + $form->{total_tax};
152 # amount for total AP
153 $form->{payables} = $form->{invtotal};
157 # if we have an id delete old records
160 # delete detail records
161 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
163 $dbh->do($query) || $form->dberror($query);
166 my $uid = rand() . time;
168 $uid .= $form->{login};
170 $uid = substr($uid, 2, 75);
172 $query = qq|INSERT INTO ap (invnumber, employee_id)
173 VALUES ('$uid', (SELECT e.id FROM employee e
174 WHERE e.login = '$form->{login}') )|;
175 $dbh->do($query) || $form->dberror($query);
177 $query = qq|SELECT a.id FROM ap a
178 WHERE a.invnumber = '$uid'|;
179 $sth = $dbh->prepare($query);
180 $sth->execute || $form->dberror($query);
182 ($form->{id}) = $sth->fetchrow_array;
187 $form->{invnumber} = $form->{id} unless $form->{invnumber};
190 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
192 $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
193 my $datepaid = ($form->{invpaid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
195 $query = qq|UPDATE ap SET
196 invnumber = '$form->{invnumber}',
197 transdate = '$form->{transdate}',
198 ordnumber = '$form->{ordnumber}',
199 vendor_id = $form->{vendor_id},
200 taxincluded = '$form->{taxincluded}',
201 amount = $form->{invtotal},
202 duedate = '$form->{duedate}',
203 paid = $form->{invpaid},
204 datepaid = $datepaid,
205 netamount = $form->{netamount},
206 curr = '$form->{currency}',
207 notes = '$form->{notes}',
208 department_id = $form->{department_id}
209 WHERE id = $form->{id}
211 $dbh->do($query) || $form->dberror($query);
213 # update exchangerate
214 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
215 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
216 $form->{exchangerate});
219 # add individual transactions
220 for $i (1 .. $form->{rowcount}) {
221 if ($form->{"amount_$i"} != 0) {
222 $project_id = 'NULL';
223 if ("amount_$i" =~ /amount_/) {
224 if ($form->{"project_id_$i"} && $form->{"projectnumber_$i"}) {
225 $project_id = $form->{"project_id_$i"};
228 if ("amount_$i" =~ /amount/) {
229 $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
232 # insert detail records in acc_trans
233 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
235 VALUES ($form->{id}, (SELECT c.id FROM chart c
236 WHERE c.accno = '$form->{AP_amounts}{"amount_$i"}'),
237 $form->{"amount_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
238 $dbh->do($query) || $form->dberror($query);
240 if ($form->{"tax_$i"} != 0) {
242 # insert detail records in acc_trans
244 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
246 VALUES ($form->{id}, (SELECT c.id FROM chart c
247 WHERE c.accno = '$form->{AP_amounts}{"tax_$i"}'),
248 $form->{"tax_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
249 $dbh->do($query) || $form->dberror($query);
256 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
258 VALUES ($form->{id}, (SELECT c.id FROM chart c
259 WHERE c.accno = '$form->{AP_amounts}{payables}'),
260 $form->{payables}, '$form->{transdate}', $project_id)|;
261 $dbh->do($query) || $form->dberror($query);
263 # if there is no amount but a payment record a payable
264 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
265 $form->{payables} = $form->{invpaid};
268 # add paid transactions
269 for my $i (1 .. $form->{paidaccounts}) {
270 if ($form->{"paid_$i"} != 0) {
273 if ($form->{currency} eq $form->{defaultcurrency}) {
274 $form->{"exchangerate_$i"} = 1;
277 $form->check_exchangerate($myconfig, $form->{currency},
278 $form->{"datepaid_$i"}, 'sell');
280 $form->{"exchangerate_$i"} =
283 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
285 $form->{"AP_paid_$i"} =~ s/\"//g;
289 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
290 $form->{"datepaid_$i"} = $form->{transdate}
291 unless ($form->{"datepaid_$i"});
293 # if there is no amount and invtotal is zero there is no exchangerate
294 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
295 $form->{exchangerate} = $form->{"exchangerate_$i"};
299 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
301 if ($form->{payables}) {
302 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
305 (SELECT c.id FROM chart c
306 WHERE c.accno = '$form->{AP}{payables}'),
307 $amount, '$form->{"datepaid_$i"}')|;
308 $dbh->do($query) || $form->dberror($query);
310 $form->{payables} = $amount;
312 $form->{"memo_$i"} =~ s/\'/\'\'/g;
315 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
316 transdate, source, memo)
318 (SELECT c.id FROM chart c
319 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
320 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
321 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
322 $dbh->do($query) || $form->dberror($query);
324 # add exchange rate difference
327 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1),
330 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
331 transdate, fx_transaction, cleared)
333 (SELECT c.id FROM chart c
334 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
335 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
337 $dbh->do($query) || $form->dberror($query);
340 # exchangerate gain/loss
344 ($form->{exchangerate} - $form->{"exchangerate_$i"}),
348 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
349 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
350 transdate, fx_transaction, cleared)
351 VALUES ($form->{id}, (SELECT c.id FROM chart c
352 WHERE c.accno = '$accno'),
353 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
354 $dbh->do($query) || $form->dberror($query);
357 # update exchange rate record
358 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
359 $form->update_exchangerate($dbh, $form->{currency},
360 $form->{"datepaid_$i"},
361 0, $form->{"exchangerate_$i"});
366 my $rc = $dbh->commit;
369 $main::lxdebug->leave_sub();
374 sub delete_transaction {
375 $main::lxdebug->enter_sub();
377 my ($self, $myconfig, $form, $spool) = @_;
379 # connect to database
380 my $dbh = $form->dbconnect_noauto($myconfig);
382 my $query = qq|DELETE FROM ap WHERE id = $form->{id}|;
383 $dbh->do($query) || $form->dberror($query);
385 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
386 $dbh->do($query) || $form->dberror($query);
388 # commit and redirect
389 my $rc = $dbh->commit;
392 $main::lxdebug->leave_sub();
397 sub ap_transactions {
398 $main::lxdebug->enter_sub();
400 my ($self, $myconfig, $form) = @_;
402 # connect to database
403 my $dbh = $form->dbconnect($myconfig);
405 my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate,
406 a.amount, a.paid, a.ordnumber, v.name, a.invoice,
407 a.netamount, a.datepaid, a.notes, e.name AS employee
409 JOIN vendor v ON (a.vendor_id = v.id)
410 LEFT JOIN employee e ON (a.employee_id = e.id)|;
414 if ($form->{vendor_id}) {
415 $where .= " AND a.vendor_id = $form->{vendor_id}";
417 if ($form->{vendor}) {
418 my $vendor = $form->like(lc $form->{vendor});
419 $where .= " AND lower(v.name) LIKE '$vendor'";
422 if ($form->{department}) {
423 my ($null, $department_id) = split /--/, $form->{department};
424 $where .= " AND a.department_id = $department_id";
426 if ($form->{invnumber}) {
427 my $invnumber = $form->like(lc $form->{invnumber});
428 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
430 if ($form->{ordnumber}) {
431 my $ordnumber = $form->like(lc $form->{ordnumber});
432 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
434 if ($form->{notes}) {
435 my $notes = $form->like(lc $form->{notes});
436 $where .= " AND lower(a.notes) LIKE '$notes'";
439 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
440 if $form->{transdatefrom};
441 $where .= " AND a.transdate <= '$form->{transdateto}'"
442 if $form->{transdateto};
443 if ($form->{open} || $form->{closed}) {
444 unless ($form->{open} && $form->{closed}) {
445 $where .= " AND a.amount <> a.paid" if ($form->{open});
446 $where .= " AND a.amount = a.paid" if ($form->{closed});
450 my @a = (transdate, invnumber, name);
451 push @a, "employee" if $self->{l_employee};
452 my $sortorder = join ', ', $form->sort_columns(@a);
453 $sortorder = $form->{sort} if $form->{sort};
455 $query .= "WHERE $where
456 ORDER by $sortorder";
458 my $sth = $dbh->prepare($query);
459 $sth->execute || $form->dberror($query);
461 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
462 push @{ $form->{AP} }, $ap;
468 $main::lxdebug->leave_sub();