1 package SL::Controller::RequirementSpec;
6 use parent qw(SL::Controller::Base);
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::Paginated;
11 use SL::Controller::Helper::Sorted;
12 use SL::Controller::Helper::ParseFilter;
13 use SL::Controller::Helper::ReportGenerator;
14 use SL::Controller::Helper::RequirementSpec;
17 use SL::DB::RequirementSpecComplexity;
18 use SL::DB::RequirementSpecRisk;
19 use SL::DB::RequirementSpecStatus;
20 use SL::DB::RequirementSpecType;
21 use SL::DB::RequirementSpec;
22 use SL::Helper::Flash;
23 use SL::Locale::String;
24 use SL::Template::LaTeX;
26 use Rose::Object::MakeMethods::Generic
28 scalar => [ qw(requirement_spec_item customers types statuses db_args flat_filter visible_item visible_section) ],
29 'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects copy_source js current_text_block_output_position) ],
32 __PACKAGE__->run_before('setup');
33 __PACKAGE__->run_before('load_select_options', only => [ qw(new ajax_edit create update list) ]);
36 __PACKAGE__->get_models_url_params('flat_filter');
37 __PACKAGE__->make_paginated(
38 MODEL => 'RequirementSpec',
39 PAGINATE_ARGS => 'db_args',
43 __PACKAGE__->make_sorted(
44 MODEL => 'RequirementSpec',
47 DEFAULT_BY => 'customer',
50 customer => t8('Customer'),
52 type => t8('Requirement Spec Type'),
53 status => t8('Requirement Spec Status'),
54 projectnumber => t8('Project Number'),
55 version => t8('Version'),
56 mtime => t8('Last modification'),
67 $self->setup_db_args_from_filter;
68 $self->flat_filter({ map { $_->{key} => $_->{value} } $::form->flatten_variables('filter') });
70 $self->prepare_report;
72 my $requirement_specs = $self->get_models(%{ $self->db_args });
74 $self->report_generator_list_objects(report => $self->{report}, objects => $requirement_specs);
80 $self->requirement_spec(SL::DB::RequirementSpec->new(is_template => $::form->{is_template}));
82 if ($self->copy_source) {
83 $self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate)
86 $self->render('requirement_spec/new', title => $self->requirement_spec->is_template ? t8('Create a new requirement spec template') : t8('Create a new requirement spec'));
89 sub action_ajax_edit {
92 $self->render('requirement_spec/_form', { layout => 0 }, submit_as => 'ajax');
95 sub action_ajax_show_time_and_cost_estimate {
98 $self->render('requirement_spec/_show_time_and_cost_estimate', { layout => 0 });
101 sub action_ajax_cancel_time_and_cost_estimate {
104 my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
107 ->replaceWith('#time_cost_estimate', $html)
111 sub action_ajax_edit_time_and_cost_estimate {
114 my $html = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
117 ->replaceWith('#time_cost_estimate', $html)
121 sub action_ajax_save_time_and_cost_estimate {
124 $self->requirement_spec->db->do_transaction(sub {
127 foreach my $attributes (@{ $::form->{requirement_spec_items} || [] }) {
128 SL::DB::RequirementSpecItem
129 ->new(id => delete $attributes->{id})
131 ->update_attributes(%{ $attributes });
137 my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
138 $self->js->replaceWith('#time_cost_estimate', $html);
140 if ($self->visible_section) {
141 $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
142 $self->js->html('#column-content', $html);
145 $self->js->render($self);
151 my $title = $self->requirement_spec->is_template ? t8('Show requirement spec template') : t8('Show requirement spec');
152 my $item = $::form->{requirement_spec_item_id} ? SL::DB::RequirementSpecItem->new(id => $::form->{requirement_spec_item_id})->load : @{ $self->requirement_spec->sections }[0];
153 $self->requirement_spec_item($item);
155 $self->render('requirement_spec/show', title => $title);
161 $self->requirement_spec(SL::DB::RequirementSpec->new);
162 $self->create_or_update;
167 $self->create_or_update;
173 if (eval { $self->requirement_spec->delete; 1; }) {
174 flash_later('info', t8('The requirement spec has been deleted.'));
176 flash_later('error', t8('The requirement spec is in use and cannot be deleted.'));
179 $self->redirect_to(action => 'list');
182 sub action_revert_to {
183 my ($self, %params) = @_;
185 return $self->js->error(t8('Cannot revert a versioned copy.'))->render($self) if $self->requirement_spec->working_copy_id;
187 my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
189 $self->requirement_spec->copy_from(
191 version_id => $versioned_copy->version_id,
194 flash_later('info', t8('The requirement spec has been reverted to version #1.', $self->requirement_spec->version->version_number));
195 $self->js->redirect_to($self->url_for(action => 'show', id => $self->requirement_spec->id))->render($self);
198 sub action_create_pdf {
199 my ($self, %params) = @_;
201 my %result = SL::Template::LaTeX->parse_and_create_pdf('requirement_spec.tex', SELF => $self, rspec => $self->requirement_spec);
203 $::form->error(t8('Conversion to PDF failed: #1', $result{error})) if $result{error};
205 my $attachment_name = $self->requirement_spec->type->description . ' ' . ($self->requirement_spec->working_copy_id || $self->requirement_spec->id);
206 $attachment_name .= ' (v' . $self->requirement_spec->version->version_number . ')' if $self->requirement_spec->version;
207 $attachment_name .= '.pdf';
208 $attachment_name =~ s/[^\wäöüÄÖÜß \-\+\(\)\[\]\{\}\.,]+/_/g;
210 $self->send_file($result{file_name}, type => 'application/pdf', name => $attachment_name);
211 unlink $result{file_name};
214 sub action_select_template_to_paste {
217 my @templates = grep { @{ $_->sections } || @{ $_->text_blocks } } @{ SL::DB::Manager::RequirementSpec->get_all(where => [ is_template => 1 ], sort_by => 'lower(title)') };
218 $self->render('requirement_spec/select_template_to_paste', { layout => 0 }, TEMPLATES => \@templates);
221 sub action_paste_template {
222 my ($self, %params) = @_;
224 my $template = SL::DB::RequirementSpec->new(id => $::form->{template_id})->load;
225 my %result = $self->requirement_spec->paste_template($template);
227 return $self->js->error($self->requirement_spec->error)->render($self) if !%result;
229 $self->render_pasted_text_block($_) for sort { $a->position <=> $b->position } @{ $result{text_blocks} };
230 $self->render_pasted_section($_) for sort { $a->position <=> $b->position } @{ $result{sections} };
232 if (@{ $result{sections} } && (($::form->{current_content_type} || 'sections') eq 'sections') && !$::form->{current_content_id}) {
233 $self->render_first_pasted_section_as_list($result{sections}->[0]);
236 $self->invalidate_version->render($self);
246 $::auth->assert('sales_quotation_edit');
247 $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
248 $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
249 $self->init_visible_section;
254 sub init_complexities {
256 return SL::DB::Manager::RequirementSpecComplexity->get_all_sorted;
261 return SL::DB::Manager::RequirementSpecRisk->get_all_sorted;
266 $self->projects(SL::DB::Manager::Project->get_all_sorted);
269 sub init_requirement_spec {
271 $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
274 sub init_copy_source {
276 $self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
281 $self->js(SL::ClientJS->new);
284 sub init_current_text_block_output_position {
286 $self->current_text_block_output_position($::form->{current_content_type} !~ m/^(?:text-blocks|tb)-(front|back)/ ? -1 : $1 eq 'front' ? 0 : 1);
289 sub load_select_options {
292 my @filter = ('!obsolete' => 1);
293 @filter = ( or => [ @filter, id => $self->requirement_spec->customer_id ] ) if $self->requirement_spec && $self->requirement_spec->customer_id;
295 $self->customers(SL::DB::Manager::Customer->get_all_sorted(where => \@filter));
296 $self->statuses( SL::DB::Manager::RequirementSpecStatus->get_all_sorted);
297 $self->types( SL::DB::Manager::RequirementSpecType->get_all_sorted);
304 sub create_or_update {
306 my $is_new = !$self->requirement_spec->id;
307 my $params = delete($::form->{requirement_spec}) || { };
309 $self->requirement_spec->assign_attributes(%{ $params });
311 my $title = $is_new && $self->requirement_spec->is_template ? t8('Create a new requirement spec template')
312 : $is_new ? t8('Create a new requirement spec')
313 : $self->requirement_spec->is_template ? t8('Edit requirement spec template')
314 : t8('Edit requirement spec');
316 my @errors = $self->requirement_spec->validate;
319 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
321 flash('error', @errors);
322 $self->render('requirement_spec/new', title => $title);
326 my $db = $self->requirement_spec->db;
327 if (!$db->do_transaction(sub {
328 if ($self->copy_source) {
329 $self->requirement_spec($self->copy_source->create_copy(%{ $params }));
331 $self->requirement_spec->save;
334 $::lxdebug->message(LXDebug::WARN(), "Error: " . $db->error);
335 @errors = ($::locale->text('Saving failed. Error message from the database: #1'), $db->error);
336 return $self->js->error(@errors)->render($self) if $::request->is_ajax;
338 $self->requirement_spec->id(undef) if $is_new;
339 flash('error', @errors);
340 return $self->render('requirement_spec/new', title => $title);
343 my $info = $self->requirement_spec->is_template ? t8('The requirement spec template has been saved.') : t8('The requirement spec has been saved.');
345 if ($::request->is_ajax) {
346 my $html = $self->render('requirement_spec/_header', { output => 0 });
347 return $self->invalidate_version
348 ->replaceWith('#requirement-spec-header', $html)
349 ->flash('info', $info)
353 flash_later('info', $info);
354 $self->redirect_to(action => 'show', id => $self->requirement_spec->id);
357 sub setup_db_args_from_filter {
360 $self->{filter} = {};
361 my %args = parse_filter(
363 with_objects => [ 'customer', 'type', 'status', 'project' ],
364 launder_to => $self->{filter},
369 @{ $args{where} || [] },
370 working_copy_id => undef,
371 is_template => $::form->{is_template} ? 1 : 0,
374 $self->db_args(\%args);
380 my $callback = $self->get_callback;
382 my $is_template = $::form->{is_template};
383 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
384 $self->{report} = $report;
386 my @columns = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime version);
387 my @sortable = $is_template ? qw(title mtime) : qw(title customer status type projectnumber mtime);
390 title => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
391 mtime => { sub => sub { ($_[0]->mtime || $_[0]->itime)->to_kivitendo(precision => 'minute') } },
397 customer => { raw_data => sub { $self->presenter->customer($_[0]->customer, display => 'table-cell', callback => $callback) },
398 sub => sub { $_[0]->customer->name } },
399 projectnumber => { raw_data => sub { $self->presenter->project($_[0]->project, display => 'table-cell', callback => $callback) },
400 sub => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
401 status => { sub => sub { $_[0]->status->description } },
402 type => { sub => sub { $_[0]->type->description } },
403 version => { sub => sub { $_[0]->version_id ? $_[0]->version->version_number : t8('Working copy without version') } },
407 map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
409 $report->set_options(
410 std_column_visibility => 1,
411 controller_class => 'RequirementSpec',
412 output_format => 'HTML',
413 raw_top_info_text => $self->render('requirement_spec/report_top', { output => 0 }, is_template => $is_template),
414 raw_bottom_info_text => $self->render('requirement_spec/report_bottom', { output => 0 }),
415 title => $is_template ? t8('Requirement Spec Templates') : t8('Requirement Specs'),
416 allow_pdf_export => 1,
417 allow_csv_export => 1,
419 $report->set_columns(%column_defs);
420 $report->set_column_order(@columns);
421 $report->set_export_options(qw(list filter));
422 $report->set_options_from_form;
423 $self->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
425 $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
428 sub invalidate_version {
431 my $rspec = SL::DB::RequirementSpec->new(id => $self->requirement_spec->id)->load;
432 return $self->js if $rspec->is_template;
434 $rspec->invalidate_version;
436 my $html = $self->render('requirement_spec/_version', { output => 0 }, requirement_spec => $rspec);
437 return $self->js->html('#requirement_spec_version', $html);
440 sub render_pasted_text_block {
441 my ($self, $text_block, %params) = @_;
443 if ($self->current_text_block_output_position == $text_block->output_position) {
444 my $html = $self->render('requirement_spec_text_block/_text_block', { output => 0 }, text_block => $text_block);
446 ->appendTo($html, '#text-block-list')
447 ->hide('#text-block-list-empty');
450 my $node = $self->presenter->requirement_spec_text_block_jstree_data($text_block);
451 my $front_back = $text_block->output_position == 0 ? 'front' : 'back';
453 ->jstree->create_node('#tree', "#tb-${front_back}", 'last', $node)
454 ->jstree->open_node( '#tree', "#tb-${front_back}");
457 sub render_pasted_section {
458 my ($self, $item, $parent_id) = @_;
460 my $node = $self->presenter->requirement_spec_item_jstree_data($item);
462 ->jstree->create_node('#tree', $parent_id ? "#fb-${parent_id}" : '#sections', 'last', $node)
463 ->jstree->open_node( '#tree', $parent_id ? "#fb-${parent_id}" : '#sections');
465 $self->render_pasted_section($_, $item->id) for @{ $item->children_sorted };
468 sub render_first_pasted_section_as_list {
469 my ($self, $section, %params) = @_;
471 my $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $section);
473 ->html('#column-content', $html)
474 ->val( '#current_content_type', $section->item_type)
475 ->val( '#current_content_id', $section->id)
476 ->jstree->select_node('#tree', '#fb-' . $section->id);