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,
78 $form->{ARAP} eq "AR" ? "AR" : "AP" );
80 $form->{PR}{ $form->{ARAP} } = ();
81 $form->{PR}{"$form->{ARAP}_paid"} = ();
83 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
84 foreach my $item (split(/:/, $ref->{link})) {
85 if ($item eq $form->{ARAP}) {
86 push(@{ $form->{PR}{ $form->{ARAP} } }, $ref);
88 if ($item eq "$form->{ARAP}_paid") {
89 push(@{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref);
95 # get currencies and closedto
96 $query = qq|SELECT curr, closedto FROM defaults|;
97 ($form->{currencies}, $form->{closedto}) =
98 selectrow_query($form, $dbh, $query);
102 $main::lxdebug->leave_sub();
106 $main::lxdebug->enter_sub();
108 my ($self, $myconfig, $form) = @_;
110 my $dbh = $form->dbconnect($myconfig);
112 my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
113 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
115 qq|SELECT count(*) | .
116 qq|FROM $vc ct, $arap a | .
117 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid)|;
118 my ($count) = selectrow_query($form, $dbh, $query);
120 # build selection list
121 if ($count < $myconfig->{vclimit}) {
123 qq|SELECT DISTINCT ct.id, ct.name | .
124 qq|FROM $vc ct, $arap a | .
125 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid) | .
126 qq|ORDER BY ct.name|;
127 $form->{"all_$form->{vc}"} = selectall_hashref_query($form, $dbh, $query);
130 if ($form->{ARAP} eq 'AR') {
132 qq|SELECT d.id, d.description | .
133 qq|FROM department d | .
134 qq|WHERE d.role = 'P' | .
138 qq|SELECT d.id, d.description | .
139 qq|FROM department d | .
142 $form->{all_departments} = selectall_hashref_query($form, $dbh, $query);
146 $main::lxdebug->leave_sub();
149 sub get_openinvoices {
150 $main::lxdebug->enter_sub();
152 my ($self, $myconfig, $form) = @_;
154 # connect to database
155 my $dbh = $form->dbconnect($myconfig);
157 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
159 my $buysell = $form->{vc} eq 'customer' ? "buy" : "sell";
160 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
163 qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr | .
165 qq|WHERE (a.${vc}_id = ?) AND (a.curr = ?) AND NOT (a.amount = paid)|;
167 my $sth = prepare_execute_query($form, $dbh, $query,
168 conv_i($form->{"${vc}_id"}),
172 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
174 # if this is a foreign currency transaction get exchangerate
175 $ref->{exchangerate} =
176 $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
177 if ($form->{currency} ne $form->{defaultcurrency});
178 push @{ $form->{PR} }, $ref;
184 $main::lxdebug->leave_sub();
187 sub process_payment {
188 $main::lxdebug->enter_sub();
190 my ($self, $myconfig, $form) = @_;
192 # connect to database, turn AutoCommit off
193 my $dbh = $form->dbconnect_noauto($myconfig);
195 my ($paymentaccno) = split /--/, $form->{account};
197 # if currency ne defaultcurrency update exchangerate
198 if ($form->{currency} ne $form->{defaultcurrency}) {
199 $form->{exchangerate} =
200 $form->parse_amount($myconfig, $form->{exchangerate});
202 if ($form->{vc} eq 'customer') {
203 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid},
204 $form->{exchangerate}, 0);
206 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0,
207 $form->{exchangerate});
210 $form->{exchangerate} = 1;
214 qq|SELECT fxgain_accno_id, fxloss_accno_id FROM defaults|;
215 my ($fxgain_accno_id, $fxloss_accno_id) =
216 selectrow_query($form, $dbh, $query);
218 my $buysell = $form->{vc} eq "customer" ? "buy" : "sell";
219 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
224 if ($form->{ARAP} eq 'AR') {
226 $where = qq| ((c.link = 'AR') OR (c.link LIKE 'AR:%')) |;
230 qq| ((c.link = 'AP') OR | .
231 qq| (c.link LIKE '%:AP') OR | .
232 qq| (c.link LIKE '%:AP:%')) |;
235 $paymentamount = $form->{amount};
237 # $paymentamount = $form->{amount};
239 ($null, $form->{department_id}) = split(/--/, $form->{department});
240 $form->{department_id} *= 1;
242 # query to retrieve paid amount
244 qq|SELECT a.paid FROM ar a | .
245 qq|WHERE a.id = ? | .
247 my $pth = prepare_query($form, $dbh, $query);
249 # go through line by line
250 for my $i (1 .. $form->{rowcount}) {
252 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
253 $form->{"due_$i"} = $form->parse_amount($myconfig, $form->{"due_$i"});
255 if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
257 (($paymentamount * 1000) - ($form->{"paid_$i"} * 1000)) / 1000;
259 # get exchangerate for original
261 qq|SELECT $buysell | .
262 qq|FROM exchangerate e | .
263 qq|JOIN ${arap} a ON (a.transdate = e.transdate) | .
264 qq|WHERE (e.curr = ?) AND (a.id = ?)|;
266 selectrow_query($form, $dbh, $query,
267 $form->{currency}, $form->{"id_$i"});
269 $exchangerate = 1 unless $exchangerate;
274 qq|JOIN acc_trans a ON (a.chart_id = c.id) | .
276 qq|AND (a.trans_id = ?)|;
277 my ($id) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
279 $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
283 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount) | .
284 qq|VALUES (?, ?, ?, ?)|;
285 do_query($form, $dbh, $query, $form->{"id_$i"}, $id,
286 conv_date($form->{datepaid}), $amount * $ml);
290 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
291 qq| source, memo) | .
292 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
293 my @values = (conv_i($form->{"id_$i"}), $paymentaccno,
294 conv_date($form->{datepaid}),
295 $form->{"paid_$i"} * $ml * -1, $form->{source},
297 do_query($form, $dbh, $query, @values);
299 # add exchangerate difference if currency ne defaultcurrency
301 $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
305 # exchangerate difference
307 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
308 qq| cleared, fx_transaction) | .
309 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
310 @values = (conv_i($form->{"id_$i"}), $paymentaccno,
311 conv_date($form->{datepaid}), ($amount * $ml * -1), '0',
313 do_query($form, $dbh, $query, @values);
318 $form->round_amount($form->{"paid_$i"} *
319 ($exchangerate - $form->{exchangerate}), 2);
321 my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
323 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, | .
324 qq| amount, cleared, fx_transaction) | .
325 qq|VALUES (?, ?, ?, ?, ?, ?)|;
326 @values = (conv_i($form->{"id_$i"}), $accno_id,
327 conv_date($form->{datepaid}), $amount * $ml * -1, '0',
329 do_query($form, $dbh, $query, @values);
334 $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
336 $pth->execute($form->{"id_$i"}) || $form->dberror;
337 ($amount) = $pth->fetchrow_array;
340 $amount += $form->{"paid_$i"};
343 if ($form->{arap} eq 'ap') {
344 $paid = "paid = paid + $amount";
346 $paid = "paid = $amount";
349 # update AR/AP transaction
350 $query = qq|UPDATE $arap SET $paid, datepaid = ? WHERE id = ?|;
351 @values = (conv_date($form->{datepaid}), conv_i($form->{"id_$i"}));
352 do_query($form, $dbh, $query, @values);
356 # record a AR/AP with a payment
357 if ($form->round_amount($paymentamount, 2) > 0) {
358 $form->{invnumber} = "";
359 OP::overpayment("", $myconfig, $form, $dbh, $paymentamount, $ml, 1);
362 if ($form->round_amount($paymentamount, 2) < 0) {
366 if ($form->round_amount($paymentamount, 2) == 0) {
372 $main::lxdebug->leave_sub();