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);
 
  51   if ($form->{method} eq "cash") {
 
  52     $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)))) |;
 
  54     $acc_cash_where = " AND ((select date_trunc('year', a.transdate::date)) >= (select date_trunc('year', current_date)))";
 
  58     qq|SELECT c.accno, SUM(a.amount) AS amount | .
 
  59     qq|FROM chart c, acc_trans a | .
 
  60     qq|WHERE c.id = a.chart_id | .
 
  63   my $sth = $dbh->prepare($query);
 
  64   $sth->execute || $form->dberror($query);
 
  66   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  67     $amount{ $ref->{accno} } = $ref->{amount};
 
  71   my $where = "AND c.id = $chart_id" if ($chart_id ne '');
 
  86       comma(tk.startdate::text) AS startdate,
 
  87       comma(tk.taxkey_id::text) AS taxkey,
 
  88       comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
 
  89       comma(tx.taxnumber::text) AS taxaccount,
 
  90       comma(tk.pos_ustva::text) AS tk_ustva,
 
  96     LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
 
  97     LEFT JOIN tax tx ON (tk.tax_id = tx.id)
 
 100     GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
 
 101       c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,
 
 106   my $sth = prepare_execute_query($form, $dbh, $query);
 
 110   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
 
 111     $ca->{amount} = $amount{ $ca->{accno} };
 
 112     if ($ca->{amount} < 0) {
 
 113       $ca->{debit} = $ca->{amount} * -1;
 
 115       $ca->{credit} = $ca->{amount};
 
 117     push(@{ $form->{CA} }, $ca);
 
 123   $main::lxdebug->leave_sub();
 
 126 sub all_transactions {
 
 127   $main::lxdebug->enter_sub();
 
 129   my ($self, $myconfig, $form) = @_;
 
 131   # connect to database
 
 132   my $dbh = $form->dbconnect($myconfig);
 
 135   my $query = qq|SELECT id FROM chart WHERE accno = ?|;
 
 136   my @id = selectall_array_query($form, $dbh, $query, $form->{accno});
 
 141   my $where = qq|1 = 1|;
 
 143   # build WHERE clause from dates if any
 
 144   #  if ($form->{fromdate}) {
 
 145   #    $where .= " AND ac.transdate >= '$form->{fromdate}'";
 
 147   #  if ($form->{todate}) {
 
 148   #    $where .= " AND ac.transdate <= '$form->{todate}'";
 
 151   my (@values, @where_values, @subwhere_values);
 
 152   if ($form->{fromdate}) {
 
 153     $where .= qq| AND ac.transdate >= ?|;
 
 154     $subwhere .= qq| AND transdate >= ?|;
 
 155     push(@where_values, conv_date($form->{fromdate}));
 
 156     push(@subwhere_values, conv_date($form->{fromdate}));
 
 159   if ($form->{todate}) {
 
 160     $where .= qq| AND ac.transdate <= ?|;
 
 161     $subwhere .= qq| AND transdate <= ?|;
 
 162     push(@where_values, conv_date($form->{todate}));
 
 163     push(@subwhere_values, conv_date($form->{todate}));
 
 167   my $sortorder = join ', ',
 
 168     $form->sort_columns(qw(transdate reference description));
 
 169   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
 
 171   # Oracle workaround, use ordinal positions
 
 172   my %ordinal = (transdate   => 4,
 
 175   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
 
 177   my ($null, $department_id) = split(/--/, $form->{department});
 
 178   my ($dpt_where, $dpt_join, @department_values);
 
 179   if ($department_id) {
 
 180     $dpt_join = qq| JOIN department t ON (t.id = a.department_id) |;
 
 181     $dpt_where = qq| AND t.id = ? |;
 
 182     @department_values = ($department_id);
 
 185   my ($project, @project_values);
 
 186   if ($form->{project_id}) {
 
 187     $project = qq| AND ac.project_id = ? |;
 
 188     @project_values = (conv_i($form->{project_id}));
 
 190   my $acc_cash_where = "";
 
 191   my $ar_cash_where = "";
 
 192   my $ap_cash_where = "";
 
 195   if ($form->{method} eq "cash") {
 
 196     $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<= ?)) |;
 
 198     push(@where_values, conv_date($form->{fromdate}));
 
 199     push(@where_values, conv_date($form->{todate}));
 
 200     push(@where_values, conv_date($form->{fromdate}));
 
 201     push(@where_values, conv_date($form->{todate}));
 
 202     push(@where_values, conv_date($form->{fromdate}));
 
 203     push(@where_values, conv_date($form->{todate}));
 
 207   if ($form->{accno}) {
 
 209     # get category for account
 
 210     $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 211     ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 213     if ($form->{fromdate}) {
 
 214       # get beginning balances
 
 216         qq|SELECT SUM(ac.amount) AS amount
 
 218             JOIN chart c ON (ac.chart_id = c.id)
 
 219             WHERE ((select date_trunc('year', ac.transdate::date)) = (select date_trunc('year', ?::date))) AND ac.ob_transaction
 
 223       ($form->{beginning_balance}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
 
 225       # get last transaction date
 
 226       my $todate = ($form->{todate}) ? " AND ac.transdate <= '$form->{todate}' " : "";
 
 227       $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 = ?|;
 
 228       ($form->{last_transaction}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
 
 231       $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)|;
 
 232       ($form->{saldo_old}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
 
 235       $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)|;
 
 236       ($form->{old_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
 
 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))) AND ac.transdate < ? AND c.accno = ? AND ac.amount > 0 AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
 
 239       ($form->{old_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{fromdate}, $form->{accno});
 
 242       my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
 
 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))) $todate AND c.accno = ? AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)|;
 
 244       ($form->{saldo_new}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
 
 247       my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
 
 248       $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)|;
 
 249       ($form->{current_balance_debit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
 
 251       my $todate = ($form->{todate} ne "") ? " AND ac.transdate <= '$form->{todate}' " : "";
 
 252       $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)|;
 
 253       ($form->{current_balance_credit}) = selectrow_query($form, $dbh, $query, $form->{fromdate}, $form->{accno});
 
 261   foreach my $id (@id) {
 
 263     # NOTE: Postgres is really picky about the order of implicit CROSS
 
 264     #  JOINs with ',' if you alias the tables and want to use the
 
 265     #  alias later in another JOIN.  the alias you want to use has to
 
 266     #  be the most recent in the list, otherwise Postgres will
 
 267     #  overwrite the alias internally and complain.  For this reason,
 
 268     #  in the next 3 SELECTs, the 'a' alias is last in the list.
 
 269     #  Don't change this, and if you do, substitute the ',' with CROSS
 
 270     #  JOIN ... that also works.
 
 272     # get all transactions
 
 274       qq|SELECT a.id, a.reference, a.description, ac.transdate, ac.chart_id, | .
 
 275       qq|  $false AS invoice, ac.amount, 'gl' as module, | .
 
 276       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 § .
 
 277       qq|FROM acc_trans ac, gl a | .
 
 279       qq|WHERE | . $where . $dpt_where . $project .
 
 280       qq|  AND ac.chart_id = ? | .
 
 281       qq| AND ac.trans_id = a.id | .
 
 282       qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL) | .
 
 286       qq|SELECT a.id, a.invnumber, c.name, ac.transdate, ac.chart_id, | .
 
 287       qq|  a.invoice, ac.amount, 'ar' 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, customer c, ar 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 a.customer_id = c.id | .
 
 295       qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
 
 299       qq|SELECT a.id, a.invnumber, v.name, ac.transdate, ac.chart_id, | .
 
 300       qq|  a.invoice, ac.amount, 'ap' as module, | .
 
 301       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  § .
 
 302       qq|FROM acc_trans ac, vendor v, ap a | .
 
 304       qq|WHERE | . $where . $dpt_where . $project .
 
 305       qq| AND ac.chart_id = ? | .
 
 306       qq| AND ac.trans_id = a.id | .
 
 307       qq| AND a.vendor_id = v.id |;
 
 308       qq| AND (NOT ac.ob_transaction OR ac.ob_transaction IS NULL)| .
 
 310          @where_values, @department_values, @project_values, $id,
 
 311          @where_values, @department_values, @project_values, $id,
 
 312          @where_values, @department_values, @project_values, $id);
 
 314     $union = qq|UNION ALL |;
 
 316     if ($form->{project_id}) {
 
 318       $fromdate_where =~ s/ac\./a\./;
 
 319       $todate_where   =~ s/ac\./a\./;
 
 324         qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
 
 325         qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module, | .
 
 326         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 § .
 
 328         qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
 
 329         qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 330         qq|JOIN customer c ON (a.customer_id = c.id) | .
 
 332         qq|WHERE p.income_accno_id = ? | .
 
 340         qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
 
 341         qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module, | .
 
 342         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 § .
 
 344         qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
 
 345         qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 346         qq|JOIN vendor v ON (a.vendor_id = v.id) | .
 
 348         qq|WHERE p.expense_accno_id = ? | .
 
 355            $id, @department_values, @project_values,
 
 356            $id, @department_values, @project_values);
 
 358       $fromdate_where =~ s/a\./ac\./;
 
 359       $todate_where   =~ s/a\./ac\./;
 
 363     $union = qq|UNION ALL|;
 
 366   my $sort = grep({ $form->{sort} eq $_ } qw(transdate reference description)) ? $form->{sort} : 'transdate';
 
 368   $query .= qq|ORDER BY $sort|;
 
 369   $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 371   #get detail information for each transaction
 
 374         qq|amount, transdate FROM acc_trans LEFT JOIN chart ON (chart_id=chart.id) WHERE | .
 
 375         qq|trans_id = ? AND sign(amount) <> sign(?) AND chart_id <> ? AND transdate = ?|;
 
 376   my $trans_sth = $dbh->prepare($trans_query);
 
 379   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
 
 381     if ($ca->{module} eq "ap") {
 
 382       $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
 
 386     if ($ca->{module} eq "ar") {
 
 387       $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
 
 390     if ($ca->{amount} < 0) {
 
 391       $ca->{debit}  = $ca->{amount} * -1;
 
 394       $ca->{credit} = $ca->{amount};
 
 398     ($ca->{ustkonto},$ca->{ustrate}) = split /--/, $ca->{taxinfo};
 
 400     #get detail information for this transaction
 
 401     $trans_sth->execute($ca->{id}, $ca->{amount}, $ca->{chart_id}, $ca->{transdate}) ||
 
 402     $form->dberror($trans_query . " (" . join(", ", $ca->{id}) . ")");
 
 403     while (my $trans = $trans_sth->fetchrow_hashref(NAME_lc)) {
 
 404       if (($ca->{transdate} eq $trans->{transdate}) && ($ca->{amount} * $trans->{amount} < 0)) {
 
 405         if ($trans->{amount} < 0) {
 
 406           $trans->{debit}  = $trans->{amount} * -1;
 
 407           $trans->{credit} = 0;
 
 409           $trans->{credit} = $trans->{amount};
 
 412         push(@{ $ca->{GEGENKONTO} }, $trans);
 
 418     $ca->{index} = join "--", map { $ca->{$_} } qw(id reference description transdate);
 
 419 #     $ca->{index} = $ca->{$form->{sort}};
 
 420     push(@{ $form->{CA} }, $ca);
 
 427   $main::lxdebug->leave_sub();