commit 991d16bbbc32c05e2e30e00711a42ba20207a8db
Author: Jan Büren <jan@kivitendo-premium.de>
Date:   Sat Jan 24 19:01:18 2015 +0100

    Weitere Positionen verknüpft II
    
    Gutschrift und Rückwartsverknüpfungen umgesetzt. Ferner für
    OE.pm und IS.pm den Aufruf von RecordLinks in eine foreach
    Schleife gesetzt (einfachere Codewartung).
    Kleinigkeiten die mir hier nicht gefällt: converted_from_quotation,
    die Variable ist jetzt noch drin und macht "nicht so viel Sinn",
    da orderitems nicht zwischen Angebot und Auftrag (oe) unterscheidet.
    Ansonsten ist die Positionsverknüpfung z.Z. genauer als die Beleg-Verknüpfung (s.a. #29)
    und der nächste Schritt hiefür wäre die Visualisierung an der Oberfläche

diff --git a/SL/IS.pm b/SL/IS.pm
index 0d3cdf0bd..7c6aac74e 100644
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -796,27 +796,18 @@ SQL
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
     }
-    # link oe items with invoice
-    if ($form->{"converted_from_orderitems_id_$i"}) {
-      RecordLinks->create_links('dbh'        => $dbh,
-                                'mode'       => 'ids',
-                                'from_table' => 'orderitems',
-                                'from_ids'   => $form->{"converted_from_orderitems_id_$i"},
-                                'to_table'   => 'invoice',
-                                'to_id'      => $form->{"invoice_id_$i"},
-      );
-      delete $form->{"converted_from_orderitems_id_$i"};
-    }
-    # link doi items with invoice
-    if ($form->{"converted_from_delivery_order_items_id_$i"}) {
-      RecordLinks->create_links('dbh'        => $dbh,
-                                'mode'       => 'ids',
-                                'from_table' => 'delivery_order_items',
-                                'from_ids'   => $form->{"converted_from_delivery_order_items_id_$i"},
-                                'to_table'   => 'invoice',
-                                'to_id'      => $form->{"invoice_id_$i"},
-      );
-      delete $form->{"converted_from_delivery_order_items_id_$i"};
+    # link previous items with invoice items
+    foreach (qw(delivery_order_items orderitems invoice)) {
+      if ($form->{"converted_from_${_}_id_$i"}) {
+        RecordLinks->create_links('dbh'        => $dbh,
+                                  'mode'       => 'ids',
+                                  'from_table' => $_,
+                                  'from_ids'   => $form->{"converted_from_${_}_id_$i"},
+                                  'to_table'   => 'invoice',
+                                  'to_id'      => $form->{"invoice_id_$i"},
+        );
+        delete $form->{"converted_from_${_}_id_$i"};
+      }
     }
   }
 
diff --git a/SL/OE.pm b/SL/OE.pm
index d8c66f5ef..3dd91bb97 100644
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -578,16 +578,20 @@ SQL
                                   name_prefix  => 'ic_',
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
-      # link quotation items with order items and delete entry (just one link)
-      if ($form->{"converted_from_quotation_orderitems_id_$i"}) {
-        RecordLinks->create_links('dbh'        => $dbh,
-                                  'mode'       => 'ids',
-                                  'from_table' => 'orderitems',
-                                  'from_ids'   => $form->{"converted_from_quotation_orderitems_id_$i"},
-                                  'to_table'   => 'orderitems',
-                                  'to_id'      => $orderitems_id,
-        );
-        delete $form->{"converted_from_quotation_orderitems_id_$i"};
+      # link previous items with orderitems
+      foreach (qw(quotation_orderitems orderitems invoice)) {
+        if ($form->{"converted_from_${_}_id_$i"}) {
+          my $table = $_;
+          $table    = 'orderitems' if $table eq 'quotation_orderitems';
+          RecordLinks->create_links('dbh'        => $dbh,
+                                    'mode'       => 'ids',
+                                    'from_table' => $table,
+                                    'from_ids'   => $form->{"converted_from_${_}_id_$i"},
+                                    'to_table'   => 'orderitems',
+                                    'to_id'      => $orderitems_id,
+          );
+          delete $form->{"converted_from_${_}_id_$i"};
+        }
       }
     }
   }
diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl
index 6a5556049..3840cbacd 100644
--- a/bin/mozilla/io.pl
+++ b/bin/mozilla/io.pl
@@ -436,10 +436,10 @@ sub display_row {
     my @hidden_vars;
     # add hidden ids for persistent (item|invoice)_ids and previous (converted_from*) ids
     if ($is_quotation) {
-      push @hidden_vars, qw(orderitems_id);
+      push @hidden_vars, qw(orderitems_id converted_from_orderitems_id);
     }
     if ($is_s_p_order) {
-      push @hidden_vars, qw(orderitems_id converted_from_quotation_orderitems_id);
+      push @hidden_vars, qw(orderitems_id converted_from_quotation_orderitems_id converted_from_invoice_id);
     }
     if ($is_invoice) {
       push @hidden_vars, qw(invoice_id converted_from_orderitems_id converted_from_delivery_order_items_id);
@@ -743,7 +743,8 @@ sub remove_emptied_rows {
                 stock_out stock_in has_sernumber reqdate orderitems_id
                 active_price_source active_discount_source delivery_order_items_id
                 invoice_id converted_from_quotation_orderitems_id
-                converted_from_orderitems_id converted_from_delivery_order_items_id);
+                converted_from_orderitems_id converted_from_delivery_order_items_id
+                converted_from_invoice_id);
 
   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
   push @flds, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
@@ -883,6 +884,7 @@ sub order {
     map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"})
             if ($form->{"${_}_${i}"}) }
         qw(ship qty sellprice listprice basefactor discount));
+    $form->{"converted_from_invoice_id_$i"} = delete $form->{"invoice_id_$i"};
   }
 
   &prepare_order;
@@ -903,7 +905,7 @@ sub quotation {
   # we are coming from *_order and convert to quotation
   # it seems that quotation is only called if we have a existing order
   if ($form->{type} =~  /(sales|purchase)_order/) {
-    delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
+    $form->{"converted_from_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
   }
   if ($form->{second_run}) {
     $form->{print_and_post} = 0;
diff --git a/bin/mozilla/is.pl b/bin/mozilla/is.pl
index e1e70ece5..c44351d00 100644
--- a/bin/mozilla/is.pl
+++ b/bin/mozilla/is.pl
@@ -983,8 +983,8 @@ sub credit_note {
       $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
     }
   }
-  # set new persistent ids for credit note
-  delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
+  # set new persistent ids for credit note and link previous invoice id
+  $form->{"converted_from_invoice_id_$_"} = delete $form->{"invoice_id_$_"} for 1 .. $form->{"rowcount"};
 
   my $currency = $form->{currency};
   &invoice_links;
diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl
index a313fb3cf..e28ff87ef 100644
--- a/bin/mozilla/oe.pl
+++ b/bin/mozilla/oe.pl
@@ -1820,8 +1820,8 @@ sub poso {
 
   # reset
   map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered ordnumber);
-  $form->{"converted_from_quotation_orderitems_id_$_"} = $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};  # always reset orderitems_id
-  delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};  # always reset orderitems_id
+  # this converted variable is also used for sales_order to purchase order and vice versa
+  $form->{"converted_from_quotation_orderitems_id_$_"} = delete $form->{"orderitems_id_$_"} for 1 .. $form->{"rowcount"};
 
   # if purchase_order was generated from sales_order, use  lastcost_$i as sellprice_$i
   # also reset discounts
