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))
60 # PROBLEM: Das date_trunc schneidet auf den 1.1.20XX ab und KEINE Buchungen werden angezeigt
61 # Lösung: date_trunc rausgeworfen und nicht mehr auf itime geprüft, sondern auf die erste Buchung
62 # in transdate jan 11.04.2011
64 my $closedto_sql = "COALESCE((SELECT closedto FROM defaults),
65 (SELECT transdate from acc_trans order by transdate limit 1))";
67 if ($form->{method} eq "cash") { # EÜR
68 $acc_cash_where = qq| AND (a.trans_id IN (SELECT id FROM ar WHERE datepaid>= $closedto_sql
69 UNION SELECT id FROM ap WHERE datepaid>= $closedto_sql
70 UNION SELECT id FROM gl WHERE transdate>= $closedto_sql
72 } else { # Bilanzierung
73 $acc_cash_where = " AND (a.transdate >= $closedto_sql) ";
77 qq|SELECT c.accno, SUM(a.amount) AS amount | .
78 qq|FROM chart c, acc_trans a | .
79 qq|WHERE c.id = a.chart_id | .
83 foreach my $ref (selectall_hashref_query($form, $dbh, $query)) {
84 $amount{ $ref->{accno} } = $ref->{amount};
87 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
102 comma(tk.startdate::text) AS startdate,
103 comma(tk.taxkey_id::text) AS taxkey,
104 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
105 comma(tx.taxnumber::text) AS taxaccount,
106 comma(tk.pos_ustva::text) AS tk_ustva,
112 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
113 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
116 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
117 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
122 my $sth = prepare_execute_query($form, $dbh, $query);
126 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
127 $ca->{amount} = $amount{ $ca->{accno} };
128 if ($ca->{amount} < 0) {
129 $ca->{debit} = $ca->{amount} * -1;
131 $ca->{credit} = $ca->{amount};
133 push(@{ $form->{CA} }, $ca);
139 $main::lxdebug->leave_sub();
142 sub all_transactions {
143 $main::lxdebug->enter_sub();
145 my ($self, $myconfig, $form) = @_;
147 # connect to database
148 my $dbh = $form->dbconnect($myconfig);
151 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
152 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
157 my $where = qq|1 = 1|;
159 # build WHERE clause from dates if any
160 # if ($form->{fromdate}) {
161 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
163 # if ($form->{todate}) {
164 # $where .= " AND ac.transdate <= '$form->{todate}'";
167 my (@values, @where_values, @subwhere_values, $subwhere);
168 if ($form->{fromdate}) {
169 $where .= qq| AND ac.transdate >= ?|;
170 $subwhere .= qq| AND transdate >= ?|;
171 push(@where_values, conv_date($form->{fromdate}));
172 push(@subwhere_values, conv_date($form->{fromdate}));
175 if ($form->{todate}) {
176 $where .= qq| AND ac.transdate <= ?|;
177 $subwhere .= qq| AND transdate <= ?|;
178 push(@where_values, conv_date($form->{todate}));
179 push(@subwhere_values, conv_date($form->{todate}));
183 my $sortorder = join ', ',
184 $form->sort_columns(qw(transdate reference description));
185 my $false = ($myconfig->{dbdriver} eq 'Pg') ? "FALSE" : q|'0'|;
187 # Oracle workaround, use ordinal positions
188 my %ordinal = (transdate => 4,
191 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
193 my ($null, $department_id) = split(/--/, $form->{department});
194 my ($dpt_where, $dpt_join, @department_values);
195 if ($department_id) {
196 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
197 $dpt_where = qq| AND t.id = ? |;
198 @department_values = ($department_id);
201 my ($project, @project_values);
202 if ($form->{project_id}) {
203 $project = qq| AND ac.project_id = ? |;
204 @project_values = (conv_i($form->{project_id}));
206 my $acc_cash_where = "";
207 my $ar_cash_where = "";
208 my $ap_cash_where = "";
211 if ($form->{method} eq "cash") {
212 $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<= ?)) |;
214 push(@where_values, conv_date($form->{fromdate}));
215 push(@where_values, conv_date($form->{todate}));
216 push(@where_values, conv_date($form->{fromdate}));
217 push(@where_values, conv_date($form->{todate}));
218 push(@where_values, conv_date($form->{fromdate}));
219 push(@where_values, conv_date($form->{todate}));
223 if ($form->{accno}) {
225 # get category for account
226 $query = qq|SELECT category FROM chart WHERE accno = ?|;
227 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
229 if ($form->{fromdate}) {
230 # get beginning balances
232 qq|SELECT SUM(ac.amount) AS amount
234 JOIN chart c ON (ac.chart_id = c.id)
235 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
239 ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
241 # get last transaction date
242 my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
243 $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 = ?|;
244 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $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 (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
248 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
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))) AND ac.transdate < ? AND c.accno = ? AND ac.amount < 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
252 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
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))) AND ac.transdate < ? AND c.accno = ? AND ac.amount > 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
255 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $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 (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
260 ($form->{saldo_new}) = 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_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
267 $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
268 $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)|;
269 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
277 foreach my $id (@id) {
279 # NOTE: Postgres is really picky about the order of implicit CROSS
280 # JOINs with ',' if you alias the tables and want to use the
281 # alias later in another JOIN. the alias you want to use has to
282 # be the most recent in the list, otherwise Postgres will
283 # overwrite the alias internally and complain. For this reason,
284 # in the next 3 SELECTs, the 'a' alias is last in the list.
285 # Don't change this, and if you do, substitute the ',' with CROSS
286 # JOIN ... that also works.
288 # get all transactions
290 qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
291 qq| $false AS invoice, ac.amount, 'gl' as module, | .
292 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 § .
293 qq|FROM acc_trans ac, gl a | .
295 qq|WHERE | . $where . $dpt_where . $project .
296 qq| AND ac.chart_id = ? | .
297 qq| AND ac.trans_id = a.id | .
298 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
302 qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
303 qq| a.invoice, ac.amount, 'ar' as module, | .
304 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 § .
305 qq|FROM acc_trans ac, customer c, ar a | .
307 qq|WHERE | . $where . $dpt_where . $project .
308 qq| AND ac.chart_id = ? | .
309 qq| AND ac.trans_id = a.id | .
310 qq| AND a.customer_id = c.id | .
311 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
315 qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
316 qq| a.invoice, ac.amount, 'ap' as module, | .
317 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 § .
318 qq|FROM acc_trans ac, vendor v, ap a | .
320 qq|WHERE | . $where . $dpt_where . $project .
321 qq| AND ac.chart_id = ? | .
322 qq| AND ac.trans_id = a.id | .
323 qq| AND a.vendor_id = v.id | .
324 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
326 @where_values, @department_values, @project_values, $id,
327 @where_values, @department_values, @project_values, $id,
328 @where_values, @department_values, @project_values, $id);
330 $union = qq|UNION ALL |;
332 if ($form->{project_id}) {
334 $fromdate_where =~ s/ac\./a\./;
335 $todate_where =~ s/ac\./a\./;
337 # strict check 20.10.2009 sschoeling
338 # the previous version contained the var $ar_ap_cash_where, which was ONLY set by
339 # RP->trial_balance() I tried to figure out which bizarre flow through the
340 # program would happen to set that var, so that it would be used here later on,
341 # (which would be nonsense, since you would normally load chart before
342 # claculating balance of said charts) and then decided that any mechanic that
343 # complex should fail anyway.
345 # if anyone is missing a time check on charts, that broke arounf the time
346 # trial_balance was rewritten, this would be it
351 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
352 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
353 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 § .
355 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
356 qq|JOIN parts p ON (ac.parts_id = p.id) | .
357 qq|JOIN customer c ON (a.customer_id = c.id) | .
359 qq|WHERE p.income_accno_id = ? | .
366 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
367 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
368 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 § .
370 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
371 qq|JOIN parts p ON (ac.parts_id = p.id) | .
372 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
374 qq|WHERE p.expense_accno_id = ? | .
380 $id, @department_values, @project_values,
381 $id, @department_values, @project_values);
383 $fromdate_where =~ s/a\./ac\./;
384 $todate_where =~ s/a\./ac\./;
388 $union = qq|UNION ALL|;
391 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
393 $query .= qq|ORDER BY $sort|;
394 my $sth = prepare_execute_query($form, $dbh, $query, @values);
396 #get detail information for each transaction
399 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
400 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
401 my $trans_sth = $dbh->prepare($trans_query);
404 while (my $ca = $sth->fetchrow_hashref("NAME_lc")) {
406 if ($ca->{module} eq "ap") {
407 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
411 if ($ca->{module} eq "ar") {
412 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
415 if ($ca->{amount} < 0) {
416 $ca->{debit} = $ca->{amount} * -1;
419 $ca->{credit} = $ca->{amount};
423 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
425 #get detail information for this transaction
426 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
427 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
428 while (my $trans = $trans_sth->fetchrow_hashref("NAME_lc")) {
429 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
430 if ($trans->{amount} < 0) {
431 $trans->{debit} = $trans->{amount} * -1;
432 $trans->{credit} = 0;
434 $trans->{credit} = $trans->{amount};
437 push(@{ $ca->{GEGENKONTO} }, $trans);
443 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
444 # $ca->{index} = $ca->{$form->{sort}};
445 push(@{ $form->{CA} }, $ca);
452 $main::lxdebug->leave_sub();