From 5c3e6de0738dab322293823a330ff79654f301f2 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 7 Apr 2013 17:39:08 +0200
Subject: [PATCH] Qual: Mutualize code. Only one function to build address
 format.

---
 htdocs/adherents/class/adherent.class.php | 34 ----------------
 htdocs/contact/class/contact.class.php    | 35 -----------------
 htdocs/core/class/commonobject.class.php  | 48 ++++++++++++++++-------
 htdocs/core/lib/functions.lib.php         | 28 +++++++------
 htdocs/societe/class/societe.class.php    | 34 ----------------
 test/phpunit/FunctionsTest.php            |  4 ++
 test/phpunit/SocieteTest.php              |  3 +-
 7 files changed, 56 insertions(+), 130 deletions(-)

diff --git a/htdocs/adherents/class/adherent.class.php b/htdocs/adherents/class/adherent.class.php
index f222d6e63e5..87a9b14877e 100644
--- a/htdocs/adherents/class/adherent.class.php
+++ b/htdocs/adherents/class/adherent.class.php
@@ -1530,40 +1530,6 @@ class Adherent extends CommonObject
         return $result;
     }
 
-
-    /**
-     * 	Return full address of member
-     *
-     * 	@param		int			$withcountry		1=Add country into address string
-     *  @param		string		$sep				Separator to use to build string
-     *	@return		string							Full address string
-     */
-    function getFullAddress($withcountry=0,$sep="\n")
-    {
-        $ret='';
-        if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country)))
-        {
-            require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
-            $tmparray=getCountry($this->country_id,'all');
-            $this->country_code=$tmparray['code'];
-            $this->country     =$tmparray['label'];
-        }
-
-        if (in_array($this->country_code,array('US')))
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        else
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        return trim($ret);
-    }
-
     /**
      *  Retourne le libelle du statut d'un adherent (brouillon, valide, resilie)
      *
diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php
index 28e23fd791f..73bdbc4a0f4 100644
--- a/htdocs/contact/class/contact.class.php
+++ b/htdocs/contact/class/contact.class.php
@@ -866,41 +866,6 @@ class Contact extends CommonObject
 		return $result;
 	}
 
-
-    /**
-     * 	Return full address of contact
-     *
-     * 	@param		int			$withcountry		1=Add country into address string
-     *  @param		string		$sep				Separator to use to build string
-     *	@return		string							Full address string
-     */
-    function getFullAddress($withcountry=0,$sep="\n")
-    {
-        $ret='';
-        if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country)))
-        {
-            require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
-            $tmparray=getCountry($this->country_id,'all');
-            $this->country_code=$tmparray['code'];
-            $this->country     =$tmparray['label'];
-        }
-
-        if (in_array($this->country_code,array('US')))
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        else
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        return trim($ret);
-    }
-
-
 	/**
 	 *    Return label of a civility contact
 	 *
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index e078639724c..f7bc6938043 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -78,6 +78,26 @@ abstract class CommonObject
         return dol_trunc($ret,$maxlen);
     }
 
+    /**
+     * 	Return full address of contact
+     *
+     * 	@param		int			$withcountry		1=Add country into address string
+     *  @param		string		$sep				Separator to use to build string
+     *	@return		string							Full address string
+     */
+    function getFullAddress($withcountry=0,$sep="\n")
+    {
+    	if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country)))
+    	{
+    		require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
+    		$tmparray=getCountry($this->country_id,'all');
+    		$this->country_code=$tmparray['code'];
+    		$this->country     =$tmparray['label'];
+    	}
+
+    	return dol_format_address($this, $withcountry, $sep);
+    }
+
     /**
      *  Check if ref is used.
      *
@@ -1558,7 +1578,7 @@ abstract class CommonObject
 
             // Add revenue stamp to total
             $this->total_ttc       += isset($this->revenuestamp)?$this->revenuestamp:0;
-            
+
             $this->db->free($resql);
 
             // Now update global field total_ht, total_ttc and tva
@@ -2139,7 +2159,7 @@ abstract class CommonObject
             			$this->array_options[$key]=$this->db->idate($this->array_options[$key]);
             			break;
                	}
-            } 
+            }
             $this->db->begin();
 
             $sql_del = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element."_extrafields WHERE fk_object = ".$this->id;
@@ -2189,27 +2209,27 @@ abstract class CommonObject
         }
         else return 0;
     }
-    
+
    /**
      * Function to show lines of extrafields with output datas
-     * 
+     *
      * @param	object	$extrafields	extrafield Object
-     * @param	string	$mode			Show output (view) or input (edit) for extrafield 
-     * 
-     * @return string	
+     * @param	string	$mode			Show output (view) or input (edit) for extrafield
+     *
+     * @return string
      */
     function showOptionals($extrafields,$mode='view')
     {
 		global $_POST;
-    	
+
 		$out = '';
-    	
+
 		if(count($extrafields->attribute_label) > 0)
 		{
 			$out .= "\n";
 			$out .= '<!-- showOptionalsInput --> ';
 			$out .= "\n";
-			
+
 			$e = 0;
 			foreach($extrafields->attribute_label as $key=>$label)
 			{
@@ -2226,7 +2246,7 @@ abstract class CommonObject
 						$out .= '<tr>';
 						$colspan='0';
 					}
-					else 
+					else
 					{
 						$out .= '<tr>';
 					}
@@ -2237,7 +2257,7 @@ abstract class CommonObject
 					}
 					$out .= '<td>'.$label.'</td>';
 					$out .='<td colspan="'.$colspan.'">';
-					    		
+
 					switch($mode) {
 					case "view":
 						$out .= $extrafields->showOutputField($key,$value);
@@ -2246,9 +2266,9 @@ abstract class CommonObject
 						$out .= $extrafields->showInputField($key,$value);
 						break;
 					}
-					    		
+
 					$out .= '</td>'."\n";
-					    		
+
 					if (! empty($conf->global->MAIN_EXTRAFIELDS_USE_TWO_COLUMS) && (($e % 2) == 1)) $out .= '</tr>';
 					else $out .= '</tr>';
 					$e++;
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 350e7c5f2e9..3fac6b67346 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -675,19 +675,21 @@ function dol_get_fiche_end($notab=0)
  *      Return a formated address (part address/zip/town/state) according to country rules
  *
  *      @param  Object		$object         A company or contact object
+ * 	    @param	int			$withcountry	1=Add country into address string
+ *      @param	string		$sep			Separator to use to build string
  *      @return string          			Formated string
  */
-function dol_format_address($object)
+function dol_format_address($object,$withcountry=0,$sep="\n")
 {
 	$ret='';
-	$countriesusingstate=array('US','IN','GB','ES');
+	$countriesusingstate=array('AU','US','IN','GB','ES');
 
 	// Address
 	$ret .= $object->address;
 	// Zip/Town/State
-	if (in_array($object->country_code,array('US')))   	// US: title firstname name \n address lines \n town, state, zip \n country
+	if (in_array($object->country_code,array('US','AU')))   	// US: title firstname name \n address lines \n town, state, zip \n country
 	{
-		$ret .= ($ret ? "\n" : '' ).$object->town;
+		$ret .= ($ret ? $sep : '' ).$object->town;
 		if ($object->state && in_array($object->country_code,$countriesusingstate))
 		{
 			$ret.=", ".$object->state;
@@ -696,16 +698,16 @@ function dol_format_address($object)
 	}
 	else if (in_array($object->country_code,array('GB'))) // UK: title firstname name \n address lines \n town state \n zip \n country
 	{
-		$ret .= ($ret ? "\n" : '' ).$object->town;
+		$ret .= ($ret ? $sep : '' ).$object->town;
 		if ($object->state && in_array($object->country_code,$countriesusingstate))
 		{
 			$ret.=", ".$object->state;
 		}
-		if ($object->zip) $ret .= ($ret ? "\n" : '' ).$object->zip;
+		if ($object->zip) $ret .= ($ret ? $sep : '' ).$object->zip;
 	}
-	else if (in_array($object->country_code,array('ES'))) // title firstname name \n address lines \n zip town \n state \n country
+	else if (in_array($object->country_code,array('ES'))) // ES: title firstname name \n address lines \n zip town \n state \n country
 	{
-		$ret .= ($ret ? "\n" : '' ).$object->zip;
+		$ret .= ($ret ? $sep : '' ).$object->zip;
 		$ret .= ' '.$object->town;
 		if ($object->state && in_array($object->country_code,$countriesusingstate))
 		{
@@ -715,7 +717,7 @@ function dol_format_address($object)
 
 	else                                        		// Other: title firstname name \n address lines \n zip town \n country
 	{
-		$ret .= ($ret ? "\n" : '' ).$object->zip;
+		$ret .= ($ret ? $sep : '' ).$object->zip;
 		$ret .= ' '.$object->town;
 		if ($object->state && in_array($object->country_code,$countriesusingstate))
 		{
@@ -723,6 +725,8 @@ function dol_format_address($object)
 		}
 	}
 
+	if ($withcountry) $ret.=($object->country?$sep.$object->country:'');
+
 	return $ret;
 }
 
@@ -2752,18 +2756,18 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
 	// Some test to guess with no need to make database access
 	if ($mysoc->country_code == 'ES') // For spain localtaxes 1 and 2, tax is qualified if buyer use local taxe
 	{
-		if ($local == 1) 
+		if ($local == 1)
 		{
 			if ($thirdparty_seller->id==$mysoc->id)
 			{
 				if (! $thirdparty_buyer->localtax1_assuj) return 0;
 			}
-			else 
+			else
 			{
 				if (! $thirdparty_seller->localtax1_assuj) return 0;
 			}
 		}
-		
+
 		if ($local == 2 && ! $thirdparty_buyer->localtax2_assuj) return 0;
 	}
 	else
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index 24339b08f79..3c4e1d657b8 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -1521,40 +1521,6 @@ class Societe extends CommonObject
         }
     }
 
-    /**
-     * 	Return full address of third party
-     *
-     * 	@param		int			$withcountry		1=Add country into address string
-     *  @param		string		$sep				Separator to use to build string
-     *	@return		string							Full address string
-     */
-    function getFullAddress($withcountry=0,$sep="\n")
-    {
-        $ret='';
-        if ($withcountry && $this->country_id && (empty($this->country_code) || empty($this->country)))
-        {
-            require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
-            $tmparray=getCountry($this->country_id,'all');
-            $this->country_code=$tmparray['code'];
-            $this->country     =$tmparray['label'];
-        }
-
-        if (in_array($this->country_code,array('US')))
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        else
-        {
-	        $ret.=($this->address?$this->address.$sep:'');
-	        $ret.=trim($this->zip.' '.$this->town);
-	        if ($withcountry) $ret.=($this->country?$sep.$this->country:'');
-        }
-        return trim($ret);
-    }
-
-
     /**
      *    Return list of contacts emails existing for third party
      *
diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php
index c6c80ae54f8..34276ce5892 100755
--- a/test/phpunit/FunctionsTest.php
+++ b/test/phpunit/FunctionsTest.php
@@ -413,6 +413,10 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
 
 		$object->country_code='US';
     	$address=dol_format_address($object);
+    	$this->assertEquals("21 jump street\nMyTown, MyState, 99999",$address);
+
+		$object->country_code='AU';
+    	$address=dol_format_address($object);
     	$this->assertEquals("21 jump street\nMyTown, MyState, 99999",$address);
     }
 
diff --git a/test/phpunit/SocieteTest.php b/test/phpunit/SocieteTest.php
index b132d0e2648..c7b3434218d 100755
--- a/test/phpunit/SocieteTest.php
+++ b/test/phpunit/SocieteTest.php
@@ -444,9 +444,10 @@ class SocieteTest extends PHPUnit_Framework_TestCase
         $localobjectadd->address='New address';
         $localobjectadd->zip='New zip';
         $localobjectadd->town='New town';
+        $localobjectadd->state='New state';
         $result=$localobjectadd->getFullAddress(1);
         print __METHOD__." id=".$localobjectadd->id." result=".$result."\n";
-        $this->assertContains("New address\nNew zip New town\nUnited States", $result);
+        $this->assertContains("New address\nNew town, New state, New zip\nUnited States", $result);
 
         return $localobjectadd->id;
     }
-- 
GitLab