2003-03-31 Herbert Thoma <herbie@hthoma.de>

* src/engine/gnc-pricedb.c
	* src/engine/gnc-pricedb.h: new function
	gnc_pricedb_lookup_latest_any_currency, return any available
	prices for given commodity regardless of currency

	* src/app-utils/gnc-ui-util.c: do a "two stage" price lookup in
	gnc_ui_convert_balance_to_currency if no price for the commodity
	is found in the requested currency, then look for prices in any
	currency and for a exchange rate from the price currency to the
	requested currency

	* src/gnome/gnc-split-reg.c: do currency conversion and show the
	value in the status line of stock and mutual fund accounts


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@8145 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming
2003-03-31 19:12:21 +00:00
parent c1818d3d2c
commit 1c2adca030
5 changed files with 184 additions and 9 deletions

View File

@@ -392,14 +392,70 @@ gnc_ui_convert_balance_to_currency(gnc_numeric balance, gnc_commodity *balance_c
pdb = gnc_book_get_pricedb (book);
price = gnc_pricedb_lookup_latest (pdb, balance_currency, currency);
if (!price)
return gnc_numeric_zero ();
balance = gnc_numeric_mul (balance, gnc_price_get_value (price),
gnc_commodity_get_fraction (currency),
GNC_RND_ROUND);
if (price)
{
balance = gnc_numeric_mul (balance, gnc_price_get_value (price),
gnc_commodity_get_fraction (currency),
GNC_RND_ROUND);
gnc_price_unref (price);
}
else
{
/* no direct price found, try if we find a price in another
currency and convert in two stages */
GList *price_list = gnc_pricedb_lookup_latest_any_currency(pdb, balance_currency);
if(price_list)
{
GList *list_helper = price_list;
gnc_numeric currency_price_value = gnc_numeric_zero();
gnc_commodity *intermediate_currency;
GNCPrice *currency_price;
gnc_price_unref (price);
do
{
price = (GNCPrice *)(list_helper->data);
intermediate_currency = gnc_price_get_currency(price);
currency_price = gnc_pricedb_lookup_latest(pdb, intermediate_currency, currency);
if(currency_price)
{
currency_price_value = gnc_price_get_value(currency_price);
gnc_price_unref(currency_price);
}
else
{
currency_price = gnc_pricedb_lookup_latest(pdb, currency, intermediate_currency);
if(currency_price)
{
/* here we need the reciprocal */
currency_price_value = gnc_numeric_div(gnc_numeric_create(1, 1),
gnc_price_get_value(currency_price),
gnc_commodity_get_fraction (currency),
GNC_RND_ROUND);
gnc_price_unref(currency_price);
}
}
list_helper = list_helper->next;
}
while((list_helper != NULL) && (!gnc_numeric_zero_p(currency_price_value)));
balance = gnc_numeric_mul (balance, currency_price_value,
gnc_commodity_get_fraction (currency),
GNC_RND_ROUND);
balance = gnc_numeric_mul (balance, gnc_price_get_value (price),
gnc_commodity_get_fraction (currency),
GNC_RND_ROUND);
gnc_price_list_destroy(price_list);
}
else
{
balance = gnc_numeric_zero ();
}
}
return balance;
}