dolibarr 20.0.5
api_multicurrencies.class.php
1<?php
2/* Copyright (C) 2022 J-F Bouculat <jfbouculat@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20//require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
21require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php';
22
30{
34 public function __construct()
35 {
36 global $db;
37
38 $this->db = $db;
39 }
40
56 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
57 {
58 global $db;
59
60 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'read')) {
61 throw new RestException(403, "Insufficient rights to read currency");
62 }
63
64 $obj_ret = array();
65
66 $sql = "SELECT t.rowid";
67 $sql .= " FROM ".$this->db->prefix()."multicurrency as t";
68 $sql .= ' WHERE 1 = 1';
69 // Add sql filters
70 if ($sqlfilters) {
71 $errormessage = '';
72 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
73 if ($errormessage) {
74 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
75 }
76 }
77
78 $sql .= $this->db->order($sortfield, $sortorder);
79 if ($limit) {
80 if ($page < 0) {
81 $page = 0;
82 }
83 $offset = $limit * $page;
84
85 $sql .= $this->db->plimit($limit + 1, $offset);
86 }
87
88 $result = $this->db->query($sql);
89 if ($result) {
90 $i = 0;
91 $num = $this->db->num_rows($result);
92 $min = min($num, ($limit <= 0 ? $num : $limit));
93 while ($i < $min) {
94 $obj = $this->db->fetch_object($result);
95 $multicurrency_static = new MultiCurrency($this->db);
96 if ($multicurrency_static->fetch($obj->rowid)) {
97 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($multicurrency_static), $properties);
98 }
99 $i++;
100 }
101 } else {
102 throw new RestException(503, 'Error when retrieve currencies list : '.$this->db->lasterror());
103 }
104
105 return $obj_ret;
106 }
107
118 public function get($id)
119 {
120 $multicurrency = new MultiCurrency($this->db);
121 if (!$multicurrency->fetch($id)) {
122 throw new RestException(404, 'Currency not found');
123 }
124
125 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'read')) {
126 throw new RestException(403, "Insufficient rights to read currency");
127 }
128
129 return $this->_cleanObjectDatas($multicurrency);
130 }
131
143 public function getByCode($code)
144 {
145 $multicurrency = new MultiCurrency($this->db);
146 if (!$multicurrency->fetch('', $code)) {
147 throw new RestException(404, 'Currency not found');
148 }
149
150 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'read')) {
151 throw new RestException(403, "Insufficient rights to read currency");
152 }
153
154 return $this->_cleanObjectDatas($multicurrency);
155 }
156
168 public function getRates($id)
169 {
170 $multicurrency = new MultiCurrency($this->db);
171 if (!$multicurrency->fetch($id)) {
172 throw new RestException(404, 'Currency not found');
173 }
174
175 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'read')) {
176 throw new RestException(403, "Insufficient rights to read currency rates");
177 }
178
179 if ($multicurrency->fetchAllCurrencyRate() < 0) {
180 throw new RestException(500, "Error when fetching currency rates");
181 }
182
183 // Clean object datas
184 foreach ($multicurrency->rates as $key => $obj) {
185 $multicurrency->rates[$key] = $this->_cleanObjectDatasRate($obj);
186 }
187
188 return $multicurrency->rates;
189 }
190
199 public function post($request_data = null)
200 {
201
202 // Check parameters
203 if (!isset($request_data['code'])) {
204 throw new RestException(400, "code field missing");
205 }
206 if (!isset($request_data['name'])) {
207 throw new RestException(400, "name field missing");
208 }
209
210 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'write')) {
211 throw new RestException(403, "Insufficient rights to create currency");
212 }
213
214 $multicurrency = new MultiCurrency($this->db);
215
216 foreach ($request_data as $field => $value) {
217 if ($field === 'caller') {
218 // 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
219 $multicurrency->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
220 continue;
221 }
222
223 $multicurrency->$field = $this->_checkValForAPI($field, $value, $multicurrency);
224 }
225
226 // Create Currency
227 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
228 throw new RestException(500, "Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
229 }
230
231 // Add default rate if defined
232 if (isset($request_data['rate']) && $request_data['rate'] > 0) {
233 if ($multicurrency->addRate($request_data['rate']) < 0) {
234 throw new RestException(500, "Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
235 }
236
237 return $multicurrency->id;
238 }
239
240 return $multicurrency->id;
241 }
242
252 public function put($id, $request_data = null)
253 {
254 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'write')) {
255 throw new RestException(403, "Insufficient rights to update currency");
256 }
257
258 $multicurrency = new MultiCurrency($this->db);
259 if (!$multicurrency->fetch($id)) {
260 throw new RestException(404, 'Currency not found');
261 }
262
263 foreach ($request_data as $field => $value) {
264 if ($field == 'id') {
265 continue;
266 }
267 if ($field === 'caller') {
268 // 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
269 $multicurrency->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
270 continue;
271 }
272
273 $multicurrency->$field = $this->_checkValForAPI($field, $value, $multicurrency);
274 }
275
276 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
277 throw new RestException(500, "Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
278 }
279
280 return $this->get($id);
281 }
282
291 public function delete($id)
292 {
293 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'delete')) {
294 throw new RestException(403, "Insufficient rights to delete currency");
295 }
296
297 $multicurrency = new MultiCurrency($this->db);
298 if (!$multicurrency->fetch($id)) {
299 throw new RestException(404, 'Currency not found');
300 }
301
302 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
303 throw new RestException(500, "Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
304 }
305
306 return array(
307 'success' => array(
308 'code' => 200,
309 'message' => 'Currency deleted'
310 )
311 );
312 }
313
314
325 public function updateRate($id, $request_data = null)
326 {
327 if (!DolibarrApiAccess::$user->hasRight('multicurrency', 'currency', 'write')) {
328 throw new RestException(403, "Insufficient rights to update currency rate");
329 }
330
331 // Check parameters
332 if (!isset($request_data['rate'])) {
333 throw new RestException(400, "Rate field is missing");
334 }
335
336 $multicurrency = new MultiCurrency($this->db);
337 if (!$multicurrency->fetch($id)) {
338 throw new RestException(404, 'Currency not found');
339 }
340
341 // Add rate
342 if ($multicurrency->addRate($request_data['rate']) < 0) {
343 throw new RestException(500, "Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
344 }
345
346 return $this->_cleanObjectDatas($multicurrency);
347 }
348
349 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
356 protected function _cleanObjectDatas($object)
357 {
358 // phpcs:enable
359 $object = parent::_cleanObjectDatas($object);
360
361 // Clear all fields out of interest
362 foreach ($object as $key => $value) {
363 if ($key == "rate") {
364 $object->$key = $this->_cleanObjectDatasRate($object->$key);
365 }
366 if ($key == "id" || $key == "code" || $key == "rate" || $key == "name") {
367 continue;
368 }
369 unset($object->$key);
370 }
371
372 return $object;
373 }
374
375 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
382 protected function _cleanObjectDatasRate($object)
383 {
384 // phpcs:enable
385 $object = parent::_cleanObjectDatas($object);
386
387 // Clear all fields out of interest
388 foreach ($object as $key => $value) {
389 if ($key == "id" || $key == "rate" || $key == "date_sync") {
390 continue;
391 }
392 unset($object->$key);
393 }
394
395 return $object;
396 }
397}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class for API REST v1.
Definition api.class.php:31
_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.
Definition api.class.php:96
_cleanObjectDatasRate($object)
Clean sensible MultiCurrencyRate object datas.
getRates($id)
List Currency rates.
put($id, $request_data=null)
Update Currency.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List Currencies.
updateRate($id, $request_data=null)
Update Currency rate @url PUT {id}/rates.
getByCode($code)
Get properties of a Currency object by code.
_cleanObjectDatas($object)
Clean sensible object datas.
post($request_data=null)
Create Currency object.
Class Currency.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.