Pflichtenhefte: Basisdaten verwalten, Such- und Listfunktion
[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) ]);
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 #
113 # filters
114 #
115
116 sub setup {
117   my ($self) = @_;
118
119   $::auth->assert('config');
120   $::request->{layout}->use_stylesheet("requirement_spec.css");
121   $self->is_template($::form->{is_template} ? 1 : 0);
122
123   return 1;
124 }
125
126 #
127 # helpers
128 #
129
130 sub create_or_update {
131   my $self   = shift;
132   my $is_new = !$self->{requirement_spec}->id;
133   my $params = delete($::form->{requirement_spec}) || { };
134   my $title  = $is_new ? t8('Create a new requirement spec') : t8('Edit requirement spec');
135
136   $self->{requirement_spec}->assign_attributes(%{ $params });
137
138   my @errors = $self->{requirement_spec}->validate;
139
140   if (@errors) {
141     flash('error', @errors);
142     $self->render('requirement_spec/form', title => $title);
143     return;
144   }
145
146   $self->{requirement_spec}->save;
147
148   flash_later('info', $is_new ? t8('The requirement spec has been created.') : t8('The requirement spec has been saved.'));
149   $self->redirect_to(action => 'list');
150 }
151
152 sub load_requirement_spec {
153   my ($self) = @_;
154   $self->{requirement_spec} = SL::DB::RequirementSpec->new(id => $::form->{id})->load;
155 }
156
157 sub load_select_options {
158   my ($self) = @_;
159
160   my @filter = ('!obsolete' => 1);
161   if ($self->requirement_spec && $self->requirement_spec->customer_id) {
162     @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] );
163   }
164
165   $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
166   $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
167   $self->types(    SL::DB::Manager::RequirementSpecType->get_all_sorted);
168 }
169
170 sub load_search_select_options {
171   my ($self) = @_;
172
173   $self->projects(SL::DB::Manager::Project->get_all_sorted);
174 }
175
176 sub setup_db_args_from_filter {
177   my ($self) = @_;
178
179   $self->{filter} = {};
180   my %args = parse_filter(
181     $::form->{filter},
182     with_objects => [ 'customer', 'type', 'status', 'project' ],
183     launder_to   => $self->{filter},
184   );
185
186   $args{where} = [
187     and => [
188       @{ $args{where} || [] },
189       is_template => $self->is_template
190     ]];
191
192   $self->db_args(\%args);
193 }
194
195 sub prepare_report {
196   my ($self)      = @_;
197
198   my $callback    = $self->get_callback;
199
200   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
201   $self->{report} = $report;
202
203   my @columns     = qw(title customer status type projectnumber);
204   my @sortable    = qw(title customer status type projectnumber);
205
206   my %column_defs = (
207     title         => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
208     customer      => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
209                        sub      => sub { $_[0]->customer->name } },
210     projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
211                        sub      => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
212     status        => { sub      => sub { $_[0]->status->description } },
213     type          => { sub      => sub { $_[0]->type->description } },
214   );
215
216   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
217
218   $report->set_options(
219     std_column_visibility => 1,
220     controller_class      => 'RequirementSpec',
221     output_format         => 'HTML',
222     raw_top_info_text     => $self->render('requirement_spec/report_top',    { output => 0 }),
223     raw_bottom_info_text  => $self->render('requirement_spec/report_bottom', { output => 0 }),
224     title                 => $::locale->text('Requirement Specs'),
225     allow_pdf_export      => 1,
226     allow_csv_export      => 1,
227   );
228   $report->set_columns(%column_defs);
229   $report->set_column_order(@columns);
230   $report->set_export_options(qw(list filter));
231   $report->set_options_from_form;
232   $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
233
234   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
235 }
236
237 1;