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 #======================================================================
 
  41   $main::lxdebug->enter_sub();
 
  43   my ($self, $myconfig, $form) = @_;
 
  48   my $dbh = $form->dbconnect($myconfig);
 
  50   my $query = qq|SELECT c.accno,
 
  51                  SUM(a.amount) AS amount
 
  52                  FROM chart c, acc_trans a
 
  53                  WHERE c.id = a.chart_id
 
  55   my $sth = $dbh->prepare($query);
 
  56   $sth->execute || $form->dberror($query);
 
  58   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  59     $amount{ $ref->{accno} } = $ref->{amount};
 
  63   $query = qq|SELECT accno, description
 
  65   $sth = $dbh->prepare($query);
 
  66   $sth->execute || $form->dberror($query);
 
  69   while (my ($accno, $description) = $sth->fetchrow_array) {
 
  70     $gifi{$accno} = $description;
 
  87       comma(tk.startdate) AS startdate,
 
  88       comma(tk.taxkey_id) AS taxkey,
 
  89       comma(tx.taxdescription || to_char (tx.rate, '99V99' ) || '%') AS taxdescription,
 
  90       comma(tx.taxnumber) AS taxaccount,
 
  91       comma(tk.pos_ustva) AS tk_ustva,
 
  97     LEFT JOIN taxkeys tk ON (c.id = tk.chart_id)
 
  98     LEFT JOIN tax tx ON (tk.tax_id = tx.id)
 
  99     GROUP BY c.accno, c.id, c.description, c.charttype, c.gifi_accno,
 
 100       c.category, c.link, c.pos_bwa, c.pos_bilanz, c.pos_eur, c.valid_from,      
 
 105   $sth = $dbh->prepare($query);
 
 106   $sth->execute || $form->dberror($query);
 
 108   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
 
 109     $ca->{amount}           = $amount{ $ca->{accno} };
 
 110     $ca->{gifi_description} = $gifi{ $ca->{gifi_accno} };
 
 111     if ($ca->{amount} < 0) {
 
 112       $ca->{debit} = $ca->{amount} * -1;
 
 114       $ca->{credit} = $ca->{amount};
 
 116     push @{ $form->{CA} }, $ca;
 
 122   $main::lxdebug->leave_sub();
 
 125 sub all_transactions {
 
 126   $main::lxdebug->enter_sub();
 
 128   my ($self, $myconfig, $form) = @_;
 
 130   # connect to database
 
 131   my $dbh = $form->dbconnect($myconfig);
 
 134   my $query = qq|SELECT c.id FROM chart c
 
 135                  WHERE c.accno = '$form->{accno}'|;
 
 136   if ($form->{accounttype} eq 'gifi') {
 
 137     $query = qq|SELECT c.id FROM chart c
 
 138                 WHERE c.gifi_accno = '$form->{gifi_accno}'|;
 
 140   my $sth = $dbh->prepare($query);
 
 141   $sth->execute || $form->dberror($query);
 
 144   while (my ($id) = $sth->fetchrow_array) {
 
 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   if ($form->{fromdate}) {
 
 163     $fromto = " AND ac.transdate >= '$form->{fromdate}'";
 
 164     $subwhere .= " AND transdate >= '$form->{fromdate}'";
 
 165     $glwhere = " AND ac.transdate >= '$form->{fromdate}'";
 
 168   if ($form->{todate}) {
 
 169     $fromto   .= " AND ac.transdate <= '$form->{todate}'";
 
 170     $subwhere .= " AND transdate <= '$form->{todate}'";
 
 171     $glwhere .= " AND ac.transdate <= '$form->{todate}'";
 
 178   $glwhere = "";    # note! gl will be aliased as "a" later...
 
 179   my $sortorder = join ', ',
 
 180     $form->sort_columns(qw(transdate reference description));
 
 181   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE: q|'0'|;
 
 183   # Oracle workaround, use ordinal positions
 
 184   my %ordinal = (transdate   => 4,
 
 187   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
 
 189   my ($null, $department_id) = split /--/, $form->{department};
 
 192   if ($department_id) {
 
 194                    JOIN department t ON (t.id = a.department_id)
 
 197                    AND t.id = $department_id
 
 202   if ($form->{project_id}) {
 
 204                  AND ac.project_id = $form->{project_id}
 
 208   if ($form->{accno} || $form->{gifi_accno}) {
 
 210     # get category for account
 
 211     $query = qq|SELECT c.category
 
 213                 WHERE c.accno = '$form->{accno}'|;
 
 215     if ($form->{accounttype} eq 'gifi') {
 
 216       $query = qq|SELECT c.category
 
 218                 WHERE c.gifi_accno = '$form->{gifi_accno}'
 
 219                 AND c.charttype = 'A'|;
 
 222     $sth = $dbh->prepare($query);
 
 224     $sth->execute || $form->dberror($query);
 
 225     ($form->{category}) = $sth->fetchrow_array;
 
 228     if ($form->{fromdate}) {
 
 230       # get beginning balance
 
 231       $query = qq|SELECT SUM(ac.amount)
 
 233                   JOIN chart c ON (ac.chart_id = c.id)
 
 235                   WHERE c.accno = '$form->{accno}'
 
 236                   AND ac.transdate < '$form->{fromdate}'
 
 241       if ($form->{project_id}) {
 
 247                   SELECT SUM(ac.qty * ac.sellprice)
 
 249                   JOIN ar a ON (ac.trans_id = a.id)
 
 250                   JOIN parts p ON (ac.parts_id = p.id)
 
 251                   JOIN chart c ON (p.income_accno_id = c.id)
 
 253                   WHERE c.accno = '$form->{accno}'
 
 254                   AND a.transdate < '$form->{fromdate}'
 
 261                   SELECT SUM(ac.qty * ac.sellprice)
 
 263                   JOIN ap a ON (ac.trans_id = a.id)
 
 264                   JOIN parts p ON (ac.parts_id = p.id)
 
 265                   JOIN chart c ON (p.expense_accno_id = c.id)
 
 267                   WHERE c.accno = '$form->{accno}'
 
 268                   AND a.transdate < '$form->{fromdate}'
 
 276       if ($form->{accounttype} eq 'gifi') {
 
 277         $query = qq|SELECT SUM(ac.amount)
 
 279                   JOIN chart c ON (ac.chart_id = c.id)
 
 281                   WHERE c.gifi_accno = '$form->{gifi_accno}'
 
 282                   AND ac.transdate < '$form->{fromdate}'
 
 287         if ($form->{project_id}) {
 
 293                   SELECT SUM(ac.qty * ac.sellprice)
 
 295                   JOIN ar a ON (ac.trans_id = a.id)
 
 296                   JOIN parts p ON (ac.parts_id = p.id)
 
 297                   JOIN chart c ON (p.income_accno_id = c.id)
 
 299                   WHERE c.gifi_accno = '$form->{gifi_accno}'
 
 300                   AND a.transdate < '$form->{fromdate}'
 
 307                   SELECT SUM(ac.qty * ac.sellprice)
 
 309                   JOIN ap a ON (ac.trans_id = a.id)
 
 310                   JOIN parts p ON (ac.parts_id = p.id)
 
 311                   JOIN chart c ON (p.expense_accno_id = c.id)
 
 313                   WHERE c.gifi_accno = '$form->{gifi_accno}'
 
 314                   AND a.transdate < '$form->{fromdate}'
 
 323       $sth = $dbh->prepare($query);
 
 325       $sth->execute || $form->dberror($query);
 
 326       ($form->{balance}) = $sth->fetchrow_array;
 
 334   foreach my $id (@id) {
 
 337     #  Postgres is really picky about the order of implicit CROSS JOINs with ','
 
 338     #  if you alias the  tables and want to use the alias later in another JOIN.
 
 339     #  the alias you want to use has to be the most recent in the list, otherwise
 
 340     #  Postgres will overwrite the alias internally and complain.
 
 341     #  For this reason, in the next 3 SELECTs, the 'a' alias is last in the list.
 
 342     #  Don't change this, and if you do, substitute the ',' with CROSS JOIN
 
 343     #  ... that also works.
 
 345     # get all transactions
 
 347       SELECT a.id, a.reference, a.description, ac.transdate,
 
 348              $false AS invoice, ac.amount, 'gl' as module
 
 349                 FROM acc_trans ac, gl a $dpt_join
 
 354                 AND ac.chart_id = $id
 
 355                 AND ac.trans_id = a.id
 
 357       SELECT a.id, a.invnumber, c.name, ac.transdate,
 
 358              a.invoice, ac.amount, 'ar' as module
 
 359                 FROM acc_trans ac, customer c, ar a $dpt_join
 
 363                 AND ac.chart_id = $id
 
 364                 AND ac.trans_id = a.id
 
 366                 AND a.customer_id = c.id
 
 368       SELECT a.id, a.invnumber, v.name, ac.transdate,
 
 369              a.invoice, ac.amount, 'ap' as module
 
 370                 FROM acc_trans ac, vendor v, ap a $dpt_join
 
 374                 AND ac.chart_id = $id
 
 375                 AND ac.trans_id = a.id
 
 377                 AND a.vendor_id = v.id
 
 383     if ($form->{project_id}) {
 
 385       $fromdate_where =~ s/ac\./a\./;
 
 386       $todate_where   =~ s/ac\./a\./;
 
 392                  SELECT a.id, a.invnumber, c.name, a.transdate,
 
 393                  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ar' as module
 
 395                  JOIN invoice ac ON (ac.trans_id = a.id)
 
 396                  JOIN parts p ON (ac.parts_id = p.id)
 
 397                  JOIN customer c ON (a.customer_id = c.id)
 
 399                  WHERE p.income_accno_id = $id
 
 407                  SELECT a.id, a.invnumber, v.name, a.transdate,
 
 408                  a.invoice, ac.qty * ac.sellprice AS sellprice, 'ap' as module
 
 410                  JOIN invoice ac ON (ac.trans_id = a.id)
 
 411                  JOIN parts p ON (ac.parts_id = p.id)
 
 412                  JOIN vendor v ON (a.vendor_id = v.id)
 
 414                  WHERE p.expense_accno_id = $id
 
 421       $fromdate_where =~ s/a\./ac\./;
 
 422       $todate_where   =~ s/a\./ac\./;
 
 432       ORDER BY $sortorder|;
 
 434   $sth = $dbh->prepare($query);
 
 435   $sth->execute || $form->dberror($query);
 
 437   while (my $ca = $sth->fetchrow_hashref(NAME_lc)) {
 
 440     if ($ca->{module} eq "gl") {
 
 441       $ca->{module} = "gl";
 
 445     if ($ca->{module} eq "ap") {
 
 446       $ca->{module} = ($ca->{invoice}) ? 'ir' : 'ap';
 
 450     if ($ca->{module} eq "ar") {
 
 451       $ca->{module} = ($ca->{invoice}) ? 'is' : 'ar';
 
 454     if ($ca->{amount} < 0) {
 
 455       $ca->{debit}  = $ca->{amount} * -1;
 
 458       $ca->{credit} = $ca->{amount};
 
 462     push @{ $form->{CA} }, $ca;
 
 469   $main::lxdebug->leave_sub();