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 #======================================================================
45 $main::lxdebug->enter_sub();
47 my ($self, $myconfig, $form, $chart_id) = @_;
49 my (%amount, $acc_cash_where);
52 my $dbh = $form->dbconnect($myconfig);
54 # bug 1071 Warum sollte bei Erreichen eines neuen Jahres die Kontenübersicht nur noch die
55 # bereits bebuchten Konten anzeigen?
56 # Folgende Erweiterung:
57 # 1.) Gehe zurück bis zu dem Datum an dem die Bücher geschlossen wurden
58 # 2.) Falls die Bücher noch nie geschlossen wurden, gehe zurück bis zum Bearbeitungsstart
59 # COALESCE((SELECT closedto FROM defaults),(SELECT itime FROM defaults))
61 my $closedto_sql = "COALESCE((SELECT closedto FROM defaults),(SELECT itime FROM defaults))";
63 if ($form->{method} eq "cash") { # EÜR
64 $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>= $closedto_sql
65 UNION SELECT id FROM ap WHERE datepaid>= $closedto_sql
66 UNION SELECT id FROM gl WHERE transdate>= $closedto_sql
68 } else { # Bilanzierung
69 $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= $closedto_sql) ";
73 qq|SELECT c.accno, SUM(a.amount) AS amount | .
74 qq|FROM chart c, acc_trans a | .
75 qq|WHERE c.id = a.chart_id | .
79 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
80 $amount{ $ref->{accno} } = $ref->{amount};
83 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
98 comma(tk.startdate::text) AS startdate,
99 comma(tk.taxkey_id::text) AS taxkey,
100 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
101 comma(tx.taxnumber::text) AS taxaccount,
102 comma(tk.pos_ustva::text) AS tk_ustva,
108 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
109 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
112 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
113 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
118 my $sth = prepare_execute_query($form, $dbh, $query);
122 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
123 $ca->{amount} = $amount{ $ca->{accno} };
124 if ($ca->{amount} < 0) {
125 $ca->{debit} = $ca->{amount} * -1;
127 $ca->{credit} = $ca->{amount};
129 push(@{ $form->{CA} }, $ca);
135 $main::lxdebug->leave_sub();
138 sub all_transactions {
139 $main::lxdebug->enter_sub();
141 my ($self, $myconfig, $form) = @_;
143 # connect to database
144 my $dbh = $form->dbconnect($myconfig);
147 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
148 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
153 my $where = qq|1 = 1|;
155 # build WHERE clause from dates if any
156 # if ($form->{fromdate}) {
157 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
159 # if ($form->{todate}) {
160 # $where .= " AND ac.transdate <= '$form->{todate}'";
163 my (@values, @where_values, @subwhere_values, $subwhere);
164 if ($form->{fromdate}) {
165 $where .= qq| AND ac.transdate >= ?|;
166 $subwhere .= qq| AND transdate >= ?|;
167 push(@where_values, conv_date($form->{fromdate}));
168 push(@subwhere_values, conv_date($form->{fromdate}));
171 if ($form->{todate}) {
172 $where .= qq| AND ac.transdate <= ?|;
173 $subwhere .= qq| AND transdate <= ?|;
174 push(@where_values, conv_date($form->{todate}));
175 push(@subwhere_values, conv_date($form->{todate}));
179 my $sortorder = join ', ',
180 $form->sort_columns(qw(transdate reference description));
181 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
183 # Oracle workaround, use ordinal positions
184 my %ordinal = (transdate => 4,
187 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
189 my ($null, $department_id) = split(/--/, $form->{department});
190 my ($dpt_where, $dpt_join, @department_values);
191 if ($department_id) {
192 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
193 $dpt_where = qq| AND t.id = ? |;
194 @department_values = ($department_id);
197 my ($project, @project_values);
198 if ($form->{project_id}) {
199 $project = qq| AND ac.project_id = ? |;
200 @project_values = (conv_i($form->{project_id}));
202 my $acc_cash_where = "";
203 my $ar_cash_where = "";
204 my $ap_cash_where = "";
207 if ($form->{method} eq "cash") {
208 $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<= ?)) |;
210 push(@where_values, conv_date($form->{fromdate}));
211 push(@where_values, conv_date($form->{todate}));
212 push(@where_values, conv_date($form->{fromdate}));
213 push(@where_values, conv_date($form->{todate}));
214 push(@where_values, conv_date($form->{fromdate}));
215 push(@where_values, conv_date($form->{todate}));
219 if ($form->{accno}) {
221 # get category for account
222 $query = qq|SELECT category FROM chart WHERE accno = ?|;
223 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
225 if ($form->{fromdate}) {
226 # get beginning balances
228 qq|SELECT SUM(ac.amount) AS amount
230 JOIN chart c ON (ac.chart_id = c.id)
231 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
235 ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
237 # get last transaction date
238 my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
239 $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 = ?|;
240 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
243 $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)|;
244 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
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))) AND ac.transdate < ? AND c.accno = ? AND ac.amount < 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
248 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
250 $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)|;
251 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
254 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
255 $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)|;
256 ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
259 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
260 $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)|;
261 ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
263 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
264 $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)|;
265 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
273 foreach my $id (@id) {
275 # NOTE: Postgres is really picky about the order of implicit CROSS
276 # JOINs with ',' if you alias the tables and want to use the
277 # alias later in another JOIN. the alias you want to use has to
278 # be the most recent in the list, otherwise Postgres will
279 # overwrite the alias internally and complain. For this reason,
280 # in the next 3 SELECTs, the 'a' alias is last in the list.
281 # Don't change this, and if you do, substitute the ',' with CROSS
282 # JOIN ... that also works.
284 # get all transactions
286 qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
287 qq| $false AS invoice, ac.amount, 'gl' as module, | .
288 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 § .
289 qq|FROM acc_trans ac, gl a | .
291 qq|WHERE | . $where . $dpt_where . $project .
292 qq| AND ac.chart_id = ? | .
293 qq| AND ac.trans_id = a.id | .
294 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
298 qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
299 qq| a.invoice, ac.amount, 'ar' 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, customer c, ar 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.customer_id = c.id | .
307 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
311 qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
312 qq| a.invoice, ac.amount, 'ap' as module, | .
313 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 § .
314 qq|FROM acc_trans ac, vendor v, ap a | .
316 qq|WHERE | . $where . $dpt_where . $project .
317 qq| AND ac.chart_id = ? | .
318 qq| AND ac.trans_id = a.id | .
319 qq| AND a.vendor_id = v.id | .
320 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
322 @where_values, @department_values, @project_values, $id,
323 @where_values, @department_values, @project_values, $id,
324 @where_values, @department_values, @project_values, $id);
326 $union = qq|UNION ALL |;
328 if ($form->{project_id}) {
330 $fromdate_where =~ s/ac\./a\./;
331 $todate_where =~ s/ac\./a\./;
333 # strict check 20.10.2009 sschoeling
334 # the previous version contained the var $ar_ap_cash_where, which was ONLY set by
335 # RP->trial_balance() I tried to figure out which bizarre flow through the
336 # program would happen to set that var, so that it would be used here later on,
337 # (which would be nonsense, since you would normally load chart before
338 # claculating balance of said charts) and then decided that any mechanic that
339 # complex should fail anyway.
341 # if anyone is missing a time check on charts, that broke arounf the time
342 # trial_balance was rewritten, this would be it
347 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
348 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
349 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 § .
351 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
352 qq|JOIN parts p ON (ac.parts_id = p.id) | .
353 qq|JOIN customer c ON (a.customer_id = c.id) | .
355 qq|WHERE p.income_accno_id = ? | .
362 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
363 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
364 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 § .
366 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
367 qq|JOIN parts p ON (ac.parts_id = p.id) | .
368 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
370 qq|WHERE p.expense_accno_id = ? | .
376 $id, @department_values, @project_values,
377 $id, @department_values, @project_values);
379 $fromdate_where =~ s/a\./ac\./;
380 $todate_where =~ s/a\./ac\./;
384 $union = qq|UNION ALL|;
387 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
389 $query .= qq|ORDER BY $sort|;
390 my $sth = prepare_execute_query($form, $dbh, $query, @values);
392 #get detail information for each transaction
395 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
396 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
397 my $trans_sth = $dbh->prepare($trans_query);
400 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
402 if ($ca->{module} eq "ap") {
403 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
407 if ($ca->{module} eq "ar") {
408 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
411 if ($ca->{amount} < 0) {
412 $ca->{debit} = $ca->{amount} * -1;
415 $ca->{credit} = $ca->{amount};
419 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
421 #get detail information for this transaction
422 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
423 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
424 while (my $trans = $trans_sth->fetchrow_hashref("NAME_lc")) {
425 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
426 if ($trans->{amount} < 0) {
427 $trans->{debit} = $trans->{amount} * -1;
428 $trans->{credit} = 0;
430 $trans->{credit} = $trans->{amount};
433 push(@{ $ca->{GEGENKONTO} }, $trans);
439 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
440 # $ca->{index} = $ca->{$form->{sort}};
441 push(@{ $form->{CA} }, $ca);
448 $main::lxdebug->leave_sub();