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)) {
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);
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);
501 public static function getIdFromCode($dbs, $code)
505 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."multicurrency WHERE code = '".$dbs->escape($code)."' AND entity = ".((int) $conf->entity);
507 dol_syslog(__METHOD__, LOG_DEBUG);
508 $resql = $dbs->query($sql);
509 if ($resql && $obj = $dbs->fetch_object($resql)) {
526 public static function getIdAndTxFromCode($dbs, $code, $date_document = '')
530 $sql1 = "SELECT m.rowid, mc.rate FROM ".MAIN_DB_PREFIX."multicurrency m";
532 $sql1 .= ' LEFT JOIN
'.MAIN_DB_PREFIX.'multicurrency_rate mc ON (m.rowid = mc.fk_multicurrency)
';
533 $sql1 .= " WHERE m.code = '".$dbs->escape($code)."'";
534 $sql1 .= " AND m.entity IN (".getEntity('multicurrency
').")";
536 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE) && !empty($date_document)) { // Use last known rate compared to document date
537 $tmparray = dol_getdate($date_document);
538 $sql2 .= " AND mc.date_sync <= '".$dbs->idate(dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year'], true))."'";
540 $sql3 = " ORDER BY mc.date_sync DESC LIMIT 1";
542 dol_syslog(__METHOD__, LOG_DEBUG);
543 $resql = $dbs->query($sql1.$sql2.$sql3);
545 if ($resql && $obj = $dbs->fetch_object($resql)) {
546 return array($obj->rowid, $obj->rate);
548 if (!empty($conf->global->MULTICURRENCY_USE_RATE_ON_DOCUMENT_DATE)) {
549 $resql = $dbs->query($sql1.$sql3);
550 if ($resql && $obj = $dbs->fetch_object($resql)) {
551 return array($obj->rowid, $obj->rate);
568 public static function getAmountConversionFromInvoiceRate($fk_facture, $amount, $way = 'dolibarr
', $table = 'facture
')
570 $multicurrency_tx = self::getInvoiceRate($fk_facture, $table);
572 if ($multicurrency_tx) {
573 if ($way == 'dolibarr
') {
574 return price2num($amount * $multicurrency_tx, 'MU
');
576 return price2num($amount / $multicurrency_tx, 'MU
');
590 public static function getInvoiceRate($fk_facture, $table = 'facture
')
594 $sql = "SELECT multicurrency_tx FROM ".MAIN_DB_PREFIX.$table." WHERE rowid = ".((int) $fk_facture);
596 dol_syslog(__METHOD__, LOG_DEBUG);
597 $resql = $db->query($sql);
598 if ($resql && ($line = $db->fetch_object($resql))) {
599 return $line->multicurrency_tx;
612 public function recalculRates(&$TRate)
616 if ($conf->currency != getDolGlobalString('MULTICURRENCY_APP_SOURCE
')) {
617 $alternate_source = 'USD
'.$conf->currency;
618 if (!empty($TRate->$alternate_source)) {
619 $coef = $TRate->USDUSD / $TRate->$alternate_source;
620 foreach ($TRate as $attr => &$rate) {
627 return -1; // Alternate souce not found
630 return 0; // Nothing to do
640 public function syncRates($key, $addifnotfound = 0)
642 global $conf, $db, $langs;
644 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php
';
646 $urlendpoint = 'http:
647 $urlendpoint .=
'&source=' . (empty($conf->global->MULTICURRENCY_APP_SOURCE) ?
'USD' : $conf->global->MULTICURRENCY_APP_SOURCE);
649 dol_syslog(
"Call url endpoint ".$urlendpoint);
653 if ($resget[
'content']) {
654 $response = $resget[
'content'];
655 $response = json_decode($response);
657 if ($response->success) {
658 $TRate = $response->quotes;
662 foreach ($TRate as $currency_code => $rate) {
663 $code = substr($currency_code, 3, 3);
665 if ($obj->fetch(
null, $code) > 0) {
666 $obj->updateRate($rate);
667 } elseif ($addifnotfound) {
675 dol_syslog(
"Failed to call endpoint ".$response->error->info, LOG_WARNING);
676 setEventMessages($langs->trans(
'multicurrency_syncronize_error', $response->error->info),
null,
'errors');
692 if ($currencytmp->fetch(
'', $code) > 0) {
709 public $element =
'multicurrency_rate';
714 public $table_element =
'multicurrency_rate';
734 public $fk_multicurrency;
761 public function create($fk_multicurrency, $trigger =
true)
765 dol_syslog(
'CurrencyRate::create', LOG_DEBUG);
769 if (empty($this->entity) || $this->entity <= 0) {
770 $this->entity = $conf->entity;
772 $now = empty($this->date_sync) ?
dol_now() : $this->date_sync;
775 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.$this->table_element.
"(";
777 $sql .=
' date_sync,';
778 $sql .=
' fk_multicurrency,';
780 $sql .=
') VALUES (';
781 $sql .=
' '.((float) $this->rate).
',';
782 $sql .=
" '".$this->db->idate($now).
"',";
783 $sql .=
" ".((int) $fk_multicurrency).
",";
784 $sql .=
" ".((int) $this->entity);
793 $this->errors[] =
'Error '.$this->db->lasterror();
794 dol_syslog(
'CurrencyRate::create '.join(
',', $this->errors), LOG_ERR);
798 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
799 $this->fk_multicurrency = $fk_multicurrency;
800 $this->date_sync = $now;
803 $result = $this->
call_trigger(
'CURRENCYRATE_CREATE', $user);
811 $this->
db->rollback();
831 $sql =
"SELECT cr.rowid, cr.rate, cr.date_sync, cr.fk_multicurrency, cr.entity";
832 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element.
" AS cr";
833 $sql .=
" WHERE cr.rowid = ".((int) $id);
838 $numrows = $this->
db->num_rows(
$resql);
840 $obj = $this->
db->fetch_object(
$resql);
842 $this->
id = $obj->rowid;
843 $this->rate = $obj->rate;
844 $this->date_sync = $this->
db->jdate($obj->date_sync);
845 $this->fk_multicurrency = $obj->fk_multicurrency;
846 $this->entity = $obj->entity;
856 $this->errors[] =
'Error '.$this->db->lasterror();
857 dol_syslog(
'CurrencyRate::fetch '.join(
',', $this->errors), LOG_ERR);
875 dol_syslog(
'CurrencyRate::update', LOG_DEBUG);
880 $sql =
"UPDATE ".MAIN_DB_PREFIX.$this->table_element;
881 $sql .=
"SET rate = ".((float) $this->rate);
882 if (!empty($this->date_sync)) {
883 $sql .=
", date_sync = '".$this->db->idate($this->date_sync).
"'";
885 if (!empty($this->fk_multicurrency)) {
886 $sql .=
', fk_multicurrency = '.((int) $this->fk_multicurrency);
888 $sql .=
" WHERE rowid =".((int) $this->
id);
896 $this->errors[] =
'Error '.$this->db->lasterror();
897 dol_syslog(
'CurrencyRate::update '.join(
',', $this->errors), LOG_ERR);
900 if (!$error && $trigger) {
901 $result = $this->
call_trigger(
'CURRENCYRATE_MODIFY', $user);
909 $this->
db->rollback();
925 public function delete($trigger =
true)
929 dol_syslog(
'CurrencyRate::delete', LOG_DEBUG);
936 $result = $this->
call_trigger(
'CURRENCYRATE_DELETE', $user);
943 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
944 $sql .=
' WHERE rowid='.((int) $this->
id);
950 $this->errors[] =
'Error '.$this->db->lasterror();
951 dol_syslog(
'CurrencyRate::delete '.join(
',', $this->errors), LOG_ERR);
957 $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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
dol_now($mode='auto')
Return date for now.
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.
$conf db
API class for accounts.
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...