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