-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposting.go
92 lines (81 loc) · 2.03 KB
/
posting.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package coin
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type Posting struct {
Notes []string
Tags Tags
Transaction *Transaction
Account *Account
Quantity *Amount // posting amount
Balance *Amount // account balance as of this posting
BalanceAsserted bool // was balance explicitly asserted in the ledger
accountName string
}
func (s *Posting) Write(w io.Writer, accountOffset, accountWidth, amountWidth int, ledger bool) error {
notes := s.Notes
commodity := s.Quantity.Commodity
line := fmt.Sprintf("%*s%-*s %*.*f %s",
accountOffset, "",
accountWidth, s.Account.FullName,
amountWidth, commodity.Decimals, s.Quantity, commodity.SafeId(ledger))
if s.BalanceAsserted {
commodity = s.Balance.Commodity
line += fmt.Sprintf(" = %.*f %s", commodity.Decimals, s.Balance, commodity.SafeId(ledger))
}
if len(notes) > 0 && len(notes[0])+len(line) < TRANSACTION_LINE_MAX-3 {
line += " ; " + notes[0]
notes = notes[1:]
}
err := writeStrings(w, nil, line, "\n")
if err != nil {
return err
}
for _, n := range notes {
err := writeStrings(w, nil, " ; ", n, "\n")
if err != nil {
return err
}
}
return nil
}
func (s *Posting) String() string {
var b strings.Builder
s.Write(&b, 0, len(s.Account.FullName)+2, bigLog10(s.Quantity.Int)+3, false)
return b.String()
}
func (s *Posting) IsEqual(s2 *Posting) bool {
return s.Account == s2.Account &&
s.Quantity.IsEqual(s2.Quantity)
}
func (p *Posting) MoveTo(a *Account) {
if p.Account == a {
return
}
p.Account.deletePosting(p)
a.addPosting(p)
p.Account = a
}
func (p *Posting) drop() {
p.Account.deletePosting(p)
}
func (p *Posting) MarshalJSON() ([]byte, error) {
var value = map[string]interface{}{
"account": p.Account.FullName,
"quantity": p.Quantity,
}
if p.Balance != nil {
value["balance"] = p.Balance
value["balance_asserted"] = p.BalanceAsserted
}
if len(p.Notes) > 0 {
value["notes"] = p.Notes
}
if p.Tags != nil {
value["tags"] = p.Tags
}
return json.MarshalIndent(value, "", "\t")
}