1 package SL::Controller::Project;
5 use parent qw(SL::Controller::Base);
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;
16 use SL::DB::DeliveryOrder;
20 use SL::DB::PurchaseInvoice;
21 use SL::Helper::Flash;
22 use SL::Locale::String;
24 use Rose::Object::MakeMethods::Generic
26 scalar => [ qw(project db_args flat_filter linked_records) ],
29 __PACKAGE__->run_before('check_auth');
30 __PACKAGE__->run_before('load_project', only => [ qw(edit update destroy) ]);
32 __PACKAGE__->get_models_url_params('flat_filter');
33 __PACKAGE__->make_paginated(
35 PAGINATE_ARGS => 'db_args',
39 __PACKAGE__->make_sorted(
43 DEFAULT_BY => 'projectnumber',
46 customer => t8('Customer'),
47 description => t8('Description'),
48 projectnumber => t8('Project Number'),
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');
68 $self->render('project/search', %params);
74 $self->setup_db_args_from_filter;
75 $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
76 # $self->make_filter_summary;
78 $self->prepare_report;
80 my $projects = $self->get_models(%{ $self->db_args });
82 $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
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'));
96 $self->linked_records([
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'),
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));
112 $self->project(SL::DB::Project->new);
113 $self->create_or_update;
118 $self->create_or_update;
124 if (eval { $self->project->delete; 1; }) {
125 flash_later('info', $::locale->text('The project has been deleted.'));
127 flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
130 $self->redirect_to(action => 'search');
138 $::auth->assert('project_edit');
146 my ($self, %params) = @_;
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} };
152 $::request->{layout}->focus('#projectnumber');
154 $self->render('project/form', %params);
157 sub create_or_update {
159 my $is_new = !$self->project->id;
160 my $params = delete($::form->{project}) || { };
162 delete $params->{id};
163 $self->project->assign_attributes(%{ $params });
165 my @errors = $self->project->validate;
168 flash('error', @errors);
169 $self->display_form(title => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
170 callback => $::form->{callback});
174 $self->project->save;
176 CVar->save_custom_variables(
177 dbh => $self->project->db->dbh,
178 module => 'Projects',
179 trans_id => $self->project->id,
180 variables => $::form,
184 flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
186 $self->redirect_to($::form->{callback} || (action => 'search'));
191 $self->project(SL::DB::Project->new(id => $::form->{id})->load);
194 sub setup_db_args_from_filter {
197 $self->{filter} = {};
198 my %args = parse_filter(
199 $self->_pre_parse_filter($::form->{filter}, $self->{filter}),
200 with_objects => [ 'customer' ],
201 launder_to => $self->{filter},
204 $self->db_args(\%args);
207 # unfortunately ParseFilter can't handle compount filters.
208 # so we clone the original filter (still need that for serializing)
209 # rip out the options we know an replace them with the compound options.
210 # ParseFilter will take care of the prefixing then.
211 sub _pre_parse_filter {
212 my ($self, $orig_filter, $launder_to) = @_;
214 return undef unless $orig_filter;
216 my $filter = clone($orig_filter);
218 $launder_to->{active} = delete $filter->{active};
219 if ($orig_filter->{active} ne 'both') {
220 push @{ $filter->{and} }, $orig_filter->{active} eq 'active' ? (active => 1) : (or => [ active => 0, active => undef ]);
223 $launder_to->{valid} = delete $filter->{valid};
224 if ($orig_filter->{valid} ne 'both') {
225 push @{ $filter->{and} }, $orig_filter->{valid} eq 'valid' ? (valid => 1) : (or => [ valid => 0, valid => undef ]);
228 $launder_to->{status} = delete $filter->{status};
229 if ($orig_filter->{status} ne 'all') {
230 push @{ $filter->{and} }, SL::DB::Manager::Project->is_not_used_filter;
239 my $callback = $self->get_callback;
241 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
242 $self->{report} = $report;
244 my @columns = qw(projectnumber description customer active valid type);
245 my @sortable = qw(projectnumber description customer type);
248 projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
249 description => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
251 customer => { sub => sub { $_[0]->customer ? $_[0]->customer->name : '' } },
252 active => { sub => sub { $_[0]->active ? $::locale->text('Active') : $::locale->text('Inactive') },
253 text => $::locale->text('Active') },
254 valid => { sub => sub { $_[0]->valid ? $::locale->text('Valid') : $::locale->text('Invalid') },
255 text => $::locale->text('Valid') },
258 map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
260 $report->set_options(
261 std_column_visibility => 1,
262 controller_class => 'Project',
263 output_format => 'HTML',
264 top_info_text => $::locale->text('Projects'),
265 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
266 title => $::locale->text('Projects'),
267 allow_pdf_export => 1,
268 allow_csv_export => 1,
270 $report->set_columns(%column_defs);
271 $report->set_column_order(@columns);
272 $report->set_export_options(qw(list filter));
273 $report->set_options_from_form;
274 $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
276 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;