1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
25 # Overpayment function
26 # used in AR, AP, IS, IR, OE, CP
28 #======================================================================
33 my ($self, $myconfig, $form, $dbh, $amount, $ml) = @_;
35 my $fxamount = $form->round_amount($amount * $form->{exchangerate}, 2);
36 my ($paymentaccno) = split /--/, $form->{account};
38 my $vc_id = "$form->{vc}_id";
41 $uid .= $form->{login};
43 # add AR/AP header transaction with a payment
44 $query = qq|INSERT INTO $form->{arap} (invnumber, employee_id)
45 VALUES ('$uid', (SELECT id FROM employee
46 WHERE login = '$form->{login}'))|;
47 $dbh->do($query) || $form->dberror($query);
49 $query = qq|SELECT id FROM $form->{arap}
50 WHERE invnumber = '$uid'|;
51 $sth = $dbh->prepare($query);
52 $sth->execute || $form->dberror($query);
54 ($uid) = $sth->fetchrow_array;
57 my $invnumber = $form->{invnumber};
59 $invnumber = $form->update_defaults($myconfig, ($form->{arap} eq 'ar') ? "sinumber" : "vinumber", $dbh);
62 $query = qq|UPDATE $form->{arap} set
63 invnumber = |.$dbh->quote($invnumber).qq|,
64 $vc_id = $form->{"$form->{vc}_id"},
65 transdate = '$form->{datepaid}',
66 datepaid = '$form->{datepaid}',
67 duedate = '$form->{datepaid}',
71 curr = '$form->{currency}',
72 department_id = $form->{department_id}
74 $dbh->do($query) || $form->dberror($query);
77 ($accno) = split /--/, $form->{$form->{ARAP}};
79 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
80 VALUES ($uid, (SELECT id FROM chart
81 WHERE accno = '$accno'),
82 '$form->{datepaid}', $fxamount * $ml)|;
83 $dbh->do($query) || $form->dberror($query);
86 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
88 VALUES ($uid, (SELECT id FROM chart
89 WHERE accno = '$paymentaccno'),
90 '$form->{datepaid}', $amount * $ml * -1, |
91 .$dbh->quote($form->{source}).qq|, |
92 .$dbh->quote($form->{memo}).qq|)|;
93 $dbh->do($query) || $form->dberror($query);
95 # add exchangerate difference
96 if ($fxamount != $amount) {
97 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
98 amount, cleared, fx_transaction)
99 VALUES ($uid, (SELECT id FROM chart
100 WHERE accno = '$paymentaccno'),
101 '$form->{datepaid}', ($fxamount - $amount) * $ml * -1,
103 $dbh->do($query) || $form->dberror($query);
106 my %audittrail = ( tablename => $form->{arap},
107 reference => $invnumber,
108 formname => ($form->{arap} eq 'ar') ? 'deposit' : 'pre-payment',
112 $form->audittrail($dbh, "", \%audittrail);