From c3f8a2b2e66537b0e11bd15a536411a73d97b93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charl=C3=A8ne=20Benke?= <1179011+defrance@users.noreply.github.com> Date: Thu, 6 Nov 2025 02:11:37 +0100 Subject: [PATCH] NEW add api List VAT (#35920) * NEW add api List VAT Enhance getListOfVAT method with SQL filters and pagination. * Update parameter type for fk_country in docblock * Ensure integer casting for SQL parameters Cast variables to integers for SQL query safety. --------- Co-authored-by: Laurent Destailleur --- htdocs/api/class/api_setup.class.php | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/htdocs/api/class/api_setup.class.php b/htdocs/api/class/api_setup.class.php index e0fe2c9c01f..b137148c319 100644 --- a/htdocs/api/class/api_setup.class.php +++ b/htdocs/api/class/api_setup.class.php @@ -2395,6 +2395,78 @@ class Setup extends DolibarrApi return $list; } + /** + * Get the list of vat. + * + * @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 Vat is active or not (-1 all, 0 = inactive, 1 = active) + * @param int $fk_country Country of vat (if -1 we use company country, 0 = all) + * @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 incoterm types + * @phan-return array + * @phpstan-return array + * + * @url GET dictionary/vat + * + * @throws RestException 503 Error when retrieving list of vatcode types + */ + public function getListOfVAT($sortfield = "taux", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $fk_country = -1, $sqlfilters = '') + { + $list = array(); + global $mysoc; + + $sql = "SELECT rowid, code, type_vat, active, fk_pays, taux, localtax1, localtax2, localtax1_type, localtax2_type, note"; + $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t"; + $sql .= " WHERE 1=1"; + + // Add sql filters + if ($sqlfilters) { + $errormessage = ''; + if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) { + throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage); + } + $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)'; + $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + if ($active != -1) + $sql .= " AND active = ".((int) $active); + + if ($fk_country == -1) + $sql .= " AND fk_pays =".((int) $mysoc->country_id); + + if ($fk_country >0) + $sql .= " AND fk_pays =".((int) $fk_country); + + $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); + $list[] = $type; + } + } else { + throw new RestException(503, 'Error when retrieving list of vat types : '.$this->db->lasterror()); + } + + return $list; + } + /** * Get properties of company *