59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package ledger
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Balances map[string]Balance
|
|
|
|
type Balance map[Currency]float64
|
|
|
|
func (balances Balances) PushAll(other Balances) {
|
|
for k, v := range other {
|
|
if _, ok := balances[k]; !ok {
|
|
balances[k] = make(Balance)
|
|
}
|
|
for k2, v2 := range v {
|
|
if _, ok := balances[k][k2]; !ok {
|
|
balances[k][k2] = 0
|
|
}
|
|
balances[k][k2] += v2
|
|
}
|
|
}
|
|
}
|
|
|
|
func (balances Balances) Push(d Delta) {
|
|
if _, ok := balances[d.Name]; !ok {
|
|
balances[d.Name] = make(Balance)
|
|
}
|
|
balances[d.Name].Push(d)
|
|
}
|
|
|
|
func (balance Balance) Push(d Delta) {
|
|
if _, ok := balance[d.Currency]; !ok {
|
|
balance[d.Currency] = 0
|
|
}
|
|
balance[d.Currency] += d.Value
|
|
}
|
|
|
|
func (balances Balances) Debug() string {
|
|
result := []string{}
|
|
for k, v := range balances {
|
|
result = append(result, fmt.Sprintf("%s:[%s]", k, v.Debug()))
|
|
}
|
|
return strings.Join(result, " ")
|
|
}
|
|
|
|
func (balance Balance) Debug() string {
|
|
result := []string{}
|
|
for k, v := range balance {
|
|
if k == USD {
|
|
result = append(result, fmt.Sprintf("%s%.2f", k, v))
|
|
} else {
|
|
result = append(result, fmt.Sprintf("%.3f %s", v, k))
|
|
}
|
|
}
|
|
return strings.Join(result, " + ")
|
|
}
|