Buchungsjournal um Anzeige von Splitbuchungen erweitert, Splitbuchungen beim Dialogbu...
[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 sub delete_transaction {
42   my ($self, $myconfig, $form) = @_;
43   
44   # connect to database
45   my $dbh = $form->dbconnect_noauto($myconfig);
46
47   my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
48   $dbh->do($query) || $form->dberror($query);
49
50   $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
51   $dbh->do($query) || $form->dberror($query);
52
53   # commit and redirect
54   my $rc = $dbh->commit;
55   $dbh->disconnect;
56   
57   $rc;
58   
59 }
60
61
62 sub post_transaction {
63   my ($self, $myconfig, $form) = @_;
64   
65   my ($debit, $credit) = (0, 0);
66   my $project_id;
67
68   my $i;
69   # check if debit and credit balances
70   
71   if ($form->{storno}) {
72         $debit = $debit * -1;
73         $credit = $credit * -1;
74         $tax = $tax * -1;
75         $form->{reference} = "Storno-".$form->{reference};
76         $form->{description} = "Storno-".$form->{description};
77   }  
78
79   # connect to database, turn off AutoCommit
80   my $dbh = $form->dbconnect_noauto($myconfig);
81
82   # post the transaction
83   # make up a unique handle and store in reference field
84   # then retrieve the record based on the unique handle to get the id
85   # replace the reference field with the actual variable
86   # add records to acc_trans
87
88   # if there is a $form->{id} replace the old transaction
89   # delete all acc_trans entries and add the new ones
90
91   # escape '
92   map { $form->{$_} =~ s/\'/\'\'/g } qw(reference description notes);
93
94   if (!$form->{taxincluded}) {
95     $form->{taxincluded} = 0;
96   }
97   
98   my ($query, $sth);
99   
100   if ($form->{id}) {
101     # delete individual transactions
102     $query = qq|DELETE FROM acc_trans 
103                 WHERE trans_id = $form->{id}|;
104     $dbh->do($query) || $form->dberror($query);
105     
106   } else {
107     my $uid = time;
108     $uid .= $form->{login};
109
110     $query = qq|INSERT INTO gl (reference, employee_id)
111                 VALUES ('$uid', (SELECT e.id FROM employee e
112                                  WHERE e.login = '$form->{login}'))|;
113     $dbh->do($query) || $form->dberror($query);
114     
115     $query = qq|SELECT g.id FROM gl g
116                 WHERE g.reference = '$uid'|;
117     $sth = $dbh->prepare($query);
118     $sth->execute || $form->dberror($query);
119
120     ($form->{id}) = $sth->fetchrow_array;
121     $sth->finish;
122
123   }
124   
125   my ($null, $department_id) = split /--/, $form->{department};
126   $department_id *= 1;
127   
128   $query = qq|UPDATE gl SET 
129               reference = '$form->{reference}',
130               description = '$form->{description}',
131               notes = '$form->{notes}',
132               transdate = '$form->{transdate}',
133               department_id = $department_id,
134               taxincluded = '$form->{taxincluded}'
135               WHERE id = $form->{id}|;
136            
137   $dbh->do($query) || $form->dberror($query);
138   ($taxkey, $rate) = split(/--/, $form->{taxkey});
139
140
141   # insert acc_trans transactions
142   for $i (1 .. $form->{rowcount}) {
143     # extract accno
144     my ($accno) = split(/--/, $form->{"accno_$i"});
145     my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
146     my $amount = 0;
147     my $debit = $form->{"debit_$i"};
148     my $credit = $form->{"credit_$i"};
149     my $tax = $form->{"tax_$i"};
150     
151     if ($credit) {
152       $amount = $credit;
153       $posted = 0;
154     }
155     if ($debit) {
156       $amount = $debit * -1;
157       $tax = $tax * -1;
158       $posted = 0;
159     }
160
161
162     # if there is an amount, add the record
163     if ($amount != 0) {
164       $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
165       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
166                   source, memo, project_id, taxkey)
167                   VALUES
168                   ($form->{id}, (SELECT c.id
169                                  FROM chart c
170                                  WHERE c.accno = '$accno'),
171                    $amount, '$form->{transdate}', |.
172                    $dbh->quote($form->{"source_$i"}) .qq|, |.
173                   $dbh->quote($form->{"memo_$i"}).qq|,
174                   $project_id, $taxkey)|;
175     
176       $dbh->do($query) || $form->dberror($query);
177     }
178   
179     if ($tax !=0) {
180           # add taxentry
181           $amount = $tax;
182           
183           
184           $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL'; 
185           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
186                   source, memo, project_id, taxkey)
187                   VALUES
188                   ($form->{id}, (SELECT t.chart_id
189                   FROM tax t
190                   WHERE t.taxkey = $taxkey),
191                   $amount, '$form->{transdate}', |.
192                    $dbh->quote($form->{"source_$i"}) .qq|, |.
193                   $dbh->quote($form->{"memo_$i"}).qq|,
194                           $project_id, $taxkey)|;
195           
196           $dbh->do($query) || $form->dberror($query);
197     }
198   }
199
200   my %audittrail = ( tablename  => 'gl',
201                      reference  => $form->{reference},
202                      formname   => 'transaction',
203                      action     => 'posted',
204                      id         => $form->{id} );
205  
206   # $form->audittrail($dbh, "", \%audittrail);
207
208   # commit and redirect
209   my $rc = $dbh->commit;
210   $dbh->disconnect;
211
212   $rc;
213
214 }
215
216
217
218 sub all_transactions {
219   my ($self, $myconfig, $form) = @_;
220
221   # connect to database
222   my $dbh = $form->dbconnect($myconfig);
223   my ($query, $sth, $source, $null);
224
225   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
226   
227   if ($form->{reference}) {
228     $source = $form->like(lc $form->{reference});
229     $glwhere .= " AND lower(g.reference) LIKE '$source'";
230     $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
231     $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
232   }
233   if ($form->{department}) {
234     ($null, $source) = split /--/, $form->{department};
235     $glwhere .= " AND g.department_id = $source";
236     $arwhere .= " AND a.department_id = $source";
237     $apwhere .= " AND a.department_id = $source";
238   }
239
240   if ($form->{source}) {
241     $source = $form->like(lc $form->{source});
242     $glwhere .= " AND lower(ac.source) LIKE '$source'";
243     $arwhere .= " AND lower(ac.source) LIKE '$source'";
244     $apwhere .= " AND lower(ac.source) LIKE '$source'";
245   }
246   if ($form->{datefrom}) {
247     $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
248     $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
249     $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
250   }
251   if ($form->{dateto}) {
252     $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
253     $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
254     $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
255   }
256   if ($form->{description}) {
257     my $description = $form->like(lc $form->{description});
258     $glwhere .= " AND lower(g.description) LIKE '$description'";
259     $arwhere .= " AND lower(ct.name) LIKE '$description'";
260     $apwhere .= " AND lower(ct.name) LIKE '$description'";
261   }
262   if ($form->{notes}) {
263     my $notes = $form->like(lc $form->{notes});
264     $glwhere .= " AND lower(g.notes) LIKE '$notes'";
265     $arwhere .= " AND lower(a.notes) LIKE '$notes'";
266     $apwhere .= " AND lower(a.notes) LIKE '$notes'";
267   }
268   if ($form->{accno}) {
269     $glwhere .= " AND c.accno = '$form->{accno}'";
270     $arwhere .= " AND c.accno = '$form->{accno}'";
271     $apwhere .= " AND c.accno = '$form->{accno}'";
272   }
273   if ($form->{gifi_accno}) {
274     $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
275     $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
276     $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
277   }
278   if ($form->{category} ne 'X') {
279     $glwhere .= " AND c.category = '$form->{category}'";
280     $arwhere .= " AND c.category = '$form->{category}'";
281     $apwhere .= " AND c.category = '$form->{category}'";
282   }
283
284   if ($form->{accno}) {
285     # get category for account
286     $query = qq|SELECT c.category
287                 FROM chart c
288                 WHERE c.accno = '$form->{accno}'|;
289     $sth = $dbh->prepare($query); 
290
291     $sth->execute || $form->dberror($query); 
292     ($form->{ml}) = $sth->fetchrow_array; 
293     $sth->finish; 
294     
295     if ($form->{datefrom}) {
296       $query = qq|SELECT SUM(ac.amount)
297                   FROM acc_trans ac, chart c
298                   WHERE ac.chart_id = c.id
299                   AND c.accno = '$form->{accno}'
300                   AND ac.transdate < date '$form->{datefrom}'
301                   |;
302       $sth = $dbh->prepare($query);
303       $sth->execute || $form->dberror($query);
304
305       ($form->{balance}) = $sth->fetchrow_array;
306       $sth->finish;
307     }
308   }
309   
310   if ($form->{gifi_accno}) {
311     # get category for account
312     $query = qq|SELECT c.category
313                 FROM chart c
314                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
315     $sth = $dbh->prepare($query); 
316
317     $sth->execute || $form->dberror($query); 
318     ($form->{ml}) = $sth->fetchrow_array; 
319     $sth->finish; 
320    
321     if ($form->{datefrom}) {
322       $query = qq|SELECT SUM(ac.amount)
323                   FROM acc_trans ac, chart c
324                   WHERE ac.chart_id = c.id
325                   AND c.gifi_accno = '$form->{gifi_accno}'
326                   AND ac.transdate < date '$form->{datefrom}'
327                   |;
328       $sth = $dbh->prepare($query);
329       $sth->execute || $form->dberror($query);
330
331       ($form->{balance}) = $sth->fetchrow_array;
332       $sth->finish;
333     }
334   }
335
336   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
337
338   
339   my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference, ac.taxkey, t.taxkey AS sorttax,
340                  g.description, ac.transdate, ac.source, ac.trans_id,
341                  ac.amount, c.accno, c.gifi_accno, g.notes, t.chart_id, ac.oid
342                  FROM gl g, acc_trans ac, chart c LEFT JOIN tax t ON
343                  (t.chart_id=c.id)
344                  WHERE $glwhere
345                  AND ac.chart_id = c.id
346                  AND g.id = ac.trans_id
347         UNION
348                  SELECT a.id, 'ar' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
349                  ct.name, ac.transdate, ac.source, ac.trans_id,
350                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
351                  FROM ar a, acc_trans ac, customer ct, chart c LEFT JOIN tax t ON
352                  (t.chart_id=c.id)
353                  WHERE $arwhere
354                  AND ac.chart_id = c.id
355                  AND a.customer_id = ct.id
356                  AND a.id = ac.trans_id
357         UNION
358                  SELECT a.id, 'ap' AS type, a.invoice, a.invnumber, ac.taxkey, t.taxkey AS sorttax,
359                  ct.name, ac.transdate, ac.source, ac.trans_id,
360                  ac.amount, c.accno, c.gifi_accno, a.notes, t.chart_id, ac.oid
361                  FROM ap a, acc_trans ac, vendor ct, chart c LEFT JOIN tax t ON
362                  (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                  ORDER BY transdate, trans_id, taxkey DESC, sorttax DESC, oid|;
368   my $sth = $dbh->prepare($query);
369   $sth->execute || $form->dberror($query);
370   
371   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
372     print(STDERR $ref->{id}, " Transaction\n");
373     # gl
374     if ($ref->{type} eq "gl") {
375       $ref->{module} = "gl";
376     }
377
378     # ap
379     if ($ref->{type} eq "ap") {
380       if ($ref->{invoice}) {
381         $ref->{module} = "ir";
382       } else {
383         $ref->{module} = "ap";
384       }
385     }
386
387     # ar
388     if ($ref->{type} eq "ar") {
389       if ($ref->{invoice}) {
390         $ref->{module} = "is";
391       } else {
392         $ref->{module} = "ar";
393       }
394     }
395     $balance=$ref->{amount};
396     $i = 0;
397     $j = 0;
398     $k = 0;
399     $l = 0;
400     if ($ref->{amount} < 0) {
401       if ($ref->{chart_id} >0) {
402         $ref->{debit_tax}{$i} = $ref->{amount} * -1;
403         $ref->{debit_tax_accno}{$i} = $ref->{accno};
404         }
405       else {
406         $ref->{debit}{$k} = $ref->{amount} * -1;
407         $ref->{debit_accno}{$k} = $ref->{accno};
408         $ref->{debit_taxkey}{$k} = $ref->{taxkey};
409         }
410     } else {
411       if ($ref->{chart_id} >0) {
412         $ref->{credit_tax}{$j} = $ref->{amount};
413         $ref->{credit_tax_accno}{$j} = $ref->{accno};
414         }
415       else {
416         $ref->{credit}{$l} = $ref->{amount};
417         $ref->{credit_accno}{$l} = $ref->{accno};
418         $ref->{credit_taxkey}{$l} = $ref->{taxkey};
419         }
420     }
421
422     while (abs($balance)>=0.015) {
423       my $ref2 = $sth->fetchrow_hashref(NAME_lc) || $form->error("Unbalanced ledger!");
424
425       $balance = (int($balance * 100000) + int(100000 * $ref2->{amount})) / 100000;
426       print(STDERR $balance," BAlance\n");
427       if ($ref2->{amount} < 0) {
428         if ($ref2->{chart_id} >0) {
429           if ($ref->{debit_tax_accno}{$i} ne "") {
430             $i++;
431           }
432           $ref->{debit_tax}{$i} = $ref2->{amount} * -1;
433           $ref->{debit_tax_accno}{$i} = $ref2->{accno};
434           }
435         else {
436           if ($ref->{debit_accno}{$k} ne "") {
437             $k++;
438           }
439           $ref->{debit}{$k} = $ref2->{amount} * -1;
440           $ref->{debit_accno}{$k} = $ref2->{accno};
441           $ref->{debit_taxkey}{$k} = $ref2->{taxkey};
442           }
443       } else {
444         if ($ref2->{chart_id} >0) {
445           if ($ref->{credit_tax_accno}{$j} ne "") {
446             $j++;
447           }
448           $ref->{credit_tax}{$j} = $ref2->{amount};
449           $ref->{credit_tax_accno}{$j} = $ref2->{accno};
450           }
451         else {
452           if ($ref->{credit_accno}{$l} ne "") {
453             $l++;
454           }
455           $ref->{credit}{$l} = $ref2->{amount};
456           $ref->{credit_accno}{$l} = $ref2->{accno};
457           $ref->{credit_taxkey}{$l} = $ref2->{taxkey};
458           }
459       }
460     }
461 #    print(STDERR Dumper($ref));       
462     push @{ $form->{GL} }, $ref;
463     $balance=0;
464   }
465   $sth->finish;
466
467   if ($form->{accno}) {
468     $query = qq|SELECT c.description FROM chart c WHERE c.accno = '$form->{accno}'|;
469     $sth = $dbh->prepare($query);
470     $sth->execute || $form->dberror($query);
471
472     ($form->{account_description}) = $sth->fetchrow_array;
473     $sth->finish;
474   }
475   if ($form->{gifi_accno}) {
476     $query = qq|SELECT g.description FROM gifi g WHERE g.accno = '$form->{gifi_accno}'|;
477     $sth = $dbh->prepare($query);
478     $sth->execute || $form->dberror($query);
479
480     ($form->{gifi_account_description}) = $sth->fetchrow_array;
481     $sth->finish;
482   }
483  
484   $dbh->disconnect;
485
486 }
487
488
489 sub transaction {
490   my ($self, $myconfig, $form) = @_;
491   
492   my ($query, $sth, $ref);
493   
494   # connect to database
495   my $dbh = $form->dbconnect($myconfig);
496
497   if ($form->{id}) {
498     $query = "SELECT closedto, revtrans
499               FROM defaults";
500     $sth = $dbh->prepare($query);
501     $sth->execute || $form->dberror($query);
502
503     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
504     $sth->finish;
505
506     $query = "SELECT g.reference, g.description, g.notes, g.transdate,
507               d.description AS department, e.name as employee, g.taxincluded, g.gldate
508               FROM gl g
509             LEFT JOIN department d ON (d.id = g.department_id)  
510             LEFT JOIN employee e ON (e.id = g.employee_id)  
511             WHERE g.id = $form->{id}";
512     $sth = $dbh->prepare($query);
513     $sth->execute || $form->dberror($query);
514     $ref = $sth->fetchrow_hashref(NAME_lc);
515     map { $form->{$_} = $ref->{$_} } keys %$ref;
516     $sth->finish;
517   
518     # retrieve individual rows
519     $query = "SELECT c.accno, a.amount, project_id,
520                 (SELECT p.projectnumber FROM project p
521                  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
522               FROM acc_trans a, chart c
523               WHERE a.chart_id = c.id
524               AND a.trans_id = $form->{id}
525               ORDER BY a.oid";
526     $sth = $dbh->prepare($query);
527     $sth->execute || $form->dberror($query);
528     
529     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
530       push @{ $form->{GL} }, $ref;
531     }
532
533       # get tax description
534     $query = qq| SELECT * FROM tax t|;
535     $sth = $dbh->prepare($query);
536     $sth->execute || $form->dberror($query);  
537     $form->{TAX} = ();
538     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
539       push @{ $form->{TAX} }, $ref;
540     }
541   
542     $sth->finish;
543   } else {
544     $query = "SELECT current_date AS transdate, closedto, revtrans
545               FROM defaults";
546     $sth = $dbh->prepare($query);
547     $sth->execute || $form->dberror($query);
548
549     ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
550     
551       # get tax description
552     $query = qq| SELECT * FROM tax t order by t.taxkey|;
553     $sth = $dbh->prepare($query);
554     $sth->execute || $form->dberror($query);  
555     $form->{TAX} = ();
556     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
557       push @{ $form->{TAX} }, $ref;
558     }
559   }
560
561   $sth->finish;
562
563   # get chart of accounts
564   $query = qq|SELECT c.accno, c.description, c.taxkey_id
565               FROM chart c
566               WHERE c.charttype = 'A'
567               ORDER by c.accno|;
568   $sth = $dbh->prepare($query);
569   $sth->execute || $form->dberror($query);
570   $form->{chart} = ();
571   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
572     push @{ $form->{chart} }, $ref;
573   }
574   $sth->finish;
575   
576
577   
578   $sth->finish;
579   
580   $dbh->disconnect;
581
582 }
583
584
585 1;
586