From 8712be5a9d8d37f680a161ec867af2b9195dc9cb Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Tue, 18 Apr 2017 20:17:44 -0400 Subject: [PATCH] Bank Transactions: Use Currency structs, not CurrSymbol directly --- bank.go | 8 ++++---- cmd/ofx/banktransactions.go | 4 ++-- cmd/ofx/cctransactions.go | 4 ++-- common.go | 10 ++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bank.go b/bank.go index b31913c..de27ced 100644 --- a/bank.go +++ b/bank.go @@ -124,8 +124,8 @@ type Transaction struct { CCAcctTo *CCAcct `xml:"CCACCTTO,omitempty"` // If the transfer was to a credit card account we have the account information for Memo String `xml:"MEMO,omitempty"` // Extra information (not in NAME) ImageData []ImageData `xml:"IMAGEDATA,omitempty"` - Currency CurrSymbol `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS - OrigCurrency CurrSymbol `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS + Currency Currency `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS + OrigCurrency Currency `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS Inv401kSource inv401kSource `xml:"INV401KSOURCE,omitempty"` // One of PRETAX, AFTERTAX, MATCH, PROFITSHARING, ROLLOVER, OTHERVEST, OTHERNONVEST (Default if not present is OTHERNONVEST. The following cash source types are subject to vesting: MATCH, PROFITSHARING, and OTHERVEST.) } @@ -212,8 +212,8 @@ type PendingTransaction struct { ExtdName String `xml:"EXTDNAME,omitempty"` // Extended name of payee or transaction description Memo String `xml:"MEMO,omitempty"` // Extra information (not in NAME) ImageData []ImageData `xml:"IMAGEDATA,omitempty"` - Currency CurrSymbol `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS - OrigCurrency CurrSymbol `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS + Currency Currency `xml:"CURRENCY,omitempty"` // If different from CURDEF in STMTTRS + OrigCurrency Currency `xml:"ORIGCURRENCY,omitempty"` // If different from CURDEF in STMTTRS } // Valid returns (true, nil) if this struct is valid OFX diff --git a/cmd/ofx/banktransactions.go b/cmd/ofx/banktransactions.go index df0bf7e..3c3eea1 100644 --- a/cmd/ofx/banktransactions.go +++ b/cmd/ofx/banktransactions.go @@ -78,9 +78,9 @@ func bankTransactions() { func printTransaction(defCurrency ofxgo.CurrSymbol, tran *ofxgo.Transaction) { currency := defCurrency if ok, _ := tran.Currency.Valid(); ok { - currency = tran.Currency + currency = tran.Currency.CurSym } else if ok, _ := tran.OrigCurrency.Valid(); ok { - currency = tran.OrigCurrency + currency = tran.OrigCurrency.CurSym } var name string diff --git a/cmd/ofx/cctransactions.go b/cmd/ofx/cctransactions.go index 4d9f055..5513c2b 100644 --- a/cmd/ofx/cctransactions.go +++ b/cmd/ofx/cctransactions.go @@ -61,9 +61,9 @@ func ccTransactions() { for _, tran := range stmt.BankTranList.Transactions { currency := stmt.CurDef if ok, _ := tran.Currency.Valid(); ok { - currency = tran.Currency + currency = tran.Currency.CurSym } else if ok, _ := tran.OrigCurrency.Valid(); ok { - currency = tran.OrigCurrency + currency = tran.OrigCurrency.CurSym } var name string diff --git a/common.go b/common.go index 63d4963..aa763cf 100644 --- a/common.go +++ b/common.go @@ -316,3 +316,13 @@ type Currency struct { CurRate Amount `xml:"CURRATE"` // Ratio of currency to currency CurSym CurrSymbol `xml:"CURSYM"` // ISO-4217 3-character currency identifier } + +// Valid returns whether the Currency is valid according to the OFX spec +func (c Currency) Valid() (bool, error) { + if c.CurRate.IsInt() && c.CurRate.Num().Int64() == 0 { + return false, errors.New("CurRate may not be zero") + } else if ok, err := c.CurSym.Valid(); !ok { + return false, err + } + return true, nil +}