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