gnucash maint: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Fri Mar 11 16:08:21 EST 2016


Updated	 via  https://github.com/Gnucash/gnucash/commit/afe7dacb (commit)
	 via  https://github.com/Gnucash/gnucash/commit/56cb0327 (commit)
	from  https://github.com/Gnucash/gnucash/commit/4a60e490 (commit)



commit afe7dacb8501f0bf3187dc29c40f04eac906dd28
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri Mar 11 12:59:01 2016 -0800

    Remove price-direction code for to default currency and alphabetic order.
    
    It's much more important to have currency exchange rates > 1 to preserve
    significant digits.

diff --git a/src/gnome-utils/dialog-transfer.c b/src/gnome-utils/dialog-transfer.c
index d9d7938..fb204f9 100644
--- a/src/gnome-utils/dialog-transfer.c
+++ b/src/gnome-utils/dialog-transfer.c
@@ -1567,20 +1567,6 @@ create_transaction(XferDialog *xferData, Timespec *ts,
         xferData->transaction_cb(trans, xferData->transaction_user_data);
 }
 
-static void
-swap_amount (gnc_commodity **from, gnc_commodity **to, gnc_numeric *value,
-             gnc_numeric *from_amt, gnc_numeric *to_amt)
-{
-    gnc_commodity *tmp = *from;
-    gnc_numeric *tmp_amt = from_amt;
-    *from = *to;
-    *to = tmp;
-    from_amt = to_amt;
-    to_amt = tmp_amt;
-    *value = gnc_numeric_invert (*value);
-    *value = round_price(*from, *to, *value);
-}
-
 static gnc_numeric
 swap_commodities(gnc_commodity **from, gnc_commodity **to, gnc_numeric value)
 {
@@ -1637,37 +1623,19 @@ new_price(XferDialog *xferData, Timespec ts)
     gnc_commodity *from = xferData->from_commodity;
     gnc_commodity *to = xferData->to_commodity;
     gnc_numeric value = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
-    gnc_numeric from_amt, to_amt;
 
-/* Normally we want to store currency rates such that the rate > 1 and commodity
- * prices in terms of a currency regardless of value, but we also try to be
- * consistent about how quotes are installed.
+/* We want to store currency rates such that the rate > 1 and commodity
+ * prices in terms of a currency regardless of value.
  */
-    if (from == gnc_default_currency() ||
-        ((to != gnc_default_currency()) &&
-         (strcmp (gnc_commodity_get_mnemonic(from),
-                  gnc_commodity_get_mnemonic(to)) < 0)))
-        swap_amount (&from, &to, &value, &from_amt, &to_amt);
-
     if (gnc_commodity_is_currency(from) && gnc_commodity_is_currency(to))
     {
         if (value.num < value.denom)
-        {
             value = swap_commodities(&from, &to, value);
-        }
-        value = gnc_numeric_convert(value, CURRENCY_DENOM,
-                                    GNC_HOW_RND_ROUND_HALF_UP);
     }
-    else if (gnc_commodity_is_currency(from) || gnc_commodity_is_currency(to))
-    {
-        int scu;
-        if (gnc_commodity_is_currency(from))
+    else if (gnc_commodity_is_currency(from))
             value = swap_commodities(&from, &to, value);
-        scu = gnc_commodity_get_fraction (to);
-        value = gnc_numeric_convert(value, scu * COMMODITY_DENOM_MULT,
-                                    GNC_HOW_RND_ROUND_HALF_UP);
-    }
 
+    value = round_price (from, to, value);
     price = gnc_price_create (xferData->book);
     gnc_price_begin_edit (price);
     gnc_price_set_commodity (price, from);

commit 56cb0327e18625a292d9e32a9e21420b51d9bca4
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri Mar 11 12:22:57 2016 -0800

    Extract functions update_price and new_price from create_price.

diff --git a/src/gnome-utils/dialog-transfer.c b/src/gnome-utils/dialog-transfer.c
index 7826e6b..d9d7938 100644
--- a/src/gnome-utils/dialog-transfer.c
+++ b/src/gnome-utils/dialog-transfer.c
@@ -1593,74 +1593,62 @@ swap_commodities(gnc_commodity **from, gnc_commodity **to, gnc_numeric value)
 }
 
 static void
-create_price(XferDialog *xferData, Timespec ts)
+update_price(XferDialog *xferData, PriceReq *pr)
 {
-    PriceReq pr;
-    GNCPrice *price = NULL;
     gnc_commodity *from = xferData->from_commodity;
     gnc_commodity *to = xferData->to_commodity;
-    gnc_numeric price_value;
-    gnc_numeric value;
-    gnc_numeric from_amt, to_amt;
-    gboolean swap = FALSE;
+    gnc_numeric value = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));;
+    gnc_numeric price_value = gnc_price_get_value(pr->price);
 
