kivitendo/SL/Controller/RequirementSpec.pm @ 94f65264
b2e1809f | Moritz Bunkus | package SL::Controller::RequirementSpec;
|
||
use strict;
|
||||
84fc52bd | Moritz Bunkus | use utf8;
|
||
b2e1809f | Moritz Bunkus | |||
use parent qw(SL::Controller::Base);
|
||||
b48939fe | Moritz Bunkus | use SL::ClientJS;
|
||
b2e1809f | Moritz Bunkus | use SL::Controller::Helper::GetModels;
|
||
use SL::Controller::Helper::Paginated;
|
||||
use SL::Controller::Helper::Sorted;
|
||||
use SL::Controller::Helper::ParseFilter;
|
||||
use SL::Controller::Helper::ReportGenerator;
|
||||
c19b1e03 | Moritz Bunkus | use SL::Controller::Helper::RequirementSpec;
|
||
b2e1809f | Moritz Bunkus | use SL::DB::Customer;
|
||
use SL::DB::Project;
|
||||
c19b1e03 | Moritz Bunkus | use SL::DB::RequirementSpecComplexity;
|
||
use SL::DB::RequirementSpecRisk;
|
||||
b2e1809f | Moritz Bunkus | use SL::DB::RequirementSpecStatus;
|
||
use SL::DB::RequirementSpecType;
|
||||
use SL::DB::RequirementSpec;
|
||||
use SL::Helper::Flash;
|
||||
use SL::Locale::String;
|
||||
84fc52bd | Moritz Bunkus | use SL::Template::LaTeX;
|
||
b2e1809f | Moritz Bunkus | |||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
c19b1e03 | Moritz Bunkus | scalar => [ qw(requirement_spec_item customers types statuses db_args flat_filter is_template visible_item visible_section) ],
|
||
13b0468f | Moritz Bunkus | 'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects copy_source js) ],
|
||
b2e1809f | Moritz Bunkus | );
|
||
__PACKAGE__->run_before('setup');
|
||||
c19b1e03 | Moritz Bunkus | __PACKAGE__->run_before('load_select_options', only => [ qw(new ajax_edit create update list) ]);
|
||
b2e1809f | Moritz Bunkus | |||
405a41ef | Moritz Bunkus | |||
b2e1809f | Moritz Bunkus | __PACKAGE__->get_models_url_params('flat_filter');
|
||
__PACKAGE__->make_paginated(
|
||||
MODEL => 'RequirementSpec',
|
||||
PAGINATE_ARGS => 'db_args',
|
||||
ONLY => [ qw(list) ],
|
||||
);
|
||||
__PACKAGE__->make_sorted(
|
||||
MODEL => 'RequirementSpec',
|
||||
ONLY => [ qw(list) ],
|
||||
DEFAULT_BY => 'customer',
|
||||
DEFAULT_DIR => 1,
|
||||
customer => t8('Customer'),
|
||||
title => t8('Title'),
|
||||
type => t8('Requirement Spec Type'),
|
||||
status => t8('Requirement Spec Status'),
|
||||
projectnumber => t8('Project Number'),
|
||||
51fec310 | Moritz Bunkus | version => t8('Version'),
|
||
mtime => t8('Last modification'),
|
||||
b2e1809f | Moritz Bunkus | );
|
||
#
|
||||
# actions
|
||||
#
|
||||
c19b1e03 | Moritz Bunkus | |||
b2e1809f | Moritz Bunkus | sub action_list {
|
||
my ($self) = @_;
|
||||
$self->setup_db_args_from_filter;
|
||||
$self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
|
||||
$self->prepare_report;
|
||||
my $requirement_specs = $self->get_models(%{ $self->db_args });
|
||||
$self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
|
||||
}
|
||||
sub action_new {
|
||||
my ($self) = @_;
|
||||
b48939fe | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new);
|
||
405a41ef | Moritz Bunkus | |||
if ($self->copy_source) {
|
||||
$self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate)
|
||||
}
|
||||
b48939fe | Moritz Bunkus | $self->render('requirement_spec/new', title => t8('Create a new requirement spec'));
|
||
b2e1809f | Moritz Bunkus | }
|
||
b48939fe | Moritz Bunkus | sub action_ajax_edit {
|
||
b2e1809f | Moritz Bunkus | my ($self) = @_;
|
||
b48939fe | Moritz Bunkus | |||
$self->render('requirement_spec/_form', { layout => 0 }, submit_as => 'ajax');
|
||||
b2e1809f | Moritz Bunkus | }
|
||
c19b1e03 | Moritz Bunkus | sub action_ajax_show_time_and_cost_estimate {
|
||
my ($self) = @_;
|
||||
$self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
|
||||
}
|
||||
sub action_ajax_cancel_time_and_cost_estimate {
|
||||
my ($self) = @_;
|
||||
my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
|
||||
13b0468f | Moritz Bunkus | $self->js
|
||
->replaceWith('#time_cost_estimate', $html)
|
||||
->render($self);
|
||||
c19b1e03 | Moritz Bunkus | }
|
||
sub action_ajax_edit_time_and_cost_estimate {
|
||||
my ($self) = @_;
|
||||
my $html = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
|
||||
13b0468f | Moritz Bunkus | $self->js
|
||
->replaceWith('#time_cost_estimate', $html)
|
||||
->render($self);
|
||||
c19b1e03 | Moritz Bunkus | }
|
||
sub action_ajax_save_time_and_cost_estimate {
|
||||
my ($self) = @_;
|
||||
$self->requirement_spec->db->do_transaction(sub {
|
||||
# Make Emacs happy
|
||||
1;
|
||||
foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
|
||||
SL::DB::RequirementSpecItem
|
||||
->new(id => delete $attributes->{id})
|
||||
->load
|
||||
->update_attributes(%{ $attributes });
|
||||
}
|
||||
1;
|
||||
});
|
||||
my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
|
||||
13b0468f | Moritz Bunkus | $self->js->replaceWith('#time_cost_estimate', $html);
|
||
c19b1e03 | Moritz Bunkus | |||
if ($self->visible_section) {
|
||||
$html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
|
||||
13b0468f | Moritz Bunkus | $self->js->html('#column-content', $html);
|
||
c19b1e03 | Moritz Bunkus | }
|
||
13b0468f | Moritz Bunkus | $self->js->render($self);
|
||
c19b1e03 | Moritz Bunkus | }
|
||
c1569bc1 | Moritz Bunkus | sub action_show {
|
||
my ($self) = @_;
|
||||
my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
|
||||
$self->requirement_spec_item($item);
|
||||
$self->render('requirement_spec/show', title => t8('Show requirement spec'));
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | sub action_create {
|
||
my ($self) = @_;
|
||||
b48939fe | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new);
|
||
b2e1809f | Moritz Bunkus | $self->create_or_update;
|
||
}
|
||||
sub action_update {
|
||||
my ($self) = @_;
|
||||
$self->create_or_update;
|
||||
}
|
||||
sub action_destroy {
|
||||
my ($self) = @_;
|
||||
b48939fe | Moritz Bunkus | if (eval { $self->requirement_spec->delete; 1; }) {
|
||
b2e1809f | Moritz Bunkus | flash_later('info', t8('The requirement spec has been deleted.'));
|
||
} else {
|
||||
flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
|
||||
}
|
||||
$self->redirect_to(action => 'list');
|
||||
}
|
||||
c96c4bb2 | Moritz Bunkus | sub action_revert_to {
|
||
my ($self, %params) = @_;
|
||||
return $self->js->error(t8('Cannot revert a versioned copy.'))->render($self) if $self->requirement_spec->working_copy_id;
|
||||
my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
|
||||
$self->requirement_spec->copy_from(
|
||||
$versioned_copy,
|
||||
version_id => $versioned_copy->version_id,
|
||||
);
|
||||
flash_later('info', t8('The requirement spec has been reverted to version #1.', $self->requirement_spec->version->version_number));
|
||||
$self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render($self);
|
||||
}
|
||||
84fc52bd | Moritz Bunkus | sub action_create_pdf {
|
||
my ($self, %params) = @_;
|
||||
my %result = SL::Template::LaTeX->parse_and_create_pdf('requirement_spec.tex', SELF => $self, rspec => $self->requirement_spec);
|
||||
$::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
|
||||
my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id);
|
||||
$attachment_name .= ' (v' . $self->requirement_spec->version->version_number . ')' if $self->requirement_spec->version;
|
||||
$attachment_name .= '.pdf';
|
||||
$attachment_name =~ s/[^\wäöüÄÖÜß \-\+\(\)\[\]\{\}\.,]+/_/g;
|
||||
$self->send_file($result{file_name}, type => 'application/pdf', name => $attachment_name);
|
||||
unlink $result{file_name};
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | #
|
||
# filters
|
||||
#
|
||||
sub setup {
|
||||
my ($self) = @_;
|
||||
51fec310 | Moritz Bunkus | $::auth->assert('sales_quotation_edit');
|
||
c1569bc1 | Moritz Bunkus | $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
|
||
ca7c2f91 | Moritz Bunkus | $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
|
||
b2e1809f | Moritz Bunkus | $self->is_template($::form->{is_template} ? 1 : 0);
|
||
c19b1e03 | Moritz Bunkus | $self->init_visible_section;
|
||
b2e1809f | Moritz Bunkus | |||
return 1;
|
||||
}
|
||||
c19b1e03 | Moritz Bunkus | sub init_complexities {
|
||
my ($self) = @_;
|
||||
return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
|
||||
}
|
||||
sub init_risks {
|
||||
my ($self) = @_;
|
||||
return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
|
||||
}
|
||||
sub init_projects {
|
||||
my ($self) = @_;
|
||||
$self->projects(SL::DB::Manager::Project->get_all_sorted);
|
||||
}
|
||||
sub init_requirement_spec {
|
||||
54daa586 | Moritz Bunkus | my ($self) = @_;
|
||
c19b1e03 | Moritz Bunkus | $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
|
||
54daa586 | Moritz Bunkus | }
|
||
405a41ef | Moritz Bunkus | sub init_copy_source {
|
||
my ($self) = @_;
|
||||
$self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
|
||||
}
|
||||
13b0468f | Moritz Bunkus | sub init_js {
|
||
my ($self) = @_;
|
||||
$self->js(SL::ClientJS->new);
|
||||
}
|
||||
54daa586 | Moritz Bunkus | sub load_select_options {
|
||
my ($self) = @_;
|
||||
my @filter = ('!obsolete' => 1);
|
||||
c19b1e03 | Moritz Bunkus | @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
|
||
54daa586 | Moritz Bunkus | |||
$self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
|
||||
$self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
|
||||
$self->types( SL::DB::Manager::RequirementSpecType->get_all_sorted);
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | #
|
||
# helpers
|
||||
#
|
||||
sub create_or_update {
|
||||
my $self = shift;
|
||||
b48939fe | Moritz Bunkus | my $is_new = !$self->requirement_spec->id;
|
||
b2e1809f | Moritz Bunkus | my $params = delete($::form->{requirement_spec}) || { };
|
||
my $title = $is_new ? t8('Create a new requirement spec') : t8('Edit requirement spec');
|
||||
b48939fe | Moritz Bunkus | $self->requirement_spec->assign_attributes(%{ $params });
|
||
b2e1809f | Moritz Bunkus | |||
b48939fe | Moritz Bunkus | my @errors = $self->requirement_spec->validate;
|
||
b2e1809f | Moritz Bunkus | |||
if (@errors) {
|
||||
13b0468f | Moritz Bunkus | return $self->js->error(@errors)->render($self) if $::request->is_ajax;
|
||
b48939fe | Moritz Bunkus | |||
b2e1809f | Moritz Bunkus | flash('error', @errors);
|
||
b48939fe | Moritz Bunkus | $self->render('requirement_spec/new', title => $title);
|
||
b2e1809f | Moritz Bunkus | return;
|
||
}
|
||||
405a41ef | Moritz Bunkus | my $db = $self->requirement_spec->db;
|
||
if (!$db->do_transaction(sub {
|
||||
if ($self->copy_source) {
|
||||
$self->requirement_spec($self->copy_source->create_copy(%{ $params }));
|
||||
} else {
|
||||
$self->requirement_spec->save;
|
||||
}
|
||||
})) {
|
||||
$::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
|
||||
51fec310 | Moritz Bunkus | @errors = ($::locale->text('Saving failed. Error message from the database: #1'), $db->error);
|
||
13b0468f | Moritz Bunkus | return $self->js->error(@errors)->render($self) if $::request->is_ajax;
|
||
405a41ef | Moritz Bunkus | |||
$self->requirement_spec->id(undef) if $is_new;
|
||||
flash('error', @errors);
|
||||
return $self->render('requirement_spec/new', title => $title);
|
||||
}
|
||||
b48939fe | Moritz Bunkus | |||
if ($::request->is_ajax) {
|
||||
my $html = $self->render('requirement_spec/_header', { output => 0 });
|
||||
b4fc4933 | Moritz Bunkus | return $self->invalidate_version
|
||
b48939fe | Moritz Bunkus | ->replaceWith('#requirement-spec-header', $html)
|
||
->flash('info', t8('The requirement spec has been saved.'))
|
||||
->render($self);
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | |||
flash_later('info', $is_new ? t8('The requirement spec has been created.') : t8('The requirement spec has been saved.'));
|
||||
b48939fe | Moritz Bunkus | $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
|
||
b2e1809f | Moritz Bunkus | }
|
||
sub setup_db_args_from_filter {
|
||||
my ($self) = @_;
|
||||
$self->{filter} = {};
|
||||
my %args = parse_filter(
|
||||
$::form->{filter},
|
||||
with_objects => [ 'customer', 'type', 'status', 'project' ],
|
||||
launder_to => $self->{filter},
|
||||
);
|
||||
$args{where} = [
|
||||
and => [
|
||||
@{ $args{where} || [] },
|
||||
51fec310 | Moritz Bunkus | working_copy_id => undef,
|
||
is_template => $self->is_template
|
||||
b2e1809f | Moritz Bunkus | ]];
|
||
$self->db_args(\%args);
|
||||
}
|
||||
sub prepare_report {
|
||||
my ($self) = @_;
|
||||
my $callback = $self->get_callback;
|
||||
my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
|
||||
$self->{report} = $report;
|
||||
51fec310 | Moritz Bunkus | my @columns = qw(title customer status type projectnumber mtime version);
|
||
my @sortable = qw(title customer status type projectnumber mtime);
|
||||
b2e1809f | Moritz Bunkus | |||
my %column_defs = (
|
||||
b48939fe | Moritz Bunkus | title => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
|
||
b2e1809f | Moritz Bunkus | customer => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
|
||
sub => sub { $_[0]->customer->name } },
|
||||
projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
|
||||
sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
|
||||
status => { sub => sub { $_[0]->status->description } },
|
||||
type => { sub => sub { $_[0]->type->description } },
|
||||
51fec310 | Moritz Bunkus | version => { sub => sub { $_[0]->version_id ? $_[0]->version->version_number : t8('Working copy without version') } },
|
||
mtime => { sub => sub { $_[0]->mtime->to_kivitendo(precision => 'minute') } },
|
||||
b2e1809f | Moritz Bunkus | );
|
||
map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
|
||||
$report->set_options(
|
||||
std_column_visibility => 1,
|
||||
controller_class => 'RequirementSpec',
|
||||
output_format => 'HTML',
|
||||
raw_top_info_text => $self->render('requirement_spec/report_top', { output => 0 }),
|
||||
raw_bottom_info_text => $self->render('requirement_spec/report_bottom', { output => 0 }),
|
||||
title => $::locale->text('Requirement Specs'),
|
||||
allow_pdf_export => 1,
|
||||
allow_csv_export => 1,
|
||||
);
|
||||
$report->set_columns(%column_defs);
|
||||
$report->set_column_order(@columns);
|
||||
$report->set_export_options(qw(list filter));
|
||||
$report->set_options_from_form;
|
||||
$self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
|
||||
$self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
|
||||
}
|
||||
b4fc4933 | Moritz Bunkus | sub invalidate_version {
|
||
my ($self) = @_;
|
||||
my $rspec = SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load;
|
||||
$rspec->invalidate_version;
|
||||
my $html = $self->render('requirement_spec/_version', { output => 0 }, requirement_spec => $rspec);
|
||||
return $self->js->html('#requirement_spec_version', $html);
|
||||
}
|
||||
b2e1809f | Moritz Bunkus | 1;
|