diff --git a/internal/handlers/handlers_testdata/401k_mutualfunds.ofx b/internal/handlers/handlers_testdata/401k_mutualfunds.ofx new file mode 100644 index 0000000..18ba784 --- /dev/null +++ b/internal/handlers/handlers_testdata/401k_mutualfunds.ofx @@ -0,0 +1 @@ + 0INFOSUCCESS20171128203521.622[-5:EST]ENGofx.bank.com9199 d87db96a-c872-7f73-7637-7e9e2816c25a 0INFOSUCCESS20171128193521.926[-5:EST]USDofx.bank.com12321 20170829213521.814[-4:EDT]20171127203521.814[-5:EST]20170901OAEL01120170901070000.000[-4:EDT]CONTRIBUTION;VANGUARD TARGET 2045 OAEL;as of 09/01/2017OAELCUSIP1.75656.97100.05OTHEROTHERBUY 20170915OAEL01120170915070000.000[-4:EDT]CONTRIBUTION;VANGUARD TARGET 2045 OAEL;as of 09/15/2017OAELCUSIP1.73757.59100.05OTHEROTHERBUY 20170901OAEL13120170901070000.000[-4:EDT]FEES;VANGUARD TARGET 2045 OAEL;as of 09/01/2017OAELCUSIP0.0756.974.0OTHEROTHERSELL 20171002OAEL13120171002070000.000[-4:EDT]FEES;VANGUARD TARGET 2045 OAEL;as of 10/02/2017OAELCUSIP0.06958.14.0OTHEROTHERSELL OAELCUSIPOTHERLONG2792.37359.64200.03 20171127160000.000[-5:EST] Market close as of 11/27/2017;VANGUARD TARGET 2045 000MarketValueMarketValueDOLLAR200.0320171128193521.926[-5:EST]VestedValueVestedValueDOLLAR200.0320171128193521.926[-5:EST]TotalAssetsValueTotalAssetsValueDOLLAR200.0320171128193521.926[-5:EST]QC 401(K) PLAN200.03MarketValueMarketValueDOLLAR200.0320171128193521.926[-5:EST]VestedValueVestedValueDOLLAR200.0320171128193521.926[-5:EST]TotalAssetsValueTotalAssetsValueDOLLAR200.0320171128193521.926[-5:EST] OAELCUSIPVANGUARD TARGET 2045OAEL59.6420171127160000.000[-5:EST]Market close as of 11/27/2017;VANGUARD TARGET 2045OTHER diff --git a/internal/handlers/ofx.go b/internal/handlers/ofx.go index 1acd120..b559000 100644 --- a/internal/handlers/ofx.go +++ b/internal/handlers/ofx.go @@ -233,6 +233,9 @@ func (i *OFXImport) GetInvBuyTran(buy *ofxgo.InvBuy, curdef *Security, account * fees.Set(&buy.Fees.Rat) load.Set(&buy.Load.Rat) total.Set(&buy.Total.Rat) + if total.Sign() > 0 { + total.Neg(&total) + } tradingTotal.Neg(&total) tradingTotal.Sub(&tradingTotal, &commission) @@ -495,6 +498,9 @@ func (i *OFXImport) GetReinvestTran(reinvest *ofxgo.Reinvest, curdef *Security, fees.Set(&reinvest.Fees.Rat) load.Set(&reinvest.Load.Rat) total.Set(&reinvest.Total.Rat) + if total.Sign() > 0 { + total.Neg(&total) + } tradingTotal.Neg(&total) tradingTotal.Sub(&tradingTotal, &commission) @@ -694,6 +700,9 @@ func (i *OFXImport) GetInvSellTran(sell *ofxgo.InvSell, curdef *Security, accoun fees.Set(&sell.Fees.Rat) load.Set(&sell.Load.Rat) total.Set(&sell.Total.Rat) + if total.Sign() < 0 { + total.Neg(&total) + } tradingTotal.Neg(&total) tradingTotal.Sub(&tradingTotal, &commission) diff --git a/internal/handlers/ofx_test.go b/internal/handlers/ofx_test.go index ddbdf6e..3e4814e 100644 --- a/internal/handlers/ofx_test.go +++ b/internal/handlers/ofx_test.go @@ -1,6 +1,7 @@ package handlers_test import ( + "github.com/aclindsa/moneygo/internal/handlers" "net/http" "strconv" "testing" @@ -60,3 +61,39 @@ func TestImportOFXCreditCard(t *testing.T) { accountBalanceHelper(t, d.clients[0], &d.accounts[7], "-4.49") }) } + +func TestImportOFX401kMutualFunds(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + // Ensure there's only one USD currency + oldDefault, err := getSecurity(d.clients[0], d.users[0].DefaultCurrency) + if err != nil { + t.Fatalf("Error fetching default security: %s\n", err) + } + d.users[0].DefaultCurrency = d.securities[0].SecurityId + if _, err := updateUser(d.clients[0], &d.users[0]); err != nil { + t.Fatalf("Error updating user: %s\n", err) + } + if err := deleteSecurity(d.clients[0], oldDefault); err != nil { + t.Fatalf("Error removing default security: %s\n", err) + } + + account := &handlers.Account{ + SecurityId: d.securities[0].SecurityId, + UserId: d.users[0].UserId, + ParentAccountId: -1, + Type: handlers.Investment, + Name: "401k", + } + + account, err = createAccount(d.clients[0], account) + if err != nil { + t.Fatalf("Error creating 401k account: %s\n", err) + } + + // Import and ensure it didn't return a nasty error code + if err = importOFX(d.clients[0], account.AccountId, "handlers_testdata/401k_mutualfunds.ofx"); err != nil { + t.Fatalf("Error importing OFX: %s\n", err) + } + accountBalanceHelper(t, d.clients[0], account, "-192.10") + }) +}