Bei eur wurden in der Kontenliste keine unbezahlten Rechnungen angezeigt. Entfernt.
[kivitendo-erp.git] / SL / CA.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 # chart of accounts
31 #
32 # CHANGE LOG:
33 #   DS. 2000-07-04  Created
34 #
35 #======================================================================
36
37 package CA;
38 use Data::Dumper;
39
40 sub all_accounts {
41   $main::lxdebug->enter_sub();
42
43   my ($self, $myconfig, $form) = @_;
44
45   my %amount;
46
47   # connect to database
48   my $dbh = $form->dbconnect($myconfig);
49
50   my $query = qq|SELECT c.accno,
51                  SUM(a.amount) AS amount
52                  FROM chart c, acc_trans a
53                  WHERE c.id = a.chart_id
54                  GROUP BY c.accno|;
55   my $sth = $dbh->prepare($query);
56   $sth->execute || $form->dberror($query);
57
58   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
59     $amount{ $ref->{accno} } = $ref->{amount};
60   }
61   $sth->finish;
62
63   $query = qq|SELECT accno, description
64               FROM gifi|;
65   $sth = $dbh->prepare($query);
66   $sth->execute || $form->dberror($query);
67
68   my %gifi;
69   while (my ($accno, $description) = $sth->fetchrow_array) {
70     $gifi{$accno} = $description;
71   }
72   $sth->finish;
73
74   $query = qq|SELECT c.id, c.accno, c.description, c.charttype, c.gifi_accno,
75               c.category, c.link
76               FROM chart c
77               ORDER BY accno|;
78   $sth = $dbh->prepare($query);
79   $sth->execute || $form->dberror($query);
80
81   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
82     $ca->{amount}           = $amount{ $ca->{accno} };
83     $ca->{gifi_description} = $gifi{ $ca->{gifi_accno} };
84     if ($ca->{amount} < 0) {
85       $ca->{debit} = $ca->{amount} * -1;
86     } else {
87       $ca->{credit} = $ca->{amount};
88     }
89     push @{ $form->{CA} }, $ca;
90   }
91
92   $sth->finish;
93   $dbh->disconnect;
94
95   $main::lxdebug->leave_sub();
96 }
97
98 sub all_transactions {
99   $main::lxdebug->enter_sub();
100
101   my ($self, $myconfig, $form) = @_;
102
103   # connect to database
104   my $dbh = $form->dbconnect($myconfig);
105
106   # get chart_id
107   my $query = qq|SELECT c.id FROM chart c
108                  WHERE c.accno = '$form->{accno}'|;
109   if ($form->{accounttype} eq 'gifi') {
110     $query = qq|SELECT c.id FROM chart c
111                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
112   }
113   my $sth = $dbh->prepare($query);
114   $sth->execute || $form->dberror($query);
115
116   my @id = ();
117   while (my ($id) = $sth->fetchrow_array) {
118     push @id, $id;
119   }
120   $sth->finish;
121
122   my $fromdate_where;
123   my $todate_where;
124
125   my $where = '1 = 1';
126
127   # build WHERE clause from dates if any
128   #  if ($form->{fromdate}) {
129   #    $where .= " AND ac.transdate >= '$form->{fromdate}'";
130   #  }
131   #  if ($form->{todate}) {
132   #    $where .= " AND ac.transdate <= '$form->{todate}'";
133   #  }
134
135   if ($form->{fromdate}) {
136     $fromto = " AND ac.transdate >= '$form->{fromdate}'";
137     $subwhere .= " AND transdate >= '$form->{fromdate}'";
138     $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
139   }
140
141   if ($form->{todate}) {
142     $fromto   .= " AND ac.transdate <= '$form->{todate}'";
143     $subwhere .= " AND transdate <= '$form->{todate}'";
144     $glwhere .= " AND ac.transdate <= '$form->{todate}'";
145   }
146
147
148   $where .= $fromto;
149   $AR_PAID = "";
150   $AP_PAID = "";
151   $glwhere = "";    # note! gl will be aliased as "a" later...
152   my $sortorder = join ', ',
153     $form->sort_columns(qw(transdate reference description));
154   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
155
156   # Oracle workaround, use ordinal positions
157   my %ordinal = (transdate   => 4,
158                  reference   => 2,
159                  description => 3);
160   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
161
162   my ($null, $department_id) = split /--/, $form->{department};
163   my $dpt_where;
164   my $dpt_join;
165   if ($department_id) {
166     $dpt_join = qq|
167                    JOIN department t ON (t.id = a.department_id)
168                   |;
169     $dpt_where = qq|
170                    AND t.id = $department_id
171                   |;
172   }
173
174   my $project;
175   if ($form->{project_id}) {
176     $project = qq|
177                  AND ac.project_id = $form->{project_id}
178                  |;
179   }
180
181   if ($form->{accno} || $form->{gifi_accno}) {
182
183     # get category for account
184     $query = qq|SELECT c.category
185                 FROM chart c
186                 WHERE c.accno = '$form->{accno}'|;
187
188     if ($form->{accounttype} eq 'gifi') {
189       $query = qq|SELECT c.category
190                 FROM chart c
191                 WHERE c.gifi_accno = '$form->{gifi_accno}'
192                 AND c.charttype = 'A'|;
193     }
194
195     $sth = $dbh->prepare($query);
196
197     $sth->execute || $form->dberror($query);
198     ($form->{category}) = $sth->fetchrow_array;
199     $sth->finish;
200
201     if ($form->{fromdate}) {
202
203       # get beginning balance
204       $query = qq|SELECT SUM(ac.amount)
205                   FROM acc_trans ac
206                   JOIN chart c ON (ac.chart_id = c.id)
207                   $dpt_join
208                   WHERE c.accno = '$form->{accno}'
209                   AND ac.transdate < '$form->{fromdate}'
210                   $dpt_where
211                   $project
212                   |;
213
214       if ($form->{project_id}) {
215
216         $query .= qq|
217
218                UNION
219
220                   SELECT SUM(ac.qty * ac.sellprice)
221                   FROM invoice ac
222                   JOIN ar a ON (ac.trans_id = a.id)
223                   JOIN parts p ON (ac.parts_id = p.id)
224                   JOIN chart c ON (p.income_accno_id = c.id)
225                   $dpt_join
226                   WHERE c.accno = '$form->{accno}'
227                   AND a.transdate < '$form->{fromdate}'
228                   AND c.category = 'I'
229                   $dpt_where
230                   $project
231
232                UNION
233
234                   SELECT SUM(ac.qty * ac.sellprice)
235                   FROM invoice ac
236                   JOIN ap a ON (ac.trans_id = a.id)
237                   JOIN parts p ON (ac.parts_id = p.id)
238                   JOIN chart c ON (p.expense_accno_id = c.id)
239                   $dpt_join
240                   WHERE c.accno = '$form->{accno}'
241                   AND a.transdate < '$form->{fromdate}'
242                   AND c.category = 'E'
243                   $dpt_where
244                   $project
245                   |;
246
247       }
248
249       if ($form->{accounttype} eq 'gifi') {
250         $query = qq|SELECT SUM(ac.amount)
251                   FROM acc_trans ac
252                   JOIN chart c ON (ac.chart_id = c.id)
253                   $dpt_join
254                   WHERE c.gifi_accno = '$form->{gifi_accno}'
255                   AND ac.transdate < '$form->{fromdate}'
256                   $dpt_where
257                   $project
258                   |;
259
260         if ($form->{project_id}) {
261
262           $query .= qq|
263
264                UNION
265
266                   SELECT SUM(ac.qty * ac.sellprice)
267                   FROM invoice ac
268                   JOIN ar a ON (ac.trans_id = a.id)
269                   JOIN parts p ON (ac.parts_id = p.id)
270                   JOIN chart c ON (p.income_accno_id = c.id)
271                   $dpt_join
272                   WHERE c.gifi_accno = '$form->{gifi_accno}'
273                   AND a.transdate < '$form->{fromdate}'
274                   AND c.category = 'I'
275                   $dpt_where
276                   $project
277
278                UNION
279
280                   SELECT SUM(ac.qty * ac.sellprice)
281                   FROM invoice ac
282                   JOIN ap a ON (ac.trans_id = a.id)
283                   JOIN parts p ON (ac.parts_id = p.id)
284                   JOIN chart c ON (p.expense_accno_id = c.id)
285                   $dpt_join
286                   WHERE c.gifi_accno = '$form->{gifi_accno}'
287                   AND a.transdate < '$form->{fromdate}'
288                   AND c.category = 'E'
289                   $dpt_where
290                   $project
291                   |;
292
293         }
294       }
295
296       $sth = $dbh->prepare($query);
297
298       $sth->execute || $form->dberror($query);
299       ($form->{balance}) = $sth->fetchrow_array;
300       $sth->finish;
301     }
302   }
303
304   $query = "";
305   my $union = "";
306
307   foreach my $id (@id) {
308
309     # NOTE:
310     #  Postgres is really picky about the order of implicit CROSS JOINs with ','
311     #  if you alias the  tables and want to use the alias later in another JOIN.
312     #  the alias you want to use has to be the most recent in the list, otherwise
313     #  Postgres will overwrite the alias internally and complain.
314     #  For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
315     #  Don't change this, and if you do, substitute the ',' with CROSS JOIN
316     #  ... that also works.
317
318     # get all transactions
319     $query .= qq|$union
320       SELECT a.id, a.reference, a.description, ac.transdate,
321              $false AS invoice, ac.amount, 'gl' as module
322                 FROM acc_trans ac, gl a $dpt_join
323                 WHERE $where
324                 $glwhere
325                 $dpt_where
326                 $project
327                 AND ac.chart_id = $id
328                 AND ac.trans_id = a.id
329       UNION
330       SELECT a.id, a.invnumber, c.name, ac.transdate,
331              a.invoice, ac.amount, 'ar' as module
332                 FROM acc_trans ac, customer c, ar a $dpt_join
333                 WHERE $where
334                 $dpt_where
335                 $project
336                 AND ac.chart_id = $id
337                 AND ac.trans_id = a.id
338                 $AR_PAID
339                 AND a.customer_id = c.id
340       UNION
341       SELECT a.id, a.invnumber, v.name, ac.transdate,
342              a.invoice, ac.amount, 'ap' as module
343                 FROM acc_trans ac, vendor v, ap a $dpt_join
344                 WHERE $where
345                 $dpt_where
346                 $project
347                 AND ac.chart_id = $id
348                 AND ac.trans_id = a.id
349                 $AP_PAID
350                 AND a.vendor_id = v.id
351                 |;
352     $union = qq|
353       UNION ALL
354       |;
355
356     if ($form->{project_id}) {
357
358       $fromdate_where =~ s/ac\./a\./;
359       $todate_where   =~ s/ac\./a\./;
360
361       $query .= qq|
362
363              UNION ALL
364
365                  SELECT a.id, a.invnumber, c.name, a.transdate,
366                  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
367                  FROM ar a
368                  JOIN invoice ac ON (ac.trans_id = a.id)
369                  JOIN parts p ON (ac.parts_id = p.id)
370                  JOIN customer c ON (a.customer_id = c.id)
371                  $dpt_join
372                  WHERE p.income_accno_id = $id
373                  $fromdate_where
374                  $todate_where
375                  $dpt_where
376                  $project
377
378              UNION ALL
379
380                  SELECT a.id, a.invnumber, v.name, a.transdate,
381                  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
382                  FROM ap a
383                  JOIN invoice ac ON (ac.trans_id = a.id)
384                  JOIN parts p ON (ac.parts_id = p.id)
385                  JOIN vendor v ON (a.vendor_id = v.id)
386                  $dpt_join
387                  WHERE p.expense_accno_id = $id
388                  $fromdate_where
389                  $todate_where
390                  $dpt_where
391                  $project
392                  |;
393
394       $fromdate_where =~ s/a\./ac\./;
395       $todate_where   =~ s/a\./ac\./;
396
397     }
398
399     $union = qq|
400              UNION ALL
401                  |;
402   }
403
404   $query .= qq|
405       ORDER BY $sortorder|;
406
407   $sth = $dbh->prepare($query);
408   $sth->execute || $form->dberror($query);
409
410   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
411
412     # gl
413     if ($ca->{module} eq "gl") {
414       $ca->{module} = "gl";
415     }
416
417     # ap
418     if ($ca->{module} eq "ap") {
419       $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
420     }
421
422     # ar
423     if ($ca->{module} eq "ar") {
424       $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
425     }
426
427     if ($ca->{amount} < 0) {
428       $ca->{debit}  = $ca->{amount} * -1;
429       $ca->{credit} = 0;
430     } else {
431       $ca->{credit} = $ca->{amount};
432       $ca->{debit}  = 0;
433     }
434
435     push @{ $form->{CA} }, $ca;
436
437   }
438
439   $sth->finish;
440   $dbh->disconnect;
441
442   $main::lxdebug->leave_sub();
443 }
444
445 1;