Projektverwaltung auf Rose- und Controller-Code umgestellt
[kivitendo-erp.git] / SL / Controller / Project.pm
1 package SL::Controller::Project;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use Clone qw(clone);
8
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::Paginated;
11 use SL::Controller::Helper::Sorted;
12 use SL::Controller::Helper::ParseFilter;
13 use SL::Controller::Helper::ReportGenerator;
14 use SL::CVar;
15 use SL::DB::Customer;
16 use SL::DB::Project;
17 use SL::Helper::Flash;
18 use SL::Locale::String;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22  scalar => [ qw(project db_args flat_filter) ],
23 );
24
25 __PACKAGE__->run_before('check_auth');
26 __PACKAGE__->run_before('load_project', only => [ qw(edit update destroy) ]);
27
28 __PACKAGE__->get_models_url_params('flat_filter');
29 __PACKAGE__->make_paginated(
30   MODEL         => 'Project',
31   PAGINATE_ARGS => 'db_args',
32   ONLY          => [ qw(list) ],
33 );
34
35 __PACKAGE__->make_sorted(
36   MODEL         => 'Project',
37   ONLY          => [ qw(list) ],
38
39   DEFAULT_BY    => 'projectnumber',
40   DEFAULT_DIR   => 1,
41
42   customer      => t8('Customer'),
43   description   => t8('Description'),
44   projectnumber => t8('Project Number'),
45   type          => t8('Type'),
46 );
47
48
49 #
50 # actions
51 #
52
53 sub action_search {
54   my ($self) = @_;
55
56   my %params;
57
58   $params{CUSTOM_VARIABLES} = CVar->get_configs(module => 'Projects');
59   ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
60     = CVar->render_search_options(variables      => $params{CUSTOM_VARIABLES},
61                                   include_prefix => 'l_',
62                                   include_value  => 'Y');
63
64   $self->render('project/search', %params);
65 }
66
67 sub action_list {
68   my ($self) = @_;
69
70   $self->setup_db_args_from_filter;
71   $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
72   # $self->make_filter_summary;
73
74   $self->prepare_report;
75
76   $self->{projects} = $self->get_models(%{ $self->db_args });
77
78   $self->list_objects;
79 }
80
81 sub action_new {
82   my ($self) = @_;
83
84   $self->project(SL::DB::Project->new);
85   $self->display_form(title    => $::locale->text('Create a new project'),
86                       callback => $::form->{callback} || $self->url_for(action => 'new'));
87 }
88
89 sub action_edit {
90   my ($self) = @_;
91   $self->display_form(title    => $::locale->text('Edit project #1', $self->project->projectnumber),
92                       callback => $::form->{callback} || $self->url_for(action => 'edit', id => $self->project->id));
93 }
94
95 sub action_create {
96   my ($self) = @_;
97
98   $self->project(SL::DB::Project->new);
99   $self->create_or_update;
100 }
101
102 sub action_update {
103   my ($self) = @_;
104   $self->create_or_update;
105 }
106
107 sub action_destroy {
108   my ($self) = @_;
109
110   if (eval { $self->project->delete; 1; }) {
111     flash_later('info',  $::locale->text('The project has been deleted.'));
112   } else {
113     flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
114   }
115
116   $self->redirect_to(action => 'search');
117 }
118
119 #
120 # filters
121 #
122
123 sub check_auth {
124   $::auth->assert('project_edit');
125 }
126
127 #
128 # helpers
129 #
130
131 sub display_form {
132   my ($self, %params) = @_;
133
134   $params{ALL_CUSTOMERS}    = SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, id => $self->project->customer_id ]]);
135   $params{CUSTOM_VARIABLES} = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
136   CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
137
138   $::request->{layout}->focus('#projectnumber');
139
140   $self->render('project/form', %params);
141 }
142
143 sub create_or_update {
144   my $self   = shift;
145   my $is_new = !$self->project->id;
146   my $params = delete($::form->{project}) || { };
147
148   delete $params->{id};
149   $self->project->assign_attributes(%{ $params });
150
151   my @errors = $self->project->validate;
152
153   if (@errors) {
154     flash('error', @errors);
155     $self->display_form(title    => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
156                         callback => $::form->{callback});
157     return;
158   }
159
160   $self->project->save;
161
162   CVar->save_custom_variables(
163     dbh          => $self->project->db->dbh,
164     module       => 'Projects',
165     trans_id     => $self->project->id,
166     variables    => $::form,
167     always_valid => 1,
168   );
169
170   flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
171
172   $self->redirect_to($::form->{callback} || (action => 'search'));
173 }
174
175 sub load_project {
176   my ($self) = @_;
177   $self->project(SL::DB::Project->new(id => $::form->{id})->load);
178 }
179
180 sub setup_db_args_from_filter {
181   my ($self) = @_;
182
183   $self->{filter} = {};
184   my %args = parse_filter(
185     $self->_pre_parse_filter($::form->{filter}, $self->{filter}),
186     with_objects => [ 'customer' ],
187     launder_to   => $self->{filter},
188   );
189
190   $self->db_args(\%args);
191 }
192
193 # unfortunately ParseFilter can't handle compount filters.
194 # so we clone the original filter (still need that for serializing)
195 # rip out the options we know an replace them with the compound options.
196 # ParseFilter will take care of the prefixing then.
197 sub _pre_parse_filter {
198   my ($self, $orig_filter, $launder_to) = @_;
199
200   return undef unless $orig_filter;
201
202   my $filter = clone($orig_filter);
203
204   $launder_to->{active} = delete $filter->{active};
205   if ($orig_filter->{active} ne 'both') {
206     push @{ $filter->{and} }, $orig_filter->{active} eq 'active' ? (active => 1) : (or => [ active => 0, active => undef ]);
207   }
208
209   $launder_to->{valid} = delete $filter->{valid};
210   if ($orig_filter->{valid} ne 'both') {
211     push @{ $filter->{and} }, $orig_filter->{valid} eq 'valid' ? (valid => 1) : (or => [ valid => 0, valid => undef ]);
212   }
213
214   $launder_to->{status} = delete $filter->{status};
215   if ($orig_filter->{status} ne 'all') {
216     push @{ $filter->{and} }, SL::DB::Manager::Project->is_not_used_filter;
217   }
218
219   return $filter;
220 }
221
222 sub prepare_report {
223   my ($self)      = @_;
224
225   my $callback    = $self->get_callback;
226
227   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
228   $self->{report} = $report;
229
230   my @columns     = qw(projectnumber description customer active valid type);
231   my @sortable    = qw(projectnumber description customer              type);
232
233   my %column_defs = (
234     projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
235     description   => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
236     type          => { },
237     customer      => { sub  => sub { $_[0]->customer ? $_[0]->customer->name     : '' } },
238     active        => { sub  => sub { $_[0]->active   ? $::locale->text('Active') : $::locale->text('Inactive') },
239                        text => $::locale->text('Active') },
240     valid         => { sub  => sub { $_[0]->valid    ? $::locale->text('Valid')  : $::locale->text('Invalid')  },
241                        text => $::locale->text('Valid')  },
242   );
243
244   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
245
246   $report->set_options(
247     std_column_visibility => 1,
248     controller_class      => 'Project',
249     output_format         => 'HTML',
250     top_info_text         => $::locale->text('Projects'),
251     raw_bottom_info_text  => $self->render('project/report_bottom', { no_output => 1, partial => 1 }),
252     title                 => $::locale->text('Projects'),
253     allow_pdf_export      => 1,
254     allow_csv_export      => 1,
255   );
256   $report->set_columns(%column_defs);
257   $report->set_column_order(@columns);
258   $report->set_export_options(qw(list filter));
259   $report->set_options_from_form;
260   $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
261
262   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
263
264   $self->{report_data} = {
265     column_defs        => \%column_defs,
266     columns            => \@columns,
267   };
268 }
269
270 sub list_objects {
271   my ($self)      = @_;
272   my $column_defs = $self->{report_data}->{column_defs};
273
274   for my $obj (@{ $self->{projects} || [] }) {
275     my %data = map {
276       $_ => {
277         data => $column_defs->{$_}{sub} ? $column_defs->{$_}{sub}->($obj)
278               : $obj->can($_)           ? $obj->$_
279               :                           $obj->{$_},
280         link => $column_defs->{$_}{obj_link} ? $column_defs->{$_}{obj_link}->($obj) : '',
281       },
282     } @{ $self->{report_data}{columns} || {} };
283
284     $self->{report}->add_data(\%data);
285   }
286
287   return $self->{report}->generate_with_headers;
288 }
289
290 1;