]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/RequirementSpec.pm
Projekt: Sortierung nach project_status
[mfinanz.git] / SL / Controller / RequirementSpec.pm
index 48af731e515a2bf924c5f0a97f2f98ba3894c2af..b72a8c4b4e6110a6483b268e48cd2ad87846a8f7 100644 (file)
@@ -1,6 +1,7 @@
 package SL::Controller::RequirementSpec;
 
 use strict;
+use utf8;
 
 use parent qw(SL::Controller::Base);
 
@@ -20,16 +21,18 @@ use SL::DB::RequirementSpecType;
 use SL::DB::RequirementSpec;
 use SL::Helper::Flash;
 use SL::Locale::String;
+use SL::Template::LaTeX;
 
 use Rose::Object::MakeMethods::Generic
 (
   scalar                  => [ qw(requirement_spec_item customers types statuses db_args flat_filter is_template visible_item visible_section) ],
-  'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects) ],
+  'scalar --get_set_init' => [ qw(requirement_spec complexities risks projects copy_source js) ],
 );
 
 __PACKAGE__->run_before('setup');
 __PACKAGE__->run_before('load_select_options',  only => [ qw(new ajax_edit create update list) ]);
 
+
 __PACKAGE__->get_models_url_params('flat_filter');
 __PACKAGE__->make_paginated(
   MODEL         => 'RequirementSpec',
@@ -49,6 +52,8 @@ __PACKAGE__->make_sorted(
   type          => t8('Requirement Spec Type'),
   status        => t8('Requirement Spec Status'),
   projectnumber => t8('Project Number'),
+  version       => t8('Version'),
+  mtime         => t8('Last modification'),
 );
 
 #
@@ -73,6 +78,11 @@ sub action_new {
   my ($self) = @_;
 
   $self->requirement_spec(SL::DB::RequirementSpec->new);
+
+  if ($self->copy_source) {
+    $self->requirement_spec->$_($self->copy_source->$_) for qw(type_id status_id customer_id title hourly_rate)
+  }
+
   $self->render('requirement_spec/new', title => t8('Create a new requirement spec'));
 }
 
@@ -93,9 +103,9 @@ sub action_ajax_cancel_time_and_cost_estimate {
 
   my $html   = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
 
-  SL::ClientJS->new
-    ->replaceWith('#time_cost_estimate', $html)
-    ->render($self);
+  $self->js
+   ->replaceWith('#time_cost_estimate', $html)
+   ->render($self);
 }
 
 sub action_ajax_edit_time_and_cost_estimate {
@@ -103,9 +113,9 @@ sub action_ajax_edit_time_and_cost_estimate {
 
   my $html   = $self->render('requirement_spec/_edit_time_and_cost_estimate', { output => 0 });
 
-  SL::ClientJS->new
-    ->replaceWith('#time_cost_estimate', $html)
-    ->render($self);
+  $self->js
+   ->replaceWith('#time_cost_estimate', $html)
+   ->render($self);
 }
 
 sub action_ajax_save_time_and_cost_estimate {
@@ -125,14 +135,14 @@ sub action_ajax_save_time_and_cost_estimate {
   });
 
   my $html = $self->render('requirement_spec/_show_time_and_cost_estimate', { output => 0 });
-  my $js   = SL::ClientJS->new->replaceWith('#time_cost_estimate', $html);
+  $self->js->replaceWith('#time_cost_estimate', $html);
 
   if ($self->visible_section) {
     $html = $self->render('requirement_spec_item/_section', { output => 0 }, requirement_spec_item => $self->visible_section);
-    $js->html('#column-content', $html);
+    $self->js->html('#column-content', $html);
   }
 
-  $js->render($self);
+  $self->js->render($self);
 }
 
 sub action_show {
@@ -168,12 +178,36 @@ sub action_destroy {
   $self->redirect_to(action => 'list');
 }
 
-sub action_reorder {
-  my ($self) = @_;
+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;
 
-  SL::DB::RequirementSpec->reorder_list(@{ $::form->{requirement_spec_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,
+  );
 
-  $self->render('1;', { type => 'js', inline => 1 });
+  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);
+}
+
+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};
 }
 
 #
@@ -183,7 +217,7 @@ sub action_reorder {
 sub setup {
   my ($self) = @_;
 
-  $::auth->assert('config');
+  $::auth->assert('sales_quotation_edit');
   $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
   $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
   $self->is_template($::form->{is_template} ? 1 : 0);
@@ -212,6 +246,16 @@ sub init_requirement_spec {
   $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{id})->load || die "No such requirement spec") if $::form->{id};
 }
 
+sub init_copy_source {
+  my ($self) = @_;
+  $self->copy_source(SL::DB::RequirementSpec->new(id => $::form->{copy_source_id})->load) if $::form->{copy_source_id};
+}
+
+sub init_js {
+  my ($self) = @_;
+  $self->js(SL::ClientJS->new);
+}
+
 sub load_select_options {
   my ($self) = @_;
 
@@ -238,18 +282,33 @@ sub create_or_update {
   my @errors = $self->requirement_spec->validate;
 
   if (@errors) {
-    return SL::ClientJS->new->error(@errors)->render($self) if $::request->is_ajax;
+    return $self->js->error(@errors)->render($self) if $::request->is_ajax;
 
     flash('error', @errors);
     $self->render('requirement_spec/new', title => $title);
     return;
   }
 
-  $self->requirement_spec->save;
+  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);
+    @errors = ($::locale->text('Saving failed. Error message from the database: #1'), $db->error);
+    return $self->js->error(@errors)->render($self) if $::request->is_ajax;
+
+    $self->requirement_spec->id(undef) if $is_new;
+    flash('error', @errors);
+    return $self->render('requirement_spec/new', title => $title);
+  }
 
   if ($::request->is_ajax) {
     my $html = $self->render('requirement_spec/_header', { output => 0 });
-    return SL::ClientJS->new
+    return $self->invalidate_version
       ->replaceWith('#requirement-spec-header', $html)
       ->flash('info', t8('The requirement spec has been saved.'))
       ->render($self);
@@ -272,7 +331,8 @@ sub setup_db_args_from_filter {
   $args{where} = [
     and => [
       @{ $args{where} || [] },
-      is_template => $self->is_template
+      working_copy_id => undef,
+      is_template     => $self->is_template
     ]];
 
   $self->db_args(\%args);
@@ -286,8 +346,8 @@ sub prepare_report {
   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
   $self->{report} = $report;
 
-  my @columns     = qw(title customer status type projectnumber);
-  my @sortable    = qw(title customer status type projectnumber);
+  my @columns     = qw(title customer status type projectnumber mtime version);
+  my @sortable    = qw(title customer status type projectnumber mtime);
 
   my %column_defs = (
     title         => { obj_link => sub { $self->url_for(action => 'show', id => $_[0]->id, callback => $callback) } },
@@ -297,6 +357,8 @@ sub prepare_report {
                        sub      => sub { $_[0]->project_id ? $_[0]->project->projectnumber : '' } },
     status        => { sub      => sub { $_[0]->status->description } },
     type          => { sub      => sub { $_[0]->type->description } },
+    version       => { sub      => sub { $_[0]->version_id ? $_[0]->version->version_number : t8('Working copy without version') } },
+    mtime         => { sub      => sub { $_[0]->mtime->to_kivitendo(precision => 'minute') } },
   );
 
   map { $column_defs{$_}->{text} ||= $::locale->text( $self->get_sort_spec->{$_}->{title} ) } keys %column_defs;
@@ -320,4 +382,15 @@ sub prepare_report {
   $self->disable_pagination if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
 }
 
+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);
+}
+
+
 1;