dolibarr 18.0.6
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
54 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $sqlfilters = '')
55 {
56 global $db;
57
58 $obj_ret = array();
59
60 $sql = "SELECT t.rowid";
61 $sql .= " FROM ".$this->db->prefix()."multicurrency as t";
62 $sql .= ' WHERE 1 = 1';
63 // Add sql filters
64 if ($sqlfilters) {
65 $errormessage = '';
66 if (!DolibarrApi::_checkFilters($sqlfilters, $errormessage)) {
67 throw new RestException(503, 'Error when validating parameter sqlfilters -> '.$errormessage);
68 }
69 $regexstring = '\‍(([^:\'\‍(\‍)]+:[^:\'\‍(\‍)]+:[^\‍(\‍)]+)\‍)';
70 $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
71 }
72
73 $sql .= $this->db->order($sortfield, $sortorder);
74 if ($limit) {
75 if ($page < 0) {
76 $page = 0;
77 }
78 $offset = $limit * $page;
79
80 $sql .= $this->db->plimit($limit + 1, $offset);
81 }
82
83 $result = $this->db->query($sql);
84 if ($result) {
85 $i = 0;
86 $num = $this->db->num_rows($result);
87 $min = min($num, ($limit <= 0 ? $num : $limit));
88 while ($i < $min) {
89 $obj = $this->db->fetch_object($result);
90 $multicurrency_static = new MultiCurrency($this->db);
91 if ($multicurrency_static->fetch($obj->rowid)) {
92 $obj_ret[] = $this->_cleanObjectDatas($multicurrency_static);
93 }
94 $i++;
95 }
96 } else {
97 throw new RestException(503, 'Error when retrieve currencies list : '.$this->db->lasterror());
98 }
99 if (!count($obj_ret)) {
100 throw new RestException(404, 'No currencies found');
101 }
102
103 return $obj_ret;
104 }
105
116 public function get($id)
117 {
118 $multicurrency = new MultiCurrency($this->db);
119 if (!$multicurrency->fetch($id)) {
120 throw new RestException(404, 'Currency not found');
121 }
122
123 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
124 throw new RestException(401, "Insufficient rights to read currency");
125 }
126
127 return $this->_cleanObjectDatas($multicurrency);
128 }
129
141 public function getByCode($code)
142 {
143 $multicurrency = new MultiCurrency($this->db);
144 if (!$multicurrency->fetch('', $code)) {
145 throw new RestException(404, 'Currency not found');
146 }
147
148 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
149 throw new RestException(401, "Insufficient rights to read currency");
150 }
151
152 return $this->_cleanObjectDatas($multicurrency);
153 }
154
166 public function getRates($id)
167 {
168 $multicurrency = new MultiCurrency($this->db);
169 if (!$multicurrency->fetch($id)) {
170 throw new RestException(404, 'Currency not found');
171 }
172
173 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
174 throw new RestException(401, "Insufficient rights to read currency rates");
175 }
176
177 if ($multicurrency->fetchAllCurrencyRate() < 0) {
178 throw new RestException(500, "Error when fetching currency rates");
179 }
180
181 // Clean object datas
182 foreach ($multicurrency->rates as $key => $obj) {
183 $multicurrency->rates[$key] = $this->_cleanObjectDatasRate($obj);
184 }
185
186 return $multicurrency->rates;
187 }
188
197 public function post($request_data = null)
198 {
199 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
200 throw new RestException(401, "Insufficient rights to create currency");
201 }
202
203 // Check parameters
204 if (!isset($request_data['code'])) {
205 throw new RestException(400, "code field missing");
206 }
207 if (!isset($request_data['name'])) {
208 throw new RestException(400, "name field missing");
209 }
210
211 $multicurrency = new MultiCurrency($this->db);
212 $multicurrency->code = $request_data['code'];
213 $multicurrency->name = $request_data['name'];
214
215 // Create Currency
216 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500, "Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
218 }
219
220 // Add default rate if defined
221 if (isset($request_data['rate']) && $request_data['rate'] > 0) {
222 if ($multicurrency->addRate($request_data['rate']) < 0) {
223 throw new RestException(500, "Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
224 }
225
226 return $multicurrency->id;
227 }
228
229 return $multicurrency->id;
230 }
231
241 public function put($id, $request_data = null)
242 {
243 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
244 throw new RestException(401, "Insufficient rights to update currency");
245 }
246
247 $multicurrency = new MultiCurrency($this->db);
248 if (!$multicurrency->fetch($id)) {
249 throw new RestException(404, 'Currency not found');
250 }
251
252 foreach ($request_data as $field => $value) {
253 if ($field == 'id') {
254 continue;
255 }
256 $multicurrency->$field = $value;
257 }
258
259 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
260 throw new RestException(500, "Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
261 }
262
263 return $this->get($id);
264 }
265
274 public function delete($id)
275 {
276 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->delete) {
277 throw new RestException(401, "Insufficient rights to delete currency");
278 }
279
280 $multicurrency = new MultiCurrency($this->db);
281 if (!$multicurrency->fetch($id)) {
282 throw new RestException(404, 'Currency not found');
283 }
284
285 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
286 throw new RestException(500, "Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
287 }
288
289 return array(
290 'success' => array(
291 'code' => 200,
292 'message' => 'Currency deleted'
293 )
294 );
295 }
296
297
308 public function updateRate($id, $request_data = null)
309 {
310 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
311 throw new RestException(401, "Insufficient rights to update currency rate");
312 }
313
314 // Check parameters
315 if (!isset($request_data['rate'])) {
316 throw new RestException(400, "rate field missing");
317 }
318
319 $multicurrency = new MultiCurrency($this->db);
320 if (!$multicurrency->fetch($id)) {
321 throw new RestException(404, 'Currency not found');
322 }
323
324 // Add rate
325 if ($multicurrency->addRate($request_data['rate']) < 0) {
326 throw new RestException(500, "Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
327 }
328
329 return $this->_cleanObjectDatas($multicurrency);
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 // Clear all fields out of interrest
345 foreach ($object as $key => $value) {
346 if ($key == "rate") {
347 $object->$key = $this->_cleanObjectDatasRate($object->$key);
348 }
349 if ($key == "id" || $key == "code" || $key == "rate" || $key == "name") {
350 continue;
351 }
352 unset($object->$key);
353 }
354
355 return $object;
356 }
357
358 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
365 protected function _cleanObjectDatasRate($object)
366 {
367 // phpcs:enable
368 $object = parent::_cleanObjectDatas($object);
369
370 // Clear all fields out of interrest
371 foreach ($object as $key => $value) {
372 if ($key == "id" || $key == "rate" || $key == "date_sync")
373 continue;
374 unset($object->$key);
375 }
376
377 return $object;
378 }
379}
Class for API REST v1.
Definition api.class.php:31
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $sqlfilters='')
List Currencies.
put($id, $request_data=null)
Update Currency.
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.