29 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
30 require_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))
127 $this->errors[] = $langs->trans(
'multicurrency_code_already_added');
131 if (empty($this->entity) || $this->entity <= 0) $this->entity = $conf->entity;
132 $now = date(
'Y-m-d H:i:s');
135 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
'(';
139 $sql .=
' date_create,';
141 $sql .=
') VALUES (';
142 $sql .=
' \''.$this->db->escape($this->
code).
'\',
';
143 $sql .= ' \
''.$this->db->escape($this->
name).
'\',
';
144 $sql .= ' \
''.$this->entity.
'\',
';
145 $sql .= ' \
''.$now.
'\',
';
146 $sql .= ' \
''.$user->id.
'\'';
155 $this->errors[] =
'Error '.$this->db->lasterror();
156 dol_syslog(
'MultiCurrency::create '.join(
',', $this->errors), LOG_ERR);
160 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
161 $this->date_create = $now;
162 $this->fk_user = $user->id;
165 $result = $this->
call_trigger(
'CURRENCY_CREATE', $user);
166 if ($result < 0) $error++;
171 $this->
db->rollback();
188 public function fetch($id, $code = null)
190 dol_syslog(
'MultiCurrency::fetch', LOG_DEBUG);
195 $sql .=
' c.rowid, c.name, c.code, c.entity, c.date_create, c.fk_user';
196 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' AS c';
197 if (!empty($code)) $sql .=
' WHERE c.code = \''.$this->db->escape($code).
'\' AND c.entity =
'.$conf->entity;
198 else $sql .= ' WHERE c.rowid =
'.$id;
200 dol_syslog(__METHOD__, LOG_DEBUG);
201 $resql = $this->db->query($sql);
204 $numrows = $this->db->num_rows($resql);
206 $obj = $this->db->fetch_object($resql);
208 $this->id = $obj->rowid;
209 $this->name = $obj->name;
210 $this->code = $obj->code;
211 $this->entity = $obj->entity;
212 $this->date_create = $obj->date_create;
213 $this->fk_user = $obj->fk_user;
215 $this->fetchAllCurrencyRate();
218 $this->db->free($resql);
226 $this->errors[] = 'Error
'.$this->db->lasterror();
238 public function fetchAllCurrencyRate()
240 $sql = 'SELECT cr.rowid
';
241 $sql .= ' FROM
'.MAIN_DB_PREFIX.$this->table_element_line.' as cr
';
242 $sql .= ' WHERE cr.fk_multicurrency =
'.$this->id;
243 $sql .= ' ORDER BY cr.date_sync DESC
';
245 $this->rates = array();
247 dol_syslog(__METHOD__, LOG_DEBUG);
248 $resql = $this->db->query($sql);
250 $num = $this->db->num_rows($resql);
252 while ($obj = $this->db->fetch_object($resql)) {
253 $rate = new CurrencyRate($this->db);
254 $rate->fetch($obj->rowid);
256 $this->rates[] = $rate;
258 $this->db->free($resql);
262 $this->errors[] = 'Error
'.$this->db->lasterror();
276 public function update(User $user, $trigger = true)
283 $this->name = trim($this->name);
284 $this->code = trim($this->code);
287 if (empty($this->code)) {
295 $sql = 'UPDATE
'.MAIN_DB_PREFIX.$this->table_element.' SET
';
296 $sql .= ' name=\
''.$this->db->escape($this->name).
'\'';
297 $sql .=
' code=\''.$this->db->escape($this->
code).
'\'';
298 $sql .=
' WHERE rowid='.$this->id;
305 $this->errors[] =
'Error '.$this->db->lasterror();
306 dol_syslog(
'MultiCurrency::update '.join(
',', $this->errors), LOG_ERR);
309 if (!$error && $trigger) {
310 $result = $this->
call_trigger(
'CURRENCY_MODIFY', $user);
311 if ($result < 0) $error++;
316 $this->
db->rollback();
332 public function delete($trigger =
true)
336 dol_syslog(
'MultiCurrency::delete', LOG_DEBUG);
343 $result = $this->
call_trigger(
'CURRENCY_DELETE', $user);
344 if ($result < 0) $error++;
351 $this->errors[] =
'Error '.$this->db->lasterror();
352 dol_syslog(
'Currency::delete '.join(
',', $this->errors), LOG_ERR);
355 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
356 $sql .=
' WHERE rowid='.$this->id;
362 $this->errors[] =
'Error '.$this->db->lasterror();
363 dol_syslog(
'MultiCurrency::delete '.join(
',', $this->errors), LOG_ERR);
369 $this->
db->rollback();
386 foreach ($this->rates as &$rate)
388 if ($rate->delete() <= 0)
408 if ($currencyRate->create($this->id) > 0)
410 $this->rate = $currencyRate;
414 $this->errors = $currencyRate->errors;
431 $currency->code = $code;
432 $currency->name = $code;
434 $sql =
'SELECT label FROM '.MAIN_DB_PREFIX.
"c_currencies WHERE code_iso = '".$this->
db->escape($code).
"'";
437 $resql = $db->query($sql);
440 $currency->name = $line->label;
443 if ($currency->create($user) > 0)
445 $currency->addRate($rate);
447 if (!empty($line))
return 2;
472 $sql =
'SELECT cr.rowid';
473 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element_line.
' as cr';
474 $sql .=
' WHERE cr.fk_multicurrency = '.$this->id;
475 $sql .=
' AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM '.MAIN_DB_PREFIX.$this->table_element_line.
' AS cr2 WHERE cr2.fk_multicurrency = '.$this->
id.
')';
481 return $this->rate->fetch($obj->rowid);
497 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
"multicurrency WHERE code = '".$db->escape($code).
"' AND entity = ".$conf->entity;
500 $resql = $db->query($sql);
501 if (
$resql && $obj = $db->fetch_object(
$resql))
return $obj->rowid;
519 $sql1 =
'SELECT m.rowid, mc.rate FROM '.MAIN_DB_PREFIX.
'multicurrency m';
521 $sql1 .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)';
522 $sql1 .=
" WHERE m.code = '".$db->escape($code).
"'";
523 $sql1 .=
" AND m.entity IN (".getEntity(
'multicurrency').
")";
525 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE) && !empty($date_document)) {
527 $sql2 .=
" AND mc.date_sync <= '".$db->idate(
dol_mktime(23, 59, 59, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year'],
true)).
"'";
529 $sql3 =
' ORDER BY mc.date_sync DESC LIMIT 1';
532 $resql = $db->query($sql1.$sql2.$sql3);
534 if (
$resql && $obj = $db->fetch_object(
$resql))
return array($obj->rowid, $obj->rate);
536 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE))
538 $resql = $db->query($sql1.$sql3);
539 if (
$resql && $obj = $db->fetch_object(
$resql))
return array($obj->rowid, $obj->rate);
557 $multicurrency_tx = self::getInvoiceRate($fk_facture, $table);
559 if ($multicurrency_tx)
561 if ($way ==
'dolibarr')
return price2num($amount * $multicurrency_tx,
'MU');
562 else return price2num($amount / $multicurrency_tx,
'MU');
563 }
else return $amount;
577 $sql =
'SELECT multicurrency_tx FROM '.MAIN_DB_PREFIX.$table.
' WHERE rowid = '.$fk_facture;
580 $resql = $db->query($sql);
583 return $line->multicurrency_tx;
600 if ($conf->currency != $conf->global->MULTICURRENCY_APP_SOURCE)
602 $alternate_source =
'USD'.$conf->currency;
603 if (!empty($TRate->{$alternate_source}))
605 $coef = $TRate->USDUSD / $TRate->{$alternate_source};
606 foreach ($TRate as $attr => &$rate)
627 public static function syncRates($key, $addifnotfound = 0)
629 global $conf, $db, $langs;
631 include_once DOL_DOCUMENT_ROOT.
'/core/lib/geturl.lib.php';
633 $urlendpoint =
'http://apilayer.net/api/live?access_key='.$key;
635 $urlendpoint .= (empty($conf->global->MULTICURRENCY_APP_SOURCE) ?
'' :
'&source='.$conf->global->MULTICURRENCY_APP_SOURCE);
637 dol_syslog(
"Call url endpoint ".$urlendpoint);
639 $resget =
getURLContent($urlendpoint,
'GET',
'', 1, array(), array(
'http',
'https'), 1);
641 if ($resget[
'content']) {
642 $response = $resget[
'content'];
643 $response = json_decode($response);
645 if ($response->success)
647 $TRate = $response->quotes;
648 $timestamp = $response->timestamp;
650 if (self::recalculRates($TRate) >= 0)
652 foreach ($TRate as $currency_code => $rate)
654 $code = substr($currency_code, 3, 3);
656 if ($obj->fetch(null, $code) > 0)
658 $obj->updateRate($rate);
659 } elseif ($addifnotfound)
661 self::addRateFromDolibarr($code, $rate);
668 dol_syslog(
"Failed to call endpoint ".$response->error->info, LOG_WARNING);
669 setEventMessages($langs->trans(
'multicurrency_syncronize_error', $response->error->info), null,
'errors');
687 if ($currency->fetch(
'', $code) > 0)
return true;
701 public $element =
'multicurrency_rate';
706 public $table_element =
'multicurrency_rate';
726 public $fk_multicurrency;
753 public function create($fk_multicurrency, $trigger =
true)
757 dol_syslog(
'CurrencyRate::create', LOG_DEBUG);
761 if (empty($this->entity) || $this->entity <= 0) $this->entity = $conf->entity;
762 $now = empty($this->date_sync) ?
dol_now() : $this->date_sync;
765 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.
'(';
767 $sql .=
' date_sync,';
768 $sql .=
' fk_multicurrency,';
770 $sql .=
') VALUES (';
771 $sql .=
' '.$this->rate.
',';
772 $sql .=
" '".$this->db->idate($now).
"',";
773 $sql .=
" ".((int) $fk_multicurrency).
",";
774 $sql .=
" ".((int) $this->entity);
783 $this->errors[] =
'Error '.$this->db->lasterror();
784 dol_syslog(
'CurrencyRate::create '.join(
',', $this->errors), LOG_ERR);
788 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
789 $this->fk_multicurrency = $fk_multicurrency;
790 $this->date_sync = $now;
793 $result = $this->
call_trigger(
'CURRENCYRATE_CREATE', $user);
794 if ($result < 0) $error++;
799 $this->
db->rollback();
819 $sql =
'SELECT cr.rowid, cr.rate, cr.date_sync, cr.fk_multicurrency, cr.entity';
820 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' AS cr';
821 $sql .=
' WHERE cr.rowid = '.$id;
826 $numrows = $this->
db->num_rows(
$resql);
828 $obj = $this->
db->fetch_object(
$resql);
830 $this->
id = $obj->rowid;
831 $this->rate = $obj->rate;
832 $this->date_sync = $this->
db->jdate($obj->date_sync);
833 $this->fk_multicurrency = $obj->fk_multicurrency;
834 $this->entity = $obj->entity;
844 $this->errors[] =
'Error '.$this->db->lasterror();
845 dol_syslog(
'CurrencyRate::fetch '.join(
',', $this->errors), LOG_ERR);
863 dol_syslog(
'CurrencyRate::update', LOG_DEBUG);
868 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element.
' SET';
869 $sql .=
' rate='.$this->rate;
870 if (!empty($this->date_sync)) $sql .=
", date_sync='".$this->db->idate($this->date_sync).
"'";
871 if (!empty($this->fk_multicurrency)) $sql .=
', fk_multicurrency='.$this->fk_multicurrency;
872 $sql .=
' WHERE rowid='.$this->id;
880 $this->errors[] =
'Error '.$this->db->lasterror();
881 dol_syslog(
'CurrencyRate::update '.join(
',', $this->errors), LOG_ERR);
884 if (!$error && $trigger) {
885 $result = $this->
call_trigger(
'CURRENCYRATE_MODIFY', $user);
886 if ($result < 0) $error++;
891 $this->
db->rollback();
907 public function delete($trigger =
true)
911 dol_syslog(
'CurrencyRate::delete', LOG_DEBUG);
918 $result = $this->
call_trigger(
'CURRENCYRATE_DELETE', $user);
919 if ($result < 0) $error++;
923 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
924 $sql .=
' WHERE rowid='.$this->id;
930 $this->errors[] =
'Error '.$this->db->lasterror();
931 dol_syslog(
'CurrencyRate::delete '.join(
',', $this->errors), LOG_ERR);
937 $this->
db->rollback();
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
getRate()
Fetch CurrencyRate object in $this->rate.
static getIdAndTxFromCode($db, $code, $date_document= '')
Get id and rate of currency from code.
__construct(DoliDB $db)
Constructor.
fetchAllCurrencyRate()
Load all rates in object from the database.
dol_now($mode= 'auto')
Return date for now.
static recalculRates(&$TRate)
With free account we can't set source then recalcul all rates to force another source.
Class to manage Dolibarr users.
Class to manage Dolibarr database access.
$conf db name
Only used if Module[ID]Name translation string is not found.
getURLContent($url, $postorget= 'GET', $param= '', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0)
Function to get a content from an URL (use proxy if proxy defined).
$conf db
API class for accounts.
static checkCodeAlreadyExists($code)
Check in database if the current code already exists.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
create(User $user, $trigger=true)
Create object into database.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
static syncRates($key, $addifnotfound=0)
Sync rates from API.
create($fk_multicurrency, $trigger=true)
Create object into database.
updateRate($rate)
Add new entry into llx_multicurrency_rate.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
static getIdFromCode($db, $code)
Get id of currency from code.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update($trigger=true)
Update object into database.
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
static getAmountConversionFromInvoiceRate($fk_facture, $amount, $way= 'dolibarr', $table= 'facture')
Get the conversion of amount with invoice rate.
deleteRates()
Delete rates in database.
fetch($id, $code=null)
Load object in memory from the database.
update(User $user, $trigger=true)
Update object into database.
addRateFromDolibarr($code, $rate)
Try get label of code in llx_currency then add rate.
call_trigger($triggerName, $user)
Call trigger based on this instance.
addRate($rate)
Add a Rate into database.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
fetch($id)
Load object in memory from the database.
__construct(DoliDB $db)
Constructor.
static getInvoiceRate($fk_facture, $table= 'facture')
Get current invoite rate.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)