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 <eldy@destailleur.fr>
This commit is contained in:
Charlène Benke
2025-11-06 02:11:37 +01:00
committed by GitHub
parent 933eeb7474
commit c3f8a2b2e6

View File

@@ -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<Object|false>
* @phpstan-return array<Object|false>
*
* @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
*