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