$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
*/
$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");
}
//---------------------------------------------------------------------------------------------
$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");
}
//---------------------------------------------------------------------------------------------
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);
}
//---------------------------------------------------------------------------------------------
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;
$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
//
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;
$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');
}
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);
}
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);
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']);
}
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);
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));
}