18use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/core/lib/multicurrency.lib.php';
54 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $sqlfilters =
'')
60 $sql =
"SELECT t.rowid";
61 $sql .=
" FROM ".$this->db->prefix().
"multicurrency as t";
62 $sql .=
' WHERE 1 = 1';
67 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
69 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
70 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
73 $sql .= $this->db->order($sortfield, $sortorder);
78 $offset = $limit * $page;
80 $sql .= $this->db->plimit($limit + 1, $offset);
83 $result = $this->db->query($sql);
86 $num = $this->db->num_rows($result);
87 $min = min($num, ($limit <= 0 ? $num : $limit));
89 $obj = $this->db->fetch_object($result);
91 if ($multicurrency_static->fetch($obj->rowid)) {
97 throw new RestException(503,
'Error when retrieve currencies list : '.$this->db->lasterror());
99 if (!count($obj_ret)) {
100 throw new RestException(404,
'No currencies found');
116 public function get($id)
119 if (!$multicurrency->fetch($id)) {
120 throw new RestException(404,
'Currency not found');
123 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
124 throw new RestException(401,
"Insufficient rights to read currency");
144 if (!$multicurrency->fetch(
'', $code)) {
145 throw new RestException(404,
'Currency not found');
148 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
149 throw new RestException(401,
"Insufficient rights to read currency");
169 if (!$multicurrency->fetch($id)) {
170 throw new RestException(404,
'Currency not found');
173 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
174 throw new RestException(401,
"Insufficient rights to read currency rates");
177 if ($multicurrency->fetchAllCurrencyRate() < 0) {
178 throw new RestException(500,
"Error when fetching currency rates");
182 foreach ($multicurrency->rates as $key => $obj) {
186 return $multicurrency->rates;
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
200 throw new RestException(401,
"Insufficient rights to create currency");
204 if (!isset($request_data[
'code'])) {
205 throw new RestException(400,
"code field missing");
207 if (!isset($request_data[
'name'])) {
208 throw new RestException(400,
"name field missing");
212 $multicurrency->code = $request_data[
'code'];
213 $multicurrency->name = $request_data[
'name'];
216 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
217 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
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));
226 return $multicurrency->id;
229 return $multicurrency->id;
241 public function put($id, $request_data =
null)
243 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
244 throw new RestException(401,
"Insufficient rights to update currency");
248 if (!$multicurrency->fetch($id)) {
249 throw new RestException(404,
'Currency not found');
252 foreach ($request_data as $field => $value) {
253 if ($field ==
'id') {
256 $multicurrency->$field = $value;
259 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
260 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
263 return $this->
get($id);
274 public function delete($id)
276 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->delete) {
277 throw new RestException(401,
"Insufficient rights to delete currency");
281 if (!$multicurrency->fetch($id)) {
282 throw new RestException(404,
'Currency not found');
285 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
286 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
292 'message' =>
'Currency deleted'
310 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
311 throw new RestException(401,
"Insufficient rights to update currency rate");
315 if (!isset($request_data[
'rate'])) {
316 throw new RestException(400,
"rate field missing");
320 if (!$multicurrency->fetch($id)) {
321 throw new RestException(404,
'Currency not found');
325 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
326 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
342 $object = parent::_cleanObjectDatas($object);
345 foreach ($object as $key => $value) {
346 if ($key ==
"rate") {
349 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
352 unset($object->$key);
368 $object = parent::_cleanObjectDatas($object);
371 foreach ($object as $key => $value) {
372 if ($key ==
"id" || $key ==
"rate" || $key ==
"date_sync")
374 unset($object->$key);
_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.
__construct()
Constructor.
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.