Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down