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 # Check and receipt printing payment module backend routines
32 # Number to text conversion routines are in
33 # locale/{countrycode}/Num2text
35 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($type, $countrycode) = @_;
48 if (-f "locale/$countrycode/Num2text") {
49 require "locale/$countrycode/Num2text";
57 $main::lxdebug->leave_sub();
63 $main::lxdebug->enter_sub();
65 my ($self, $myconfig, $form) = @_;
68 my $dbh = $form->dbconnect($myconfig);
70 my $ARAP = $form->{ARAP} eq "AR" ? "AR" : "AP";
73 qq|SELECT accno, description, link | .
75 qq|WHERE link LIKE ? |.
77 my $sth = prepare_execute_query($form, $dbh, $query, '%' . $ARAP . '%');
79 $form->{PR}{ $form->{ARAP} } = ();
80 $form->{PR}{"$form->{ARAP}_paid"} = ();
82 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
83 foreach my $item (split(/:/, $ref->{link})) {
84 if ($item eq $form->{ARAP}) {
85 push(@{ $form->{PR}{ $form->{ARAP} } }, $ref);
87 if ($item eq "$form->{ARAP}_paid") {
88 push(@{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref);
94 # get currencies and closedto
95 $query = qq|SELECT curr, closedto FROM defaults|;
96 ($form->{currencies}, $form->{closedto}) =
97 selectrow_query($form, $dbh, $query);
101 $main::lxdebug->leave_sub();
105 $main::lxdebug->enter_sub();
107 my ($self, $myconfig, $form) = @_;
109 my $dbh = $form->dbconnect($myconfig);
111 my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
112 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
114 qq|SELECT count(*) | .
115 qq|FROM $vc ct, $arap a | .
116 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid)|;
117 my ($count) = selectrow_query($form, $dbh, $query);
119 # build selection list
120 if ($count < $myconfig->{vclimit}) {
122 qq|SELECT DISTINCT ct.id, ct.name | .
123 qq|FROM $vc ct, $arap a | .
124 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid) | .
125 qq|ORDER BY ct.name|;
126 $form->{"all_$form->{vc}"} = selectall_hashref_query($form, $dbh, $query);
129 if ($form->{ARAP} eq 'AR') {
131 qq|SELECT d.id, d.description | .
132 qq|FROM department d | .
133 qq|WHERE d.role = 'P' | .
137 qq|SELECT d.id, d.description | .
138 qq|FROM department d | .
141 $form->{all_departments} = selectall_hashref_query($form, $dbh, $query);
145 $main::lxdebug->leave_sub();
148 sub get_openinvoices {
149 $main::lxdebug->enter_sub();
151 my ($self, $myconfig, $form) = @_;
153 # connect to database
154 my $dbh = $form->dbconnect($myconfig);
156 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
158 my $buysell = $form->{vc} eq 'customer' ? "buy" : "sell";
159 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
161 my $curr_null = $form->{curreny} ? '' : ' OR a.curr IS NULL'; # fix: after sql-injection fix, curr is inserted as NULL, before that as ''
164 qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr | .
166 qq|WHERE (a.${vc}_id = ?) AND (a.curr = ? $curr_null) AND NOT (a.amount = paid)| .
168 my $sth = prepare_execute_query($form, $dbh, $query,
169 conv_i($form->{"${vc}_id"}),
173 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
175 # if this is a foreign currency transaction get exchangerate
176 $ref->{exchangerate} =
177 $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
178 if ($form->{currency} ne $form->{defaultcurrency});
179 push @{ $form->{PR} }, $ref;
185 $main::lxdebug->leave_sub();
188 sub process_payment {
189 $main::lxdebug->enter_sub();
191 my ($self, $myconfig, $form) = @_;
194 # connect to database, turn AutoCommit off
195 my $dbh = $form->dbconnect_noauto($myconfig);
197 my ($paymentaccno) = split /--/, $form->{account};
199 # if currency ne defaultcurrency update exchangerate
200 if ($form->{currency} ne $form->{defaultcurrency}) {
201 $form->{exchangerate} =
202 $form->parse_amount($myconfig, $form->{exchangerate});
204 if ($form->{vc} eq 'customer') {
205 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, $form->{exchangerate}, 0);
207 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0, $form->{exchangerate});
210 $form->{exchangerate} = 1;
213 my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id FROM defaults|;
214 my ($fxgain_accno_id, $fxloss_accno_id) = selectrow_query($form, $dbh, $query);
216 my $buysell = $form->{vc} eq "customer" ? "buy" : "sell";
217 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
222 if ($form->{ARAP} eq 'AR') {
224 $where = qq| ((c.link = 'AR') OR (c.link LIKE 'AR:%')) |;
228 qq| ((c.link = 'AP') OR | .
229 qq| (c.link LIKE '%:AP') OR | .
230 qq| (c.link LIKE '%:AP:%')) |;
233 my $paymentamount = $form->{amount};
236 ($null, $form->{department_id}) = split(/--/, $form->{department});
237 $form->{department_id} *= 1;
239 # query to retrieve paid amount
241 qq|SELECT a.paid FROM ar a | .
242 qq|WHERE a.id = ? | .
244 my $pth = prepare_query($form, $dbh, $query);
246 # go through line by line
247 for my $i (1 .. $form->{rowcount}) {
249 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
250 $form->{"due_$i"} = $form->parse_amount($myconfig, $form->{"due_$i"});
252 if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
253 $paymentamount = (($paymentamount * 1000) - ($form->{"paid_$i"} * 1000)) / 1000;
255 # get exchangerate for original
257 qq|SELECT $buysell | .
258 qq|FROM exchangerate e | .
259 qq|JOIN ${arap} a ON (a.transdate = e.transdate) | .
260 qq|WHERE (e.curr = ?) AND (a.id = ?)|;
262 selectrow_query($form, $dbh, $query,
263 $form->{currency}, $form->{"id_$i"});
265 $exchangerate = 1 unless $exchangerate;
270 qq|JOIN acc_trans a ON (a.chart_id = c.id) | .
272 qq|AND (a.trans_id = ?)|;
273 my ($id) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
275 $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
279 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount) | .
280 qq|VALUES (?, ?, ?, ?)|;
281 do_query($form, $dbh, $query, $form->{"id_$i"}, $id,
282 conv_date($form->{datepaid}), $amount * $ml);
286 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
287 qq| source, memo) | .
288 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
289 my @values = (conv_i($form->{"id_$i"}), $paymentaccno,
290 conv_date($form->{datepaid}),
291 $form->{"paid_$i"} * $ml * -1, $form->{source},
293 do_query($form, $dbh, $query, @values);
295 # add exchangerate difference if currency ne defaultcurrency
296 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
300 # exchangerate difference
302 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
303 qq| cleared, fx_transaction) | .
304 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
305 @values = (conv_i($form->{"id_$i"}), $paymentaccno,
306 conv_date($form->{datepaid}), ($amount * $ml * -1), '0',
308 do_query($form, $dbh, $query, @values);
313 $form->round_amount($form->{"paid_$i"} *
314 ($exchangerate - $form->{exchangerate}), 2);
316 my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
318 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, | .
319 qq| amount, cleared, fx_transaction) | .
320 qq|VALUES (?, ?, ?, ?, ?, ?)|;
321 @values = (conv_i($form->{"id_$i"}), $accno_id,
322 conv_date($form->{datepaid}), $amount * $ml * -1, '0',
324 do_query($form, $dbh, $query, @values);
329 $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
330 $pth->execute($form->{"id_$i"}) || $form->dberror;
331 ($amount) = $pth->fetchrow_array;
334 $amount += $form->{"paid_$i"};
338 if ($form->{arap} eq 'ap') {
339 $paid = "paid = paid + $amount";
341 $paid = "paid = $amount";
344 # update AR/AP transaction
345 $query = qq|UPDATE $arap SET $paid, datepaid = ? WHERE id = ?|;
346 @values = (conv_date($form->{datepaid}), conv_i($form->{"id_$i"}));
347 do_query($form, $dbh, $query, @values);
349 $form->{id} = $form->{"id_$i"};
350 if(!exists $form->{addition}) {
351 $form->{snumbers} = qq|invnumber_| . $form->{"invnumber_$i"};
352 $form->{addition} = "POSTED";
353 $form->save_history($form->dbconnect($myconfig));
355 # /saving the history
359 # record a AR/AP with a payment
360 if ($form->round_amount($paymentamount, 2) > 0) {
361 $form->{invnumber} = "";
362 OP::overpayment("", $myconfig, $form, $dbh, $paymentamount, $ml, 1);
366 if ($form->round_amount($paymentamount, 2) < 0) {
370 if ($form->round_amount($paymentamount, 2) == 0) {
376 $main::lxdebug->leave_sub();