Qual: Mutualize code. Only one function to build address format.

This commit is contained in:
Laurent Destailleur 2013-04-07 17:39:08 +02:00
parent 8e2171b64c
commit 5c3e6de073
7 changed files with 56 additions and 130 deletions

View File

@ -1530,40 +1530,6 @@ class Adherent extends CommonObject
return $result; 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) * Retourne le libelle du statut d'un adherent (brouillon, valide, resilie)
* *

View File

@ -866,41 +866,6 @@ class Contact extends CommonObject
return $result; 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 * Return label of a civility contact
* *

View File

@ -78,6 +78,26 @@ abstract class CommonObject
return dol_trunc($ret,$maxlen); 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. * Check if ref is used.
* *

View File

@ -675,19 +675,21 @@ function dol_get_fiche_end($notab=0)
* Return a formated address (part address/zip/town/state) according to country rules * Return a formated address (part address/zip/town/state) according to country rules
* *
* @param Object $object A company or contact object * @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 * @return string Formated string
*/ */
function dol_format_address($object) function dol_format_address($object,$withcountry=0,$sep="\n")
{ {
$ret=''; $ret='';
$countriesusingstate=array('US','IN','GB','ES'); $countriesusingstate=array('AU','US','IN','GB','ES');
// Address // Address
$ret .= $object->address; $ret .= $object->address;
// Zip/Town/State // 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)) if ($object->state && in_array($object->country_code,$countriesusingstate))
{ {
$ret.=", ".$object->state; $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 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)) if ($object->state && in_array($object->country_code,$countriesusingstate))
{ {
$ret.=", ".$object->state; $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; $ret .= ' '.$object->town;
if ($object->state && in_array($object->country_code,$countriesusingstate)) 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 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; $ret .= ' '.$object->town;
if ($object->state && in_array($object->country_code,$countriesusingstate)) 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; return $ret;
} }

View File

@ -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 * Return list of contacts emails existing for third party
* *

View File

@ -413,6 +413,10 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$object->country_code='US'; $object->country_code='US';
$address=dol_format_address($object); $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); $this->assertEquals("21 jump street\nMyTown, MyState, 99999",$address);
} }

View File

@ -444,9 +444,10 @@ class SocieteTest extends PHPUnit_Framework_TestCase
$localobjectadd->address='New address'; $localobjectadd->address='New address';
$localobjectadd->zip='New zip'; $localobjectadd->zip='New zip';
$localobjectadd->town='New town'; $localobjectadd->town='New town';
$localobjectadd->state='New state';
$result=$localobjectadd->getFullAddress(1); $result=$localobjectadd->getFullAddress(1);
print __METHOD__." id=".$localobjectadd->id." result=".$result."\n"; 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; return $localobjectadd->id;
} }