Skip to content

Commit

Permalink
fix parent account comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Oct 11, 2022
1 parent 39fcff6 commit 5a73722
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/server/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ananthakumaran/paisa/internal/model/posting"
"github.com/ananthakumaran/paisa/internal/query"
"github.com/ananthakumaran/paisa/internal/service"
"github.com/ananthakumaran/paisa/internal/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func computeBreakdown(db *gorm.DB, postings []posting.Posting) map[string]Breakd
result := make(map[string]Breakdown)

for group, leaf := range accounts {
ps := lo.Filter(postings, func(p posting.Posting, _ int) bool { return strings.HasPrefix(p.Account, group) })
ps := lo.Filter(postings, func(p posting.Posting, _ int) bool { return utils.IsSameOrParent(p.Account, group) })
investmentAmount := lo.Reduce(ps, func(acc float64, p posting.Posting, _ int) float64 {
if p.Account == "Assets:Checking" || p.Amount < 0 || service.IsInterest(db, p) {
return acc
Expand Down
9 changes: 9 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"strings"
"time"

"github.com/google/btree"
Expand Down Expand Up @@ -54,3 +55,11 @@ func IsWithDate(date time.Time, start time.Time, end time.Time) bool {
func toDate(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.UTC)
}

func IsSameOrParent(account string, comparison string) bool {
if account == comparison {
return true
}

return strings.HasPrefix(account, comparison+":")
}

0 comments on commit 5a73722

Please sign in to comment.