From 4d01969a4c6bc88796b96f866c0dd79b9a58365d Mon Sep 17 00:00:00 2001 From: Thomas Negre Date: Wed, 2 Feb 2022 16:45:12 +0100 Subject: [PATCH] Societe: add method to find parent companies --- htdocs/societe/class/societe.class.php | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 89eeeaca8e5..8d0a836ae86 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -3462,6 +3462,37 @@ class Societe extends CommonObject } } + /** + * Get parents for company + * + * @param int $company_id ID of company to search parent + * @param array $parents List of companies ID found + * @return array + */ + public function getParentsForCompany($company_id, $parents = []) + { + global $langs; + + if ($company_id > 0) { + $sql = "SELECT parent FROM " . MAIN_DB_PREFIX . "societe WHERE rowid = $company_id"; + $resql = $this->db->query($sql); + if ($resql) { + if ($obj = $this->db->fetch_object($resql)) { + $parent = $obj->parent; + if ($parent > 0 && !in_array($parent, $parents)) { + $parents[] = $parent; + return $this->getParentsForCompany($parent, $parents); + } else { + return $parents; + } + } + $this->db->free($resql); + } else { + setEventMessage($langs->trans('GetCompanyParentsError', $this->db->lasterror()), 'errors'); + } + } + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Returns if a profid sould be verified to be unique