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 #======================================================================
39 sub post_transaction {
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form) = @_;
45 my $dbh = $form->dbconnect_noauto($myconfig);
47 my ($null, $taxrate, $amount);
50 ($null, $form->{department_id}) = split(/--/, $form->{department});
51 $form->{department_id} *= 1;
53 if ($form->{currency} eq $form->{defaultcurrency}) {
54 $form->{exchangerate} = 1;
57 $form->check_exchangerate($myconfig, $form->{currency},
58 $form->{transdate}, 'sell');
60 $form->{exchangerate} =
63 : $form->parse_amount($myconfig, $form->{exchangerate});
66 for $i (1 .. $form->{rowcount}) {
67 $form->{AP_amounts}{"amount_$i"} =
68 (split(/--/, $form->{"AP_amount_$i"}))[0];
70 ($form->{AP_amounts}{payables}) = split(/--/, $form->{APselected});
71 ($form->{AP}{payables}) = split(/--/, $form->{APselected});
73 # reverse and parse amounts
74 for my $i (1 .. $form->{rowcount}) {
75 $form->{"amount_$i"} =
77 $form->parse_amount($myconfig, $form->{"amount_$i"}) *
78 $form->{exchangerate} * -1,
80 $amount += ($form->{"amount_$i"} * -1);
82 # parse tax_$i for later
83 $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"}) * -1;
87 $form->{amount} = $amount;
89 # taxincluded doesn't make sense if there is no amount
90 $form->{taxincluded} = 0 if ($form->{amount} == 0);
92 for $i (1 .. $form->{rowcount}) {
93 ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
96 qq|SELECT c.accno, t.taxkey, t.rate | .
97 qq|FROM tax t LEFT JOIN chart c on (c.id=t.chart_id) | .
100 $sth = $dbh->prepare($query);
101 $sth->execute($form->{"tax_id_$i"}) || $form->dberror($query . " (" . $form->{"tax_id_$i"} . ")");
102 ($form->{AP_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) =
103 $sth->fetchrow_array;
104 $form->{AP_amounts}{"tax_$i"}{taxkey} = $form->{"taxkey_$i"};
105 $form->{AP_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
108 if ($form->{taxincluded} *= 1) {
109 if (!$form->{"korrektur_$i"}) {
111 $form->{"amount_$i"} -
112 ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
114 $tax = $form->{"tax_$i"};
116 $amount = $form->{"amount_$i"} - $tax;
117 $form->{"amount_$i"} = $form->round_amount($amount, 2);
118 $diff += $amount - $form->{"amount_$i"};
119 $form->{"tax_$i"} = $form->round_amount($tax, 2);
120 $form->{netamount} += $form->{"amount_$i"};
122 if (!$form->{"korrektur_$i"}) {
123 $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
125 $tax = $form->{"tax_$i"};
128 $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
129 $form->{netamount} += $form->{"amount_$i"};
131 $form->{total_tax} += $form->{"tax_$i"} * -1;
134 # adjust paidaccounts if there is no date in the last row
135 $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
137 $form->{invpaid} = 0;
138 $form->{netamount} *= -1;
141 for my $i (1 .. $form->{paidaccounts}) {
143 $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
146 $form->{invpaid} += $form->{"paid_$i"};
147 $form->{datepaid} = $form->{"datepaid_$i"};
152 $form->round_amount($form->{invpaid} * $form->{exchangerate}, 2);
154 # store invoice total, this goes into ap table
155 $form->{invtotal} = $form->{netamount} + $form->{total_tax};
157 # amount for total AP
158 $form->{payables} = $form->{invtotal};
162 # if we have an id delete old records
165 # delete detail records
166 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
167 do_query($form, $dbh, $query, $form->{id});
170 my $uid = rand() . time;
172 $uid .= $form->{login};
174 $uid = substr($uid, 2, 75);
177 qq|INSERT INTO ap (invnumber, employee_id) | .
178 qq|VALUES (?, (SELECT e.id FROM employee e WHERE e.login = ?))|;
179 do_query($form, $dbh, $query, $uid, $form->{login});
181 $query = qq|SELECT a.id FROM ap a
182 WHERE a.invnumber = ?|;
183 ($form->{id}) = selectrow_query($form, $dbh, $query, $uid);
186 $form->{invnumber} = $form->{id} unless $form->{invnumber};
188 $form->{datepaid} = $form->{transdate} unless ($form->{datepaid});
189 my $datepaid = ($form->{invpaid} != 0) ? $form->{datepaid} : undef;
191 $query = qq|UPDATE ap SET
206 my @values = ($form->{invnumber}, conv_date($form->{transdate}),
207 $form->{ordnumber}, conv_i($form->{vendor_id}),
208 $form->{taxincluded}, $form->{invtotal},
209 conv_date($form->{duedate}), $form->{invpaid},
210 conv_date($datepaid), $form->{netamount},
211 $form->{currency}, $form->{notes},
212 conv_i($form->{department_id}), $form->{id});
213 do_query($form, $dbh, $query, @values);
215 # update exchangerate
216 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
217 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0,
218 $form->{exchangerate});
221 # add individual transactions
222 for $i (1 .. $form->{rowcount}) {
223 if ($form->{"amount_$i"} != 0) {
225 $project_id = conv_i($form->{"project_id_$i"});
226 $taxkey = $form->{AP_amounts}{"amount_$i"}{taxkey};
228 # insert detail records in acc_trans
230 qq|INSERT INTO acc_trans | .
231 qq| (trans_id, chart_id, amount, transdate, project_id, taxkey)| .
232 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
234 @values = ($form->{id}, $form->{AP_amounts}{"amount_$i"},
235 $form->{"amount_$i"}, conv_date($form->{transdate}),
236 $project_id, $taxkey);
237 do_query($form, $dbh, $query, @values);
239 if ($form->{"tax_$i"} != 0) {
240 # insert detail records in acc_trans
242 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
243 qq| project_id, taxkey) | .
244 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
246 @values = ($form->{id}, $form->{AP_amounts}{"tax_$i"},
247 $form->{"tax_$i"}, conv_date($form->{transdate}),
248 $project_id, $taxkey);
249 do_query($form, $dbh, $query, @values);
257 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate) | .
258 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?)|;
259 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
260 conv_date($form->{transdate}));
261 do_query($form, $dbh, $query, @values);
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) {
271 my $project_id = conv_i($form->{"paid_project_id_$i"});
274 if ($form->{currency} eq $form->{defaultcurrency}) {
275 $form->{"exchangerate_$i"} = 1;
278 $form->check_exchangerate($myconfig, $form->{currency},
279 $form->{"datepaid_$i"}, 'sell');
281 $form->{"exchangerate_$i"} =
284 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
286 $form->{"AP_paid_$i"} =~ s/\"//g;
290 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
291 $form->{"datepaid_$i"} = $form->{transdate}
292 unless ($form->{"datepaid_$i"});
294 # if there is no amount and invtotal is zero there is no exchangerate
295 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
296 $form->{exchangerate} = $form->{"exchangerate_$i"};
300 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
302 if ($form->{payables}) {
304 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id) | .
305 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?)|;
306 @values = ($form->{id}, $form->{AP}{payables}, $amount,
307 conv_date($form->{"datepaid_$i"}), $project_id);
308 do_query($form, $dbh, $query, @values);
310 $form->{payables} = $amount;
314 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id) | .
315 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?)|;
316 @values = ($form->{id}, $form->{AP}{"paid_$i"}, $form->{"paid_$i"},
317 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
318 $form->{"memo_$i"}, $project_id);
319 do_query($form, $dbh, $query, @values);
321 # add exchange rate difference
323 $form->round_amount($form->{"paid_$i"} *
324 ($form->{"exchangerate_$i"} - 1), 2);
327 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id) | .
328 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, 't', 'f', ?)|;
329 @values = ($form->{id}, $form->{AP}{"paid_$i"}, $amount,
330 conv_date($form->{"datepaid_$i"}), $project_id);
331 do_query($form, $dbh, $query, @values);
334 # exchangerate gain/loss
336 $form->round_amount($form->{"paid_$i"} *
337 ($form->{exchangerate} -
338 $form->{"exchangerate_$i"}), 2);
342 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id) | .
343 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, 't', 'f', ?)|;
344 @values = ($form->{id}, ($amount > 0) ?
345 $form->{fxgain_accno} : $form->{fxloss_accno},
346 $amount, conv_date($form->{"datepaid_$i"}), $project_id);
347 do_query($form, $dbh, $query, @values);
350 # update exchange rate record
351 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
352 $form->update_exchangerate($dbh, $form->{currency},
353 $form->{"datepaid_$i"},
354 0, $form->{"exchangerate_$i"});
359 my $rc = $dbh->commit;
362 $main::lxdebug->leave_sub();
367 sub delete_transaction {
368 $main::lxdebug->enter_sub();
370 my ($self, $myconfig, $form, $spool) = @_;
372 # connect to database
373 my $dbh = $form->dbconnect_noauto($myconfig);
375 my $query = qq|DELETE FROM ap WHERE id = ?|;
376 do_query($form, $dbh, $query, $form->{id});
378 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
379 do_query($form, $dbh, $query, $form->{id});
381 # commit and redirect
382 my $rc = $dbh->commit;
385 $main::lxdebug->leave_sub();
390 sub ap_transactions {
391 $main::lxdebug->enter_sub();
393 my ($self, $myconfig, $form) = @_;
395 # connect to database
396 my $dbh = $form->dbconnect($myconfig);
399 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
400 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
401 qq| a.globalproject_id, | .
402 qq| pr.projectnumber AS globalprojectnumber, | .
403 qq| e.name AS employee | .
405 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
406 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
407 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) |;
412 if ($form->{vendor_id}) {
413 $where .= " AND a.vendor_id = ?";
414 push(@values, $form->{vendor_id});
415 } elsif ($form->{vendor}) {
416 $where .= " AND v.name ILIKE ?";
417 push(@values, $form->like($form->{vendor}));
419 if ($form->{department}) {
420 my ($null, $department_id) = split /--/, $form->{department};
421 $where .= " AND a.department_id = ?";
422 push(@values, $department_id);
424 if ($form->{invnumber}) {
425 $where .= " AND a.invnumber ILIKE ?";
426 push(@values, $form->like($form->{invnumber}));
428 if ($form->{ordnumber}) {
429 $where .= " AND a.ordnumber ILIKE ?";
430 push(@values, $form->like($form->{ordnumber}));
432 if ($form->{notes}) {
433 $where .= " AND lower(a.notes) LIKE ?";
434 push(@values, $form->like($form->{notes}));
436 if ($form->{project_id}) {
438 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
439 qq| (SELECT * FROM invoice i | .
440 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
441 push(@values, $form->{project_id}, $form->{project_id});
444 if ($form->{transdatefrom}) {
445 $where .= " AND a.transdate >= ?";
446 push(@values, $form->{transdatefrom});
448 if ($form->{transdateto}) {
449 $where .= " AND a.transdate <= ?";
450 push(@values, $form->{transdateto});
452 if ($form->{open} || $form->{closed}) {
453 unless ($form->{open} && $form->{closed}) {
454 $where .= " AND a.amount <> a.paid" if ($form->{open});
455 $where .= " AND a.amount = a.paid" if ($form->{closed});
460 substr($where, 0, 4) = "WHERE";
464 my @a = (transdate, invnumber, name);
465 push @a, "employee" if $self->{l_employee};
466 my $sortorder = join(', ', @a);
468 if (grep({ $_ eq $form->{sort} }
469 qw(transdate id invnumber ordnumber name netamount tax amount
470 paid datepaid due duedate notes employee))) {
471 $sortorder = $form->{sort};
474 $query .= " ORDER by $sortorder";
476 my $sth = $dbh->prepare($query);
477 $sth->execute(@values) ||
478 $form->dberror($query . " (" . join(", ", @values) . ")");
481 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
482 push @{ $form->{AP} }, $ap;
488 $main::lxdebug->leave_sub();
492 $main::lxdebug->enter_sub();
494 my ($self, $myconfig, $form) = @_;
496 # connect to database
497 my $dbh = $form->dbconnect($myconfig);
501 " (SELECT transdate FROM ap WHERE id = " .
502 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
504 ($form->{transdate}) = $dbh->selectrow_array($query);
508 $main::lxdebug->leave_sub();
513 $main::lxdebug->enter_sub();
515 my ($self, $myconfig, $form, $locale) = @_;
517 # connect to database, turn off autocommit
518 my $dbh = $form->dbconnect_noauto($myconfig);
520 $form->{datepaid} = $form->{transdate};
522 # total payments, don't move we need it here
523 for my $i (1 .. $form->{paidaccounts}) {
524 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
525 $form->{paid} += $form->{"paid_$i"};
526 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
529 $form->{exchangerate} =
530 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
533 my (@values, $query);
535 my ($accno_ap) = split(/--/, $form->{APselected});
537 # record payments and offsetting AP
538 for my $i (1 .. $form->{paidaccounts}) {
540 if ($form->{"paid_$i"} != 0) {
541 my ($accno) = split /--/, $form->{"AP_paid_$i"};
542 $form->{"datepaid_$i"} = $form->{transdate}
543 unless ($form->{"datepaid_$i"});
544 $form->{datepaid} = $form->{"datepaid_$i"};
547 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
548 $form->{"exchangerate_$i"} = 1;
551 $form->check_exchangerate($myconfig, $form->{currency},
552 $form->{"datepaid_$i"}, 'buy');
554 $form->{"exchangerate_$i"} =
557 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
562 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
566 qq|DELETE FROM acc_trans | .
567 qq|WHERE trans_id = ? | .
568 qq| AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
569 qq| AND amount = ? AND transdate = ?|;
570 @values = ($form->{id}, $accno_ap, $amount,
571 conv_date($form->{"datepaid_$i"}));
572 do_query($form, $dbh, $query, @values);
575 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id) | .
576 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?)|;
577 @values = ($form->{id}, $accno_ap, $amount,
578 conv_date($form->{"datepaid_$i"}),
579 conv_i($form->{"paid_project_id_$i"}));
580 do_query($form, $dbh, $query, @values);
583 qq|DELETE FROM acc_trans | .
584 qq|WHERE trans_id = ? | .
585 qq| AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
586 qq| AND amount = ? AND transdate = ? AND source = ? AND memo = ?|;
587 @values = ($form->{id}, $accno, $form->{"paid_$i"},
588 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
590 do_query($form, $dbh, $query, @values);
593 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id) | .
594 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?)|;
595 @values = ($form->{id}, $accno, $form->{"paid_$i"},
596 $form->{"datepaid_$i"},
597 $form->{"source_$i"}, $form->{"memo_$i"},
598 conv_i($form->{"paid_project_id_$i"}));
599 do_query($form, $dbh, $query, @values);
603 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
604 $form->{"exchangerate_$i"};
606 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
609 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
615 # update exchange rate
616 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
617 $form->update_exchangerate($dbh, $form->{currency},
618 $form->{"datepaid_$i"},
619 $form->{"exchangerate_$i"}, 0);
624 # record exchange rate differences and gains/losses
625 foreach my $accno (keys %{ $form->{fx} }) {
626 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
628 ($form->{fx}{$accno}{$transdate} =
629 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
633 qq|DELETE FROM acc_trans | .
634 qq|WHERE trans_id = ? AND chart_id = | .
635 qq| (SELECT c.id FROM chart c WHERE c.accno = ?) AND amount = ? | .
636 qq| AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
637 @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
638 conv_date($transdate),);
639 do_query($form, $dbh, $query, @values);
642 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction) | .
643 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, 'f', 't')|;
644 @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
645 conv_date($transdate));
646 do_query($form, $dbh, $query, @values);
652 my $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
653 @values = ($form->{paid}, $form->{paid} ? $form->{datepaid} : undef,
655 do_query($form, $dbh, $query, @values);
657 my $rc = $dbh->commit;
660 $main::lxdebug->leave_sub();