dolibarr 25.0.0-alpha
api_localtaxes.class.php
1<?php
2/*
3 * Copyright (C) 2026 Xabier Trigo Losada <xabi@galicloud.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
22
31{
35 public static $FIELDS = array(
36 'localtaxtype',
37 'datep',
38 'datev',
39 'amount',
40 'label',
41 );
42
46 public function __construct()
47 {
48 global $db;
49 $this->db = $db;
50 }
51
61 public function get($id)
62 {
63 if (!DolibarrApiAccess::$user->hasRight('tax', 'charges', 'lire')) {
64 throw new RestException(403);
65 }
66
67 $localtax = new Localtax($this->db);
68 $result = $localtax->fetch($id);
69 // Localtax::fetch() returns 1 even when the record does not exist,
70 // so also check that the id was actually loaded.
71 if ($result <= 0 || empty($localtax->id)) {
72 throw new RestException(404, 'Local tax not found');
73 }
74
75 return $this->_cleanObjectDatas($localtax);
76 }
77
96 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
97 {
98 if (!DolibarrApiAccess::$user->hasRight('tax', 'charges', 'lire')) {
99 throw new RestException(403);
100 }
101
102 $obj_ret = array();
103
104 $sql = "SELECT t.rowid";
105 $sql .= " FROM ".MAIN_DB_PREFIX."localtax AS t";
106 $sql .= " WHERE t.entity IN (".getEntity('localtax').")";
107
108 // Add sql filters
109 if ($sqlfilters) {
110 $errormessage = '';
111 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
112 if ($errormessage) {
113 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
114 }
115 }
116
117 // This query will count total records matching the filters (before pagination)
118 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
119
120 $sql .= $this->db->order($sortfield, $sortorder);
121 if ($limit) {
122 if ($page < 0) {
123 $page = 0;
124 }
125 $offset = $limit * $page;
126
127 $sql .= $this->db->plimit($limit + 1, $offset);
128 }
129
130 dol_syslog("API Rest request");
131 $result = $this->db->query($sql);
132
133 if (!$result) {
134 throw new RestException(503, 'Error when retrieving list of local taxes: '.$this->db->lasterror());
135 }
136
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) {
143 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($localtax), $properties);
144 }
145 }
146
147 // If $pagination_data is true, the response will contain the element data with all values
148 // and the element pagination with pagination data (total, page, page_count, limit)
149 if ($pagination_data) {
150 $totalsResult = $this->db->query($sqlTotals);
151 $total = $this->db->fetch_object($totalsResult)->total;
152
153 $tmp = $obj_ret;
154 $obj_ret = array();
155
156 $obj_ret['data'] = $tmp;
157 $obj_ret['pagination'] = array(
158 'total' => (int) $total,
159 'page' => $page, // count starts from 0
160 'page_count' => ($limit > 0 ? (int) ceil((int) $total / $limit) : 1),
161 'limit' => $limit
162 );
163 }
164
165 return $obj_ret;
166 }
167
180 public function post($request_data = null)
181 {
182 if (!DolibarrApiAccess::$user->hasRight('tax', 'charges', 'creer')) {
183 throw new RestException(403);
184 }
185
186 // Check mandatory fields
187 $this->_validate($request_data);
188
189 $localtax = new Localtax($this->db);
190 foreach ($request_data as $field => $value) {
191 if ($field === 'caller') {
192 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
193 $localtax->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
194 continue;
195 }
196 // The business class stores the local tax type in property "ltt" (int)
197 if ($field == 'localtaxtype') {
198 $localtax->ltt = (int) $value;
199 continue;
200 }
201
202 $localtax->$field = $this->_checkValForAPI($field, $value, $localtax);
203 }
204
205 $localtax->fk_user_creat = DolibarrApiAccess::$user->id;
206 if (empty($localtax->tms)) {
207 $localtax->tms = dol_now();
208 }
209
210 if ($localtax->create(DolibarrApiAccess::$user) < 0) {
211 throw new RestException(500, 'Error when creating local tax: '.$localtax->errorsToString());
212 }
213
214 return $localtax->id;
215 }
216
230 public function put($id, $request_data = null)
231 {
232 if (!DolibarrApiAccess::$user->hasRight('tax', 'charges', 'creer')) {
233 throw new RestException(403);
234 }
235
236 $localtax = new Localtax($this->db);
237 $result = $localtax->fetch($id);
238 // Localtax::fetch() returns 1 even when the record does not exist,
239 // so also check that the id was actually loaded.
240 if ($result <= 0 || empty($localtax->id)) {
241 throw new RestException(404, 'Local tax not found');
242 }
243
244 foreach ($request_data as $field => $value) {
245 if ($field == 'id') {
246 continue;
247 }
248 if ($field === 'caller') {
249 // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
250 $localtax->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
251 continue;
252 }
253 // The business class stores the local tax type in property "ltt" (int)
254 if ($field == 'localtaxtype') {
255 $localtax->ltt = (int) $value;
256 continue;
257 }
258
259 $localtax->$field = $this->_checkValForAPI($field, $value, $localtax);
260 }
261
262 $localtax->fk_user_modif = DolibarrApiAccess::$user->id;
263
264 if ($localtax->update(DolibarrApiAccess::$user) > 0) {
265 return $this->get($id);
266 } else {
267 throw new RestException(500, 'Error when updating local tax: '.$localtax->errorsToString());
268 }
269 }
270
283 public function delete($id)
284 {
285 if (!DolibarrApiAccess::$user->hasRight('tax', 'charges', 'supprimer')) {
286 throw new RestException(403);
287 }
288
289 $localtax = new Localtax($this->db);
290 $result = $localtax->fetch($id);
291 // Localtax::fetch() returns 1 even when the record does not exist,
292 // so also check that the id was actually loaded.
293 if ($result <= 0 || empty($localtax->id)) {
294 throw new RestException(404, 'Local tax not found');
295 }
296
297 if ($localtax->delete(DolibarrApiAccess::$user) < 0) {
298 throw new RestException(500, 'Error when deleting local tax: '.$localtax->errorsToString());
299 }
300
301 return array(
302 'success' => array(
303 'code' => 200,
304 'message' => 'Local tax deleted'
305 )
306 );
307 }
308
317 private function _validate($data)
318 {
319 if ($data === null) {
320 $data = array();
321 }
322 $localtax = array();
323 foreach (LocalTaxes::$FIELDS as $field) {
324 if (!isset($data[$field])) {
325 throw new RestException(400, "$field field missing");
326 }
327 $localtax[$field] = $data[$field];
328 }
329 return $localtax;
330 }
331
332 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
339 protected function _cleanObjectDatas($object)
340 {
341 // phpcs:enable
342 $object = parent::_cleanObjectDatas($object);
343
344 unset($object->statut);
345 unset($object->labelStatus);
346 unset($object->labelStatusShort);
347
348 return $object;
349 }
350}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:35
_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.