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}'";
147 $AR_PAID = qq|AND ac.trans_id IN
151 JOIN chart ON (chart_id = id)
152 WHERE link LIKE '%AR_paid%'
155 $AP_PAID = qq|AND ac.trans_id IN
159 JOIN chart ON (chart_id = id)
160 WHERE link LIKE '%AP_paid%'
167 $glwhere = ""; # note! gl will be aliased as "a" later...
169 my $sortorder = join ', ',
170 $form->sort_columns(qw(transdate reference description));
171 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
173 # Oracle workaround, use ordinal positions
174 my %ordinal = (transdate => 4,
177 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
179 my ($null, $department_id) = split /--/, $form->{department};
182 if ($department_id) {
184 JOIN department t ON (t.id = a.department_id)
187 AND t.id = $department_id
192 if ($form->{project_id}) {
194 AND ac.project_id = $form->{project_id}
198 if ($form->{accno} || $form->{gifi_accno}) {
200 # get category for account
201 $query = qq|SELECT c.category
203 WHERE c.accno = '$form->{accno}'|;
205 if ($form->{accounttype} eq 'gifi') {
206 $query = qq|SELECT c.category
208 WHERE c.gifi_accno = '$form->{gifi_accno}'
209 AND c.charttype = 'A'|;
212 $sth = $dbh->prepare($query);
214 $sth->execute || $form->dberror($query);
215 ($form->{category}) = $sth->fetchrow_array;
218 if ($form->{fromdate}) {
220 # get beginning balance
221 $query = qq|SELECT SUM(ac.amount)
223 JOIN chart c ON (ac.chart_id = c.id)
225 WHERE c.accno = '$form->{accno}'
226 AND ac.transdate < '$form->{fromdate}'
231 if ($form->{project_id}) {
237 SELECT SUM(ac.qty * ac.sellprice)
239 JOIN ar a ON (ac.trans_id = a.id)
240 JOIN parts p ON (ac.parts_id = p.id)
241 JOIN chart c ON (p.income_accno_id = c.id)
243 WHERE c.accno = '$form->{accno}'
244 AND a.transdate < '$form->{fromdate}'
251 SELECT SUM(ac.qty * ac.sellprice)
253 JOIN ap a ON (ac.trans_id = a.id)
254 JOIN parts p ON (ac.parts_id = p.id)
255 JOIN chart c ON (p.expense_accno_id = c.id)
257 WHERE c.accno = '$form->{accno}'
258 AND a.transdate < '$form->{fromdate}'
266 if ($form->{accounttype} eq 'gifi') {
267 $query = qq|SELECT SUM(ac.amount)
269 JOIN chart c ON (ac.chart_id = c.id)
271 WHERE c.gifi_accno = '$form->{gifi_accno}'
272 AND ac.transdate < '$form->{fromdate}'
277 if ($form->{project_id}) {
283 SELECT SUM(ac.qty * ac.sellprice)
285 JOIN ar a ON (ac.trans_id = a.id)
286 JOIN parts p ON (ac.parts_id = p.id)
287 JOIN chart c ON (p.income_accno_id = c.id)
289 WHERE c.gifi_accno = '$form->{gifi_accno}'
290 AND a.transdate < '$form->{fromdate}'
297 SELECT SUM(ac.qty * ac.sellprice)
299 JOIN ap a ON (ac.trans_id = a.id)
300 JOIN parts p ON (ac.parts_id = p.id)
301 JOIN chart c ON (p.expense_accno_id = c.id)
303 WHERE c.gifi_accno = '$form->{gifi_accno}'
304 AND a.transdate < '$form->{fromdate}'
313 $sth = $dbh->prepare($query);
315 $sth->execute || $form->dberror($query);
316 ($form->{balance}) = $sth->fetchrow_array;
324 foreach my $id (@id) {
327 # Postgres is really picky about the order of implicit CROSS JOINs with ','
328 # if you alias the tables and want to use the alias later in another JOIN.
329 # the alias you want to use has to be the most recent in the list, otherwise
330 # Postgres will overwrite the alias internally and complain.
331 # For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
332 # Don't change this, and if you do, substitute the ',' with CROSS JOIN
333 # ... that also works.
335 # get all transactions
337 SELECT a.id, a.reference, a.description, ac.transdate,
338 $false AS invoice, ac.amount, 'gl' as module
339 FROM acc_trans ac, gl a $dpt_join
344 AND ac.chart_id = $id
345 AND ac.trans_id = a.id
347 SELECT a.id, a.invnumber, c.name, ac.transdate,
348 a.invoice, ac.amount, 'ar' as module
349 FROM acc_trans ac, customer c, ar a $dpt_join
353 AND ac.chart_id = $id
354 AND ac.trans_id = a.id
356 AND a.customer_id = c.id
358 SELECT a.id, a.invnumber, v.name, ac.transdate,
359 a.invoice, ac.amount, 'ap' as module
360 FROM acc_trans ac, vendor v, ap a $dpt_join
364 AND ac.chart_id = $id
365 AND ac.trans_id = a.id
367 AND a.vendor_id = v.id
373 if ($form->{project_id}) {
375 $fromdate_where =~ s/ac\./a\./;
376 $todate_where =~ s/ac\./a\./;
382 SELECT a.id, a.invnumber, c.name, a.transdate,
383 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
385 JOIN invoice ac ON (ac.trans_id = a.id)
386 JOIN parts p ON (ac.parts_id = p.id)
387 JOIN customer c ON (a.customer_id = c.id)
389 WHERE p.income_accno_id = $id
397 SELECT a.id, a.invnumber, v.name, a.transdate,
398 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
400 JOIN invoice ac ON (ac.trans_id = a.id)
401 JOIN parts p ON (ac.parts_id = p.id)
402 JOIN vendor v ON (a.vendor_id = v.id)
404 WHERE p.expense_accno_id = $id
411 $fromdate_where =~ s/a\./ac\./;
412 $todate_where =~ s/a\./ac\./;
422 ORDER BY $sortorder|;
424 $sth = $dbh->prepare($query);
425 $sth->execute || $form->dberror($query);
427 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
430 if ($ca->{module} eq "gl") {
431 $ca->{module} = "gl";
435 if ($ca->{module} eq "ap") {
436 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
440 if ($ca->{module} eq "ar") {
441 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
444 if ($ca->{amount} < 0) {
445 $ca->{debit} = $ca->{amount} * -1;
448 $ca->{credit} = $ca->{amount};
452 push @{ $form->{CA} }, $ca;
459 $main::lxdebug->leave_sub();