-/* Bail in the unlikely event that both currencies have joined the Euro. */
-    if (gnc_is_euro_currency (from) && gnc_is_euro_currency (to))
+    if (gnc_numeric_equal(pr->reverse ? gnc_numeric_invert(value) : value,
+                          price_value))
+    {
+        PINFO("Same price for %s in %s",
+              gnc_commodity_get_mnemonic(pr->from),
+              gnc_commodity_get_mnemonic(pr->to));
+        gnc_price_unref (pr->price);
+        return;
+    }
+    if (gnc_price_get_source(pr->price) < xferData->price_source)
+    {
+        PINFO("Existing price is preferred, so won't supersede.");
+        gnc_price_unref (pr->price);
         return;
+    }
+    gnc_price_begin_edit (pr->price);
+    gnc_price_set_time (pr->price, pr->ts);
+    gnc_price_set_typestr(pr->price, xferData->price_type);
+    if (pr->reverse)
+        gnc_price_set_value (pr->price, gnc_numeric_invert(value));
+    else
+        gnc_price_set_value (pr->price, value);
+    gnc_price_commit_edit (pr->price);
+    PINFO("Updated price: 1 %s = %f %s",
+          gnc_commodity_get_mnemonic(pr->from),
+          gnc_numeric_to_double(gnc_price_get_value(pr->price)),
+          gnc_commodity_get_mnemonic(pr->to));
+    gnc_price_unref (pr->price);
+}
+
+static void
+new_price(XferDialog *xferData, Timespec ts)
+{
+    GNCPrice *price = NULL;
+    gnc_commodity *from = xferData->from_commodity;
+    gnc_commodity *to = xferData->to_commodity;
+    gnc_numeric value = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
+    gnc_numeric from_amt, to_amt;
 
-    value = gnc_amount_edit_get_amount(GNC_AMOUNT_EDIT(xferData->price_edit));
-    /* Try to be consistent about how quotes are installed. */
+/* Normally we want to store currency rates such that the rate > 1 and commodity
+ * prices in terms of a currency regardless of value, but we also try to be
+ * consistent about how quotes are installed.
+ */
     if (from == gnc_default_currency() ||
         ((to != gnc_default_currency()) &&
          (strcmp (gnc_commodity_get_mnemonic(from),
                   gnc_commodity_get_mnemonic(to)) < 0)))
         swap_amount (&from, &to, &value, &from_amt, &to_amt);
 
-/* Normally we want to store currency rates such that the rate > 1 and commodity
- * prices in terms of a currency regardless of value. However, if we already
- * have a price in either direction we want to continue using that direction for
- * the rest of the day so that we don't wind up with two prices if the rate
- * shifts to be < 1. */
-
-    price_request_from_xferData(&pr, xferData);
-    if (lookup_price(&pr, SAME_DAY))
-    {
-        price_value = gnc_price_get_value(pr.price);
-        if (gnc_numeric_equal(pr.reverse ? gnc_numeric_invert(value) : value,
-                              price_value))
-        {
-            PINFO("Found price for %s in %s",
-                  gnc_commodity_get_mnemonic(pr.from),
-                  gnc_commodity_get_mnemonic(pr.to));
-            gnc_price_unref (pr.price);
-            return;
-        }
-        if (gnc_price_get_source(pr.price) < xferData->price_source)
-        {
-            PINFO("Existing price is preferred, so won't supersede.");
-            gnc_price_unref (pr.price);
-            return;
-        }
-        if (pr.reverse)
-        {
-            value = swap_commodities(&from, &to, value);
-            xferData->from_commodity = from;
-            xferData->to_commodity = to;
-        }
-        value = round_price(pr.from, pr.to, value);
-        gnc_price_begin_edit (pr.price);
-        gnc_price_set_time (pr.price, ts);
-        gnc_price_set_source (pr.price, xferData->price_source);
-        gnc_price_set_typestr(pr.price, xferData->price_type);
-        gnc_price_set_value (pr.price, value);
-        gnc_price_commit_edit (pr.price);
-        PINFO("Modified price: 1 %s = %f %s",
-              gnc_commodity_get_mnemonic(from),
-              gnc_numeric_to_double(value),
-              gnc_commodity_get_mnemonic(to));
-        gnc_price_unref (pr.price);
-        return;
-    }
     if (gnc_commodity_is_currency(from) && gnc_commodity_is_currency(to))
     {
         if (value.num < value.denom)
@@ -1679,6 +1667,7 @@ create_price(XferDialog *xferData, Timespec ts)
         value = gnc_numeric_convert(value, scu * COMMODITY_DENOM_MULT,
                                     GNC_HOW_RND_ROUND_HALF_UP);
     }
+
     price = gnc_price_create (xferData->book);
     gnc_price_begin_edit (price);
     gnc_price_set_commodity (price, from);
@@ -1692,6 +1681,25 @@ create_price(XferDialog *xferData, Timespec ts)
     PINFO("Created price: 1 %s = %f %s", gnc_commodity_get_mnemonic(from),
           gnc_numeric_to_double(value), gnc_commodity_get_mnemonic(to));
     gnc_price_unref (price);
+}    
+
+static void
+create_price(XferDialog *xferData, Timespec ts)
+{
+    PriceReq pr;
+
+/* Bail in the unlikely event that both currencies have joined the Euro. */
+    if (gnc_is_euro_currency (xferData->from_commodity) &&
+        gnc_is_euro_currency (xferData->to_commodity))
+        return;
+
+    price_request_from_xferData(&pr, xferData);
+    if (lookup_price(&pr, SAME_DAY))
+    {
+        update_price(xferData, &pr);
+        return;
+    }
+    new_price (xferData, ts);
 }
 
 void



Summary of changes:
 src/gnome-utils/dialog-transfer.c | 148 ++++++++++++++++----------------------
 1 file changed, 62 insertions(+), 86 deletions(-)



More information about the gnucash-changes mailing list