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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # Check and receipt printing payment module backend routines
33 # Number to text conversion routines are in
34 # locale/{countrycode}/Num2text
36 #======================================================================
45 $main::lxdebug->enter_sub();
47 my ($type, $countrycode) = @_;
52 if (-f "locale/$countrycode/Num2text") {
53 require "locale/$countrycode/Num2text";
61 $main::lxdebug->leave_sub();
67 $main::lxdebug->enter_sub();
69 my ($self, $myconfig, $form) = @_;
71 my $dbh = SL::DB->client->dbh;
73 my $ARAP = $form->{ARAP} eq "AR" ? "AR" : "AP";
76 qq|SELECT accno, description, link | .
78 qq|WHERE link LIKE ? |.
80 my $sth = prepare_execute_query($form, $dbh, $query, like($ARAP));
82 $form->{PR}{ $form->{ARAP} } = ();
83 $form->{PR}{"$form->{ARAP}_paid"} = ();
85 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
86 foreach my $item (split(/:/, $ref->{link})) {
87 if ($item eq $form->{ARAP}) {
88 push(@{ $form->{PR}{ $form->{ARAP} } }, $ref);
90 if ($item eq "$form->{ARAP}_paid") {
91 push(@{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref);
98 $query = qq|SELECT closedto FROM defaults|;
99 ($form->{closedto}) = selectrow_query($form, $dbh, $query);
101 $main::lxdebug->leave_sub();
104 sub get_openinvoices {
105 $main::lxdebug->enter_sub();
107 my ($self, $myconfig, $form) = @_;
109 # connect to database
110 my $dbh = SL::DB->client->dbh;
112 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
114 my $buysell = $form->{vc} eq 'customer' ? "buy" : "sell";
115 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
117 my @values = (conv_i($form->{"${vc}_id"}), "$form->{currency}");
118 my $whereinvoice = '';
119 if ($::form->{invnumber}) {
120 $whereinvoice = ' AND a.invnumber LIKE ? ';
121 push @values, $::form->{invnumber};
125 qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, cu.name AS curr | .
127 qq|LEFT JOIN currencies cu ON (cu.id=a.currency_id)| .
128 qq|WHERE (a.${vc}_id = ?) AND cu.name = ? AND NOT (a.amount = a.paid)| .
132 my $sth = prepare_execute_query($form, $dbh, $query, @values);
135 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
137 # if this is a foreign currency transaction get exchangerate
138 $ref->{exchangerate} =
139 $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
140 if ($form->{currency} ne $form->{defaultcurrency});
141 push @{ $form->{PR} }, $ref;
150 AND ((SELECT cu.name FROM currencies cu WHERE cu.id=${arap}.currency_id) <> ?)
153 ($form->{openinvoices_other_currencies}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{"${vc}_id"}), "$form->{currency}");
155 $main::lxdebug->leave_sub();
158 sub process_payment {
159 my ($self, $myconfig, $form) = @_;
160 $main::lxdebug->enter_sub();
162 my $rc = SL::DB->client->with_transaction(\&_process_payment, $self, $myconfig, $form);
164 $::lxdebug->leave_sub;
168 sub _process_payment {
169 my ($self, $myconfig, $form) = @_;
172 my $dbh = SL::DB->client->dbh;
174 my ($paymentaccno) = split /--/, $form->{account};
176 # if currency ne defaultcurrency update exchangerate
177 if ($form->{currency} ne $form->{defaultcurrency}) {
178 $form->{exchangerate} =
179 $form->parse_amount($myconfig, $form->{exchangerate});
181 if ($form->{vc} eq 'customer') {
182 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, $form->{exchangerate}, 0);
184 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0, $form->{exchangerate});
187 $form->{exchangerate} = 1;
190 my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id FROM defaults|;
191 my ($fxgain_accno_id, $fxloss_accno_id) = selectrow_query($form, $dbh, $query);
193 my $buysell = $form->{vc} eq "customer" ? "buy" : "sell";
194 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
199 if ($form->{ARAP} eq 'AR') {
201 $where = qq| ((c.link = 'AR') OR (c.link LIKE 'AR:%')) |;
205 qq| ((c.link = 'AP') OR | .
206 qq| (c.link LIKE '%:AP') OR | .
207 qq| (c.link LIKE '%:AP:%')) |;
211 # query to retrieve paid amount
213 qq|SELECT a.paid FROM ar a | .
214 qq|WHERE a.id = ? | .
216 my $pth = prepare_query($form, $dbh, $query);
218 # go through line by line
219 for my $i (1 .. $form->{rowcount}) {
221 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
222 $form->{"due_$i"} = $form->parse_amount($myconfig, $form->{"due_$i"});
224 if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
226 # get exchangerate for original
228 qq|SELECT $buysell | .
229 qq|FROM exchangerate e | .
230 qq|JOIN ${arap} a ON (a.transdate = e.transdate) | .
231 qq|WHERE (e.currency_id = (SELECT id FROM currencies WHERE name = ?)) AND (a.id = ?)|;
233 selectrow_query($form, $dbh, $query,
234 $form->{currency}, $form->{"id_$i"});
236 $exchangerate = 1 unless $exchangerate;
241 qq|JOIN acc_trans a ON (a.chart_id = c.id) | .
243 qq|AND (a.trans_id = ?)|;
244 my ($id) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
246 $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
250 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, chart_link, taxkey, tax_id) | .
251 qq|VALUES (?, ?, ?, ?, (SELECT link FROM chart WHERE id=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
252 do_query($form, $dbh, $query, $form->{"id_$i"}, $id,
253 conv_date($form->{datepaid}), $amount * $ml, $id);
257 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
258 qq| source, memo, chart_link, taxkey, tax_id) | .
259 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, (SELECT link FROM chart WHERE accno=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
260 my @values = (conv_i($form->{"id_$i"}), $paymentaccno,
261 conv_date($form->{datepaid}),
262 $form->{"paid_$i"} * $ml * -1, $form->{source},
263 $form->{memo}, $paymentaccno);
264 do_query($form, $dbh, $query, @values);
266 # add exchangerate difference if currency ne defaultcurrency
267 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
271 # exchangerate difference
273 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
274 qq| cleared, fx_transaction, chart_link, taxkey, tax_id) | .
275 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, (SELECT link FROM chart WHERE accno = ?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
276 @values = (conv_i($form->{"id_$i"}), $paymentaccno,
277 conv_date($form->{datepaid}), ($amount * $ml * -1), '0',
279 do_query($form, $dbh, $query, @values);
284 $form->round_amount($form->{"paid_$i"} *
285 ($exchangerate - $form->{exchangerate}), 2);
287 my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
289 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, | .
290 qq| amount, cleared, fx_transaction, chart_link, taxkey, tax_id) | .
291 qq|VALUES (?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE id=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
292 @values = (conv_i($form->{"id_$i"}), $accno_id,
293 conv_date($form->{datepaid}), $amount * $ml * -1, '0',
295 do_query($form, $dbh, $query, @values);
300 $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
301 $pth->execute($form->{"id_$i"}) || $form->dberror;
302 ($amount) = $pth->fetchrow_array;
305 $amount += $form->{"paid_$i"};
309 if ($form->{arap} eq 'ap') {
310 $paid = "paid = paid + $amount";
312 $paid = "paid = $amount";
315 # update AR/AP transaction
316 $query = qq|UPDATE $arap SET $paid, datepaid = ? WHERE id = ?|;
317 @values = (conv_date($form->{datepaid}), conv_i($form->{"id_$i"}));
318 do_query($form, $dbh, $query, @values);
320 $form->{id} = $form->{"id_$i"};
321 if(!exists $form->{addition}) {
322 $form->{snumbers} = qq|invnumber_| . $form->{"invnumber_$i"};
323 $form->{what_done} = "invoice";
324 $form->{addition} = "PAYMENT POSTED";
327 # /saving the history