bd77fea12829543cab253505abd17444885c03e8
[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 use SL::DB;
39
40 use strict;
41
42 sub paymentaccounts {
43   $main::lxdebug->enter_sub();
44
45   my ($self, $myconfig, $form) = @_;
46
47   my $dbh = SL::DB->client->dbh;
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
57   $main::lxdebug->leave_sub();
58 }
59
60 sub payment_transactions {
61   $main::lxdebug->enter_sub();
62
63   my ($self, $myconfig, $form) = @_;
64
65   # connect to database, turn AutoCommit off
66   my $dbh = SL::DB->client->dbh;
67
68   my ($query, @values);
69
70   # get cleared balance
71   if ($form->{fromdate}) {
72     $query =
73       qq|SELECT sum(a.amount), | .
74       qq|  (SELECT DISTINCT c2.category FROM chart c2 | .
75       qq|   WHERE c2.accno = ?) AS category | .
76       qq|FROM acc_trans a | .
77       qq|JOIN chart c ON (c.id = a.chart_id) | .
78       qq|WHERE a.transdate < ? AND a.cleared = '1' AND c.accno = ?|;
79     @values = ($form->{accno}, conv_date($form->{fromdate}), $form->{accno});
80
81   } else {
82     $query =
83       qq|SELECT sum(a.amount), | .
84       qq|  (SELECT DISTINCT c2.category FROM chart c2 | .
85       qq|   WHERE c2.accno = ?) AS category | .
86       qq|FROM acc_trans a | .
87       qq|JOIN chart c ON (c.id = a.chart_id) | .
88       qq|WHERE a.cleared = '1' AND c.accno = ?|;
89     @values = ($form->{accno}, $form->{accno});
90   }
91
92   ($form->{beginningbalance}, $form->{category}) =
93     selectrow_query($form, $dbh, $query, @values);
94
95   @values = ();
96   $query =
97     qq|SELECT c.name, ac.source, ac.transdate, ac.cleared, | .
98     qq|  ac.fx_transaction, ac.amount, a.id, | .
99     qq|  ac.acc_trans_id AS oid | .
100     qq|FROM customer c, acc_trans ac, ar a, chart ch | .
101     qq|WHERE c.id = a.customer_id | .
102     qq|  AND ac.cleared = '0' | .
103     qq|  AND ac.trans_id = a.id | .
104     qq|  AND ac.chart_id = ch.id | .
105     qq|  AND ch.accno = ? |;
106   push(@values, $form->{accno});
107
108   if($form->{fromdate}) {
109     $query .= qq|  AND ac.transdate >= ? |;
110     push(@values, conv_date($form->{fromdate}));
111   }
112
113   if($form->{todate}){
114     $query .= qq|  AND ac.transdate <= ? |;
115     push(@values, conv_date($form->{todate}));
116   }
117
118   if($form->{additional_fromdate}) {
119     $query .= qq|  AND ac.transdate >= ? |;
120     push(@values, conv_date($form->{additional_fromdate}));
121   }
122
123   if($form->{additional_todate}){
124     $query .= qq|  AND ac.transdate <= ? |;
125     push(@values, conv_date($form->{additional_todate}));
126   }
127
128   if($form->{filter_amount}){
129     $query .= qq|  AND ac.amount = ? |;
130     push(@values, conv_i($form->{filter_amount}));
131   }
132
133   $query .=
134     qq|UNION | .
135
136     qq|SELECT v.name, ac.source, ac.transdate, ac.cleared, | .
137     qq|  ac.fx_transaction, ac.amount, a.id, | .
138     qq|  ac.acc_trans_id AS oid | .
139     qq|FROM vendor v, acc_trans ac, ap a, chart ch | .
140     qq|WHERE v.id = a.vendor_id | .
141     qq|  AND ac.cleared = '0' | .
142     qq|  AND ac.trans_id = a.id | .
143     qq|  AND ac.chart_id = ch.id | .
144     qq|  AND ch.accno = ? |;
145
146   push(@values, $form->{accno});
147
148   if($form->{fromdate}) {
149     $query .= qq| AND ac.transdate >= ? |;
150     push(@values, conv_date($form->{fromdate}));
151   }
152
153   if($form->{todate}){
154     $query .= qq| AND ac.transdate <= ? |;
155     push(@values, conv_date($form->{todate}));
156   }
157
158   if($form->{additional_fromdate}) {
159     $query .= qq| AND ac.transdate >= ? |;
160     push(@values, conv_date($form->{additional_fromdate}));
161   }
162
163   if($form->{additional_todate}){
164     $query .= qq| AND ac.transdate <= ? |;
165     push(@values, conv_date($form->{additional_todate}));
166   }
167
168   if($form->{filter_amount}){
169     $query .= qq| AND ac.amount = ? |;
170     push(@values, conv_i($form->{filter_amount}));
171   }
172
173   $query .=
174     qq|UNION | .
175
176     qq|SELECT g.description, ac.source, ac.transdate, ac.cleared, | .
177     qq|  ac.fx_transaction, ac.amount, g.id, | .
178     qq|  ac.acc_trans_id AS oid | .
179     qq|FROM gl g, acc_trans ac, chart ch | .
180     qq|WHERE g.id = ac.trans_id | .
181     qq|  AND ac.cleared = '0' | .
182     qq|  AND ac.trans_id = g.id | .
183     qq|  AND ac.chart_id = ch.id | .
184     qq|  AND ch.accno = ? |;
185
186   push(@values, $form->{accno});
187
188   if($form->{fromdate}) {
189     $query .= qq| AND ac.transdate >= ? |;
190     push(@values, conv_date($form->{fromdate}));
191   }
192
193   if($form->{todate}){
194     $query .= qq| AND ac.transdate <= ? |;
195     push(@values, conv_date($form->{todate}));
196   }
197
198   if($form->{additional_fromdate}) {
199     $query .= qq| AND ac.transdate >= ? |;
200     push(@values, conv_date($form->{additional_fromdate}));
201   }
202
203   if($form->{additional_todate}){
204     $query .= qq| AND ac.transdate <= ? |;
205     push(@values, conv_date($form->{additional_todate}));
206   }
207
208   if($form->{filter_amount}){
209     $query .= qq| AND ac.amount = ? |;
210     push(@values, conv_i($form->{filter_amount}));
211   }
212
213   $query .= " ORDER BY 3,7,8";
214
215   $form->{PR} = selectall_hashref_query($form, $dbh, $query, @values);
216
217   $main::lxdebug->leave_sub();
218 }
219
220 sub reconcile {
221   $main::lxdebug->enter_sub();
222
223   my ($self, $myconfig, $form) = @_;
224
225   SL::DB->client->with_transaction(sub {
226     my $dbh = SL::DB->client->dbh;
227
228     my ($query, $i);
229
230     # clear flags
231     for $i (1 .. $form->{rowcount}) {
232       if ($form->{"cleared_$i"}) {
233         $query =
234           qq|UPDATE acc_trans SET cleared = '1' | .
235           qq|WHERE acc_trans_id = ?|;
236         do_query($form, $dbh, $query, $form->{"oid_$i"});
237
238         # clear fx_transaction
239         if ($form->{"fxoid_$i"}) {
240           $query =
241             qq|UPDATE acc_trans SET cleared = '1' | .
242             qq|WHERE acc_trans_id = ?|;
243           do_query($form, $dbh, $query, $form->{"fxoid_$i"});
244         }
245       }
246     }
247     1;
248   }) or do { die SL::DB->client->error };
249
250   $main::lxdebug->leave_sub();
251 }
252
253 sub get_statement_balance {
254   $main::lxdebug->enter_sub();
255
256   my ($self, $myconfig, $form) = @_;
257
258   # connect to database, turn AutoCommit off
259   my $dbh = SL::DB->client->dbh;
260
261   my ($query, @values);
262
263   $query = qq|SELECT sum(amount) FROM acc_trans where chart_id=45 AND cleared='1'|;
264
265   if($form->{fromdate}) {
266     $query .= qq| AND transdate >= ? |;
267     push(@values, conv_date($form->{fromdate}));
268   }
269
270   if($form->{todate}){
271     $query .= qq| AND transdate <= ? |;
272     push(@values, conv_date($form->{todate}));
273   }
274
275   ($form->{statement_balance}) = selectrow_query($form, $dbh, $query, @values);
276
277   $main::lxdebug->leave_sub();
278 }
279
280 1;