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