From 8a9c21c642b62d74d5d80645a4d7edd904a32856 Mon Sep 17 00:00:00 2001 From: Sylvain Legrand Date: Wed, 19 May 2021 18:47:07 +0200 Subject: [PATCH 1/7] Update societe.class.php --- htdocs/societe/class/societe.class.php | 61 ++++++++++++++++++++------ 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 011dd3dcce5..3c7d642c23f 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3112,20 +3112,55 @@ 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; - return 1; - } else { + // InfraS change (to avoid infinite loop) + $sameparent = $this->get_parents($id, $this->id); + if ($sameparent < 0) return -1; + elseif ($sameparent == 1) + { + setEventMessages('la maison mère choisie est déjà filiale de ce tiers', null, 'warnings'); return -1; - } - } else { - return -1; - } + } // elseif ($sameparent == 1) + else + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.$this->id; + dol_syslog(get_class($this).'::set_parent', LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) + { + $this->parent = $id; + return 1; + } // if ($resql) + else return -1; + } // else // elseif ($sameparent == 1) + } // if ($this->id) + else return -1; + } + + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps + /** + * Search parent commany of current company + * + * @param int $idparent Id of thirdparty to check + * @param int $idchild Id of thirdparty to compare to + * @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 get_parents($idparent, $idchild) + { + // phpcs:enable + $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 $sameparent = $this->get_parents($obj->parent, $idchild); + return $sameparent; + } // if ($resql) + else return -1; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps From b5ac78d9a0532eac0961437adaf0ae05dc5a820f Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Wed, 19 May 2021 16:56:52 +0000 Subject: [PATCH 2/7] Fixing style errors. --- htdocs/societe/class/societe.class.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 3c7d642c23f..a1950bd5214 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3120,8 +3120,7 @@ class Societe extends CommonObject setEventMessages('la maison mère choisie est déjà filiale de ce tiers', null, 'warnings'); return -1; } // elseif ($sameparent == 1) - else - { + else { $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.$this->id; dol_syslog(get_class($this).'::set_parent', LOG_DEBUG); $resql = $this->db->query($sql); @@ -3130,7 +3129,7 @@ class Societe extends CommonObject $this->parent = $id; return 1; } // if ($resql) - else return -1; + else return -1; } // else // elseif ($sameparent == 1) } // if ($this->id) else return -1; @@ -3157,10 +3156,10 @@ class Societe extends CommonObject if ($obj->parent == '') return 0; elseif ($obj->parent == $idchild) return 1; - else $sameparent = $this->get_parents($obj->parent, $idchild); + else $sameparent = $this->get_parents($obj->parent, $idchild); return $sameparent; } // if ($resql) - else return -1; + else return -1; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps From a3d1b49c6588da6fef07b4c6da55d5c654350123 Mon Sep 17 00:00:00 2001 From: Sylvain Legrand Date: Wed, 19 May 2021 19:08:58 +0200 Subject: [PATCH 3/7] Error in list when using alias name of thirdparty --- htdocs/comm/propal/list.php | 3 ++- htdocs/commande/list.php | 3 ++- htdocs/compta/facture/list.php | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/htdocs/comm/propal/list.php b/htdocs/comm/propal/list.php index fd6df13ae9e..f867f94a2d0 100644 --- a/htdocs/comm/propal/list.php +++ b/htdocs/comm/propal/list.php @@ -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; diff --git a/htdocs/commande/list.php b/htdocs/commande/list.php index 9b3e865e459..1979d057f89 100644 --- a/htdocs/commande/list.php +++ b/htdocs/commande/list.php @@ -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])) { diff --git a/htdocs/compta/facture/list.php b/htdocs/compta/facture/list.php index 6d4af64902d..57082ce5eee 100644 --- a/htdocs/compta/facture/list.php +++ b/htdocs/compta/facture/list.php @@ -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; From 890d0f8040e648d6ff4542b6580efdaccc155869 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 20 May 2021 14:38:24 +0200 Subject: [PATCH 4/7] Update societe.class.php --- htdocs/societe/class/societe.class.php | 46 ++++++++++++++------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index a1950bd5214..39524d18e63 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3113,26 +3113,26 @@ class Societe extends CommonObject // phpcs:enable if ($this->id) { // InfraS change (to avoid infinite loop) - $sameparent = $this->get_parents($id, $this->id); - if ($sameparent < 0) return -1; - elseif ($sameparent == 1) - { - setEventMessages('la maison mère choisie est déjà filiale de ce tiers', null, 'warnings'); + $sameparent = $this->get_parents($id, $this->id); + if ($sameparent < 0) { return -1; - } // elseif ($sameparent == 1) - else { + } elseif ($sameparent == 1) { + setEventMessages('ParentCompanyIsAlreadyAChild', null, 'warnings'); + return -1; + } else { $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.$this->id; dol_syslog(get_class($this).'::set_parent', LOG_DEBUG); $resql = $this->db->query($sql); - if ($resql) - { + if ($resql) { $this->parent = $id; return 1; - } // if ($resql) - else return -1; - } // else // elseif ($sameparent == 1) - } // if ($this->id) - else return -1; + } else { + return -1; + } + } + } else { + return -1; + } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps @@ -3150,16 +3150,20 @@ class Societe extends CommonObject $sql .= ' FROM '.MAIN_DB_PREFIX.'societe as s'; $sql .= ' WHERE rowid = '.$idparent; $resql = $this->db->query($sql); - if ($resql) - { + if ($resql) { $obj = $this->db->fetch_object($resql); - if ($obj->parent == '') return 0; - elseif ($obj->parent == $idchild) return 1; - else $sameparent = $this->get_parents($obj->parent, $idchild); + if ($obj->parent == '') { + return 0; + } elseif ($obj->parent == $idchild) { + return 1; + } else { + $sameparent = $this->get_parents($obj->parent, $idchild); + } return $sameparent; - } // if ($resql) - else return -1; + } else { + return -1; + } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps From b64c3073586199e378d4ada177c92aae8dfaf58e Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 20 May 2021 14:50:02 +0200 Subject: [PATCH 5/7] Update societe.class.php --- htdocs/societe/class/societe.class.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 39524d18e63..d7aa01e20cc 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3113,7 +3113,7 @@ class Societe extends CommonObject // phpcs:enable if ($this->id) { // InfraS change (to avoid infinite loop) - $sameparent = $this->get_parents($id, $this->id); + $sameparent = $this->validateFamilyTree($id, $this->id, 0); if ($sameparent < 0) { return -1; } elseif ($sameparent == 1) { @@ -3135,17 +3135,20 @@ class Societe extends CommonObject } } - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** - * Search parent commany of current company + * 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 - * @return int <0 if KO, 0 if OK or 1 if at some level a parent company was the child 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 get_parents($idparent, $idchild) + public function validateFamilyTree($idparent, $idchild, $counter = 0) { - // phpcs:enable + 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; @@ -3158,7 +3161,7 @@ class Societe extends CommonObject } elseif ($obj->parent == $idchild) { return 1; } else { - $sameparent = $this->get_parents($obj->parent, $idchild); + $sameparent = $this->validateFamilyTree($obj->parent, $idchild, ($counter + 1)); } return $sameparent; } else { From 94ca682cbf2c1473c1197f8cfb2280f3bacdc8d3 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 20 May 2021 12:51:53 +0000 Subject: [PATCH 6/7] Fixing style errors. --- htdocs/societe/class/societe.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index d7aa01e20cc..0310b9da79c 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3148,7 +3148,7 @@ class Societe extends CommonObject 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; From 28487fac12585640eae947b3378059993a89e740 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 20 May 2021 14:53:50 +0200 Subject: [PATCH 7/7] Create societe.class.php --- htdocs/societe/class/societe.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 0310b9da79c..e85f3a6c5ee 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3112,15 +3112,15 @@ class Societe extends CommonObject { // phpcs:enable if ($this->id) { - // InfraS change (to avoid infinite loop) + // 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('ParentCompanyIsAlreadyAChild', null, 'warnings'); + setEventMessages('ParentCompanyToAddIsAlreadyAChildOfModifiedCompany', null, 'warnings'); return -1; } else { - $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe SET parent = '.($id > 0 ? $id : 'null').' WHERE rowid = '.$this->id; + $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) { @@ -3148,7 +3148,7 @@ class Societe extends CommonObject 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;