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