Societe: add method to find parent companies

This commit is contained in:
Thomas Negre 2022-02-02 16:45:12 +01:00
parent f75049d0e8
commit 4d01969a4c

View File

@ -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