dolibarr 19.0.3
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->rights->multicurrency->currency->read) {
61 throw new RestException(401, "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 if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
73 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
74 }
75 $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
76 $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
77 }
78
79 $sql .= $this->db->order($sortfield, $sortorder);
80 if ($limit) {
81 if ($page < 0) {
82 $page = 0;
83 }
84 $offset = $limit * $page;
85
86 $sql .= $this->db->plimit($limit + 1, $offset);
87 }
88
89 $result = $this->db->query($sql);
90 if ($result) {
91 $i = 0;
92 $num = $this->db->num_rows($result);
93 $min = min($num, ($limit <= 0 ? $num : $limit));
94 while ($i < $min) {
95 $obj = $this->db->fetch_object($result);
96 $multicurrency_static = new MultiCurrency($this->db);
97 if ($multicurrency_static->fetch($obj->rowid)) {
98 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($multicurrency_static), $properties);
99 }
100 $i++;
101 }
102 } else {
103 throw new RestException(503, 'Error when retrieve currencies list : '.$this->db->lasterror());
104 }
105
106 return $obj_ret;
107 }
108
119 public function get($id)
120 {
121 $multicurrency = new MultiCurrency($this->db);
122 if (!$multicurrency->fetch($id)) {
123 throw new RestException(404, 'Currency not found');
124 }
125
126 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
127 throw new RestException(401, "Insufficient rights to read currency");
128 }
129
130 return $this->_cleanObjectDatas($multicurrency);
131 }
132
144 public function getByCode($code)
145 {
146 $multicurrency = new MultiCurrency($this->db);
147 if (!$multicurrency->fetch('', $code)) {
148 throw new RestException(404, 'Currency not found');
149 }
150
151 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
152 throw new RestException(401, "Insufficient rights to read currency");
153 }
154
155 return $this->_cleanObjectDatas($multicurrency);
156 }
157
169 public function getRates($id)
170 {
171 $multicurrency = new MultiCurrency($this->db);
172 if (!$multicurrency->fetch($id)) {
173 throw new RestException(404, 'Currency not found');
174 }
175
176 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
177 throw new RestException(401, "Insufficient rights to read currency rates");
178 }
179
180 if ($multicurrency->fetchAllCurrencyRate() < 0) {
181 throw new RestException(500, "Error when fetching currency rates");
182 }
183
184 // Clean object datas
185 foreach ($multicurrency->rates as $key => $obj) {
186 $multicurrency->rates[$key] = $this->_cleanObjectDatasRate($obj);
187 }
188
189 return $multicurrency->rates;
190 }
191
200 public function post($request_data = null)
201 {
202 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
203 throw new RestException(401, "Insufficient rights to create currency");
204 }
205
206 // Check parameters
207 if (!isset($request_data['code'])) {
208 throw new RestException(400, "code field missing");
209 }
210 if (!isset($request_data['name'])) {
211 throw new RestException(400, "name field missing");
212 }
213
214 $multicurrency = new MultiCurrency($this->db);
215 $multicurrency->code = $request_data['code'];
216 $multicurrency->name = $request_data['name'];
217
218 // Create Currency
219 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500, "Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
221 }
222
223 // Add default rate if defined
224 if (isset($request_data['rate']) && $request_data['rate'] > 0) {
225 if ($multicurrency->addRate($request_data['rate']) < 0) {
226 throw new RestException(500, "Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
227 }
228
229 return $multicurrency->id;
230 }
231
232 return $multicurrency->id;
233 }
234
244 public function put($id, $request_data = null)
245 {
246 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
247 throw new RestException(401, "Insufficient rights to update currency");
248 }
249
250 $multicurrency = new MultiCurrency($this->db);
251 if (!$multicurrency->fetch($id)) {
252 throw new RestException(404, 'Currency not found');
253 }
254
255 foreach ($request_data as $field => $value) {
256 if ($field == 'id') {
257 continue;
258 }
259 if ($field === 'caller') {
260 // 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 whith the caller
261 $multicurrency->context['caller'] = $request_data['caller'];
262 continue;
263 }
264
265 $multicurrency->$field = $value;
266 }
267
268 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
269 throw new RestException(500, "Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
270 }
271
272 return $this->get($id);
273 }
274
283 public function delete($id)
284 {
285 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->delete) {
286 throw new RestException(401, "Insufficient rights to delete currency");
287 }
288
289 $multicurrency = new MultiCurrency($this->db);
290 if (!$multicurrency->fetch($id)) {
291 throw new RestException(404, 'Currency not found');
292 }
293
294 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
295 throw new RestException(500, "Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
296 }
297
298 return array(
299 'success' => array(
300 'code' => 200,
301 'message' => 'Currency deleted'
302 )
303 );
304 }
305
306
317 public function updateRate($id, $request_data = null)
318 {
319 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
320 throw new RestException(401, "Insufficient rights to update currency rate");
321 }
322
323 // Check parameters
324 if (!isset($request_data['rate'])) {
325 throw new RestException(400, "rate field missing");
326 }
327
328 $multicurrency = new MultiCurrency($this->db);
329 if (!$multicurrency->fetch($id)) {
330 throw new RestException(404, 'Currency not found');
331 }
332
333 // Add rate
334 if ($multicurrency->addRate($request_data['rate']) < 0) {
335 throw new RestException(500, "Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
336 }
337
338 return $this->_cleanObjectDatas($multicurrency);
339 }
340
341 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
348 protected function _cleanObjectDatas($object)
349 {
350 // phpcs:enable
351 $object = parent::_cleanObjectDatas($object);
352
353 // Clear all fields out of interrest
354 foreach ($object as $key => $value) {
355 if ($key == "rate") {
356 $object->$key = $this->_cleanObjectDatasRate($object->$key);
357 }
358 if ($key == "id" || $key == "code" || $key == "rate" || $key == "name") {
359 continue;
360 }
361 unset($object->$key);
362 }
363
364 return $object;
365 }
366
367 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
374 protected function _cleanObjectDatasRate($object)
375 {
376 // phpcs:enable
377 $object = parent::_cleanObjectDatas($object);
378
379 // Clear all fields out of interrest
380 foreach ($object as $key => $value) {
381 if ($key == "id" || $key == "rate" || $key == "date_sync") {
382 continue;
383 }
384 unset($object->$key);
385 }
386
387 return $object;
388 }
389}
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid Function no more used.
_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.