From d0d6ea3a78bb5da4b3b23e1d914d0c1a5ae12d00 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 20 Sep 2017 21:30:17 -0400 Subject: [PATCH] OFX imports: Properly import 'trading' transaction splits --- accounts.go | 2 +- imports.go | 31 +++++++++++++++---------------- transactions.go | 5 +++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/accounts.go b/accounts.go index 17ff4c1..920c1d9 100644 --- a/accounts.go +++ b/accounts.go @@ -201,7 +201,7 @@ func GetTradingAccount(transaction *gorp.Transaction, userid int64, securityid i return nil, err } - security, err := GetSecurity(securityid, userid) + security, err := GetSecurityTx(transaction, securityid, userid) if err != nil { return nil, err } diff --git a/imports.go b/imports.go index 1c55f8b..5ff30a7 100644 --- a/imports.go +++ b/imports.go @@ -120,8 +120,21 @@ func ofxImportHelper(r io.Reader, w http.ResponseWriter, user *User, accountid i split.AccountId = account.AccountId } else if split.SecurityId != -1 { if sec, ok := securitymap[split.SecurityId]; ok { - split.SecurityId = sec.SecurityId // TODO try to auto-match splits to existing accounts based on past transactions that look like this one + if split.ImportSplitType == TradingAccount { + // Find/make trading account if we're that type of split + trading_account, err := GetTradingAccount(sqltransaction, user.UserId, sec.SecurityId) + if err != nil { + sqltransaction.Rollback() + WriteError(w, 999 /*Internal Error*/) + log.Print("Couldn't find split's SecurityId in map during OFX import") + return + } + split.AccountId = trading_account.AccountId + split.SecurityId = -1 + } else { + split.SecurityId = sec.SecurityId + } } else { sqltransaction.Rollback() WriteError(w, 999 /*Internal Error*/) @@ -146,23 +159,9 @@ func ofxImportHelper(r io.Reader, w http.ResponseWriter, user *User, accountid i // Fixup any imbalances in transactions var zero big.Rat - var num_imbalances int - for _, imbalance := range imbalances { - if imbalance.Cmp(&zero) != 0 { - num_imbalances += 1 - } - } - for imbalanced_security, imbalance := range imbalances { if imbalance.Cmp(&zero) != 0 { - var imbalanced_account *Account - // If we're dealing with exactly two securities, assume any imbalances - // from imports are from trading currencies/securities - if num_imbalances == 2 { - imbalanced_account, err = GetTradingAccount(sqltransaction, user.UserId, imbalanced_security) - } else { - imbalanced_account, err = GetImbalanceAccount(sqltransaction, user.UserId, imbalanced_security) - } + imbalanced_account, err := GetImbalanceAccount(sqltransaction, user.UserId, imbalanced_security) if err != nil { sqltransaction.Rollback() WriteError(w, 999 /*Internal Error*/) diff --git a/transactions.go b/transactions.go index 8c0e3ae..54649dc 100644 --- a/transactions.go +++ b/transactions.go @@ -26,8 +26,8 @@ const ( // Split.ImportSplitType const ( Default int64 = 0 - ImportAccount = 1 - SubAccount = 2 + ImportAccount = 1 // This split belongs to the main account being imported + SubAccount = 2 // This split belongs to a sub-account of that being imported ExternalAccount = 3 TradingAccount = 4 Commission = 5 @@ -35,6 +35,7 @@ const ( Fees = 7 Load = 8 IncomeAccount = 9 + ExpenseAccount = 10 ) type Split struct {