Pflichtenhefte bearbeiten
[kivitendo-erp.git] / SL / Controller / RequirementSpec.pm
1 package SL::Controller::RequirementSpec;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Controller::Helper::GetModels;
8 use SL::Controller::Helper::Paginated;
9 use SL::Controller::Helper::Sorted;
10 use SL::Controller::Helper::ParseFilter;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::DB::Customer;
13 use SL::DB::Project;
14 use SL::DB::RequirementSpecStatus;
15 use SL::DB::RequirementSpecType;
16 use SL::DB::RequirementSpec;
17 use SL::Helper::Flash;
18 use SL::Locale::String;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22  scalar => [ qw(requirement_spec requirement_spec_item customers projects types statuses db_args flat_filter is_template) ],
23 );
24
25 __PACKAGE__->run_before('setup');
26 __PACKAGE__->run_before('load_requirement_spec',      only => [ qw(    edit        update show destroy tree) ]);
27 __PACKAGE__->run_before('load_select_options',        only => [ qw(new edit create update list) ]);
28 __PACKAGE__->run_before('load_search_select_options', only => [ qw(                       list) ]);
29
30 __PACKAGE__->get_models_url_params('flat_filter');
31 __PACKAGE__->make_paginated(
32   MODEL         => 'RequirementSpec',
33   PAGINATE_ARGS => 'db_args',
34   ONLY          => [ qw(list) ],
35 );
36
37 __PACKAGE__->make_sorted(
38   MODEL         => 'RequirementSpec',
39   ONLY          => [ qw(list) ],
40
41   DEFAULT_BY    => 'customer',
42   DEFAULT_DIR   => 1,
43
44   customer      => t8('Customer'),
45   title         => t8('Title'),
46   type          => t8('Requirement Spec Type'),
47   status        => t8('Requirement Spec Status'),
48   projectnumber => t8('Project Number'),
49 );
50
51 #
52 # actions
53 #
54
55 sub action_list {
56   my ($self) = @_;
57
58   $self->setup_db_args_from_filter;
59   $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
60
61   $self->prepare_report;
62
63   my $requirement_specs = $self->get_models(%{ $self->db_args });
64
65   $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
66 }
67
68 sub action_new {
69   my ($self) = @_;
70
71   $self->{requirement_spec} = SL::DB::RequirementSpec->new;
72   $self->render('requirement_spec/form', title => t8('Create a new requirement spec'));
73 }
74
75 sub action_edit {
76   my ($self) = @_;
77   $self->render('requirement_spec/form', title => t8('Edit requirement spec'));
78 }
79
80 sub action_show {
81   my ($self) = @_;
82
83   my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
84   $self->requirement_spec_item($item);
85
86   $self->render('requirement_spec/show', title => t8('Show requirement spec'));
87 }
88
89 sub action_create {
90   my ($self) = @_;
91
92   $self->{requirement_spec} = SL::DB::RequirementSpec->new;
93   $self->create_or_update;
94 }
95
96 sub action_update {
97   my ($self) = @_;
98   $self->create_or_update;
99 }
100
101 sub action_destroy {
102   my ($self) = @_;
103
104   if (eval { $self->{requirement_spec}->delete; 1; }) {
105     flash_later('info',  t8('The requirement spec has been deleted.'));
106   } else {
107     flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
108   }
109
110   $self->redirect_to(action => 'list');
111 }
112
113 sub action_reorder {
114   my ($self) = @_;
115
116   SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_id} || [] });
117
118   $self->render('1;', { type => 'js', inline => 1 });
119 }
120
121 sub action_tree {
122   my ($self) = @_;
123   my $r = $self->render('requirement_spec/tree', now => DateTime->now);
124 }
125
126 #
127 # filters
128 #
129
130 sub setup {
131   my ($self) = @_;
132
133   $::auth->assert('config');
134   $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
135   $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu requirement_spec);
136   $self->is_template($::form->{is_template} ? 1 : 0);
137
138   return 1;
139 }
140
141 sub load_requirement_spec {
142   my ($self) = @_;
143   $self->{requirement_spec} = SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec";
144 }
145
146 sub load_select_options {
147   my ($self) = @_;
148
149   my @filter = ('!obsolete' => 1);
150   if ($self->requirement_spec && $self->requirement_spec->customer_id) {
151     @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] );
152   }
153
154   $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
155   $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
156   $self->types(    SL::DB::Manager::RequirementSpecType->get_all_sorted);
157 }
158
159 sub load_search_select_options {
160   my ($self) = @_;
161
162   $self->projects(SL::DB::Manager::Project->get_all_sorted);
163 }
164
165 #
166 # helpers
167 #
168
169 sub create_or_update {
170   my $self   = shift;
171   my $is_new = !$self->{requirement_spec}->id;
172   my $params = delete($::form->{requirement_spec}) || { };
173   my $title  = $is_new ? t8('Create a new requirement spec') : t8('Edit requirement spec');
174
175   $self->{requirement_spec}->assign_attributes(%{ $params });
176
177   my @errors = $self->{requirement_spec}->validate;
178
179   if (@errors) {
180     flash('error', @errors);
181     $self->render('requirement_spec/form', title => $title);
182     return;
183   }
184
185   $self->{requirement_spec}->save;
186
187   flash_later('info', $is_new ? t8('The requirement spec has been created.') : t8('The requirement spec has been saved.'));
188   $self->redirect_to(action => 'list');
189 }
190
191 sub setup_db_args_from_filter {
192   my ($self) = @_;
193
194   $self->{filter} = {};
195   my %args = parse_filter(
196     $::form->{filter},
197     with_objects => [ 'customer', 'type', 'status', 'project' ],
198     launder_to   => $self->{filter},
199   );
200
201   $args{where} = [
202     and => [
203       @{ $args{where} || [] },
204       is_template => $self->is_template
205     ]];
206
207   $self->db_args(\%args);
208 }
209
210 sub prepare_report {
211   my ($self)      = @_;
212
213   my $callback    = $self->get_callback;
214
215   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
216   $self->{report} = $report;
217
218   my @columns     = qw(title customer status type projectnumber);
219   my @sortable    = qw(title customer status type projectnumber);
220
221   my %column_defs = (
222     title         => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
223     customer      => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
224                        sub      => sub { $_[0]->customer->name } },
225     projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
226                        sub      => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
227     status        => { sub      => sub { $_[0]->status->description } },
228     type          => { sub      => sub { $_[0]->type->description } },
229   );
230
231   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
232
233   $report->set_options(
234     std_column_visibility => 1,
235     controller_class      => 'RequirementSpec',
236     output_format         => 'HTML',
237     raw_top_info_text     => $self->render('requirement_spec/report_top',    { output => 0 }),
238     raw_bottom_info_text  => $self->render('requirement_spec/report_bottom', { output => 0 }),
239     title                 => $::locale->text('Requirement Specs'),
240     allow_pdf_export      => 1,
241     allow_csv_export      => 1,
242   );
243   $report->set_columns(%column_defs);
244   $report->set_column_order(@columns);
245   $report->set_export_options(qw(list filter));
246   $report->set_options_from_form;
247   $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
248
249   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
250 }
251
252 1;