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