From da17e6e676de53dd5dafcc2c2746aa7b136e1f36 Mon Sep 17 00:00:00 2001 From: phf Date: Thu, 10 Mar 2016 23:52:06 +0100 Subject: [PATCH 1/5] New add sync with api to create/update all rates --- htdocs/admin/multicurrency.php | 103 ++++++++++++++++-- .../class/multicurrency.class.php | 11 ++ 2 files changed, 106 insertions(+), 8 deletions(-) diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index b1ad347d8b7..afb396a0217 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,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 ' '; print ''; print '
'; print ''; -print ''; -print ' '; +print ''; +print ' '; print ''; print '
'; print ''; @@ -244,16 +252,95 @@ print ''; print ''; print ''; - -print ''; - print '
'; + +print ''; + + print ''; print ''; print ''."\n"; print ''; -print ''."\n"; +print ''."\n"; $var=!$var; print ''; diff --git a/htdocs/multicurrency/class/multicurrency.class.php b/htdocs/multicurrency/class/multicurrency.class.php index fd6da3816ea..c78e7784ad1 100644 --- a/htdocs/multicurrency/class/multicurrency.class.php +++ b/htdocs/multicurrency/class/multicurrency.class.php @@ -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']; + } } /** From 3134af5013984bb356716e9254891788f4312ef4 Mon Sep 17 00:00:00 2001 From: phf Date: Fri, 11 Mar 2016 23:42:02 +0100 Subject: [PATCH 2/5] Fix sync with currencylayer --- htdocs/admin/multicurrency.php | 84 ++++++++++------ .../class/multicurrency.class.php | 95 +++++++++++++++++-- 2 files changed, 142 insertions(+), 37 deletions(-) diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index afb396a0217..e4f4fc34301 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -225,32 +225,6 @@ print '' print ''; print ''; -$var=!$var; -print ''; -print ''; -print ''; -print ''; - -$var=!$var; -print ''; -print ''; -print ''; -print ''; - print '
'.$langs->trans("Currencies").' '.$langs->trans("Rate").''.$langs->trans("Rate").' 
'.$langs->transnoentitiesnoconv("multicurrency_appId").' '; -print '
'; -print ''; -print ''; -print ' '; -print ''; -print '
'; -print '
'.$langs->transnoentitiesnoconv("multicurrency_currencyFromToRate").' '; -print '
'; -print ''; -print ''; -print ' '; // CURRENCY_BASE - CURRENCY_ENTITY - ID_ENTITY -print ''; -print '
'; -print '
'; print '
'; @@ -306,7 +280,8 @@ print ''; +$var=false; +print ''; +print ''; +print ''."\n"; +print ''; +print ''."\n"; + +$var=!$var; +print ''; +print ''; +print ''; +print ''; + +$var=!$var; +print ''; +print ''; +print ''; +print ''; + +$var=!$var; +print ''; +print ''; +print ''; +print ''; + +print '
'.$langs->trans("CurrencyLayerAccount").' '.$langs->trans("Value").' 
'.$langs->transnoentitiesnoconv("multicurrency_appId").' '; +print '
'; +print ''; +print ''; +print ' '; +print ''; +print '
'; +print '
'.$langs->transnoentitiesnoconv("multicurrency_appCurrencySource").' '; +print '
'; +print ''; +print ''; +print ' '; // Default: USD +print ''; +print '
'; +print '
'.$langs->transnoentitiesnoconv("multicurrency_alternateCurrencySource").' '; +print '
'; +print ''; +print ''; +print ' '; // Example: EUR +print ''; +print '
'; +print '
'; +print '
'; print ''; print ''; print ''."\n"; print ''; -print ''."\n"; +print ''."\n"; $var=!$var; print ''; diff --git a/htdocs/multicurrency/class/multicurrency.class.php b/htdocs/multicurrency/class/multicurrency.class.php index c78e7784ad1..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,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(); From 0a533660b8834d83a5738e42c6da4e33c4bfdddb Mon Sep 17 00:00:00 2001 From: phf Date: Sat, 12 Mar 2016 21:17:09 +0100 Subject: [PATCH 3/5] Fix travis phpcs --- htdocs/compta/prelevement/create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/prelevement/create.php b/htdocs/compta/prelevement/create.php index 33d02bff05b..c2607d5b83d 100644 --- a/htdocs/compta/prelevement/create.php +++ b/htdocs/compta/prelevement/create.php @@ -65,7 +65,7 @@ if ($action == 'create') $result=$bprev->create($conf->global->PRELEVEMENT_CODE_BANQUE, $conf->global->PRELEVEMENT_CODE_GUICHET); if ($result < 0) { - setEventMessages($bprev->error , $bprev->errors, 'errors'); + setEventMessages($bprev->error, $bprev->errors, 'errors'); } if ($result == 0) { From 661ee72d55a377932068266d271a7e6c88f6ddea Mon Sep 17 00:00:00 2001 From: phf Date: Sun, 20 Mar 2016 15:17:30 +0100 Subject: [PATCH 4/5] UPDATE move ajax request in js file and call a new php file to update all rates --- htdocs/admin/multicurrency.php | 91 +----------------- htdocs/multicurrency/ajax/updaterates.php | 35 +++++++ htdocs/multicurrency/js/currencylayer.js.php | 98 ++++++++++++++++++++ 3 files changed, 136 insertions(+), 88 deletions(-) create mode 100644 htdocs/multicurrency/ajax/updaterates.php create mode 100644 htdocs/multicurrency/js/currencylayer.js.php diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index e4f4fc34301..235b16ba100 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -42,16 +42,10 @@ 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)) { @@ -145,8 +139,9 @@ if ($resql) */ $page_name = "MultiCurrency"; +$morejs = array('/multicurrency/js/currencylayer.js.php'); -llxHeader('', $langs->trans($page_name)); +llxHeader('', $langs->trans($page_name), '', '', '', '', $morejs); // Subheader $linkback = '' @@ -228,86 +223,6 @@ print ''; print '
'.$langs->trans("Currencies").' '.$langs->trans("Rate").' '.$langs->trans("Rate").'
'; print '
'; -print ''; - $var=false; print ''; print ''; diff --git a/htdocs/multicurrency/ajax/updaterates.php b/htdocs/multicurrency/ajax/updaterates.php new file mode 100644 index 00000000000..663e68a647c --- /dev/null +++ b/htdocs/multicurrency/ajax/updaterates.php @@ -0,0 +1,35 @@ + + * Copyright (C) 2016 Pierre-Henry Favre + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK','1'); + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; + +$sync_response = GETPOST('sync_response'); + +if (!empty($sync_response)) +{ + $sync_response = json_decode($sync_response); + MultiCurrency::syncRates($sync_response); +} \ No newline at end of file diff --git a/htdocs/multicurrency/js/currencylayer.js.php b/htdocs/multicurrency/js/currencylayer.js.php new file mode 100644 index 00000000000..0b563aae266 --- /dev/null +++ b/htdocs/multicurrency/js/currencylayer.js.php @@ -0,0 +1,98 @@ + + * Copyright (C) 2016 Pierre-Henry Favre + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +include '../../main.inc.php'; +$langs->load('errors'); + +?> + +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("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>", "error"); + else alert("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>"); + + 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() +{ + document.getElementById("bt_sync").disabled = true; + var url_sync = "http://apilayer.net/api/live?access_key=global->MULTICURRENCY_APP_ID; ?>&format=1global->MULTICURRENCY_APP_SOURCE)) echo '&source='.$conf->global->MULTICURRENCY_APP_SOURCE; ?>"; + request(url_sync, update_rates); +} + +function update_rates(responseText) +{ + var response = JSON.parse(responseText); + if (response.success) + { + var url = "/multicurrency/ajax/updaterates.php?sync_response="+JSON.stringify(response); + request(url, reloadpage); + } + else + { + if (typeof $ !== "undefined") $.jnotify("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info, "error"); + else alert("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info); + } +} + +function reloadpage(responseText) +{ + document.getElementById("bt_sync").disabled = false; + window.location.href = window.location.pathname; +} From a33472942d22e7c6829cc8592fd86e9311192f9a Mon Sep 17 00:00:00 2001 From: phf Date: Sat, 9 Apr 2016 22:12:33 +0200 Subject: [PATCH 5/5] Fix synchronize rates for multicurrency module --- htdocs/admin/multicurrency.php | 48 +++++++++- htdocs/multicurrency/ajax/updaterates.php | 35 ------- htdocs/multicurrency/js/currencylayer.js.php | 98 -------------------- 3 files changed, 44 insertions(+), 137 deletions(-) delete mode 100644 htdocs/multicurrency/ajax/updaterates.php delete mode 100644 htdocs/multicurrency/js/currencylayer.js.php diff --git a/htdocs/admin/multicurrency.php b/htdocs/admin/multicurrency.php index 235b16ba100..e09347099de 100644 --- a/htdocs/admin/multicurrency.php +++ b/htdocs/admin/multicurrency.php @@ -119,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; @@ -139,9 +154,7 @@ if ($resql) */ $page_name = "MultiCurrency"; -$morejs = array('/multicurrency/js/currencylayer.js.php'); - -llxHeader('', $langs->trans($page_name), '', '', '', '', $morejs); +llxHeader('', $langs->trans($page_name)); // Subheader $linkback = '' @@ -228,7 +241,14 @@ print '
'; print ''; print ''."\n"; print ''; -print ''."\n"; +print ''; + $var=!$var; print ''; @@ -311,6 +331,26 @@ foreach ($TCurrency as &$currency) print '
'.$langs->trans("CurrencyLayerAccount").' '.$langs->trans("Value").' '; +print '
'; +print ''; +print ''; +print $langs->trans("Value").' '; +print '
'; +print '
'; + + +print ' + +'; + llxFooter(); $db->close(); \ No newline at end of file diff --git a/htdocs/multicurrency/ajax/updaterates.php b/htdocs/multicurrency/ajax/updaterates.php deleted file mode 100644 index 663e68a647c..00000000000 --- a/htdocs/multicurrency/ajax/updaterates.php +++ /dev/null @@ -1,35 +0,0 @@ - - * Copyright (C) 2016 Pierre-Henry Favre - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal -if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); -if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); -if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); -if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); -if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK','1'); - -require '../../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/multicurrency/class/multicurrency.class.php'; - -$sync_response = GETPOST('sync_response'); - -if (!empty($sync_response)) -{ - $sync_response = json_decode($sync_response); - MultiCurrency::syncRates($sync_response); -} \ No newline at end of file diff --git a/htdocs/multicurrency/js/currencylayer.js.php b/htdocs/multicurrency/js/currencylayer.js.php deleted file mode 100644 index 0b563aae266..00000000000 --- a/htdocs/multicurrency/js/currencylayer.js.php +++ /dev/null @@ -1,98 +0,0 @@ - - * Copyright (C) 2016 Pierre-Henry Favre - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -include '../../main.inc.php'; -$langs->load('errors'); - -?> - -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("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>", "error"); - else alert("transnoentitiesnoconv('multicurrency_error_browser_incompatible'); ?>"); - - 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() -{ - document.getElementById("bt_sync").disabled = true; - var url_sync = "http://apilayer.net/api/live?access_key=global->MULTICURRENCY_APP_ID; ?>&format=1global->MULTICURRENCY_APP_SOURCE)) echo '&source='.$conf->global->MULTICURRENCY_APP_SOURCE; ?>"; - request(url_sync, update_rates); -} - -function update_rates(responseText) -{ - var response = JSON.parse(responseText); - if (response.success) - { - var url = "/multicurrency/ajax/updaterates.php?sync_response="+JSON.stringify(response); - request(url, reloadpage); - } - else - { - if (typeof $ !== "undefined") $.jnotify("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info, "error"); - else alert("transnoentitiesnoconv('multicurrency_syncronize_error'); ?>: "+response.error.info); - } -} - -function reloadpage(responseText) -{ - document.getElementById("bt_sync").disabled = false; - window.location.href = window.location.pathname; -}