From ad8440d276d2d524a43a71d5f7e2e25f37fa1859 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Fri, 18 Jun 2010 22:54:16 +0000
Subject: [PATCH] Show state in address for us and india

---
 htdocs/adherents/fiche.php                    |  2 +-
 htdocs/admin/company.php                      |  4 +-
 htdocs/lib/company.lib.php                    | 41 ++++++++++++--
 htdocs/lib/pdf.lib.php                        | 11 +++-
 .../multicompany/class/multicompany.class.php | 56 +++++++++----------
 5 files changed, 77 insertions(+), 37 deletions(-)

diff --git a/htdocs/adherents/fiche.php b/htdocs/adherents/fiche.php
index aab97cbe493..0b8107ef5a8 100644
--- a/htdocs/adherents/fiche.php
+++ b/htdocs/adherents/fiche.php
@@ -1226,7 +1226,7 @@ if ($rowid && $action != 'edit')
     print '<tr><td>'.$langs->trans("Zip").' / '.$langs->trans("Town").'</td><td class="valeur">'.$adh->cp.' '.$adh->ville.'</td></tr>';
 
 	// Country
-    print '<tr><td>'.$langs->trans("Country").'</td><td class="valeur">'.getCountryLabel($adh->pays_id).'</td></tr>';
+    print '<tr><td>'.$langs->trans("Country").'</td><td class="valeur">'.getCountry($adh->pays_id).'</td></tr>';
 
 	// Department
 	print '<tr><td>'.$langs->trans('State').'</td><td class="valeur">'.$adh->departement.'</td>';
