commit 0b50bdf8c8b97351bbfa747da48c6f8c194ac27a
Author: Tamino Steinert <tamino.steinert@tamino.st>
Date:   Fri Apr 15 13:35:50 2022 +0200

    Reclamation: set record_links automatic after reclamation->save

diff --git a/SL/Controller/Reclamation.pm b/SL/Controller/Reclamation.pm
index bcaea3775..2f0b79e20 100644
--- a/SL/Controller/Reclamation.pm
+++ b/SL/Controller/Reclamation.pm
@@ -83,6 +83,16 @@ __PACKAGE__->run_before('get_unalterable_data',
                           save_and_credit_note
                         )]);
 
+__PACKAGE__->run_before('get_record_links_data_from_form',
+                        only => [qw(
+                          save save_as_new print preview_pdf send_email
+                          save_and_show_email_dialog
+                          workflow_save_and_sales_or_purchase_reclamation
+                          save_and_order
+                          save_and_delivery_order
+                          save_and_credit_note
+                        )]);
+
 #
 # actions
 #
@@ -1643,6 +1653,27 @@ sub get_unalterable_data {
   }
 }
 
+# get data for record_links from form and store it in the object
+sub get_record_links_data_from_form {
+  my ($self) = @_;
+
+  my $reclamation = $self->reclamation;
+
+  $reclamation->{converted_from_record_id} = delete $::form->{converted_from_record_id};
+  $reclamation->{converted_from_record_type_ref} = delete $::form->{converted_from_record_type_ref};
+
+  my $from_record_item_ids = delete $::form->{converted_from_record_item_ids} ;
+  my $from_record_item_type_refs = delete $::form->{converted_from_record_item_type_refs} ;
+
+  if (scalar @{ $from_record_item_ids || [] }) {
+    for my $idx (0 .. $#{ $reclamation->items_sorted }) {
+      my $reclamation_item = $reclamation->items_sorted->[$idx];
+      $reclamation_item->{converted_from_record_item_id} = $from_record_item_ids->[$idx];
+      $reclamation_item->{converted_from_record_item_type_ref} = $from_record_item_type_refs->[$idx];
+    }
+  }
+}
+
 # delete the reclamation
 #
 # And remove related files in the spool directory
