c1b8b7d6e5c5dcfa73e09d144b324d4cf5e84c44
[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   $::request->{layout}->focus('#projectnumber');
96
97   $form->header();
98   print $form->parse_html_template('projects/search');
99
100   $main::lxdebug->leave_sub();
101 }
102
103 sub project_report {
104   $main::lxdebug->enter_sub();
105
106   $main::auth->assert('project_edit');
107
108   my $form     = $main::form;
109   my %myconfig = %main::myconfig;
110   my $locale   = $main::locale;
111
112   $form->{sort} ||= 'projectnumber';
113   my $filter      = $form->{filter} || { };
114
115   Projects->search_projects(%{ $filter }, 'sort' => $form->{sort});
116
117   my $cvar_configs = CVar->get_configs('module' => 'Projects');
118
119   my $report       = SL::ReportGenerator->new(\%myconfig, $form);
120
121   my @columns      = qw(projectnumber description customer type active valid);
122
123   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
124   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $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
136   my @hidden_vars  = (
137     'filter',
138     map({ ('cvar_'. $_->{name} , 'l_cvar_'. $_->{name}) } @includeable_custom_variables),
139     map({'cvar_'. $_->{name} .'_qtyop'} grep({$_->{type} eq 'number'} @searchable_custom_variables)),
140   );
141   my $href         = build_std_url('action=project_report', @hidden_vars);
142
143
144   my %column_defs  = (
145     'projectnumber'            => { 'text' => $locale->text('Number'), },
146     'description'              => { 'text' => $locale->text('Description'), },
147     'customer'                 => { 'text' => $locale->text('Customer'), },
148     'type'                     => { 'text' => $locale->text('Type'), },
149     'active'                   => { 'text' => $locale->text('Active'), 'visible' => 'both' eq $filter->{active}, },
150     'valid'                    => { 'text' => $locale->text('Valid'),  'visible' => 'both' eq $filter->{active}, },
151     %column_defs_cvars,
152     );
153
154   foreach (qw(projectnumber description customer type)) {
155     $column_defs{$_}->{link}    = $href . "&sort=$_";
156     $column_defs{$_}->{visible} = 1;
157   }
158
159   $report->set_columns(%column_defs);
160   $report->set_column_order(@columns);
161
162   $report->set_export_options('project_report', @hidden_vars, 'sort');
163
164   CVar->add_custom_variables_to_report('module'         => 'Project',
165                                        'trans_id_field' => 'id',
166                                        'configs'        => $cvar_configs,
167                                        'column_defs'    => \%column_defs,
168                                        'data'           => $form->{project_list},
169                                        );
170
171   $report->set_sort_indicator($form->{sort}, 1);
172
173   my @options;
174   push @options, $locale->text('All')                                            if ($filter->{all});
175   push @options, $locale->text('Orphaned')                                       if ($filter->{orphaned});
176   push @options, $locale->text('Project Number') . " : $filter->{projectnumber}" if ($filter->{projectnumber});
177   push @options, $locale->text('Description')    . " : $filter->{description}"   if ($filter->{description});
178   push @options, $locale->text('Customer')       . " : $filter->{customer}"      if ($filter->{customer});
179   push @options, $locale->text('Type')           . " : $filter->{type}"          if ($filter->{type});
180   push @options, $locale->text('Active')                                         if ($filter->{active} eq 'active');
181   push @options, $locale->text('Inactive')                                       if ($filter->{active} eq 'inactive');
182   push @options, $locale->text('Orphaned')                                       if ($filter->{status} eq 'orphaned');
183
184   $form->{title} = $locale->text('Projects');
185
186   $report->set_options('top_info_text'       => join("\n", @options),
187                        'output_format'       => 'HTML',
188                        'title'               => $form->{title},
189                        'attachment_basename' => $locale->text('project_list') . strftime('_%Y%m%d', localtime time),
190     );
191   $report->set_options_from_form();
192   $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
193
194   CVar->add_custom_variables_to_report('module'         => 'Projects',
195                                        'trans_id_field' => 'id',
196                                        'configs'        => $cvar_configs,
197                                        'column_defs'    => \%column_defs,
198                                        'data'           => $form->{project_list});
199
200   my $edit_url = build_std_url('action=edit&type=project');
201   my $callback = $form->escape($href) . '&sort=' . E($form->{sort});
202
203   foreach my $project (@{ $form->{project_list} }) {
204     $project->{active} = $project->{active} ? $locale->text('Yes')  : $locale->text('No');
205     $project->{valid}  = $project->{valid} ? $locale->text('Yes')  : $locale->text('No');
206
207     my $row = { map { $_ => { 'data' => $project->{$_} } } keys %{ $project } };
208
209     $row->{projectnumber}->{link} = $edit_url . "&id=" . E($project->{id}) . "&callback=${callback}";
210
211     $report->add_data($row);
212   }
213
214   $report->generate_with_headers();
215
216   $main::lxdebug->leave_sub();
217 }
218
219 sub display_project_form {
220   $main::lxdebug->enter_sub();
221
222   $main::auth->assert('project_edit');
223
224   my $form     = $main::form;
225   my $locale   = $main::locale;
226
227   $form->{project} ||= { };
228
229   $form->{title}     = $form->{project}->{id} ? $locale->text("Edit Project") : $locale->text("Add Project");
230
231   $form->{ALL_CUSTOMERS}    = SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, id => $form->{project}->{customer_id} ]]);
232   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'Projects', 'trans_id' => $form->{project}->{id});
233 #  $main::lxdebug->dump(0, "cv", $form->{CUSTOM_VARIABLES});
234   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
235
236   $form->header();
237   print $form->parse_html_template('projects/project_form');
238
239   $main::lxdebug->leave_sub();
240 }
241
242 sub save {
243   $main::lxdebug->enter_sub();
244
245   $main::auth->assert('project_edit');
246
247   my $form     = $main::form;
248   my %myconfig = %main::myconfig;
249   my $locale   = $main::locale;
250
251   $form->isblank("project.projectnumber", $locale->text('Project Number missing!'));
252
253   my $project    = $form->{project} || { };
254   my $is_new     = !$project->{id};
255   $project->{id} = Projects->save_project(%{ $project });
256
257   # saving the history
258   if(!exists $form->{addition} && $project->{id} ne "") {
259     $form->{id}       = $project->{id};
260     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
261     $form->{addition} = "SAVED";
262     $form->save_history;
263   }
264   # /saving the history
265
266   if ($form->{callback}) {
267     map { $form->{callback} .= "&new_${_}=" . $form->escape($project->{$_}); } qw(projectnumber description id);
268     my $message              = $is_new ? $locale->text('The project has been added.') : $locale->text('The project has been saved.');
269     $form->{callback}       .= "&message="  . E($message);
270   }
271
272   $form->redirect($locale->text('Project saved!'));
273
274   $main::lxdebug->leave_sub();
275 }
276
277 sub save_as_new {
278   $main::lxdebug->enter_sub();
279
280   my $form     = $main::form;
281   my $locale   = $main::locale;
282
283   delete $form->{project}->{id} if ($form->{project});
284   save();
285
286   $main::lxdebug->leave_sub();
287 }
288
289 sub delete {
290   $main::lxdebug->enter_sub();
291
292   $main::auth->assert('project_edit');
293
294   my $form     = $main::form;
295   my %myconfig = %main::myconfig;
296   my $locale   = $main::locale;
297
298   my $project = $form->{project} || { };
299   Projects->delete_project('id' => $project->{id});
300
301   # saving the history
302   if(!exists $form->{addition}) {
303     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
304     $form->{addition} = "DELETED";
305     $form->save_history;
306   }
307   # /saving the history
308
309   $form->redirect($locale->text('Project deleted!'));
310
311   $main::lxdebug->leave_sub();
312 }
313
314 sub continue {
315   call_sub($main::form->{nextsub});
316 }