unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / SL / GL.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # General ledger backend code
32 #
33 # CHANGE LOG:
34 #   DS. 2000-07-04  Created
35 #   DS. 2001-06-12  Changed relations from accno to chart_id
36 #
37 #======================================================================
38
39 package GL;
40
41 use Data::Dumper;
42
43 sub delete_transaction {
44   $main::lxdebug->enter_sub();
45
46   my ($self, $myconfig, $form) = @_;
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   
61   $main::lxdebug->leave_sub();
62
63   return $rc;
64 }
65
66
67 sub post_transaction {
68   $main::lxdebug->enter_sub();
69
70   my ($self, $myconfig, $form) = @_;
71   
72   my ($debit, $credit) = (0, 0);
73   my $project_id;
74
75   my $i;
76   # check if debit and credit balances
77   
78   $debit = abs(int($form->round_amount($form->{debit},3)*1000));
79   $credit = abs(int($form->round_amount($form->{credit},3)*1000));
80   $tax = abs(int($form->round_amount($form->{tax},3)*1000));
81   
82   if ((($debit >= $credit) && (abs($debit-($credit +$tax))>4)) || (($debit < $credit) && (abs(($debit + $tax)-$credit)>4))) {
83    return -2;
84   }
85
86   if (($debit + $credit +$tax) == 0) {
87     return -3;
88   }
89
90   
91   $debit = $form->round_amount($form->{debit}, 2);
92   $credit = $form->round_amount($form->{credit}, 2);
93   $tax = $form->round_amount($form->{tax}, 2);
94   debug($debit,$credit,$tax,"Betraege");
95   
96   if ($form->{storno}) {
97         $debit = $debit * -1;
98         $credit = $credit * -1;
99         $tax = $tax * -1;
100         $form->{reference} = "Storno-".$form->{reference};
101         $form->{description} = "Storno-".$form->{description};
102   }  
103
104   # connect to database, turn off AutoCommit
105   my $dbh = $form->dbconnect_noauto($myconfig);
106
107   # post the transaction
108   # make up a unique handle and store in reference field
109   # then retrieve the record based on the unique handle to get the id
110   # replace the reference field with the actual variable
111   # add records to acc_trans
112
113   # if there is a $form->{id} replace the old transaction
114   # delete all acc_trans entries and add the new ones
115
116   # escape '
117   map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
118
119   if (!$form->{taxincluded}) {
120     $form->{taxincluded} = 0;
121   }
122   
123   my ($query, $sth);
124   if ($form->{id}) {
125     # delete individual transactions
126     $query = qq|DELETE FROM acc_trans 
127                 WHERE trans_id = $form->{id}|;
128     $dbh->do($query) || $form->dberror($query);
129     
130   } else {
131     my $uid = time;
132     $uid .= $form->{login};
133
134     $query = qq|INSERT INTO gl (reference, employee_id)
135                 VALUES ('$uid', (SELECT e.id FROM employee e
136                                  WHERE e.login = '$form->{login}'))|;
137     $dbh->do($query) || $form->dberror($query);
138     
139     $query = qq|SELECT g.id FROM gl g
140                 WHERE g.reference = '$uid'|;
141     $sth = $dbh->prepare($query);
142     $sth->execute || $form->dberror($query);
143
144     ($form->{id}) = $sth->fetchrow_array;
145     $sth->finish;
146
147   }
148   my ($null, $department_id) = split /--/, $form->{department};
149   $department_id *= 1;
150   
151   $query = qq|UPDATE gl SET 
152               reference = '$form->{reference}',
153               description = '$form->{description}',
154               notes = '$form->{notes}',
155               transdate = '$form->{transdate}',
156               department_id = $department_id,
157               taxincluded = '$form->{taxincluded}'
158               WHERE id = $form->{id}|;
159            
160   $dbh->do($query) || $form->dberror($query);
161   ($taxkey, $rate) = split(/--/, $form->{taxkey});  
162   # insert acc_trans transactions
163   foreach $i  ((credit,debit)) {
164     # extract accno
165     ($accno) = split(/--/, $form->{"${i}chartselected"});
166     my $amount = 0;
167     debug("$accno $i Kontonummer");
168     if ($i eq "credit") {
169       $amount = $credit;
170     }
171     if ($i eq "debit") {
172       $amount = $debit * -1;
173     }
174     
175     if ($form->{"${i}_splited"}) {
176       # if there is an amount, add the record
177         for $j (2 .. $form->{"${i}rowcount"}) {
178         ($accno) = split(/--/, $form->{"${i}chartselected_$j"});
179         
180         $amount = $form->{"${i}_$j"};
181         
182         ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$j"});
183         
184         if ($i eq "debit") {
185           $amount *=  -1;
186         }
187         if ($amount != 0) {
188           $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
189           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
190                       source, project_id, taxkey)
191                       VALUES
192                       ($form->{id}, (SELECT c.id
193                                     FROM chart c
194                                     WHERE c.accno = '$accno'),
195                       $amount, '$form->{transdate}', '$form->{reference}',
196                       $project_id, $taxkey)|;
197         
198           $dbh->do($query) || $form->dberror($query);
199         }
200         
201         $tax = $form->{"tax_$j"};
202         print(STDERR $tax, " Steuer bei Durchlauf $j\n\n");
203         if ($tax !=0) {
204               # add taxentry
205             if ($i eq "debit") {
206             $tax = $tax * (-1);
207             }
208             $amount = $tax;
209             
210             
211             $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
212             $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
213                     source, project_id, taxkey)
214                     VALUES
215                     ($form->{id}, (SELECT t.chart_id
216                     FROM tax t
217                     WHERE t.taxkey = $taxkey),
218                     $amount, '$form->{transdate}', '$form->{reference}',
219                             $project_id, $taxkey)|;
220             
221             $dbh->do($query) || $form->dberror($query);
222         }
223       }
224     } else {   
225         # if there is an amount, add the record
226         ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected"});
227         $taxkey *= 1;
228         debug("$amount auf $accno buchen");
229         if ($amount != 0) {
230           $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
231           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
232                       source, project_id, taxkey)
233                       VALUES
234                       ($form->{id}, (SELECT c.id
235                                     FROM chart c
236                                     WHERE c.accno = '$accno'),
237                       $amount, '$form->{transdate}', '$form->{reference}',
238                       $project_id, $taxkey)|;
239         
240           $dbh->do($query) || $form->dberror($query);
241         }
242       }
243     }
244     if ($tax !=0 && !($form->{credit_splited} || $form->{debit_splited})) {
245           # add taxentry
246           if ($form->{debittaxkey}) {
247           $tax = $tax * (-1);
248           }
249           $amount = $tax;
250           debug("$amount Steuern buchen");
251           
252           $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
253           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
254                   source, project_id, taxkey)
255                   VALUES
256                   ($form->{id}, (SELECT t.chart_id
257                   FROM tax t
258                   WHERE t.taxkey = $taxkey),
259                   $amount, '$form->{transdate}', '$form->{reference}',
260                           $project_id, $taxkey)|;
261           
262           $dbh->do($query) || $form->dberror($query);
263     }
264     
265   
266   # commit and redirect
267   my $rc = $dbh->commit;
268   $dbh->disconnect;
269
270   $main::lxdebug->leave_sub();
271
272   return $rc;
273 }
274
275
276
277 sub all_transactions {
278   $main::lxdebug->enter_sub();
279
280   my ($self, $myconfig, $form) = @_;
281
282   # connect to database
283   my $dbh = $form->dbconnect($myconfig);
284   my ($query, $sth, $source, $null);
285
286   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
287   
288   if ($form->{reference}) {
289     $source = $form->like(lc $form->{reference});
290     $glwhere .= " AND lower(g.reference) LIKE '$source'";
291     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
292     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
293   }
294   if ($form->{department}) {
295     ($null, $source) = split /--/, $form->{department};
296     $glwhere .= " AND g.department_id = $source";
297     $arwhere .= " AND a.department_id = $source";
298     $apwhere .= " AND a.department_id = $source";
299   }
300
301   if ($form->{source}) {
302     $source = $form->like(lc $form->{source});
303     $glwhere .= " AND lower(ac.source) LIKE '$source'";
304     $arwhere .= " AND lower(ac.source) LIKE '$source'";
305     $apwhere .= " AND lower(ac.source) LIKE '$source'";
306   }
307   if ($form->{datefrom}) {
308     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
309     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
310     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
311   }
312   if ($form->{dateto}) {
313     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
314     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
315     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
316   }
317   if ($form->{description}) {
318     my $description = $form->like(lc $form->{description});
319     $glwhere .= " AND lower(g.description) LIKE '$description'";
320     $arwhere .= " AND lower(ct.name) LIKE '$description'";
321     $apwhere .= " AND lower(ct.name) LIKE '$description'";
322   }
323   if ($form->{notes}) {
324     my $notes = $form->like(lc $form->{notes});
325     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
326     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
327     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
328   }
329   if ($form->{accno}) {
330     $glwhere .= " AND c.accno = '$form->{accno}'";
331     $arwhere .= " AND c.accno = '$form->{accno}'";
332     $apwhere .= " AND c.accno = '$form->{accno}'";
333   }
334   if ($form->{gifi_accno}) {
335     $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
336     $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
337     $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
338   }
339   if ($form->{category} ne 'X') {
340     $glwhere .= " AND c.category = '$form->{category}'";
341     $arwhere .= " AND c.category = '$form->{category}'";
342     $apwhere .= " AND c.category = '$form->{category}'";
343   }
344
345   if ($form->{accno}) {
346     # get category for account
347     $query = qq|SELECT c.category
348                 FROM chart c
349                 WHERE c.accno = '$form->{accno}'|;
350     $sth = $dbh->prepare($query); 
351
352     $sth->execute || $form->dberror($query); 
353     ($form->{ml}) = $sth->fetchrow_array; 
354     $sth->finish; 
355     
356     if ($form->{datefrom}) {
357       $query = qq|SELECT SUM(ac.amount)
358                   FROM acc_trans ac, chart c
359                   WHERE ac.chart_id = c.id
360                   AND c.accno = '$form->{accno}'
361                   AND ac.transdate < date '$form->{datefrom}'
362                   |;
363       $sth = $dbh->prepare($query);
364       $sth->execute || $form->dberror($query);
365
366       ($form->{balance}) = $sth->fetchrow_array;
367       $sth->finish;
368     }
369   }
370   
371   if ($form->{gifi_accno}) {
372     # get category for account
373     $query = qq|SELECT c.category
374                 FROM chart c
375                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
376     $sth = $dbh->prepare($query); 
377
378     $sth->execute || $form->dberror($query); 
379     ($form->{ml}) = $sth->fetchrow_array; 
380     $sth->finish; 
381    
382     if ($form->{datefrom}) {
383       $query = qq|SELECT SUM(ac.amount)
384                   FROM acc_trans ac, chart c
385                   WHERE ac.chart_id = c.id
386                   AND c.gifi_accno = '$form->{gifi_accno}'
387                   AND ac.transdate < date '$form->{datefrom}'
388                   |;
389       $sth = $dbh->prepare($query);
390       $sth->execute || $form->dberror($query);
391
392       ($form->{balance}) = $sth->fetchrow_array;
393       $sth->finish;
394     }
395   }
396
397   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
398
399   
400   my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
401                  g.description, ac.transdate, ac.source, ac.trans_id,
402                  ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
403                  FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
404                  (t.chart_id=c.id)
405                  WHERE $glwhere
406                  AND ac.chart_id = c.id
407                  AND g.id = ac.trans_id
408         UNION
409                  SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
410                  ct.name, ac.transdate, ac.source, ac.trans_id,
411                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
412                  FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
413                  (t.chart_id=c.id)
414                  WHERE $arwhere
415                  AND ac.chart_id = c.id
416                  AND a.customer_id = ct.id
417                  AND a.id = ac.trans_id
418         UNION
419                  SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
420                  ct.name, ac.transdate, ac.source, ac.trans_id,
421                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
422                  FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
423                  (t.chart_id=c.id)
424                  WHERE $apwhere
425                  AND ac.chart_id = c.id
426                  AND a.vendor_id = ct.id
427                  AND a.id = ac.trans_id
428                  ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
429   my $sth = $dbh->prepare($query);
430   $sth->execute || $form->dberror($query);
431   
432   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
433     print(STDERR $ref->{id}, " Transaction\n");
434     # gl
435     if ($ref->{type} eq "gl") {
436       $ref->{module} = "gl";
437     }
438
439     # ap
440     if ($ref->{type} eq "ap") {
441       if ($ref->{invoice}) {
442         $ref->{module} = "ir";
443       } else {
444         $ref->{module} = "ap";
445       }
446     }
447
448     # ar
449     if ($ref->{type} eq "ar") {
450       if ($ref->{invoice}) {
451         $ref->{module} = "is";
452       } else {
453         $ref->{module} = "ar";
454       }
455     }
456     $balance=$ref->{amount};
457     $i = 0;
458     $j = 0;
459     $k = 0;
460     $l = 0;
461     if ($ref->{amount} < 0) {
462       if ($ref->{chart_id} >0) {
463         $ref->{debit_tax}{$i} = $ref->{amount} * -1;
464         $ref->{debit_tax_accno}{$i} = $ref->{accno};
465         }
466       else {
467         $ref->{debit}{$k} = $ref->{amount} * -1;
468         $ref->{debit_accno}{$k} = $ref->{accno};
469         $ref->{debit_taxkey}{$k} = $ref->{taxkey};
470         }
471     } else {
472       if ($ref->{chart_id} >0) {
473         $ref->{credit_tax}{$j} = $ref->{amount};
474         $ref->{credit_tax_accno}{$j} = $ref->{accno};
475         }
476       else {
477         $ref->{credit}{$l} = $ref->{amount};
478         $ref->{credit_accno}{$l} = $ref->{accno};
479         $ref->{credit_taxkey}{$l} = $ref->{taxkey};
480         }
481     }
482
483     while (abs($balance)>=0.015) {
484       my $ref2 = $sth->fetchrow_hashref(NAME_lc) || $form->error("Unbalanced ledger!");
485
486       $balance = (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
487       print(STDERR $balance," BAlance\n");
488       if ($ref2->{amount} < 0) {
489         if ($ref2->{chart_id} >0) {
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 ($ref->{debit_accno}{$k} ne "") {
498             $k++;
499           }
500           $ref->{debit}{$k} = $ref2->{amount} * -1;
501           $ref->{debit_accno}{$k} = $ref2->{accno};
502           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
503           }
504       } else {
505         if ($ref2->{chart_id} >0) {
506           if ($ref->{credit_tax_accno}{$j} ne "") {
507             $j++;
508           }
509           $ref->{credit_tax}{$j} = $ref2->{amount};
510           $ref->{credit_tax_accno}{$j} = $ref2->{accno};
511           }
512         else {
513           if ($ref->{credit_accno}{$l} ne "") {
514             $l++;
515           }
516           $ref->{credit}{$l} = $ref2->{amount};
517           $ref->{credit_accno}{$l} = $ref2->{accno};
518           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
519           }
520       }
521     }
522 #    print(STDERR Dumper($ref));       
523     push @{ $form->{GL} }, $ref;
524     $balance=0;
525   }
526   $sth->finish;
527
528   if ($form->{accno}) {
529     $query = qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
530     $sth = $dbh->prepare($query);
531     $sth->execute || $form->dberror($query);
532
533     ($form->{account_description}) = $sth->fetchrow_array;
534     $sth->finish;
535   }
536   if ($form->{gifi_accno}) {
537     $query = qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
538     $sth = $dbh->prepare($query);
539     $sth->execute || $form->dberror($query);
540
541     ($form->{gifi_account_description}) = $sth->fetchrow_array;
542     $sth->finish;
543   }
544  
545   $dbh->disconnect;
546
547   $main::lxdebug->leave_sub();
548 }
549
550
551 sub transaction {
552   $main::lxdebug->enter_sub();
553
554   my ($self, $myconfig, $form) = @_;
555   
556   my ($query, $sth, $ref);
557   
558   # connect to database
559   my $dbh = $form->dbconnect($myconfig);
560   $form->{creditrowcount} = 1;
561   $form->{debitrowcount} = 1;
562   if ($form->{id}) {
563     $query = "SELECT closedto, revtrans
564               FROM defaults";
565     $sth = $dbh->prepare($query);
566     $sth->execute || $form->dberror($query);
567
568     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
569     $sth->finish;
570
571     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
572               d.description AS department, e.name as employee, g.taxincluded, g.gldate
573               FROM gl g
574             LEFT JOIN department d ON (d.id = g.department_id)  
575             LEFT JOIN employee e ON (e.id = g.employee_id)  
576             WHERE g.id = $form->{id}";
577     $sth = $dbh->prepare($query);
578     $sth->execute || $form->dberror($query);
579     $ref = $sth->fetchrow_hashref(NAME_lc);
580     map { $form->{$_} = $ref->{$_} } keys %$ref;
581     $sth->finish;
582   
583     # retrieve individual rows
584     $query = "SELECT c.accno, a.amount, project_id,
585                 (SELECT p.projectnumber FROM project p
586                  WHERE a.project_id = p.id) AS projectnumber, a.taxkey, (SELECT c1.accno FROM chart c1, tax t WHERE t.taxkey=a.taxkey AND c1.id=t.chart_id) AS taxaccno
587               FROM acc_trans a, chart c
588               WHERE a.chart_id = c.id
589               AND a.trans_id = $form->{id}
590               ORDER BY accno";
591     $sth = $dbh->prepare($query);
592     $sth->execute || $form->dberror($query);
593     
594     $debitcount=2;
595     $creditcount=2;
596     $taxcount=2;
597     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
598
599       if ($ref->{accno} eq $ref->{taxaccno}) {
600         $form->{"tax_$taxcount"}= $ref->{amount};
601         $form->{"tax"} += $ref->{amount};
602         $form->{"taxchartselected_$taxcount"} = $ref->{taxkey};
603         $taxcount++;
604       } else {
605       
606                 if ($ref->{amount} < 0) {
607                         $form->{"debit_$debitcount"} = $ref->{amount} * -1;
608                         $form->{"debit"} += $ref->{amount} * -1;
609                         $form->{"debitchartselected_$debitcount"} = $ref->{accno};
610                         $debitcount++;
611                 }
612                 if ($ref->{amount} > 0) {
613                         
614                         $form->{"credit_$creditcount"} = $ref->{amount};
615                         $form->{"credit"} += $ref->{amount};
616                         $form->{"creditchartselected_$creditcount"} = $ref->{accno};
617                         $creditcount++;
618                 }
619         }
620       
621       $taxkey = $ref->{taxkey} *1;
622     }
623     if ($creditcount > 3) {
624       $form->{credit_splited} = 1;
625       $form->{credit} = $form->{credit} + $form->{tax};
626       $form->{creditrowcount} = $creditcount - 1;
627     } else {
628       $form->{credit} = $form->{credit_2};
629       $form->{amount} = $form->{amount_2};
630       $form->{creditaccno} = $form->{creditchartselected_2};
631     }
632     if ($debitcount > 3) {
633       $form->{debit_splited} = 1;
634       $form->{debit} = $form->{debit} + $form->{tax};
635       $form->{debitrowcount} = $debitcount - 1;
636     } else {
637       $form->{debit} = $form->{debit_2};
638       $form->{debitaccno} = $form->{debitchartselected_2};
639     }
640     
641     if ((($form->{credit} > $form->{debit}) && (!$form->{taxincluded})) || (($form->{credit} > $form->{debit}) && ($form->{taxincluded}))) {
642         $form->{amount} = $form->{debit};
643     } else {
644         $form->{amount} = $form->{credit};
645     }
646     
647       # get tax description
648     $query = qq| SELECT * FROM tax t|;
649     $sth = $dbh->prepare($query);
650     $sth->execute || $form->dberror($query);  
651     $form->{TAX} = ();
652     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
653       push @{ $form->{TAX} }, $ref;
654     }
655   
656     $sth->finish;
657   } else {
658     $query = "SELECT current_date AS transdate, closedto, revtrans
659               FROM defaults";
660     $sth = $dbh->prepare($query);
661     $sth->execute || $form->dberror($query);
662
663     ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
664     
665       # get tax description
666     $query = qq| SELECT * FROM tax t order by t.taxkey|;
667     $sth = $dbh->prepare($query);
668     $sth->execute || $form->dberror($query);  
669     $form->{TAX} = ();
670     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
671       push @{ $form->{TAX} }, $ref;
672     }
673   }
674
675   $sth->finish;
676
677   # get chart of accounts
678   $query = qq|SELECT c.accno, c.description, c.taxkey_id
679               FROM chart c
680               WHERE c.charttype = 'A'
681               ORDER by c.accno|;
682   $sth = $dbh->prepare($query);
683   $sth->execute || $form->dberror($query);
684   $form->{chart} = ();
685   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
686     push @{ $form->{chart} }, $ref;
687   }
688   $sth->finish;
689   
690
691   
692   $sth->finish;
693   
694   $dbh->disconnect;
695
696   $main::lxdebug->leave_sub();
697 }
698
699 sub debug {
700   local *OUT;
701   if (open(OUT, ">>/tmp/linet.log")) {
702 #    chomp(@_);
703     print(OUT join("\n", @_), "\n");
704     close(OUT);
705   } else {
706     print(STDERR "noe: $!\n");
707   }
708 }
709
710 1;
711