Bei Nummernkreisen auch Sonderzeichen erlauben (Perls eigener Algorithmus hinter...
[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 use Data::Dumper;
38 use SL::DBUtils;
39
40 sub post_transaction {
41   $main::lxdebug->enter_sub();
42
43   my ($self, $myconfig, $form) = @_;
44
45   my ($null, $taxrate, $amount, $tax, $diff);
46   my $exchangerate = 0;
47   my $i;
48
49   my @values;
50
51   my $dbh = $form->dbconnect_noauto($myconfig);
52
53   if ($form->{currency} eq $form->{defaultcurrency}) {
54     $form->{exchangerate} = 1;
55   } else {
56     $exchangerate =
57       $form->check_exchangerate($myconfig, $form->{currency},
58                                 $form->{transdate}, 'buy');
59   }
60   for $i (1 .. $form->{rowcount}) {
61     $form->{AR_amounts}{"amount_$i"} =
62       (split(/--/, $form->{"AR_amount_$i"}))[0];
63   }
64   $form->{AR_amounts}{receivables} = $form->{ARselected};
65   $form->{AR}{receivables}         = $form->{ARselected};
66
67   $form->{exchangerate} =
68     ($exchangerate)
69     ? $exchangerate
70     : $form->parse_amount($myconfig, $form->{exchangerate});
71
72   for $i (1 .. $form->{rowcount}) {
73
74     $form->{"amount_$i"} =
75       $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"})
76                             * $form->{exchangerate},
77                           2);
78
79     $form->{netamount} += $form->{"amount_$i"};
80
81     # parse tax_$i for later
82     $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"});
83   }
84
85   # this is for ar
86
87   $form->{amount} = $form->{netamount};
88
89   $form->{tax}       = 0;
90   $form->{netamount} = 0;
91   $form->{total_tax} = 0;
92
93   # taxincluded doesn't make sense if there is no amount
94
95   $form->{taxincluded} = 0 if ($form->{amount} == 0);
96   for $i (1 .. $form->{rowcount}) {
97     ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
98
99     $query =
100       qq|SELECT c.accno, t.taxkey, t.rate | .
101       qq|FROM tax t LEFT JOIN chart c on (c.id = t.chart_id) | .
102       qq|WHERE t.id = ? | .
103       qq|ORDER BY c.accno|;
104
105     ($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) =
106       selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"});
107     $form->{AR_amounts}{"tax_$i"}{taxkey}    = $form->{"taxkey_$i"};
108     $form->{AR_amounts}{"amount_$i"}{taxkey} = $form->{"taxkey_$i"};
109
110     if ($form->{taxincluded} *= 1) {
111       if (!$form->{"korrektur_$i"}) {
112       $tax =
113         $form->{"amount_$i"} -
114         ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1));
115       } else {
116         $tax = $form->{"tax_$i"};
117       }
118       $amount = $form->{"amount_$i"} - $tax;
119       $form->{"amount_$i"} = $form->round_amount($amount, 2);
120       $diff += $amount - $form->{"amount_$i"};
121       $form->{"tax_$i"} = $form->round_amount($tax, 2);
122       $form->{netamount} += $form->{"amount_$i"};
123     } else {
124       if (!$form->{"korrektur_$i"}) {
125         $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"};
126       }
127       $form->{"tax_$i"} =
128         $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2);
129       $form->{netamount} += $form->{"amount_$i"};
130     }
131     
132     $form->{total_tax} += $form->{"tax_$i"};
133   }
134
135   # adjust paidaccounts if there is no date in the last row
136   $form->{paidaccounts}-- unless ($form->{"datepaid_$form->{paidaccounts}"});
137   $form->{paid} = 0;
138
139   # add payments
140   for $i (1 .. $form->{paidaccounts}) {
141     $form->{"paid_$i"} =
142       $form->round_amount($form->parse_amount($myconfig, $form->{"paid_$i"}),
143                           2);
144
145     $form->{paid} += $form->{"paid_$i"};
146     $form->{datepaid} = $form->{"datepaid_$i"};
147
148   }
149
150   $form->{amount} = $form->{netamount} + $form->{total_tax};
151   $form->{paid}   =
152     $form->round_amount($form->{paid} * $form->{exchangerate}, 2);
153
154   my ($query, $sth, $null);
155
156   ($null, $form->{employee_id}) = split /--/, $form->{employee};
157   unless ($form->{employee_id}) {
158     $form->get_employee($dbh);
159   }
160
161   # if we have an id delete old records
162   if ($form->{id}) {
163     # delete detail records
164     $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
165     do_query($form, $dbh, $query, $form->{id});
166
167   } else {
168     $query = qq|SELECT nextval('glid')|;
169     ($form->{id}) = selectrow_query($form, $dbh, $query);
170
171     $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|;
172     do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
173
174     if (!$form->{invnumber}) {
175       $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh);
176     }
177   }
178
179   # update department
180   ($null, $form->{department_id}) = split(/--/, $form->{department});
181   $form->{department_id} *= 1;
182
183   # record last payment date in ar table
184   $form->{datepaid} = $form->{transdate} unless $form->{datepaid};
185   my $datepaid = ($form->{paid} != 0) ? $form->{datepaid} : undef;
186
187   $query =
188     qq|UPDATE ar set | .
189     qq|  invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, | .
190     qq|  taxincluded = ?, amount = ?, duedate = ?, paid = ?, datepaid = ?, | .
191     qq|  netamount = ?, curr = ?, notes = ?, department_id = ?, | .
192     qq|  employee_id = ? | .
193     qq|WHERE id = ?|;
194   my @values = ($form->{invnumber}, $form->{ordnumber},
195                 conv_date($form->{transdate}), conv_i($form->{customer_id}),
196                 $form->{taxincluded} ? 't' : 'f', $form->{amount},
197                 conv_date($form->{duedate}), $form->{paid},
198                 conv_date($datepaid), $form->{netamount},
199                 $form->{currency}, $form->{notes},
200                 conv_i($form->{department_id}),
201                 conv_i($form->{employee_id}),
202                 conv_i($form->{id}));
203   do_query($form, $dbh, $query, @values);
204
205   # amount for AR account
206   $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1;
207
208   # update exchangerate
209   if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
210     $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
211                                $form->{exchangerate}, 0);
212   }
213
214   # add individual transactions for AR, amount and taxes
215   for $i (1 .. $form->{rowcount}) {
216     if ($form->{"amount_$i"} != 0) {
217       my $project_id = conv_i($form->{"project_id_$i"});
218       $taxkey = $form->{AR_amounts}{"amount_$i"}{taxkey};
219
220       # insert detail records in acc_trans
221       $query =
222         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
223         qq|  project_id, taxkey) | .
224         qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|;
225       @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}),
226                  conv_i($form->{"amount_$i"}), conv_date($form->{transdate}),
227                  $project_id, conv_i($taxkey));
228       do_query($form, $dbh, $query, @values);
229
230       if ($form->{"tax_$i"} != 0) {
231         # insert detail records in acc_trans
232         $query =
233           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | .
234           qq|  project_id, taxkey) | .
235           qq|VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), | .
236           qq|  ?, ?, ?, ?)|;
237         @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}),
238                    conv_i($form->{"tax_$i"}), conv_date($form->{transdate}),
239                    $project_id, conv_i($taxkey));
240         do_query($form, $dbh, $query, @values);
241       }
242     }
243   }
244
245   # add recievables
246   $query =
247     qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) | .
248     qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, | .
249     qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
250   @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables},
251              conv_i($form->{receivables}), conv_date($form->{transdate}),
252              $form->{AR_amounts}{receivables});
253   do_query($form, $dbh, $query, @values);
254
255   # add paid transactions
256   for my $i (1 .. $form->{paidaccounts}) {
257     if ($form->{"paid_$i"} != 0) {
258       my $project_id = conv_i($form->{"paid_project_id_$i"});
259
260       $form->{"AR_paid_$i"} =~ s/\"//g;
261       ($form->{AR}{"paid_$i"}) = split(/--/, $form->{"AR_paid_$i"});
262       $form->{"datepaid_$i"} = $form->{transdate}
263         unless ($form->{"datepaid_$i"});
264
265       $exchangerate = 0;
266       if ($form->{currency} eq $form->{defaultcurrency}) {
267         $form->{"exchangerate_$i"} = 1;
268       } else {
269         $exchangerate =
270           $form->check_exchangerate($myconfig, $form->{currency},
271                                     $form->{"datepaid_$i"}, 'buy');
272
273         $form->{"exchangerate_$i"} =
274           ($exchangerate)
275           ? $exchangerate
276           : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
277       }
278
279       # if there is no amount and invtotal is zero there is no exchangerate
280       if ($form->{amount} == 0 && $form->{netamount} == 0) {
281         $form->{exchangerate} = $form->{"exchangerate_$i"};
282       }
283
284       # receivables amount
285       $amount =
286         $form->round_amount($form->{"paid_$i"} * $form->{exchangerate}, 2);
287
288       if ($form->{receivables} != 0) {
289         # add receivable
290         $query =
291           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
292           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
293           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
294         @values = (conv_i($form->{id}), $form->{AR}{receivables},
295                    $amount, conv_date($form->{"datepaid_$i"}), $project_id,
296                    $form->{AR}{receivables});
297         do_query($form, $dbh, $query, @values);
298       }
299       $form->{receivables} = $amount;
300
301       if ($form->{"paid_$i"} != 0) {
302         my $project_id = conv_i($form->{"paid_project_id_$i"});
303         # add payment
304         $amount = $form->{"paid_$i"} * -1;
305         $query  =
306           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
307           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
308           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
309         @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount,
310                    conv_date($form->{"datepaid_$i"}), $form->{"source_$i"},
311                    $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"});
312         do_query($form, $dbh, $query, @values);
313
314         # exchangerate difference for payment
315         $amount =
316           $form->round_amount(
317                     $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1,
318                     2);
319
320         if ($amount != 0) {
321           $query =
322             qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
323             qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
324             qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
325           @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"},
326                      $amount, conv_date($form->{"datepaid_$i"}), $project_id,
327                      $form->{AR}{"paid_$i"});
328           do_query($form, $dbh, $query, @values);
329         }
330
331         # exchangerate gain/loss
332         $amount =
333           $form->round_amount(
334                    $form->{"paid_$i"} *
335                      ($form->{exchangerate} - $form->{"exchangerate_$i"}) * -1,
336                    2);
337
338         if ($amount != 0) {
339           $accno =
340             ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno};
341           $query =
342             qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) | .
343             qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, | .
344             qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
345           @values = (conv_i($form->{id}), $accno, $amount,
346                      conv_date($form->{"datepaid_$i"}), $project_id, $accno);
347           do_query($form, $dbh, $query, @values);
348         }
349       }
350
351       # update exchangerate record
352       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
353         $form->update_exchangerate($dbh, $form->{currency},
354                                    $form->{"datepaid_$i"},
355                                    $form->{"exchangerate_$i"}, 0);
356       }
357     }
358   }
359
360   my $rc = $dbh->commit;
361   $dbh->disconnect;
362
363   $main::lxdebug->leave_sub();
364
365   return $rc;
366 }
367
368 sub post_payment {
369   $main::lxdebug->enter_sub();
370
371   my ($self, $myconfig, $form, $locale) = @_;
372
373   # connect to database, turn off autocommit
374   my $dbh = $form->dbconnect_noauto($myconfig);
375
376   $form->{datepaid} = $form->{transdate};
377
378   # total payments, don't move we need it here
379   for my $i (1 .. $form->{paidaccounts}) {
380     if ($form->{type} eq "credit_note") {
381       $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"}) * -1;
382     } else {
383       $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
384     } 
385     $form->{paid} += $form->{"paid_$i"};
386     $form->{datepaid} = $form->{"datepaid_$i"} if ($form->{"datepaid_$i"});
387   }
388
389   $form->{exchangerate} =
390       $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
391                               "buy");
392
393   my $accno_ar = $form->{ARselected};
394
395   # record payments and offsetting AR
396   for my $i (1 .. $form->{paidaccounts}) {
397
398     if ($form->{"paid_$i"} != 0) {
399       my $project_id = conv_i($form->{"paid_project_id_$i"});
400
401       my ($accno) = split /--/, $form->{"AR_paid_$i"};
402       $form->{"datepaid_$i"} = $form->{transdate}
403         unless ($form->{"datepaid_$i"});
404       $form->{datepaid} = $form->{"datepaid_$i"};
405
406       $exchangerate = 0;
407       if (($form->{currency} eq $form->{defaultcurrency}) || ($form->{defaultcurrency} eq "")) {
408         $form->{"exchangerate_$i"} = 1;
409       } else {
410         $exchangerate =
411           $form->check_exchangerate($myconfig, $form->{currency},
412                                     $form->{"datepaid_$i"}, 'buy');
413
414         $form->{"exchangerate_$i"} =
415           ($exchangerate)
416           ? $exchangerate
417           : $form->parse_amount($myconfig, $form->{"exchangerate_$i"});
418       }
419
420       # record AR
421       $amount =
422         $form->round_amount($form->{"paid_$i"} * $form->{"exchangerate"},
423                             2);
424
425
426       $query =
427         qq|DELETE FROM acc_trans | .
428         qq|WHERE trans_id = ? AND amount = ? AND transdate = ? AND | .
429         qq|  chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?)|;
430       @values = (conv_i($form->{id}), $amount,
431                  conv_date($form->{"datepaid_$i"}), $accno_ar);
432       do_query($form, $dbh, $query, @values);
433
434       $query =
435         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) | .
436         qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, | .
437         qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
438       @values = (conv_i($form->{id}), $accno_ar, conv_i($amount),
439                  conv_date($form->{"datepaid_$i"}), $project_id, $accno_ar);
440       do_query($form, $dbh, $query, @values);
441
442       # record payment
443       $form->{"paid_$i"} *= -1;
444
445       $query =
446         qq|DELETE FROM acc_trans | .
447         qq|WHERE trans_id = ? AND | .
448         qq|  chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
449         qq|  amount = ? AND transdate = ? AND source = ? AND memo = ?|;
450       @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
451                  conv_date($form->{"datepaid_$i"}),
452                  $form->{"source_$i"}, $form->{"memo_$i"});
453       do_query($form, $dbh, $query, @values);
454
455       $query =
456         qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) | .
457         qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, | .
458         qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
459       @values = (conv_i($form->{id}), $accno, conv_i($form->{"paid_$i"}),
460                  conv_date($form->{"datepaid_$i"}),
461                  $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $accno);
462       do_query($form, $dbh, $query, @values);
463
464       # gain/loss
465       $amount =
466         $form->{"paid_$i"} * $form->{exchangerate} - $form->{"paid_$i"} *
467         $form->{"exchangerate_$i"};
468       if ($amount > 0) {
469         $form->{fx}{ $form->{fxgain_accno} }{ $form->{"datepaid_$i"} } +=
470           $amount;
471       } else {
472         $form->{fx}{ $form->{fxloss_accno} }{ $form->{"datepaid_$i"} } +=
473           $amount;
474       }
475
476       $diff = 0;
477
478       # update exchange rate
479       if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
480         $form->update_exchangerate($dbh, $form->{currency},
481                                    $form->{"datepaid_$i"},
482                                    $form->{"exchangerate_$i"}, 0);
483       }
484     }
485   }
486
487   # record exchange rate differences and gains/losses
488   foreach my $accno (keys %{ $form->{fx} }) {
489     foreach my $transdate (keys %{ $form->{fx}{$accno} }) {
490       if (
491           ($form->{fx}{$accno}{$transdate} =
492            $form->round_amount($form->{fx}{$accno}{$transdate}, 2)
493           ) != 0
494         ) {
495         $query =
496           qq|DELETE FROM acc_trans | .
497           qq|WHERE trans_id = ? AND | .
498           qq|  chart_id = (SELECT c.id FROM chart c WHERE c.accno = ?) AND | .
499           qq|  amount = ? AND transdate = ? AND cleared = 'f' AND fx_transaction = 't'|;
500         @values = (conv_i($form->{id}), $accno,
501                    conv_i($form->{fx}{$accno}{$transdate}),
502                    conv_date($transdate));
503         do_query($form, $dbh, $query, @values);
504
505         $query =
506           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, cleared, fx_transaction, project_id, taxkey) | .
507           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 'f', 't', ?, | .
508           qq|        (SELECT taxkey_id FROM chart WHERE accno = ?))|;
509         @values = (conv_i($form->{id}), $accno,
510                    $form->{fx}{$accno}{$transdate},
511                    conv_date($transdate), $project_id,
512                    $form->{fx}{$accno}{$transdate});
513         do_query($form, $dbh, $query, @values);
514       }
515     }
516   }
517   my $datepaid = ($form->{paid}) ? $form->{datepaid} : "NULL";
518
519   # save AR record
520   my $query =
521     qq|UPDATE ar set paid = ?, datepaid = ? WHERE id = ?|;
522   @values = (conv_i($form->{paid}), conv_date($datepaid), conv_i($form->{id}));
523   do_query($form, $dbh, $query, @values);
524
525   my $rc = $dbh->commit;
526   $dbh->disconnect;
527
528   $main::lxdebug->leave_sub();
529
530   return $rc;
531 }
532
533 sub delete_transaction {
534   $main::lxdebug->enter_sub();
535
536   my ($self, $myconfig, $form) = @_;
537
538   # connect to database, turn AutoCommit off
539   my $dbh = $form->dbconnect_noauto($myconfig);
540
541   my $query = qq|DELETE FROM ar WHERE id = ?|;
542   do_query($form, $dbh, $query, $form->{id});
543
544   $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|;
545   do_query($form, $dbh, $query, $form->{id});
546
547   # commit
548   my $rc = $dbh->commit;
549   $dbh->disconnect;
550
551   $main::lxdebug->leave_sub();
552
553   return $rc;
554 }
555
556 sub ar_transactions {
557   $main::lxdebug->enter_sub();
558
559   my ($self, $myconfig, $form) = @_;
560
561   # connect to database
562   my $dbh = $form->dbconnect($myconfig);
563
564   my @values;
565
566   my $query =
567     qq|SELECT a.id, a.invnumber, a.ordnumber, a.transdate, | .
568     qq|  a.duedate, a.netamount, a.amount, a.paid, | .
569     qq|  a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | .
570     qq|  a.shippingpoint, a.storno, a.globalproject_id, | .
571     qq|  pr.projectnumber AS globalprojectnumber, | .
572     qq|  c.name, | .
573     qq|  e.name AS employee | .
574     qq|FROM ar a | .
575     qq|JOIN customer c ON (a.customer_id = c.id) | .
576     qq|LEFT JOIN employee e ON (a.employee_id = e.id) | .
577     qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)|;
578
579   my $where = "1 = 1";
580   if ($form->{customer_id}) {
581     $where .= " AND a.customer_id = ?";
582     push(@values, $form->{customer_id});
583   } elsif ($form->{customer}) {
584     $where .= " AND c.name ILIKE ?";
585     push(@values, $form->like($form->{customer}));
586   }
587   if ($form->{department}) {
588     my ($null, $department_id) = split /--/, $form->{department};
589     $where .= " AND a.department_id = ?";
590     push(@values, $department_id);
591   }
592   foreach my $column (qw(invnumber ordnumber notes)) {
593     if ($form->{$column}) {
594       $where .= " AND a.$column ILIKE ?";
595       push(@values, $form->like($form->{$column}));
596     }
597   }
598   if ($form->{"project_id"}) {
599     $where .=
600       qq|AND ((a.globalproject_id = ?) OR EXISTS | .
601       qq|  (SELECT * FROM invoice i | .
602       qq|   WHERE i.project_id = ? AND i.trans_id = a.id))|;
603     push(@values, $form->{"project_id"}, $form->{"project_id"});
604   }
605
606   if ($form->{transdatefrom}) {
607     $where .= " AND a.transdate >= ?";
608     push(@values, $form->{transdatefrom});
609   }
610   if ($form->{transdateto}) {
611     $where .= " AND a.transdate <= ?";
612     push(@values, $form->{transdateto});
613   }
614   if ($form->{open} || $form->{closed}) {
615     unless ($form->{open} && $form->{closed}) {
616       $where .= " AND a.amount <> a.paid" if ($form->{open});
617       $where .= " AND a.amount = a.paid"  if ($form->{closed});
618     }
619   }
620
621   my @a = (transdate, invnumber, name);
622   push @a, "employee" if $form->{l_employee};
623   my $sortorder = join(', ', @a);
624
625   if (grep({ $_ eq $form->{sort} }
626            qw(id transdate duedate invnumber ordnumber name
627               datepaid employee shippingpoint shipvia))) {
628     $sortorder = $form->{sort};
629   }
630
631   $query .= " WHERE $where ORDER by $sortorder";
632
633   my $sth = $dbh->prepare($query);
634   $sth->execute(@values) ||
635     $form->dberror($query . " (" . join(", ", @values) . ")");
636
637   $form->{AR} = [];
638   while (my $ar = $sth->fetchrow_hashref(NAME_lc)) {
639     push @{ $form->{AR} }, $ar;
640   }
641
642   $sth->finish;
643   $dbh->disconnect;
644
645   $main::lxdebug->leave_sub();
646 }
647
648 sub get_transdate {
649   $main::lxdebug->enter_sub();
650
651   my ($self, $myconfig, $form) = @_;
652
653   # connect to database
654   my $dbh = $form->dbconnect($myconfig);
655
656   my $query =
657     "SELECT COALESCE(" .
658     "  (SELECT transdate FROM ar WHERE id = " .
659     "    (SELECT MAX(id) FROM ar) LIMIT 1), " .
660     "  current_date)";
661   ($form->{transdate}) = $dbh->selectrow_array($query);
662
663   $dbh->disconnect;
664
665   $main::lxdebug->leave_sub();
666 }
667
668 1;
669