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