1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
27 # DS. 2000-07-04 Created
29 #======================================================================
36 my ($self, $myconfig, $form) = @_;
40 my $dbh = $form->dbconnect($myconfig);
42 my $query = qq|SELECT accno,
43 SUM(acc_trans.amount) AS amount
45 WHERE chart.id = acc_trans.chart_id
47 my $sth = $dbh->prepare($query);
48 $sth->execute || $form->dberror($query);
50 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
51 $amount{$ref->{accno}} = $ref->{amount}
55 $query = qq|SELECT accno, description
57 $sth = $dbh->prepare($query);
58 $sth->execute || $form->dberror($query);
61 while (my ($accno, $description) = $sth->fetchrow_array) {
62 $gifi{$accno} = $description;
66 $query = qq|SELECT c.id, c.accno, c.description, c.charttype, c.gifi_accno,
70 $sth = $dbh->prepare($query);
71 $sth->execute || $form->dberror($query);
73 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
74 $ca->{amount} = $amount{$ca->{accno}};
75 $ca->{gifi_description} = $gifi{$ca->{gifi_accno}};
76 if ($ca->{amount} < 0) {
77 $ca->{debit} = $ca->{amount} * -1;
79 $ca->{credit} = $ca->{amount};
81 push @{ $form->{CA} }, $ca;
90 sub all_transactions {
91 my ($self, $myconfig, $form) = @_;
94 my $dbh = $form->dbconnect($myconfig);
97 my $query = qq|SELECT id FROM chart
98 WHERE accno = '$form->{accno}'|;
99 if ($form->{accounttype} eq 'gifi') {
100 $query = qq|SELECT id FROM chart
101 WHERE gifi_accno = '$form->{gifi_accno}'|;
103 my $sth = $dbh->prepare($query);
104 $sth->execute || $form->dberror($query);
107 while (my ($id) = $sth->fetchrow_array) {
114 # build WHERE clause from dates if any
115 if ($form->{fromdate}) {
116 $where .= " AND ac.transdate >= '$form->{fromdate}'";
118 if ($form->{todate}) {
119 $where .= " AND ac.transdate <= '$form->{todate}'";
122 my $sortorder = join ', ', $form->sort_columns(qw(transdate reference description));
123 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
125 # Oracle workaround, use ordinal positions
126 my %ordinal = ( transdate => 4,
129 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
132 if ($form->{accno}) {
133 # get category for account
134 $query = qq|SELECT category
136 WHERE accno = '$form->{accno}'|;
137 $sth = $dbh->prepare($query);
139 $sth->execute || $form->dberror($query);
140 ($form->{category}) = $sth->fetchrow_array;
143 if ($form->{fromdate}) {
144 # get beginning balance
145 $query = qq|SELECT SUM(ac.amount)
146 FROM acc_trans ac, chart c
147 WHERE ac.chart_id = c.id
148 AND c.accno = '$form->{accno}'
149 AND ac.transdate < date '$form->{fromdate}'
151 $sth = $dbh->prepare($query);
153 $sth->execute || $form->dberror($query);
154 ($form->{balance}) = $sth->fetchrow_array;
159 if ($form->{accounttype} eq 'gifi' && $form->{gifi_accno}) {
160 # get category for account
161 $query = qq|SELECT category
163 WHERE gifi_accno = '$form->{gifi_accno}'|;
164 $sth = $dbh->prepare($query);
166 $sth->execute || $form->dberror($query);
167 ($form->{category}) = $sth->fetchrow_array;
170 if ($form->{fromdate}) {
171 # get beginning balance
172 $query = qq|SELECT SUM(ac.amount)
173 FROM acc_trans ac, chart c
174 WHERE ac.chart_id = c.id
175 AND c.gifi_accno = '$form->{gifi_accno}'
176 AND ac.transdate < date '$form->{fromdate}'
178 $sth = $dbh->prepare($query);
180 $sth->execute || $form->dberror($query);
181 ($form->{balance}) = $sth->fetchrow_array;
188 foreach my $id (@id) {
190 # get all transactions
192 SELECT g.id, g.reference, g.description, ac.transdate,
194 ac.amount, 'gl' as charttype
195 FROM gl g, acc_trans ac
197 AND ac.chart_id = $id
198 AND ac.trans_id = g.id
200 SELECT a.id, a.invnumber, c.name, ac.transdate,
202 ac.amount, 'ar' as charttype
203 FROM ar a, acc_trans ac, customer c
205 AND ac.chart_id = $id
206 AND ac.trans_id = a.id
207 AND a.customer_id = c.id
209 SELECT a.id, a.invnumber, v.name, ac.transdate,
211 ac.amount, 'ap' as charttype
212 FROM ap a, acc_trans ac, vendor v
214 AND ac.chart_id = $id
215 AND ac.trans_id = a.id
216 AND a.vendor_id = v.id
220 $query =~ s/UNION ALL$//;
222 ORDER BY $sortorder|;
224 $sth = $dbh->prepare($query);
225 $sth->execute || $form->dberror($query);
227 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
230 if ($ca->{charttype} eq "gl") {
231 $ca->{module} = "gl";
235 if ($ca->{charttype} eq "ap") {
236 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
240 if ($ca->{charttype} eq "ar") {
241 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
244 if ($ca->{amount} < 0) {
245 $ca->{debit} = $ca->{amount} * -1;
248 $ca->{credit} = $ca->{amount};
252 push @{ $form->{CA} }, $ca;