Updated method to fetch state name, code and id to match method used with country

Reverted some previous changes to allow backwards compatibility
Extended MAIN_SHOW_REGION_IN_STATE to include showing region in thirdparty card
This commit is contained in:
Rui Strecht 2017-09-20 12:06:35 +01:00
parent 76a2513c76
commit f1126b65bf
4 changed files with 112 additions and 26 deletions

View File

@ -67,7 +67,19 @@ if ( ($action == 'update' && ! GETPOST("cancel",'alpha'))
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_ADDRESS", GETPOST("address",'nohtml'),'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_TOWN", GETPOST("town",'nohtml'),'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_ZIP", GETPOST("zipcode",'alpha'),'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_STATE", GETPOST("state_id",'alpha'),'chaine',0,'',$conf->entity);
//dolibarr_set_const($db, "MAIN_INFO_SOCIETE_STATE", GETPOST("state_id",'alpha'),'chaine',0,'',$conf->entity);
$tmparray=getState(GETPOST('state_id','int'),'all','',$db,$langs,0);
if (! empty($tmparray['id']))
{
$mysoc->state_id =$tmparray['id'];
$mysoc->state_code =$tmparray['code'];
$mysoc->state_label=$tmparray['label'];
$s=$mysoc->state_id.':'.$mysoc->state_code.':'.$mysoc->state_label;
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_STATE", $s,'chaine',0,'',$conf->entity);
}
dolibarr_set_const($db, "MAIN_MONNAIE", GETPOST("currency",'alpha'),'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_TEL", GETPOST("tel",'alpha'),'chaine',0,'',$conf->entity);
dolibarr_set_const($db, "MAIN_INFO_SOCIETE_FAX", GETPOST("fax",'alpha'),'chaine',0,'',$conf->entity);
@ -336,7 +348,7 @@ if ($action == 'edit' || $action == 'updateedit')
print '<tr class="oddeven"><td><label for="state_id">'.$langs->trans("State").'</label></td><td class="maxwidthonsmartphone">';
$formcompany->select_departement($conf->global->MAIN_INFO_SOCIETE_STATE,$mysoc->country_code,'state_id');
$formcompany->select_departement($mysoc->state_id,$mysoc->country_code,'state_id');
print '</td></tr>'."\n";
@ -721,7 +733,9 @@ else
print '<tr class="oddeven"><td>'.$langs->trans("State").'</td><td>';
if (! empty($conf->global->MAIN_INFO_SOCIETE_STATE)) print getState($conf->global->MAIN_INFO_SOCIETE_STATE);
if ($mysoc->state_id) {
print getState($mysoc->state_id,$conf->global->MAIN_SHOW_STATE_CODE,0,$conf->global->MAIN_SHOW_REGION_IN_STATE);
}
else print '&nbsp;';
print '</td></tr>';

View File

@ -186,7 +186,7 @@ abstract class CommonObject
* @var string
* @see getFullAddress()
*/
public $country;
public $country;
/**
* @var int
* @see getFullAddress(), country
@ -196,7 +196,32 @@ abstract class CommonObject
* @var string
* @see getFullAddress(), isInEEC(), country
*/
public $country_code;
public $country_code;
/**
* @var string
* @see getFullAddress()
*/
public $state;
/**
* @var int
* @see getFullAddress(), state
*/
public $state_id;
/**
* @var string
* @see getFullAddress(), state
*/
public $state_code;
/**
* @var string
* @see getFullAddress(), region
*/
public $region;
/**
* @var string
* @see getFullAddress(), region
*/
public $region_code;
/**
* @var int
@ -418,15 +443,25 @@ abstract class CommonObject
* @param string $sep Separator to use to build string
* @return string Full address string
*/
function getFullAddress($withcountry=0,$sep="\n")
function getFullAddress($withcountry=0,$sep="\n",$withregion=0)
{
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'];
}
$this->country =$tmparray['label'];
}
if ($withregion && $this->state_id && (empty($this->state_code) || empty($this->state) || empty($this->region) || empty($this->region_cpde)))
{
require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
$tmparray=getState($this->state_id,'all',0,1);
$this->state_code =$tmparray['code'];
$this->state =$tmparray['label'];
$this->region_code =$tmparray['region_code'];
$this->region =$tmparray['region'];
}
return dol_format_address($this, $withcountry, $sep);
}
@ -465,7 +500,7 @@ abstract class CommonObject
$out='<!-- BEGIN part to show address block -->';
$outdone=0;
$coords = $this->getFullAddress(1,', ');
$coords = $this->getFullAddress(1,', ',$conf->global->MAIN_SHOW_REGION_IN_STATE);
if ($coords)
{
if (! empty($conf->use_javascript_ajax))
@ -483,7 +518,12 @@ abstract class CommonObject
if (! in_array($this->country_code,$countriesusingstate) && empty($conf->global->MAIN_FORCE_STATE_INTO_ADDRESS) // If MAIN_FORCE_STATE_INTO_ADDRESS is on, state is already returned previously with getFullAddress
&& empty($conf->global->SOCIETE_DISABLE_STATE) && $this->state)
{
$out.=($outdone?' - ':'').$this->state;
if (!empty($conf->global->MAIN_SHOW_REGION_IN_STATE) && $conf->global->MAIN_SHOW_REGION_IN_STATE == 1 && $this->region) {
$out.=($outdone?' - ':'').$this->region.' - '.$this->state;
}
else {
$out.=($outdone?' - ':'').$this->state;
}
$outdone++;
}

View File

@ -391,13 +391,13 @@ function getCountry($searchkey,$withcode='',$dbtouse=0,$outputlangs='',$entconv=
* @param DoliDB $dbtouse Database handler (using in global way may fail because of conflicts with some autoload features)
* @return string String with state code or state name (Return value is always utf8 encoded and without entities)
*/
function getState($id,$dbtouse=0)
function getState($id,$withcode='',$dbtouse=0,$withregion='',$outputlangs='',$entconv=1,$searchlabel='')
{
global $db,$langs,$conf;
global $db,$langs;
if (! is_object($dbtouse)) $dbtouse=$db;
$sql = "SELECT d.rowid, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.nom as region_name FROM";
$sql = "SELECT d.rowid as id, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.code_region as region_code, r.nom as region_name FROM";
$sql .= " ".MAIN_DB_PREFIX ."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c";
$sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid and d.rowid=".$id;
$sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
@ -410,22 +410,41 @@ function getState($id,$dbtouse=0)
$obj = $dbtouse->fetch_object($resql);
if ($obj)
{
if(!empty($conf->global->MAIN_SHOW_STATE_CODE) && $conf->global->MAIN_SHOW_STATE_CODE == 1) {
if(!empty($conf->global->MAIN_SHOW_REGION_IN_STATE) && $conf->global->MAIN_SHOW_REGION_IN_STATE == 1) {
$label=((! empty($obj->name) && $obj->name!='-')?$obj->name:'');
if (is_object($outputlangs))
{
$outputlangs->load("dict");
if ($entconv) $label=($obj->code && ($outputlangs->trans("State".$obj->code)!="State".$obj->code))?$outputlangs->trans("State".$obj->code):$label;
else $label=($obj->code && ($outputlangs->transnoentitiesnoconv("State".$obj->code)!="State".$obj->code))?$outputlangs->transnoentitiesnoconv("State".$obj->code):$label;
}
if ($withcode == 1) {
if ($withregion == 1) {
return $label = $obj->region_name . ' - ' . $obj->code . ' - ' . ($langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):($obj->name!='-'?$obj->name:''));
}
else {
return $label = $obj->code . ' - ' . ($langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):($obj->name!='-'?$obj->name:''));
}
}
else {
if(!empty($conf->global->MAIN_SHOW_REGION_IN_STATE) && $conf->global->MAIN_SHOW_REGION_IN_STATE == 1) {
else if ($withcode == 2) {
if ($withregion == 1) {
return $label = $obj->region_name . ' - ' . ($langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):($obj->name!='-'?$obj->name:''));
}
else {
return $label = ($langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):($obj->name!='-'?$obj->name:''));
}
}
else if ($withcode === 'all') {
if ($withregion == 1) {
return array('id'=>$obj->id,'code'=>$obj->code,'label'=>$label,'region_code'=>$obj->region_code,'region'=>$obj->region_name);
}
else {
return array('id'=>$obj->id,'code'=>$obj->code,'label'=>$label);
}
}
else {
return $label;
}
}
else
{

View File

@ -3050,16 +3050,29 @@ class Societe extends CommonObject
$this->address=empty($conf->global->MAIN_INFO_SOCIETE_ADDRESS)?'':$conf->global->MAIN_INFO_SOCIETE_ADDRESS;
$this->zip=empty($conf->global->MAIN_INFO_SOCIETE_ZIP)?'':$conf->global->MAIN_INFO_SOCIETE_ZIP;
$this->town=empty($conf->global->MAIN_INFO_SOCIETE_TOWN)?'':$conf->global->MAIN_INFO_SOCIETE_TOWN;
$this->state_id=empty($conf->global->MAIN_INFO_SOCIETE_STATE)?'':$conf->global->MAIN_INFO_SOCIETE_STATE;
/* Disabled: we don't want any SQL request into method setMySoc. This method set object from env only.
If we need label, label must be loaded by output that need it from id (label depends on output language)
require_once DOL_DOCUMENT_ROOT .'/core/lib/company.lib.php';
if (!empty($conf->global->MAIN_INFO_SOCIETE_STATE)) {
$this->state_id= $conf->global->MAIN_INFO_SOCIETE_STATE;
$this->state = getState($this->state_id);
}
*/
// We define state_id, state_code and state
$state_id=$state_code=$state_label='';
if (! empty($conf->global->MAIN_INFO_SOCIETE_STATE))
{
$tmp=explode(':',$conf->global->MAIN_INFO_SOCIETE_STATE);
$state_id=$tmp[0];
if (! empty($tmp[1])) // If $conf->global->MAIN_INFO_SOCIETE_STATE is "id:code:label" or "id:code:label:region_code:region"
{
$state_code=$tmp[1];
$state_label=$tmp[2];
}
else // For backward compatibility
{
include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
$state_code=getState($state_id,2,$this->db); // This need a SQL request, but it's the old feature that should not be used anymore
$state_label=getState($state_id,0,$this->db); // This need a SQL request, but it's the old feature that should not be used anymore
}
}
$this->state_id=$state_id;
$this->state_code=$state_code;
$this->state=$state_label;
if (is_object($langs)) $this->state=($langs->trans('State'.$state_code)!='State'.$state_code)?$langs->trans('State'.$state_code):$state_label;
$this->note_private=empty($conf->global->MAIN_INFO_SOCIETE_NOTE)?'':$conf->global->MAIN_INFO_SOCIETE_NOTE;