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 #======================================================================
 
  44 my %project_id_column_prefixes  = ("ar"              => "global",
 
  47                                    "delivery_orders" => "global");
 
  49 my @tables_with_project_id_cols = qw(acc_trans
 
  57                                      delivery_order_items);
 
  60   $main::lxdebug->enter_sub();
 
  65   my $myconfig = \%main::myconfig;
 
  66   my $form     = $main::form;
 
  68   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  70   my (@filters, @values);
 
  72   foreach my $column (qw(projectnumber description)) {
 
  73     if ($params{$column}) {
 
  74       push @filters, "p.$column ILIKE ?";
 
  75       push @values, '%' . $params{$column} . '%';
 
  79   if ($params{status} eq 'orphaned') {
 
  82     foreach my $table (@tables_with_project_id_cols) {
 
  83       push @sub_filters, qq|SELECT DISTINCT $project_id_column_prefixes{$table}project_id FROM $table
 
  84                             WHERE NOT $project_id_column_prefixes{$table}project_id ISNULL|;
 
  87     push @filters, "p.id NOT IN (" . join(" UNION ", @sub_filters) . ")";
 
  90   if ($params{active} eq "active") {
 
  91     push @filters, 'p.active';
 
  93   } elsif ($params{active} eq "inactive") {
 
  94     push @filters, 'NOT COALESCE(p.active, FALSE)';
 
  97   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'Projects',
 
  98                                                             'trans_id_field' => 'p.id',
 
 102     push @filters, $cvar_where;
 
 103     push @values,  @cvar_values;
 
 107   my $where = @filters ? 'WHERE ' . join(' AND ', map { "($_)" } @filters) : '';
 
 109   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
 
 110   $sortorder    =~ s/[^a-z_]//g;
 
 111   my $query     = qq|SELECT p.id, p.projectnumber, p.description, p.active
 
 114                      ORDER BY $sortorder|;
 
 116   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 118   $main::lxdebug->leave_sub();
 
 120   return scalar(@{ $form->{project_list} });
 
 124   $main::lxdebug->enter_sub();
 
 130     $main::lxdebug->leave_sub();
 
 134   my $myconfig = \%main::myconfig;
 
 135   my $form     = $main::form;
 
 137   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 139   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
 
 141   if ($params{orphaned}) {
 
 142     # check if it is orphaned
 
 143     my (@values, $query);
 
 145     foreach my $table (@tables_with_project_id_cols) {
 
 146       $query .= " + " if ($query);
 
 147       $query .= qq|(SELECT COUNT(*) FROM $table
 
 148                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
 
 149       push @values, conv_i($params{id});
 
 152     $query = 'SELECT ' . $query;
 
 154     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 155     $project->{orphaned}   = !$project->{orphaned};
 
 158   $main::lxdebug->leave_sub();
 
 164   $main::lxdebug->enter_sub();
 
 169   my $myconfig = \%main::myconfig;
 
 170   my $form     = $main::form;
 
 172   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 177     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
 178     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
 
 183   my $query  = qq|UPDATE project SET projectnumber = ?, description = ?, active = ?
 
 186   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{id}));
 
 187   do_query($form, $dbh, $query, @values);
 
 189   CVar->save_custom_variables('dbh'       => $dbh,
 
 190                               'module'    => 'Projects',
 
 191                               'trans_id'  => $params{id},
 
 192                               'variables' => $form,
 
 193                               'always_valid' => 1);
 
 197   $main::lxdebug->leave_sub();
 
 203   $main::lxdebug->enter_sub();
 
 208   Common::check_params(\%params, qw(id));
 
 210   my $myconfig = \%main::myconfig;
 
 211   my $form     = $main::form;
 
 213   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 215   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
 
 219   $main::lxdebug->leave_sub();