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;
19 use SL::DB::ProjectType;
20 use SL::DB::ProjectStatus;
21 use SL::DB::PurchaseInvoice;
22 use SL::DB::ProjectType;
23 use SL::Helper::Flash;
24 use SL::Locale::String;
28 use Rose::DB::Object::Helpers qw(as_tree);
30 use Rose::Object::MakeMethods::Generic
32 scalar => [ qw(project) ],
33 'scalar --get_set_init' => [ qw(models customers project_types project_statuses projects linked_records employees may_edit_invoice_permissions) ],
36 __PACKAGE__->run_before('check_auth', except => [ qw(ajax_autocomplete) ]);
37 __PACKAGE__->run_before('load_project', only => [ qw(edit update destroy) ]);
38 __PACKAGE__->run_before('use_multiselect_js', only => [ qw(new create edit update) ]);
49 $params{CUSTOM_VARIABLES} = CVar->get_configs(module => 'Projects');
51 ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
52 = CVar->render_search_options(variables => $params{CUSTOM_VARIABLES},
53 include_prefix => 'l_',
54 include_value => 'Y');
56 $self->setup_search_action_bar;
58 $self->render('project/search', %params);
64 $self->setup_search_action_bar;
66 $self->make_filter_summary;
68 $self->prepare_report;
70 $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
76 $self->project(SL::DB::Project->new);
77 $self->display_form(title => $::locale->text('Create a new project'),
78 callback => $::form->{callback} || $self->url_for(action => 'list'));
84 $self->display_form(title => $::locale->text('Edit project #1', $self->project->projectnumber),
85 callback => $::form->{callback} || $self->url_for(action => 'list'));
91 $self->project(SL::DB::Project->new);
92 $self->create_or_update;
97 $self->create_or_update;
103 if (eval { $self->project->delete; 1; }) {
104 flash_later('info', $::locale->text('The project has been deleted.'));
106 flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
109 $self->redirect_to(action => 'search');
112 sub action_ajax_autocomplete {
113 my ($self, %params) = @_;
115 $::form->{filter}{'all:substr:multi::ilike'} =~ s{[\(\)]+}{}g;
117 # if someone types something, and hits enter, assume he entered the full name.
118 # if something matches, treat that as sole match
119 # unfortunately get_models can't do more than one per package atm, so we d it
120 # the oldfashioned way.
121 if ($::form->{prefer_exact}) {
123 if (1 == scalar @{ $exact_matches = SL::DB::Manager::Project->get_all(
127 description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
128 projectnumber => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
133 $self->projects($exact_matches);
137 $::form->{sort_by} = 'customer_and_description';
141 value => $_->full_description(style => 'full'),
142 label => $_->full_description(style => 'full'),
144 projectnumber => $_->projectnumber,
145 description => $_->description,
146 cvars => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
148 } @{ $self->projects }; # neato: if exact match triggers we don't even need the init_projects
150 $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
153 sub action_test_page {
154 $_[0]->render('project/test_page');
162 $::auth->assert('project_edit');
169 sub init_project_statuses { SL::DB::Manager::ProjectStatus->get_all_sorted }
170 sub init_project_types { SL::DB::Manager::ProjectType->get_all_sorted }
171 sub init_employees { SL::DB::Manager::Employee->get_all_sorted }
172 sub init_may_edit_invoice_permissions { $::auth->assert('project_edit_view_invoices_permission', 1) }
174 sub init_linked_records {
179 SL::DB::Manager::Invoice-> get_all(where => [ invoice => 1, or => [ globalproject_id => $self->project->id, 'invoiceitems.project_id' => $self->project->id ] ],
180 with_objects => [ 'invoiceitems', 'customer' ],
181 distinct => [ 'customer' ],
182 sort_by => 'transdate ASC'),
183 SL::DB::Manager::Invoice-> get_all(where => [ invoice => 0, or => [ globalproject_id => $self->project->id, 'transactions.project_id' => $self->project->id ] ],
184 with_objects => [ 'transactions', 'customer' ],
185 distinct => [ 'customer' ],
186 sort_by => 'transdate ASC'),
187 SL::DB::Manager::PurchaseInvoice->get_all(where => [ invoice => 1,
188 or => [ globalproject_id => $self->project->id, 'invoiceitems.project_id' => $self->project->id ]
190 with_objects => [ 'invoiceitems', 'vendor' ],
191 distinct => [ 'customer' ],
192 sort_by => 'transdate ASC'),
193 SL::DB::Manager::PurchaseInvoice->get_all(where => [ invoice => 0,
194 or => [ globalproject_id => $self->project->id, 'transactions.project_id' => $self->project->id ]
196 with_objects => [ 'transactions', 'vendor' ],
197 distinct => [ 'customer' ],
198 sort_by => 'transdate ASC'),
199 SL::DB::Manager::GLTransaction-> get_all(where => [ 'transactions.project_id' => $self->project->id ],
200 with_objects => [ 'transactions' ],
202 sort_by => 'transdate ASC'),
203 SL::DB::Manager::Order-> get_all(where => [ or => [ globalproject_id => $self->project->id, 'orderitems.project_id' => $self->project->id ] ],
204 with_objects => [ 'orderitems', 'customer', 'vendor' ],
205 distinct => [ 'customer', 'vendor' ],
206 sort_by => 'transdate ASC' ),
207 SL::DB::Manager::DeliveryOrder-> get_all(where => [ or => [ globalproject_id => $self->project->id, 'orderitems.project_id' => $self->project->id ] ],
208 with_objects => [ 'orderitems', 'customer', 'vendor' ],
209 distinct => [ 'customer', 'vendor' ],
210 sort_by => 'transdate ASC'),
216 if ($::form->{no_paginate}) {
217 $_[0]->models->disable_plugin('paginated');
225 my @customer_id = $self->project && $self->project->customer_id ? (id => $self->project->customer_id) : ();
227 return SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, @customer_id ]]);
230 sub use_multiselect_js {
231 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
235 my ($self, %params) = @_;
237 $params{CUSTOM_VARIABLES} = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
239 if ($params{keep_cvars}) {
240 for my $cvar (@{ $params{CUSTOM_VARIABLES} }) {
241 $cvar->{value} = $::form->{"cvar_$cvar->{name}"} if $::form->{"cvar_$cvar->{name}"};
245 CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
247 $self->setup_edit_action_bar(callback => $params{callback});
249 $self->render('project/form', %params);
252 sub create_or_update {
254 my $is_new = !$self->project->id;
255 my $params = delete($::form->{project}) || { };
257 if (!$self->may_edit_invoice_permissions) {
258 delete $params->{employee_invoice_permissions};
259 } elsif (!$params->{employee_invoice_permissions}) {
260 $params->{employee_invoice_permissions} = [];
263 delete $params->{id};
264 $self->project->assign_attributes(%{ $params });
266 my @errors = $self->project->validate;
269 flash('error', @errors);
270 $self->display_form(title => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
271 callback => $::form->{callback},
276 $self->project->save;
278 CVar->save_custom_variables(
279 dbh => $self->project->db->dbh,
280 module => 'Projects',
281 trans_id => $self->project->id,
282 variables => $::form,
286 flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
288 $self->redirect_to($::form->{callback} || (action => 'search'));
293 $self->project(SL::DB::Project->new(id => $::form->{id})->load);
300 my $callback = $self->models->get_callback;
302 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
303 $self->{report} = $report;
305 my @columns = qw(project_status customer projectnumber description active valid project_type);
306 my @sortable = qw(projectnumber description customer project_type project_status);
309 projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
310 description => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
311 project_type => { sub => sub { $_[0]->project_type->description } },
312 project_status => { sub => sub { $_[0]->project_status->description }, text => t8('Status') },
313 customer => { sub => sub { !$_[0]->customer_id ? '' : $_[0]->customer->name },
314 raw_data => sub { !$_[0]->customer_id ? '' : $_[0]->customer->presenter->customer(display => 'table-cell', callback => $callback) } },
315 active => { sub => sub { $_[0]->active ? $::locale->text('Active') : $::locale->text('Inactive') },
316 text => $::locale->text('Active') },
317 valid => { sub => sub { $_[0]->valid ? $::locale->text('Valid') : $::locale->text('Invalid') },
318 text => $::locale->text('Valid') },
321 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
323 $report->set_options(
324 std_column_visibility => 1,
325 controller_class => 'Project',
326 output_format => 'HTML',
327 title => $::locale->text('Projects'),
328 allow_pdf_export => 1,
329 allow_csv_export => 1,
331 $report->set_columns(%column_defs);
332 $report->set_column_order(@columns);
333 $report->set_export_options(qw(list filter));
334 $report->set_options_from_form;
335 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
336 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
337 $report->set_options(
338 raw_top_info_text => $self->render('project/report_top', { output => 0 }),
339 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
346 SL::Controller::Helper::GetModels->new(
350 by => 'projectnumber',
353 customer => t8('Customer'),
354 description => t8('Description'),
355 projectnumber => t8('Project Number'),
356 project_type => t8('Project Type'),
357 project_status => t8('Project Status'),
358 customer_and_description => 1,
360 with_objects => [ 'customer', 'project_status', 'project_type' ],
364 sub make_filter_summary {
367 my $filter = $::form->{filter} || {};
371 [ $filter->{"projectnumber:substr::ilike"}, t8('Project Number') ],
372 [ $filter->{"description:substr::ilike"}, t8('Description') ],
373 [ $filter->{customer}{"name:substr::ilike"}, t8('Customer') ],
374 [ $filter->{"project_type_id"}, t8('Project Type'), sub { SL::DB::Manager::ProjectType->find_by(id => $filter->{"project_type_id"})->description } ],
375 [ $filter->{"project_status_id"}, t8('Project Status'), sub { SL::DB::Manager::ProjectStatus->find_by(id => $filter->{"project_status_id"})->description } ],
379 [ $filter->{active} eq 'active', $::locale->text('Active') ],
380 [ $filter->{active} eq 'inactive', $::locale->text('Inactive') ],
381 [ $filter->{valid} eq 'valid', $::locale->text('Valid') ],
382 [ $filter->{valid} eq 'invalid', $::locale->text('Invalid') ],
383 [ $filter->{orphaned}, $::locale->text('Orphaned') ],
387 push @filter_strings, "$_->[1]" if $_->[0];
390 push @filter_strings, "$_->[1]: " . ($_->[2] ? $_->[2]->() : $_->[0]) if $_->[0];
393 $self->{filter_summary} = join ', ', @filter_strings;
396 sub setup_edit_action_bar {
397 my ($self, %params) = @_;
399 my $is_new = !$self->project->id;
401 for my $bar ($::request->layout->get('actionbar')) {
406 submit => [ '#form', { action => 'Project/' . ($is_new ? 'create' : 'update') } ],
407 accesskey => 'enter',
411 submit => [ '#form', { action => 'Project/create' }],
412 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
414 ], # end of combobox "Save"
418 submit => [ '#form', { action => 'Project/destroy' } ],
419 confirm => $::locale->text('Do you really want to delete this object?'),
420 disabled => $is_new ? t8('This object has not been saved yet.')
421 : $self->project->is_used ? t8('This object has already been used.')
427 link => $params{callback} || $self->url_for(action => 'list'),
433 sub setup_search_action_bar {
434 my ($self, %params) = @_;
436 for my $bar ($::request->layout->get('actionbar')) {
440 submit => [ '#search_form', { action => 'Project/list' } ],
441 accesskey => 'enter',
445 link => $self->url_for(action => 'new'),