Recommit von r1001 von skoehler: Behebt/umgeht Bug 323. SQL-Fehler bei Kontenabgeleic...
[kivitendo-erp.git] / SL / RC.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) 2002
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 # Account reconciliation routines
32 #
33 #======================================================================
34
35 package RC;
36
37 sub paymentaccounts {
38   $main::lxdebug->enter_sub();
39
40   my ($self, $myconfig, $form) = @_;
41
42   # connect to database
43   my $dbh = $form->dbconnect($myconfig);
44
45   my $query = qq|SELECT c.accno, c.description
46                  FROM chart c
47                  WHERE c.link LIKE '%_paid%'
48                  AND (c.category = 'A' OR c.category = 'L')
49                  ORDER BY c.accno|;
50   my $sth = $dbh->prepare($query);
51   $sth->execute || $form->dberror($query);
52
53   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
54     push @{ $form->{PR} }, $ref;
55   }
56   $sth->finish;
57   $dbh->disconnect;
58
59   $main::lxdebug->leave_sub();
60 }
61
62 sub payment_transactions {
63   $main::lxdebug->enter_sub();
64
65   my ($self, $myconfig, $form) = @_;
66
67   # connect to database, turn AutoCommit off
68   my $dbh = $form->dbconnect_noauto($myconfig);
69
70   my ($query, $sth);
71
72   # get cleared balance
73   if ($form->{fromdate}) {
74     $query = qq|SELECT sum(a.amount),
75                      (SELECT DISTINCT c2.category FROM chart c2
76                       WHERE c2.accno = '$form->{accno}') AS category
77                 FROM acc_trans a
78                 JOIN chart c ON (c.id = a.chart_id)
79                 WHERE a.transdate < date '$form->{fromdate}'
80                 AND a.cleared = '1'
81                 AND c.accno = '$form->{accno}'
82                 |;
83   } else {
84     $query = qq|SELECT sum(a.amount),
85                      (SELECT DISTINCT c2.category FROM chart c2
86                       WHERE c2.accno = '$form->{accno}') AS category
87                 FROM acc_trans a
88                 JOIN chart c ON (c.id = a.chart_id)
89                 WHERE a.cleared = '1'
90                 AND c.accno = '$form->{accno}'
91                 |;
92   }
93
94   $sth = $dbh->prepare($query);
95   $sth->execute || $form->dberror($query);
96
97   ($form->{beginningbalance}, $form->{category}) = $sth->fetchrow_array;
98
99   $sth->finish;
100
101   my %oid = ('Pg'     => 'ac.oid',
102              'Oracle' => 'ac.rowid');
103
104   $query = qq|SELECT c.name, ac.source, ac.transdate, ac.cleared,
105               ac.fx_transaction, ac.amount, a.id,
106               $oid{$myconfig->{dbdriver}} AS oid
107               FROM customer c, acc_trans ac, ar a, chart ch
108               WHERE c.id = a.customer_id
109 --            AND NOT ac.fx_transaction
110               AND ac.cleared = '0'
111               AND ac.trans_id = a.id
112               AND ac.chart_id = ch.id
113               AND ch.accno = '$form->{accno}'
114               |;
115
116   $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
117   $query .= " AND ac.transdate <= '$form->{todate}'"   if $form->{todate};
118
119   $query .= qq|
120
121       UNION
122               SELECT v.name, ac.source, ac.transdate, ac.cleared,
123               ac.fx_transaction, ac.amount, a.id,
124               $oid{$myconfig->{dbdriver}} AS oid
125               FROM vendor v, acc_trans ac, ap a, chart ch
126               WHERE v.id = a.vendor_id
127 --            AND NOT ac.fx_transaction
128               AND ac.cleared = '0'
129               AND ac.trans_id = a.id
130               AND ac.chart_id = ch.id
131               AND ch.accno = '$form->{accno}'
132              |;
133
134   $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
135   $query .= " AND ac.transdate <= '$form->{todate}'"   if $form->{todate};
136
137   $query .= qq|
138
139       UNION
140               SELECT g.description, ac.source, ac.transdate, ac.cleared,
141               ac.fx_transaction, ac.amount, g.id,
142               $oid{$myconfig->{dbdriver}} AS oid
143               FROM gl g, acc_trans ac, chart ch
144               WHERE g.id = ac.trans_id
145 --            AND NOT ac.fx_transaction
146               AND ac.cleared = '0'
147               AND ac.trans_id = g.id
148               AND ac.chart_id = ch.id
149               AND ch.accno = '$form->{accno}'
150               |;
151
152   $query .= " AND ac.transdate >= '$form->{fromdate}'" if $form->{fromdate};
153   $query .= " AND ac.transdate <= '$form->{todate}'"   if $form->{todate};
154
155   $query .= " ORDER BY 3,7,8";
156
157   $sth = $dbh->prepare($query);
158   $sth->execute || $form->dberror($query);
159
160   while (my $pr = $sth->fetchrow_hashref(NAME_lc)) {
161     push @{ $form->{PR} }, $pr;
162   }
163   $sth->finish;
164
165   $dbh->disconnect;
166
167   $main::lxdebug->leave_sub();
168 }
169
170 sub reconcile {
171   $main::lxdebug->enter_sub();
172
173   my ($self, $myconfig, $form) = @_;
174
175   # connect to database
176   my $dbh = $form->dbconnect($myconfig);
177
178   my ($query, $i);
179   my %oid = ('Pg'     => 'oid',
180              'Oracle' => 'rowid');
181
182   # clear flags
183   for $i (1 .. $form->{rowcount}) {
184     if ($form->{"cleared_$i"}) {
185       $query = qq|UPDATE acc_trans SET cleared = '1'
186                   WHERE $oid{$myconfig->{dbdriver}} = $form->{"oid_$i"}|;
187       $dbh->do($query) || $form->dberror($query);
188
189       # clear fx_transaction
190       if ($form->{"fxoid_$i"}) {
191         $query = qq|UPDATE acc_trans SET cleared = '1'
192                     WHERE $oid{$myconfig->{dbdriver}} = $form->{"fxoid_$i"}|;
193         $dbh->do($query) || $form->dberror($query);
194       }
195     }
196   }
197
198   $dbh->disconnect;
199
200   $main::lxdebug->leave_sub();
201 }
202
203 1;