gnucash maint: Bug 798019 - Currency rates in OFX file are ignored

John Ralls jralls at code.gnucash.org
Tue Feb 9 18:11:14 EST 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/96dcca99 (commit)
	from  https://github.com/Gnucash/gnucash/commit/f0ff1675 (commit)



commit 96dcca997fb23060c0c78dde51aeb984e47f1ae9
Author: John Ralls <jralls at ceridwen.us>
Date:   Tue Feb 9 15:09:50 2021 -0800

    Bug 798019 - Currency rates in OFX file are ignored
    
    Use currency_rate information from OFX file if available. Requires
    LibOFX v 0.10.0 or later.

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 876c646ed..5f272b427 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -404,6 +404,9 @@ int main(int argc, char** argv)
 }
 " HAVE_OFX_BUG_39)
   set(HAVE_OFX_BUG_39 ${HAVE_OFX_BUG_39})
+  if (LIBOFX_VERSION VERSION_GREATER_EQUAL 0.10.0)
+    set(HAVE_LIBOFX_VERSION_0_10 1)
+  endif()
   set(CMAKE_REQUIRED_LIBRARIES)
 endif()
 # ############################################################
diff --git a/common/config.h.cmake.in b/common/config.h.cmake.in
index 1dc59e294..a68408b05 100644
--- a/common/config.h.cmake.in
+++ b/common/config.h.cmake.in
@@ -295,6 +295,8 @@
 /* Result of LibOFX Bug 39 detection (see
   https://sourceforge.net/p/libofx/bugs/39/ for details). */
 #cmakedefine HAVE_OFX_BUG_39 1
+/* LibOFX supports investment transactions. */
+#cmakedefine HAVE_LIBOFX_VERSION_0_10
 
 /* Name of package containing qt3-wizard. */
 #define QT3_WIZARD_PACKAGE "aqbanking"
diff --git a/gnucash/import-export/ofx/gnc-ofx-import.c b/gnucash/import-export/ofx/gnc-ofx-import.c
index cfd67dbef..2c98665e6 100644
--- a/gnucash/import-export/ofx/gnc-ofx-import.c
+++ b/gnucash/import-export/ofx/gnc-ofx-import.c
@@ -584,13 +584,17 @@ int ofx_proc_transaction_cb(struct OfxTransactionData data, void *user_data)
     {
         if (!data.invtransactiontype_valid)
         {
+            double amount = data.amount;
+#ifdef HAVE_LIBOFX_VERSION_0_10
+            if (data.currency_ratio_valid && data.currency_ratio != 0)
+                amount *= data.currency_ratio;
+#endif
             /***** Process a normal transaction ******/
             DEBUG("Adding split; Ordinary banking transaction, money flows from or into the source account");
             split = xaccMallocSplit(book);
             xaccTransAppendSplit(transaction, split);
             xaccAccountInsertSplit(account, split);
-
-            gnc_amount = gnc_ofx_numeric_from_double_txn(data.amount, transaction);
+            gnc_amount = gnc_ofx_numeric_from_double_txn(amount, transaction);
             xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction));
 
             /* set tran-num and/or split-action per book option */
@@ -798,11 +802,15 @@ int ofx_proc_transaction_cb(struct OfxTransactionData data, void *user_data)
 
             if (data.invtransactiontype_valid && investment_account)
             {
+                double amount = data.amount;
+#ifdef HAVE_LIBOFX_VERSION_0_10
+                if (data.currency_ratio_valid && data.currency_ratio != 0)
+                    amount *= data.currency_ratio;
+#endif
                 if (data.invtransactiontype == OFX_REINVEST
                         || data.invtransactiontype == OFX_INCOME)
                 {
                     DEBUG("Now let's find an account for the destination split");
-
                     income_account =
                         get_associated_income_account(investment_account);
 
@@ -845,8 +853,8 @@ int ofx_proc_transaction_cb(struct OfxTransactionData data, void *user_data)
                     split = xaccMallocSplit(book);
                     xaccTransAppendSplit(transaction, split);
                     xaccAccountInsertSplit(income_account, split);
-
-                    gnc_amount = gnc_ofx_numeric_from_double_txn (data.amount, transaction);
+                    gnc_amount = gnc_ofx_numeric_from_double_txn(amount,
+                                                                 transaction);
                     xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction));
 
                     // Set split memo from ofx transaction name or memo
@@ -859,9 +867,10 @@ int ofx_proc_transaction_cb(struct OfxTransactionData data, void *user_data)
                     split = xaccMallocSplit(book);
                     xaccTransAppendSplit(transaction, split);
                     xaccAccountInsertSplit(income_account, split);
+                    /*OFX_INCOME amounts come in as positive numbers*/
+                    gnc_amount = gnc_ofx_numeric_from_double_txn (-amount,
+                                                                  transaction);
 
-                    gnc_amount = gnc_ofx_numeric_from_double_txn (-data.amount,/*OFX_INCOME amounts come in as positive numbers*/
-                                 transaction);
                     xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction));
 
                     // Set split memo from ofx transaction name or memo
@@ -1040,6 +1049,11 @@ int ofx_proc_account_cb(struct OfxAccountData data, void * account_user_data)
 
 double ofx_get_investment_amount(const struct OfxTransactionData* data)
 {
+    double amount = data->amount;
+#ifdef HAVE_LIBOFX_VERSION_0_10
+   if (data->currency_ratio_valid && data->currency_ratio != 0)
+        amount *= data->currency_ratio;
+#endif
     g_assert(data);
     switch (data->invtransactiontype)
     {
@@ -1048,15 +1062,15 @@ double ofx_get_investment_amount(const struct OfxTransactionData* data)
     case OFX_BUYOPT:
     case OFX_BUYOTHER:
     case OFX_BUYSTOCK:
-        return fabs(data->amount);
+        return fabs(amount);
     case OFX_SELLDEBT:
     case OFX_SELLMF:
     case OFX_SELLOPT:
     case OFX_SELLOTHER:
     case OFX_SELLSTOCK:
-        return -1 * fabs(data->amount);
+        return -1 * fabs(amount);
     default:
-        return -1 * data->amount;
+        return -1 * amount;
     }
 }
 



Summary of changes:
 CMakeLists.txt                             |  3 +++
 common/config.h.cmake.in                   |  2 ++
 gnucash/import-export/ofx/gnc-ofx-import.c | 34 +++++++++++++++++++++---------
 3 files changed, 29 insertions(+), 10 deletions(-)



More information about the gnucash-changes mailing list