Projektverwaltung in eine eigene Datei ausgelagert und auf die Verwendung von Templat...
[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::Projects;
36 use SL::ReportGenerator;
37
38 require "bin/mozilla/common.pl";
39 require "bin/mozilla/reportgenerator.pl";
40
41 sub add {
42   $lxdebug->enter_sub();
43
44   $auth->assert('project_edit');
45
46   # construct callback
47   $form->{callback} = build_std_url('action') unless $form->{callback};
48
49   display_project_form();
50
51   $lxdebug->leave_sub();
52 }
53
54 sub edit {
55   $lxdebug->enter_sub();
56
57   $auth->assert('project_edit');
58
59   # show history button
60   $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
61   #/show hhistory button
62   $form->{title} = "Edit";
63
64   $form->{project} = Projects->get_project('id' => $form->{id}, 'orphaned' => 1);
65
66   display_project_form();
67
68   $lxdebug->leave_sub();
69 }
70
71 sub search {
72   $lxdebug->enter_sub();
73
74   $auth->assert('project_edit');
75
76   $form->{title} = $locale->text('Projects');
77
78   $form->header();
79   print $form->parse_html_template('projects/search');
80
81   $lxdebug->leave_sub();
82 }
83
84 sub project_report {
85   $lxdebug->enter_sub();
86
87   $auth->assert('project_edit');
88
89   $form->{sort} ||= 'projectnumber';
90
91   my $filter      = $form->{filter} || { };
92   Projects->search_projects(%{ $filter }, 'sort' => $form->{sort});
93
94   my $report      = SL::ReportGenerator->new(\%myconfig, $form);
95
96   my @columns     = qw(projectnumber description active);
97   my @hidden_vars = ('filter');
98   my $href        = build_std_url('action=project_report', @hidden_vars);
99
100   my %column_defs = (
101     'projectnumber'            => { 'text' => $locale->text('Number'), },
102     'description'              => { 'text' => $locale->text('Description'), },
103     'active'                   => { 'text' => $locale->text('Active'), 'visible' => 'both' eq $filter->{active}, },
104     );
105
106   foreach (qw(projectnumber description)) {
107     $column_defs{$_}->{link}    = $href . "&sort=$_";
108     $column_defs{$_}->{visible} = 1;
109   }
110
111   $report->set_columns(%column_defs);
112   $report->set_column_order(@columns);
113
114   $report->set_export_options('project_report', @hidden_vars);
115
116   $report->set_sort_indicator($form->{sort}, 1);
117
118   my @options;
119   push @options, $locale->text('All')                                            if ($filter->{all});
120   push @options, $locale->text('Orphaned')                                       if ($filter->{orphaned});
121   push @options, $locale->text('Project Number') . " : $filter->{projectnumber}" if ($filter->{projectnumber});
122   push @options, $locale->text('Description') . " : $filter->{description}"      if ($filter->{description});
123   push @options, $locale->text('Active')                                         if ($filter->{active} eq 'active');
124   push @options, $locale->text('Inactive')                                       if ($filter->{active} eq 'inactive');
125   push @options, $locale->text('Orphaned')                                       if ($filter->{status} eq 'orphaned');
126
127   $form->{title} = $locale->text('Projects');
128
129   $report->set_options('top_info_text'       => join("\n", @options),
130                        'output_format'       => 'HTML',
131                        'title'               => $form->{title},
132                        'attachment_basename' => $locale->text('project_list') . strftime('_%Y%m%d', localtime time),
133     );
134   $report->set_options_from_form();
135
136   my $edit_url = build_std_url('action=edit&type=project');
137   my $callback = $form->escape($href) . '&sort=' . E($form->{sort});
138
139   foreach $project (@{ $form->{project_list} }) {
140     $project->{active} = $project->{active} ? $locale->text('Yes')  : $locale->text('No');
141
142     my $row = { map { $_ => { 'data' => $project->{$_} } } keys %{ $project } };
143
144     $row->{projectnumber}->{link} = $edit_url . "&id=" . E($project->{id}) . "&callback=${callback}";
145
146     $report->add_data($row);
147   }
148
149   $report->generate_with_headers();
150
151   $lxdebug->leave_sub();
152 }
153
154 sub display_project_form {
155   $lxdebug->enter_sub();
156
157   $auth->assert('project_edit');
158
159   $form->{project} ||= { };
160
161   $form->{title}     = $form->{project}->{id} ? $locale->text("Edit Project") : $locale->text("Add Project");
162
163   $form->header();
164   print $form->parse_html_template('projects/project_form');
165
166   $lxdebug->leave_sub();
167 }
168
169 sub save {
170   $lxdebug->enter_sub();
171
172   $auth->assert('project_edit');
173
174   $form->isblank("project.projectnumber", $locale->text('Project Number missing!'));
175
176   my $project    = $form->{project} || { };
177   my $is_new     = !$project->{id};
178   $project->{id} = Projects->save_project(%{ $project });
179
180   # saving the history
181   if(!exists $form->{addition} && $project->{id} ne "") {
182     $form->{id}       = $project->{id};
183     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
184         $form->{addition} = "SAVED";
185         $form->save_history($form->dbconnect(\%myconfig));
186   }
187   # /saving the history
188
189   if ($form->{callback}) {
190     map { $form->{callback} .= "&new_${_}=" . $form->escape($project->{$_}); } qw(projectnumber description id);
191     my $message              = $is_new ? $locale->text('The project has been added.') : $locale->text('The project has been saved.');
192     $form->{callback}       .= "&message="  . E($message);
193   }
194
195   $form->redirect($locale->text('Project saved!'));
196
197   $lxdebug->leave_sub();
198 }
199
200 sub save_as_new {
201   $lxdebug->enter_sub();
202
203   delete $form->{project}->{id} if ($form->{project});
204   save();
205
206   $lxdebug->leave_sub();
207 }
208
209 sub delete {
210   $lxdebug->enter_sub();
211
212   $auth->assert('project_edit');
213
214   my $project = $form->{project} || { };
215   Projects->delete_project('id' => $project->{id});
216
217   # saving the history
218   if(!exists $form->{addition}) {
219     $form->{snumbers} = qq|projectnumber_| . $project->{projectnumber};
220         $form->{addition} = "DELETED";
221         $form->save_history($form->dbconnect(\%myconfig));
222   }
223   # /saving the history
224
225   $form->redirect($locale->text('Project deleted!'));
226
227   $lxdebug->leave_sub();
228 }
229
230 sub continue {
231   call_sub($form->{nextsub});
232 }