diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index 0df8e50efe2..1e84d558c81 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -2260,6 +2260,71 @@ class Setup extends DolibarrApi return $list; } + /** + * Get the list of thirdparties types. + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Number of items per page + * @param int $page Page number (starting from zero) + * @param int $active Type is active or not {@min 0} {@max 1} + * @param string $lang Code of the language the label of the type must be translated to + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)" + * @return array List of thirdparties types + * @phan-return array + * @phpstan-return array + * + * @url GET dictionary/thirdparty_types + * + * @throws RestException 400 Bad value for sqlfilters + * @throws RestException 503 Error when retrieving list of thirdparties types + */ + public function getThirdpartiesTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $lang = '', $sqlfilters = '') + { + $list = array(); + + $sql = "SELECT id, code, libelle as label, fk_country, module, position"; + $sql .= " FROM ".MAIN_DB_PREFIX."c_typent as t"; + $sql .= " WHERE t.active = ".((int) $active); + + // Add sql filters + if ($sqlfilters) { + $errormessage = ''; + $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage); + if ($errormessage) { + throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); + } + } + + + $sql .= $this->db->order($sortfield, $sortorder); + + if ($limit) { + if ($page < 0) { + $page = 0; + } + $offset = $limit * $page; + + $sql .= $this->db->plimit($limit, $offset); + } + + $result = $this->db->query($sql); + + if ($result) { + $num = $this->db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + for ($i = 0; $i < $min; $i++) { + $type = $this->db->fetch_object($result); + $this->translateLabel($type, $lang, '', array('companies')); + $list[] = $type; + } + } else { + throw new RestException(503, 'Error when retrieving list of thirdparty types : '.$this->db->lasterror()); + } + + return $list; + } + /** * Get the list of incoterms. *