19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/compta/localtax/class/localtax.class.php';
35 public static $FIELDS = array(
61 public function get(
$id)
63 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
64 throw new RestException(403);
68 $result = $localtax->fetch(
$id);
71 if ($result <= 0 || empty($localtax->id)) {
72 throw new RestException(404,
'Local tax not found');
96 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
98 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'lire')) {
99 throw new RestException(403);
104 $sql =
"SELECT t.rowid";
105 $sql .=
" FROM ".MAIN_DB_PREFIX.
"localtax AS t";
106 $sql .=
" WHERE t.entity IN (".getEntity(
'localtax').
")";
113 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
118 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
120 $sql .= $this->db->order($sortfield, $sortorder);
125 $offset = $limit * $page;
127 $sql .= $this->db->plimit($limit + 1, $offset);
131 $result = $this->db->query($sql);
134 throw new RestException(503,
'Error when retrieving list of local taxes: '.$this->db->lasterror());
137 $num = $this->db->num_rows($result);
138 $min = min($num, ($limit <= 0 ? $num : $limit));
139 for ($i = 0; $i < $min; $i++) {
140 $obj = $this->db->fetch_object($result);
141 $localtax =
new Localtax($this->db);
142 if ($localtax->fetch($obj->rowid) > 0) {
149 if ($pagination_data) {
150 $totalsResult = $this->db->query($sqlTotals);
151 $total = $this->db->fetch_object($totalsResult)->total;
156 $obj_ret[
'data'] = $tmp;
157 $obj_ret[
'pagination'] = array(
158 'total' => (
int) $total,
160 'page_count' => ($limit > 0 ? (
int) ceil((
int) $total / $limit) : 1),
180 public function post($request_data =
null)
182 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
183 throw new RestException(403);
189 $localtax =
new Localtax($this->db);
190 foreach ($request_data as $field => $value) {
191 if ($field ===
'caller') {
193 $localtax->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
197 if ($field ==
'localtaxtype') {
198 $localtax->ltt = (int) $value;
205 $localtax->fk_user_creat = DolibarrApiAccess::$user->id;
206 if (empty($localtax->tms)) {
210 if ($localtax->create(DolibarrApiAccess::$user) < 0) {
211 throw new RestException(500,
'Error when creating local tax: '.$localtax->errorsToString());
214 return $localtax->id;
230 public function put(
$id, $request_data =
null)
232 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'creer')) {
233 throw new RestException(403);
236 $localtax =
new Localtax($this->db);
237 $result = $localtax->fetch(
$id);
240 if ($result <= 0 || empty($localtax->id)) {
241 throw new RestException(404,
'Local tax not found');
244 foreach ($request_data as $field => $value) {
245 if ($field ==
'id') {
248 if ($field ===
'caller') {
250 $localtax->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
254 if ($field ==
'localtaxtype') {
255 $localtax->ltt = (int) $value;
262 $localtax->fk_user_modif = DolibarrApiAccess::$user->id;
264 if ($localtax->update(DolibarrApiAccess::$user) > 0) {
265 return $this->
get(
$id);
267 throw new RestException(500,
'Error when updating local tax: '.$localtax->errorsToString());
283 public function delete(
$id)
285 if (!DolibarrApiAccess::$user->hasRight(
'tax',
'charges',
'supprimer')) {
286 throw new RestException(403);
289 $localtax =
new Localtax($this->db);
290 $result = $localtax->fetch(
$id);
293 if ($result <= 0 || empty($localtax->id)) {
294 throw new RestException(404,
'Local tax not found');
297 if ($localtax->delete(DolibarrApiAccess::$user) < 0) {
298 throw new RestException(500,
'Error when deleting local tax: '.$localtax->errorsToString());
304 'message' =>
'Local tax deleted'
319 if ($data ===
null) {
323 foreach (LocalTaxes::$FIELDS as $field) {
324 if (!isset($data[$field])) {
325 throw new RestException(400,
"$field field missing");
327 $localtax[$field] = $data[$field];
346 unset(
$object->labelStatusShort);
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_validate($data)
Validate fields before creating an object.
put($id, $request_data=null)
Update a local tax.
__construct()
Constructor.
_cleanObjectDatas($object)
Clean sensitive object data.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List local taxes (RE / IRPF).
post($request_data=null)
Create a local tax.
Class to manage local tax.
dol_now($mode='gmt')
Return date for now.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.