r21719 - gnucash/branches/2.4/src - [21713]Bug #649362 - Transfer Funds Window Exchange Rate and Decimal Points

Geert Janssens gjanssens at code.gnucash.org
Sun Dec 11 17:15:40 EST 2011


Author: gjanssens
Date: 2011-12-11 17:15:39 -0500 (Sun, 11 Dec 2011)
New Revision: 21719
Trac: http://svn.gnucash.org/trac/changeset/21719

Modified:
   gnucash/branches/2.4/src/business/business-gnome/dialog-invoice.c
   gnucash/branches/2.4/src/engine/gncInvoice.c
Log:
[21713]Bug #649362 - Transfer Funds Window Exchange Rate and Decimal Points
Rounding in Bill/Invoices
This patch fixes the rounding by showing the actual value to convert. As
a result the exchange rate direction had to be swapped as well to avoid
lots of confusion.

Conflicts:

	src/engine/gncInvoice.c

Modified: gnucash/branches/2.4/src/business/business-gnome/dialog-invoice.c
===================================================================
--- gnucash/branches/2.4/src/business/business-gnome/dialog-invoice.c	2011-12-11 22:15:29 UTC (rev 21718)
+++ gnucash/branches/2.4/src/business/business-gnome/dialog-invoice.c	2011-12-11 22:15:39 UTC (rev 21719)
@@ -755,13 +755,15 @@
     for (entries_iter = entries; entries_iter != NULL; entries_iter = g_list_next(entries_iter))
     {
         Account *this_acc;
+        gnc_commodity *account_currency;
 
         entry = (GncEntry*)entries_iter->data;
         this_acc = (is_cust_doc ? gncEntryGetInvAccount (entry) :
                     gncEntryGetBillAccount (entry));
+        account_currency = xaccAccountGetCommodity (this_acc);
 
         if (this_acc &&
-            !gnc_commodity_equal (gncInvoiceGetCurrency (invoice), xaccAccountGetCommodity (this_acc)))
+            !gnc_commodity_equal (gncInvoiceGetCurrency (invoice), account_currency))
         {
             GNCPrice *convprice;
 
@@ -771,18 +773,37 @@
                 show_dialog = FALSE;
             }
 
-            convprice = gncInvoiceGetPrice(invoice, xaccAccountGetCommodity(this_acc));
+            convprice = gncInvoiceGetPrice(invoice, account_currency);
             if (convprice == NULL)
             {
                 XferDialog *xfer;
                 gnc_numeric exch_rate;
                 Timespec date;
                 gnc_numeric amount = gnc_numeric_create(1, 1);
+                gnc_numeric value;
+                gnc_numeric tax;
 
+                /* Note some twisted logic here:
+                 * We ask the exchange rate
+                 *  FROM invoice currency
+                 *  TO other account currency
+                 *  Because that's what happens logically.
+                 *  But the internal posting logic works backwards:
+                 *  It searches for an exchange rate
+                 *  FROM other account currency
+                 *  TO invoice currency
+                 *  So we will store the inverted exchange rate
+                 */
 
+                /* Obtain the Entry's total value (net + tax) */
+                gncEntryGetValue (entry, is_cust_doc, &value, NULL, &tax, NULL);
+                amount = gnc_numeric_add (value, tax,
+                        gnc_commodity_get_fraction (account_currency),
+                        GNC_HOW_RND_ROUND_HALF_UP );
+
                 /* create the exchange-rate dialog */
-                xfer = gnc_xfer_dialog (iw_get_window(iw), this_acc);
-                gnc_xfer_dialog_select_to_account(xfer, acc);
+                xfer = gnc_xfer_dialog (iw_get_window(iw), acc);
+                gnc_xfer_dialog_select_to_account(xfer, this_acc);
                 gnc_xfer_dialog_set_amount(xfer, amount);
 
                 /* All we want is the exchange rate so prevent the user from thinking
@@ -795,9 +816,14 @@
                 if (gnc_xfer_dialog_run_until_done(xfer))
                 {
                     /* User finished the transfer dialog successfully */
+
+                    /* Invert the exchange rate as explained above */
+                    if (!gnc_numeric_zero_p (exch_rate))
+                        exch_rate = gnc_numeric_div ((gnc_numeric) {1, 1}, exch_rate,
+                                                      GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
                     convprice = gnc_price_create(iw->book);
                     gnc_price_begin_edit (convprice);
-                    gnc_price_set_commodity (convprice, xaccAccountGetCommodity(this_acc));
+                    gnc_price_set_commodity (convprice, account_currency);
                     gnc_price_set_currency (convprice, gncInvoiceGetCurrency (invoice));
                     date.tv_sec = time (NULL);
                     date.tv_nsec = 0;

Modified: gnucash/branches/2.4/src/engine/gncInvoice.c
===================================================================
--- gnucash/branches/2.4/src/engine/gncInvoice.c	2011-12-11 22:15:29 UTC (rev 21718)
+++ gnucash/branches/2.4/src/engine/gncInvoice.c	2011-12-11 22:15:39 UTC (rev 21719)
@@ -1366,7 +1366,7 @@
                             gnc_numeric converted_amount;
                             xaccSplitSetValue(split, (reverse ? gnc_numeric_neg(value) : value));
                             converted_amount = gnc_numeric_div(value, gnc_price_get_value(price), GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
-                            printf("converting from %f to %f\n", gnc_numeric_to_double(value), gnc_numeric_to_double(converted_amount));
+                            DEBUG("converting from %f to %f\n", gnc_numeric_to_double(value), gnc_numeric_to_double(converted_amount));
                             xaccSplitSetAmount(split, reverse ? gnc_numeric_neg(converted_amount) : converted_amount);
                         }
                     }
@@ -1457,7 +1457,7 @@
                 gnc_numeric converted_amount;
                 xaccSplitSetValue(split, (reverse ? gnc_numeric_neg(acc_val->value) : acc_val->value));
                 converted_amount = gnc_numeric_div(acc_val->value, gnc_price_get_value(price), GNC_DENOM_AUTO, GNC_HOW_RND_ROUND_HALF_UP);
-                printf("converting from %f to %f\n", gnc_numeric_to_double(acc_val->value), gnc_numeric_to_double(converted_amount));
+                DEBUG("converting from %f to %f\n", gnc_numeric_to_double(acc_val->value), gnc_numeric_to_double(converted_amount));
 
                 xaccSplitSetAmount(split, reverse ? gnc_numeric_neg(converted_amount) : converted_amount);
             }



More information about the gnucash-changes mailing list