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