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