Merge pull request #17680 from SylvainLegrand/13.0
avoid an infinite loop between a parent company and a branch when updating customer rates
This commit is contained in:
commit
7a24d6399b
@ -349,7 +349,7 @@ $help_url = 'EN:Commercial_Proposals|FR:Proposition_commerciale|ES:Presupuestos'
|
||||
|
||||
$sql = 'SELECT';
|
||||
if ($sall || $search_product_category > 0 || $search_user > 0) $sql = 'SELECT DISTINCT';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.email, s.town, s.zip, s.fk_pays, s.client, s.code_client, ';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.name_alias, s.email, s.town, s.zip, s.fk_pays, s.client, s.code_client, '; // InfraS change
|
||||
$sql .= " typent.code as typent_code,";
|
||||
$sql .= " ava.rowid as availability,";
|
||||
$sql .= " state.code_departement as state_code, state.nom as state_name,";
|
||||
@ -949,6 +949,7 @@ if ($resql)
|
||||
|
||||
$companystatic->id = $obj->socid;
|
||||
$companystatic->name = $obj->name;
|
||||
$companystatic->name_alias = $obj->name_alias; // InfraS add
|
||||
$companystatic->client = $obj->client;
|
||||
$companystatic->code_client = $obj->code_client;
|
||||
$companystatic->email = $obj->email;
|
||||
|
||||
@ -270,7 +270,7 @@ $help_url = "EN:Module_Customers_Orders|FR:Module_Commandes_Clients|ES:Módulo_P
|
||||
|
||||
$sql = 'SELECT';
|
||||
if ($sall || $search_product_category > 0 || $search_user > 0) $sql = 'SELECT DISTINCT';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.email, s.town, s.zip, s.fk_pays, s.client, s.code_client,';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.name_alias, s.email, s.town, s.zip, s.fk_pays, s.client, s.code_client,'; // InfraS change
|
||||
$sql .= " typent.code as typent_code,";
|
||||
$sql .= " state.code_departement as state_code, state.nom as state_name,";
|
||||
$sql .= ' c.rowid, c.ref, c.total_ht, c.tva as total_tva, c.total_ttc, c.ref_client, c.fk_user_author,';
|
||||
@ -937,6 +937,7 @@ if ($resql)
|
||||
$companystatic->id = $obj->socid;
|
||||
$companystatic->code_client = $obj->code_client;
|
||||
$companystatic->name = $obj->name;
|
||||
$companystatic->name_alias = $obj->name_alias; // InfraS add
|
||||
$companystatic->client = $obj->client;
|
||||
$companystatic->email = $obj->email;
|
||||
if (!isset($getNomUrl_cache[$obj->socid])) {
|
||||
|
||||
@ -422,7 +422,7 @@ $sql .= ' f.datef as df, f.date_valid, f.date_lim_reglement as datelimite, f.mod
|
||||
$sql .= ' f.paye as paye, f.fk_statut, f.close_code,';
|
||||
$sql .= ' f.datec as date_creation, f.tms as date_update, f.date_closing as date_closing,';
|
||||
$sql .= ' f.retained_warranty, f.retained_warranty_date_limit, f.situation_final, f.situation_cycle_ref, f.situation_counter,';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.email, s.town, s.zip, s.fk_pays, s.client, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta as code_compta_client, s.code_compta_fournisseur,';
|
||||
$sql .= ' s.rowid as socid, s.nom as name, s.name_alias, s.email, s.town, s.zip, s.fk_pays, s.client, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta as code_compta_client, s.code_compta_fournisseur,'; // InfraS change
|
||||
$sql .= " typent.code as typent_code,";
|
||||
$sql .= " state.code_departement as state_code, state.nom as state_name,";
|
||||
$sql .= " country.code as country_code,";
|
||||
@ -1193,6 +1193,7 @@ if ($resql)
|
||||
}
|
||||
$thirdpartystatic->id = $obj->socid;
|
||||
$thirdpartystatic->name = $obj->name;
|
||||
$thirdpartystatic->name_alias = $obj->name_alias; // InfraS add
|
||||
$thirdpartystatic->client = $obj->client;
|
||||
$thirdpartystatic->fournisseur = $obj->fournisseur;
|
||||
$thirdpartystatic->code_client = $obj->code_client;
|
||||
|
||||
@ -3112,17 +3112,58 @@ class Societe extends CommonObject
|
||||
{
|
||||
// phpcs:enable
|
||||
if ($this->id) {
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."societe";
|
||||
$sql .= " SET parent = ".($id > 0 ? $id : "null");
|
||||
$sql .= " WHERE rowid = ".$this->id;
|
||||
dol_syslog(get_class($this).'::set_parent', LOG_DEBUG);
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$this->parent = $id;
|
||||
// Check if the id we want to add as parent has not already one parent that is the current id we try to update
|
||||
$sameparent = $this->validateFamilyTree($id, $this->id, 0);
|
||||
if ($sameparent < 0) {
|
||||
return -1;
|
||||
} elseif ($sameparent == 1) {
|
||||
setEventMessages('ParentCompanyToAddIsAlreadyAChildOfModifiedCompany', null, 'warnings');
|
||||
return -1;
|
||||
} else {
|
||||
$sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.((int) $this->id);
|
||||
dol_syslog(get_class($this).'::set_parent', LOG_DEBUG);
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$this->parent = $id;
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a thirdparty $idchild is or not inside the parents (or grand parents) of another thirdparty id $idparent.
|
||||
*
|
||||
* @param int $idparent Id of thirdparty to check
|
||||
* @param int $idchild Id of thirdparty to compare to
|
||||
* @param int $counter Counter to protect against infinite loops
|
||||
* @return int <0 if KO, 0 if OK or 1 if at some level a parent company was the child to compare to
|
||||
*/
|
||||
public function validateFamilyTree($idparent, $idchild, $counter = 0)
|
||||
{
|
||||
if ($counter > 100) {
|
||||
dol_syslog("Too high level of parent - child for company. May be an infinite loop ?", LOG_WARNING);
|
||||
}
|
||||
|
||||
$sql = 'SELECT s.parent';
|
||||
$sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s';
|
||||
$sql .= ' WHERE rowid = '.$idparent;
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$obj = $this->db->fetch_object($resql);
|
||||
|
||||
if ($obj->parent == '') {
|
||||
return 0;
|
||||
} elseif ($obj->parent == $idchild) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
$sameparent = $this->validateFamilyTree($obj->parent, $idchild, ($counter + 1));
|
||||
}
|
||||
return $sameparent;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user