New add sync with api to create/update all rates

This commit is contained in:
phf 2016-03-10 23:52:06 +01:00
parent 4b0b1fb5f6
commit da17e6e676
2 changed files with 106 additions and 8 deletions

View File

@ -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,17 @@ if (! $user->admin) {
// Parameters
$action = GETPOST('action', 'alpha');
$sync_response = GETPOST('sync_response');
/*
* Actions
*/
if (!empty($sync_response))
{
$sync_response = json_decode($sync_response);
MultiCurrency::syncRates($sync_response);
exit;
}
if (preg_match('/set_(.*)/',$action,$reg))
{
$code=$reg[1];
@ -64,7 +72,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
@ -224,8 +232,8 @@ print '<td align="center" width="20">&nbsp;</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_CURRENCY_APP_ID">';
print '<input type="text" name="CURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
print '<input type="hidden" name="action" value="set_MULTICURRENCY_APP_ID">';
print '<input type="text" name="MULTICURRENCY_APP_ID" value="'.$conf->global->MULTICURRENCY_APP_ID.'" size="28" />&nbsp;';
print '<input type="submit" class="button" value="'.$langs->trans("Modify").'">';
print '</form>';
print '</td></tr>';
@ -244,16 +252,95 @@ print '</form>';
print '</td></tr>';
print '</table>';
print '</form>';
print '<br />';
print '<script type="text/javascript">
function getXMLHttpRequest()
{
var xhr = null;
if (window.XMLHttpRequest || window.ActiveXObject)
{
if (window.ActiveXObject)
{
try
{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else
{
xhr = new XMLHttpRequest();
}
}
else
{
if (typeof $ !== "undefined") $.jnotify("'.$langs->transnoentitiesnoconv('multicurrency_error_browser_incompatible').'", "error");
else alert("'.$langs->transnoentitiesnoconv('multicurremulticurrency_error_browser_incompatiblency_syncronize_error').'");
return null;
}
return xhr;
}
function request(url, callback)
{
var xhr = getXMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0))
{
callback(xhr.responseText);
}
};
xhr.open("GET", url, true);
xhr.send(null)
}
function syncronize_rates()
{
var url_sync = "http://apilayer.net/api/live?access_key='.$conf->global->MULTICURRENCY_APP_ID.'&format=1";
request(url_sync, update_rates);
}
function update_rates(responseText)
{
var response = JSON.parse(responseText);
if (response.success)
{
var url = "multicurrency.php?sync_response="+JSON.stringify(response);
request(url, reloadpage);
}
else
{
if (typeof $ !== "undefined") $.jnotify("'.$langs->transnoentitiesnoconv('multicurrency_syncronize_error').': "+response.error.info, "error");
else alert("'.$langs->transnoentitiesnoconv('multicurrency_syncronize_error').': "+response.error.info);
}
}
function reloadpage(responseText)
{
var response = JSON.parse(responseText);
console.log(response);
}
</script>';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Currencies").'</td>'."\n";
print '<td align="center" width="20">&nbsp;</td>';
print '<td align="center" width="100">'.$langs->trans("Rate").'</td>'."\n";
print '<td align="right" width="100">'.$langs->trans("Rate").'&nbsp;<input type="button" class="button" onclick="javascript:syncronize_rates();" value="'.$langs->trans('Synchronize').'" /></td>'."\n";
$var=!$var;
print '<tr '.$bc[$var].'>';

View File

@ -520,6 +520,17 @@ class MultiCurrency extends CommonObject
return false;
}
/**
* Sync rates from api
*
* @param array $response array of reponse from api to sync dolibarr rates
*/
public static function syncRates($response)
{
$TRate = $response['quotes'];
$timestamp = $response['timestamp'];
}
}
/**