@@ -3,8 +3,6 @@ package ledger
3
3
import (
4
4
"slices"
5
5
"strings"
6
-
7
- "github.com/howeyc/ledger/decimal"
8
6
)
9
7
10
8
// GetBalances provided a list of transactions and filter strings, returns account balances of
@@ -13,43 +11,33 @@ import (
13
11
//
14
12
// Accounts are sorted by name.
15
13
func GetBalances (generalLedger []* Transaction , filterArr []string ) []* Account {
16
- balances := make ( map [ string ]decimal. Decimal )
17
- filters := len ( filterArr ) > 0
14
+ var accList [] * Account
15
+ balances := make ( map [ string ] * Account )
18
16
for _ , trans := range generalLedger {
19
17
for _ , accChange := range trans .AccountChanges {
20
- inFilter := false
21
- if filters {
22
- for i := 0 ; i < len (filterArr ) && ! inFilter ; i ++ {
23
- if strings .Contains (accChange .Name , filterArr [i ]) {
24
- inFilter = true
25
- }
18
+ inFilter := len (filterArr ) == 0
19
+ for i := 0 ; i < len (filterArr ) && ! inFilter ; i ++ {
20
+ if strings .Contains (accChange .Name , filterArr [i ]) {
21
+ inFilter = true
26
22
}
27
- } else {
28
- inFilter = true
29
23
}
30
24
if inFilter {
31
25
accHier := strings .Split (accChange .Name , ":" )
32
26
accDepth := len (accHier )
33
27
for currDepth := accDepth ; currDepth > 0 ; currDepth -- {
34
28
currAccName := strings .Join (accHier [:currDepth ], ":" )
35
- if ratNum , ok := balances [currAccName ]; ! ok {
36
- balances [currAccName ] = accChange .Balance
29
+ if acc , ok := balances [currAccName ]; ! ok {
30
+ acc := & Account {Name : currAccName , Balance : accChange .Balance }
31
+ accList = append (accList , acc )
32
+ balances [currAccName ] = acc
37
33
} else {
38
- balances [ currAccName ] = ratNum .Add (accChange .Balance )
34
+ acc . Balance = acc . Balance .Add (accChange .Balance )
39
35
}
40
36
}
41
37
}
42
38
}
43
39
}
44
40
45
- accList := make ([]* Account , len (balances ))
46
- count := 0
47
- for accName , accBalance := range balances {
48
- account := & Account {Name : accName , Balance : accBalance }
49
- accList [count ] = account
50
- count ++
51
- }
52
-
53
41
slices .SortFunc (accList , func (a , b * Account ) int {
54
42
return strings .Compare (a .Name , b .Name )
55
43
})
0 commit comments