epic-s6ts
[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., 51 Franklin Street, Fifth Floor, Boston,
29 # MA 02110-1335, USA.
30 #======================================================================
31 #
32 # Account reconciliation routines
33 #
34 #======================================================================
35
36 package RC;
37
38 use SL::DBUtils;
39 use SL::DB;
40
41 use strict;
42
43 sub paymentaccounts {
44   $main::lxdebug->enter_sub();
45
46   my ($self, $myconfig, $form) = @_;
47
48   my $dbh = SL::DB->client->dbh;
49
50   my $query =
51     qq|SELECT accno, description | .
52     qq|FROM chart | .
53     qq|WHERE link LIKE '%_paid%' AND category IN ('A', 'L') | .
54     qq|ORDER BY accno|;
55
56   $form->{PR} = selectall_hashref_query($form, $dbh, $query);
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 = SL::DB->client->dbh;
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   @values = ();
97   $query =
98     qq|SELECT c.name, ac.source, ac.transdate, ac.cleared, | .
99     qq|  ac.fx_transaction, ac.amount, a.id, | .
100     qq|  ac.acc_trans_id AS oid | .
101     qq|FROM customer c, acc_trans ac, ar a, chart ch | .
102     qq|WHERE c.id = a.customer_id | .
103     qq|  AND ac.cleared = '0' | .
104     qq|  AND ac.trans_id = a.id | .
105     qq|  AND ac.chart_id = ch.id | .
106     qq|  AND ch.accno = ? |;
107   push(@values, $form->{accno});
108
109   if($form->{fromdate}) {
110     $query .= qq|  AND ac.transdate >= ? |;
111     push(@values, conv_date($form->{fromdate}));
112   }
113
114   if($form->{todate}){
115     $query .= qq|  AND ac.transdate <= ? |;
116     push(@values, conv_date($form->{todate}));
117   }
118
119   if($form->{additional_fromdate}) {
120     $query .= qq|  AND ac.transdate >= ? |;
121     push(@values, conv_date($form->{additional_fromdate}));
122   }
123
124   if($form->{additional_todate}){
125     $query .= qq|  AND ac.transdate <= ? |;
126     push(@values, conv_date($form->{additional_todate}));
127   }
128
129   if($form->{filter_amount}){
130     $query .= qq|  AND ac.amount = ? |;
131     push(@values, conv_i($form->{filter_amount}));
132   }
133
134   $query .=
135     qq|UNION | .
136
137     qq|SELECT v.name, ac.source, ac.transdate, ac.cleared, | .
138     qq|  ac.fx_transaction, ac.amount, a.id, | .
139     qq|  ac.acc_trans_id AS oid | .
140     qq|FROM vendor v, acc_trans ac, ap a, chart ch | .
141     qq|WHERE v.id = a.vendor_id | .
142     qq|  AND ac.cleared = '0' | .
143     qq|  AND ac.trans_id = a.id | .
144     qq|  AND ac.chart_id = ch.id | .
145     qq|  AND ch.accno = ? |;
146
147   push(@values, $form->{accno});
148
149   if($form->{fromdate}) {
150     $query .= qq| AND ac.transdate >= ? |;
151     push(@values, conv_date($form->{fromdate}));
152   }
153
154   if($form->{todate}){
155     $query .= qq| AND ac.transdate <= ? |;
156     push(@values, conv_date($form->{todate}));
157   }
158
159   if($form->{additional_fromdate}) {
160     $query .= qq| AND ac.transdate >= ? |;
161     push(@values, conv_date($form->{additional_fromdate}));
162   }
163
164   if($form->{additional_todate}){
165     $query .= qq| AND ac.transdate <= ? |;
166     push(@values, conv_date($form->{additional_todate}));
167   }
168
169   if($form->{filter_amount}){
170     $query .= qq| AND ac.amount = ? |;
171     push(@values, conv_i($form->{filter_amount}));
172   }
173
174   $query .=
175     qq|UNION | .
176
177     qq|SELECT g.description, ac.source, ac.transdate, ac.cleared, | .
178     qq|  ac.fx_transaction, ac.amount, g.id, | .
179     qq|  ac.acc_trans_id AS oid | .
180     qq|FROM gl g, acc_trans ac, chart ch | .
181     qq|WHERE g.id = ac.trans_id | .
182     qq|  AND ac.cleared = '0' | .
183     qq|  AND ac.trans_id = g.id | .
184     qq|  AND ac.chart_id = ch.id | .
185     qq|  AND ch.accno = ? |;
186
187   push(@values, $form->{accno});
188
189   if($form->{fromdate}) {
190     $query .= qq| AND ac.transdate >= ? |;
191     push(@values, conv_date($form->{fromdate}));
192   }
193
194   if($form->{todate}){
195     $query .= qq| AND ac.transdate <= ? |;
196     push(@values, conv_date($form->{todate}));
197   }
198
199   if($form->{additional_fromdate}) {
200     $query .= qq| AND ac.transdate >= ? |;
201     push(@values, conv_date($form->{additional_fromdate}));
202   }
203
204   if($form->{additional_todate}){
205     $query .= qq| AND ac.transdate <= ? |;
206     push(@values, conv_date($form->{additional_todate}));
207   }
208
209   if($form->{filter_amount}){
210     $query .= qq| AND ac.amount = ? |;
211     push(@values, conv_i($form->{filter_amount}));
212   }
213
214   $query .= " ORDER BY 3,7,8";
215
216   $form->{PR} = selectall_hashref_query($form, $dbh, $query, @values);
217
218   $main::lxdebug->leave_sub();
219 }
220
221 sub reconcile {
222   $main::lxdebug->enter_sub();
223
224   my ($self, $myconfig, $form) = @_;
225
226   SL::DB->client->with_transaction(sub {
227     my $dbh = SL::DB->client->dbh;
228
229     my ($query, $i);
230
231     # clear flags
232     for $i (1 .. $form->{rowcount}) {
233       if ($form->{"cleared_$i"}) {
234         $query =
235           qq|UPDATE acc_trans SET cleared = '1' | .
236           qq|WHERE acc_trans_id = ?|;
237         do_query($form, $dbh, $query, $form->{"oid_$i"});
238
239         # clear fx_transaction
240         if ($form->{"fxoid_$i"}) {
241           $query =
242             qq|UPDATE acc_trans SET cleared = '1' | .
243             qq|WHERE acc_trans_id = ?|;
244           do_query($form, $dbh, $query, $form->{"fxoid_$i"});
245         }
246       }
247     }
248     1;
249   }) or do { die SL::DB->client->error };
250
251   $main::lxdebug->leave_sub();
252 }
253
254 sub get_statement_balance {
255   $main::lxdebug->enter_sub();
256
257   my ($self, $myconfig, $form) = @_;
258
259   # connect to database, turn AutoCommit off
260   my $dbh = SL::DB->client->dbh;
261
262   my ($query, @values);
263
264   $query = qq|SELECT sum(amount) FROM acc_trans where chart_id=45 AND cleared='1'|;
265
266   if($form->{fromdate}) {
267     $query .= qq| AND transdate >= ? |;
268     push(@values, conv_date($form->{fromdate}));
269   }
270
271   if($form->{todate}){
272     $query .= qq| AND transdate <= ? |;
273     push(@values, conv_date($form->{todate}));
274   }
275
276   ($form->{statement_balance}) = selectrow_query($form, $dbh, $query, @values);
277
278   $main::lxdebug->leave_sub();
279 }
280
281 1;