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 # bug 1071 Warum sollte bei Erreichen eines neuen Jahres die Kontenübersicht nur noch die
54 # bereits bebuchten Konten anzeigen?
55 # Folgende Erweiterung:
56 # 1.) Gehe zurück bis zu dem Datum an dem die Bücher geschlossen wurden
57 # 2.) Falls die Bücher noch nie geschlossen wurden, gehe zurück bis zum Bearbeitungsstart
58 # COALESCE((SELECT closedto FROM defaults),(SELECT itime FROM defaults))
60 my $closedto_sql = "COALESCE((SELECT closedto FROM defaults),(SELECT itime FROM defaults))";
62 if ($form->{method} eq "cash") { # EÜR
63 $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>= $closedto_sql
64 UNION SELECT id FROM ap WHERE datepaid>= $closedto_sql
65 UNION SELECT id FROM gl WHERE transdate>= $closedto_sql
67 } else { # Bilanzierung
68 $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= $closedto_sql) ";
72 qq|SELECT c.accno, SUM(a.amount) AS amount | .
73 qq|FROM chart c, acc_trans a | .
74 qq|WHERE c.id = a.chart_id | .
78 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
79 $amount{ $ref->{accno} } = $ref->{amount};
82 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
97 comma(tk.startdate::text) AS startdate,
98 comma(tk.taxkey_id::text) AS taxkey,
99 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
100 comma(tx.taxnumber::text) AS taxaccount,
101 comma(tk.pos_ustva::text) AS tk_ustva,
107 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
108 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
111 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
112 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
117 my $sth = prepare_execute_query($form, $dbh, $query);
121 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
122 $ca->{amount} = $amount{ $ca->{accno} };
123 if ($ca->{amount} < 0) {
124 $ca->{debit} = $ca->{amount} * -1;
126 $ca->{credit} = $ca->{amount};
128 push(@{ $form->{CA} }, $ca);
134 $main::lxdebug->leave_sub();
137 sub all_transactions {
138 $main::lxdebug->enter_sub();
140 my ($self, $myconfig, $form) = @_;
142 # connect to database
143 my $dbh = $form->dbconnect($myconfig);
146 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
147 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
152 my $where = qq|1 = 1|;
154 # build WHERE clause from dates if any
155 # if ($form->{fromdate}) {
156 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
158 # if ($form->{todate}) {
159 # $where .= " AND ac.transdate <= '$form->{todate}'";
162 my (@values, @where_values, @subwhere_values, $subwhere);
163 if ($form->{fromdate}) {
164 $where .= qq| AND ac.transdate >= ?|;
165 $subwhere .= qq| AND transdate >= ?|;
166 push(@where_values, conv_date($form->{fromdate}));
167 push(@subwhere_values, conv_date($form->{fromdate}));
170 if ($form->{todate}) {
171 $where .= qq| AND ac.transdate <= ?|;
172 $subwhere .= qq| AND transdate <= ?|;
173 push(@where_values, conv_date($form->{todate}));
174 push(@subwhere_values, conv_date($form->{todate}));
178 my $sortorder = join ', ',
179 $form->sort_columns(qw(transdate reference description));
180 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
182 # Oracle workaround, use ordinal positions
183 my %ordinal = (transdate => 4,
186 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
188 my ($null, $department_id) = split(/--/, $form->{department});
189 my ($dpt_where, $dpt_join, @department_values);
190 if ($department_id) {
191 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
192 $dpt_where = qq| AND t.id = ? |;
193 @department_values = ($department_id);
196 my ($project, @project_values);
197 if ($form->{project_id}) {
198 $project = qq| AND ac.project_id = ? |;
199 @project_values = (conv_i($form->{project_id}));
201 my $acc_cash_where = "";
202 my $ar_cash_where = "";
203 my $ap_cash_where = "";
206 if ($form->{method} eq "cash") {
207 $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<= ?)) |;
209 push(@where_values, conv_date($form->{fromdate}));
210 push(@where_values, conv_date($form->{todate}));
211 push(@where_values, conv_date($form->{fromdate}));
212 push(@where_values, conv_date($form->{todate}));
213 push(@where_values, conv_date($form->{fromdate}));
214 push(@where_values, conv_date($form->{todate}));
218 if ($form->{accno}) {
220 # get category for account
221 $query = qq|SELECT category FROM chart WHERE accno = ?|;
222 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
224 if ($form->{fromdate}) {
225 # get beginning balances
227 qq|SELECT SUM(ac.amount) AS amount
229 JOIN chart c ON (ac.chart_id = c.id)
230 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
234 ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
236 # get last transaction date
237 my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
238 $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 = ?|;
239 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
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))) AND ac.transdate < ? AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
243 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
246 $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)|;
247 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
249 $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)|;
250 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
253 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
254 $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)|;
255 ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
258 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
259 $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)|;
260 ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
262 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
263 $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)|;
264 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
272 foreach my $id (@id) {
274 # NOTE: Postgres is really picky about the order of implicit CROSS
275 # JOINs with ',' if you alias the tables and want to use the
276 # alias later in another JOIN. the alias you want to use has to
277 # be the most recent in the list, otherwise Postgres will
278 # overwrite the alias internally and complain. For this reason,
279 # in the next 3 SELECTs, the 'a' alias is last in the list.
280 # Don't change this, and if you do, substitute the ',' with CROSS
281 # JOIN ... that also works.
283 # get all transactions
285 qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
286 qq| $false AS invoice, ac.amount, 'gl' 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, gl 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 (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
297 qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
298 qq| a.invoice, ac.amount, 'ar' as module, | .
299 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 § .
300 qq|FROM acc_trans ac, customer c, ar a | .
302 qq|WHERE | . $where . $dpt_where . $project .
303 qq| AND ac.chart_id = ? | .
304 qq| AND ac.trans_id = a.id | .
305 qq| AND a.customer_id = c.id | .
306 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
310 qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
311 qq| a.invoice, ac.amount, 'ap' as module, | .
312 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 § .
313 qq|FROM acc_trans ac, vendor v, ap a | .
315 qq|WHERE | . $where . $dpt_where . $project .
316 qq| AND ac.chart_id = ? | .
317 qq| AND ac.trans_id = a.id | .
318 qq| AND a.vendor_id = v.id | .
319 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
321 @where_values, @department_values, @project_values, $id,
322 @where_values, @department_values, @project_values, $id,
323 @where_values, @department_values, @project_values, $id);
325 $union = qq|UNION ALL |;
327 if ($form->{project_id}) {
329 $fromdate_where =~ s/ac\./a\./;
330 $todate_where =~ s/ac\./a\./;
332 # strict check 20.10.2009 sschoeling
333 # the previous version contained the var $ar_ap_cash_where, which was ONLY set by
334 # RP->trial_balance() I tried to figure out which bizarre flow through the
335 # program would happen to set that var, so that it would be used here later on,
336 # (which would be nonsense, since you would normally load chart before
337 # claculating balance of said charts) and then decided that any mechanic that
338 # complex should fail anyway.
340 # if anyone is missing a time check on charts, that broke arounf the time
341 # trial_balance was rewritten, this would be it
346 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
347 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
348 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 § .
350 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
351 qq|JOIN parts p ON (ac.parts_id = p.id) | .
352 qq|JOIN customer c ON (a.customer_id = c.id) | .
354 qq|WHERE p.income_accno_id = ? | .
361 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
362 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
363 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 § .
365 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
366 qq|JOIN parts p ON (ac.parts_id = p.id) | .
367 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
369 qq|WHERE p.expense_accno_id = ? | .
375 $id, @department_values, @project_values,
376 $id, @department_values, @project_values);
378 $fromdate_where =~ s/a\./ac\./;
379 $todate_where =~ s/a\./ac\./;
383 $union = qq|UNION ALL|;
386 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
388 $query .= qq|ORDER BY $sort|;
389 my $sth = prepare_execute_query($form, $dbh, $query, @values);
391 #get detail information for each transaction
394 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
395 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
396 my $trans_sth = $dbh->prepare($trans_query);
399 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
401 if ($ca->{module} eq "ap") {
402 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
406 if ($ca->{module} eq "ar") {
407 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
410 if ($ca->{amount} < 0) {
411 $ca->{debit} = $ca->{amount} * -1;
414 $ca->{credit} = $ca->{amount};
418 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
420 #get detail information for this transaction
421 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
422 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
423 while (my $trans = $trans_sth->fetchrow_hashref("NAME_lc")) {
424 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
425 if ($trans->{amount} < 0) {
426 $trans->{debit} = $trans->{amount} * -1;
427 $trans->{credit} = 0;
429 $trans->{credit} = $trans->{amount};
432 push(@{ $ca->{GEGENKONTO} }, $trans);
438 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
439 # $ca->{index} = $ca->{$form->{sort}};
440 push(@{ $form->{CA} }, $ca);
447 $main::lxdebug->leave_sub();