Merge pull request #4800 from atm-ph/new_multicurrency_sync_rates
New multicurrency sync rates
This commit is contained in:
commit
b71c6a8a54
@ -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.'/core/lib/multicurrency.lib.php';
|
||||||
require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
|
require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php';
|
||||||
|
|
||||||
|
|
||||||
// Translations
|
// Translations
|
||||||
$langs->load("multicurrency");
|
$langs->load("multicurrency");
|
||||||
|
|
||||||
@ -41,10 +42,11 @@ if (! $user->admin) {
|
|||||||
|
|
||||||
// Parameters
|
// Parameters
|
||||||
$action = GETPOST('action', 'alpha');
|
$action = GETPOST('action', 'alpha');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Actions
|
* Actions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
if (preg_match('/set_(.*)/',$action,$reg))
|
if (preg_match('/set_(.*)/',$action,$reg))
|
||||||
{
|
{
|
||||||
$code=$reg[1];
|
$code=$reg[1];
|
||||||
@ -64,7 +66,7 @@ if (preg_match('/del_(.*)/',$action,$reg))
|
|||||||
$code=$reg[1];
|
$code=$reg[1];
|
||||||
if (dolibarr_del_const($db, $code, 0) > 0)
|
if (dolibarr_del_const($db, $code, 0) > 0)
|
||||||
{
|
{
|
||||||
Header("Location: ".$_SERVER["PHP_SELF"]);
|
header("Location: ".$_SERVER["PHP_SELF"]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
else
|
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();
|
$TCurrency = array();
|
||||||
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
|
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'multicurrency WHERE entity = '.$conf->entity;
|
||||||
@ -137,7 +154,6 @@ if ($resql)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
$page_name = "MultiCurrency";
|
$page_name = "MultiCurrency";
|
||||||
|
|
||||||
llxHeader('', $langs->trans($page_name));
|
llxHeader('', $langs->trans($page_name));
|
||||||
|
|
||||||
// Subheader
|
// Subheader
|
||||||
@ -217,37 +233,65 @@ print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">'
|
|||||||
print '</form>';
|
print '</form>';
|
||||||
print '</td></tr>';
|
print '</td></tr>';
|
||||||
|
|
||||||
|
print '</table>';
|
||||||
|
print '<br />';
|
||||||
|
|
||||||
|
$var=false;
|
||||||
|
print '<table class="noborder" width="100%">';
|
||||||
|
print '<tr class="liste_titre">';
|
||||||
|
print '<td>'.$langs->trans("CurrencyLayerAccount").'</td>'."\n";
|
||||||
|
print '<td align="center" width="20"> </td>';
|
||||||
|
print '<td align="right" width="100">';
|
||||||
|
print '<form id="form_sync" action="" method="POST">';
|
||||||
|
print '<input type="hidden" name="action" value="synchronize" />';
|
||||||
|
print '<textarea id="response" class="hideobject" name="response"></textarea>';
|
||||||
|
print $langs->trans("Value").' <input type="button" id="bt_sync" class="button" onclick="javascript:getRates();" value="'.$langs->trans('Synchronize').'" />';
|
||||||
|
print '</form>';
|
||||||
|
print '</td></tr>';
|
||||||
|
|
||||||
|
|
||||||
$var=!$var;
|
$var=!$var;
|
||||||
print '<tr '.$bc[$var].'>';
|
print '<tr '.$bc[$var].'>';
|
||||||
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appId").'</td>';
|
print '<td><a target="_blank" href="https://currencylayer.com">'.$langs->transnoentitiesnoconv("multicurrency_appId").'</a></td>';
|
||||||
print '<td align="center" width="20"> </td>';
|
print '<td align="center" width="20"> </td>';
|
||||||
print '<td align="right" width="400">';
|
print '<td align="right" width="400">';
|
||||||
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
|
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
|
||||||
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
||||||
print '<input type="hidden" name="action" value="set_CURRENCY_APP_ID">';
|
print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_ID">';
|
||||||
print '<input type="text" name="CURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" /> ';
|
print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" /> ';
|
||||||
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
|
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
|
||||||
print '</form>';
|
print '</form>';
|
||||||
print '</td></tr>';
|
print '</td></tr>';
|
||||||
|
|
||||||
$var=!$var;
|
$var=!$var;
|
||||||
print '<tr '.$bc[$var].'>';
|
print '<tr '.$bc[$var].'>';
|
||||||
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_currencyFromToRate").'</td>';
|
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</td>';
|
||||||
print '<td align="center" width="20"> </td>';
|
print '<td align="center" width="20"> </td>';
|
||||||
print '<td align="right" width="400">';
|
print '<td align="right" width="400">';
|
||||||
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
|
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
|
||||||
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
||||||
print '<input type="hidden" name="action" value="set_MULTICURRENCY_FROM_TO_RATE">';
|
print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_SOURCE">';
|
||||||
print '<input type="text" name="MULTICURRENCY_FROM_TO_RATE" value="'.$conf->global->MULTICURRENCY_FROM_TO_RATE.'" size="10" placeholder="USD-EUR-1" /> '; // CURRENCY_BASE - CURRENCY_ENTITY - ID_ENTITY
|
print '<input type="text" name="MULTICURRENCY_APP_SOURCE" value="'.$conf->global->MULTICURRENCY_APP_SOURCE.'" size="10" placeholder="USD" /> '; // Default: USD
|
||||||
|
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
|
||||||
|
print '</form>';
|
||||||
|
print '</td></tr>';
|
||||||
|
|
||||||
|
$var=!$var;
|
||||||
|
print '<tr '.$bc[$var].'>';
|
||||||
|
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").'</td>';
|
||||||
|
print '<td align="center" width="20"> </td>';
|
||||||
|
print '<td align="right" width="400">';
|
||||||
|
print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
|
||||||
|
print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
|
||||||
|
print '<input type="hidden" name="action" value="set_MULTICURRENCY_ALTERNATE_SOURCE">';
|
||||||
|
print '<input type="text" name="MULTICURRENCY_ALTERNATE_SOURCE" value="'.$conf->global->MULTICURRENCY_ALTERNATE_SOURCE.'" size="10" placeholder="EUR" /> '; // Example: EUR
|
||||||
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
|
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
|
||||||
print '</form>';
|
print '</form>';
|
||||||
print '</td></tr>';
|
print '</td></tr>';
|
||||||
|
|
||||||
print '</table>';
|
print '</table>';
|
||||||
|
|
||||||
print '</form>';
|
|
||||||
|
|
||||||
print '<br />';
|
print '<br />';
|
||||||
|
|
||||||
print '<table class="noborder" width="100%">';
|
print '<table class="noborder" width="100%">';
|
||||||
|
|
||||||
print '<tr class="liste_titre">';
|
print '<tr class="liste_titre">';
|
||||||
@ -287,6 +331,26 @@ foreach ($TCurrency as &$currency)
|
|||||||
|
|
||||||
print '</table>';
|
print '</table>';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print '
|
||||||
|
<script type="text/javascript">
|
||||||
|
function getRates()
|
||||||
|
{
|
||||||
|
$("#bt_sync").attr("disabled", true);
|
||||||
|
var url_sync = "http://apilayer.net/api/live?access_key='.$conf->global->MULTICURRENCY_APP_ID.'&format=1'.(!empty($conf->global->MULTICURRENCY_APP_SOURCE) ? '&source='.$conf->global->MULTICURRENCY_APP_SOURCE : '').'";
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: url_sync,
|
||||||
|
dataType: "jsonp"
|
||||||
|
}).done(function(response) {
|
||||||
|
$("#response").val(JSON.stringify(response));
|
||||||
|
$("#form_sync").submit();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
';
|
||||||
|
|
||||||
llxFooter();
|
llxFooter();
|
||||||
|
|
||||||
$db->close();
|
$db->close();
|
||||||
@ -383,7 +383,7 @@ class MultiCurrency extends CommonObject
|
|||||||
*
|
*
|
||||||
* @param double $rate rate value
|
* @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)
|
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
|
* Update rate in database
|
||||||
*
|
*
|
||||||
@ -520,6 +554,66 @@ class MultiCurrency extends CommonObject
|
|||||||
|
|
||||||
return false;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user