1 package SL::Controller::Project;
5 use parent qw(SL::Controller::Base);
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ParseFilter;
11 use SL::Controller::Helper::ReportGenerator;
14 use SL::DB::DeliveryOrder;
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;
25 use Rose::Object::MakeMethods::Generic
27 scalar => [ qw(project linked_records) ],
28 'scalar --get_set_init' => [ qw(models) ],
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) ]);
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');
51 $self->render('project/search', %params);
57 $self->make_filter_summary;
59 my $projects = $self->models->get;
61 $self->prepare_report;
63 $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
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'));
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));
85 $self->project(SL::DB::Project->new);
86 $self->create_or_update;
91 $self->create_or_update;
97 if (eval { $self->project->delete; 1; }) {
98 flash_later('info', $::locale->text('The project has been deleted.'));
100 flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
103 $self->redirect_to(action => 'search');
111 $::auth->assert('project_edit');
119 my ($self, %params) = @_;
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);
124 if ($params{keep_cvars}) {
125 for my $cvar (@{ $params{CUSTOM_VARIABLES} }) {
126 $cvar->{value} = $::form->{"cvar_$cvar->{name}"} if $::form->{"cvar_$cvar->{name}"};
130 CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
132 $self->render('project/form', %params);
135 sub create_or_update {
137 my $is_new = !$self->project->id;
138 my $params = delete($::form->{project}) || { };
140 delete $params->{id};
141 $self->project->assign_attributes(%{ $params });
143 my @errors = $self->project->validate;
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},
153 $self->project->save;
155 CVar->save_custom_variables(
156 dbh => $self->project->db->dbh,
157 module => 'Projects',
158 trans_id => $self->project->id,
159 variables => $::form,
163 flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
165 $self->redirect_to($::form->{callback} || (action => 'search'));
170 $self->project(SL::DB::Project->new(id => $::form->{id})->load);
173 sub get_linked_records {
176 $self->linked_records([
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'),
186 sub setup_db_args_from_filter {
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},
196 $self->db_args(\%args);
202 my $callback = $self->models->get_callback;
204 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
205 $self->{report} = $report;
207 my @columns = qw(project_status customer projectnumber description active valid project_type);
208 my @sortable = qw(projectnumber description customer project_type project_status);
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('Status') },
215 customer => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) } },
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') },
222 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
224 if ( $report->{options}{output_format} =~ /^(pdf|csv)$/i ) {
225 $self->models->disable_plugin('paginated');
227 $report->set_options(
228 std_column_visibility => 1,
229 controller_class => 'Project',
230 output_format => 'HTML',
231 raw_top_info_text => $self->render('project/report_top', { output => 0 }),
232 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
233 title => $::locale->text('Projects'),
234 allow_pdf_export => 1,
235 allow_csv_export => 1,
237 $report->set_columns(%column_defs);
238 $report->set_column_order(@columns);
239 $report->set_export_options(qw(list filter));
240 $report->set_options_from_form;
241 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
242 $report->set_options(
243 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
250 SL::Controller::Helper::GetModels->new(
254 by => 'projectnumber',
257 customer => t8('Customer'),
258 description => t8('Description'),
259 projectnumber => t8('Project Number'),
260 project_type => t8('Project Type'),
262 with_objects => [ 'customer' ],
266 sub make_filter_summary {
269 my $filter = $::form->{filter} || {};
273 [ $filter->{"projectnumber:substr::ilike"}, t8('Project Number') ],
274 [ $filter->{"description:substr::ilike"}, t8('Description') ],
275 [ $filter->{customer}{"name:substr::ilike"}, t8('Customer') ],
276 [ $filter->{"project_type_id"}, t8('Project Type'), sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description } ],
277 [ $filter->{"project_status_id"}, t8('Project Status'), sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
281 [ $filter->{active} eq 'active', $::locale->text('Active') ],
282 [ $filter->{active} eq 'inactive', $::locale->text('Inactive') ],
283 [ $filter->{valid} eq 'valid', $::locale->text('Valid') ],
284 [ $filter->{valid} eq 'invalid', $::locale->text('Invalid') ],
285 [ $filter->{orphaned}, $::locale->text('Orphaned') ],
289 push @filter_strings, "$_->[1]" if $_->[0];
292 push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
295 $self->{filter_summary} = join ', ', @filter_strings;
298 sub load_project_types {
299 $_[0]{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
302 sub load_project_status {
303 $_[0]{ALL_PROJECT_STATUS} = SL::DB::Manager::ProjectStatus->get_all_sorted;