0e74d53e2277d7cb5d58f9ce3032bb34b307cf4c
[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 use strict;
43
44 sub add {
45   $main::lxdebug->enter_sub();
46
47   $main::auth->assert('project_edit');
48
49   my $form     = $main::form;
50   my $locale   = $main::locale;
51
52   # construct callback
53   $form->{callback} = build_std_url('action') unless $form->{callback};
54
55   display_project_form();
56
57   $main::lxdebug->leave_sub();
58 }
59
60 sub edit {
61   $main::lxdebug->enter_sub();
62
63   $main::auth->assert('project_edit');
64
65   my $form     = $main::form;
66   my $locale   = $main::locale;
67
68   # show history button
69   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
70   #/show hhistory button
71   $form->{title} = "Edit";
72
73   $form->{project} = Projects->get_project('id' => $form->{id}, 'orphaned' => 1);
74
75   display_project_form();
76
77   $main::lxdebug->leave_sub();
78 }
79
80 sub search {
81   $main::lxdebug->enter_sub();
82
83   $main::auth->assert('project_edit');
84
85   my $form     = $main::form;
86   my $locale   = $main::locale;
87
88   $form->{title} = $locale->text('Projects');
89
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
96   $form->header();
97   print $form->parse_html_template('projects/search');
98
99   $main::lxdebug->leave_sub();
100 }
101
102 sub project_report {
103   $main::lxdebug->enter_sub();
104
105   $main::auth->assert('project_edit');
106
107   my $form     = $main::form;
108   my %myconfig = %main::myconfig;
109   my $locale   = $main::locale;
110
111   $form->{sort} ||= 'projectnumber';
112   my $filter      = $form->{filter} || { };
113
114   Projects->search_projects(%{ $filter }, 'sort' => $form->{sort});
115
116   my $cvar_configs = CVar->get_configs('module' => 'Projects');
117
118   my $report       = SL::ReportGenerator->new(\%myconfig, $form);
119
120   my @columns      = qw(projectnumber description active);
121   my @hidden_vars  = ('filter');
122   my $href         = build_std_url('action=project_report', @hidden_vars);
123
124   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
125   my %column_defs_cvars            = ();
126   foreach (@includeable_custom_variables) {
127     $column_defs_cvars{"cvar_$_->{name}"} = {
128       'text'    => $_->{description},
129       'visible' => $form->{"l_cvar_$_->{name}"} eq 'Y',
130     };
131   }
132
133   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
134
135   my %column_defs  = (
136     'projectnumber'            => { 'text' => $locale->text('Number'), },
137     'description'              => { 'text' => $locale->text('Description'), },
138     'active'                   => { 'text' => $locale->text('Active'), 'visible' => 'both' eq $filter->{active}, },
139     %column_defs_cvars,
140     );
141
142   foreach (qw(projectnumber description)) {
143     $column_defs{$_}->{link}    = $href . "&sort=$_";
144     $column_defs{$_}->{visible} = 1;
145   }
146
147   $report->set_columns(%column_defs);
148   $report->set_column_order(@columns);
149
150   $report->set_export_options('project_report', @hidden_vars, 'sort');
151
152   $report->set_sort_indicator($form->{sort}, 1);
153
154   my @options;
155   push @options, $locale->text('All')                                            if ($filter->{all});
156   push @options, $locale->text('Orphaned')                                       if ($filter->{orphaned});
157   push @options, $locale->text('Project Number') . " : $filter->{projectnumber}" if ($filter->{projectnumber});
158   push @options, $locale->text('Description') . " : $filter->{description}"      if ($filter->{description});
159   push @options, $locale->text('Active')                                         if ($filter->{active} eq 'active');
160   push @options, $locale->text('Inactive')                                       if ($filter->{active} eq 'inactive');
161   push @options, $locale->text('Orphaned')                                       if ($filter->{status} eq 'orphaned');
162
163   $form->{title} = $locale->text('Projects');
164
165   $report->set_options('top_info_text'       => join("\n", @options),
166                        'output_format'       => 'HTML',
167                        'title'               => $form->{title},
168                        'attachment_basename' => $locale->text('project_list') . strftime('_%Y%m%d', localtime time),
169     );
170   $report->set_options_from_form();
171
172   CVar->add_custom_variables_to_report('module'         => 'Projects',
173                                        'trans_id_field' => 'id',
174                                        'configs'        => $cvar_configs,
175                                        'column_defs'    => \%column_defs,
176                                        'data'           => $form->{project_list});
177
178   my $edit_url = build_std_url('action=edit&type=project');
179   my $callback = $form->escape($href) . '&sort=' . E($form->{sort});
180
181   foreach my $project (@{ $form->{project_list} }) {
182     $project->{active} = $project->{active} ? $locale->text('Yes')  : $locale->text('No');
183
184     my $row = { map { $_ => { 'data' => $project->{$_} } } keys %{ $project } };
185
186     $row->{projectnumber}->{link} = $edit_url . "&id=" . E($project->{id}) . "&callback=${callback}";
187
188     $report->add_data($row);
189   }
190
191   $report->generate_with_headers();
192
193   $main::lxdebug->leave_sub();
194 }
195
196 sub display_project_form {
197   $main::lxdebug->enter_sub();
198
199   $main::auth->assert('project_edit');
200
201   my $form     = $main::form;
202   my $locale   = $main::locale;
203
204   $form->{project} ||= { };
205
206   $form->{title}     = $form->{project}->{id} ? $locale->text("Edit Project") : $locale->text("Add Project");
207
208   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'Projects', 'trans_id' => $form->{project}->{id});
209   $main::lxdebug->dump(0, "cv", $form->{CUSTOM_VARIABLES});
210   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
211
212   $form->header();
213   print $form->parse_html_template('projects/project_form');
214
215   $main::lxdebug->leave_sub();
216 }
217
218 sub save {
219   $main::lxdebug->enter_sub();
220
221   $main::auth->assert('project_edit');
222
223   my $form     = $main::form;
224   my %myconfig = %main::myconfig;
225   my $locale   = $main::locale;
226
227   $form->isblank("project.projectnumber", $locale->text('Project Number missing!'));
228
229   my $project    = $form->{project} || { };
230   my $is_new     = !$project->{id};
231   $project->{id} = Projects->save_project(%{ $project });
232
233   # saving the history
234   if(!exists $form->{addition} && $project->{id} ne "") {
235     $form->{id}       = $project->{id};
236     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
237     $form->{addition} = "SAVED";
238     $form->save_history($form->dbconnect(\%myconfig));
239   }
240   # /saving the history
241
242   if ($form->{callback}) {
243     map { $form->{callback} .= "&new_${_}=" . $form->escape($project->{$_}); } qw(projectnumber description id);
244     my $message              = $is_new ? $locale->text('The project has been added.') : $locale->text('The project has been saved.');
245     $form->{callback}       .= "&message="  . E($message);
246   }
247
248   $form->redirect($locale->text('Project saved!'));
249
250   $main::lxdebug->leave_sub();
251 }
252
253 sub save_as_new {
254   $main::lxdebug->enter_sub();
255
256   my $form     = $main::form;
257   my $locale   = $main::locale;
258
259   delete $form->{project}->{id} if ($form->{project});
260   save();
261
262   $main::lxdebug->leave_sub();
263 }
264
265 sub delete {
266   $main::lxdebug->enter_sub();
267
268   $main::auth->assert('project_edit');
269
270   my $form     = $main::form;
271   my %myconfig = %main::myconfig;
272   my $locale   = $main::locale;
273
274   my $project = $form->{project} || { };
275   Projects->delete_project('id' => $project->{id});
276
277   # saving the history
278   if(!exists $form->{addition}) {
279     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
280     $form->{addition} = "DELETED";
281     $form->save_history($form->dbconnect(\%myconfig));
282   }
283   # /saving the history
284
285   $form->redirect($locale->text('Project deleted!'));
286
287   $main::lxdebug->leave_sub();
288 }
289
290 sub continue {
291   call_sub($main::form->{nextsub});
292 }