Weitere Vorkommen von GIFI entfernt. GIFI müsste bis auf Spalte chart.gifi_accno...
[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   my %audittrail = (tablename => 'gl',
215                     reference => $form->{reference},
216                     formname  => 'transaction',
217                     action    => 'posted',
218                     id        => $form->{id});
219
220   # $form->audittrail($dbh, "", \%audittrail);
221
222   # commit and redirect
223   my $rc = $dbh->commit;
224   $dbh->disconnect;
225   $main::lxdebug->leave_sub();
226
227   $rc;
228
229 }
230
231 sub all_transactions {
232   my ($self, $myconfig, $form) = @_;
233   $main::lxdebug->enter_sub();
234
235   # connect to database
236   my $dbh = $form->dbconnect($myconfig);
237   my ($query, $sth, $source, $null);
238
239   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
240
241   if ($form->{reference}) {
242     $source = $form->like(lc $form->{reference});
243     $glwhere .= " AND lower(g.reference) LIKE '$source'";
244     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
245     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
246   }
247   if ($form->{department}) {
248     ($null, $source) = split /--/, $form->{department};
249     $glwhere .= " AND g.department_id = $source";
250     $arwhere .= " AND a.department_id = $source";
251     $apwhere .= " AND a.department_id = $source";
252   }
253
254   if ($form->{source}) {
255     $source = $form->like(lc $form->{source});
256     $glwhere .= " AND lower(ac.source) LIKE '$source'";
257     $arwhere .= " AND lower(ac.source) LIKE '$source'";
258     $apwhere .= " AND lower(ac.source) LIKE '$source'";
259   }
260   if ($form->{datefrom}) {
261     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
262     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
263     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
264   }
265   if ($form->{dateto}) {
266     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
267     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
268     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
269   }
270   if ($form->{description}) {
271     my $description = $form->like(lc $form->{description});
272     $glwhere .= " AND lower(g.description) LIKE '$description'";
273     $arwhere .= " AND lower(ct.name) LIKE '$description'";
274     $apwhere .= " AND lower(ct.name) LIKE '$description'";
275   }
276   if ($form->{notes}) {
277     my $notes = $form->like(lc $form->{notes});
278     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
279     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
280     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
281   }
282   if ($form->{accno}) {
283     $glwhere .= " AND c.accno = '$form->{accno}'";
284     $arwhere .= " AND c.accno = '$form->{accno}'";
285     $apwhere .= " AND c.accno = '$form->{accno}'";
286   }
287   if ($form->{category} ne 'X') {
288     $glwhere .=
289       " 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}'))";
290     $arwhere .=
291       " 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}'))";
292     $apwhere .=
293       " 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}'))";
294   }
295   if ($form->{project_id}) {
296     $glwhere .= " AND g.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")";
297     $arwhere .=
298       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
299       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
300     $apwhere .=
301       " AND ((a.globalproject_id = " . conv_i($form->{project_id}, 'NULL') . ") OR " .
302       "      (a.id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE project_id = " . conv_i($form->{project_id}, 'NULL') . ")))";
303   }
304
305   my ($project_columns, %project_join);
306   if ($form->{"l_projectnumbers"}) {
307     $project_columns = ", ac.project_id, pr.projectnumber";
308     $project_join = "LEFT JOIN project pr ON (ac.project_id = pr.id)";
309   }
310
311   if ($form->{accno}) {
312
313     # get category for account
314     $query = qq|SELECT c.category
315                 FROM chart c
316                 WHERE c.accno = '$form->{accno}'|;
317     $sth = $dbh->prepare($query);
318
319     $sth->execute || $form->dberror($query);
320     ($form->{ml}) = $sth->fetchrow_array;
321     $sth->finish;
322
323     if ($form->{datefrom}) {
324       $query = qq|SELECT SUM(ac.amount)
325                   FROM acc_trans ac, chart c
326                   WHERE ac.chart_id = c.id
327                   AND c.accno = '$form->{accno}'
328                   AND ac.transdate < date '$form->{datefrom}'
329                   |;
330       $sth = $dbh->prepare($query);
331       $sth->execute || $form->dberror($query);
332
333       ($form->{balance}) = $sth->fetchrow_array;
334       $sth->finish;
335     }
336   }
337
338   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
339
340   my $sortorder = join ', ',
341     $form->sort_columns(qw(transdate reference source description accno));
342   my %ordinal = (transdate   => 6,
343                  reference   => 4,
344                  source      => 7,
345                  description => 5);
346   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
347
348   if ($form->{sort}) {
349     $sortorder = $form->{sort} . ",";
350   } else {
351     $sortorder = "";
352   }
353
354   my $query =
355     qq|SELECT ac.oid AS acoid, g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, c.link,
356                  g.description, ac.transdate, ac.source, ac.trans_id,
357                  ac.amount, c.accno, g.notes, t.chart_id, ac.oid
358                  $project_columns
359                  FROM gl g, acc_trans ac $project_join, chart c LEFT JOIN tax t ON
360                  (t.chart_id=c.id)
361                  WHERE $glwhere
362                  AND ac.chart_id = c.id
363                  AND g.id = ac.trans_id
364         UNION
365                  SELECT ac.oid AS acoid, a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
366                  ct.name, ac.transdate, ac.source, ac.trans_id,
367                  ac.amount, c.accno, a.notes, t.chart_id, ac.oid
368                  $project_columns
369                  FROM ar a, acc_trans ac $project_join, customer ct, chart c LEFT JOIN tax t ON
370                  (t.chart_id=c.id)
371                  WHERE $arwhere
372                  AND ac.chart_id = c.id
373                  AND a.customer_id = ct.id
374                  AND a.id = ac.trans_id
375         UNION
376                  SELECT ac.oid AS acoid, a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, c.link,
377                  ct.name, ac.transdate, ac.source, ac.trans_id,
378                  ac.amount, c.accno, a.notes, t.chart_id, ac.oid
379                  $project_columns
380                  FROM ap a, acc_trans ac $project_join, vendor ct, chart c LEFT JOIN tax t ON
381                  (t.chart_id=c.id)
382                  WHERE $apwhere
383                  AND ac.chart_id = c.id
384                  AND a.vendor_id = ct.id
385                  AND a.id = ac.trans_id
386                  ORDER BY $sortorder transdate, trans_id, acoid, taxkey DESC|;
387
388   # Show all $query in Debuglevel LXDebug::QUERY
389   $callingdetails = (caller (0))[3];
390   $main::lxdebug->message(LXDebug::QUERY, "$callingdetails \$query=\n $query");
391       
392   my $sth = $dbh->prepare($query);
393   $sth->execute || $form->dberror($query);
394   my $trans_id  = "";
395   my $trans_id2 = "";
396
397   while (my $ref0 = $sth->fetchrow_hashref(NAME_lc)) {
398     
399     $trans_id = $ref0->{id};
400     
401     if ($trans_id != $trans_id2) { # first line of a booking
402     
403       if ($trans_id2) {
404         push @{ $form->{GL} }, $ref;
405         $balance = 0;
406       }
407     
408       $ref       = $ref0;
409       $trans_id2 = $ref->{id};
410
411       # gl
412       if ($ref->{type} eq "gl") {
413         $ref->{module} = "gl";
414       }
415
416       # ap
417       if ($ref->{type} eq "ap") {
418         if ($ref->{invoice}) {
419           $ref->{module} = "ir";
420         } else {
421           $ref->{module} = "ap";
422         }
423       }
424
425       # ar
426       if ($ref->{type} eq "ar") {
427         if ($ref->{invoice}) {
428           $ref->{module} = "is";
429         } else {
430           $ref->{module} = "ar";
431         }
432       }
433
434       $ref->{"projectnumbers"} = {};
435       $ref->{"projectnumbers"}->{$ref->{"projectnumber"}} = 1 if ($ref->{"projectnumber"});
436
437       $balance = $ref->{amount};
438     
439       # Linenumbers of General Ledger  
440       $k       = 0; # Debit      # AP      # Soll
441       $l       = 0; # Credit     # AR      # Haben
442       $i       = 0; # Debit Tax  # AP_tax  # VSt
443       $j       = 0; # Credit Tax # AR_tax  # USt
444       
445
446       if ($ref->{chart_id} > 0) { # all tax accounts first line, no line increasing
447         if ($ref->{amount} < 0) {
448           if ($ref->{link} =~ /AR_tax/) {
449             $ref->{credit_tax}{$j}       = $ref->{amount};
450             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
451           }
452           if ($ref->{link} =~ /AP_tax/) {
453             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
454             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
455           }
456         } else {
457           if ($ref->{link} =~ /AR_tax/) {
458             $ref->{credit_tax}{$j}       = $ref->{amount};
459             $ref->{credit_tax_accno}{$j} = $ref->{accno};              
460           }
461           if ($ref->{link} =~ /AP_tax/) {
462             $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
463             $ref->{debit_tax_accno}{$i} = $ref->{accno};   
464           }
465         }
466       } else { #all other accounts first line
467         if ($ref->{amount} < 0) {
468           $ref->{debit}{$k}        = $ref->{amount} * -1;
469           $ref->{debit_accno}{$k}  = $ref->{accno};
470           $ref->{debit_taxkey}{$k} = $ref->{taxkey};
471           $ref->{ac_transdate}{$k} = $ref->{transdate};
472
473         } else {
474           $ref->{credit}{$l}        = $ref->{amount} * 1;
475           $ref->{credit_accno}{$l}  = $ref->{accno};
476           $ref->{credit_taxkey}{$l} = $ref->{taxkey};
477           $ref->{ac_transdate}{$l}  = $ref->{transdate};
478
479
480         }
481       }
482
483     } else { # following lines of a booking, line increasing
484
485       $ref2      = $ref0;
486       $trans_old  =$trans_id2;
487       $trans_id2 = $ref2->{id};
488   
489       $balance =
490         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
491
492       $ref->{"projectnumbers"}->{$ref2->{"projectnumber"}} = 1 if ($ref2->{"projectnumber"});
493
494       if ($ref2->{chart_id} > 0) { # all tax accounts, following lines
495         if ($ref2->{amount} < 0) {
496           if ($ref2->{link} =~ /AR_tax/) {
497             if ($ref->{credit_tax_accno}{$j} ne "") {
498               $j++;
499             }
500             $ref->{credit_tax}{$j}       = $ref2->{amount};
501             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
502           }
503           if ($ref2->{link} =~ /AP_tax/) {
504             if ($ref->{debit_tax_accno}{$i} ne "") {
505               $i++;
506             }
507             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
508             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
509           }
510         } else {
511           if ($ref2->{link} =~ /AR_tax/) {
512             if ($ref->{credit_tax_accno}{$j} ne "") {
513               $j++;
514             }
515             $ref->{credit_tax}{$j}       = $ref2->{amount};
516             $ref->{credit_tax_accno}{$j} = $ref2->{accno};              
517           }
518           if ($ref2->{link} =~ /AP_tax/) {
519             if ($ref->{debit_tax_accno}{$i} ne "") {
520               $i++;
521             }
522             $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
523             $ref->{debit_tax_accno}{$i} = $ref2->{accno};   
524           }
525         }
526       } else { # all other accounts, following lines
527         if ($ref2->{amount} < 0) {
528           if ($ref->{debit_accno}{$k} ne "") {
529             $k++;
530           }
531           $ref->{debit}{$k}        = $ref2->{amount} * - 1;
532           $ref->{debit_accno}{$k}  = $ref2->{accno};
533           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
534           $ref->{ac_transdate}{$k} = $ref2->{transdate};
535         } else {
536           if ($ref->{credit_accno}{$l} ne "") {
537             $l++;
538           }
539           $ref->{credit}{$l}        = $ref2->{amount};
540           $ref->{credit_accno}{$l}  = $ref2->{accno};
541           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
542           $ref->{ac_transdate}{$l}  = $ref2->{transdate};
543         }
544       }
545     }
546   }
547   push @{ $form->{GL} }, $ref;
548   $sth->finish;
549
550   if ($form->{accno}) {
551     $query =
552       qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
553     $sth = $dbh->prepare($query);
554     $sth->execute || $form->dberror($query);
555
556     ($form->{account_description}) = $sth->fetchrow_array;
557     $sth->finish;
558   }
559
560   $main::lxdebug->leave_sub();
561
562   $dbh->disconnect;
563
564 }
565
566 sub transaction {
567   my ($self, $myconfig, $form) = @_;
568   $main::lxdebug->enter_sub();
569
570   my ($query, $sth, $ref);
571
572   # connect to database
573   my $dbh = $form->dbconnect($myconfig);
574
575   if ($form->{id}) {
576     $query = "SELECT closedto, revtrans
577               FROM defaults";
578     $sth = $dbh->prepare($query);
579     $sth->execute || $form->dberror($query);
580
581     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
582     $sth->finish;
583
584     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
585               d.description AS department, e.name as employee, g.taxincluded, g.gldate
586               FROM gl g
587             LEFT JOIN department d ON (d.id = g.department_id)  
588             LEFT JOIN employee e ON (e.id = g.employee_id)  
589             WHERE g.id = $form->{id}";
590     $sth = $dbh->prepare($query);
591     $sth->execute || $form->dberror($query);
592     $ref = $sth->fetchrow_hashref(NAME_lc);
593     map { $form->{$_} = $ref->{$_} } keys %$ref;
594     $sth->finish;
595
596     # retrieve individual rows
597     $query = qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo,
598                 a.transdate, a.cleared, a.project_id, p.projectnumber,
599                  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
600                 FROM acc_trans a
601                 JOIN chart c ON (c.id = a.chart_id)
602                 LEFT JOIN project p ON (p.id = a.project_id)
603                 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)) 
604                 WHERE a.trans_id = $form->{id}
605                 AND a.fx_transaction = '0'
606                 ORDER BY a.oid,a.transdate|;
607
608     $sth = $dbh->prepare($query);
609     $sth->execute || $form->dberror($query);
610
611     $form->{GL} = [];
612     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
613       push @{ $form->{GL} }, $ref;
614     }
615
616     # get tax description
617     $query = qq| SELECT * FROM tax t order by t.taxkey|;
618     $sth   = $dbh->prepare($query);
619     $sth->execute || $form->dberror($query);
620     $form->{TAX} = [];
621     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
622       push @{ $form->{TAX} }, $ref;
623     }
624
625     $sth->finish;
626   } else {
627     $query = "SELECT closedto, revtrans FROM defaults";
628     ($form->{closedto}, $form->{revtrans}) = $dbh->selectrow_array($query);
629     $query =
630       "SELECT COALESCE(" .
631       "  (SELECT transdate FROM gl WHERE id = " .
632       "    (SELECT MAX(id) FROM gl) LIMIT 1), " .
633       "  current_date)";
634     ($form->{transdate}) = $dbh->selectrow_array($query);
635
636     # get tax description
637     $query = qq| SELECT * FROM tax t order by t.taxkey|;
638     $sth   = $dbh->prepare($query);
639     $sth->execute || $form->dberror($query);
640     $form->{TAX} = ();
641     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
642       push @{ $form->{TAX} }, $ref;
643     }
644   }
645
646   $sth->finish;
647   my $transdate = "current_date";
648   if ($form->{transdate}) {
649     $transdate = qq|'$form->{transdate}'|;
650   }
651   # get chart of accounts
652   $query = qq|SELECT c.accno, c.description, c.link, tk.taxkey_id, tk.tax_id
653                 FROM chart c 
654                 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))
655                 ORDER BY c.accno|;
656   $sth = $dbh->prepare($query);
657   $sth->execute || $form->dberror($query);
658   $form->{chart} = ();
659   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
660     push @{ $form->{chart} }, $ref;
661   }
662   $sth->finish;
663
664   $sth->finish;
665   $main::lxdebug->leave_sub();
666
667   $dbh->disconnect;
668
669 }
670
671 1;
672