18use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/core/lib/multicurrency.lib.php';
56 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
60 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
61 throw new RestException(401,
"Insufficient rights to read currency");
66 $sql =
"SELECT t.rowid";
67 $sql .=
" FROM ".$this->db->prefix().
"multicurrency as t";
68 $sql .=
' WHERE 1 = 1';
73 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
75 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
76 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
79 $sql .= $this->db->order($sortfield, $sortorder);
84 $offset = $limit * $page;
86 $sql .= $this->db->plimit($limit + 1, $offset);
89 $result = $this->db->query($sql);
92 $num = $this->db->num_rows($result);
93 $min = min($num, ($limit <= 0 ? $num : $limit));
95 $obj = $this->db->fetch_object($result);
97 if ($multicurrency_static->fetch($obj->rowid)) {
103 throw new RestException(503,
'Error when retrieve currencies list : '.$this->db->lasterror());
119 public function get($id)
122 if (!$multicurrency->fetch($id)) {
123 throw new RestException(404,
'Currency not found');
126 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
127 throw new RestException(401,
"Insufficient rights to read currency");
147 if (!$multicurrency->fetch(
'', $code)) {
148 throw new RestException(404,
'Currency not found');
151 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
152 throw new RestException(401,
"Insufficient rights to read currency");
172 if (!$multicurrency->fetch($id)) {
173 throw new RestException(404,
'Currency not found');
176 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->read) {
177 throw new RestException(401,
"Insufficient rights to read currency rates");
180 if ($multicurrency->fetchAllCurrencyRate() < 0) {
181 throw new RestException(500,
"Error when fetching currency rates");
185 foreach ($multicurrency->rates as $key => $obj) {
189 return $multicurrency->rates;
200 public function post($request_data =
null)
202 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
203 throw new RestException(401,
"Insufficient rights to create currency");
207 if (!isset($request_data[
'code'])) {
208 throw new RestException(400,
"code field missing");
210 if (!isset($request_data[
'name'])) {
211 throw new RestException(400,
"name field missing");
215 $multicurrency->code = $request_data[
'code'];
216 $multicurrency->name = $request_data[
'name'];
219 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
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));
229 return $multicurrency->id;
232 return $multicurrency->id;
244 public function put($id, $request_data =
null)
246 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
247 throw new RestException(401,
"Insufficient rights to update currency");
251 if (!$multicurrency->fetch($id)) {
252 throw new RestException(404,
'Currency not found');
255 foreach ($request_data as $field => $value) {
256 if ($field ==
'id') {
259 if ($field ===
'caller') {
261 $multicurrency->context[
'caller'] = $request_data[
'caller'];
265 $multicurrency->$field = $value;
268 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
269 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
272 return $this->
get($id);
283 public function delete($id)
285 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->delete) {
286 throw new RestException(401,
"Insufficient rights to delete currency");
290 if (!$multicurrency->fetch($id)) {
291 throw new RestException(404,
'Currency not found');
294 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
295 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
301 'message' =>
'Currency deleted'
319 if (!DolibarrApiAccess::$user->rights->multicurrency->currency->write) {
320 throw new RestException(401,
"Insufficient rights to update currency rate");
324 if (!isset($request_data[
'rate'])) {
325 throw new RestException(400,
"rate field missing");
329 if (!$multicurrency->fetch($id)) {
330 throw new RestException(404,
'Currency not found');
334 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
335 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
351 $object = parent::_cleanObjectDatas($object);
354 foreach ($object as $key => $value) {
355 if ($key ==
"rate") {
358 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
361 unset($object->$key);
377 $object = parent::_cleanObjectDatas($object);
380 foreach ($object as $key => $value) {
381 if ($key ==
"id" || $key ==
"rate" || $key ==
"date_sync") {
384 unset($object->$key);
_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.
__construct()
Constructor.
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.