diff --git a/htdocs/admin/company.php b/htdocs/admin/company.php index 1a0680363a9..9d7d02882d3 100644 --- a/htdocs/admin/company.php +++ b/htdocs/admin/company.php @@ -5,6 +5,7 @@ * Copyright (C) 2010-2014 Juanjo Menent * Copyright (C) 2011-2017 Philippe Grand * Copyright (C) 2015 Alexandre Spangaro + * Copyright (C) 2017 Rui Strecht * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -78,6 +79,7 @@ if ( ($action == 'update' && ! GETPOST("cancel",'alpha')) 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_REGION", GETPOST("region_code",'alpha'),'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); @@ -726,8 +728,9 @@ else print ''; - print ''.$langs->trans("State").''; - if (! empty($conf->global->MAIN_INFO_SOCIETE_STATE)) print getState($conf->global->MAIN_INFO_SOCIETE_STATE); + if (! empty($conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT)) print ''.$langs->trans("Region-State").''; + else print ''.$langs->trans("State").''; + if (! empty($conf->global->MAIN_INFO_SOCIETE_STATE)) print getState($conf->global->MAIN_INFO_SOCIETE_STATE,$conf->global->MAIN_SHOW_STATE_CODE,0,$conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT); else print ' '; print ''; diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 4600cefbdc3..fdc33294f84 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -13,6 +13,7 @@ * Copyright (C) 2016 Bahfir abbes * Copyright (C) 2017 ATM Consulting * Copyright (C) 2017 Nicolas ZABOURI + * Copyright (C) 2017 Rui Strecht * Copyright (C) 2018 Frederic France * * This program is free software; you can redistribute it and/or modify @@ -197,7 +198,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 @@ -422,9 +448,10 @@ abstract class CommonObject * * @param int $withcountry 1=Add country into address string * @param string $sep Separator to use to build string + * @param int $withregion 1=Add region into address 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))) { @@ -434,6 +461,16 @@ abstract class CommonObject $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); } @@ -471,7 +508,7 @@ abstract class CommonObject $out=''; $outdone=0; - $coords = $this->getFullAddress(1,', '); + $coords = $this->getFullAddress(1,', ',$conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT); if ($coords) { if (! empty($conf->use_javascript_ajax)) @@ -489,7 +526,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_SELECT) && $conf->global->MAIN_SHOW_REGION_IN_STATE_SELECT == 1 && $this->region) { + $out.=($outdone?' - ':'').$this->region.' - '.$this->state; + } + else { + $out.=($outdone?' - ':'').$this->state; + } $outdone++; } diff --git a/htdocs/core/class/html.formcompany.class.php b/htdocs/core/class/html.formcompany.class.php index faa548a5d16..d9bd4325e24 100644 --- a/htdocs/core/class/html.formcompany.class.php +++ b/htdocs/core/class/html.formcompany.class.php @@ -265,16 +265,26 @@ class FormCompany { $out.= ''; } $i++; diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 3a295f7a941..6e056971c0d 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -9,6 +9,7 @@ * Copyright (C) 2013 Alexandre Spangaro * Copyright (C) 2015 Frederic France * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2017 Rui Strecht * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -473,22 +474,29 @@ function getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entc /** * Return state translated from an id. Return value is always utf8 encoded and without entities. * - * @param int $id id of state (province/departement) + * @param int $id id of state (province/departement) * @param int $withcode '0'=Return label, * '1'=Return string code + label, * '2'=Return code, * 'all'=return array('id'=>,'code'=>,'label'=>) * @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) + * @param int $withregion '0'=Ignores region, + * '1'=Add region name/code/id as needed to output, + * @param Translate $outputlangs Langs object for output translation, not fully implemented yet + * @param int $entconv 0=Return value without entities and not converted to output charset, 1=Ready for html output + * @return mixed String with state code or state name or Array('id','code','label')/Array('id','code','label','region_code','region') */ -function getState($id,$withcode='',$dbtouse=0) +function getState($id,$withcode='',$dbtouse=0,$withregion=0,$outputlangs='',$entconv=1) { global $db,$langs; if (! is_object($dbtouse)) $dbtouse=$db; - $sql = "SELECT rowid, code_departement as code, nom as label FROM ".MAIN_DB_PREFIX."c_departements"; - $sql.= " WHERE rowid=".$id; + $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"; + $sql .= " ORDER BY c.code, d.code_departement"; dol_syslog("Company.lib::getState", LOG_DEBUG); $resql=$dbtouse->query($sql); @@ -497,11 +505,46 @@ function getState($id,$withcode='',$dbtouse=0) $obj = $dbtouse->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 if ($withcode == 'all') return array('id'=>$obj->rowid,'code'=>$obj->code,'label'=>$label); - else return $label; + $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 ($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 { + if ($withregion == 1) { + return $label = $obj->region_name . ' - ' . $label; + } + else { + return $label; + } + } } else { diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index dbf3015ba1e..d5591f47037 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -12,6 +12,7 @@ * Copyright (C) 2013 Peter Fontaine * Copyright (C) 2014-2015 Marcos García * Copyright (C) 2015 Raphaël Doursenaud + * Copyright (C) 2017 Rui Strecht * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -116,6 +117,13 @@ class Societe extends CommonObject var $state_code; var $state; + /** + * Id of region + * @var int + */ + var $region_code; + var $region; + /** * State code * @var string @@ -3243,15 +3251,7 @@ class Societe extends CommonObject $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); - } - */ + $this->region_code=empty($conf->global->MAIN_INFO_SOCIETE_REGION)?'':$conf->global->MAIN_INFO_SOCIETE_REGION; $this->note_private=empty($conf->global->MAIN_INFO_SOCIETE_NOTE)?'':$conf->global->MAIN_INFO_SOCIETE_NOTE;