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