unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / SL / CP.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Check and receipt printing payment module backend routines
32 # Number to text conversion routines are in
33 # locale/{countrycode}/Num2text
34 #
35 #======================================================================
36
37 package CP;
38
39
40 sub new {
41   $main::lxdebug->enter_sub();
42
43   my ($type, $countrycode) = @_;
44
45   $self = {};
46
47   if ($countrycode) {
48     if (-f "locale/$countrycode/Num2text") {
49       require "locale/$countrycode/Num2text";
50     } else {
51       use SL::Num2text;
52     }
53   } else {
54     use SL::Num2text;
55   }
56
57   $main::lxdebug->leave_sub();
58
59   bless $self, $type;
60 }
61
62
63 sub paymentaccounts {
64   $main::lxdebug->enter_sub();
65
66   my ($self, $myconfig, $form) = @_;
67
68   # connect to database
69   my $dbh = $form->dbconnect($myconfig);
70   
71   my $query = qq|SELECT c.accno, c.description, c.link
72                  FROM chart c
73                  WHERE c.link LIKE '%$form->{ARAP}%'
74                  ORDER BY c.accno|;
75   my $sth = $dbh->prepare($query);
76   $sth->execute || $form->dberror($query);
77
78   $form->{PR}{$form->{ARAP}} = ();
79   $form->{PR}{"$form->{ARAP}_paid"} = ();
80   
81   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
82     foreach my $item (split /:/, $ref->{link}) {
83       if ($item eq $form->{ARAP}) {
84         push @{ $form->{PR}{$form->{ARAP}} }, $ref;
85       }
86       if ($item eq "$form->{ARAP}_paid") {
87         push @{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref;
88       }
89     }
90   }
91   $sth->finish;
92   
93   # get currencies and closedto
94   $query = qq|SELECT curr, closedto
95               FROM defaults|;
96   $sth = $dbh->prepare($query);
97   $sth->execute || $form->dberror($query);
98   
99   ($form->{currencies}, $form->{closedto}) = $sth->fetchrow_array;
100   $sth->finish;
101
102   $dbh->disconnect;
103
104   $main::lxdebug->leave_sub();
105 }
106
107
108 sub get_openvc {
109   $main::lxdebug->enter_sub();
110
111   my ($self, $myconfig, $form) = @_;
112
113   my $dbh = $form->dbconnect($myconfig);
114
115   my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
116   my $query = qq|SELECT count(*)
117                  FROM $form->{vc} ct, $arap a
118                  WHERE a.$form->{vc}_id = ct.id
119                  AND a.amount != a.paid|;
120   my $sth = $dbh->prepare($query);
121   $sth->execute || $form->dberror($query);
122   my ($count) = $sth->fetchrow_array;
123   $sth->finish;
124
125   my $ref;
126
127   # build selection list
128   if ($count < $myconfig->{vclimit}) {
129     $query = qq|SELECT DISTINCT ct.id, ct.name
130                 FROM $form->{vc} ct, $arap a
131                 WHERE a.$form->{vc}_id = ct.id
132                 AND a.amount != a.paid
133                 ORDER BY ct.name|;
134     $sth = $dbh->prepare($query);
135     $sth->execute || $form->dberror($query);
136
137     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
138       push @{ $form->{"all_$form->{vc}"} }, $ref;
139     }
140
141     $sth->finish;
142
143   }
144
145   if ($form->{ARAP} eq 'AR') {
146     $query = qq|SELECT d.id, d.description
147                 FROM department d
148                 WHERE d.role = 'P'
149                 ORDER BY 2|;
150   } else {
151     $query = qq|SELECT d.id, d.description
152                 FROM department d
153                 ORDER BY 2|;
154   }
155   $sth = $dbh->prepare($query);
156   $sth->execute || $form->dberror($query);
157
158   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
159     push @{ $form->{all_departments} }, $ref;
160   }
161   $sth->finish;
162   
163   $dbh->disconnect;
164
165   $main::lxdebug->leave_sub();
166 }
167
168
169 sub get_openinvoices {
170   $main::lxdebug->enter_sub();
171
172   my ($self, $myconfig, $form) = @_;
173
174   # connect to database
175   my $dbh = $form->dbconnect($myconfig);
176
177   my $where = qq|WHERE a.$form->{vc}_id = $form->{"$form->{vc}_id"}
178                  AND a.curr = '$form->{currency}'
179                  AND NOT a.amount = paid|;
180   
181   my ($buysell);
182   if ($form->{vc} eq 'customer') {
183     $buysell = "buy";
184   } else {
185     $buysell = "sell";
186   }
187   
188   my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr
189                  FROM $form->{arap} a
190                  $where
191                  ORDER BY a.id|;
192   my $sth = $dbh->prepare($query);
193   $sth->execute || $form->dberror($query);
194
195   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
196     # if this is a foreign currency transaction get exchangerate
197     $ref->{exchangerate} = $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell) if ($form->{currency} ne $form->{defaultcurrency});
198     push @{ $form->{PR} }, $ref;
199   }
200   
201   $sth->finish;
202   $dbh->disconnect;
203
204   $main::lxdebug->leave_sub();
205 }
206
207
208
209 sub process_payment {
210   $main::lxdebug->enter_sub();
211
212   my ($self, $myconfig, $form) = @_;
213     
214   # connect to database, turn AutoCommit off
215   my $dbh = $form->dbconnect_noauto($myconfig);
216
217   my ($paymentaccno) = split /--/, $form->{account};
218   
219   # if currency ne defaultcurrency update exchangerate
220   if ($form->{currency} ne $form->{defaultcurrency}) {
221     $form->{exchangerate} = $form->parse_amount($myconfig, $form->{exchangerate});
222
223     if ($form->{vc} eq 'customer') {
224       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, $form->{exchangerate}, 0);
225     } else {
226       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0, $form->{exchangerate});
227     }
228   } else {
229     $form->{exchangerate} = 1;
230   }
231
232   my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id
233                  FROM defaults|;
234   my $sth = $dbh->prepare($query);
235   $sth->execute || $form->dberror($query);
236
237   my ($fxgain_accno_id, $fxloss_accno_id) = $sth->fetchrow_array;
238   $sth->finish;
239
240   my ($buysell);
241   
242   if ($form->{vc} eq 'customer') {
243     $buysell = "buy";
244   } else {
245     $buysell = "sell";
246   }
247
248   my $ml;
249   my $where;
250   
251   if ($form->{ARAP} eq 'AR') {
252     $ml = 1;
253     $where = qq|
254                 (c.link = 'AR'
255                 OR c.link LIKE 'AR:%')
256                 |;
257   } else {
258     $ml = -1;
259     $where = qq|
260                 (c.link = 'AP'
261                 OR c.link LIKE '%:AP'
262                 OR c.link LIKE '%:AP:%')
263                 |;
264   }
265   
266   $paymentamount = $form->{amount}; 
267   
268 #  $paymentamount = $form->{amount};  
269   my $null;
270   ($null, $form->{department_id}) = split /--/, $form->{department};
271   $form->{department_id} *= 1;
272
273
274   # query to retrieve paid amount
275   $query = qq|SELECT a.paid FROM ar a
276               WHERE a.id = ?
277               FOR UPDATE|;
278   my $pth = $dbh->prepare($query) || $form->dberror($query);
279
280   # go through line by line
281   for my $i (1 .. $form->{rowcount}) {
282
283     $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
284     $form->{"due_$i"} = $form->parse_amount($myconfig, $form->{"due_$i"});
285     
286     if ($form->{"checked_$i"} && $form->{"paid_$i"}) {     
287       $paymentamount = (($paymentamount * 1000) - ($form->{"paid_$i"} * 1000)) / 1000;
288
289       # get exchangerate for original 
290       $query = qq|SELECT $buysell
291                   FROM exchangerate e
292                   JOIN $form->{arap} a ON (a.transdate = e.transdate)
293                   WHERE e.curr = '$form->{currency}'
294                   AND a.id = $form->{"id_$i"}|;
295       $sth = $dbh->prepare($query);
296       $sth->execute || $form->dberror($query);
297
298       my ($exchangerate) = $sth->fetchrow_array;
299       $sth->finish;
300
301       $exchangerate = 1 unless $exchangerate;
302
303       $query = qq|SELECT c.id
304                   FROM chart c
305                   JOIN acc_trans a ON (a.chart_id = c.id)
306                   WHERE $where
307                   AND a.trans_id = $form->{"id_$i"}|;
308       $sth = $dbh->prepare($query);
309       $sth->execute || $form->dberror($query);
310
311       my ($id) = $sth->fetchrow_array;
312       $sth->finish;
313
314      
315       $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
316       
317       # add AR/AP
318       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
319                   amount)
320                   VALUES ($form->{"id_$i"}, $id, '$form->{datepaid}',
321                   $amount * $ml)|;
322       $dbh->do($query) || $form->dberror($query);
323       # add payment
324       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
325                   amount, source, memo)
326                   VALUES ($form->{"id_$i"},
327                          (SELECT c.id FROM chart c
328                           WHERE c.accno = '$paymentaccno'),
329                   '$form->{datepaid}', $form->{"paid_$i"} * $ml * -1,
330                   '$form->{source}', '$form->{memo}')|;
331       $dbh->do($query) || $form->dberror($query);
332       # add exchangerate difference if currency ne defaultcurrency
333       $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1), 2);
334       if ($amount != 0) {
335         # exchangerate difference
336         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
337                     amount, cleared, fx_transaction)
338                     VALUES ($form->{"id_$i"},
339                            (SELECT c.id FROM chart c
340                             WHERE c.accno = '$paymentaccno'),
341                   '$form->{datepaid}', $amount * $ml * -1, '0', '1')|;
342         $dbh->do($query) || $form->dberror($query);
343
344         # gain/loss
345         
346         $amount = $form->round_amount($form->{"paid_$i"} * ($exchangerate - $form->{exchangerate}), 2);
347         if ($amount != 0) {
348           my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
349           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
350                       amount, cleared, fx_transaction)
351                       VALUES ($form->{"id_$i"}, $accno_id,
352                       '$form->{datepaid}', $amount * $ml * -1, '0', '1')|;
353           $dbh->do($query) || $form->dberror($query);
354         }
355       }
356
357       $form->{"paid_$i"} = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
358
359       $pth->execute($form->{"id_$i"}) || $form->dberror;
360       ($amount) = $pth->fetchrow_array;
361       $pth->finish;
362
363       $amount += $form->{"paid_$i"};
364                   
365       # update AR/AP transaction
366       $query = qq|UPDATE $form->{arap} set
367                   paid = $amount,
368                   datepaid = '$form->{datepaid}'
369                   WHERE id = $form->{"id_$i"}|;
370       $dbh->do($query) || $form->dberror($query);
371     }
372   }
373
374
375   # record a AR/AP with a payment
376   if ($form->round_amount($paymentamount, 2) > 0) {
377     $form->{invnumber} = "";
378     OP::overpayment("", $myconfig, $form, $dbh, $paymentamount, $ml, 1);
379   }
380   
381   if ($form->round_amount($paymentamount, 2) < 0) {
382     $dbh->rollback;
383     $rc = 0;
384   }  
385   if ($form->round_amount($paymentamount, 2) == 0) {
386     $rc = $dbh->commit;
387   } 
388   
389   $dbh->disconnect;
390
391   $main::lxdebug->leave_sub();
392
393   return $rc;
394 }
395
396
397 1;
398