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 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($type, $countrycode) = @_;
50 if (-f "locale/$countrycode/Num2text") {
51 require "locale/$countrycode/Num2text";
59 $main::lxdebug->leave_sub();
65 $main::lxdebug->enter_sub();
67 my ($self, $myconfig, $form) = @_;
70 my $dbh = $form->dbconnect($myconfig);
72 my $ARAP = $form->{ARAP} eq "AR" ? "AR" : "AP";
75 qq|SELECT accno, description, link | .
77 qq|WHERE link LIKE ? |.
79 my $sth = prepare_execute_query($form, $dbh, $query, '%' . $ARAP . '%');
81 $form->{PR}{ $form->{ARAP} } = ();
82 $form->{PR}{"$form->{ARAP}_paid"} = ();
84 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
85 foreach my $item (split(/:/, $ref->{link})) {
86 if ($item eq $form->{ARAP}) {
87 push(@{ $form->{PR}{ $form->{ARAP} } }, $ref);
89 if ($item eq "$form->{ARAP}_paid") {
90 push(@{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref);
96 # get currencies and closedto
97 $query = qq|SELECT curr, closedto FROM defaults|;
98 ($form->{currencies}, $form->{closedto}) =
99 selectrow_query($form, $dbh, $query);
103 $main::lxdebug->leave_sub();
107 $main::lxdebug->enter_sub();
109 my ($self, $myconfig, $form) = @_;
111 my $dbh = $form->dbconnect($myconfig);
113 my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
114 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
116 qq|SELECT count(*) | .
117 qq|FROM $vc ct, $arap a | .
118 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid)|;
119 my ($count) = selectrow_query($form, $dbh, $query);
121 # build selection list
122 if ($count < $myconfig->{vclimit}) {
124 qq|SELECT DISTINCT ct.id, ct.name | .
125 qq|FROM $vc ct, $arap a | .
126 qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid) | .
127 qq|ORDER BY ct.name|;
128 $form->{"all_$form->{vc}"} = selectall_hashref_query($form, $dbh, $query);
131 # aufruf für all_deparments rausgenommen, da die abteilungen nur
132 # beim buchen der belege (rechnung, fibu) geändert werden und danach
133 # NICHT mehr überschrieben werden
136 $main::lxdebug->leave_sub();
139 sub get_openinvoices {
140 $main::lxdebug->enter_sub();
142 my ($self, $myconfig, $form) = @_;
144 # connect to database
145 my $dbh = $form->dbconnect($myconfig);
147 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
149 my $buysell = $form->{vc} eq 'customer' ? "buy" : "sell";
150 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
153 qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr | .
155 qq|WHERE (a.${vc}_id = ?) AND (COALESCE(a.curr, '') = ?) AND NOT (a.amount = a.paid)| .
157 my $sth = prepare_execute_query($form, $dbh, $query,
158 conv_i($form->{"${vc}_id"}),
159 "$form->{currency}");
162 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
164 # if this is a foreign currency transaction get exchangerate
165 $ref->{exchangerate} =
166 $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
167 if ($form->{currency} ne $form->{defaultcurrency});
168 push @{ $form->{PR} }, $ref;
177 AND (COALESCE(curr, '') <> ?)
180 ($form->{openinvoices_other_currencies}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{"${vc}_id"}), "$form->{currency}");
184 $main::lxdebug->leave_sub();
187 sub process_payment {
188 $main::lxdebug->enter_sub();
190 my ($self, $myconfig, $form) = @_;
193 # connect to database, turn AutoCommit off
194 my $dbh = $form->dbconnect_noauto($myconfig);
196 my ($paymentaccno) = split /--/, $form->{account};
198 # if currency ne defaultcurrency update exchangerate
199 if ($form->{currency} ne $form->{defaultcurrency}) {
200 $form->{exchangerate} =
201 $form->parse_amount($myconfig, $form->{exchangerate});
203 if ($form->{vc} eq 'customer') {
204 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, $form->{exchangerate}, 0);
206 $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0, $form->{exchangerate});
209 $form->{exchangerate} = 1;
212 my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id FROM defaults|;
213 my ($fxgain_accno_id, $fxloss_accno_id) = selectrow_query($form, $dbh, $query);
215 my $buysell = $form->{vc} eq "customer" ? "buy" : "sell";
216 my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
221 if ($form->{ARAP} eq 'AR') {
223 $where = qq| ((c.link = 'AR') OR (c.link LIKE 'AR:%')) |;
227 qq| ((c.link = 'AP') OR | .
228 qq| (c.link LIKE '%:AP') OR | .
229 qq| (c.link LIKE '%:AP:%')) |;
233 # query to retrieve paid amount
235 qq|SELECT a.paid FROM ar a | .
236 qq|WHERE a.id = ? | .
238 my $pth = prepare_query($form, $dbh, $query);
240 # go through line by line
241 for my $i (1 .. $form->{rowcount}) {
243 $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
244 $form->{"due_$i"} = $form->parse_amount($myconfig, $form->{"due_$i"});
246 if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
248 # get exchangerate for original
250 qq|SELECT $buysell | .
251 qq|FROM exchangerate e | .
252 qq|JOIN ${arap} a ON (a.transdate = e.transdate) | .
253 qq|WHERE (e.curr = ?) AND (a.id = ?)|;
255 selectrow_query($form, $dbh, $query,
256 $form->{currency}, $form->{"id_$i"});
258 $exchangerate = 1 unless $exchangerate;
263 qq|JOIN acc_trans a ON (a.chart_id = c.id) | .
265 qq|AND (a.trans_id = ?)|;
266 my ($id) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
268 $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
272 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount) | .
273 qq|VALUES (?, ?, ?, ?)|;
274 do_query($form, $dbh, $query, $form->{"id_$i"}, $id,
275 conv_date($form->{datepaid}), $amount * $ml);
279 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
280 qq| source, memo) | .
281 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
282 my @values = (conv_i($form->{"id_$i"}), $paymentaccno,
283 conv_date($form->{datepaid}),
284 $form->{"paid_$i"} * $ml * -1, $form->{source},
286 do_query($form, $dbh, $query, @values);
288 # add exchangerate difference if currency ne defaultcurrency
289 $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
293 # exchangerate difference
295 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
296 qq| cleared, fx_transaction) | .
297 qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?)|;
298 @values = (conv_i($form->{"id_$i"}), $paymentaccno,
299 conv_date($form->{datepaid}), ($amount * $ml * -1), '0',
301 do_query($form, $dbh, $query, @values);
306 $form->round_amount($form->{"paid_$i"} *
307 ($exchangerate - $form->{exchangerate}), 2);
309 my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
311 qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, | .
312 qq| amount, cleared, fx_transaction) | .
313 qq|VALUES (?, ?, ?, ?, ?, ?)|;
314 @values = (conv_i($form->{"id_$i"}), $accno_id,
315 conv_date($form->{datepaid}), $amount * $ml * -1, '0',
317 do_query($form, $dbh, $query, @values);
322 $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
323 $pth->execute($form->{"id_$i"}) || $form->dberror;
324 ($amount) = $pth->fetchrow_array;
327 $amount += $form->{"paid_$i"};
331 if ($form->{arap} eq 'ap') {
332 $paid = "paid = paid + $amount";
334 $paid = "paid = $amount";
337 # update AR/AP transaction
338 $query = qq|UPDATE $arap SET $paid, datepaid = ? WHERE id = ?|;
339 @values = (conv_date($form->{datepaid}), conv_i($form->{"id_$i"}));
340 do_query($form, $dbh, $query, @values);
342 $form->{id} = $form->{"id_$i"};
343 if(!exists $form->{addition}) {
344 $form->{snumbers} = qq|invnumber_| . $form->{"invnumber_$i"};
345 $form->{addition} = "POSTED";
348 # /saving the history
352 # Hier wurden negativen Zahlungseingänge abgefangen
353 # da Zahlungsein- und ausgänge immer positiv sind
354 # Besser: in Oberfläche schon prüfen erledigt jb 10.2010
359 $main::lxdebug->leave_sub();