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::PurchaseInvoice;
19 use SL::DB::ProjectType;
20 use SL::Helper::Flash;
21 use SL::Locale::String;
23 use Rose::Object::MakeMethods::Generic
25 scalar => [ qw(project linked_records) ],
26 'scalar --get_set_init' => [ qw(models) ],
29 __PACKAGE__->run_before('check_auth');
30 __PACKAGE__->run_before('load_project', only => [ qw(edit update destroy) ]);
41 $params{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
42 $params{CUSTOM_VARIABLES} = CVar->get_configs(module => 'Projects');
43 ($params{CUSTOM_VARIABLES_FILTER_CODE}, $params{CUSTOM_VARIABLES_INCLUSION_CODE})
44 = CVar->render_search_options(variables => $params{CUSTOM_VARIABLES},
45 include_prefix => 'l_',
46 include_value => 'Y');
48 $self->render('project/search', %params);
54 # $self->make_filter_summary;
56 my $projects = $self->models->get;
58 $self->prepare_report;
60 $self->report_generator_list_objects(report => $self->{report}, objects => $projects);
66 $self->project(SL::DB::Project->new);
67 $self->display_form(title => $::locale->text('Create a new project'),
68 callback => $::form->{callback} || $self->url_for(action => 'new'));
74 $self->linked_records([
77 SL::DB::Manager::Order-> get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
78 SL::DB::Manager::DeliveryOrder-> get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer', 'vendor' ], sort_by => 'transdate ASC'),
79 SL::DB::Manager::Invoice-> get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'customer' ], sort_by => 'transdate ASC'),
80 SL::DB::Manager::PurchaseInvoice->get_all(where => [ globalproject_id => $self->project->id ], with_objects => [ 'vendor' ], sort_by => 'transdate ASC'),
83 $self->display_form(title => $::locale->text('Edit project #1', $self->project->projectnumber),
84 callback => $::form->{callback} || $self->url_for(action => 'edit', id => $self->project->id));
90 $self->project(SL::DB::Project->new);
91 $self->create_or_update;
96 $self->create_or_update;
102 if (eval { $self->project->delete; 1; }) {
103 flash_later('info', $::locale->text('The project has been deleted.'));
105 flash_later('error', $::locale->text('The project is in use and cannot be deleted.'));
108 $self->redirect_to(action => 'search');
116 $::auth->assert('project_edit');
124 my ($self, %params) = @_;
126 $params{ALL_CUSTOMERS} = SL::DB::Manager::Customer->get_all_sorted(where => [ or => [ obsolete => 0, obsolete => undef, id => $self->project->customer_id ]]);
127 $params{ALL_PROJECT_TYPES} = SL::DB::Manager::ProjectType->get_all_sorted;
128 $params{CUSTOM_VARIABLES} = CVar->get_custom_variables(module => 'Projects', trans_id => $self->project->id);
129 CVar->render_inputs(variables => $params{CUSTOM_VARIABLES}) if @{ $params{CUSTOM_VARIABLES} };
131 $self->render('project/form', %params);
134 sub create_or_update {
136 my $is_new = !$self->project->id;
137 my $params = delete($::form->{project}) || { };
139 delete $params->{id};
140 $self->project->assign_attributes(%{ $params });
142 my @errors = $self->project->validate;
145 flash('error', @errors);
146 $self->display_form(title => $is_new ? $::locale->text('Create a new project') : $::locale->text('Edit project'),
147 callback => $::form->{callback});
151 $self->project->save;
153 CVar->save_custom_variables(
154 dbh => $self->project->db->dbh,
155 module => 'Projects',
156 trans_id => $self->project->id,
157 variables => $::form,
161 flash_later('info', $is_new ? $::locale->text('The project has been created.') : $::locale->text('The project has been saved.'));
163 $self->redirect_to($::form->{callback} || (action => 'search'));
168 $self->project(SL::DB::Project->new(id => $::form->{id})->load);
171 sub setup_db_args_from_filter {
174 $self->{filter} = {};
175 my %args = parse_filter(
176 $self->_pre_parse_filter($::form->{filter}, $self->{filter}),
177 with_objects => [ 'customer', 'project_type' ],
178 launder_to => $self->{filter},
181 $self->db_args(\%args);
187 my $callback = $self->models->get_callback;
189 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
190 $self->{report} = $report;
192 my @columns = qw(projectnumber description customer active valid project_type);
193 my @sortable = qw(projectnumber description customer project_type);
196 projectnumber => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
197 description => { obj_link => sub { $self->url_for(action => 'edit', id => $_[0]->id, callback => $callback) } },
198 project_type => { sub => sub { $_[0]->project_type->description } },
199 customer => { sub => sub { $_[0]->customer ? $_[0]->customer->name : '' } },
200 active => { sub => sub { $_[0]->active ? $::locale->text('Active') : $::locale->text('Inactive') },
201 text => $::locale->text('Active') },
202 valid => { sub => sub { $_[0]->valid ? $::locale->text('Valid') : $::locale->text('Invalid') },
203 text => $::locale->text('Valid') },
206 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
208 $report->set_options(
209 std_column_visibility => 1,
210 controller_class => 'Project',
211 output_format => 'HTML',
212 top_info_text => $::locale->text('Projects'),
213 title => $::locale->text('Projects'),
214 allow_pdf_export => 1,
215 allow_csv_export => 1,
217 $report->set_columns(%column_defs);
218 $report->set_column_order(@columns);
219 $report->set_export_options(qw(list filter));
220 $report->set_options_from_form;
221 $self->models->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
222 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
223 $report->set_options(
224 raw_bottom_info_text => $self->render('project/report_bottom', { output => 0 }),
231 SL::Controller::Helper::GetModels->new(
235 by => 'projectnumber',
238 customer => t8('Customer'),
239 description => t8('Description'),
240 projectnumber => t8('Project Number'),
241 project_type => t8('Project Type'),
243 with_objects => [ 'customer' ],