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 #======================================================================
 
  42 my %project_id_column_prefixes  = ("ar"              => "global",
 
  45                                    "delivery_orders" => "global");
 
  47 my @tables_with_project_id_cols = qw(acc_trans
 
  55                                      delivery_order_items);
 
  58   $main::lxdebug->enter_sub();
 
  63   my $myconfig = \%main::myconfig;
 
  64   my $form     = $main::form;
 
  66   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  68   my (@filters, @values);
 
  70   foreach my $column (qw(projectnumber description)) {
 
  71     if ($params{$column}) {
 
  72       push @filters, "p.$column ILIKE ?";
 
  73       push @values, '%' . $params{$column} . '%';
 
  77   if ($params{status} eq 'orphaned') {
 
  80     foreach my $table (@tables_with_project_id_cols) {
 
  81       push @sub_filters, qq|SELECT DISTINCT $project_id_column_prefixes{$table}project_id FROM $table
 
  82                             WHERE NOT $project_id_column_prefixes{$table}project_id ISNULL|;
 
  85     push @filters, "p.id NOT IN (" . join(" UNION ", @sub_filters) . ")";
 
  88   if ($params{active} eq "active") {
 
  89     push @filters, 'p.active';
 
  91   } elsif ($params{active} eq "inactive") {
 
  92     push @filters, 'NOT COALESCE(p.active, FALSE)';
 
  95   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'Projects',
 
  96                                                             'trans_id_field' => 'p.id',
 
 100     push @filters, $cvar_where;
 
 101     push @values,  @cvar_values;
 
 105   my $where = 'WHERE ' . join(' AND ', map { "($_)" } @filters) if (scalar @filters);
 
 107   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
 
 108   $sortorder    =~ s/[^a-z_]//g;
 
 109   my $query     = qq|SELECT p.id, p.projectnumber, p.description, p.active
 
 112                      ORDER BY $sortorder|;
 
 114   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 116   $main::lxdebug->leave_sub();
 
 118   return scalar(@{ $form->{project_list} });
 
 122   $main::lxdebug->enter_sub();
 
 128     $main::lxdebug->leave_sub();
 
 132   my $myconfig = \%main::myconfig;
 
 133   my $form     = $main::form;
 
 135   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 137   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
 
 139   if ($params{orphaned}) {
 
 140     # check if it is orphaned
 
 141     my (@values, $query);
 
 143     foreach my $table (@tables_with_project_id_cols) {
 
 144       $query .= " + " if ($query);
 
 145       $query .= qq|(SELECT COUNT(*) FROM $table
 
 146                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
 
 147       push @values, conv_i($params{id});
 
 150     $query = 'SELECT ' . $query;
 
 152     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 153     $project->{orphaned}   = !$project->{orphaned};
 
 156   $main::lxdebug->leave_sub();
 
 162   $main::lxdebug->enter_sub();
 
 167   my $myconfig = \%main::myconfig;
 
 168   my $form     = $main::form;
 
 170   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 175     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
 
 176     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
 
 181   $query  = qq|UPDATE project SET projectnumber = ?, description = ?, active = ?
 
 184   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{id}));
 
 185   do_query($form, $dbh, $query, @values);
 
 187   CVar->save_custom_variables('dbh'       => $dbh,
 
 188                               'module'    => 'Projects',
 
 189                               'trans_id'  => $params{id},
 
 190                               'variables' => $form);
 
 194   $main::lxdebug->leave_sub();
 
 200   $main::lxdebug->enter_sub();
 
 205   Common::check_params(\%params, qw(id));
 
 207   my $myconfig = \%main::myconfig;
 
 208   my $form     = $main::form;
 
 210   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 212   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
 
 216   $main::lxdebug->leave_sub();