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