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;
27 use Rose::DB::Object::Helpers qw(as_tree);
29 use Rose::Object::MakeMethods::Generic
31 scalar => [ qw(project) ],
32 'scalar --get_set_init' => [ qw(models customers project_types project_statuses projects linked_records) ],
35 __PACKAGE__->run_before('check_auth', except => [ qw(ajax_autocomplete) ]);
36 __PACKAGE__->run_before('load_project', only => [ qw(edit update destroy) ]);
47 $params{CUSTOM_VARIABLES} = CVar->get_configs(module => 'Projects');
49 ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
50 = CVar->render_search_options(variables => $params{CUSTOM_VARIABLES},
51 include_prefix => 'l_',
52 include_value => 'Y');
54 $self->render('project/search', %params);
60 $self->make_filter_summary;
62 $self->prepare_report;
64 $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
70 $self->project(SL::DB::Project->new);
71 $self->display_form(title => $::locale->text('Create a new project'),
72 callback => $::form->{callback} || $self->url_for(action => 'new'));
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');
106 sub action_ajax_autocomplete {
107 my ($self, %params) = @_;
109 $::form->{filter}{'all:substr:multi::ilike'} =~ s{[\(\)]+}{}g;
111 # if someone types something, and hits enter, assume he entered the full name.
112 # if something matches, treat that as sole match
113 # unfortunately get_models can't do more than one per package atm, so we d it
114 # the oldfashioned way.
115 if ($::form->{prefer_exact}) {
117 if (1 == scalar @{ $exact_matches = SL::DB::Manager::Project->get_all(
121 description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
122 projectnumber => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
127 $self->projects($exact_matches);
131 $::form->{sort_by} = 'customer_and_description';
135 value => $_->full_description(style => 'full'),
136 label => $_->full_description(style => 'full'),
138 projectnumber => $_->projectnumber,
139 description => $_->description,
140 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
142 } @{ $self->projects }; # neato: if exact match triggers we don't even need the init_projects
144 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
147 sub action_test_page {
148 $_[0]->render('project/test_page');
156 $::auth->assert('project_edit');
163 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
164 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
166 sub init_linked_records {
171 SL::DB::Manager::Invoice-> get_all(where => [ invoice => 1, or => [ globalproject_id => $self->project->id, 'invoiceitems.project_id' => $self->project->id ] ],
172 with_objects => [ 'invoiceitems', 'customer' ],
173 distinct => [ 'customer' ],
174 sort_by => 'transdate ASC'),
175 SL::DB::Manager::Invoice-> get_all(where => [ invoice => 0, or => [ globalproject_id => $self->project->id, 'transactions.project_id' => $self->project->id ] ],
176 with_objects => [ 'transactions', 'customer' ],
177 distinct => [ 'customer' ],
178 sort_by => 'transdate ASC'),
179 SL::DB::Manager::PurchaseInvoice->get_all(where => [ invoice => 1,
180 or => [ globalproject_id => $self->project->id, 'invoiceitems.project_id' => $self->project->id ]
182 with_objects => [ 'invoiceitems', 'vendor' ],
183 distinct => [ 'customer' ],
184 sort_by => 'transdate ASC'),
185 SL::DB::Manager::PurchaseInvoice->get_all(where => [ invoice => 0,
186 or => [ globalproject_id => $self->project->id, 'transactions.project_id' => $self->project->id ]
188 with_objects => [ 'transactions', 'vendor' ],
189 distinct => [ 'customer' ],
190 sort_by => 'transdate ASC'),
191 SL::DB::Manager::GLTransaction-> get_all(where => [ 'transactions.project_id' => $self->project->id ],
192 with_objects => [ 'transactions' ],
194 sort_by => 'transdate ASC'),
195 SL::DB::Manager::Order-> get_all(where => [ or => [ globalproject_id => $self->project->id, 'orderitems.project_id' => $self->project->id ] ],
196 with_objects => [ 'orderitems', 'customer', 'vendor' ],
197 distinct => [ 'customer', 'vendor' ],
198 sort_by => 'transdate ASC' ),
199 SL::DB::Manager::DeliveryOrder-> get_all(where => [ or => [ globalproject_id => $self->project->id, 'orderitems.project_id' => $self->project->id ] ],
200 with_objects => [ 'orderitems', 'customer', 'vendor' ],
201 distinct => [ 'customer', 'vendor' ],
202 sort_by => 'transdate ASC'),
208 if ($::form->{no_paginate}) {
209 $_[0]->models->disable_plugin('paginated');
217 my @customer_id = $self->project && $self->project->customer_id ? (id => $self->project->customer_id) : ();
219 return SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, @customer_id ]]);
223 my ($self, %params) = @_;
225 $params{CUSTOM_VARIABLES} = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
227 if ($params{keep_cvars}) {
228 for my $cvar (@{ $params{CUSTOM_VARIABLES} }) {
229 $cvar->{value} = $::form->{"cvar_$cvar->{name}"} if $::form->{"cvar_$cvar->{name}"};
233 CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
235 $self->render('project/form', %params);
238 sub create_or_update {
240 my $is_new = !$self->project->id;
241 my $params = delete($::form->{project}) || { };
243 delete $params->{id};
244 $self->project->assign_attributes(%{ $params });
246 my @errors = $self->project->validate;
249 flash('error', @errors);
250 $self->display_form(title => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
251 callback => $::form->{callback},
256 $self->project->save;
258 CVar->save_custom_variables(
259 dbh => $self->project->db->dbh,
260 module => 'Projects',
261 trans_id => $self->project->id,
262 variables => $::form,
266 flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
268 $self->redirect_to($::form->{callback} || (action => 'search'));
273 $self->project(SL::DB::Project->new(id => $::form->{id})->load);
280 my $callback = $self->models->get_callback;
282 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
283 $self->{report} = $report;
285 my @columns = qw(project_status customer projectnumber description active valid project_type);
286 my @sortable = qw(projectnumber description customer project_type project_status);
289 projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
290 description => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
291 project_type => { sub => sub { $_[0]->project_type->description } },
292 project_status => { sub => sub { $_[0]->project_status->description }, text => t8('Status') },
293 customer => { sub => sub { !$_[0]->customer_id ? '' : $_[0]->customer->name },
294 raw_data => sub { !$_[0]->customer_id ? '' : $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) } },
295 active => { sub => sub { $_[0]->active ? $::locale->text('Active') : $::locale->text('Inactive') },
296 text => $::locale->text('Active') },
297 valid => { sub => sub { $_[0]->valid ? $::locale->text('Valid') : $::locale->text('Invalid') },
298 text => $::locale->text('Valid') },
301 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
303 $report->set_options(
304 std_column_visibility => 1,
305 controller_class => 'Project',
306 output_format => 'HTML',
307 title => $::locale->text('Projects'),
308 allow_pdf_export => 1,
309 allow_csv_export => 1,
311 $report->set_columns(%column_defs);
312 $report->set_column_order(@columns);
313 $report->set_export_options(qw(list filter));
314 $report->set_options_from_form;
315 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
316 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
317 $report->set_options(
318 raw_top_info_text => $self->render('project/report_top', { output => 0 }),
319 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
326 SL::Controller::Helper::GetModels->new(
330 by => 'projectnumber',
333 customer => t8('Customer'),
334 description => t8('Description'),
335 projectnumber => t8('Project Number'),
336 project_type => t8('Project Type'),
337 project_status => t8('Project Status'),
338 customer_and_description => 1,
340 with_objects => [ 'customer', 'project_status', 'project_type' ],
344 sub make_filter_summary {
347 my $filter = $::form->{filter} || {};
351 [ $filter->{"projectnumber:substr::ilike"}, t8('Project Number') ],
352 [ $filter->{"description:substr::ilike"}, t8('Description') ],
353 [ $filter->{customer}{"name:substr::ilike"}, t8('Customer') ],
354 [ $filter->{"project_type_id"}, t8('Project Type'), sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description } ],
355 [ $filter->{"project_status_id"}, t8('Project Status'), sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
359 [ $filter->{active} eq 'active', $::locale->text('Active') ],
360 [ $filter->{active} eq 'inactive', $::locale->text('Inactive') ],
361 [ $filter->{valid} eq 'valid', $::locale->text('Valid') ],
362 [ $filter->{valid} eq 'invalid', $::locale->text('Invalid') ],
363 [ $filter->{orphaned}, $::locale->text('Orphaned') ],
367 push @filter_strings, "$_->[1]" if $_->[0];
370 push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
373 $self->{filter_summary} = join ', ', @filter_strings;