r18321 - gnucash/trunk/src/import-export/ofx - Bug #572938: Fix OFX Mutual fund buys that are imported as sells

Christian Stimming cstim at code.gnucash.org
Fri Sep 18 15:51:44 EDT 2009


Author: cstim
Date: 2009-09-18 15:51:43 -0400 (Fri, 18 Sep 2009)
New Revision: 18321
Trac: http://svn.gnucash.org/trac/changeset/18321

Modified:
   gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c
Log:
Bug #572938: Fix OFX Mutual fund buys that are imported as sells

When I try to import a downloaded OFX from Fidelity NetBenefits, my mutual fund
purchases become sales. I looked at the code and it seemed like a pretty simple
fix, it seems as if it was treading all non-income investing transactions as sales.
I wrote and tested this patch to fix the problem.

Patch by Matt Lavin, signed-off by Benoit Grégoire.

Modified: gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c
===================================================================
--- gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c	2009-09-18 19:50:48 UTC (rev 18320)
+++ gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c	2009-09-18 19:51:43 UTC (rev 18321)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <sys/time.h>
 #include <libguile.h>
+#include <math.h>
 
 #include <libofx/libofx.h>
 #include "import-account-matcher.h"
@@ -71,6 +72,7 @@
 int ofx_proc_security_cb(const struct OfxSecurityData data, void * security_user_data);
 int ofx_proc_transaction_cb(struct OfxTransactionData data, void * transaction_user_data);
 int ofx_proc_account_cb(struct OfxAccountData data, void * account_user_data);
+double ofx_get_investment_amount(struct OfxTransactionData data);
 
 int ofx_proc_security_cb(const struct OfxSecurityData data, void * security_user_data)
 {
@@ -379,7 +381,7 @@
 			xaccTransAppendSplit(transaction,split);
 			xaccAccountInsertSplit(investment_account,split);
 
-			gnc_amount = double_to_gnc_numeric (-(data.amount),
+			gnc_amount = double_to_gnc_numeric (ofx_get_investment_amount(data),
 							    gnc_commodity_get_fraction(investment_commodity),
 							    GNC_RND_ROUND);
 			gnc_units = double_to_gnc_numeric (data.units,
@@ -505,7 +507,7 @@
 			xaccTransAppendSplit(transaction,split);
 			xaccAccountInsertSplit(account,split);
 
-			gnc_amount = double_to_gnc_numeric (data.amount,
+			gnc_amount = double_to_gnc_numeric (-ofx_get_investment_amount(data),
 							    gnc_commodity_get_fraction(xaccTransGetCurrency(transaction)),
 							    GNC_RND_ROUND);
 			xaccSplitSetBaseValue(split, gnc_amount, xaccTransGetCurrency(transaction));
@@ -632,6 +634,26 @@
   return 0;
 }
 
+double ofx_get_investment_amount(struct OfxTransactionData data)
+{
+  switch(data.invtransactiontype){
+  case OFX_BUYDEBT: 
+  case OFX_BUYMF:
+  case OFX_BUYOPT:
+  case OFX_BUYOTHER:
+  case OFX_BUYSTOCK:
+    return fabs(data.amount);
+  case OFX_SELLDEBT:
+  case OFX_SELLMF:
+  case OFX_SELLOPT:
+  case OFX_SELLOTHER:
+  case OFX_SELLSTOCK:
+    return -1*fabs(data.amount);
+  default:
+    return -1*data.amount;
+  }
+}
+
 void gnc_file_ofx_import (void)
 {
   extern int ofx_PARSER_msg;



More information about the gnucash-changes mailing list