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';
74 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
78 $sql .= $this->db->order($sortfield, $sortorder);
83 $offset = $limit * $page;
85 $sql .= $this->db->plimit($limit + 1, $offset);
88 $result = $this->db->query($sql);
91 $num = $this->db->num_rows($result);
92 $min = min($num, ($limit <= 0 ? $num : $limit));
94 $obj = $this->db->fetch_object($result);
96 if ($multicurrency_static->fetch($obj->rowid)) {
102 throw new RestException(503,
'Error when retrieve currencies list : '.$this->db->lasterror());
118 public function get($id)
121 if (!$multicurrency->fetch($id)) {
122 throw new RestException(404,
'Currency not found');
125 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
126 throw new RestException(403,
"Insufficient rights to read currency");
146 if (!$multicurrency->fetch(
'', $code)) {
147 throw new RestException(404,
'Currency not found');
150 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
151 throw new RestException(403,
"Insufficient rights to read currency");
171 if (!$multicurrency->fetch($id)) {
172 throw new RestException(404,
'Currency not found');
175 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'read')) {
176 throw new RestException(403,
"Insufficient rights to read currency rates");
179 if ($multicurrency->fetchAllCurrencyRate() < 0) {
180 throw new RestException(500,
"Error when fetching currency rates");
184 foreach ($multicurrency->rates as $key => $obj) {
188 return $multicurrency->rates;
199 public function post($request_data =
null)
203 if (!isset($request_data[
'code'])) {
204 throw new RestException(400,
"code field missing");
206 if (!isset($request_data[
'name'])) {
207 throw new RestException(400,
"name field missing");
210 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
211 throw new RestException(403,
"Insufficient rights to create currency");
216 foreach ($request_data as $field => $value) {
217 if ($field ===
'caller') {
219 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
223 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
227 if ($multicurrency->create(DolibarrApiAccess::$user) < 0) {
228 throw new RestException(500,
"Error creating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
232 if (isset($request_data[
'rate']) && $request_data[
'rate'] > 0) {
233 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
234 throw new RestException(500,
"Error adding currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
237 return $multicurrency->id;
240 return $multicurrency->id;
252 public function put($id, $request_data =
null)
254 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
255 throw new RestException(403,
"Insufficient rights to update currency");
259 if (!$multicurrency->fetch($id)) {
260 throw new RestException(404,
'Currency not found');
263 foreach ($request_data as $field => $value) {
264 if ($field ==
'id') {
267 if ($field ===
'caller') {
269 $multicurrency->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
273 $multicurrency->$field = $this->
_checkValForAPI($field, $value, $multicurrency);
276 if ($multicurrency->update(DolibarrApiAccess::$user) < 0) {
277 throw new RestException(500,
"Error updating currency", array_merge(array($multicurrency->error), $multicurrency->errors));
280 return $this->
get($id);
291 public function delete($id)
293 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'delete')) {
294 throw new RestException(403,
"Insufficient rights to delete currency");
298 if (!$multicurrency->fetch($id)) {
299 throw new RestException(404,
'Currency not found');
302 if (!$multicurrency->delete(DolibarrApiAccess::$user)) {
303 throw new RestException(500,
"Error deleting currency", array_merge(array($multicurrency->error), $multicurrency->errors));
309 'message' =>
'Currency deleted'
327 if (!DolibarrApiAccess::$user->hasRight(
'multicurrency',
'currency',
'write')) {
328 throw new RestException(403,
"Insufficient rights to update currency rate");
332 if (!isset($request_data[
'rate'])) {
333 throw new RestException(400,
"Rate field is missing");
337 if (!$multicurrency->fetch($id)) {
338 throw new RestException(404,
'Currency not found');
342 if ($multicurrency->addRate($request_data[
'rate']) < 0) {
343 throw new RestException(500,
"Error updating currency rate", array_merge(array($multicurrency->error), $multicurrency->errors));
362 foreach (
$object as $key => $value) {
363 if ($key ==
"rate") {
366 if ($key ==
"id" || $key ==
"code" || $key ==
"rate" || $key ==
"name") {
388 foreach (
$object as $key => $value) {
389 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.
_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.