unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / SL / AR.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) 2001
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 # Accounts Receivable module backend routines
32 #
33 #======================================================================
34
35 package AR;
36
37
38 sub post_transaction {
39   $main::lxdebug->enter_sub();
40
41   my ($self, $myconfig, $form) = @_;
42
43   my ($null, $taxrate, $amount, $tax, $diff);
44   my $exchangerate = 0;
45   my $i;
46   
47   my $dbh = $form->dbconnect_noauto($myconfig);
48   
49   if ($form->{currency} eq $form->{defaultcurrency}) {
50     $form->{exchangerate} = 1;
51   } else {
52     $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'buy');
53   }
54   
55   $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate}); 
56
57  
58   for $i (1 .. 1) {
59     
60     $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2);
61     
62     $form->{netamount} += $form->{"amount_$i"};
63   }
64   # this is for ar
65   
66   $form->{amount} = $form->{netamount}; 
67   
68   $form->{tax}=0;
69   # taxincluded doesn't make sense if there is no amount
70
71   $form->{taxincluded} = 0 if ($form->{amount} == 0);
72   
73   $query = qq| SELECT c.accno, t.rate FROM chart c, tax t where c.id=t.chart_id AND t.taxkey=$form->{taxkey}|;
74   $sth = $dbh->prepare($query);
75   $sth->execute || $form->dberror($query);  
76   ($form->{AR}{tax}, $form->{taxrate}) =  $sth->fetchrow_array;
77   $sth->finish;
78   
79   $form->{tax} = $form->{amount_1} * $form->{taxrate};
80   $form->{tax} = $form->round_amount($form->{tax} * $form->{exchangerate}, 2);
81   $form->{total_tax} += $form->{tax};
82   
83   # adjust paidaccounts if there is no date in the last row
84   $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
85   $form->{paid} = 0;
86   # add payments
87   for $i (1 .. $form->{paidaccounts}) {
88     $form->{"paid_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}), 2);
89     
90     $form->{paid} += $form->{"paid_$i"};
91     $form->{datepaid} = $form->{"datepaid_$i"};
92
93   }
94
95
96   if ($form->{taxincluded} *= 1) {
97     for $i (1 .. 1) {
98       $tax = $form->{"amount_$i"} - ( $form->{"amount_$i"} / ($form->{taxrate} + 1)) ;
99       $amount = $form->{"amount_$i"} - $tax;
100       $form->{"amount_$i"} = $form->round_amount($amount, 2);
101       $diff += $amount - $form->{"amount_$i"};
102       $form->{tax} = $form->round_amount($tax,2);
103       $form->{total_tax} = $form->{tax};
104     }
105
106     # deduct difference from amount_1
107     # $form->{amount_1} += $form->round_amount($diff, 2);
108     $form->{netamount} = $form->{amount_1};
109
110   }
111
112   $form->{amount} = $form->{netamount} + $form->{total_tax};
113   $form->{paid} = $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
114   
115
116   my ($query, $sth, $null);
117   
118   ($null, $form->{employee_id}) = split /--/, $form->{employee};
119   unless ($form->{employee_id}) {
120     $form->get_employee($dbh); 
121   }
122   
123   # if we have an id delete old records
124   if ($form->{id}) {
125
126     # delete detail records
127     $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
128     $dbh->do($query) || $form->dberror($query);
129     
130   } else {
131     my $uid = rand().time;
132
133     $uid .= $form->{login};
134
135     $uid = substr($uid,2,75);
136
137     $query = qq|INSERT INTO ar (invnumber, employee_id)
138                 VALUES ('$uid', $form->{employee_id})|;
139     $dbh->do($query) || $form->dberror($query);
140     
141     $query = qq|SELECT a.id FROM ar a
142                 WHERE a.invnumber = '$uid'|;
143     $sth = $dbh->prepare($query);
144     $sth->execute || $form->dberror($query);
145
146     ($form->{id}) = $sth->fetchrow_array;
147     $sth->finish;
148
149   }
150
151   
152   # update department
153   ($null, $form->{department_id}) = split(/--/, $form->{department});
154   $form->{department_id} *= 1;
155
156   # escape '
157   map { $form->{$_} =~ s/\'/\'\'/g } qw(invnumber ordnumber notes);
158
159   # record last payment date in ar table
160   $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
161   my $datepaid = ($form->{paid} != 0) ? qq|'$form->{datepaid}'| : 'NULL';
162
163   $query = qq|UPDATE ar set
164               invnumber = '$form->{invnumber}',
165               ordnumber = '$form->{ordnumber}',
166               transdate = '$form->{transdate}',
167               customer_id = $form->{customer_id},
168               taxincluded = '$form->{taxincluded}',
169               amount = $form->{amount},
170               duedate = '$form->{duedate}',
171               paid = $form->{paid},
172               datepaid = $datepaid,
173               netamount = $form->{netamount},
174               curr = '$form->{currency}',
175               notes = '$form->{notes}',
176               department_id = $form->{department_id},
177               employee_id = $form->{employee_id}
178               WHERE id = $form->{id}|;
179   $dbh->do($query) || $form->dberror($query);
180
181   
182   # amount for AR account
183   $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
184
185
186   # update exchangerate
187   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
188     $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
189   }
190   # add individual transactions for AR, amount and taxes
191   foreach my $item (keys %{ $form->{AR} }) {
192     if ($form->{$item} != 0) {
193       $project_id = 'NULL';
194       if ($item =~ /amount_/) {
195         if ($form->{"project_id_$'"} && $form->{"projectnumber_$'"}) {
196           $project_id = $form->{"project_id_$'"};
197         }
198       }
199       
200       # insert detail records in acc_trans
201       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
202                                          project_id, taxkey)
203                   VALUES ($form->{id}, (SELECT c.id FROM chart c
204                                         WHERE c.accno = '$form->{AR}{$item}'),
205                   $form->{$item}, '$form->{transdate}', $project_id, '$form->{taxkey}')|;
206       $dbh->do($query) || $form->dberror($query);
207     }
208   }
209
210   
211   # add paid transactions
212   for my $i (1 .. $form->{paidaccounts}) {
213     if ($form->{"paid_$i"} != 0) {
214       
215       $form->{"AR_paid_$i"} =~ s/\"//g;
216       ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
217       $form->{"datepaid_$i"} = $form->{transdate} unless ($form->{"datepaid_$i"});
218      
219       $exchangerate = 0;
220       if ($form->{currency} eq $form->{defaultcurrency}) {
221         $form->{"exchangerate_$i"} = 1;
222       } else {
223         $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
224         
225         $form->{"exchangerate_$i"} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{"exchangerate_$i"}); 
226       }
227       
228      
229       # if there is no amount and invtotal is zero there is no exchangerate
230       if ($form->{amount} == 0 && $form->{netamount} == 0) {
231         $form->{exchangerate} = $form->{"exchangerate_$i"};
232       }
233       
234       # receivables amount
235       $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
236       
237       if ($form->{receivables} != 0) {
238         # add receivable
239         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
240                     transdate)
241                     VALUES ($form->{id},
242                            (SELECT c.id FROM chart c
243                             WHERE c.accno = '$form->{AR}{receivables}'),
244                     $amount, '$form->{"datepaid_$i"}')|;
245         $dbh->do($query) || $form->dberror($query);
246       }
247       $form->{receivables} = $amount;
248       
249       $form->{"memo_$i"} =~ s/\'/\'\'/g;
250
251       if ($form->{"paid_$i"} != 0) {
252         # add payment
253         $amount = $form->{"paid_$i"} * -1;
254         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
255                     transdate, source, memo)
256                     VALUES ($form->{id},
257                            (SELECT c.id FROM chart c
258                             WHERE c.accno = '$form->{AR}{"paid_$i"}'),
259                     $amount, '$form->{"datepaid_$i"}',
260                     '$form->{"source_$i"}', '$form->{"memo_$i"}')|;
261         $dbh->do($query) || $form->dberror($query);
262         
263         
264         # exchangerate difference for payment
265         $amount = $form->round_amount($form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1, 2);
266           
267         if ($amount != 0) {
268           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
269                       transdate, fx_transaction, cleared)
270                       VALUES ($form->{id},
271                              (SELECT c.id FROM chart c
272                               WHERE c.accno = '$form->{AR}{"paid_$i"}'),
273                       $amount, '$form->{"datepaid_$i"}', '1', '0')|;
274           $dbh->do($query) || $form->dberror($query);
275         }
276           
277         # exchangerate gain/loss
278         $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1, 2);
279         
280         if ($amount != 0) {
281           $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
282           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount,
283                       transdate, fx_transaction, cleared)
284                       VALUES ($form->{id}, (SELECT c.id FROM chart c
285                                             WHERE c.accno = '$accno'),
286                       $amount, '$form->{"datepaid_$i"}', '1', '0')|;
287           $dbh->do($query) || $form->dberror($query);
288         }
289       }
290       
291       # update exchangerate record
292       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
293         $form->update_exchangerate($dbh, $form->{currency}, $form->{"datepaid_$i"}, $form->{"exchangerate_$i"}, 0);
294       }
295     }
296   }
297
298  
299   my $rc = $dbh->commit;
300   $dbh->disconnect;
301
302   $main::lxdebug->leave_sub();
303
304   return $rc;
305 }
306
307
308
309 sub delete_transaction {
310   $main::lxdebug->enter_sub();
311
312   my ($self, $myconfig, $form) = @_;
313
314   # connect to database, turn AutoCommit off
315   my $dbh = $form->dbconnect_noauto($myconfig);
316
317   my $query = qq|DELETE FROM ar WHERE id = $form->{id}|;
318   $dbh->do($query) || $form->dberror($query);
319
320   $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
321   $dbh->do($query) || $form->dberror($query);
322   
323   # commit
324   my $rc = $dbh->commit;
325   $dbh->disconnect;
326   
327   $main::lxdebug->leave_sub();
328
329   return $rc;
330 }
331
332
333
334 sub ar_transactions {
335   $main::lxdebug->enter_sub();
336
337   my ($self, $myconfig, $form) = @_;
338
339   # connect to database
340   my $dbh = $form->dbconnect($myconfig);
341
342   my $query = qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate,
343                  a.duedate, a.netamount, a.amount, a.paid, c.name,
344                  a.invoice, a.datepaid, a.terms, a.notes, a.shipvia,
345                  a.shippingpoint,
346                  e.name AS employee
347                  FROM ar a
348               JOIN customer c ON (a.customer_id = c.id)
349               LEFT JOIN employee e ON (a.employee_id = e.id)|;
350               
351   my $where = "1 = 1";
352   if ($form->{customer_id}) {
353     $where .= " AND a.customer_id = $form->{customer_id}";
354   } else {
355     if ($form->{customer}) {
356       my $customer = $form->like(lc $form->{customer});
357       $where .= " AND lower(c.name) LIKE '$customer'";
358     }
359   }
360   if ($form->{department}) {
361     my ($null, $department_id) = split /--/, $form->{department};
362     $where .= " AND a.department_id = $department_id";
363   }
364   if ($form->{invnumber}) {
365     my $invnumber = $form->like(lc $form->{invnumber});
366     $where .= " AND lower(a.invnumber) LIKE '$invnumber'";
367   }
368   if ($form->{ordnumber}) {
369     my $ordnumber = $form->like(lc $form->{ordnumber});
370     $where .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
371   }
372   if ($form->{notes}) {
373     my $notes = $form->like(lc $form->{notes});
374     $where .= " AND lower(a.notes) LIKE '$notes'";
375   }
376   
377   $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
378   $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
379   if ($form->{open} || $form->{closed}) {
380     unless ($form->{open} && $form->{closed}) {
381     $where .= " AND a.amount <> a.paid" if ($form->{open});
382     $where .= " AND a.amount = a.paid" if ($form->{closed});
383     }
384   }
385
386   my @a = (transdate, invnumber, name);
387   push @a, "employee" if $form->{l_employee};
388   my $sortorder = join ', ', $form->sort_columns(@a);
389   $sortorder = $form->{sort} unless $sortorder;
390   
391   $query .= "WHERE $where
392              ORDER by $sortorder";
393   
394   my $sth = $dbh->prepare($query);
395   $sth->execute || $form->dberror($query);
396
397   while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
398     push @{ $form->{AR} }, $ar;
399   }
400   
401   $sth->finish;
402   $dbh->disconnect;
403
404   $main::lxdebug->leave_sub();
405 }
406
407
408 1;
409