From d593878c781aa341b0baa88a2ba102fd4ac715d5 Mon Sep 17 00:00:00 2001
From: Regis Houssin <regis@dolibarr.fr>
Date: Mon, 2 Jul 2012 17:07:37 +0200
Subject: [PATCH] Fix: big problem with linked elements

---
 htdocs/core/class/commonobject.class.php   |  2 +-
 htdocs/core/lib/sendings.lib.php           | 15 ++++++++++-----
 htdocs/expedition/fiche.php                |  3 ---
 htdocs/livraison/class/livraison.class.php |  8 ++++----
 4 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index e5571e70464..f24574f41fe 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -1646,7 +1646,7 @@ abstract class CommonObject
 
         $sourceid = (! empty($sourceid) ? $sourceid : $this->id);
         $targetid = (! empty($targetid) ? $targetid : $this->id);
-        $sourcetype = (! empty($sourcetype) ? $sourcetype : (! empty($this->origin) ? $this->origin : $this->element));
+        $sourcetype = (! empty($sourcetype) ? $sourcetype : $this->element);
         $targettype = (! empty($targettype) ? $targettype : $this->element);
 
         // Links beetween objects are stored in this table
diff --git a/htdocs/core/lib/sendings.lib.php b/htdocs/core/lib/sendings.lib.php
index 1a14077c8b6..4003db49467 100644
--- a/htdocs/core/lib/sendings.lib.php
+++ b/htdocs/core/lib/sendings.lib.php
@@ -45,12 +45,17 @@ function shipping_prepare_head($object)
 	$head[$h][2] = 'shipping';
 	$h++;
 
-	if ($conf->livraison_bon->enabled && $user->rights->expedition->livraison->lire && ! empty($object->linkedObjectsIds['delivery'][0]))
+	if ($conf->livraison_bon->enabled && $user->rights->expedition->livraison->lire)
 	{
-		$head[$h][0] = DOL_URL_ROOT."/livraison/fiche.php?id=".$object->linkedObjectsIds['delivery'][0];
-		$head[$h][1] = $langs->trans("DeliveryCard");
-		$head[$h][2] = 'delivery';
-		$h++;
+		// delivery link
+		$object->fetchObjectLinked($object->id,$object->element);
+		if (! empty($object->linkedObjectsIds['delivery'][0]))
+		{
+			$head[$h][0] = DOL_URL_ROOT."/livraison/fiche.php?id=".$object->linkedObjectsIds['delivery'][0];
+			$head[$h][1] = $langs->trans("DeliveryCard");
+			$head[$h][2] = 'delivery';
+			$h++;
+		}
 	}
 
 	$head[$h][0] = DOL_URL_ROOT."/expedition/contact.php?id=".$object->id;
diff --git a/htdocs/expedition/fiche.php b/htdocs/expedition/fiche.php
index ac45f0db7f3..88464400297 100644
--- a/htdocs/expedition/fiche.php
+++ b/htdocs/expedition/fiche.php
@@ -872,9 +872,6 @@ else
             $soc = new Societe($db);
             $soc->fetch($object->socid);
 
-            // delivery link
-            $object->fetchObjectLinked($object->id,$object->element,-1,-1);
-
             $head=shipping_prepare_head($object);
             dol_fiche_head($head, 'shipping', $langs->trans("Sending"), 0, 'sending');
 
diff --git a/htdocs/livraison/class/livraison.class.php b/htdocs/livraison/class/livraison.class.php
index b63fdba5302..3dcfd88f220 100644
--- a/htdocs/livraison/class/livraison.class.php
+++ b/htdocs/livraison/class/livraison.class.php
@@ -345,7 +345,7 @@ class Livraison extends CommonObject
 				if (is_readable(DOL_DOCUMENT_ROOT .'/core/modules/livraison/'.$modName.'.php'))
 				{
 					require_once DOL_DOCUMENT_ROOT .'/core/modules/livraison/'.$modName.'.php';
-					
+
 					$now=dol_now();
 
 					// Recuperation de la nouvelle reference
@@ -552,7 +552,7 @@ class Livraison extends CommonObject
 	{
         require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
 		$this->db->begin();
-		
+
 		$error=0;
 
 		$sql = "DELETE FROM ".MAIN_DB_PREFIX."livraisondet";
@@ -562,7 +562,7 @@ class Livraison extends CommonObject
 			// Delete linked object
 			$res = $this->deleteObjectLinked();
 			if ($res < 0) $error++;
-			
+
 			if (! $error)
 			{
 				$sql = "DELETE FROM ".MAIN_DB_PREFIX."livraison";
@@ -807,7 +807,7 @@ class Livraison extends CommonObject
 		global $langs;
 
 		// Get the linked object
-		$this->fetchObjectLinked(-1,-1,$this->id,$this->element);
+		$this->fetchObjectLinked('','',$this->id,$this->element);
 		//var_dump($this->linkedObjectIds);
 		// Get the product ref and qty in source
 		$sqlSourceLine = "SELECT st.rowid, st.description, st.qty";
-- 
GitLab