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};
148 # if we have an id delete old records
151 # delete detail records
152 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
154 $dbh->do($query) || $form->dberror($query);
157 my $uid = rand() . time;
159 $uid .= $form->{login};
161 $uid = substr($uid, 2, 75);
163 $query = qq|INSERT INTO ap (invnumber, employee_id)
164 VALUES ('$uid', (SELECT e.id FROM employee e
165 WHERE e.login = '$form->{login}') )|;
166 $dbh->do($query) || $form->dberror($query);
168 $query = qq|SELECT a.id FROM ap a
169 WHERE a.invnumber = '$uid'|;
170 $sth = $dbh->prepare($query);
171 $sth->execute || $form->dberror($query);
173 ($form->{id}) = $sth->fetchrow_array;
178 $form->{invnumber} = $form->{id} unless $form->{invnumber};
181 map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
183 $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
184 my $datepaid = ($form->{invpaid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
186 $query = qq|UPDATE ap SET
187 invnumber = '$form->{invnumber}',
188 transdate = '$form->{transdate}',
189 ordnumber = '$form->{ordnumber}',
190 vendor_id = $form->{vendor_id},
191 taxincluded = '$form->{taxincluded}',
192 amount = $form->{invtotal},
193 duedate = '$form->{duedate}',
194 paid = $form->{invpaid},
195 datepaid = $datepaid,
196 netamount = $form->{netamount},
197 curr = '$form->{currency}',
198 notes = '$form->{notes}',
199 department_id = $form->{department_id}
200 WHERE id = $form->{id}
202 $dbh->do($query) || $form->dberror($query);
204 # update exchangerate
205 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
206 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
207 $form->{exchangerate});
210 # add individual transactions
211 for $i (1 .. $form->{rowcount}) {
212 if ($form->{"amount_$i"} != 0) {
213 $project_id = 'NULL';
214 if ("amount_$i" =~ /amount_/) {
215 if ($form->{"project_id_$i"} && $form->{"projectnumber_$i"}) {
216 $project_id = $form->{"project_id_$i"};
219 if ("amount_$i" =~ /amount/) {
220 $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
223 # insert detail records in acc_trans
224 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
226 VALUES ($form->{id}, (SELECT c.id FROM chart c
227 WHERE c.accno = '$form->{AP_amounts}{"amount_$i"}'),
228 $form->{"amount_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
229 $dbh->do($query) || $form->dberror($query);
231 if ($form->{"tax_$i"} != 0) {
233 # insert detail records in acc_trans
235 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
237 VALUES ($form->{id}, (SELECT c.id FROM chart c
238 WHERE c.accno = '$form->{AP_amounts}{"tax_$i"}'),
239 $form->{"tax_$i"}, '$form->{transdate}', $project_id, '$taxkey')|;
240 $dbh->do($query) || $form->dberror($query);
247 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
249 VALUES ($form->{id}, (SELECT c.id FROM chart c
250 WHERE c.accno = '$form->{AP_amounts}{payables}'),
251 $form->{payables}, '$form->{transdate}', $project_id)|;
252 $dbh->do($query) || $form->dberror($query);
254 # if there is no amount but a payment record a payable
255 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
256 $form->{payables} = $form->{invpaid};
259 # add paid transactions
260 for my $i (1 .. $form->{paidaccounts}) {
261 if ($form->{"paid_$i"} != 0) {
264 if ($form->{currency} eq $form->{defaultcurrency}) {
265 $form->{"exchangerate_$i"} = 1;
268 $form->check_exchangerate($myconfig, $form->{currency},
269 $form->{"datepaid_$i"}, 'sell');
271 $form->{"exchangerate_$i"} =
274 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
276 $form->{"AP_paid_$i"} =~ s/\"//g;
280 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
281 $form->{"datepaid_$i"} = $form->{transdate}
282 unless ($form->{"datepaid_$i"});
284 # if there is no amount and invtotal is zero there is no exchangerate
285 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
286 $form->{exchangerate} = $form->{"exchangerate_$i"};
290 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
292 if ($form->{payables}) {
293 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
296 (SELECT c.id FROM chart c
297 WHERE c.accno = '$form->{AP}{payables}'),
298 $amount, '$form->{"datepaid_$i"}')|;
299 $dbh->do($query) || $form->dberror($query);
301 $form->{payables} = $amount;
303 $form->{"memo_$i"} =~ s/\'/\'\'/g;
306 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
307 transdate, source, memo)
309 (SELECT c.id FROM chart c
310 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
311 $form->{"paid_$i"}, '$form->{"datepaid_$i"}',
312 '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
313 $dbh->do($query) || $form->dberror($query);
315 # add exchange rate difference
318 $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1),
321 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
322 transdate, fx_transaction, cleared)
324 (SELECT c.id FROM chart c
325 WHERE c.accno = '$form->{AP}{"paid_$i"}'),
326 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
328 $dbh->do($query) || $form->dberror($query);
331 # exchangerate gain/loss
335 ($form->{exchangerate} - $form->{"exchangerate_$i"}),
339 $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
340 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
341 transdate, fx_transaction, cleared)
342 VALUES ($form->{id}, (SELECT c.id FROM chart c
343 WHERE c.accno = '$accno'),
344 $amount, '$form->{"datepaid_$i"}', '1', '0')|;
345 $dbh->do($query) || $form->dberror($query);
348 # update exchange rate record
349 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
350 $form->update_exchangerate($dbh, $form->{currency},
351 $form->{"datepaid_$i"},
352 0, $form->{"exchangerate_$i"});
357 my $rc = $dbh->commit;
360 $main::lxdebug->leave_sub();
365 sub delete_transaction {
366 $main::lxdebug->enter_sub();
368 my ($self, $myconfig, $form, $spool) = @_;
370 # connect to database
371 my $dbh = $form->dbconnect_noauto($myconfig);
373 my $query = qq|DELETE FROM ap WHERE id = $form->{id}|;
374 $dbh->do($query) || $form->dberror($query);
376 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
377 $dbh->do($query) || $form->dberror($query);
379 # commit and redirect
380 my $rc = $dbh->commit;
383 $main::lxdebug->leave_sub();
388 sub ap_transactions {
389 $main::lxdebug->enter_sub();
391 my ($self, $myconfig, $form) = @_;
393 # connect to database
394 my $dbh = $form->dbconnect($myconfig);
396 my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate,
397 a.amount, a.paid, a.ordnumber, v.name, a.invoice,
398 a.netamount, a.datepaid, a.notes, e.name AS employee
400 JOIN vendor v ON (a.vendor_id = v.id)
401 LEFT JOIN employee e ON (a.employee_id = e.id)|;
405 if ($form->{vendor_id}) {
406 $where .= " AND a.vendor_id = $form->{vendor_id}";
408 if ($form->{vendor}) {
409 my $vendor = $form->like(lc $form->{vendor});
410 $where .= " AND lower(v.name) LIKE '$vendor'";
413 if ($form->{department}) {
414 my ($null, $department_id) = split /--/, $form->{department};
415 $where .= " AND a.department_id = $department_id";
417 if ($form->{invnumber}) {
418 my $invnumber = $form->like(lc $form->{invnumber});
419 $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
421 if ($form->{ordnumber}) {
422 my $ordnumber = $form->like(lc $form->{ordnumber});
423 $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
425 if ($form->{notes}) {
426 my $notes = $form->like(lc $form->{notes});
427 $where .= " AND lower(a.notes) LIKE '$notes'";
430 $where .= " AND a.transdate >= '$form->{transdatefrom}'"
431 if $form->{transdatefrom};
432 $where .= " AND a.transdate <= '$form->{transdateto}'"
433 if $form->{transdateto};
434 if ($form->{open} || $form->{closed}) {
435 unless ($form->{open} && $form->{closed}) {
436 $where .= " AND a.amount <> a.paid" if ($form->{open});
437 $where .= " AND a.amount = a.paid" if ($form->{closed});
441 my @a = (transdate, invnumber, name);
442 push @a, "employee" if $self->{l_employee};
443 my $sortorder = join ', ', $form->sort_columns(@a);
444 $sortorder = $form->{sort} if $form->{sort};
446 $query .= "WHERE $where
447 ORDER by $sortorder";
449 my $sth = $dbh->prepare($query);
450 $sth->execute || $form->dberror($query);
452 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
453 push @{ $form->{AP} }, $ap;
459 $main::lxdebug->leave_sub();