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