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
 
  16 # This program is free software; you can redistribute it and/or modify
 
  17 # it under the terms of the GNU General Public License as published by
 
  18 # the Free Software Foundation; either version 2 of the License, or
 
  19 # (at your option) any later version.
 
  21 # This program is distributed in the hope that it will be useful,
 
  22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  24 # GNU General Public License for more details.
 
  25 # You should have received a copy of the GNU General Public License
 
  26 # along with this program; if not, write to the Free Software
 
  27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  28 #======================================================================
 
  30 # project administration
 
  31 #======================================================================
 
  33 use POSIX qw(strftime);
 
  37 use SL::ReportGenerator;
 
  39 require "bin/mozilla/common.pl";
 
  40 require "bin/mozilla/reportgenerator.pl";
 
  45   $main::lxdebug->enter_sub();
 
  47   $main::auth->assert('project_edit');
 
  49   my $form     = $main::form;
 
  50   my $locale   = $main::locale;
 
  53   $form->{callback} = build_std_url('action') unless $form->{callback};
 
  55   display_project_form();
 
  57   $main::lxdebug->leave_sub();
 
  61   $main::lxdebug->enter_sub();
 
  63   $main::auth->assert('project_edit');
 
  65   my $form     = $main::form;
 
  66   my $locale   = $main::locale;
 
  69   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
 
  70   #/show hhistory button
 
  71   $form->{title} = "Edit";
 
  73   $form->{project} = Projects->get_project('id' => $form->{id}, 'orphaned' => 1);
 
  75   display_project_form();
 
  77   $main::lxdebug->leave_sub();
 
  81   $main::lxdebug->enter_sub();
 
  83   $main::auth->assert('project_edit');
 
  85   my $form     = $main::form;
 
  86   my $locale   = $main::locale;
 
  88   $form->{title} = $locale->text('Projects');
 
  90   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Projects');
 
  91   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
 
  92    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
 
  93                                                                            'include_prefix' => 'l_',
 
  94                                                                            'include_value'  => 'Y');
 
  95   $form->{fokus}    = 'Form.projectnumber';
 
  98   print $form->parse_html_template('projects/search');
 
 100   $main::lxdebug->leave_sub();
 
 104   $main::lxdebug->enter_sub();
 
 106   $main::auth->assert('project_edit');
 
 108   my $form     = $main::form;
 
 109   my %myconfig = %main::myconfig;
 
 110   my $locale   = $main::locale;
 
 112   $form->{sort} ||= 'projectnumber';
 
 113   my $filter      = $form->{filter} || { };
 
 115   Projects->search_projects(%{ $filter }, 'sort' => $form->{sort});
 
 117   my $cvar_configs = CVar->get_configs('module' => 'Projects');
 
 119   my $report       = SL::ReportGenerator->new(\%myconfig, $form);
 
 121   my @columns      = qw(projectnumber description active);
 
 123   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
 
 124   my %column_defs_cvars            = ();
 
 125   foreach (@includeable_custom_variables) {
 
 126     $column_defs_cvars{"cvar_$_->{name}"} = {
 
 127       'text'    => $_->{description},
 
 128       'visible' => $form->{"l_cvar_$_->{name}"} eq 'Y',
 
 132   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
 
 135   my @hidden_vars  = ('filter', map { ('cvar_'. $_->{name} , 'l_cvar_'. $_->{name}) } @includeable_custom_variables);
 
 136   my $href         = build_std_url('action=project_report', @hidden_vars);
 
 140     'projectnumber'            => { 'text' => $locale->text('Number'), },
 
 141     'description'              => { 'text' => $locale->text('Description'), },
 
 142     'active'                   => { 'text' => $locale->text('Active'), 'visible' => 'both' eq $filter->{active}, },
 
 146   foreach (qw(projectnumber description)) {
 
 147     $column_defs{$_}->{link}    = $href . "&sort=$_";
 
 148     $column_defs{$_}->{visible} = 1;
 
 151   $report->set_columns(%column_defs);
 
 152   $report->set_column_order(@columns);
 
 154   $report->set_export_options('project_report', @hidden_vars, 'sort');
 
 156   CVar->add_custom_variables_to_report('module'         => 'Project',
 
 157                                        'trans_id_field' => 'id',
 
 158                                        'configs'        => $cvar_configs,
 
 159                                        'column_defs'    => \%column_defs,
 
 160                                        'data'           => $form->{project_list},
 
 163   $report->set_sort_indicator($form->{sort}, 1);
 
 166   push @options, $locale->text('All')                                            if ($filter->{all});
 
 167   push @options, $locale->text('Orphaned')                                       if ($filter->{orphaned});
 
 168   push @options, $locale->text('Project Number') . " : $filter->{projectnumber}" if ($filter->{projectnumber});
 
 169   push @options, $locale->text('Description') . " : $filter->{description}"      if ($filter->{description});
 
 170   push @options, $locale->text('Active')                                         if ($filter->{active} eq 'active');
 
 171   push @options, $locale->text('Inactive')                                       if ($filter->{active} eq 'inactive');
 
 172   push @options, $locale->text('Orphaned')                                       if ($filter->{status} eq 'orphaned');
 
 174   $form->{title} = $locale->text('Projects');
 
 176   $report->set_options('top_info_text'       => join("\n", @options),
 
 177                        'output_format'       => 'HTML',
 
 178                        'title'               => $form->{title},
 
 179                        'attachment_basename' => $locale->text('project_list') . strftime('_%Y%m%d', localtime time),
 
 181   $report->set_options_from_form();
 
 182   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
 184   CVar->add_custom_variables_to_report('module'         => 'Projects',
 
 185                                        'trans_id_field' => 'id',
 
 186                                        'configs'        => $cvar_configs,
 
 187                                        'column_defs'    => \%column_defs,
 
 188                                        'data'           => $form->{project_list});
 
 190   my $edit_url = build_std_url('action=edit&type=project');
 
 191   my $callback = $form->escape($href) . '&sort=' . E($form->{sort});
 
 193   foreach my $project (@{ $form->{project_list} }) {
 
 194     $project->{active} = $project->{active} ? $locale->text('Yes')  : $locale->text('No');
 
 196     my $row = { map { $_ => { 'data' => $project->{$_} } } keys %{ $project } };
 
 198     $row->{projectnumber}->{link} = $edit_url . "&id=" . E($project->{id}) . "&callback=${callback}";
 
 200     $report->add_data($row);
 
 203   $report->generate_with_headers();
 
 205   $main::lxdebug->leave_sub();
 
 208 sub display_project_form {
 
 209   $main::lxdebug->enter_sub();
 
 211   $main::auth->assert('project_edit');
 
 213   my $form     = $main::form;
 
 214   my $locale   = $main::locale;
 
 216   $form->{project} ||= { };
 
 218   $form->{title}     = $form->{project}->{id} ? $locale->text("Edit Project") : $locale->text("Add Project");
 
 220   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'Projects', 'trans_id' => $form->{project}->{id});
 
 221 #  $main::lxdebug->dump(0, "cv", $form->{CUSTOM_VARIABLES});
 
 222   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
 
 225   print $form->parse_html_template('projects/project_form');
 
 227   $main::lxdebug->leave_sub();
 
 231   $main::lxdebug->enter_sub();
 
 233   $main::auth->assert('project_edit');
 
 235   my $form     = $main::form;
 
 236   my %myconfig = %main::myconfig;
 
 237   my $locale   = $main::locale;
 
 239   $form->isblank("project.projectnumber", $locale->text('Project Number missing!'));
 
 241   my $project    = $form->{project} || { };
 
 242   my $is_new     = !$project->{id};
 
 243   $project->{id} = Projects->save_project(%{ $project });
 
 246   if(!exists $form->{addition} && $project->{id} ne "") {
 
 247     $form->{id}       = $project->{id};
 
 248     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
 
 249     $form->{addition} = "SAVED";
 
 252   # /saving the history
 
 254   if ($form->{callback}) {
 
 255     map { $form->{callback} .= "&new_${_}=" . $form->escape($project->{$_}); } qw(projectnumber description id);
 
 256     my $message              = $is_new ? $locale->text('The project has been added.') : $locale->text('The project has been saved.');
 
 257     $form->{callback}       .= "&message="  . E($message);
 
 260   $form->redirect($locale->text('Project saved!'));
 
 262   $main::lxdebug->leave_sub();
 
 266   $main::lxdebug->enter_sub();
 
 268   my $form     = $main::form;
 
 269   my $locale   = $main::locale;
 
 271   delete $form->{project}->{id} if ($form->{project});
 
 274   $main::lxdebug->leave_sub();
 
 278   $main::lxdebug->enter_sub();
 
 280   $main::auth->assert('project_edit');
 
 282   my $form     = $main::form;
 
 283   my %myconfig = %main::myconfig;
 
 284   my $locale   = $main::locale;
 
 286   my $project = $form->{project} || { };
 
 287   Projects->delete_project('id' => $project->{id});
 
 290   if(!exists $form->{addition}) {
 
 291     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
 
 292     $form->{addition} = "DELETED";
 
 295   # /saving the history
 
 297   $form->redirect($locale->text('Project deleted!'));
 
 299   $main::lxdebug->leave_sub();
 
 303   call_sub($main::form->{nextsub});