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