diff --git a/main.go b/main.go index 6479690..b6d0a51 100644 --- a/main.go +++ b/main.go @@ -35,10 +35,10 @@ func (t *Transaction) Values() []string { tType = "Sale" } return []string{ - tType, // "Type" - t.Date.Format("01/02/2006"), // "Trans Date" - t.Date.Format("01/02/2006"), // "Post Date" - t.MerchantName, // "Description" + tType, // "Type" + t.Date.Format("01/02/2006"), // "Trans Date" + t.Date.Format("01/02/2006"), // "Post Date" + t.MerchantName, // "Description" strconv.FormatFloat(-1.0*t.Amount, 'f', 2, 64), // "Amount" } } @@ -91,6 +91,33 @@ func (s *Statement) Reconcile() (float64, bool) { return total, isOK } +// FixEndOfYearEdgeCase checks if there are both January and December transactions. +// If both months are present, all December transactions are changed to the previous year. +func (s *Statement) FixEndOfYearEdgeCase() bool { + hasDecember := false + hasJanuary := false + + for _, t := range s.Transactions { + month := t.Date.Month() + if month == time.December { + hasDecember = true + } + if month == time.January { + hasJanuary = true + } + } + + if hasDecember && hasJanuary { + for i, t := range s.Transactions { + if t.Date.Month() == time.December { + s.Transactions[i].Date = t.Date.AddDate(-1, 0, 0) + } + } + return true + } + return false +} + var findStatements = regexp.MustCompile(`(?m)^([0-9]{0,2})/([0-9]{0,2}) (.*) ([0-9\-\.,]+)`) var ( @@ -157,6 +184,9 @@ func main() { } else { log.Fatal("could not find ending balance :( ") } + if fixed := (statement.FixEndOfYearEdgeCase()); fixed { + log.Print("found end of year transactions and revised december dates") + } sort.Sort(statement.Transactions) if val, res := statement.Reconcile(); !res { log.Fatalf("reconciliation doesn't match :(, actual: %v, expected %v", val, statement.EndingBalance)