Pflichtenhefte: Beim Einfügen ganzer Vorlagen Nummerierung neu vergeben
[kivitendo-erp.git] / SL / DB / RequirementSpec.pm
index f7fd2d5..0ad61aa 100644 (file)
@@ -27,6 +27,11 @@ __PACKAGE__->meta->add_relationship(
     class          => 'SL::DB::RequirementSpec',
     column_map     => { id => 'working_copy_id' },
   },
+  orders           => {
+    type           => 'one to many',
+    class          => 'SL::DB::RequirementSpecOrder',
+    column_map     => { id => 'requirement_spec_id' },
+  },
 );
 
 __PACKAGE__->meta->initialize;
@@ -71,6 +76,13 @@ sub sections_sorted {
 
 sub sections { &sections_sorted; }
 
+sub orders_sorted {
+  my ($self, %params) = _hashify(1, @_);
+  my $by              = $params{by} || 'itime';
+
+  return [ sort { $a->$by cmp $b->$by } @{ $self->orders } ];
+}
+
 sub displayable_name {
   my ($self) = @_;
 
@@ -111,16 +123,35 @@ sub _create_copy {
 }
 
 sub _copy_from {
-  my ($self, $source, %attributes) = @_;
+  my ($self, $params, %attributes) = @_;
+
+  my $source = $params->{source};
 
   croak "Missing parameter 'source'" unless $source;
 
   # Copy attributes.
-  $self->assign_attributes(map({ ($_ => $source->$_) } qw(type_id status_id customer_id project_id title hourly_rate net_sum previous_section_number previous_fb_number is_template)),
-                           %attributes);
+  if (!$params->{paste_template}) {
+    $self->assign_attributes(map({ ($_ => $source->$_) } qw(type_id status_id customer_id project_id title hourly_rate time_estimation previous_section_number previous_fb_number is_template)),
+                             %attributes);
+  }
+
+  my %paste_template_result;
 
   # Clone text blocks.
-  $self->text_blocks(map { Rose::DB::Object::Helpers::clone_and_reset($_) } @{ $source->text_blocks });
+  my $clone_text_block = sub {
+    my ($text_block) = @_;
+    my $cloned       = Rose::DB::Object::Helpers::clone_and_reset($text_block);
+    $cloned->position(undef);
+    return $cloned;
+  };
+
+  $paste_template_result{text_blocks} = [ map { $clone_text_block->($_) } @{ $source->text_blocks_sorted } ];
+
+  if (!$params->{paste_template}) {
+    $self->text_blocks($paste_template_result{text_blocks});
+  } else {
+    $self->add_text_blocks($paste_template_result{text_blocks});
+  }
 
   # Save new object -- we need its ID for the items.
   $self->save;
@@ -133,6 +164,8 @@ sub _copy_from {
     my ($item) = @_;
     my $cloned = Rose::DB::Object::Helpers::clone_and_reset($item);
     $cloned->requirement_spec_id($self->id);
+    $cloned->position(undef);
+    $cloned->fb_number(undef) if $params->{paste_template};
     $cloned->children(map { $clone_item->($_) } @{ $item->children });
 
     $id_to_clone{ $item->id } = $cloned;
@@ -140,7 +173,13 @@ sub _copy_from {
     return $cloned;
   };
 
-  $self->items(map { $clone_item->($_) } @{ $source->sections });
+  $paste_template_result{sections} = [ map { $clone_item->($_) } @{ $source->sections_sorted } ];
+
+  if (!$params->{paste_template}) {
+    $self->items($paste_template_result{sections});
+  } else {
+    $self->add_items($paste_template_result{sections});
+  }
 
   # Save the items -- need to do that before setting dependencies.
   $self->save;
@@ -151,15 +190,21 @@ sub _copy_from {
     $id_to_clone{ $item->id }->update_attributes(dependencies => [ map { $id_to_clone{$_->id} } @{ $item->dependencies } ]);
   }
 
-  $self->update_attributes(%attributes);
+  $self->update_attributes(%attributes) unless $params->{paste_template};
 
-  return $self;
+  return %paste_template_result;
 }
 
 sub copy_from {
   my ($self, $source, %attributes) = @_;
 
-  $self->db->with_transaction(sub { $self->_copy_from($source, %attributes); });
+  $self->db->with_transaction(sub { $self->_copy_from({ source => $source, paste_template => 0 }, %attributes); });
+}
+
+sub paste_template {
+  my ($self, $template) = @_;
+
+  $self->db->with_transaction(sub { $self->_copy_from({ source => $template, paste_template => 1 }); });
 }
 
 sub highest_version {
@@ -329,7 +374,7 @@ saved. Creating the copy happens within a transaction.
 
 =item C<create_version %attributes>
 
-Prerequisites: C<$self> must be a working copy (see L</working_copy>),
+Prerequisites: C<$self> must be a working copy (see the overview),
 not a versioned copy.
 
 This function creates a new version for C<$self>. This involves
@@ -376,7 +421,7 @@ copy and the last version created for it.
 
 =item C<invalidate_version>
 
-Prerequisites: C<$self> must be a working copy (see L</working_copy>),
+Prerequisites: C<$self> must be a working copy (see the overview),
 not a versioned copy.
 
 Sets the C<version_id> field to C<undef> and saves C<$self>.