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 #======================================================================
44 $main::lxdebug->enter_sub();
46 my ($self, $myconfig, $form, $chart_id) = @_;
48 my (%amount, $acc_cash_where);
51 my $dbh = $form->dbconnect($myconfig);
53 if ($form->{method} eq "cash") {
54 $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)))) |;
56 $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= (select date_trunc('year', current_date)))";
60 qq|SELECT c.accno, SUM(a.amount) AS amount | .
61 qq|FROM chart c, acc_trans a | .
62 qq|WHERE c.id = a.chart_id | .
66 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
67 $amount{ $ref->{accno} } = $ref->{amount};
70 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
85 comma(tk.startdate::text) AS startdate,
86 comma(tk.taxkey_id::text) AS taxkey,
87 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
88 comma(tx.taxnumber::text) AS taxaccount,
89 comma(tk.pos_ustva::text) AS tk_ustva,
95 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
96 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
99 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
100 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
105 my $sth = prepare_execute_query($form, $dbh, $query);
109 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
110 $ca->{amount} = $amount{ $ca->{accno} };
111 if ($ca->{amount} < 0) {
112 $ca->{debit} = $ca->{amount} * -1;
114 $ca->{credit} = $ca->{amount};
116 push(@{ $form->{CA} }, $ca);
122 $main::lxdebug->leave_sub();
125 sub all_transactions {
126 $main::lxdebug->enter_sub();
128 my ($self, $myconfig, $form) = @_;
130 # connect to database
131 my $dbh = $form->dbconnect($myconfig);
134 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
135 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
140 my $where = qq|1 = 1|;
142 # build WHERE clause from dates if any
143 # if ($form->{fromdate}) {
144 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
146 # if ($form->{todate}) {
147 # $where .= " AND ac.transdate <= '$form->{todate}'";
150 my (@values, @where_values, @subwhere_values, $subwhere);
151 if ($form->{fromdate}) {
152 $where .= qq| AND ac.transdate >= ?|;
153 $subwhere .= qq| AND transdate >= ?|;
154 push(@where_values, conv_date($form->{fromdate}));
155 push(@subwhere_values, conv_date($form->{fromdate}));
158 if ($form->{todate}) {
159 $where .= qq| AND ac.transdate <= ?|;
160 $subwhere .= qq| AND transdate <= ?|;
161 push(@where_values, conv_date($form->{todate}));
162 push(@subwhere_values, conv_date($form->{todate}));
166 my $sortorder = join ', ',
167 $form->sort_columns(qw(transdate reference description));
168 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
170 # Oracle workaround, use ordinal positions
171 my %ordinal = (transdate => 4,
174 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
176 my ($null, $department_id) = split(/--/, $form->{department});
177 my ($dpt_where, $dpt_join, @department_values);
178 if ($department_id) {
179 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
180 $dpt_where = qq| AND t.id = ? |;
181 @department_values = ($department_id);
184 my ($project, @project_values);
185 if ($form->{project_id}) {
186 $project = qq| AND ac.project_id = ? |;
187 @project_values = (conv_i($form->{project_id}));
189 my $acc_cash_where = "";
190 my $ar_cash_where = "";
191 my $ap_cash_where = "";
194 if ($form->{method} eq "cash") {
195 $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<= ?)) |;
197 push(@where_values, conv_date($form->{fromdate}));
198 push(@where_values, conv_date($form->{todate}));
199 push(@where_values, conv_date($form->{fromdate}));
200 push(@where_values, conv_date($form->{todate}));
201 push(@where_values, conv_date($form->{fromdate}));
202 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 = ?|;
227 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
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)|;
231 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
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)|;
235 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
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)|;
238 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
241 $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)|;
243 ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
246 $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)|;
248 ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
250 $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)|;
252 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
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\./;
320 # strict check 20.10.2009 sschoeling
321 # the previous version contained the var $ar_ap_cash_where, which was ONLY set by
322 # RP->trial_balance() I tried to figure out which bizarre flow through the
323 # program would happen to set that var, so that it would be used here later on,
324 # (which would be nonsense, since you would normally load chart before
325 # claculating balance of said charts) and then decided that any mechanic that
326 # complex should fail anyway.
328 # if anyone is missing a time check on charts, that broke arounf the time
329 # trial_balance was rewritten, this would be it
334 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
335 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
336 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 § .
338 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
339 qq|JOIN parts p ON (ac.parts_id = p.id) | .
340 qq|JOIN customer c ON (a.customer_id = c.id) | .
342 qq|WHERE p.income_accno_id = ? | .
349 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
350 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
351 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 § .
353 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
354 qq|JOIN parts p ON (ac.parts_id = p.id) | .
355 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
357 qq|WHERE p.expense_accno_id = ? | .
363 $id, @department_values, @project_values,
364 $id, @department_values, @project_values);
366 $fromdate_where =~ s/a\./ac\./;
367 $todate_where =~ s/a\./ac\./;
371 $union = qq|UNION ALL|;
374 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
376 $query .= qq|ORDER BY $sort|;
377 my $sth = prepare_execute_query($form, $dbh, $query, @values);
379 #get detail information for each transaction
382 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
383 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
384 my $trans_sth = $dbh->prepare($trans_query);
387 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
389 if ($ca->{module} eq "ap") {
390 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
394 if ($ca->{module} eq "ar") {
395 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
398 if ($ca->{amount} < 0) {
399 $ca->{debit} = $ca->{amount} * -1;
402 $ca->{credit} = $ca->{amount};
406 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
408 #get detail information for this transaction
409 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
410 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
411 while (my $trans = $trans_sth->fetchrow_hashref("NAME_lc")) {
412 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
413 if ($trans->{amount} < 0) {
414 $trans->{debit} = $trans->{amount} * -1;
415 $trans->{credit} = 0;
417 $trans->{credit} = $trans->{amount};
420 push(@{ $ca->{GEGENKONTO} }, $trans);
426 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
427 # $ca->{index} = $ca->{$form->{sort}};
428 push(@{ $form->{CA} }, $ca);
435 $main::lxdebug->leave_sub();