Pflichtenhefte: Ausgabemöglichkeit als HTML
[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::ParseFilter;
11 use SL::Controller::Helper::ReportGenerator;
12 use SL::CVar;
13 use SL::DB::Customer;
14 use SL::DB::DeliveryOrder;
15 use SL::DB::Invoice;
16 use SL::DB::Order;
17 use SL::DB::Project;
18 use SL::DB::ProjectType;
19 use SL::DB::ProjectStatus;
20 use SL::DB::PurchaseInvoice;
21 use SL::DB::ProjectType;
22 use SL::Helper::Flash;
23 use SL::Locale::String;
24
25 use Rose::Object::MakeMethods::Generic
26 (
27  scalar => [ qw(project linked_records) ],
28  'scalar --get_set_init' => [ qw(models customers project_types project_statuses) ],
29 );
30
31 __PACKAGE__->run_before('check_auth');
32 __PACKAGE__->run_before('load_project',        only => [ qw(edit update destroy) ]);
33
34 #
35 # actions
36 #
37
38 sub action_search {
39   my ($self) = @_;
40
41   my %params;
42
43   $params{CUSTOM_VARIABLES}  = CVar->get_configs(module => 'Projects');
44   ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
45     = CVar->render_search_options(variables      => $params{CUSTOM_VARIABLES},
46                                   include_prefix => 'l_',
47                                   include_value  => 'Y');
48
49   $self->render('project/search', %params);
50 }
51
52 sub action_list {
53   my ($self) = @_;
54
55   $self->make_filter_summary;
56
57   my $projects = $self->models->get;
58
59   $self->prepare_report;
60
61   $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
62 }
63
64 sub action_new {
65   my ($self) = @_;
66
67   $self->project(SL::DB::Project->new);
68   $self->display_form(title    => $::locale->text('Create a new project'),
69                       callback => $::form->{callback} || $self->url_for(action => 'new'));
70 }
71
72 sub action_edit {
73   my ($self) = @_;
74
75   $self->get_linked_records;
76   $self->display_form(title    => $::locale->text('Edit project #1', $self->project->projectnumber),
77                       callback => $::form->{callback} || $self->url_for(action => 'edit', id => $self->project->id));
78 }
79
80 sub action_create {
81   my ($self) = @_;
82
83   $self->project(SL::DB::Project->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->project->delete; 1; }) {
96     flash_later('info',  $::locale->text('The project has been deleted.'));
97   } else {
98     flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
99   }
100
101   $self->redirect_to(action => 'search');
102 }
103
104 #
105 # filters
106 #
107
108 sub check_auth {
109   $::auth->assert('project_edit');
110 }
111
112 #
113 # helpers
114 #
115
116 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
117 sub init_project_types    { SL::DB::Manager::ProjectType->get_all_sorted   }
118
119 sub init_customers {
120   my ($self)      = @_;
121   my @customer_id = $self->project && $self->project->customer_id ? (id => $self->project->customer_id) : ();
122
123   return SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, @customer_id ]]);
124 }
125
126 sub display_form {
127   my ($self, %params) = @_;
128
129   $params{CUSTOM_VARIABLES}  = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
130
131   if ($params{keep_cvars}) {
132     for my $cvar (@{ $params{CUSTOM_VARIABLES} }) {
133       $cvar->{value} = $::form->{"cvar_$cvar->{name}"} if $::form->{"cvar_$cvar->{name}"};
134     }
135   }
136
137   CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
138
139   $self->render('project/form', %params);
140 }
141
142 sub create_or_update {
143   my $self   = shift;
144   my $is_new = !$self->project->id;
145   my $params = delete($::form->{project}) || { };
146
147   delete $params->{id};
148   $self->project->assign_attributes(%{ $params });
149
150   my @errors = $self->project->validate;
151
152   if (@errors) {
153     flash('error', @errors);
154     $self->display_form(title    => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
155                         callback => $::form->{callback},
156                         keep_cvars => 1);
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 get_linked_records {
181   my ($self) = @_;
182
183   $self->linked_records([
184     map  { @{ $_ } }
185     grep { $_      } (
186       SL::DB::Manager::Order->          get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
187       SL::DB::Manager::DeliveryOrder->  get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
188       SL::DB::Manager::Invoice->        get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer'           ], sort_by => 'transdate ASC'),
189       SL::DB::Manager::PurchaseInvoice->get_all(where => [ globalproject_id => $self->project->id ], with_objects => [             'vendor' ], sort_by => 'transdate ASC'),
190     )]);
191 }
192
193 sub prepare_report {
194   my ($self)      = @_;
195
196   my $callback    = $self->models->get_callback;
197
198   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
199   $self->{report} = $report;
200
201   my @columns     = qw(project_status customer projectnumber description active valid project_type);
202   my @sortable    = qw(projectnumber description customer              project_type project_status);
203
204   my %column_defs = (
205     projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
206     description   => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
207     project_type  => { sub  => sub { $_[0]->project_type->description } },
208     project_status => { sub  => sub { $_[0]->project_status->description }, text => t8('Status') },
209     customer      => { raw_data  => sub { $_[0]->customer_id ? $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) : '' } },
210     active        => { sub  => sub { $_[0]->active   ? $::locale->text('Active') : $::locale->text('Inactive') },
211                        text => $::locale->text('Active') },
212     valid         => { sub  => sub { $_[0]->valid    ? $::locale->text('Valid')  : $::locale->text('Invalid')  },
213                        text => $::locale->text('Valid')  },
214   );
215
216   map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
217
218   if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
219     $self->models->disable_plugin('paginated');
220   }
221   $report->set_options(
222     std_column_visibility => 1,
223     controller_class      => 'Project',
224     output_format         => 'HTML',
225     raw_top_info_text     => $self->render('project/report_top', { output => 0 }),
226     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
227     title                 => $::locale->text('Projects'),
228     allow_pdf_export      => 1,
229     allow_csv_export      => 1,
230   );
231   $report->set_columns(%column_defs);
232   $report->set_column_order(@columns);
233   $report->set_export_options(qw(list filter));
234   $report->set_options_from_form;
235   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
236   $report->set_options(
237     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
238   );
239 }
240
241 sub init_models {
242   my ($self) = @_;
243
244   SL::Controller::Helper::GetModels->new(
245     controller => $self,
246     sorted => {
247       _default => {
248         by    => 'projectnumber',
249         dir   => 1,
250       },
251       customer      => t8('Customer'),
252       description   => t8('Description'),
253       projectnumber => t8('Project Number'),
254       project_type  => t8('Project Type'),
255     },
256     with_objects => [ 'customer' ],
257   );
258 }
259
260 sub make_filter_summary {
261   my ($self) = @_;
262
263   my $filter = $::form->{filter} || {};
264   my @filter_strings;
265
266   my @filters = (
267     [ $filter->{"projectnumber:substr::ilike"},  t8('Project Number') ],
268     [ $filter->{"description:substr::ilike"},    t8('Description')    ],
269     [ $filter->{customer}{"name:substr::ilike"}, t8('Customer')       ],
270     [ $filter->{"project_type_id"},              t8('Project Type'),    sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description }   ],
271     [ $filter->{"project_status_id"},            t8('Project Status'),  sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
272   );
273
274   my @flags = (
275     [ $filter->{active} eq 'active',    $::locale->text('Active')      ],
276     [ $filter->{active} eq 'inactive',  $::locale->text('Inactive')    ],
277     [ $filter->{valid}  eq 'valid',     $::locale->text('Valid')       ],
278     [ $filter->{valid}  eq 'invalid',   $::locale->text('Invalid')     ],
279     [ $filter->{orphaned},              $::locale->text('Orphaned')    ],
280   );
281
282   for (@flags) {
283     push @filter_strings, "$_->[1]" if $_->[0];
284   }
285   for (@filters) {
286     push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
287   }
288
289   $self->{filter_summary} = join ', ', @filter_strings;
290 }
291 1;