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