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};
76 comma(tk.startdate) AS startdate,
77 comma(tk.taxkey_id) AS taxkey,
78 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
79 comma(tx.taxnumber) AS taxaccount,
80 comma(tk.pos_ustva) AS tk_ustva,
86 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
87 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
88 GROUP BY c.accno, c.id, c.description, c.charttype,
89 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
94 $sth = $dbh->prepare($query);
95 $sth->execute || $form->dberror($query);
97 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
98 $ca->{amount} = $amount{ $ca->{accno} };
99 if ($ca->{amount} < 0) {
100 $ca->{debit} = $ca->{amount} * -1;
102 $ca->{credit} = $ca->{amount};
104 push @{ $form->{CA} }, $ca;
110 $main::lxdebug->leave_sub();
113 sub all_transactions {
114 $main::lxdebug->enter_sub();
116 my ($self, $myconfig, $form) = @_;
118 # connect to database
119 my $dbh = $form->dbconnect($myconfig);
122 my $query = qq|SELECT c.id FROM chart c
123 WHERE c.accno = '$form->{accno}'|;
124 my $sth = $dbh->prepare($query);
125 $sth->execute || $form->dberror($query);
128 while (my ($id) = $sth->fetchrow_array) {
138 # build WHERE clause from dates if any
139 # if ($form->{fromdate}) {
140 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
142 # if ($form->{todate}) {
143 # $where .= " AND ac.transdate <= '$form->{todate}'";
146 if ($form->{fromdate}) {
147 $fromto = " AND ac.transdate >= '$form->{fromdate}'";
148 $subwhere .= " AND transdate >= '$form->{fromdate}'";
149 $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
152 if ($form->{todate}) {
153 $fromto .= " AND ac.transdate <= '$form->{todate}'";
154 $subwhere .= " AND transdate <= '$form->{todate}'";
155 $glwhere .= " AND ac.transdate <= '$form->{todate}'";
162 $glwhere = ""; # note! gl will be aliased as "a" later...
163 my $sortorder = join ', ',
164 $form->sort_columns(qw(transdate reference description));
165 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
167 # Oracle workaround, use ordinal positions
168 my %ordinal = (transdate => 4,
171 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
173 my ($null, $department_id) = split /--/, $form->{department};
176 if ($department_id) {
178 JOIN department t ON (t.id = a.department_id)
181 AND t.id = $department_id
186 if ($form->{project_id}) {
188 AND ac.project_id = $form->{project_id}
192 if ($form->{accno}) {
194 # get category for account
195 $query = qq|SELECT c.category
197 WHERE c.accno = '$form->{accno}'|;
199 $sth = $dbh->prepare($query);
201 $sth->execute || $form->dberror($query);
202 ($form->{category}) = $sth->fetchrow_array;
205 if ($form->{fromdate}) {
207 # get beginning balance
208 $query = qq|SELECT SUM(ac.amount)
210 JOIN chart c ON (ac.chart_id = c.id)
212 WHERE c.accno = '$form->{accno}'
213 AND ac.transdate < '$form->{fromdate}'
218 if ($form->{project_id}) {
224 SELECT SUM(ac.qty * ac.sellprice)
226 JOIN ar a ON (ac.trans_id = a.id)
227 JOIN parts p ON (ac.parts_id = p.id)
228 JOIN chart c ON (p.income_accno_id = c.id)
230 WHERE c.accno = '$form->{accno}'
231 AND a.transdate < '$form->{fromdate}'
238 SELECT SUM(ac.qty * ac.sellprice)
240 JOIN ap a ON (ac.trans_id = a.id)
241 JOIN parts p ON (ac.parts_id = p.id)
242 JOIN chart c ON (p.expense_accno_id = c.id)
244 WHERE c.accno = '$form->{accno}'
245 AND a.transdate < '$form->{fromdate}'
253 $sth = $dbh->prepare($query);
255 $sth->execute || $form->dberror($query);
256 ($form->{balance}) = $sth->fetchrow_array;
264 foreach my $id (@id) {
267 # Postgres is really picky about the order of implicit CROSS JOINs with ','
268 # if you alias the tables and want to use the alias later in another JOIN.
269 # the alias you want to use has to be the most recent in the list, otherwise
270 # Postgres will overwrite the alias internally and complain.
271 # For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
272 # Don't change this, and if you do, substitute the ',' with CROSS JOIN
273 # ... that also works.
275 # get all transactions
277 SELECT a.id, a.reference, a.description, ac.transdate,
278 $false AS invoice, ac.amount, 'gl' as module
279 FROM acc_trans ac, gl a $dpt_join
284 AND ac.chart_id = $id
285 AND ac.trans_id = a.id
287 SELECT a.id, a.invnumber, c.name, ac.transdate,
288 a.invoice, ac.amount, 'ar' as module
289 FROM acc_trans ac, customer c, ar a $dpt_join
293 AND ac.chart_id = $id
294 AND ac.trans_id = a.id
296 AND a.customer_id = c.id
298 SELECT a.id, a.invnumber, v.name, ac.transdate,
299 a.invoice, ac.amount, 'ap' as module
300 FROM acc_trans ac, vendor v, ap a $dpt_join
304 AND ac.chart_id = $id
305 AND ac.trans_id = a.id
307 AND a.vendor_id = v.id
313 if ($form->{project_id}) {
315 $fromdate_where =~ s/ac\./a\./;
316 $todate_where =~ s/ac\./a\./;
322 SELECT a.id, a.invnumber, c.name, a.transdate,
323 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
325 JOIN invoice ac ON (ac.trans_id = a.id)
326 JOIN parts p ON (ac.parts_id = p.id)
327 JOIN customer c ON (a.customer_id = c.id)
329 WHERE p.income_accno_id = $id
337 SELECT a.id, a.invnumber, v.name, a.transdate,
338 a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
340 JOIN invoice ac ON (ac.trans_id = a.id)
341 JOIN parts p ON (ac.parts_id = p.id)
342 JOIN vendor v ON (a.vendor_id = v.id)
344 WHERE p.expense_accno_id = $id
351 $fromdate_where =~ s/a\./ac\./;
352 $todate_where =~ s/a\./ac\./;
362 ORDER BY $sortorder|;
364 $sth = $dbh->prepare($query);
365 $sth->execute || $form->dberror($query);
367 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
370 if ($ca->{module} eq "gl") {
371 $ca->{module} = "gl";
375 if ($ca->{module} eq "ap") {
376 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
380 if ($ca->{module} eq "ar") {
381 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
384 if ($ca->{amount} < 0) {
385 $ca->{debit} = $ca->{amount} * -1;
388 $ca->{credit} = $ca->{amount};
392 push @{ $form->{CA} }, $ca;
399 $main::lxdebug->leave_sub();