Benutzerdefinierte Variablen für Projekte implementiert.
[kivitendo-erp.git] / bin / mozilla / projects.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
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.
20 #
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 #======================================================================
29 #
30 # project administration
31 #======================================================================
32
33 use POSIX qw(strftime);
34
35 use SL::CVar;
36 use SL::Projects;
37 use SL::ReportGenerator;
38
39 require "bin/mozilla/common.pl";
40 require "bin/mozilla/reportgenerator.pl";
41
42 sub add {
43   $lxdebug->enter_sub();
44
45   $auth->assert('project_edit');
46
47   # construct callback
48   $form->{callback} = build_std_url('action') unless $form->{callback};
49
50   display_project_form();
51
52   $lxdebug->leave_sub();
53 }
54
55 sub edit {
56   $lxdebug->enter_sub();
57
58   $auth->assert('project_edit');
59
60   # show history button
61   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
62   #/show hhistory button
63   $form->{title} = "Edit";
64
65   $form->{project} = Projects->get_project('id' => $form->{id}, 'orphaned' => 1);
66
67   display_project_form();
68
69   $lxdebug->leave_sub();
70 }
71
72 sub search {
73   $lxdebug->enter_sub();
74
75   $auth->assert('project_edit');
76
77   $form->{title} = $locale->text('Projects');
78
79   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'Projects');
80   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
81    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
82                                                                            'include_prefix' => 'l_',
83                                                                            'include_value'  => 'Y');
84
85   $form->header();
86   print $form->parse_html_template('projects/search');
87
88   $lxdebug->leave_sub();
89 }
90
91 sub project_report {
92   $lxdebug->enter_sub();
93
94   $auth->assert('project_edit');
95
96   $form->{sort} ||= 'projectnumber';
97   my $filter      = $form->{filter} || { };
98
99   Projects->search_projects(%{ $filter }, 'sort' => $form->{sort});
100
101   my $cvar_configs = CVar->get_configs('module' => 'Projects');
102
103   my $report       = SL::ReportGenerator->new(\%myconfig, $form);
104
105   my @columns      = qw(projectnumber description active);
106   my @hidden_vars  = ('filter');
107   my $href         = build_std_url('action=project_report', @hidden_vars);
108
109   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
110   my %column_defs_cvars            = ();
111   foreach (@includeable_custom_variables) {
112     $column_defs_cvars{"cvar_$_->{name}"} = {
113       'text'    => $_->{description},
114       'visible' => $form->{"l_cvar_$_->{name}"} eq 'Y',
115     };
116   }
117
118   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
119
120   my %column_defs  = (
121     'projectnumber'            => { 'text' => $locale->text('Number'), },
122     'description'              => { 'text' => $locale->text('Description'), },
123     'active'                   => { 'text' => $locale->text('Active'), 'visible' => 'both' eq $filter->{active}, },
124     %column_defs_cvars,
125     );
126
127   $main::lxdebug->dump(0, "cdc", \@columns);
128   $main::lxdebug->dump(0, "cdc", \%column_defs);
129
130   foreach (qw(projectnumber description)) {
131     $column_defs{$_}->{link}    = $href . "&sort=$_";
132     $column_defs{$_}->{visible} = 1;
133   }
134
135   $report->set_columns(%column_defs);
136   $report->set_column_order(@columns);
137
138   $report->set_export_options('project_report', @hidden_vars);
139
140   $report->set_sort_indicator($form->{sort}, 1);
141
142   my @options;
143   push @options, $locale->text('All')                                            if ($filter->{all});
144   push @options, $locale->text('Orphaned')                                       if ($filter->{orphaned});
145   push @options, $locale->text('Project Number') . " : $filter->{projectnumber}" if ($filter->{projectnumber});
146   push @options, $locale->text('Description') . " : $filter->{description}"      if ($filter->{description});
147   push @options, $locale->text('Active')                                         if ($filter->{active} eq 'active');
148   push @options, $locale->text('Inactive')                                       if ($filter->{active} eq 'inactive');
149   push @options, $locale->text('Orphaned')                                       if ($filter->{status} eq 'orphaned');
150
151   $form->{title} = $locale->text('Projects');
152
153   $report->set_options('top_info_text'       => join("\n", @options),
154                        'output_format'       => 'HTML',
155                        'title'               => $form->{title},
156                        'attachment_basename' => $locale->text('project_list') . strftime('_%Y%m%d', localtime time),
157     );
158   $report->set_options_from_form();
159
160   CVar->add_custom_variables_to_report('module'         => 'Projects',
161                                        'trans_id_field' => 'id',
162                                        'configs'        => $cvar_configs,
163                                        'column_defs'    => \%column_defs,
164                                        'data'           => $form->{project_list});
165
166   my $edit_url = build_std_url('action=edit&type=project');
167   my $callback = $form->escape($href) . '&sort=' . E($form->{sort});
168
169   foreach $project (@{ $form->{project_list} }) {
170     $project->{active} = $project->{active} ? $locale->text('Yes')  : $locale->text('No');
171
172     my $row = { map { $_ => { 'data' => $project->{$_} } } keys %{ $project } };
173
174     $row->{projectnumber}->{link} = $edit_url . "&id=" . E($project->{id}) . "&callback=${callback}";
175
176     $report->add_data($row);
177   }
178
179   $report->generate_with_headers();
180
181   $lxdebug->leave_sub();
182 }
183
184 sub display_project_form {
185   $lxdebug->enter_sub();
186
187   $auth->assert('project_edit');
188
189   $form->{project} ||= { };
190
191   $form->{title}     = $form->{project}->{id} ? $locale->text("Edit Project") : $locale->text("Add Project");
192
193   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'Projects', 'trans_id' => $form->{project}->{id});
194   $main::lxdebug->dump(0, "cv", $form->{CUSTOM_VARIABLES});
195   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
196
197   $form->header();
198   print $form->parse_html_template('projects/project_form');
199
200   $lxdebug->leave_sub();
201 }
202
203 sub save {
204   $lxdebug->enter_sub();
205
206   $auth->assert('project_edit');
207
208   $form->isblank("project.projectnumber", $locale->text('Project Number missing!'));
209
210   my $project    = $form->{project} || { };
211   my $is_new     = !$project->{id};
212   $project->{id} = Projects->save_project(%{ $project });
213
214   # saving the history
215   if(!exists $form->{addition} && $project->{id} ne "") {
216     $form->{id}       = $project->{id};
217     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
218         $form->{addition} = "SAVED";
219         $form->save_history($form->dbconnect(\%myconfig));
220   }
221   # /saving the history
222
223   if ($form->{callback}) {
224     map { $form->{callback} .= "&new_${_}=" . $form->escape($project->{$_}); } qw(projectnumber description id);
225     my $message              = $is_new ? $locale->text('The project has been added.') : $locale->text('The project has been saved.');
226     $form->{callback}       .= "&message="  . E($message);
227   }
228
229   $form->redirect($locale->text('Project saved!'));
230
231   $lxdebug->leave_sub();
232 }
233
234 sub save_as_new {
235   $lxdebug->enter_sub();
236
237   delete $form->{project}->{id} if ($form->{project});
238   save();
239
240   $lxdebug->leave_sub();
241 }
242
243 sub delete {
244   $lxdebug->enter_sub();
245
246   $auth->assert('project_edit');
247
248   my $project = $form->{project} || { };
249   Projects->delete_project('id' => $project->{id});
250
251   # saving the history
252   if(!exists $form->{addition}) {
253     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
254         $form->{addition} = "DELETED";
255         $form->save_history($form->dbconnect(\%myconfig));
256   }
257   # /saving the history
258
259   $form->redirect($locale->text('Project deleted!'));
260
261   $lxdebug->leave_sub();
262 }
263
264 sub continue {
265   call_sub($form->{nextsub});
266 }