-
Notifications
You must be signed in to change notification settings - Fork 42
/
printEquity.go
68 lines (58 loc) · 1.71 KB
/
printEquity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cmd
import (
"log"
"os"
"slices"
"strings"
"time"
"github.com/howeyc/ledger"
"github.com/howeyc/ledger/decimal"
"github.com/spf13/cobra"
)
// equityCmd represents the equity command
var equityCmd = &cobra.Command{
Use: "equity",
Short: "Print account equity as transaction",
Run: func(cmd *cobra.Command, args []string) {
generalLedger, err := cliTransactions()
if err != nil {
log.Fatalln(err)
}
var trans ledger.Transaction
trans.Payee = "Opening Balances"
trans.Date = time.Now()
if len(generalLedger) > 0 {
trans.Date = generalLedger[len(generalLedger)-1].Date
}
balances := make(map[string]decimal.Decimal)
for _, trans := range generalLedger {
for _, accChange := range trans.AccountChanges {
if decNum, ok := balances[accChange.Name]; !ok {
balances[accChange.Name] = accChange.Balance
} else {
balances[accChange.Name] = decNum.Add(accChange.Balance)
}
}
}
for name, bal := range balances {
if !bal.IsZero() {
trans.AccountChanges = append(trans.AccountChanges, ledger.Account{
Name: name,
Balance: bal,
})
}
}
slices.SortFunc(trans.AccountChanges, func(a, b ledger.Account) int {
return strings.Compare(a.Name, b.Name)
})
WriteTransaction(os.Stdout, &trans, 80)
},
}
func init() {
rootCmd.AddCommand(equityCmd)
var startDate, endDate time.Time
startDate = time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)
endDate = time.Now()
equityCmd.Flags().StringVarP(&startString, "begin-date", "b", startDate.Format(transactionDateFormat), "Begin date of transaction processing.")
equityCmd.Flags().StringVarP(&endString, "end-date", "e", endDate.Format(transactionDateFormat), "End date of transaction processing.")
}