AUDIT: r17850 - gnucash/trunk/src/engine - Bug #568945: gnc_pricedb_convert_balance_* should look for the reciprocal of the exchange rate

Christian Stimming cstim at cvs.gnucash.org
Wed Jan 28 15:55:11 EST 2009


Author: cstim
Date: 2009-01-28 15:55:11 -0500 (Wed, 28 Jan 2009)
New Revision: 17850
Trac: http://svn.gnucash.org/trac/changeset/17850

Modified:
   gnucash/trunk/src/engine/gnc-pricedb.c
Log:
Bug #568945: gnc_pricedb_convert_balance_* should look for the reciprocal of the exchange rate

If the price DB contains the reciprocal of the exchange rate which
gnc_pricedb_convert_balance_nearest_price,
gnc_pricedb_convert_balance_latest_price, and
gnc_pricedb_convert_balance_latest_before need to do their conversion they
don't find it and return zero.

Patch by Mike Alexander.
BP

Modified: gnucash/trunk/src/engine/gnc-pricedb.c
===================================================================
--- gnucash/trunk/src/engine/gnc-pricedb.c	2009-01-28 20:55:05 UTC (rev 17849)
+++ gnucash/trunk/src/engine/gnc-pricedb.c	2009-01-28 20:55:11 UTC (rev 17850)
@@ -1848,6 +1848,18 @@
     return balance;
   }
 
+  /* Look for a price of the new currency in the balance currency and use
+   * the reciprocal if we find it 
+   */
+  price = gnc_pricedb_lookup_latest (pdb, new_currency, balance_currency);
+  if (price) {
+    balance = gnc_numeric_div (balance, gnc_price_get_value (price),
+                               gnc_commodity_get_fraction (new_currency),
+                               GNC_HOW_RND_ROUND);
+    gnc_price_unref (price);
+    return balance;
+  }
+
   /*
    * no direct price found, try if we find a price in another currency
    * and convert in two stages
@@ -1885,7 +1897,7 @@
 
     list_helper = list_helper->next;
   } while((list_helper != NULL) &&
-          (!gnc_numeric_zero_p(currency_price_value)));
+          (gnc_numeric_zero_p(currency_price_value)));
 
   balance = gnc_numeric_mul (balance, currency_price_value,
                              GNC_DENOM_AUTO,
@@ -1924,6 +1936,18 @@
     return balance;
   }
 
+  /* Look for a price of the new currency in the balance currency and use
+   * the reciprocal if we find it 
+   */
+  price = gnc_pricedb_lookup_nearest_in_time (pdb, new_currency, balance_currency, t);
+  if (price) {
+    balance = gnc_numeric_div (balance, gnc_price_get_value (price),
+                               gnc_commodity_get_fraction (new_currency),
+                               GNC_HOW_RND_ROUND);
+    gnc_price_unref (price);
+    return balance;
+  }
+
   /*
    * no direct price found, try if we find a price in another currency
    * and convert in two stages
@@ -1961,7 +1985,7 @@
 
     list_helper = list_helper->next;
   } while((list_helper != NULL) &&
-          (!gnc_numeric_zero_p(currency_price_value)));
+	  (gnc_numeric_zero_p(currency_price_value)));
 
   balance = gnc_numeric_mul (balance, currency_price_value,
                              gnc_commodity_get_fraction (new_currency),
@@ -2003,6 +2027,18 @@
     return balance;
   }
 
+  /* Look for a price of the new currency in the balance currency and use
+   * the reciprocal if we find it.
+   */
+  price = gnc_pricedb_lookup_latest_before (pdb, new_currency, balance_currency, t);
+  if (price) {
+    balance = gnc_numeric_div (balance, gnc_price_get_value (price),
+                               gnc_commodity_get_fraction (new_currency),
+                               GNC_HOW_RND_ROUND);
+    gnc_price_unref (price);
+    return balance;
+  }
+  
   /*
    * no direct price found, try if we find a price in another currency
    * and convert in two stages
@@ -2040,7 +2076,7 @@
 
     list_helper = list_helper->next;
   } while((list_helper != NULL) &&
-          (!gnc_numeric_zero_p(currency_price_value)));
+          (gnc_numeric_zero_p(currency_price_value)));
 
   balance = gnc_numeric_mul (balance, currency_price_value,
                              gnc_commodity_get_fraction (new_currency),



More information about the gnucash-changes mailing list