Umstellung der Form.pm auf die Verwendung parametrisierter Queries zur Vermeidung...
[kivitendo-erp.git] / SL / GL.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 # General ledger backend code
32 #
33 # CHANGE LOG:
34 #   DS. 2000-07-04  Created
35 #   DS. 2001-06-12  Changed relations from accno to chart_id
36 #
37 #======================================================================
38
39 package GL;
40
41 use Data::Dumper;
42 use SL::DBUtils;
43
44 sub delete_transaction {
45   my ($self, $myconfig, $form) = @_;
46   $main::lxdebug->enter_sub();
47
48   # connect to database
49   my $dbh = $form->dbconnect_noauto($myconfig);
50
51   my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
52   $dbh->do($query) || $form->dberror($query);
53
54   $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
55   $dbh->do($query) || $form->dberror($query);
56
57   # commit and redirect
58   my $rc = $dbh->commit;
59   $dbh->disconnect;
60   $main::lxdebug->leave_sub();
61
62   $rc;
63
64 }
65
66 sub post_transaction {
67   my ($self, $myconfig, $form) = @_;
68   $main::lxdebug->enter_sub();
69
70   my ($debit, $credit) = (0, 0);
71   my $project_id;
72
73   my $i;
74
75   # check if debit and credit balances
76
77   if ($form->{storno}) {
78     $form->{reference}   = "Storno-" . $form->{reference};
79     $form->{description} = "Storno-" . $form->{description};
80   }
81
82   # connect to database, turn off AutoCommit
83   my $dbh = $form->dbconnect_noauto($myconfig);
84
85   # post the transaction
86   # make up a unique handle and store in reference field
87   # then retrieve the record based on the unique handle to get the id
88   # replace the reference field with the actual variable
89   # add records to acc_trans
90
91   # if there is a $form->{id} replace the old transaction
92   # delete all acc_trans entries and add the new ones
93
94   # escape '
95   map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
96
97   if (!$form->{taxincluded}) {
98     $form->{taxincluded} = 0;
99   }
100
101   my ($query, $sth);
102
103   if ($form->{id}) {
104
105     # delete individual transactions
106     $query = qq|DELETE FROM acc_trans 
107                 WHERE trans_id = $form->{id}|;
108     $dbh->do($query) || $form->dberror($query);
109
110   } else {
111     my $uid = time;
112     $uid .= $form->{login};
113
114     $query = qq|INSERT INTO gl (reference, employee_id)
115                 VALUES ('$uid', (SELECT e.id FROM employee e
116                                  WHERE e.login = '$form->{login}'))|;
117     $dbh->do($query) || $form->dberror($query);
118
119     $query = qq|SELECT g.id FROM gl g
120                 WHERE g.reference = '$uid'|;
121     $sth = $dbh->prepare($query);
122     $sth->execute || $form->dberror($query);
123
124     ($form->{id}) = $sth->fetchrow_array;
125     $sth->finish;
126
127   }
128
129   my ($null, $department_id) = split /--/, $form->{department};
130   $department_id *= 1;
131
132   $query = qq|UPDATE gl SET 
133               reference = '$form->{reference}',
134               description = '$form->{description}',
135               notes = '$form->{notes}',
136               transdate = '$form->{transdate}',
137               department_id = $department_id,
138               taxincluded = '$form->{taxincluded}'
139               WHERE id = $form->{id}|;
140
141   $dbh->do($query) || $form->dberror($query);
142   ($taxkey, $rate) = split(/--/, $form->{taxkey});
143
144   # insert acc_trans transactions
145   for $i (1 .. $form->{rowcount}) {
146     my $taxkey;
147     my $rate;
148     # extract accno
149     print(STDERR $form->{"taxchart_$i"}, "TAXCHART\n");
150     my ($accno) = split(/--/, $form->{"accno_$i"});
151     my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
152     ($form->{"tax_id_$i"}, $NULL) = split /--/, $form->{"taxchart_$i"};
153     if ($form->{"tax_id_$i"} ne "") {
154       $query = qq|SELECT t.taxkey, t.rate
155               FROM tax t
156               WHERE t.id=$form->{"tax_id_$i"}|;
157   
158       $sth = $dbh->prepare($query);
159       $sth->execute || $form->dberror($query);
160       ($taxkey, $rate) =
161         $sth->fetchrow_array;
162       $sth->finish;
163     }
164
165     my $amount = 0;
166     my $debit  = $form->{"debit_$i"};
167     my $credit = $form->{"credit_$i"};
168     my $tax    = $form->{"tax_$i"};
169
170     if ($credit) {
171       $amount = $credit;
172       $posted = 0;
173     }
174     if ($debit) {
175       $amount = $debit * -1;
176       $tax    = $tax * -1;
177       $posted = 0;
178     }
179
180     $project_id = conv_i($form->{"project_id_$i"});
181
182     # if there is an amount, add the record
183     if ($amount != 0) {
184       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
185                   source, memo, project_id, taxkey)
186                   VALUES
187                   ($form->{id}, (SELECT c.id
188                                  FROM chart c
189                                  WHERE c.accno = '$accno'),
190                    $amount, '$form->{transdate}', |
191         . $dbh->quote($form->{"source_$i"}) . qq|, |
192         . $dbh->quote($form->{"memo_$i"}) . qq|,
193                   ?, $taxkey)|;
194
195       do_query($form, $dbh, $query, $project_id);
196     }
197
198     if ($tax != 0) {
199       # add taxentry
200       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
201                   source, memo, project_id, taxkey)
202                   VALUES
203                   ($form->{id}, (SELECT t.chart_id
204                   FROM tax t
205                   WHERE t.id = $form->{"tax_id_$i"}),
206                   $tax, '$form->{transdate}', |
207         . $dbh->quote($form->{"source_$i"}) . qq|, |
208         . $dbh->quote($form->{"memo_$i"}) . qq|, ?, $taxkey)|;
209
210       do_query($form, $dbh, $query, $project_id);
211     }
212   }
213
214   # commit and redirect
215   my $rc = $dbh->commit;
216   $dbh->disconnect;
217   $main::lxdebug->leave_sub();
218
219   $rc;
220
221 }
222
223 sub all_transactions {
224   my ($self, $myconfig, $form) = @_;
225   $main::lxdebug->enter_sub();
226
227   # connect to database
228   my $dbh = $form->dbconnect($myconfig);
229   my ($query, $sth, $source, $null);
230
231   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
232
233   if ($form->{reference}) {
234     $source = $form->like(lc $form->{reference});
235     $glwhere .= " AND lower(g.reference) LIKE '$source'";
236     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
237     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
238   }
239   if ($form->{department}) {
240     ($null, $source) = split /--/, $form->{department};
241     $glwhere .= " AND g.department_id = $source";
242     $arwhere .= " AND a.department_id = $source";
243     $apwhere .= " AND a.department_id = $source";
244   }
245
246   if ($form->{source}) {
247     $source = $form->like(lc $form->{source});
248     $glwhere .= " AND lower(ac.source) LIKE '$source'";
249     $arwhere .= " AND lower(ac.source) LIKE '$source'";
250     $apwhere .= " AND lower(ac.source) LIKE '$source'";
251   }
252   if ($form->{datefrom}) {
253     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
254     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
255     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
256   }
257   if ($form->{dateto}) {
258     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
259     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
260     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
261   }
262   if ($form->{description}) {
263     my $description = $form->like(lc $form->{description});
264     $glwhere .= " AND lower(g.description) LIKE '$description'";
265     $arwhere .= " AND lower(ct.name) LIKE '$description'";
266     $apwhere .= " AND lower(ct.name) LIKE '$description'";
267   }
268   if ($form->{notes}) {
269     my $notes = $form->like(lc $form->{notes});
270     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
271     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
272     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
273   }
274   if ($form->{accno}) {
275     $glwhere .= " AND c.accno = '$form->{accno}'";
276     $arwhere .= " AND c.accno = '$form->{accno}'";
277     $apwhere .= " AND c.accno = '$form->{accno}'";
278   }
279   if ($form->{category} ne 'X') {
280     $glwhere .=
281       " AND gl.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
282     $arwhere .=
283       " AND ar.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
284     $apwhere .=
285       " AND ap.id in (SELECT trans_id FROM acc_trans ac2 WHERE ac2.chart_id IN (SELECT id FROM chart c2 WHERE c2.category = '$form->{category}'))";
286   }
287   if ($form->{project_id}) {
288     $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
289     $arwhere .=
290       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
291       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
292     $apwhere .=
293       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
294       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
295   }
296
297   my ($project_columns, %project_join);
298   if ($form->{"l_projectnumbers"}) {
299     $project_columns = ", ac.project_id, pr.projectnumber";
300     $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
301   }
302
303   if ($form->{accno}) {
304
305     # get category for account
306     $query = qq|SELECT c.category
307                 FROM chart c
308                 WHERE c.accno = '$form->{accno}'|;
309     $sth = $dbh->prepare($query);
310
311     $sth->execute || $form->dberror($query);
312     ($form->{ml}) = $sth->fetchrow_array;
313     $sth->finish;
314
315     if ($form->{datefrom}) {
316       $query = qq|SELECT SUM(ac.amount)
317                   FROM acc_trans ac, chart c
318                   WHERE ac.chart_id = c.id
319                   AND c.accno = '$form->{accno}'
320                   AND ac.transdate < date '$form->{datefrom}'
321                   |;
322       $sth = $dbh->prepare($query);
323       $sth->execute || $form->dberror($query);
324
325       ($form->{balance}) = $sth->fetchrow_array;
326       $sth->finish;
327     }
328   }
329
330   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
331
332   my $sortorder = join ', ',
333     $form->sort_columns(qw(transdate reference source description accno));
334   my %ordinal = (transdate   => 6,
335                  reference   => 4,
336                  source      => 7,
337                  description => 5);
338   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
339
340   if ($form->{sort}) {
341     $sortorder = $form->{sort} . ",";
342   } else {
343     $sortorder = "";
344   }
345
346   my $query =
347     qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
348                  g.description, ac.transdate, ac.source, ac.trans_id,
349                  ac.amount, c.accno, g.notes, t.chart_id, ac.oid
350                  $project_columns
351                  FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
352                  (t.chart_id=c.id)
353                  WHERE $glwhere
354                  AND ac.chart_id = c.id
355                  AND g.id = ac.trans_id
356         UNION
357                  SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
358                  ct.name, ac.transdate, ac.source, ac.trans_id,
359                  ac.amount, c.accno, a.notes, t.chart_id, ac.oid
360                  $project_columns
361                  FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
362                  (t.chart_id=c.id)
363                  WHERE $arwhere
364                  AND ac.chart_id = c.id
365                  AND a.customer_id = ct.id
366                  AND a.id = ac.trans_id
367         UNION
368                  SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
369                  ct.name, ac.transdate, ac.source, ac.trans_id,
370                  ac.amount, c.accno, a.notes, t.chart_id, ac.oid
371                  $project_columns
372                  FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
373                  (t.chart_id=c.id)
374                  WHERE $apwhere
375                  AND ac.chart_id = c.id
376                  AND a.vendor_id = ct.id
377                  AND a.id = ac.trans_id
378                  ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
379
380   # Show all $query in Debuglevel LXDebug::QUERY
381   $callingdetails = (caller (0))[3];
382   $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
383       
384   my $sth = $dbh->prepare($query);
385   $sth->execute || $form->dberror($query);
386   my $trans_id  = "";
387   my $trans_id2 = "";
388
389   while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
390     
391     $trans_id = $ref0->{id};
392     
393     if ($trans_id != $trans_id2) { # first line of a booking
394     
395       if ($trans_id2) {
396         push @{ $form->{GL} }, $ref;
397         $balance = 0;
398       }
399     
400       $ref       = $ref0;
401       $trans_id2 = $ref->{id};
402
403       # gl
404       if ($ref->{type} eq "gl") {
405         $ref->{module} = "gl";
406       }
407
408       # ap
409       if ($ref->{type} eq "ap") {
410         if ($ref->{invoice}) {
411           $ref->{module} = "ir";
412         } else {
413           $ref->{module} = "ap";
414         }
415       }
416
417       # ar
418       if ($ref->{type} eq "ar") {
419         if ($ref->{invoice}) {
420           $ref->{module} = "is";
421         } else {
422           $ref->{module} = "ar";
423         }
424       }
425
426       $ref->{"projectnumbers"} = {};
427       $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
428
429       $balance = $ref->{amount};
430     
431       # Linenumbers of General Ledger  
432       $k       = 0; # Debit      # AP      # Soll
433       $l       = 0; # Credit     # AR      # Haben
434       $i       = 0; # Debit Tax  # AP_tax  # VSt
435       $j       = 0; # Credit Tax # AR_tax  # USt
436       
437
438       if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
439         if ($ref->{amount} < 0) {
440           if ($ref->{link} =~ /AR_tax/) {
441             $ref->{credit_tax}{$j}       = $ref->{amount};
442             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
443           }
444           if ($ref->{link} =~ /AP_tax/) {
445             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
446             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
447           }
448         } else {
449           if ($ref->{link} =~ /AR_tax/) {
450             $ref->{credit_tax}{$j}       = $ref->{amount};
451             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
452           }
453           if ($ref->{link} =~ /AP_tax/) {
454             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
455             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
456           }
457         }
458       } else { #all other accounts first line
459         if ($ref->{amount} < 0) {
460           $ref->{debit}{$k}        = $ref->{amount} * -1;
461           $ref->{debit_accno}{$k}  = $ref->{accno};
462           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
463           $ref->{ac_transdate}{$k} = $ref->{transdate};
464
465         } else {
466           $ref->{credit}{$l}        = $ref->{amount} * 1;
467           $ref->{credit_accno}{$l}  = $ref->{accno};
468           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
469           $ref->{ac_transdate}{$l}  = $ref->{transdate};
470
471
472         }
473       }
474
475     } else { # following lines of a booking, line increasing
476
477       $ref2      = $ref0;
478       $trans_old  =$trans_id2;
479       $trans_id2 = $ref2->{id};
480   
481       $balance =
482         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
483
484       $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
485
486       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
487         if ($ref2->{amount} < 0) {
488           if ($ref2->{link} =~ /AR_tax/) {
489             if ($ref->{credit_tax_accno}{$j} ne "") {
490               $j++;
491             }
492             $ref->{credit_tax}{$j}       = $ref2->{amount};
493             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
494           }
495           if ($ref2->{link} =~ /AP_tax/) {
496             if ($ref->{debit_tax_accno}{$i} ne "") {
497               $i++;
498             }
499             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
500             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
501           }
502         } else {
503           if ($ref2->{link} =~ /AR_tax/) {
504             if ($ref->{credit_tax_accno}{$j} ne "") {
505               $j++;
506             }
507             $ref->{credit_tax}{$j}       = $ref2->{amount};
508             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
509           }
510           if ($ref2->{link} =~ /AP_tax/) {
511             if ($ref->{debit_tax_accno}{$i} ne "") {
512               $i++;
513             }
514             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
515             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
516           }
517         }
518       } else { # all other accounts, following lines
519         if ($ref2->{amount} < 0) {
520           if ($ref->{debit_accno}{$k} ne "") {
521             $k++;
522           }
523           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
524           $ref->{debit_accno}{$k}  = $ref2->{accno};
525           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
526           $ref->{ac_transdate}{$k} = $ref2->{transdate};
527         } else {
528           if ($ref->{credit_accno}{$l} ne "") {
529             $l++;
530           }
531           $ref->{credit}{$l}        = $ref2->{amount};
532           $ref->{credit_accno}{$l}  = $ref2->{accno};
533           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
534           $ref->{ac_transdate}{$l}  = $ref2->{transdate};
535         }
536       }
537     }
538   }
539   push @{ $form->{GL} }, $ref;
540   $sth->finish;
541
542   if ($form->{accno}) {
543     $query =
544       qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
545     $sth = $dbh->prepare($query);
546     $sth->execute || $form->dberror($query);
547
548     ($form->{account_description}) = $sth->fetchrow_array;
549     $sth->finish;
550   }
551
552   $main::lxdebug->leave_sub();
553
554   $dbh->disconnect;
555
556 }
557
558 sub transaction {
559   my ($self, $myconfig, $form) = @_;
560   $main::lxdebug->enter_sub();
561
562   my ($query, $sth, $ref);
563
564   # connect to database
565   my $dbh = $form->dbconnect($myconfig);
566
567   if ($form->{id}) {
568     $query = "SELECT closedto, revtrans
569               FROM defaults";
570     $sth = $dbh->prepare($query);
571     $sth->execute || $form->dberror($query);
572
573     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
574     $sth->finish;
575
576     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
577               d.description AS department, e.name as employee, g.taxincluded, g.gldate
578               FROM gl g
579             LEFT JOIN department d ON (d.id = g.department_id)  
580             LEFT JOIN employee e ON (e.id = g.employee_id)  
581             WHERE g.id = $form->{id}";
582     $sth = $dbh->prepare($query);
583     $sth->execute || $form->dberror($query);
584     $ref = $sth->fetchrow_hashref(NAME_lc);
585     map { $form->{$_} = $ref->{$_} } keys %$ref;
586     $sth->finish;
587
588     # retrieve individual rows
589     $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
590                 a.transdate, a.cleared, a.project_id, p.projectnumber,
591                  a.taxkey, t.rate AS taxrate, t.id, (SELECT c1.accno FROM chart c1, tax t1 WHERE t1.id=t.id AND c1.id=t.chart_id) AS taxaccno, (SELECT tk.tax_id FROM taxkeys tk WHERE tk.chart_id =a.chart_id AND tk.startdate<=a.transdate ORDER BY tk.startdate desc LIMIT 1) AS tax_id
592                 FROM acc_trans a
593                 JOIN chart c ON (c.id = a.chart_id)
594                 LEFT JOIN project p ON (p.id = a.project_id)
595                 LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link LIKE '%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1)) 
596                 WHERE a.trans_id = $form->{id}
597                 AND a.fx_transaction = '0'
598                 ORDER BY a.oid,a.transdate|;
599
600     $sth = $dbh->prepare($query);
601     $sth->execute || $form->dberror($query);
602
603     $form->{GL} = [];
604     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
605       push @{ $form->{GL} }, $ref;
606     }
607
608     # get tax description
609     $query = qq| SELECT * FROM tax t order by t.taxkey|;
610     $sth   = $dbh->prepare($query);
611     $sth->execute || $form->dberror($query);
612     $form->{TAX} = [];
613     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
614       push @{ $form->{TAX} }, $ref;
615     }
616
617     $sth->finish;
618   } else {
619     $query = "SELECT closedto, revtrans FROM defaults";
620     ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
621     $query =
622       "SELECT COALESCE(" .
623       "  (SELECT transdate FROM gl WHERE id = " .
624       "    (SELECT MAX(id) FROM gl) LIMIT 1), " .
625       "  current_date)";
626     ($form->{transdate}) = $dbh->selectrow_array($query);
627
628     # get tax description
629     $query = qq| SELECT * FROM tax t order by t.taxkey|;
630     $sth   = $dbh->prepare($query);
631     $sth->execute || $form->dberror($query);
632     $form->{TAX} = ();
633     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
634       push @{ $form->{TAX} }, $ref;
635     }
636   }
637
638   $sth->finish;
639   my $transdate = "current_date";
640   if ($form->{transdate}) {
641     $transdate = qq|'$form->{transdate}'|;
642   }
643   # get chart of accounts
644   $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
645                 FROM chart c 
646                 LEFT JOIN taxkeys tk ON (tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1))
647                 ORDER BY c.accno|;
648   $sth = $dbh->prepare($query);
649   $sth->execute || $form->dberror($query);
650   $form->{chart} = ();
651   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
652     push @{ $form->{chart} }, $ref;
653   }
654   $sth->finish;
655
656   $sth->finish;
657   $main::lxdebug->leave_sub();
658
659   $dbh->disconnect;
660
661 }
662
663 1;
664