Whitespace an den Zeilenenden entfernt.
[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 sub post_transaction {
67   $main::lxdebug->enter_sub();
68
69   my ($self, $myconfig, $form) = @_;
70
71   my ($debit, $credit) = (0, 0);
72   my $project_id;
73
74   my $i;
75
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))
83       || (($debit < $credit) && (abs(($debit + $tax) - $credit) > 4))) {
84     return -2;
85   }
86
87   if (($debit + $credit + $tax) == 0) {
88     return -3;
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
126     # delete individual transactions
127     $query = qq|DELETE FROM acc_trans
128                 WHERE trans_id = $form->{id}|;
129     $dbh->do($query) || $form->dberror($query);
130
131   } else {
132     my $uid = time;
133     $uid .= $form->{login};
134
135     $query = qq|INSERT INTO gl (reference, employee_id)
136                 VALUES ('$uid', (SELECT e.id FROM employee e
137                                  WHERE e.login = '$form->{login}'))|;
138     $dbh->do($query) || $form->dberror($query);
139
140     $query = qq|SELECT g.id FROM gl g
141                 WHERE g.reference = '$uid'|;
142     $sth = $dbh->prepare($query);
143     $sth->execute || $form->dberror($query);
144
145     ($form->{id}) = $sth->fetchrow_array;
146     $sth->finish;
147
148   }
149   my ($null, $department_id) = split /--/, $form->{department};
150   $department_id *= 1;
151
152   $query = qq|UPDATE gl SET
153               reference = '$form->{reference}',
154               description = '$form->{description}',
155               notes = '$form->{notes}',
156               transdate = '$form->{transdate}',
157               department_id = $department_id,
158               taxincluded = '$form->{taxincluded}'
159               WHERE id = $form->{id}|;
160
161   $dbh->do($query) || $form->dberror($query);
162   ($taxkey, $rate) = split(/--/, $form->{taxkey});
163
164   # insert acc_trans transactions
165   foreach $i ((credit, debit)) {
166
167     # extract accno
168     ($accno) = split(/--/, $form->{"${i}chartselected"});
169     my $amount = 0;
170     debug("$accno $i Kontonummer");
171     if ($i eq "credit") {
172       $amount = $credit;
173     }
174     if ($i eq "debit") {
175       $amount = $debit * -1;
176     }
177
178     if ($form->{"${i}_splited"}) {
179
180       # if there is an amount, add the record
181       for $j (2 .. $form->{"${i}rowcount"}) {
182         ($accno) = split(/--/, $form->{"${i}chartselected_$j"});
183
184         $amount = $form->{"${i}_$j"};
185
186         ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$j"});
187
188         if ($i eq "debit") {
189           $amount *= -1;
190         }
191         if ($amount != 0) {
192           $project_id =
193             ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
194           $query =
195             qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
196                       source, project_id, taxkey)
197                       VALUES
198                       ($form->{id}, (SELECT c.id
199                                     FROM chart c
200                                     WHERE c.accno = '$accno'),
201                       $amount, '$form->{transdate}', '$form->{reference}',
202                       $project_id, $taxkey)|;
203
204           $dbh->do($query) || $form->dberror($query);
205         }
206
207         $tax = $form->{"tax_$j"};
208         print(STDERR $tax, " Steuer bei Durchlauf $j\n\n");
209         if ($tax != 0) {
210
211           # add taxentry
212           if ($i eq "debit") {
213             $tax = $tax * (-1);
214           }
215           $amount = $tax;
216
217           $project_id =
218             ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
219           $query =
220             qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
221                     source, project_id, taxkey)
222                     VALUES
223                     ($form->{id}, (SELECT t.chart_id
224                     FROM tax t
225                     WHERE t.taxkey = $taxkey),
226                     $amount, '$form->{transdate}', '$form->{reference}',
227                             $project_id, $taxkey)|;
228
229           $dbh->do($query) || $form->dberror($query);
230         }
231       }
232     } else {
233
234       # if there is an amount, add the record
235       ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected"});
236       $taxkey *= 1;
237       debug("$amount auf $accno buchen");
238       if ($amount != 0) {
239         $project_id =
240           ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
241         $query =
242           qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
243                       source, project_id, taxkey)
244                       VALUES
245                       ($form->{id}, (SELECT c.id
246                                     FROM chart c
247                                     WHERE c.accno = '$accno'),
248                       $amount, '$form->{transdate}', '$form->{reference}',
249                       $project_id, $taxkey)|;
250
251         $dbh->do($query) || $form->dberror($query);
252       }
253     }
254   }
255   if ($tax != 0 && !($form->{credit_splited} || $form->{debit_splited})) {
256
257     # add taxentry
258     if ($form->{debittaxkey}) {
259       $tax = $tax * (-1);
260     }
261     $amount = $tax;
262     debug("$amount Steuern buchen");
263
264     $project_id =
265       ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
266     $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
267                   source, project_id, taxkey)
268                   VALUES
269                   ($form->{id}, (SELECT t.chart_id
270                   FROM tax t
271                   WHERE t.taxkey = $taxkey),
272                   $amount, '$form->{transdate}', '$form->{reference}',
273                           $project_id, $taxkey)|;
274
275     $dbh->do($query) || $form->dberror($query);
276   }
277
278   # commit and redirect
279   my $rc = $dbh->commit;
280   $dbh->disconnect;
281
282   $main::lxdebug->leave_sub();
283
284   return $rc;
285 }
286
287 sub all_transactions {
288   $main::lxdebug->enter_sub();
289
290   my ($self, $myconfig, $form) = @_;
291
292   # connect to database
293   my $dbh = $form->dbconnect($myconfig);
294   my ($query, $sth, $source, $null);
295
296   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
297
298   if ($form->{reference}) {
299     $source = $form->like(lc $form->{reference});
300     $glwhere .= " AND lower(g.reference) LIKE '$source'";
301     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
302     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
303   }
304   if ($form->{department}) {
305     ($null, $source) = split /--/, $form->{department};
306     $glwhere .= " AND g.department_id = $source";
307     $arwhere .= " AND a.department_id = $source";
308     $apwhere .= " AND a.department_id = $source";
309   }
310
311   if ($form->{source}) {
312     $source = $form->like(lc $form->{source});
313     $glwhere .= " AND lower(ac.source) LIKE '$source'";
314     $arwhere .= " AND lower(ac.source) LIKE '$source'";
315     $apwhere .= " AND lower(ac.source) LIKE '$source'";
316   }
317   if ($form->{datefrom}) {
318     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
319     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
320     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
321   }
322   if ($form->{dateto}) {
323     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
324     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
325     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
326   }
327   if ($form->{description}) {
328     my $description = $form->like(lc $form->{description});
329     $glwhere .= " AND lower(g.description) LIKE '$description'";
330     $arwhere .= " AND lower(ct.name) LIKE '$description'";
331     $apwhere .= " AND lower(ct.name) LIKE '$description'";
332   }
333   if ($form->{notes}) {
334     my $notes = $form->like(lc $form->{notes});
335     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
336     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
337     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
338   }
339   if ($form->{accno}) {
340     $glwhere .= " AND c.accno = '$form->{accno}'";
341     $arwhere .= " AND c.accno = '$form->{accno}'";
342     $apwhere .= " AND c.accno = '$form->{accno}'";
343   }
344   if ($form->{gifi_accno}) {
345     $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
346     $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
347     $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
348   }
349   if ($form->{category} ne 'X') {
350     $glwhere .= " AND c.category = '$form->{category}'";
351     $arwhere .= " AND c.category = '$form->{category}'";
352     $apwhere .= " AND c.category = '$form->{category}'";
353   }
354
355   if ($form->{accno}) {
356
357     # get category for account
358     $query = qq|SELECT c.category
359                 FROM chart c
360                 WHERE c.accno = '$form->{accno}'|;
361     $sth = $dbh->prepare($query);
362
363     $sth->execute || $form->dberror($query);
364     ($form->{ml}) = $sth->fetchrow_array;
365     $sth->finish;
366
367     if ($form->{datefrom}) {
368       $query = qq|SELECT SUM(ac.amount)
369                   FROM acc_trans ac, chart c
370                   WHERE ac.chart_id = c.id
371                   AND c.accno = '$form->{accno}'
372                   AND ac.transdate < date '$form->{datefrom}'
373                   |;
374       $sth = $dbh->prepare($query);
375       $sth->execute || $form->dberror($query);
376
377       ($form->{balance}) = $sth->fetchrow_array;
378       $sth->finish;
379     }
380   }
381
382   if ($form->{gifi_accno}) {
383
384     # get category for account
385     $query = qq|SELECT c.category
386                 FROM chart c
387                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
388     $sth = $dbh->prepare($query);
389
390     $sth->execute || $form->dberror($query);
391     ($form->{ml}) = $sth->fetchrow_array;
392     $sth->finish;
393
394     if ($form->{datefrom}) {
395       $query = qq|SELECT SUM(ac.amount)
396                   FROM acc_trans ac, chart c
397                   WHERE ac.chart_id = c.id
398                   AND c.gifi_accno = '$form->{gifi_accno}'
399                   AND ac.transdate < date '$form->{datefrom}'
400                   |;
401       $sth = $dbh->prepare($query);
402       $sth->execute || $form->dberror($query);
403
404       ($form->{balance}) = $sth->fetchrow_array;
405       $sth->finish;
406     }
407   }
408
409   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
410
411   my $query =
412     qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
413                  g.description, ac.transdate, ac.source, ac.trans_id,
414                  ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
415                  FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
416                  (t.chart_id=c.id)
417                  WHERE $glwhere
418                  AND ac.chart_id = c.id
419                  AND g.id = ac.trans_id
420         UNION
421                  SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
422                  ct.name, ac.transdate, ac.source, ac.trans_id,
423                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
424                  FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
425                  (t.chart_id=c.id)
426                  WHERE $arwhere
427                  AND ac.chart_id = c.id
428                  AND a.customer_id = ct.id
429                  AND a.id = ac.trans_id
430         UNION
431                  SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
432                  ct.name, ac.transdate, ac.source, ac.trans_id,
433                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
434                  FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
435                  (t.chart_id=c.id)
436                  WHERE $apwhere
437                  AND ac.chart_id = c.id
438                  AND a.vendor_id = ct.id
439                  AND a.id = ac.trans_id
440                  ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
441   my $sth = $dbh->prepare($query);
442   $sth->execute || $form->dberror($query);
443
444   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
445     print(STDERR $ref->{id}, " Transaction\n");
446
447     # gl
448     if ($ref->{type} eq "gl") {
449       $ref->{module} = "gl";
450     }
451
452     # ap
453     if ($ref->{type} eq "ap") {
454       if ($ref->{invoice}) {
455         $ref->{module} = "ir";
456       } else {
457         $ref->{module} = "ap";
458       }
459     }
460
461     # ar
462     if ($ref->{type} eq "ar") {
463       if ($ref->{invoice}) {
464         $ref->{module} = "is";
465       } else {
466         $ref->{module} = "ar";
467       }
468     }
469     $balance = $ref->{amount};
470     $i       = 0;
471     $j       = 0;
472     $k       = 0;
473     $l       = 0;
474     if ($ref->{amount} < 0) {
475       if ($ref->{chart_id} > 0) {
476         $ref->{debit_tax}{$i}       = $ref->{amount} * -1;
477         $ref->{debit_tax_accno}{$i} = $ref->{accno};
478       } else {
479         $ref->{debit}{$k}        = $ref->{amount} * -1;
480         $ref->{debit_accno}{$k}  = $ref->{accno};
481         $ref->{debit_taxkey}{$k} = $ref->{taxkey};
482       }
483     } else {
484       if ($ref->{chart_id} > 0) {
485         $ref->{credit_tax}{$j}       = $ref->{amount};
486         $ref->{credit_tax_accno}{$j} = $ref->{accno};
487       } else {
488         $ref->{credit}{$l}        = $ref->{amount};
489         $ref->{credit_accno}{$l}  = $ref->{accno};
490         $ref->{credit_taxkey}{$l} = $ref->{taxkey};
491       }
492     }
493
494     while (abs($balance) >= 0.015) {
495       my $ref2 = $sth->fetchrow_hashref(NAME_lc)
496         || $form->error("Unbalanced ledger!");
497
498       $balance =
499         (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
500       print(STDERR $balance, " BAlance\n");
501       if ($ref2->{amount} < 0) {
502         if ($ref2->{chart_id} > 0) {
503           if ($ref->{debit_tax_accno}{$i} ne "") {
504             $i++;
505           }
506           $ref->{debit_tax}{$i}       = $ref2->{amount} * -1;
507           $ref->{debit_tax_accno}{$i} = $ref2->{accno};
508         } else {
509           if ($ref->{debit_accno}{$k} ne "") {
510             $k++;
511           }
512           $ref->{debit}{$k}        = $ref2->{amount} * -1;
513           $ref->{debit_accno}{$k}  = $ref2->{accno};
514           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
515         }
516       } else {
517         if ($ref2->{chart_id} > 0) {
518           if ($ref->{credit_tax_accno}{$j} ne "") {
519             $j++;
520           }
521           $ref->{credit_tax}{$j}       = $ref2->{amount};
522           $ref->{credit_tax_accno}{$j} = $ref2->{accno};
523         } else {
524           if ($ref->{credit_accno}{$l} ne "") {
525             $l++;
526           }
527           $ref->{credit}{$l}        = $ref2->{amount};
528           $ref->{credit_accno}{$l}  = $ref2->{accno};
529           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
530         }
531       }
532     }
533
534     #    print(STDERR Dumper($ref));
535     push @{ $form->{GL} }, $ref;
536     $balance = 0;
537   }
538   $sth->finish;
539
540   if ($form->{accno}) {
541     $query =
542       qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
543     $sth = $dbh->prepare($query);
544     $sth->execute || $form->dberror($query);
545
546     ($form->{account_description}) = $sth->fetchrow_array;
547     $sth->finish;
548   }
549   if ($form->{gifi_accno}) {
550     $query =
551       qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
552     $sth = $dbh->prepare($query);
553     $sth->execute || $form->dberror($query);
554
555     ($form->{gifi_account_description}) = $sth->fetchrow_array;
556     $sth->finish;
557   }
558
559   $dbh->disconnect;
560
561   $main::lxdebug->leave_sub();
562 }
563
564 sub transaction {
565   $main::lxdebug->enter_sub();
566
567   my ($self, $myconfig, $form) = @_;
568
569   my ($query, $sth, $ref);
570
571   # connect to database
572   my $dbh = $form->dbconnect($myconfig);
573   $form->{creditrowcount} = 1;
574   $form->{debitrowcount}  = 1;
575   if ($form->{id}) {
576     $query = "SELECT closedto, revtrans
577               FROM defaults";
578     $sth = $dbh->prepare($query);
579     $sth->execute || $form->dberror($query);
580
581     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
582     $sth->finish;
583
584     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
585               d.description AS department, e.name as employee, g.taxincluded, g.gldate
586               FROM gl g
587             LEFT JOIN department d ON (d.id = g.department_id)
588             LEFT JOIN employee e ON (e.id = g.employee_id)
589             WHERE g.id = $form->{id}";
590     $sth = $dbh->prepare($query);
591     $sth->execute || $form->dberror($query);
592     $ref = $sth->fetchrow_hashref(NAME_lc);
593     map { $form->{$_} = $ref->{$_} } keys %$ref;
594     $sth->finish;
595
596     # retrieve individual rows
597     $query = "SELECT c.accno, a.amount, project_id,
598                 (SELECT p.projectnumber FROM project p
599                  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
600               FROM acc_trans a, chart c
601               WHERE a.chart_id = c.id
602               AND a.trans_id = $form->{id}
603               ORDER BY accno";
604     $sth = $dbh->prepare($query);
605     $sth->execute || $form->dberror($query);
606
607     $debitcount  = 2;
608     $creditcount = 2;
609     $taxcount    = 2;
610     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
611
612       if ($ref->{accno} eq $ref->{taxaccno}) {
613         $form->{"tax_$taxcount"} = $ref->{amount};
614         $form->{"tax"} += $ref->{amount};
615         $form->{"taxchartselected_$taxcount"} = $ref->{taxkey};
616         $taxcount++;
617       } else {
618
619         if ($ref->{amount} < 0) {
620           $form->{"debit_$debitcount"} = $ref->{amount} * -1;
621           $form->{"debit"} += $ref->{amount} * -1;
622           $form->{"debitchartselected_$debitcount"} = $ref->{accno};
623           $debitcount++;
624         }
625         if ($ref->{amount} > 0) {
626
627           $form->{"credit_$creditcount"} = $ref->{amount};
628           $form->{"credit"} += $ref->{amount};
629           $form->{"creditchartselected_$creditcount"} = $ref->{accno};
630           $creditcount++;
631         }
632       }
633
634       $taxkey = $ref->{taxkey} * 1;
635     }
636     if ($creditcount > 3) {
637       $form->{credit_splited} = 1;
638       $form->{credit}         = $form->{credit} + $form->{tax};
639       $form->{creditrowcount} = $creditcount - 1;
640     } else {
641       $form->{credit}      = $form->{credit_2};
642       $form->{amount}      = $form->{amount_2};
643       $form->{creditaccno} = $form->{creditchartselected_2};
644     }
645     if ($debitcount > 3) {
646       $form->{debit_splited} = 1;
647       $form->{debit}         = $form->{debit} + $form->{tax};
648       $form->{debitrowcount} = $debitcount - 1;
649     } else {
650       $form->{debit}      = $form->{debit_2};
651       $form->{debitaccno} = $form->{debitchartselected_2};
652     }
653
654     if (   (($form->{credit} > $form->{debit}) && (!$form->{taxincluded}))
655         || (($form->{credit} > $form->{debit}) && ($form->{taxincluded}))) {
656       $form->{amount} = $form->{debit};
657         } else {
658       $form->{amount} = $form->{credit};
659     }
660
661     # get tax description
662     $query = qq| SELECT * FROM tax t|;
663     $sth   = $dbh->prepare($query);
664     $sth->execute || $form->dberror($query);
665     $form->{TAX} = ();
666     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
667       push @{ $form->{TAX} }, $ref;
668     }
669
670     $sth->finish;
671   } else {
672     $query = "SELECT current_date AS transdate, closedto, revtrans
673               FROM defaults";
674     $sth = $dbh->prepare($query);
675     $sth->execute || $form->dberror($query);
676
677     ($form->{transdate}, $form->{closedto}, $form->{revtrans}) =
678       $sth->fetchrow_array;
679
680     # get tax description
681     $query = qq| SELECT * FROM tax t order by t.taxkey|;
682     $sth   = $dbh->prepare($query);
683     $sth->execute || $form->dberror($query);
684     $form->{TAX} = ();
685     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
686       push @{ $form->{TAX} }, $ref;
687     }
688   }
689
690   $sth->finish;
691
692   # get chart of accounts
693   $query = qq|SELECT c.accno, c.description, c.taxkey_id
694               FROM chart c
695               WHERE c.charttype = 'A'
696               ORDER by c.accno|;
697   $sth = $dbh->prepare($query);
698   $sth->execute || $form->dberror($query);
699   $form->{chart} = ();
700   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
701     push @{ $form->{chart} }, $ref;
702   }
703   $sth->finish;
704
705   $sth->finish;
706
707   $dbh->disconnect;
708
709   $main::lxdebug->leave_sub();
710 }
711
712 sub debug {
713   local *OUT;
714   if (open(OUT, ">>/tmp/linet.log")) {
715
716     #    chomp(@_);
717     print(OUT join("\n", @_), "\n");
718     close(OUT);
719   } else {
720     print(STDERR "noe: $!\n");
721   }
722 }
723
724 1;