19use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/core/lib/multicurrency.lib.php';
59 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
63 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
64 throw new RestException(403,
"Insufficient rights to read currency");
69 $sql =
"SELECT t.rowid";
70 $sql .=
" FROM ".$this->db->prefix().
"multicurrency as t";
71 $sql .=
" WHERE t.entity IN (".getEntity(
'multicurrency').
")";
77 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
81 $sql .= $this->db->order($sortfield, $sortorder);
86 $offset = $limit * $page;
88 $sql .= $this->db->plimit($limit + 1, $offset);
91 $result = $this->db->query($sql);
94 $num = $this->db->num_rows($result);
95 $min = min($num, ($limit <= 0 ? $num : $limit));
97 $obj = $this->db->fetch_object($result);
99 if ($multicurrency_static->fetch($obj->rowid)) {
105 throw new RestException(503,
'Error when retrieve currencies list : '.$this->db->lasterror());
124 if (!$multicurrency->fetch(
$id)) {
125 throw new RestException(404,
'Currency not found');
128 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
129 throw new RestException(403,
"Insufficient rights to read currency");
149 if (!$multicurrency->fetch(0, $code)) {
150 throw new RestException(404,
'Currency not found');
153 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
154 throw new RestException(403,
"Insufficient rights to read currency");
174 if (!$multicurrency->fetch(
$id)) {
175 throw new RestException(404,
'Currency not found');
178 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
179 throw new RestException(403,
"Insufficient rights to read currency rates");
182 if ($multicurrency->fetchAllCurrencyRate() < 0) {
183 throw new RestException(500,
"Error when fetching currency rates");
187 foreach ($multicurrency->rates as $key => $obj) {
191 return $multicurrency->rates;
204 public function post($request_data =
null)
208 if (!isset($request_data[
'code'])) {
209 throw new RestException(400,
"code field missing");
211 if (!isset($request_data[
'name'])) {
212 throw new RestException(400,
"name field missing");
215 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
216 throw new RestException(403,
"Insufficient rights to create currency");
221 foreach ($request_data as $field => $value) {
222 if ($field ===
'caller') {
224 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
228 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
232 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
233 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
237 if (isset($request_data[
'rate']) && $request_data[
'rate'] > 0) {
238 if ($multicurrency->addRate((
float) $request_data[
'rate']) < 0) {
239 throw new RestException(500,
"Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
242 return $multicurrency->id;
245 return $multicurrency->id;
259 public function put(
$id, $request_data =
null)
261 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
262 throw new RestException(403,
"Insufficient rights to update currency");
266 if (!$multicurrency->fetch(
$id)) {
267 throw new RestException(404,
'Currency not found');
270 foreach ($request_data as $field => $value) {
271 if ($field ==
'id') {
274 if ($field ===
'caller') {
276 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
280 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
283 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
284 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
287 return $this->
get(
$id);
300 public function delete(
$id)
302 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'delete')) {
303 throw new RestException(403,
"Insufficient rights to delete currency");
307 if (!$multicurrency->fetch(
$id)) {
308 throw new RestException(404,
'Currency not found');
311 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
312 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
318 'message' =>
'Currency deleted'
338 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
339 throw new RestException(403,
"Insufficient rights to update currency rate");
343 if (!isset($request_data[
'rate'])) {
344 throw new RestException(400,
"Rate field is missing");
348 if (!$multicurrency->fetch(
$id)) {
349 throw new RestException(404,
'Currency not found');
353 if ($multicurrency->addRate((
float) $request_data[
'rate']) < 0) {
354 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
373 foreach (
$object as $key => $value) {
374 if ($key ==
"rate") {
377 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
399 foreach (
$object as $key => $value) {
400 if ($key ==
"id" || $key ==
"rate" || $key ==
"date_sync") {
$id
Support class for third parties, contacts, members, users or resources.
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.