diff --git a/htdocs/admin/company.php b/htdocs/admin/company.php
index 0b7094ab3a2..8a88e61a393 100644
--- a/htdocs/admin/company.php
+++ b/htdocs/admin/company.php
@@ -270,7 +270,7 @@ if ((isset($_GET["action"]) && $_GET["action"] == 'edit')
 
 	$var=!$var;
 	print '<tr '.$bc[$var].'><td>'.$langs->trans("State").'</td><td>';
-	$pays_code=getCountryLabel($conf->global->MAIN_INFO_SOCIETE_PAYS,2);
+	$pays_code=getCountry($conf->global->MAIN_INFO_SOCIETE_PAYS,2);
 	$formcompany->select_departement($conf->global->MAIN_INFO_SOCIETE_DEPARTEMENT,$pays_code,'departement_id');
 	print '</td></tr>'."\n";
 
@@ -601,7 +601,7 @@ else
 
 	$var=!$var;
 	print '<tr '.$bc[$var].'><td>'.$langs->trans("CompanyCountry").'</td><td>';
-	print getCountryLabel($conf->global->MAIN_INFO_SOCIETE_PAYS,1);
+	print getCountry($conf->global->MAIN_INFO_SOCIETE_PAYS,1);
 	print '</td></tr>';
 
 	$var=!$var;
diff --git a/htdocs/lib/company.lib.php b/htdocs/lib/company.lib.php
index 7b22467f6e0..e07f13089c0 100644
--- a/htdocs/lib/company.lib.php
+++ b/htdocs/lib/company.lib.php
@@ -180,19 +180,19 @@ function societe_prepare_head2($objsoc)
 
 
 /**
- *    \brief      Retourne le nom traduit ou code+nom d'un pays depuis id
+ *    \brief      Return country translated from an id
  *    \param      id          id of country
  *    \param      withcode    0=Return label, 1=Return code + label, 2=Return code
  *    \return     string      String with country code or translated country name
  */
-function getCountryLabel($id,$withcode=0)
+function getCountry($id,$withcode=0)
 {
 	global $db,$langs;
 
 	$sql = "SELECT rowid, code, libelle FROM ".MAIN_DB_PREFIX."c_pays";
 	$sql.= " WHERE rowid=".$id;
 
-	dol_syslog("Company.lib::getCountryLabel sql=".$sql);
+	dol_syslog("Company.lib::getCountry sql=".$sql);
 	$resql=$db->query($sql);
 	if ($resql)
 	{
@@ -208,8 +208,41 @@ function getCountryLabel($id,$withcode=0)
 		{
 			return $langs->trans("NotDefined");
 		}
-		$db->free($resql);
 	}
+	else dol_print_error($db,'');
+}
+
+/**
+ *    \brief      Return state translated from an id
+ *    \param      id          id of state (province/departement)
+ *    \param      withcode    0=Return label, 1=Return code + label, 2=Return code
+ *    \return     string      String with state code or translated state name
+ */
+function getState($id,$withcode=0)
+{
+	global $db,$langs;
+
+	$sql = "SELECT rowid, code_departement as code, nom as label FROM ".MAIN_DB_PREFIX."c_departements";
+	$sql.= " WHERE rowid=".$id;
+
+	dol_syslog("Company.lib::getState sql=".$sql);
+	$resql=$db->query($sql);
+	if ($resql)
+	{
+		$obj = $db->fetch_object($resql);
+		if ($obj)
+		{
+			$label=$obj->label;
+			if ($withcode == 1) return $label=$obj->code?"$obj->code":"$obj->code - $label";
+			else if ($withcode == 2) return $label=$obj->code;
+			else return $label;
+		}
+		else
+		{
+			return $langs->trans("NotDefined");
+		}
+	}
+	else dol_print_error($db,'');
 }
 
 /**
diff --git a/htdocs/lib/pdf.lib.php b/htdocs/lib/pdf.lib.php
index 6f2b2d9d0c8..fb4aaac9770 100644
--- a/htdocs/lib/pdf.lib.php
+++ b/htdocs/lib/pdf.lib.php
@@ -49,6 +49,11 @@ function pdf_build_address($outputlangs,$sourcecompany,$targetcompany='',$target
 	{
 		$stringaddress .= ($stringaddress ? "\n" : '' ).$outputlangs->convToOutputCharset($sourcecompany->address);
 		$stringaddress .= ($stringaddress ? "\n" : '' ).$outputlangs->convToOutputCharset($sourcecompany->cp).' '.$outputlangs->convToOutputCharset($sourcecompany->ville);
+		if (($sourcecompany->departement_id || $sourcecompany->departement) && in_array($sourcecompany->pays_code,array('US','IN')))
+		{
+			if ($sourcecompany->departement_id && empty($sourcecompany->departement)) $sourcecompany->departement=getState($sourcecompany->departement_id);
+			$stringaddress.=" - ".$outputlangs->convToOutputCharset($sourcecompany->departement);
+		}
 		$stringaddress .= "\n";
 		// Tel
 		if ($sourcecompany->tel) $stringaddress .= ($stringaddress ? "\n" : '' ).$outputlangs->transnoentities("Phone").": ".$outputlangs->convToOutputCharset($sourcecompany->tel);
@@ -68,8 +73,9 @@ function pdf_build_address($outputlangs,$sourcecompany,$targetcompany='',$target
 			// Recipient properties
 			$stringaddress.="\n".$outputlangs->convToOutputCharset($targetcontact->address);
 			$stringaddress.="\n".$outputlangs->convToOutputCharset($targetcontact->cp) . " " . $outputlangs->convToOutputCharset($targetcontact->ville);
-			if ($targetcompany->departement && in_array($targetcompany->pays_code,array('US','IN')))
+			if (($targetcompany->departement_id || $targetcompany->departement) && in_array($targetcompany->pays_code,array('US','IN')))
 			{
+				if ($targetcompany->departement_id && empty($targetcompany->departement)) $targetcompany->departement=getState($targetcompany->departement_id);
 				$stringaddress.=" - ".$outputlangs->convToOutputCharset($targetcompany->departement);
 			}
 			$stringaddress.="\n";
@@ -80,8 +86,9 @@ function pdf_build_address($outputlangs,$sourcecompany,$targetcompany='',$target
 			// Recipient properties
 			$stringaddress.="\n".$outputlangs->convToOutputCharset($targetcompany->address);
 			$stringaddress.="\n".$outputlangs->convToOutputCharset($targetcompany->cp) . " " . $outputlangs->convToOutputCharset($targetcompany->ville);
-			if ($targetcompany->departement && in_array($targetcompany->pays_code,array('US','IN')))
+			if (($targetcompany->departement_id || $targetcompany->departement) && in_array($targetcompany->pays_code,array('US','IN')))
 			{
+				if ($targetcompany->departement_id && empty($targetcompany->departement)) $targetcompany->departement=getState($targetcompany->departement_id);
 				$stringaddress.=" - ".$outputlangs->convToOutputCharset($targetcompany->departement);
 			}
 			$stringaddress.="\n";
diff --git a/htdocs/multicompany/class/multicompany.class.php b/htdocs/multicompany/class/multicompany.class.php
index bcd973c2354..82ec1be6789 100644
--- a/htdocs/multicompany/class/multicompany.class.php
+++ b/htdocs/multicompany/class/multicompany.class.php
@@ -34,7 +34,7 @@ class Multicompany
 	var $error;
 	//! Numero de l'erreur
 	var $errno = 0;
-	
+
 	var $entities = array();
 
 	/**
@@ -45,7 +45,7 @@ class Multicompany
 	function Multicompany($DB)
 	{
 		$this->db = $DB;
-		
+
 		$this->canvas = "default";
 		$this->name = "admin";
 		$this->description = "";
@@ -56,9 +56,9 @@ class Multicompany
 	 */
 	function Create($user,$datas)
 	{
-		
+
 	}
-	
+
 	/**
 	 *    \brief      Supression
 	 */
@@ -66,35 +66,35 @@ class Multicompany
 	{
 
 	}
-	
+
     /**
 	 *    \brief      Fetch entity
 	 */
 	function fetch($id)
 	{
 		global $conf;
-		
+
 		$sql = "SELECT ";
 		$sql.= $this->db->decrypt('name')." as name";
 		$sql.= ", ".$this->db->decrypt('value')." as value";
 		$sql.= " FROM ".MAIN_DB_PREFIX."const";
 		$sql.= " WHERE ".$this->db->decrypt('name')." LIKE 'MAIN_%'";
 		$sql.= " AND entity = ".$id;
-		
+
 		$result = $this->db->query($sql);
 		if ($result)
 		{
 			$num = $this->db->num_rows($result);
 			$entityDetails = array();
 			$i = 0;
-			
+
 			while ($i < $num)
 			{
 				$obj = $this->db->fetch_object($result);
 
 				if (preg_match('/^MAIN_INFO_SOCIETE_PAYS$/i',$obj->name))
 				{
-					$entityDetails[$obj->name] = getCountryLabel($obj->value);
+					$entityDetails[$obj->name] = getCountry($obj->value);
 				}
 				else if (preg_match('/^MAIN_MONNAIE$/i',$obj->name))
 				{
@@ -104,14 +104,14 @@ class Multicompany
 				{
 					$entityDetails[$obj->name] = $obj->value;
 				}
-				
+
 				$i++;
 			}
 			return $entityDetails;
 		}
-		
+
 	}
-	
+
     /**
 	 *    \brief      Enable/disable entity
 	 */
@@ -122,46 +122,46 @@ class Multicompany
 		$sql = "UPDATE ".MAIN_DB_PREFIX."entity";
 		$sql.= " SET ".$type." = ".$value;
 		$sql.= " WHERE rowid = ".$id;
-		
+
 		dol_syslog("Multicompany::setEntity sql=".$sql, LOG_DEBUG);
-		
+
 		$result = $this->db->query($sql);
 	}
-	
+
 	/**
 	 *    \brief      List of entities
 	 */
 	function getEntities($details=0,$visible=0)
 	{
 		global $conf;
-		
+
 		$sql = "SELECT rowid, label, description, visible, active";
 		$sql.= " FROM ".MAIN_DB_PREFIX."entity";
 		if ($visible) $sql.= " WHERE visible = 1";
-		
+
 		$result = $this->db->query($sql);
 		if ($result)
 		{
 			$num = $this->db->num_rows($result);
 			$i = 0;
-			
+
 			while ($i < $num)
 			{
 				$obj = $this->db->fetch_object($result);
-				
+
 				$this->entities[$i]['id']			= $obj->rowid;
 				$this->entities[$i]['label']		= $obj->label;
 				$this->entities[$i]['description'] 	= $obj->description;
 				$this->entities[$i]['visible'] 		= $obj->visible;
 				$this->entities[$i]['active']		= $obj->active;
 				if ($details) $this->entities[$i]['details'] = $this->fetch($obj->rowid);
-				
+
 				$i++;
 			}
 		}
-		
+
 	}
-	
+
 	/**
 	 *    \brief      Return combo list of entities.
 	 *    \param      entities    Entities array
@@ -170,9 +170,9 @@ class Multicompany
 	function select_entities($entities,$selected='',$option='')
 	{
 		$return = '<select class="flat" name="entity" '.$option.'>';
-				
+
 		if (is_array($entities))
-		{	
+		{
 			foreach ($entities as $entity)
 			{
 				if ($entity['active'] == 1)
@@ -186,7 +186,7 @@ class Multicompany
 			}
 		}
 		$return.= '</select>';
-		
+
 		return $return;
 	}
 
@@ -197,13 +197,13 @@ class Multicompany
 	function assign_smarty_values(&$smarty, $action='')
 	{
 		global $conf,$langs;
-		
+
 		$smarty->assign('langs', $langs);
-		
+
 		$picto='title.png';
 		if (empty($conf->browser->firefox)) $picto='title.gif';
 		$smarty->assign('title_picto', img_picto('',$picto));
-		
+
 		$smarty->assign('entities',$this->entities);
 		$smarty->assign('img_on',img_picto($langs->trans("Activated"),'on'));
 		$smarty->assign('img_off',img_picto($langs->trans("Disabled"),'off'));
-- 
GitLab