From 4243b4ff57d66d390d6eeb1b30584d50b12df7db Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Wed, 29 May 2013 15:58:30 +0200 Subject: [PATCH] Uniform display of exchange rate across aplication, added xr_provider_authoritative option, fixed bug in stored rate value. --- config.default.php | 6 ++++++ gl/includes/db/gl_db_rates.inc | 12 +++++++---- gl/includes/ui/gl_bank_ui.inc | 2 +- includes/ui/ui_view.inc | 30 +++++++++++++++------------ purchasing/includes/ui/grn_ui.inc | 2 +- purchasing/includes/ui/invoice_ui.inc | 2 +- purchasing/includes/ui/po_ui.inc | 2 +- sales/includes/ui/sales_credit_ui.inc | 2 +- sales/includes/ui/sales_order_ui.inc | 2 +- 9 files changed, 37 insertions(+), 23 deletions(-) diff --git a/config.default.php b/config.default.php index b0e8cc55..7067ae78 100644 --- a/config.default.php +++ b/config.default.php @@ -281,6 +281,12 @@ $login_max_attempts = 10; $xr_providers = array("ECB", "YAHOO", "GOOGLE", "BLOOMBERG"); $dflt_xr_provider = 0; +/* + Set to true when remote service is authoritative source of exchange rates, and can be stored automatically without + manual edition. Otherwise exrate is stored on first new currency transaction of the day. +*/ +$xr_provider_authoritative = false; + /* Optional sorting sales documents lines during edition according to item code */ diff --git a/gl/includes/db/gl_db_rates.inc b/gl/includes/db/gl_db_rates.inc index 1d358ec8..4988ff4c 100644 --- a/gl/includes/db/gl_db_rates.inc +++ b/gl/includes/db/gl_db_rates.inc @@ -45,7 +45,7 @@ function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate) $sql = "UPDATE ".TB_PREF."exchange_rates SET rate_buy=$buy_rate, rate_sell=".db_escape($sell_rate) ." WHERE curr_code=".db_escape($curr_code)." AND date_='$date'"; - db_query($sql, "could not add exchange rate for $curr_code"); + db_query($sql, "could not add exchange rate for $curr_code"); } //--------------------------------------------------------------------------------------------- @@ -60,7 +60,7 @@ function add_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate) $sql = "INSERT INTO ".TB_PREF."exchange_rates (curr_code, date_, rate_buy, rate_sell) VALUES (".db_escape($curr_code).", '$date', ".db_escape($buy_rate) .", ".db_escape($sell_rate).")"; - db_query($sql, "could not add exchange rate for $curr_code"); + db_query($sql, "could not add exchange rate for $curr_code"); } //--------------------------------------------------------------------------------------------- @@ -71,7 +71,7 @@ function add_new_exchange_rate($curr_code, $date_, $ex_rate) return; if (!get_date_exchange_rate($curr_code, $date_)) - add_exchange_rate($curr_code, $date_, 1.0/$ex_rate, 1.0/$ex_rate); + add_exchange_rate($curr_code, $date_, $ex_rate, $ex_rate); } //--------------------------------------------------------------------------------------------- @@ -79,12 +79,16 @@ function add_new_exchange_rate($curr_code, $date_, $ex_rate) function delete_exchange_rate($rate_id) { $sql = "DELETE FROM ".TB_PREF."exchange_rates WHERE id=".db_escape($rate_id); - db_query($sql, "could not delete exchange rate $rate_id"); + db_query($sql, "could not delete exchange rate $rate_id"); } //----------------------------------------------------------------------------- // Retrieve exchange rate as of date $date from external source (usually inet) // +// Exchange rate for currency revaluation purposes is defined in FA as home_currency/curr_b ratio i.e. +// +// amount [home] = amount [curr] * ex_rate +// function retrieve_exrate($curr_b, $date) { global $xr_providers, $dflt_xr_provider; diff --git a/gl/includes/ui/gl_bank_ui.inc b/gl/includes/ui/gl_bank_ui.inc index fbbf7708..7ea96e1f 100644 --- a/gl/includes/ui/gl_bank_ui.inc +++ b/gl/includes/ui/gl_bank_ui.inc @@ -123,7 +123,7 @@ function display_bank_header(&$order) $bank_currency = get_bank_account_currency($_POST['bank_account']); - exchange_rate_display($bank_currency, get_company_currency(), $_POST['date_']); + exchange_rate_display(get_company_currency(), $bank_currency, $_POST['date_']); end_outer_table(1); // outer table diff --git a/includes/ui/ui_view.inc b/includes/ui/ui_view.inc index 492bd486..f382fba5 100644 --- a/includes/ui/ui_view.inc +++ b/includes/ui/ui_view.inc @@ -272,14 +272,14 @@ function check_ui_refresh($name=null) // function exchange_rate_display($from_currency, $to_currency, $date_, $force_edit=false) { - global $Ajax; + global $Ajax, $xr_provider_authoritative; $readonly = false; if ($from_currency != $to_currency) { $rate = get_post('_ex_rate'); - if (!$rate || check_ui_refresh()) { // readonly or ui context changed + if (check_ui_refresh() || !$rate) { // readonly or ui context changed $comp_currency = get_company_currency(); if ($from_currency == $comp_currency) $currency = $to_currency; @@ -287,27 +287,31 @@ function exchange_rate_display($from_currency, $to_currency, $date_, $force_edit $currency = $from_currency; $rate = get_date_exchange_rate($currency, $date_); // try local + if ($rate) - $readonly = true; + $readonly = true; // if we have already local exrate keep it unchanged + if (!$rate) { // retry from remote service $row = get_currency($currency); - if ($row['auto_update']) - $rate = retrieve_exrate($currency, $date_); - if ($rate) - add_new_exchange_rate($currency, $date_, $rate); - else - { - display_warning(sprintf(_("Cannot retrieve exchange rate for currency %s. Please adjust approximate rate if needed."), $currency)); - $readonly = false; + + if ($row['auto_update']) // autoupdate means use remote service & store exrate on first transaction. + { + $rate = retrieve_exrate($currency, $date_); + if (!$rate) + display_warning(sprintf(_("Cannot retrieve exchange rate for currency %s. Please adjust approximate rate if needed."), $currency)); + elseif ($xr_provider_authoritative) { + // if the remote exrate is considered authoritative we can store the rate here, + // otherwise exrate will be stored during transaction write + $readonly = true; + add_new_exchange_rate($currency, $date_, $rate); + } } } if (!$rate) { // get and edit latest available - $readonly = false; $rate = get_exchange_rate_from_home_currency($currency, $date_); } if ($from_currency != $comp_currency) $rate = 1 / ($rate / get_exchange_rate_from_home_currency($to_currency, $date_)); - $Ajax->activate('_ex_rate_span'); } diff --git a/purchasing/includes/ui/grn_ui.inc b/purchasing/includes/ui/grn_ui.inc index d0db38f3..059c7bd1 100644 --- a/purchasing/includes/ui/grn_ui.inc +++ b/purchasing/includes/ui/grn_ui.inc @@ -84,7 +84,7 @@ function edit_grn_summary(&$po) label_row(_("Order Comments"), $po->Comments, "class='tableheader2'", "colspan=9"); if (!is_company_currency($po->curr_code)) - exchange_rate_display($po->curr_code, get_company_currency(), get_post('DefaultReceivedDate')); + exchange_rate_display(get_company_currency(), $po->curr_code, get_post('DefaultReceivedDate')); end_outer_table(1); } diff --git a/purchasing/includes/ui/invoice_ui.inc b/purchasing/includes/ui/invoice_ui.inc index 3421d1fb..aae151d3 100644 --- a/purchasing/includes/ui/invoice_ui.inc +++ b/purchasing/includes/ui/invoice_ui.inc @@ -137,7 +137,7 @@ function invoice_header(&$supp_trans) if ($supplier_currency != $company_currency) { label_row(_("Supplier's Currency:"), "" . $supplier_currency . ""); - exchange_rate_display($supplier_currency, $company_currency, $_POST['tran_date']); + exchange_rate_display($company_currency, $supplier_currency, $_POST['tran_date']); } label_row(_("Tax Group:"), $supp_trans->tax_description); diff --git a/purchasing/includes/ui/po_ui.inc b/purchasing/includes/ui/po_ui.inc index 5f09dc10..728d2e7c 100644 --- a/purchasing/includes/ui/po_ui.inc +++ b/purchasing/includes/ui/po_ui.inc @@ -166,7 +166,7 @@ function display_po_header(&$order) if (!is_company_currency($order->curr_code)) { label_row(_("Supplier Currency:"), $order->curr_code); - exchange_rate_display($order->curr_code, get_company_currency(), + exchange_rate_display(get_company_currency(), $order->curr_code, $_POST['OrderDate']); } diff --git a/sales/includes/ui/sales_credit_ui.inc b/sales/includes/ui/sales_credit_ui.inc index b75aac1e..07006fb6 100644 --- a/sales/includes/ui/sales_credit_ui.inc +++ b/sales/includes/ui/sales_credit_ui.inc @@ -92,7 +92,7 @@ function display_credit_header(&$order) table_section(2); label_row(_("Customer Currency:"), $order->customer_currency); - exchange_rate_display($order->customer_currency, get_company_currency(), + exchange_rate_display(get_company_currency(), $order->customer_currency, $_POST['OrderDate']); } table_section(3); diff --git a/sales/includes/ui/sales_order_ui.inc b/sales/includes/ui/sales_order_ui.inc index f58f677c..585434c0 100644 --- a/sales/includes/ui/sales_order_ui.inc +++ b/sales/includes/ui/sales_order_ui.inc @@ -382,7 +382,7 @@ function display_order_header(&$order, $editable, $date_text) if (!is_company_currency($order->customer_currency) && in_array($order->trans_type, array(ST_SALESINVOICE, ST_CUSTDELIVERY))) { label_row(_("Customer Currency:"), $order->customer_currency); - exchange_rate_display($order->customer_currency, get_company_currency(), + exchange_rate_display(get_company_currency(), $order->customer_currency, ($editable ? $_POST['OrderDate'] : $order->document_date)); } -- 2.30.2