Uniform display of exchange rate across aplication, added xr_provider_authoritative...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 29 May 2013 13:58:30 +0000 (15:58 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 10 Jun 2013 17:00:10 +0000 (19:00 +0200)
config.default.php
gl/includes/db/gl_db_rates.inc
gl/includes/ui/gl_bank_ui.inc
includes/ui/ui_view.inc
purchasing/includes/ui/grn_ui.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/includes/ui/po_ui.inc
sales/includes/ui/sales_credit_ui.inc
sales/includes/ui/sales_order_ui.inc

index b0e8cc551fb649fa4d274d2c30706db2b6e06f27..7067ae786a7d83cbca5ceee37245da6749503657 100644 (file)
@@ -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
 */
index 1d358ec8ac501d6daf80e68cb1e808fa919d6339..4988ff4c6632cb02ec52fd50463926f2d84156a6 100644 (file)
@@ -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;
index fbbf770851c8e847ddeaa3d0539b88846e7cf43b..7ea96e1f6c4f53de415b714c98d4bb6f9222c43c 100644 (file)
@@ -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
 
index 492bd48634523ac838984c7b30e5401683b91e40..f382fba595e74ce67c3f285c35ed130af1e977f3 100644 (file)
@@ -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');
                }
 
index d0db38f3e4766560b80ef3ac9c6fff817d7877b2..059c7bd13e270e09bb04d81b92e6b7aeee13e94e 100644 (file)
@@ -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);
 }
 
index 3421d1fb6b014f5bb5baf009cb17497a70b08891..aae151d3d6120d0bda6738c838cc34e315653293 100644 (file)
@@ -137,7 +137,7 @@ function invoice_header(&$supp_trans)
        if ($supplier_currency != $company_currency)
        {
         label_row(_("Supplier's Currency:"), "<b>" . $supplier_currency . "</b>");
-               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);
index 5f09dc10a52d5476e8f459e2ba4c6b78f00030ae..728d2e7ce190b57f404e72b9f8984e4cdcf174f9 100644 (file)
@@ -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']);
        }
 
index b75aac1ef7091f11bb7ec1aba8619bcfb0b11ee7..07006fb6fa8d2f114294f7692fc56162a481bb41 100644 (file)
@@ -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);
index f58f677c1fea70d472b5c555e331f5b00bf230d3..585434c0ff348be48f358f4b020a58df2a138439 100644 (file)
@@ -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));
        }