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) AS startdate,
 
  80       comma(tk.taxkey_id) AS taxkey,
 
  81       comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
 
  82       comma(tx.taxnumber) AS taxaccount,
 
  83       comma(tk.pos_ustva) 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}));
 
 184   if ($form->{accno}) {
 
 186     # get category for account
 
 187     $query = qq|SELECT category FROM chart WHERE accno = ?|;
 
 188     ($form->{category}) = selectrow_query($form, $dbh, $query, $form->{accno});
 
 190     if ($form->{fromdate}) {
 
 191       # get beginning balance
 
 193         qq|SELECT SUM(ac.amount) | .
 
 194         qq|FROM acc_trans ac | .
 
 195         qq|JOIN chart c ON (ac.chart_id = c.id) | .
 
 197         qq|WHERE c.accno = ? | .
 
 198         qq|AND ac.transdate < ? | .
 
 201       @values = ($form->{accno}, conv_date($form->{fromdate}),
 
 202                  @department_values, @project_values);
 
 204       if ($form->{project_id}) {
 
 208           qq|SELECT SUM(ac.qty * ac.sellprice) | .
 
 209           qq|FROM invoice ac | .
 
 210           qq|JOIN ar a ON (ac.trans_id = a.id) | .
 
 211           qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 212           qq|JOIN chart c ON (p.income_accno_id = c.id) | .
 
 214           qq|WHERE c.accno = ? | .
 
 215           qq|  AND a.transdate < ? | .
 
 216           qq|  AND c.category = 'I' | .
 
 222           qq|SELECT SUM(ac.qty * ac.sellprice) | .
 
 223           qq|FROM invoice ac | .
 
 224           qq|JOIN ap a ON (ac.trans_id = a.id) | .
 
 225           qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 226           qq|JOIN chart c ON (p.expense_accno_id = c.id) | .
 
 228           qq|WHERE c.accno = ? | .
 
 229           qq|  AND a.transdate < ? | .
 
 230           qq|  AND c.category = 'E' | .
 
 235              $form->{accno}, conv_date($form->{transdate}),
 
 236              @department_values, @project_values,
 
 237              $form->{accno}, conv_date($form->{transdate}),
 
 238              @department_values, @project_values);
 
 241       ($form->{balance}) = selectrow_query($form, $dbh, $query, @values);
 
 249   foreach my $id (@id) {
 
 251     # NOTE: Postgres is really picky about the order of implicit CROSS
 
 252     #  JOINs with ',' if you alias the tables and want to use the
 
 253     #  alias later in another JOIN.  the alias you want to use has to
 
 254     #  be the most recent in the list, otherwise Postgres will
 
 255     #  overwrite the alias internally and complain.  For this reason,
 
 256     #  in the next 3 SELECTs, the 'a' alias is last in the list.
 
 257     #  Don't change this, and if you do, substitute the ',' with CROSS
 
 258     #  JOIN ... that also works.
 
 260     # get all transactions
 
 263       qq|SELECT a.id, a.reference, a.description, ac.transdate, | .
 
 264       qq|  $false AS invoice, ac.amount, 'gl' as module | .
 
 265       qq|FROM acc_trans ac, gl a | .
 
 267       qq|WHERE | . $where . $dpt_where . $project .
 
 268       qq|  AND ac.chart_id = ? | .
 
 272       qq|SELECT a.id, a.invnumber, c.name, ac.transdate, | .
 
 273       qq|  a.invoice, ac.amount, 'ar' as module | .
 
 274       qq|FROM acc_trans ac, customer c, ar a | .
 
 276       qq|WHERE | . $where . $dpt_where . $project .
 
 277       qq| AND ac.chart_id = ? | .
 
 278       qq| AND NOT a.storno | .
 
 279       qq| AND a.customer_id = c.id | .
 
 283       qq|SELECT a.id, a.invnumber, v.name, ac.transdate, | .
 
 284       qq|  a.invoice, ac.amount, 'ap' as module | .
 
 285       qq|FROM acc_trans ac, vendor v, ap a | .
 
 287       qq|WHERE | . $where . $dpt_where . $project .
 
 288       qq| AND ac.chart_id = ? | .
 
 289       qq| AND NOT a.storno | .
 
 290       qq| AND a.vendor_id = v.id |;
 
 293          @where_values, @department_values, @project_values, $id,
 
 294          @where_values, @department_values, @project_values, $id,
 
 295          @where_values, @department_values, @project_values, $id);
 
 297     $union = qq|UNION ALL |;
 
 299     if ($form->{project_id}) {
 
 301       $fromdate_where =~ s/ac\./a\./;
 
 302       $todate_where   =~ s/ac\./a\./;
 
 307         qq|SELECT a.id, a.invnumber, c.name, a.transdate, | .
 
 308         qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module | .
 
 310         qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
 
 311         qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 312         qq|JOIN customer c ON (a.customer_id = c.id) | .
 
 314         qq|WHERE p.income_accno_id = ? | .
 
 322         qq|SELECT a.id, a.invnumber, v.name, a.transdate, | .
 
 323         qq|  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module | .
 
 325         qq|JOIN invoice ac ON (ac.trans_id = a.id) | .
 
 326         qq|JOIN parts p ON (ac.parts_id = p.id) | .
 
 327         qq|JOIN vendor v ON (a.vendor_id = v.id) | .
 
 329         qq|WHERE p.expense_accno_id = ? | .
 
 336            $id, @department_values, @project_values,
 
 337            $id, @department_values, @project_values);
 
 339       $fromdate_where =~ s/a\./ac\./;
 
 340       $todate_where   =~ s/a\./ac\./;
 
 344     $union = qq|UNION ALL|;
 
 347   $query .= qq|ORDER BY | . $form->{sort};
 
 348   $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 351   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
 
 353     if ($ca->{module} eq "ap") {
 
 354       $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
 
 358     if ($ca->{module} eq "ar") {
 
 359       $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
 
 362     if ($ca->{amount} < 0) {
 
 363       $ca->{debit}  = $ca->{amount} * -1;
 
 366       $ca->{credit} = $ca->{amount};
 
 370     push(@{ $form->{CA} }, $ca);
 
 377   $main::lxdebug->leave_sub();