diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index b1ad347d8b7..e09347099de 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -31,6 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/multicurrency.lib.php'; require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; + // Translations $langs->load("multicurrency"); @@ -41,10 +42,11 @@ if (! $user->admin) { // Parameters $action = GETPOST('action', 'alpha'); - /* * Actions */ + + if (preg_match('/set_(.*)/',$action,$reg)) { $code=$reg[1]; @@ -64,7 +66,7 @@ if (preg_match('/del_(.*)/',$action,$reg)) $code=$reg[1]; if (dolibarr_del_const($db, $code, 0) > 0) { - Header("Location: ".$_SERVER["PHP_SELF"]); + header("Location: ".$_SERVER["PHP_SELF"]); exit; } else @@ -117,6 +119,21 @@ elseif ($action == 'update_currency') } } } +elseif ($action == 'synchronize') +{ + $response = GETPOST('response'); + $response = json_decode($response); + + if ($response->success) + { + MultiCurrency::syncRates($response); + } + else + { + setEventMessages($langs->trans('multicurrency_syncronize_error', $reponse->error->info), null, 'errors'); + } +} + $TCurrency = array(); $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity; @@ -137,7 +154,6 @@ if ($resql) */ $page_name = "MultiCurrency"; - llxHeader('', $langs->trans($page_name)); // Subheader @@ -217,37 +233,65 @@ print '' print ''; print ''; +print ''; +print '
'; + +$var=false; +print ''; +print ''; +print ''."\n"; +print ''; +print ''; + + $var=!$var; print ''; -print ''; +print ''; print ''; print ''; $var=!$var; print ''; -print ''; +print ''; print ''; print ''; + +$var=!$var; +print ''; +print ''; +print ''; +print ''; print '
'.$langs->trans("CurrencyLayerAccount").' '; +print '
'; +print ''; +print ''; +print $langs->trans("Value").' '; +print '
'; +print '
'.$langs->transnoentitiesnoconv("multicurrency_appId").''.$langs->transnoentitiesnoconv("multicurrency_appId").' '; print '
'; print ''; -print ''; -print ' '; +print ''; +print ' '; print ''; print '
'; print '
'.$langs->transnoentitiesnoconv("multicurrency_currencyFromToRate").''.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").' '; print '
'; print ''; -print ''; -print ' '; // CURRENCY_BASE - CURRENCY_ENTITY - ID_ENTITY +print ''; +print ' '; // Default: USD +print ''; +print '
'; +print '
'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").' '; +print '
'; +print ''; +print ''; +print ' '; // Example: EUR print ''; print '
'; print '
'; - -print ''; - print '
'; + print ''; print ''; @@ -287,6 +331,26 @@ foreach ($TCurrency as &$currency) print '
'; + + +print ' + +'; + llxFooter(); $db->close(); \ No newline at end of file diff --git a/htdocs/multicurrency/class/multicurrency.class.php b/htdocs/multicurrency/class/multicurrency.class.php index fd6da3816ea..36c392b5930 100644 --- a/htdocs/multicurrency/class/multicurrency.class.php +++ b/htdocs/multicurrency/class/multicurrency.class.php @@ -208,7 +208,7 @@ class MultiCurrency extends CommonObject $this->errors[] = 'Error ' . $this->db->lasterror(); dol_syslog('Currency::fetch ' . join(',', $this->errors), LOG_ERR); - return - 1; + return -1; } } @@ -383,7 +383,7 @@ class MultiCurrency extends CommonObject * * @param double $rate rate value * - * @return bool false if KO, true if OK + * @return int -1 if KO, 1 if OK */ public function addRate($rate) { @@ -402,6 +402,40 @@ class MultiCurrency extends CommonObject } } + /** + * Try get label of code in llx_currency then add rate + * + * @param string $code currency code + * @param double $rate new rate + * + * @return int -1 if KO, 1 if OK, 2 if label found and OK + */ + function addRateFromDolibarr($code, $rate) + { + global $db, $user; + + $currency = new MultiCurrency($db); + $currency->code = $code; + $currency->name = $code; + + $sql = 'SELECT label FROM '.MAIN_DB_PREFIX.'c_currencies WHERE code_iso = "'.$db->escape($code).'"'; + $resql = $db->query($sql); + if ($resql && ($line = $db->fetch_object($resql))) + { + $currency->name = $line->label; + } + + if ($currency->create($user) > 0) + { + $currency->addRate($rate); + + if (!empty($line)) return 2; + else return 1; + } + + return -1; + } + /** * Update rate in database * @@ -520,6 +554,66 @@ class MultiCurrency extends CommonObject return false; } + + /** + * With free account we can't set source then recalcul all rates to force another source + * + * @param stdClass $TRate Object containing all currencies rates + * @return -1 if KO, 0 if nothing, 1 if OK + */ + public static function recalculRates(&$TRate) + { + global $conf; + + if (!empty($conf->global->MULTICURRENCY_ALTERNATE_SOURCE)) + { + $alternate_source = 'USD'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE; + if (!empty($TRate->{$alternate_source})) + { + $coef = $TRate->USDUSD / $TRate->{$alternate_source}; + foreach ($TRate as $attr => &$rate) + { + $rate *= $coef; + } + + return 1; + } + + return -1; // Alternate souce not found + } + + return 0; // Nothing to do + } + + /** + * Sync rates from api + * + * @param array $response array of reponse from api to sync dolibarr rates + */ + public static function syncRates($response) + { + global $db,$conf; + + $TRate = $response->quotes; + $timestamp = $response->timestamp; + + if (self::recalculRates($TRate) >= 0) + { + foreach ($TRate as $currency_code => $rate) + { + $code = substr($currency_code, 3, 3); + $obj = new MultiCurrency($db); + if ($obj->fetch(null, $code) > 0) + { + $obj->updateRate($rate); + } + else + { + self::addRateFromDolibarr($code, $rate); + } + } + } + } } /** @@ -713,7 +807,7 @@ class CurrencyRate extends CommonObjectLine if ($error) { $this->db->rollback(); - return - 1 * $error; + return -1 * $error; } else { $this->db->commit();