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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form, $chart_id) = @_;
49 my $dbh = $form->dbconnect($myconfig);
51 if ($form->{method} eq "cash") {
52 $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>=(select date_trunc('year', current_date)) UNION SELECT id FROM ap WHERE datepaid>=(select date_trunc('year', current_date)) UNION SELECT id FROM gl WHERE transdate>=(select date_trunc('year', current_date)))) |;
54 $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= (select date_trunc('year', current_date)))";
58 qq|SELECT c.accno, SUM(a.amount) AS amount | .
59 qq|FROM chart c, acc_trans a | .
60 qq|WHERE c.id = a.chart_id | .
63 my $sth = $dbh->prepare($query);
64 $sth->execute || $form->dberror($query);
66 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
67 $amount{ $ref->{accno} } = $ref->{amount};
71 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
86 comma(tk.startdate::text) AS startdate,
87 comma(tk.taxkey_id::text) AS taxkey,
88 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
89 comma(tx.taxnumber::text) AS taxaccount,
90 comma(tk.pos_ustva::text) AS tk_ustva,
96 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
97 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
100 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
101 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
106 my $sth = prepare_execute_query($form, $dbh, $query);
110 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
111 $ca->{amount} = $amount{ $ca->{accno} };
112 if ($ca->{amount} < 0) {
113 $ca->{debit} = $ca->{amount} * -1;
115 $ca->{credit} = $ca->{amount};
117 push(@{ $form->{CA} }, $ca);
123 $main::lxdebug->leave_sub();
126 sub all_transactions {
127 $main::lxdebug->enter_sub();
129 my ($self, $myconfig, $form) = @_;
131 # connect to database
132 my $dbh = $form->dbconnect($myconfig);
135 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
136 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
141 my $where = qq|1 = 1|;
143 # build WHERE clause from dates if any
144 # if ($form->{fromdate}) {
145 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
147 # if ($form->{todate}) {
148 # $where .= " AND ac.transdate <= '$form->{todate}'";
151 my (@values, @where_values, @subwhere_values);
152 if ($form->{fromdate}) {
153 $where .= qq| AND ac.transdate >= ?|;
154 $subwhere .= qq| AND transdate >= ?|;
155 push(@where_values, conv_date($form->{fromdate}));
156 push(@subwhere_values, conv_date($form->{fromdate}));
159 if ($form->{todate}) {
160 $where .= qq| AND ac.transdate <= ?|;
161 $subwhere .= qq| AND transdate <= ?|;
162 push(@where_values, conv_date($form->{todate}));
163 push(@subwhere_values, conv_date($form->{todate}));
167 my $sortorder = join ', ',
168 $form->sort_columns(qw(transdate reference description));
169 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
171 # Oracle workaround, use ordinal positions
172 my %ordinal = (transdate => 4,
175 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
177 my ($null, $department_id) = split(/--/, $form->{department});
178 my ($dpt_where, $dpt_join, @department_values);
179 if ($department_id) {
180 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
181 $dpt_where = qq| AND t.id = ? |;
182 @department_values = ($department_id);
185 my ($project, @project_values);
186 if ($form->{project_id}) {
187 $project = qq| AND ac.project_id = ? |;
188 @project_values = (conv_i($form->{project_id}));
190 my $acc_cash_where = "";
191 my $ar_cash_where = "";
192 my $ap_cash_where = "";
195 if ($form->{method} eq "cash") {
196 $where = qq| (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>= ? AND datepaid<= ? UNION SELECT id FROM ap WHERE datepaid>= ? AND datepaid<= ? UNION SELECT id FROM gl WHERE transdate>= ? AND transdate<= ?)) |;
198 push(@where_values, conv_date($form->{fromdate}));
199 push(@where_values, conv_date($form->{todate}));
200 push(@where_values, conv_date($form->{fromdate}));
201 push(@where_values, conv_date($form->{todate}));
202 push(@where_values, conv_date($form->{fromdate}));
203 push(@where_values, conv_date($form->{todate}));
206 if ($form->{accno}) {
208 # get category for account
209 $query = qq|SELECT category FROM chart WHERE accno = ?|;
210 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
212 if ($form->{fromdate}) {
213 # get beginning balances
215 qq|SELECT SUM(ac.amount) AS amount
217 JOIN chart c ON (ac.chart_id = c.id)
218 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
222 ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
224 # get last transaction date
225 my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
226 $query = qq|SELECT max(ac.transdate) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id) WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) $todate AND c.accno = ? AND $where|;
227 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno}, @where_values);
230 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) AND ac.transdate < ? AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
231 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno}, @where_values);
234 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) AND ac.transdate < ? AND c.accno = ? AND ac.amount < 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
235 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno}, @where_values);
237 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) AND ac.transdate < ? AND c.accno = ? AND ac.amount > 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
238 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno}, @where_values);
241 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
242 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) $todate AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
243 ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno}, @where_values);
246 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
247 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) $todate AND c.accno = ? AND ac.amount < 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
248 ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno}, @where_values);
250 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
251 $query = qq|SELECT sum(ac.amount) FROM acc_trans ac LEFT JOIN chart c ON (ac.chart_id = c.id)WHERE ((select date_trunc('year', ac.transdate::date)) >= (select date_trunc('year', ?::date))) $todate AND c.accno = ? AND ac.amount > 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) AND $where|;
252 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno}, @where_values);
260 foreach my $id (@id) {
262 # NOTE: Postgres is really picky about the order of implicit CROSS
263 # JOINs with ',' if you alias the tables and want to use the
264 # alias later in another JOIN. the alias you want to use has to
265 # be the most recent in the list, otherwise Postgres will
266 # overwrite the alias internally and complain. For this reason,
267 # in the next 3 SELECTs, the 'a' alias is last in the list.
268 # Don't change this, and if you do, substitute the ',' with CROSS
269 # JOIN ... that also works.
271 # get all transactions
273 qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
274 qq| $false AS invoice, ac.amount, 'gl' as module, | .
275 qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
276 qq|FROM acc_trans ac, gl a | .
278 qq|WHERE | . $where . $dpt_where . $project .
279 qq| AND ac.chart_id = ? | .
280 qq| AND ac.trans_id = a.id | .
281 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
285 qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
286 qq| a.invoice, ac.amount, 'ar' as module, | .
287 qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
288 qq|FROM acc_trans ac, customer c, ar a | .
290 qq|WHERE | . $where . $dpt_where . $project .
291 qq| AND ac.chart_id = ? | .
292 qq| AND ac.trans_id = a.id | .
293 qq| AND a.customer_id = c.id | .
294 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
298 qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
299 qq| a.invoice, ac.amount, 'ap' as module, | .
300 qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo, ac.source || ' ' || ac.memo AS memo § .
301 qq|FROM acc_trans ac, vendor v, ap a | .
303 qq|WHERE | . $where . $dpt_where . $project .
304 qq| AND ac.chart_id = ? | .
305 qq| AND ac.trans_id = a.id | .
306 qq| AND a.vendor_id = v.id |;
307 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
309 @where_values, @department_values, @project_values, $id,
310 @where_values, @department_values, @project_values, $id,
311 @where_values, @department_values, @project_values, $id);
313 $union = qq|UNION ALL |;
315 if ($form->{project_id}) {
317 $fromdate_where =~ s/ac\./a\./;
318 $todate_where =~ s/ac\./a\./;
323 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
324 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
325 qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo § .
327 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
328 qq|JOIN parts p ON (ac.parts_id = p.id) | .
329 qq|JOIN customer c ON (a.customer_id = c.id) | .
331 qq|WHERE p.income_accno_id = ? | .
339 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
340 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
341 qq§(SELECT accno||'--'||rate FROM tax LEFT JOIN chart ON (tax.chart_id=chart.id) WHERE tax.id = (SELECT tax_id FROM taxkeys WHERE taxkey_id = ac.taxkey AND taxkeys.startdate <= ac.transdate ORDER BY taxkeys.startdate DESC LIMIT 1)) AS taxinfo § .
343 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
344 qq|JOIN parts p ON (ac.parts_id = p.id) | .
345 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
347 qq|WHERE p.expense_accno_id = ? | .
354 $id, @department_values, @project_values,
355 $id, @department_values, @project_values);
357 $fromdate_where =~ s/a\./ac\./;
358 $todate_where =~ s/a\./ac\./;
362 $union = qq|UNION ALL|;
365 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
367 $query .= qq|ORDER BY $sort|;
368 $sth = prepare_execute_query($form, $dbh, $query, @values);
370 #get detail information for each transaction
373 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
374 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
375 my $trans_sth = $dbh->prepare($trans_query);
378 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
380 if ($ca->{module} eq "ap") {
381 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
385 if ($ca->{module} eq "ar") {
386 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
389 if ($ca->{amount} < 0) {
390 $ca->{debit} = $ca->{amount} * -1;
393 $ca->{credit} = $ca->{amount};
397 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
399 #get detail information for this transaction
400 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
401 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
402 while (my $trans = $trans_sth->fetchrow_hashref(NAME_lc)) {
403 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
404 if ($trans->{amount} < 0) {
405 $trans->{debit} = $trans->{amount} * -1;
406 $trans->{credit} = 0;
408 $trans->{credit} = $trans->{amount};
411 push(@{ $ca->{GEGENKONTO} }, $trans);
417 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
418 # $ca->{index} = $ca->{$form->{sort}};
419 push(@{ $form->{CA} }, $ca);
426 $main::lxdebug->leave_sub();