1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  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 #======================================================================
 
  41 my %project_id_column_prefixes  = ("ar"              => "global",
 
  44                                    "delivery_orders" => "global");
 
  46 my @tables_with_project_id_cols = qw(acc_trans
 
  54                                      delivery_order_items);
 
  57   $main::lxdebug->enter_sub();
 
  62   my $myconfig = \%main::myconfig;
 
  63   my $form     = $main::form;
 
  65   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  67   my (@filters, @values);
 
  69   foreach my $column (qw(projectnumber description)) {
 
  70     if ($params{$column}) {
 
  71       push @filters, "$column ILIKE ?";
 
  72       push @values, '%' . $params{$column} . '%';
 
  76   if ($params{status} eq 'orphaned') {
 
  79     foreach my $table (@tables_with_project_id_cols) {
 
  80       push @sub_filters, qq|SELECT DISTINCT $project_id_column_prefixes{$table}project_id FROM $table
 
  81                             WHERE NOT $project_id_column_prefixes{$table}project_id ISNULL|;
 
  84     push @filters, "id NOT IN (" . join(" UNION ", @sub_filters) . ")";
 
  87   if ($params{active} eq "active") {
 
  88     push @filters, 'active';
 
  90   } elsif ($params{active} eq "inactive") {
 
  91     push @filters, 'NOT COALESCE(active, FALSE)';
 
  94   my $where = 'WHERE ' . join(' AND ', map { "($_)" } @filters) if (scalar @filters);
 
  96   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
 
  97   $sortorder    =~ s/[^a-z_]//g;
 
  98   my $query     = qq|SELECT id, projectnumber, description, active
 
 101                      ORDER BY $sortorder|;
 
 103   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 105   $main::lxdebug->leave_sub();
 
 107   return scalar(@{ $form->{project_list} });
 
 111   $main::lxdebug->enter_sub();
 
 117     $main::lxdebug->leave_sub();
 
 121   my $myconfig = \%main::myconfig;
 
 122   my $form     = $main::form;
 
 124   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 126   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
 
 128   if ($params{orphaned}) {
 
 129     # check if it is orphaned
 
 130     my (@values, $query);
 
 132     foreach my $table (@tables_with_project_id_cols) {
 
 133       $query .= " + " if ($query);
 
 134       $query .= qq|(SELECT COUNT(*) FROM $table
 
 135                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
 
 136       push @values, conv_i($params{id});
 
 139     $query = 'SELECT ' . $query;
 
 141     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 142     $project->{orphaned}   = !$project->{orphaned};
 
 145   $main::lxdebug->leave_sub();
 
 151   $main::lxdebug->enter_sub();
 
 156   my $myconfig = \%main::myconfig;
 
 157   my $form     = $main::form;
 
 159   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 164     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
 165     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
 
 170   $query  = qq|UPDATE project SET projectnumber = ?, description = ?, active = ?
 
 173   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{id}));
 
 174   do_query($form, $dbh, $query, @values);
 
 178   $main::lxdebug->leave_sub();
 
 184   $main::lxdebug->enter_sub();
 
 189   Common::check_params(\%params, qw(id));
 
 191   my $myconfig = \%main::myconfig;
 
 192   my $form     = $main::form;
 
 194   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 196   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
 
 200   $main::lxdebug->leave_sub();