Pflichtenhefte: Erste Version Baumansicht Textblöcke/Abschnitte/Funktionsblöcke
[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 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 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_create {
81   my ($self) = @_;
82
83   $self->{requirement_spec} = SL::DB::RequirementSpec->new;
84   $self->create_or_update;
85 }
86
87 sub action_update {
88   my ($self) = @_;
89   $self->create_or_update;
90 }
91
92 sub action_destroy {
93   my ($self) = @_;
94
95   if (eval { $self->{requirement_spec}->delete; 1; }) {
96     flash_later('info',  t8('The requirement spec has been deleted.'));
97   } else {
98     flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
99   }
100
101   $self->redirect_to(action => 'list');
102 }
103
104 sub action_reorder {
105   my ($self) = @_;
106
107   SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_id} || [] });
108
109   $self->render('1;', { type => 'js', inline => 1 });
110 }
111
112 sub action_tree {
113   my ($self) = @_;
114   my $r = $self->render('requirement_spec/tree', now => DateTime->now);
115 }
116
117 #
118 # filters
119 #
120
121 sub setup {
122   my ($self) = @_;
123
124   $::auth->assert('config');
125   $::request->{layout}->use_stylesheet("${_}.css") for qw(requirement_spec yaml/core/base.min);
126   $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree requirement_spec);
127   $self->is_template($::form->{is_template} ? 1 : 0);
128
129   return 1;
130 }
131
132 sub load_requirement_spec {
133   my ($self) = @_;
134   $self->{requirement_spec} = SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec";
135 }
136
137 sub load_select_options {
138   my ($self) = @_;
139
140   my @filter = ('!obsolete' => 1);
141   if ($self->requirement_spec && $self->requirement_spec->customer_id) {
142     @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] );
143   }
144
145   $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
146   $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
147   $self->types(    SL::DB::Manager::RequirementSpecType->get_all_sorted);
148 }
149
150 sub load_search_select_options {
151   my ($self) = @_;
152
153   $self->projects(SL::DB::Manager::Project->get_all_sorted);
154 }
155
156 #
157 # helpers
158 #
159
160 sub create_or_update {
161   my $self   = shift;
162   my $is_new = !$self->{requirement_spec}->id;
163   my $params = delete($::form->{requirement_spec}) || { };
164   my $title  = $is_new ? t8('Create a new requirement spec') : t8('Edit requirement spec');
165
166   $self->{requirement_spec}->assign_attributes(%{ $params });
167
168   my @errors = $self->{requirement_spec}->validate;
169
170   if (@errors) {
171     flash('error', @errors);
172     $self->render('requirement_spec/form', title => $title);
173     return;
174   }
175
176   $self->{requirement_spec}->save;
177
178   flash_later('info', $is_new ? t8('The requirement spec has been created.') : t8('The requirement spec has been saved.'));
179   $self->redirect_to(action => 'list');
180 }
181
182 sub setup_db_args_from_filter {
183   my ($self) = @_;
184
185   $self->{filter} = {};
186   my %args = parse_filter(
187     $::form->{filter},
188     with_objects => [ 'customer', 'type', 'status', 'project' ],
189     launder_to   => $self->{filter},
190   );
191
192   $args{where} = [
193     and => [
194       @{ $args{where} || [] },
195       is_template => $self->is_template
196     ]];
197
198   $self->db_args(\%args);
199 }
200
201 sub prepare_report {
202   my ($self)      = @_;
203
204   my $callback    = $self->get_callback;
205
206   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
207   $self->{report} = $report;
208
209   my @columns     = qw(title customer status type projectnumber);
210   my @sortable    = qw(title customer status type projectnumber);
211
212   my %column_defs = (
213     title         => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
214     customer      => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
215                        sub      => sub { $_[0]->customer->name } },
216     projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
217                        sub      => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
218     status        => { sub      => sub { $_[0]->status->description } },
219     type          => { sub      => sub { $_[0]->type->description } },
220   );
221
222   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
223
224   $report->set_options(
225     std_column_visibility => 1,
226     controller_class      => 'RequirementSpec',
227     output_format         => 'HTML',
228     raw_top_info_text     => $self->render('requirement_spec/report_top',    { output => 0 }),
229     raw_bottom_info_text  => $self->render('requirement_spec/report_bottom', { output => 0 }),
230     title                 => $::locale->text('Requirement Specs'),
231     allow_pdf_export      => 1,
232     allow_csv_export      => 1,
233   );
234   $report->set_columns(%column_defs);
235   $report->set_column_order(@columns);
236   $report->set_export_options(qw(list filter));
237   $report->set_options_from_form;
238   $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
239
240   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
241 }
242
243 1;