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);
52 qq|SELECT c.accno, SUM(a.amount) AS amount | .
53 qq|FROM chart c, acc_trans a | .
54 qq|WHERE c.id = a.chart_id | .
56 my $sth = $dbh->prepare($query);
57 $sth->execute || $form->dberror($query);
59 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
60 $amount{ $ref->{accno} } = $ref->{amount};
64 my $where = "AND c.id = $chart_id" if ($chart_id ne '');
79 comma(tk.startdate::text) AS startdate,
80 comma(tk.taxkey_id::text) AS taxkey,
81 comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
82 comma(tx.taxnumber::text) AS taxaccount,
83 comma(tk.pos_ustva::text) AS tk_ustva,
89 LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
90 LEFT JOIN tax tx ON (tk.tax_id = tx.id)
93 GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
94 c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
99 my $sth = prepare_execute_query($form, $dbh, $query);
103 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
104 $ca->{amount} = $amount{ $ca->{accno} };
105 if ($ca->{amount} < 0) {
106 $ca->{debit} = $ca->{amount} * -1;
108 $ca->{credit} = $ca->{amount};
110 push(@{ $form->{CA} }, $ca);
116 $main::lxdebug->leave_sub();
119 sub all_transactions {
120 $main::lxdebug->enter_sub();
122 my ($self, $myconfig, $form) = @_;
124 # connect to database
125 my $dbh = $form->dbconnect($myconfig);
128 my $query = qq|SELECT id FROM chart WHERE accno = ?|;
129 my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
134 my $where = qq|1 = 1|;
136 # build WHERE clause from dates if any
137 # if ($form->{fromdate}) {
138 # $where .= " AND ac.transdate >= '$form->{fromdate}'";
140 # if ($form->{todate}) {
141 # $where .= " AND ac.transdate <= '$form->{todate}'";
144 my (@values, @where_values, @subwhere_values);
145 if ($form->{fromdate}) {
146 $where .= qq| AND ac.transdate >= ?|;
147 $subwhere .= qq| AND transdate >= ?|;
148 push(@where_values, conv_date($form->{fromdate}));
149 push(@subwhere_values, conv_date($form->{fromdate}));
152 if ($form->{todate}) {
153 $where .= qq| AND ac.transdate <= ?|;
154 $subwhere .= qq| AND transdate <= ?|;
155 push(@where_values, conv_date($form->{todate}));
156 push(@subwhere_values, conv_date($form->{todate}));
160 my $sortorder = join ', ',
161 $form->sort_columns(qw(transdate reference description));
162 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
164 # Oracle workaround, use ordinal positions
165 my %ordinal = (transdate => 4,
168 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
170 my ($null, $department_id) = split(/--/, $form->{department});
171 my ($dpt_where, $dpt_join, @department_values);
172 if ($department_id) {
173 $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
174 $dpt_where = qq| AND t.id = ? |;
175 @department_values = ($department_id);
178 my ($project, @project_values);
179 if ($form->{project_id}) {
180 $project = qq| AND ac.project_id = ? |;
181 @project_values = (conv_i($form->{project_id}));
183 my $acc_cash_where = "";
184 my $ar_cash_where = "";
185 my $ap_cash_where = "";
188 if ($form->{method} eq "cash") {
189 $acc_cash_where = qq| AND (ac.trans_id IN (SELECT id FROM ar WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM ap WHERE datepaid>='$form->{fromdate}' AND datepaid<='$form->{todate}' UNION SELECT id FROM gl WHERE transdate>='$form->{fromdate}' AND transdate<='$form->{todate}')) |;
190 $ar_ap_cash_where = qq| AND (a.datepaid>='$form->{fromdate}' AND a.datepaid<='$form->{todate}') |;
193 if ($form->{accno}) {
195 # get category for account
196 $query = qq|SELECT category FROM chart WHERE accno = ?|;
197 ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
199 if ($form->{fromdate}) {
200 # get beginning balances
202 qq|SELECT SUM(ac.amount) AS amount
204 JOIN chart c ON (ac.chart_id = c.id)
205 WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
207 AND c.accno = ? $acc_cash_where|;
209 ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
211 # get last transaction date
212 my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
213 $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 = ? $acc_cash_where|;
214 ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
217 $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) $acc_cash_where|;
218 ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
221 $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) $acc_cash_where|;
222 ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
224 $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) $acc_cash_where|;
225 ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
228 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
229 $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) $acc_cash_where|;
230 ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
233 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
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))) $todate AND c.accno = ? AND ac.amount < 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) $acc_cash_where|;
235 ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
237 my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
238 $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) $acc_cash_where|;
239 ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
247 foreach my $id (@id) {
249 # NOTE: Postgres is really picky about the order of implicit CROSS
250 # JOINs with ',' if you alias the tables and want to use the
251 # alias later in another JOIN. the alias you want to use has to
252 # be the most recent in the list, otherwise Postgres will
253 # overwrite the alias internally and complain. For this reason,
254 # in the next 3 SELECTs, the 'a' alias is last in the list.
255 # Don't change this, and if you do, substitute the ',' with CROSS
256 # JOIN ... that also works.
258 # get all transactions
260 qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
261 qq| $false AS invoice, ac.amount, 'gl' as module, | .
262 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 § .
263 qq|FROM acc_trans ac, gl a | .
265 qq|WHERE | . $where . $dpt_where . $project .
266 qq| AND ac.chart_id = ? | .
267 qq| AND ac.trans_id = a.id | .
268 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
272 qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
273 qq| a.invoice, ac.amount, 'ar' as module, | .
274 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 § .
275 qq|FROM acc_trans ac, customer c, ar a | .
277 qq|WHERE | . $where . $dpt_where . $project .
278 qq| AND ac.chart_id = ? | .
279 qq| AND ac.trans_id = a.id | .
280 qq| AND a.customer_id = c.id | .
281 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) $ar_ap_cash_where| .
285 qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
286 qq| a.invoice, ac.amount, 'ap' 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, vendor v, ap 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.vendor_id = v.id |;
294 qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) $ar_ap_cash_where| .
297 @where_values, @department_values, @project_values, $id,
298 @where_values, @department_values, @project_values, $id,
299 @where_values, @department_values, @project_values, $id);
301 $union = qq|UNION ALL |;
303 if ($form->{project_id}) {
305 $fromdate_where =~ s/ac\./a\./;
306 $todate_where =~ s/ac\./a\./;
311 qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
312 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' 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 § .
315 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
316 qq|JOIN parts p ON (ac.parts_id = p.id) | .
317 qq|JOIN customer c ON (a.customer_id = c.id) | .
319 qq|WHERE p.income_accno_id = ? | .
327 qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
328 qq| a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
329 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 § .
331 qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
332 qq|JOIN parts p ON (ac.parts_id = p.id) | .
333 qq|JOIN vendor v ON (a.vendor_id = v.id) | .
335 qq|WHERE p.expense_accno_id = ? | .
342 $id, @department_values, @project_values,
343 $id, @department_values, @project_values);
345 $fromdate_where =~ s/a\./ac\./;
346 $todate_where =~ s/a\./ac\./;
350 $union = qq|UNION ALL|;
353 my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
355 $query .= qq|ORDER BY $sort|;
356 $sth = prepare_execute_query($form, $dbh, $query, @values);
358 #get detail information for each transaction
361 qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
362 qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
363 my $trans_sth = $dbh->prepare($trans_query);
366 while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
368 if ($ca->{module} eq "ap") {
369 $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
373 if ($ca->{module} eq "ar") {
374 $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
377 if ($ca->{amount} < 0) {
378 $ca->{debit} = $ca->{amount} * -1;
381 $ca->{credit} = $ca->{amount};
385 ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
387 #get detail information for this transaction
388 $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
389 $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
390 while (my $trans = $trans_sth->fetchrow_hashref(NAME_lc)) {
391 if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
392 if ($trans->{amount} < 0) {
393 $trans->{debit} = $trans->{amount} * -1;
394 $trans->{credit} = 0;
396 $trans->{credit} = $trans->{amount};
399 push(@{ $ca->{GEGENKONTO} }, $trans);
405 $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
406 # $ca->{index} = $ca->{$form->{sort}};
407 push(@{ $form->{CA} }, $ca);
414 $main::lxdebug->leave_sub();