31require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
32require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
46 public $element =
'multicurrency';
51 public $table_element =
'multicurrency';
56 public $table_element_line =
"multicurrency_rate";
61 public $rates = array();
118 global $conf, $langs;
120 dol_syslog(
'MultiCurrency::create', LOG_DEBUG);
124 if (self::checkCodeAlreadyExists($this->code)) {
126 $this->errors[] = $langs->trans(
'multicurrency_code_already_added');
130 if (empty($this->entity) || $this->entity <= 0) {
131 $this->entity = $conf->entity;
136 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
140 $sql .=
' date_create,';
142 $sql .=
') VALUES (';
143 $sql .=
" '".$this->db->escape($this->code).
"',";
144 $sql .=
" '".$this->db->escape($this->
name).
"',";
145 $sql .=
" ".((int) $this->entity).
",";
146 $sql .=
" '".$this->db->idate($now).
"',";
147 $sql .=
" ".((int) $user->id);
153 $resql = $this->db->query($sql);
156 $this->errors[] =
'Error '.$this->db->lasterror();
157 dol_syslog(
'MultiCurrency::create '.implode(
',', $this->errors), LOG_ERR);
161 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
162 $this->date_create = $now;
163 $this->fk_user = $user->id;
165 if (empty($notrigger)) {
166 $result = $this->
call_trigger(
'CURRENCY_CREATE', $user);
174 $this->db->rollback();
191 public function fetch($id, $code =
null)
193 dol_syslog(
'MultiCurrency::fetch', LOG_DEBUG);
198 $sql .=
' c.rowid, c.name, c.code, c.entity, c.date_create, c.fk_user';
199 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' AS c';
201 $sql .=
' WHERE c.code = \''.$this->db->escape($code).
'\' AND c.entity =
'.$conf->entity;
203 $sql .= ' WHERE c.rowid =
'.((int) $id);
206 dol_syslog(__METHOD__, LOG_DEBUG);
207 $resql = $this->db->query($sql);
210 $numrows = $this->db->num_rows($resql);
212 $obj = $this->db->fetch_object($resql);
214 $this->id = $obj->rowid;
215 $this->name = $obj->name;
216 $this->code = $obj->code;
217 $this->entity = $obj->entity;
218 $this->date_create = $obj->date_create;
219 $this->fk_user = $obj->fk_user;
221 $this->fetchAllCurrencyRate();
224 $this->db->free($resql);
232 $this->errors[] = 'Error
'.$this->db->lasterror();
244 public function fetchAllCurrencyRate()
246 $sql = "SELECT cr.rowid";
247 $sql .= ' FROM
'.MAIN_DB_PREFIX.$this->table_element_line.' as cr
';
248 $sql .= ' WHERE cr.fk_multicurrency =
'.((int) $this->id);
249 $sql .= ' ORDER BY cr.date_sync DESC
';
251 $this->rates = array();
253 dol_syslog(__METHOD__, LOG_DEBUG);
254 $resql = $this->db->query($sql);
256 $num = $this->db->num_rows($resql);
258 while ($obj = $this->db->fetch_object($resql)) {
259 $rate = new CurrencyRate($this->db);
260 $rate->fetch($obj->rowid);
262 $this->rates[] = $rate;
264 $this->db->free($resql);
268 $this->errors[] = 'Error
'.$this->db->lasterror();
282 public function update(User $user, $notrigger = 0)
289 $this->name = trim($this->name);
290 $this->code = trim($this->code);
293 if (empty($this->code)) {
301 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
302 $sql .= " name = '".$this->db->escape($this->name)."',";
303 $sql .= " code = '".$this->db->escape($this->code)."'";
304 $sql .= " WHERE rowid = ".((int) $this->id);
308 $resql = $this->db->query($sql);
311 $this->errors[] = 'Error
'.$this->db->lasterror();
315 if (!$error && empty($notrigger)) {
316 $result = $this->call_trigger('CURRENCY_MODIFY
', $user);
322 // Commit or rollback
324 $this->db->rollback();
341 public function delete(User $user, $notrigger = 0)
349 if (empty($notrigger)) {
350 $result = $this->call_trigger('CURRENCY_DELETE
', $user);
357 // Delete all rates before
358 if (!$this->deleteRates()) {
360 $this->errors[] = 'Error
'.$this->db->lasterror();
361 dol_syslog('Currency::delete
'.implode(',
', $this->errors), LOG_ERR);
364 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
365 $sql .= " WHERE rowid = ".((int) $this->id);
367 dol_syslog(__METHOD__, LOG_DEBUG);
368 $resql = $this->db->query($sql);
371 $this->errors[] = 'Error
'.$this->db->lasterror();
376 // Commit or rollback
378 $this->db->rollback();
393 public function deleteRates()
397 foreach ($this->rates as &$rate) {
398 if ($rate->delete($user) <= 0) {
412 public function addRate($rate)
416 $currencyRate = new CurrencyRate($this->db);
417 $currencyRate->rate = (float) price2num($rate);
419 if ($currencyRate->create($user, $this->id) > 0) {
420 $this->rate = $currencyRate;
424 $this->errors = $currencyRate->errors;
436 public function addRateFromDolibarr($code, $rate)
440 $currency = new MultiCurrency($this->db);
441 $currency->code = $code;
442 $currency->name = $code;
444 $sql = "SELECT label FROM ".MAIN_DB_PREFIX."c_currencies WHERE code_iso = '
".$this->db->escape($code)."'";
446 dol_syslog(__METHOD__, LOG_DEBUG);
447 $resql = $this->db->query($sql);
448 if ($resql && ($line = $this->db->fetch_object($resql))) {
449 $currency->name = $line->label;
452 if ($currency->create($user) > 0) {
453 $currency->addRate($rate);
471 public function updateRate($rate)
473 return $this->addRate($rate);
481 public function getRate()
483 $sql = "SELECT cr.rowid";
484 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element_line." as cr";
485 $sql .= " WHERE cr.fk_multicurrency = ".((int) $this->id);
486 $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX.$this->table_element_line." AS cr2 WHERE cr2.fk_multicurrency = ".((int) $this->id).")";
488 dol_syslog(__METHOD__, LOG_DEBUG);
489 $resql = $this->db->query($sql);
490 if ($resql && ($obj = $this->db->fetch_object($resql))) {
491 $this->rate = new CurrencyRate($this->db);
492 return $this->rate->fetch($obj->rowid);
506 public static function getIdFromCode($dbs, $code)
510 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE code = '
".$dbs->escape($code)."' AND entity = ".((int) $conf->entity);
512 dol_syslog(__METHOD__, LOG_DEBUG);
513 $resql = $dbs->query($sql);
514 if ($resql && $obj = $dbs->fetch_object($resql)) {
531 public static function getIdAndTxFromCode($dbs, $code, $date_document = '
')
535 $sql1 = "SELECT m.rowid, mc.rate FROM ".MAIN_DB_PREFIX."multicurrency m";
537 $sql1 .= ' LEFT JOIN
'.MAIN_DB_PREFIX.'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)
';
538 $sql1 .= " WHERE m.code = '".$dbs->escape($code)."'";
539 $sql1 .= " AND m.entity IN (".getEntity('multicurrency
').")";
541 if (getDolGlobalString('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE
') && !empty($date_document)) { // Use last known rate compared to document date
542 $tmparray = dol_getdate($date_document);
543 $sql2 .= " AND mc.date_sync <= '".$dbs->idate(dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], true))."'";
545 $sql3 = " ORDER BY mc.date_sync DESC LIMIT 1";
547 dol_syslog(__METHOD__, LOG_DEBUG);
548 $resql = $dbs->query($sql1.$sql2.$sql3);
550 if ($resql && $obj = $dbs->fetch_object($resql)) {
551 return array($obj->rowid, $obj->rate);
553 if (getDolGlobalString('MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE
')) {
554 $resql = $dbs->query($sql1.$sql3);
555 if ($resql && $obj = $dbs->fetch_object($resql)) {
556 return array($obj->rowid, $obj->rate);
574 public static function getAmountConversionFromInvoiceRate($fk_facture, $amount, $way = 'dolibarr
', $table = 'facture
', $invoice_rate = null)
576 if (!is_null($invoice_rate)) {
577 $multicurrency_tx = $invoice_rate;
579 $multicurrency_tx = self::getInvoiceRate($fk_facture, $table);
582 if ($multicurrency_tx) {
583 if ($way == 'dolibarr
') {
584 return (float) price2num($amount * $multicurrency_tx, 'MU
');
586 return (float) price2num($amount / $multicurrency_tx, 'MU
');
600 public static function getInvoiceRate($fk_facture, $table = 'facture
')
604 $sql = "SELECT multicurrency_tx FROM ".MAIN_DB_PREFIX.$table." WHERE rowid = ".((int) $fk_facture);
606 dol_syslog(__METHOD__, LOG_DEBUG);
607 $resql = $db->query($sql);
608 if ($resql && ($line = $db->fetch_object($resql))) {
609 return $line->multicurrency_tx;
622 public function recalculRates(&$TRate)
626 if ($conf->currency != getDolGlobalString('MULTICURRENCY_APP_SOURCE
')) {
627 $alternate_source = 'USD
'.$conf->currency;
628 if (!empty($TRate->$alternate_source)) {
629 $coef = 1 / $TRate->$alternate_source;
630 foreach ($TRate as $attr => &$rate) {
633 $TRate->USDUSD = $coef;
637 return -1; // Alternate source not found
640 return 0; // Nothing to do
651 public function syncRates($key, $addifnotfound = 0, $mode = "")
653 global $conf, $db, $langs;
655 if (getDolGlobalString('MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER
')) {
656 if ($mode == "cron") {
657 $this->output = $langs->trans('Use of API
for currency
update is
disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER
');
659 setEventMessages($langs->trans('Use of API
for currency
update is
disabled by option MULTICURRENCY_DISABLE_SYNC_CURRENCYLAYER
'), null, 'errors
');
665 $key = getDolGlobalString("MULTICURRENCY_APP_ID");
668 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php
';
670 $urlendpoint = 'http:
671 $urlendpoint .=
'&source=' . (!
getDolGlobalString(
'MULTICURRENCY_APP_SOURCE') ?
'USD' : $conf->global->MULTICURRENCY_APP_SOURCE);
673 dol_syslog(
"Call url endpoint ".$urlendpoint);
677 if ($resget[
'content']) {
678 $response = $resget[
'content'];
679 $response = json_decode($response);
681 if ($response->success) {
682 $TRate = $response->quotes;
686 foreach ($TRate as $currency_code => $rate) {
687 $code = substr($currency_code, 3, 3);
689 if ($obj->fetch(0, $code) > 0) {
690 $obj->updateRate($rate);
691 } elseif ($addifnotfound) {
697 if ($mode ==
"cron") {
702 dol_syslog(
"Failed to call endpoint ".$response->error->info, LOG_WARNING);
703 if ($mode ==
"cron") {
704 $this->output = $langs->trans(
'multicurrency_syncronize_error', $response->error->info);
706 setEventMessages($langs->trans(
'multicurrency_syncronize_error', $response->error->info),
null,
'errors');
724 if ($currencytmp->fetch(
'', $code) > 0) {
741 public $element =
'multicurrency_rate';
746 public $table_element =
'multicurrency_rate';
761 public $rate_indirect;
771 public $fk_multicurrency;
797 public function create(
User $user,
int $fk_multicurrency, $notrigger = 0)
801 dol_syslog(
'CurrencyRate::create', LOG_DEBUG);
804 $this->rate = (float)
price2num($this->rate);
805 if (empty($this->entity) || $this->entity <= 0) {
806 $this->entity = $conf->entity;
808 $now = empty($this->date_sync) ?
dol_now() : $this->date_sync;
811 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
813 $sql .=
' rate_indirect,';
814 $sql .=
' date_sync,';
815 $sql .=
' fk_multicurrency,';
817 $sql .=
') VALUES (';
818 $sql .=
' '.((float) $this->rate).
',';
819 $sql .=
' '.((float) $this->rate_indirect).
',';
820 $sql .=
" '".$this->db->idate($now).
"',";
821 $sql .=
" ".((int) $fk_multicurrency).
",";
822 $sql .=
" ".((int) $this->entity);
828 $resql = $this->db->query($sql);
831 $this->errors[] =
'Error '.$this->db->lasterror();
832 dol_syslog(
'CurrencyRate::create '.implode(
',', $this->errors), LOG_ERR);
836 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
837 $this->fk_multicurrency = $fk_multicurrency;
838 $this->date_sync = $now;
840 if (empty($notrigger)) {
841 $result = $this->call_trigger(
'CURRENCYRATE_CREATE', $user);
849 $this->db->rollback();
869 $sql =
"SELECT cr.rowid, cr.rate, cr.rate_indirect, cr.date_sync, cr.fk_multicurrency, cr.entity";
870 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS cr";
871 $sql .=
" WHERE cr.rowid = ".((int) $id);
874 $resql = $this->db->query($sql);
876 $numrows = $this->db->num_rows($resql);
878 $obj = $this->db->fetch_object($resql);
880 $this->
id = $obj->rowid;
881 $this->rate = $obj->rate;
882 $this->rate_indirect = $obj->rate_indirect;
883 $this->date_sync = $this->db->jdate($obj->date_sync);
884 $this->fk_multicurrency = $obj->fk_multicurrency;
885 $this->entity = $obj->entity;
887 $this->db->free($resql);
895 $this->errors[] =
'Error '.$this->db->lasterror();
896 dol_syslog(
'CurrencyRate::fetch '.implode(
',', $this->errors), LOG_ERR);
913 dol_syslog(
'CurrencyRate::update', LOG_DEBUG);
915 $this->rate = (float)
price2num($this->rate);
918 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
919 $sql .=
"SET rate = ".((float) $this->rate);
920 if (!empty($this->date_sync)) {
921 $sql .=
", date_sync = '".$this->db->idate($this->date_sync).
"'";
923 if (!empty($this->fk_multicurrency)) {
924 $sql .=
', fk_multicurrency = '.((int) $this->fk_multicurrency);
926 $sql .=
" WHERE rowid =".((int) $this->
id);
931 $resql = $this->db->query($sql);
934 $this->errors[] =
'Error '.$this->db->lasterror();
935 dol_syslog(
'CurrencyRate::update '.implode(
',', $this->errors), LOG_ERR);
938 if (!$error && empty($notrigger)) {
939 $result = $this->call_trigger(
'CURRENCYRATE_MODIFY', $user);
947 $this->db->rollback();
964 public function delete(
User $user, $notrigger = 0)
966 dol_syslog(
'CurrencyRate::delete', LOG_DEBUG);
972 if (empty($notrigger)) {
973 $result = $this->call_trigger(
'CURRENCYRATE_DELETE', $user);
980 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
981 $sql .=
' WHERE rowid='.((int) $this->
id);
984 $resql = $this->db->query($sql);
987 $this->errors[] =
'Error '.$this->db->lasterror();
988 dol_syslog(
'CurrencyRate::delete '.implode(
',', $this->errors), LOG_ERR);
994 $this->db->rollback();
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
fetch($id)
Load object in memory from the database.
update(User $user, $notrigger=0)
Update object into database.
create(User $user, int $fk_multicurrency, $notrigger=0)
Create object into database.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
delete(User $user, $notrigger=0)
Delete object in database.
update(User $user, $notrigger=0)
Update object into database.
fetchAllCurrencyRate()
Load all rates in object from the database.
checkCodeAlreadyExists($code)
Check in database if the current code already exists.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=0)
Create object into database.
addRateFromDolibarr($code, $rate)
Try get label of code in llx_currency then add rate.
recalculRates(&$TRate)
With free account we can't set source then recalcul all rates to force another source.
fetch($id, $code=null)
Load object in memory from the database.
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_now($mode='auto')
Return date for now.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
$conf db name
Only used if Module[ID]Name translation string is not found.