Fix sync with currencylayer
This commit is contained in:
parent
da17e6e676
commit
3134af5013
@ -225,32 +225,6 @@ 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_appId").'</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_APP_ID">';
|
||||
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 '</form>';
|
||||
print '</td></tr>';
|
||||
|
||||
$var=!$var;
|
||||
print '<tr '.$bc[$var].'>';
|
||||
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_currencyFromToRate").'</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_FROM_TO_RATE">';
|
||||
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="submit" class="button" value="'.$langs->trans("Modify").'">';
|
||||
print '</form>';
|
||||
print '</td></tr>';
|
||||
|
||||
print '</table>';
|
||||
print '<br />';
|
||||
|
||||
@ -306,7 +280,8 @@ print '<script type="text/javascript">
|
||||
|
||||
function syncronize_rates()
|
||||
{
|
||||
var url_sync = "http://apilayer.net/api/live?access_key='.$conf->global->MULTICURRENCY_APP_ID.'&format=1";
|
||||
document.getElementById("bt_sync").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 : '').'";
|
||||
request(url_sync, update_rates);
|
||||
}
|
||||
|
||||
@ -327,20 +302,67 @@ print '<script type="text/javascript">
|
||||
|
||||
function reloadpage(responseText)
|
||||
{
|
||||
var response = JSON.parse(responseText);
|
||||
console.log(response);
|
||||
|
||||
document.getElementById("bt_sync").disabled = false;
|
||||
window.location.href = window.location.pathname;
|
||||
}
|
||||
|
||||
</script>';
|
||||
|
||||
$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">'.$langs->trans("Value").' <input type="button" id="bt_sync" class="button" onclick="javascript:syncronize_rates();" value="'.$langs->trans('Synchronize').'" /></td>'."\n";
|
||||
|
||||
$var=!$var;
|
||||
print '<tr '.$bc[$var].'>';
|
||||
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="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_APP_ID">';
|
||||
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 '</form>';
|
||||
print '</td></tr>';
|
||||
|
||||
$var=!$var;
|
||||
print '<tr '.$bc[$var].'>';
|
||||
print '<td>'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").'</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_APP_SOURCE">';
|
||||
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 '</form>';
|
||||
print '</td></tr>';
|
||||
|
||||
print '</table>';
|
||||
print '<br />';
|
||||
|
||||
print '<table class="noborder" width="100%">';
|
||||
|
||||
print '<tr class="liste_titre">';
|
||||
print '<td>'.$langs->trans("Currencies").'</td>'."\n";
|
||||
print '<td align="center" width="20"> </td>';
|
||||
print '<td align="right" width="100">'.$langs->trans("Rate").' <input type="button" class="button" onclick="javascript:syncronize_rates();" value="'.$langs->trans('Synchronize').'" /></td>'."\n";
|
||||
print '<td align="center" width="100">'.$langs->trans("Rate").'</td>'."\n";
|
||||
|
||||
$var=!$var;
|
||||
print '<tr '.$bc[$var].'>';
|
||||
|
||||
@ -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,7 +554,37 @@ 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
|
||||
*
|
||||
@ -528,8 +592,27 @@ class MultiCurrency extends CommonObject
|
||||
*/
|
||||
public static function syncRates($response)
|
||||
{
|
||||
$TRate = $response['quotes'];
|
||||
$timestamp = $response['timestamp'];
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -724,7 +807,7 @@ class CurrencyRate extends CommonObjectLine
|
||||
if ($error) {
|
||||
$this->db->rollback();
|
||||
|
||||
return - 1 * $error;
|
||||
return -1 * $error;
|
||||
} else {
|
||||
$this->db->commit();
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user