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