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} ? 't' : 'f', $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, taxkey) | .
258 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
259 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
260 @values = ($form->{id}, $form->{AP_amounts}{payables}, $form->{payables},
261 conv_date($form->{transdate}), $form->{AP_amounts}{payables});
262 do_query($form, $dbh, $query, @values);
264 # if there is no amount but a payment record a payable
265 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
266 $form->{payables} = $form->{invpaid};
269 # add paid transactions
270 for my $i (1 .. $form->{paidaccounts}) {
271 if ($form->{"paid_$i"} != 0) {
272 my $project_id = conv_i($form->{"paid_project_id_$i"});
275 if ($form->{currency} eq $form->{defaultcurrency}) {
276 $form->{"exchangerate_$i"} = 1;
279 $form->check_exchangerate($myconfig, $form->{currency},
280 $form->{"datepaid_$i"}, 'sell');
282 $form->{"exchangerate_$i"} =
285 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
287 $form->{"AP_paid_$i"} =~ s/\"//g;
291 ($form->{AP}{"paid_$i"}) = split(/--/, $form->{"AP_paid_$i"});
292 $form->{"datepaid_$i"} = $form->{transdate}
293 unless ($form->{"datepaid_$i"});
295 # if there is no amount and invtotal is zero there is no exchangerate
296 if ($form->{amount} == 0 && $form->{invtotal} == 0) {
297 $form->{exchangerate} = $form->{"exchangerate_$i"};
301 $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} * -1,
303 if ($form->{payables}) {
305 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
306 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
307 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
308 @values = ($form->{id}, $form->{AP}{payables}, $amount,
309 conv_date($form->{"datepaid_$i"}), $project_id,
310 $form->{AP}{payables});
311 do_query($form, $dbh, $query, @values);
313 $form->{payables} = $amount;
317 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
318 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
319 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
320 @values = ($form->{id}, $form->{AP}{"paid_$i"}, $form->{"paid_$i"},
321 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
322 $form->{"memo_$i"}, $project_id, $form->{AP}{"paid_$i"});
323 do_query($form, $dbh, $query, @values);
325 # add exchange rate difference
327 $form->round_amount($form->{"paid_$i"} *
328 ($form->{"exchangerate_$i"} - 1), 2);
331 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
332 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
333 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
334 @values = ($form->{id}, $form->{AP}{"paid_$i"}, $amount,
335 conv_date($form->{"datepaid_$i"}), $project_id,
336 $form->{AP}{"paid_$i"});
337 do_query($form, $dbh, $query, @values);
340 # exchangerate gain/loss
342 $form->round_amount($form->{"paid_$i"} *
343 ($form->{exchangerate} -
344 $form->{"exchangerate_$i"}), 2);
348 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
349 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
350 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
351 @values = ($form->{id}, ($amount > 0) ?
352 $form->{fxgain_accno} : $form->{fxloss_accno},
353 $amount, conv_date($form->{"datepaid_$i"}), $project_id,
355 $form->{fxgain_accno} : $form->{fxloss_accno});
356 do_query($form, $dbh, $query, @values);
359 # update exchange rate record
360 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
361 $form->update_exchangerate($dbh, $form->{currency},
362 $form->{"datepaid_$i"},
363 0, $form->{"exchangerate_$i"});
368 my $rc = $dbh->commit;
371 $main::lxdebug->leave_sub();
376 sub delete_transaction {
377 $main::lxdebug->enter_sub();
379 my ($self, $myconfig, $form, $spool) = @_;
381 # connect to database
382 my $dbh = $form->dbconnect_noauto($myconfig);
384 my $query = qq|DELETE FROM ap WHERE id = ?|;
385 do_query($form, $dbh, $query, $form->{id});
387 $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
388 do_query($form, $dbh, $query, $form->{id});
390 # commit and redirect
391 my $rc = $dbh->commit;
394 $main::lxdebug->leave_sub();
399 sub ap_transactions {
400 $main::lxdebug->enter_sub();
402 my ($self, $myconfig, $form) = @_;
404 # connect to database
405 my $dbh = $form->dbconnect($myconfig);
408 qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | .
409 qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | .
410 qq| a.globalproject_id, | .
411 qq| pr.projectnumber AS globalprojectnumber, | .
412 qq| e.name AS employee | .
414 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
415 qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
416 qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id) |;
421 if ($form->{vendor_id}) {
422 $where .= " AND a.vendor_id = ?";
423 push(@values, $form->{vendor_id});
424 } elsif ($form->{vendor}) {
425 $where .= " AND v.name ILIKE ?";
426 push(@values, $form->like($form->{vendor}));
428 if ($form->{department}) {
429 my ($null, $department_id) = split /--/, $form->{department};
430 $where .= " AND a.department_id = ?";
431 push(@values, $department_id);
433 if ($form->{invnumber}) {
434 $where .= " AND a.invnumber ILIKE ?";
435 push(@values, $form->like($form->{invnumber}));
437 if ($form->{ordnumber}) {
438 $where .= " AND a.ordnumber ILIKE ?";
439 push(@values, $form->like($form->{ordnumber}));
441 if ($form->{notes}) {
442 $where .= " AND lower(a.notes) LIKE ?";
443 push(@values, $form->like($form->{notes}));
445 if ($form->{project_id}) {
447 qq|AND ((a.globalproject_id = ?) OR EXISTS | .
448 qq| (SELECT * FROM invoice i | .
449 qq| WHERE i.project_id = ? AND i.trans_id = a.id))|;
450 push(@values, $form->{project_id}, $form->{project_id});
453 if ($form->{transdatefrom}) {
454 $where .= " AND a.transdate >= ?";
455 push(@values, $form->{transdatefrom});
457 if ($form->{transdateto}) {
458 $where .= " AND a.transdate <= ?";
459 push(@values, $form->{transdateto});
461 if ($form->{open} || $form->{closed}) {
462 unless ($form->{open} && $form->{closed}) {
463 $where .= " AND a.amount <> a.paid" if ($form->{open});
464 $where .= " AND a.amount = a.paid" if ($form->{closed});
469 substr($where, 0, 4) = "WHERE";
473 my @a = (transdate, invnumber, name);
474 push @a, "employee" if $self->{l_employee};
475 my $sortorder = join(', ', @a);
477 if (grep({ $_ eq $form->{sort} }
478 qw(transdate id invnumber ordnumber name netamount tax amount
479 paid datepaid due duedate notes employee))) {
480 $sortorder = $form->{sort};
483 $query .= " ORDER by $sortorder";
485 my $sth = $dbh->prepare($query);
486 $sth->execute(@values) ||
487 $form->dberror($query . " (" . join(", ", @values) . ")");
490 while (my $ap = $sth->fetchrow_hashref(NAME_lc)) {
491 push @{ $form->{AP} }, $ap;
497 $main::lxdebug->leave_sub();
501 $main::lxdebug->enter_sub();
503 my ($self, $myconfig, $form) = @_;
505 # connect to database
506 my $dbh = $form->dbconnect($myconfig);
510 " (SELECT transdate FROM ap WHERE id = " .
511 " (SELECT MAX(id) FROM ap) LIMIT 1), " .
513 ($form->{transdate}) = $dbh->selectrow_array($query);
517 $main::lxdebug->leave_sub();
522 $main::lxdebug->enter_sub();
524 my ($self, $myconfig, $form, $locale) = @_;
526 # connect to database, turn off autocommit
527 my $dbh = $form->dbconnect_noauto($myconfig);
529 $form->{datepaid} = $form->{transdate};
531 # total payments, don't move we need it here
532 for my $i (1 .. $form->{paidaccounts}) {
533 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
534 $form->{paid} += $form->{"paid_$i"};
535 $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
538 $form->{exchangerate} =
539 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
542 my (@values, $query);
544 my ($accno_ap) = split(/--/, $form->{APselected});
546 # record payments and offsetting AP
547 for my $i (1 .. $form->{paidaccounts}) {
549 if ($form->{"paid_$i"} != 0) {
550 my ($accno) = split /--/, $form->{"AP_paid_$i"};
551 $form->{"datepaid_$i"} = $form->{transdate}
552 unless ($form->{"datepaid_$i"});
553 $form->{datepaid} = $form->{"datepaid_$i"};
556 if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
557 $form->{"exchangerate_$i"} = 1;
560 $form->check_exchangerate($myconfig, $form->{currency},
561 $form->{"datepaid_$i"}, 'buy');
563 $form->{"exchangerate_$i"} =
566 : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
571 $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
575 qq|DELETE FROM acc_trans | .
576 qq|WHERE trans_id = ? | .
577 qq| AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
578 qq| AND amount = ? AND transdate = ?|;
579 @values = ($form->{id}, $accno_ap, $amount,
580 conv_date($form->{"datepaid_$i"}));
581 do_query($form, $dbh, $query, @values);
584 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
585 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
586 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
587 @values = ($form->{id}, $accno_ap, $amount,
588 conv_date($form->{"datepaid_$i"}),
589 conv_i($form->{"paid_project_id_$i"}), $accno_ap);
590 do_query($form, $dbh, $query, @values);
593 qq|DELETE FROM acc_trans | .
594 qq|WHERE trans_id = ? | .
595 qq| AND chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) | .
596 qq| AND amount = ? AND transdate = ? AND source = ? AND memo = ?|;
597 @values = ($form->{id}, $accno, $form->{"paid_$i"},
598 conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
600 do_query($form, $dbh, $query, @values);
603 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
604 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
605 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
606 @values = ($form->{id}, $accno, $form->{"paid_$i"},
607 $form->{"datepaid_$i"},
608 $form->{"source_$i"}, $form->{"memo_$i"},
609 conv_i($form->{"paid_project_id_$i"}), $accno);
610 do_query($form, $dbh, $query, @values);
614 $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
615 $form->{"exchangerate_$i"};
617 $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
620 $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
626 # update exchange rate
627 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
628 $form->update_exchangerate($dbh, $form->{currency},
629 $form->{"datepaid_$i"},
630 $form->{"exchangerate_$i"}, 0);
635 # record exchange rate differences and gains/losses
636 foreach my $accno (keys %{ $form->{fx} }) {
637 foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
639 ($form->{fx}{$accno}{$transdate} =
640 $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
644 qq|DELETE FROM acc_trans | .
645 qq|WHERE trans_id = ? AND chart_id = | .
646 qq| (SELECT c.id FROM chart c WHERE c.accno = ?) AND amount = ? | .
647 qq| AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
648 @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
649 conv_date($transdate),);
650 do_query($form, $dbh, $query, @values);
653 qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, taxkey) | .
654 qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, 'f', 't', | .
655 qq| (SELECT taxkey_id FROM chart WHERE accno = ?))|;
656 @values = ($form->{id}, $accno, $form->{fx}{$accno}{$transdate},
657 conv_date($transdate), $accno);
658 do_query($form, $dbh, $query, @values);
664 my $query = qq|UPDATE ap SET paid = ?, datepaid = ? WHERE id = ?|;
665 @values = ($form->{paid}, $form->{paid} ? $form->{datepaid} : undef,
667 do_query($form, $dbh, $query, @values);
669 my $rc = $dbh->commit;
672 $main::lxdebug->leave_sub();