19use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/core/lib/multicurrency.lib.php';
57 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
61 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
62 throw new RestException(403,
"Insufficient rights to read currency");
67 $sql =
"SELECT t.rowid";
68 $sql .=
" FROM ".$this->db->prefix().
"multicurrency as t";
69 $sql .=
' WHERE 1 = 1';
74 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
76 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
77 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
80 $sql .= $this->db->order($sortfield, $sortorder);
85 $offset = $limit * $page;
87 $sql .= $this->db->plimit($limit + 1, $offset);
90 $result = $this->db->query($sql);
93 $num = $this->db->num_rows($result);
94 $min = min($num, ($limit <= 0 ? $num : $limit));
96 $obj = $this->db->fetch_object($result);
98 if ($multicurrency_static->fetch($obj->rowid)) {
104 throw new RestException(503,
'Error when retrieve currencies list : '.$this->db->lasterror());
123 if (!$multicurrency->fetch(
$id)) {
124 throw new RestException(404,
'Currency not found');
127 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
128 throw new RestException(403,
"Insufficient rights to read currency");
148 if (!$multicurrency->fetch(0, $code)) {
149 throw new RestException(404,
'Currency not found');
152 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
153 throw new RestException(403,
"Insufficient rights to read currency");
173 if (!$multicurrency->fetch(
$id)) {
174 throw new RestException(404,
'Currency not found');
177 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
178 throw new RestException(403,
"Insufficient rights to read currency rates");
181 if ($multicurrency->fetchAllCurrencyRate() < 0) {
182 throw new RestException(500,
"Error when fetching currency rates");
186 foreach ($multicurrency->rates as $key => $obj) {
190 return $multicurrency->rates;
201 public function post($request_data =
null)
205 if (!isset($request_data[
'code'])) {
206 throw new RestException(400,
"code field missing");
208 if (!isset($request_data[
'name'])) {
209 throw new RestException(400,
"name field missing");
212 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
213 throw new RestException(403,
"Insufficient rights to create currency");
218 foreach ($request_data as $field => $value) {
219 if ($field ===
'caller') {
221 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
225 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
229 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
230 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
234 if (isset($request_data[
'rate']) && $request_data[
'rate'] > 0) {
235 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
236 throw new RestException(500,
"Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
239 return $multicurrency->id;
242 return $multicurrency->id;
254 public function put(
$id, $request_data =
null)
256 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
257 throw new RestException(403,
"Insufficient rights to update currency");
261 if (!$multicurrency->fetch(
$id)) {
262 throw new RestException(404,
'Currency not found');
265 foreach ($request_data as $field => $value) {
266 if ($field ==
'id') {
269 if ($field ===
'caller') {
271 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
275 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
278 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
279 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
282 return $this->
get(
$id);
293 public function delete(
$id)
295 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'delete')) {
296 throw new RestException(403,
"Insufficient rights to delete currency");
300 if (!$multicurrency->fetch(
$id)) {
301 throw new RestException(404,
'Currency not found');
304 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
305 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
311 'message' =>
'Currency deleted'
329 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
330 throw new RestException(403,
"Insufficient rights to update currency rate");
334 if (!isset($request_data[
'rate'])) {
335 throw new RestException(400,
"Rate field is missing");
339 if (!$multicurrency->fetch(
$id)) {
340 throw new RestException(404,
'Currency not found');
344 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
345 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
364 foreach (
$object as $key => $value) {
365 if ($key ==
"rate") {
368 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
390 foreach (
$object as $key => $value) {
391 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.