@@ -1689,8 +1720,6 @@ sub save {
     SL::DB::ReclamationItem->new(id => $_)->delete for @{$self->item_ids_to_delete || []};
     $self->reclamation->save(cascade => 1);
 
-    $self->_link_to_records();
-
     $self->save_history('SAVED');
 
     1;
@@ -1699,58 +1728,6 @@ sub save {
   return $errors;
 }
 
-sub _link_to_records {
-  my ($self) = @_;
-  my %allowed_linked_records = map {$_ => 1} qw(
-    SL::DB::Reclamation
-    SL::DB::Order
-    SL::DB::DeliveryOrder
-    SL::DB::Invoice
-    SL::DB::PurchaseInvoice
-  );
-  my %allowed_linked_record_items = map {$_ => 1} qw(
-    SL::DB::ReclamationItem
-    SL::DB::OrderItem
-    SL::DB::DeliveryOrderItem
-    SL::DB::InvoiceItem
-  );
-
-  my $from_record_id = delete $::form->{converted_from_record_id};
-  $from_record_id ||= $self->reclamation->{converted_from_record_id};
-  if ($from_record_id) {
-    my $from_record_type = delete $::form->{converted_from_record_type_ref};
-    $from_record_type ||= $self->reclamation->{converted_from_record_type_ref};
-    unless ($allowed_linked_records{$from_record_type}) {
-      croak("Not allowed converted_from_record_type_ref: '" . $from_record_type);
-    }
-    my $src = ${from_record_type}->new(id => $from_record_id)->load;
-    $src->link_to_record($self->reclamation);
-    #clear converted_from;
-    delete $self->reclamation->{$_} for qw(converted_from_record_id converted_from_record_type_ref);
-
-    if (scalar @{ $::form->{converted_from_record_item_ids} || [] }) {
-      my $idx = -1;
-      my $from_record_item_ids = delete $::form->{converted_from_record_item_ids} ;
-      my $from_record_item_type_refs = delete $::form->{converted_from_record_item_type_refs} ;
-      for my $idx (0 .. $#{ $self->reclamation->items_sorted }) {
-        my $from_item_id = $from_record_item_ids->[$idx];
-        my $reclamation_item = $self->reclamation->items_sorted->[$idx];
-        $from_item_id ||= $reclamation_item->{converted_from_record_item_id};
-        next if !$from_item_id;
-        my $from_item_type = $from_record_item_type_refs->[$idx];
-        $from_item_type ||= $reclamation_item->{converted_from_record_item_type_ref};
-        unless ($allowed_linked_record_items{$from_item_type}) {
-          croak("Not allowed converted_from_record_item_type_ref: '" . $from_item_type);
-        }
-        my $src_item = ${from_item_type}->new(id => $from_item_id)->load;
-        $src_item->link_to_record($reclamation_item);
-        #clear converted_from;
-        delete $reclamation_item->{$_} for qw(converted_from_record_item_id converted_from_record_item_type_ref);
-      }
-    }
-  }
-}
-
 sub save_with_render_error {
   my ($self) = @_;
 
diff --git a/SL/DB/Reclamation.pm b/SL/DB/Reclamation.pm
index 412f5a5ad..f6710d638 100644
--- a/SL/DB/Reclamation.pm
+++ b/SL/DB/Reclamation.pm
@@ -55,6 +55,7 @@ __PACKAGE__->attr_sorted('items');
 __PACKAGE__->before_save('_before_save_set_record_number');
 __PACKAGE__->before_save('_before_save_remove_empty_custom_shipto');
 __PACKAGE__->before_save('_before_save_set_custom_shipto_module');
+__PACKAGE__->after_save('_after_save_link_records');
 
 # hooks
 
@@ -82,6 +83,53 @@ sub _before_save_set_custom_shipto_module {
   return 1;
 }
 
+sub _after_save_link_records {
+  my ($self) = @_;
+  my %allowed_linked_records = map {$_ => 1} qw(
+    SL::DB::Reclamation
+    SL::DB::Order
+    SL::DB::DeliveryOrder
+    SL::DB::Invoice
+    SL::DB::PurchaseInvoice
+  );
+  my %allowed_linked_record_items = map {$_ => 1} qw(
+    SL::DB::ReclamationItem
+    SL::DB::OrderItem
+    SL::DB::DeliveryOrderItem
+    SL::DB::InvoiceItem
+  );
+
+  my $from_record_id = $self->{converted_from_record_id};
+  if ($from_record_id) {
+    my $from_record_type = $self->{converted_from_record_type_ref};
+    unless ($allowed_linked_records{$from_record_type}) {
+      croak("Not allowed converted_from_record_type_ref: '" . $from_record_type);
+    }
+    my $src = ${from_record_type}->new(id => $from_record_id)->load;
+    $src->link_to_record($self);
+    #clear converted_from;
+    delete $self->{$_} for qw(converted_from_record_id converted_from_record_type_ref);
+
+    if (scalar @{ $self->items_sorted || [] }) {
+      for my $idx (0 .. $#{ $self->items_sorted }) {
+        my $reclamation_item = $self->items_sorted->[$idx];
+        my $from_item_id = $reclamation_item->{converted_from_record_item_id};
+        next if !$from_item_id;
+        my $from_item_type = $reclamation_item->{converted_from_record_item_type_ref};
+        unless ($allowed_linked_record_items{$from_item_type}) {
+          croak("Not allowed converted_from_record_item_type_ref: '" . $from_item_type);
+        }
+        my $src_item = ${from_item_type}->new(id => $from_item_id)->load;
+        $src_item->link_to_record($reclamation_item);
+        #clear converted_from;
+        delete $reclamation_item->{$_} for qw(converted_from_record_item_id converted_from_record_item_type_ref);
+      }
+    }
+  }
+
+  return 1;
+}
+
 # methods
 
 sub items { goto &reclamation_items; }
