Projekte: Rebase-Fehler für »db_args, pre_parse_filter und flat_filter entfernt«...
[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 list) ]);
34 __PACKAGE__->run_before('load_project_status', only => [ qw(search edit new list) ]);
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 prepare_report {
187   my ($self)      = @_;
188
189   my $callback    = $self->models->get_callback;
190
191   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
192   $self->{report} = $report;
193
194   my @columns     = qw(project_status customer projectnumber description active valid project_type);
195   my @sortable    = qw(projectnumber description customer              project_type project_status);
196
197   my %column_defs = (
198     projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
199     description   => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
200     project_type  => { sub  => sub { $_[0]->project_type->description } },
201     project_status => { sub  => sub { $_[0]->project_status->description }, text => t8('Status') },
202     customer      => { raw_data  => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) } },
203     active        => { sub  => sub { $_[0]->active   ? $::locale->text('Active') : $::locale->text('Inactive') },
204                        text => $::locale->text('Active') },
205     valid         => { sub  => sub { $_[0]->valid    ? $::locale->text('Valid')  : $::locale->text('Invalid')  },
206                        text => $::locale->text('Valid')  },
207   );
208
209   map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
210
211   if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
212     $self->models->disable_plugin('paginated');
213   }
214   $report->set_options(
215     std_column_visibility => 1,
216     controller_class      => 'Project',
217     output_format         => 'HTML',
218     raw_top_info_text     => $self->render('project/report_top', { output => 0 }),
219     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
220     title                 => $::locale->text('Projects'),
221     allow_pdf_export      => 1,
222     allow_csv_export      => 1,
223   );
224   $report->set_columns(%column_defs);
225   $report->set_column_order(@columns);
226   $report->set_export_options(qw(list filter));
227   $report->set_options_from_form;
228   $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
229   $report->set_options(
230     raw_bottom_info_text  => $self->render('project/report_bottom', { output => 0 }),
231   );
232 }
233
234 sub init_models {
235   my ($self) = @_;
236
237   SL::Controller::Helper::GetModels->new(
238     controller => $self,
239     sorted => {
240       _default => {
241         by    => 'projectnumber',
242         dir   => 1,
243       },
244       customer      => t8('Customer'),
245       description   => t8('Description'),
246       projectnumber => t8('Project Number'),
247       project_type  => t8('Project Type'),
248     },
249     with_objects => [ 'customer' ],
250   );
251 }
252
253 sub make_filter_summary {
254   my ($self) = @_;
255
256   my $filter = $::form->{filter} || {};
257   my @filter_strings;
258
259   my @filters = (
260     [ $filter->{"projectnumber:substr::ilike"},  t8('Project Number') ],
261     [ $filter->{"description:substr::ilike"},    t8('Description')    ],
262     [ $filter->{customer}{"name:substr::ilike"}, t8('Customer')       ],
263     [ $filter->{"project_type_id"},              t8('Project Type'),    sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description }   ],
264     [ $filter->{"project_status_id"},            t8('Project Status'),  sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
265   );
266
267   my @flags = (
268     [ $filter->{active} eq 'active',    $::locale->text('Active')      ],
269     [ $filter->{active} eq 'inactive',  $::locale->text('Inactive')    ],
270     [ $filter->{valid}  eq 'valid',     $::locale->text('Valid')       ],
271     [ $filter->{valid}  eq 'invalid',   $::locale->text('Invalid')     ],
272     [ $filter->{orphaned},              $::locale->text('Orphaned')    ],
273   );
274
275   for (@flags) {
276     push @filter_strings, "$_->[1]" if $_->[0];
277   }
278   for (@filters) {
279     push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
280   }
281
282   $self->{filter_summary} = join ', ', @filter_strings;
283 }
284
285 sub load_project_types {
286   $_[0]{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
287 }
288
289 sub load_project_status {
290   $_[0]{ALL_PROJECT_STATUS} = SL::DB::Manager::ProjectStatus->get_all_sorted;
291 }
292
293 1;