1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
33 # DS. 2000-07-04 Created
35 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
48 my $dbh = $form->dbconnect($myconfig);
50 my $query = qq|SELECT c.accno,
51 SUM(a.amount) AS amount
52 FROM chart c, acc_trans a
53 WHERE c.id = a.chart_id
55 my $sth = $dbh->prepare($query);
56 $sth->execute || $form->dberror($query);
58 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
59 $amount{ $ref->{accno} } = $ref->{amount};
63 $query = qq|SELECT accno, description
65 $sth = $dbh->prepare($query);
66 $sth->execute || $form->dberror($query);
69 while (my ($accno, $description) = $sth->fetchrow_array) {
70 $gifi{$accno} = $description;
74 $query = qq|SELECT c.id, c.accno, c.description, c.charttype, c.gifi_accno,
78 $sth = $dbh->prepare($query);
79 $sth->execute || $form->dberror($query);
81 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
82 $ca->{amount} = $amount{ $ca->{accno} };
83 $ca->{gifi_description} = $gifi{ $ca->{gifi_accno} };
84 if ($ca->{amount} < 0) {
85 $ca->{debit} = $ca->{amount} * -1;
87 $ca->{credit} = $ca->{amount};
89 push @{ $form->{CA} }, $ca;
95 $main::lxdebug->leave_sub();
98 sub all_transactions {
99 $main::lxdebug->enter_sub();
101 my ($self, $myconfig, $form) = @_;
103 # connect to database
104 my $dbh = $form->dbconnect($myconfig);
107 my $query = qq|SELECT c.id FROM chart c
108 WHERE c.accno = '$form->{accno}'|;
109 if ($form->{accounttype} eq 'gifi') {
110 $query = qq|SELECT c.id FROM chart c
111 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
113 my $sth = $dbh->prepare($query);
114 $sth->execute || $form->dberror($query);
117 while (my ($id) = $sth->fetchrow_array) {
127 # build WHERE clause from dates if any
128 # if ($form->{fromdate}) {
129 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
131 # if ($form->{todate}) {
132 # $where .= " AND ac.transdate <= '$form->{todate}'";
135 if ($form->{fromdate}) {
136 $fromto = " AND ac.transdate >= '$form->{fromdate}'";
137 $subwhere .= " AND transdate >= '$form->{fromdate}'";
138 $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
141 if ($form->{todate}) {
142 $fromto .= " AND ac.transdate <= '$form->{todate}'";
143 $subwhere .= " AND transdate <= '$form->{todate}'";
144 $glwhere .= " AND ac.transdate <= '$form->{todate}'";
151 $glwhere = ""; # note! gl will be aliased as "a" later...
152 my $sortorder = join ', ',
153 $form->sort_columns(qw(transdate reference description));
154 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
156 # Oracle workaround, use ordinal positions
157 my %ordinal = (transdate => 4,
160 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
162 my ($null, $department_id) = split /--/, $form->{department};
165 if ($department_id) {
167 JOIN department t ON (t.id = a.department_id)
170 AND t.id = $department_id
175 if ($form->{project_id}) {
177 AND ac.project_id = $form->{project_id}
181 if ($form->{accno} || $form->{gifi_accno}) {
183 # get category for account
184 $query = qq|SELECT c.category
186 WHERE c.accno = '$form->{accno}'|;
188 if ($form->{accounttype} eq 'gifi') {
189 $query = qq|SELECT c.category
191 WHERE c.gifi_accno = '$form->{gifi_accno}'
192 AND c.charttype = 'A'|;
195 $sth = $dbh->prepare($query);
197 $sth->execute || $form->dberror($query);
198 ($form->{category}) = $sth->fetchrow_array;
201 if ($form->{fromdate}) {
203 # get beginning balance
204 $query = qq|SELECT SUM(ac.amount)
206 JOIN chart c ON (ac.chart_id = c.id)
208 WHERE c.accno = '$form->{accno}'
209 AND ac.transdate < '$form->{fromdate}'
214 if ($form->{project_id}) {
220 SELECT SUM(ac.qty * ac.sellprice)
222 JOIN ar a ON (ac.trans_id = a.id)
223 JOIN parts p ON (ac.parts_id = p.id)
224 JOIN chart c ON (p.income_accno_id = c.id)
226 WHERE c.accno = '$form->{accno}'
227 AND a.transdate < '$form->{fromdate}'
234 SELECT SUM(ac.qty * ac.sellprice)
236 JOIN ap a ON (ac.trans_id = a.id)
237 JOIN parts p ON (ac.parts_id = p.id)
238 JOIN chart c ON (p.expense_accno_id = c.id)
240 WHERE c.accno = '$form->{accno}'
241 AND a.transdate < '$form->{fromdate}'
249 if ($form->{accounttype} eq 'gifi') {
250 $query = qq|SELECT SUM(ac.amount)
252 JOIN chart c ON (ac.chart_id = c.id)
254 WHERE c.gifi_accno = '$form->{gifi_accno}'
255 AND ac.transdate < '$form->{fromdate}'
260 if ($form->{project_id}) {
266 SELECT SUM(ac.qty * ac.sellprice)
268 JOIN ar a ON (ac.trans_id = a.id)
269 JOIN parts p ON (ac.parts_id = p.id)
270 JOIN chart c ON (p.income_accno_id = c.id)
272 WHERE c.gifi_accno = '$form->{gifi_accno}'
273 AND a.transdate < '$form->{fromdate}'
280 SELECT SUM(ac.qty * ac.sellprice)
282 JOIN ap a ON (ac.trans_id = a.id)
283 JOIN parts p ON (ac.parts_id = p.id)
284 JOIN chart c ON (p.expense_accno_id = c.id)
286 WHERE c.gifi_accno = '$form->{gifi_accno}'
287 AND a.transdate < '$form->{fromdate}'
296 $sth = $dbh->prepare($query);
298 $sth->execute || $form->dberror($query);
299 ($form->{balance}) = $sth->fetchrow_array;
307 foreach my $id (@id) {
310 # Postgres is really picky about the order of implicit CROSS JOINs with ','
311 # if you alias the tables and want to use the alias later in another JOIN.
312 # the alias you want to use has to be the most recent in the list, otherwise
313 # Postgres will overwrite the alias internally and complain.
314 # For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
315 # Don't change this, and if you do, substitute the ',' with CROSS JOIN
316 # ... that also works.
318 # get all transactions
320 SELECT a.id, a.reference, a.description, ac.transdate,
321 $false AS invoice, ac.amount, 'gl' as module
322 FROM acc_trans ac, gl a $dpt_join
327 AND ac.chart_id = $id
328 AND ac.trans_id = a.id
330 SELECT a.id, a.invnumber, c.name, ac.transdate,
331 a.invoice, ac.amount, 'ar' as module
332 FROM acc_trans ac, customer c, ar a $dpt_join
336 AND ac.chart_id = $id
337 AND ac.trans_id = a.id
339 AND a.customer_id = c.id
341 SELECT a.id, a.invnumber, v.name, ac.transdate,
342 a.invoice, ac.amount, 'ap' as module
343 FROM acc_trans ac, vendor v, ap a $dpt_join
347 AND ac.chart_id = $id
348 AND ac.trans_id = a.id
350 AND a.vendor_id = v.id
356 if ($form->{project_id}) {
358 $fromdate_where =~ s/ac\./a\./;
359 $todate_where =~ s/ac\./a\./;
365 SELECT a.id, a.invnumber, c.name, a.transdate,
366 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
368 JOIN invoice ac ON (ac.trans_id = a.id)
369 JOIN parts p ON (ac.parts_id = p.id)
370 JOIN customer c ON (a.customer_id = c.id)
372 WHERE p.income_accno_id = $id
380 SELECT a.id, a.invnumber, v.name, a.transdate,
381 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
383 JOIN invoice ac ON (ac.trans_id = a.id)
384 JOIN parts p ON (ac.parts_id = p.id)
385 JOIN vendor v ON (a.vendor_id = v.id)
387 WHERE p.expense_accno_id = $id
394 $fromdate_where =~ s/a\./ac\./;
395 $todate_where =~ s/a\./ac\./;
405 ORDER BY $sortorder|;
407 $sth = $dbh->prepare($query);
408 $sth->execute || $form->dberror($query);
410 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
413 if ($ca->{module} eq "gl") {
414 $ca->{module} = "gl";
418 if ($ca->{module} eq "ap") {
419 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
423 if ($ca->{module} eq "ar") {
424 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
427 if ($ca->{amount} < 0) {
428 $ca->{debit} = $ca->{amount} * -1;
431 $ca->{credit} = $ca->{amount};
435 push @{ $form->{CA} }, $ca;
442 $main::lxdebug->leave_sub();