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->hasRight(
'multicurrency',
'currency',
'read')) {
61 throw new RestException(403,
"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());
122 if (!$multicurrency->fetch(
$id)) {
123 throw new RestException(404,
'Currency not found');
126 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
127 throw new RestException(403,
"Insufficient rights to read currency");
147 if (!$multicurrency->fetch(
'', $code)) {
148 throw new RestException(404,
'Currency not found');
151 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
152 throw new RestException(403,
"Insufficient rights to read currency");
172 if (!$multicurrency->fetch(
$id)) {
173 throw new RestException(404,
'Currency not found');
176 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
177 throw new RestException(403,
"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)
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");
211 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
212 throw new RestException(403,
"Insufficient rights to create currency");
217 foreach ($request_data as $field => $value) {
218 if ($field ===
'caller') {
220 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
224 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
228 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
229 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
233 if (isset($request_data[
'rate']) && $request_data[
'rate'] > 0) {
234 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
235 throw new RestException(500,
"Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
238 return $multicurrency->id;
241 return $multicurrency->id;
253 public function put(
$id, $request_data =
null)
255 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
256 throw new RestException(403,
"Insufficient rights to update currency");
260 if (!$multicurrency->fetch(
$id)) {
261 throw new RestException(404,
'Currency not found');
264 foreach ($request_data as $field => $value) {
265 if ($field ==
'id') {
268 if ($field ===
'caller') {
270 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
274 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
277 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
278 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
281 return $this->
get(
$id);
292 public function delete(
$id)
294 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'delete')) {
295 throw new RestException(403,
"Insufficient rights to delete currency");
299 if (!$multicurrency->fetch(
$id)) {
300 throw new RestException(404,
'Currency not found');
303 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
304 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
310 'message' =>
'Currency deleted'
328 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
329 throw new RestException(403,
"Insufficient rights to update currency rate");
333 if (!isset($request_data[
'rate'])) {
334 throw new RestException(400,
"Rate field is missing");
338 if (!$multicurrency->fetch(
$id)) {
339 throw new RestException(404,
'Currency not found');
343 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
344 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
363 foreach (
$object as $key => $value) {
364 if ($key ==
"rate") {
367 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
389 foreach (
$object as $key => $value) {
390 if ($key ==
"id" || $key ==
"rate" || $key ==
"date_sync") {
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_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.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.