29require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
44 public $element =
'multicurrency';
49 public $table_element =
'multicurrency';
54 public $table_element_line =
"multicurrency_rate";
59 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 '.join(
',', $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;
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, $trigger = true)
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 && $trigger) {
316 $result = $this->call_trigger('CURRENCY_MODIFY
', $user);
322 // Commit or rollback
324 $this->db->rollback();
340 public function delete($trigger = true)
351 $result = $this->call_trigger('CURRENCY_DELETE
', $user);
358 // Delete all rates before
359 if (!$this->deleteRates()) {
361 $this->errors[] = 'Error
'.$this->db->lasterror();
362 dol_syslog('Currency::delete
'.join(',
', $this->errors), LOG_ERR);
365 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
366 $sql .= " WHERE rowid = ".((int) $this->id);
368 dol_syslog(__METHOD__, LOG_DEBUG);
369 $resql = $this->db->query($sql);
372 $this->errors[] = 'Error
'.$this->db->lasterror();
377 // Commit or rollback
379 $this->db->rollback();
394 public function deleteRates()
396 foreach ($this->rates as &$rate) {
397 if ($rate->delete() <= 0) {
411 public function addRate($rate)
413 $currencyRate = new CurrencyRate($this->db);
414 $currencyRate->rate = price2num($rate);
416 if ($currencyRate->create($this->id) > 0) {
417 $this->rate = $currencyRate;
421 $this->errors = $currencyRate->errors;
433 public function addRateFromDolibarr($code, $rate)
437 $currency = new MultiCurrency($this->db);
438 $currency->code = $code;
439 $currency->name = $code;
441 $sql = "SELECT label FROM ".MAIN_DB_PREFIX."c_currencies WHERE code_iso = '
".$this->db->escape($code)."'";
443 dol_syslog(__METHOD__, LOG_DEBUG);
444 $resql = $this->db->query($sql);
445 if ($resql && ($line = $this->db->fetch_object($resql))) {
446 $currency->name = $line->label;
449 if ($currency->create($user) > 0) {
450 $currency->addRate($rate);
468 public function updateRate($rate)
470 return $this->addRate($rate);
478 public function getRate()
480 $sql = "SELECT cr.rowid";
481 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element_line." as cr";
482 $sql .= " WHERE cr.fk_multicurrency = ".((int) $this->id);
483 $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).")";
485 dol_syslog(__METHOD__, LOG_DEBUG);
486 $resql = $this->db->query($sql);
487 if ($resql && ($obj = $this->db->fetch_object($resql))) {
488 $this->rate = new CurrencyRate($this->db);
489 return $this->rate->fetch($obj->rowid);
503 public static function getIdFromCode($dbs, $code)
507 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE code = '
".$dbs->escape($code)."' AND entity = ".((int) $conf->entity);
509 dol_syslog(__METHOD__, LOG_DEBUG);
510 $resql = $dbs->query($sql);
511 if ($resql && $obj = $dbs->fetch_object($resql)) {
528 public static function getIdAndTxFromCode($dbs, $code, $date_document = '
')
532 $sql1 = "SELECT m.rowid, mc.rate FROM ".MAIN_DB_PREFIX."multicurrency m";
534 $sql1 .= ' LEFT JOIN
'.MAIN_DB_PREFIX.'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)
';
535 $sql1 .= " WHERE m.code = '".$dbs->escape($code)."'";
536 $sql1 .= " AND m.entity IN (".getEntity('multicurrency
').")";
538 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE) && !empty($date_document)) { // Use last known rate compared to document date
539 $tmparray = dol_getdate($date_document);
540 $sql2 .= " AND mc.date_sync <= '".$dbs->idate(dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], true))."'";
542 $sql3 = " ORDER BY mc.date_sync DESC LIMIT 1";
544 dol_syslog(__METHOD__, LOG_DEBUG);
545 $resql = $dbs->query($sql1.$sql2.$sql3);
547 if ($resql && $obj = $dbs->fetch_object($resql)) {
548 return array($obj->rowid, $obj->rate);
550 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE)) {
551 $resql = $dbs->query($sql1.$sql3);
552 if ($resql && $obj = $dbs->fetch_object($resql)) {
553 return array($obj->rowid, $obj->rate);
571 public static function getAmountConversionFromInvoiceRate($fk_facture, $amount, $way = 'dolibarr
', $table = 'facture
', $invoice_rate = null)
573 if (!is_null($invoice_rate)) {
574 $multicurrency_tx = $invoice_rate;
576 $multicurrency_tx = self::getInvoiceRate($fk_facture, $table);
579 if ($multicurrency_tx) {
580 if ($way == 'dolibarr
') {
581 return price2num($amount * $multicurrency_tx, 'MU
');
583 return price2num($amount / $multicurrency_tx, 'MU
');
597 public static function getInvoiceRate($fk_facture, $table = 'facture
')
601 $sql = "SELECT multicurrency_tx FROM ".MAIN_DB_PREFIX.$table." WHERE rowid = ".((int) $fk_facture);
603 dol_syslog(__METHOD__, LOG_DEBUG);
604 $resql = $db->query($sql);
605 if ($resql && ($line = $db->fetch_object($resql))) {
606 return $line->multicurrency_tx;
619 public function recalculRates(&$TRate)
623 if ($conf->currency != getDolGlobalString('MULTICURRENCY_APP_SOURCE
')) {
624 $alternate_source = 'USD
'.$conf->currency;
625 if (!empty($TRate->$alternate_source)) {
626 $coef = $TRate->USDUSD / $TRate->$alternate_source;
627 foreach ($TRate as $attr => &$rate) {
634 return -1; // Alternate souce not found
637 return 0; // Nothing to do
647 public function syncRates($key, $addifnotfound = 0)
649 global $conf, $db, $langs;
651 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php
';
653 $urlendpoint = 'http:
654 $urlendpoint .=
'&source=' . (empty($conf->global->MULTICURRENCY_APP_SOURCE) ?
'USD' : $conf->global->MULTICURRENCY_APP_SOURCE);
656 dol_syslog(
"Call url endpoint ".$urlendpoint);
660 if ($resget[
'content']) {
661 $response = $resget[
'content'];
662 $response = json_decode($response);
664 if ($response->success) {
665 $TRate = $response->quotes;
669 foreach ($TRate as $currency_code => $rate) {
670 $code = substr($currency_code, 3, 3);
672 if ($obj->fetch(0, $code) > 0) {
673 $obj->updateRate($rate);
674 } elseif ($addifnotfound) {
682 dol_syslog(
"Failed to call endpoint ".$response->error->info, LOG_WARNING);
683 setEventMessages($langs->trans(
'multicurrency_syncronize_error', $response->error->info),
null,
'errors');
701 if ($currencytmp->fetch(
'', $code) > 0) {
718 public $element =
'multicurrency_rate';
723 public $table_element =
'multicurrency_rate';
743 public $fk_multicurrency;
770 public function create($fk_multicurrency, $trigger =
true)
774 dol_syslog(
'CurrencyRate::create', LOG_DEBUG);
778 if (empty($this->entity) || $this->entity <= 0) {
779 $this->entity = $conf->entity;
781 $now = empty($this->date_sync) ?
dol_now() : $this->date_sync;
784 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
786 $sql .=
' date_sync,';
787 $sql .=
' fk_multicurrency,';
789 $sql .=
') VALUES (';
790 $sql .=
' '.((float) $this->rate).
',';
791 $sql .=
" '".$this->db->idate($now).
"',";
792 $sql .=
" ".((int) $fk_multicurrency).
",";
793 $sql .=
" ".((int) $this->entity);
799 $resql = $this->db->query($sql);
802 $this->errors[] =
'Error '.$this->db->lasterror();
803 dol_syslog(
'CurrencyRate::create '.join(
',', $this->errors), LOG_ERR);
807 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
808 $this->fk_multicurrency = $fk_multicurrency;
809 $this->date_sync = $now;
812 $result = $this->call_trigger(
'CURRENCYRATE_CREATE', $user);
820 $this->db->rollback();
840 $sql =
"SELECT cr.rowid, cr.rate, cr.date_sync, cr.fk_multicurrency, cr.entity";
841 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS cr";
842 $sql .=
" WHERE cr.rowid = ".((int) $id);
845 $resql = $this->db->query($sql);
847 $numrows = $this->db->num_rows($resql);
849 $obj = $this->db->fetch_object($resql);
851 $this->
id = $obj->rowid;
852 $this->rate = $obj->rate;
853 $this->date_sync = $this->db->jdate($obj->date_sync);
854 $this->fk_multicurrency = $obj->fk_multicurrency;
855 $this->entity = $obj->entity;
857 $this->db->free($resql);
865 $this->errors[] =
'Error '.$this->db->lasterror();
866 dol_syslog(
'CurrencyRate::fetch '.join(
',', $this->errors), LOG_ERR);
884 dol_syslog(
'CurrencyRate::update', LOG_DEBUG);
889 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
890 $sql .=
"SET rate = ".((float) $this->rate);
891 if (!empty($this->date_sync)) {
892 $sql .=
", date_sync = '".$this->db->idate($this->date_sync).
"'";
894 if (!empty($this->fk_multicurrency)) {
895 $sql .=
', fk_multicurrency = '.((int) $this->fk_multicurrency);
897 $sql .=
" WHERE rowid =".((int) $this->
id);
902 $resql = $this->db->query($sql);
905 $this->errors[] =
'Error '.$this->db->lasterror();
906 dol_syslog(
'CurrencyRate::update '.join(
',', $this->errors), LOG_ERR);
909 if (!$error && $trigger) {
910 $result = $this->call_trigger(
'CURRENCYRATE_MODIFY', $user);
918 $this->db->rollback();
934 public function delete($trigger =
true)
938 dol_syslog(
'CurrencyRate::delete', LOG_DEBUG);
945 $result = $this->call_trigger(
'CURRENCYRATE_DELETE', $user);
952 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
953 $sql .=
' WHERE rowid='.((int) $this->
id);
956 $resql = $this->db->query($sql);
959 $this->errors[] =
'Error '.$this->db->lasterror();
960 dol_syslog(
'CurrencyRate::delete '.join(
',', $this->errors), LOG_ERR);
966 $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($trigger=true)
Update object into database.
create($fk_multicurrency, $trigger=true)
Create object into database.
__construct(DoliDB $db)
Constructor.
Class to manage Dolibarr database access.
fetchAllCurrencyRate()
Load all rates in object from the database.
checkCodeAlreadyExists($code)
Check in database if the current code already exists.
__construct(DoliDB $db)
Constructor.
update(User $user, $trigger=true)
Update object into database.
delete($trigger=true)
Delete object in database.
addRateFromDolibarr($code, $rate)
Try get label of code in llx_currency then add rate.
create(User $user, $trigger=true)
Create object into database.
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.
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.