-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsie.go
159 lines (141 loc) · 3.12 KB
/
sie.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sie
import (
"fmt"
"strconv"
"strings"
"time"
)
type Decimal int64 // "cents"
func (d Decimal) String() string {
if d%100 == 0 {
return d.FloatString(0)
}
return d.FloatString(2)
}
func (d Decimal) FloatString(decimals int) string {
if decimals <= 0 {
return fmt.Sprintf("%d", (d+50)/100)
}
abs := d
if d < 0 {
abs = -d
}
return fmt.Sprintf("%d.%0*d", d/100, decimals, abs%100)
}
func (d Decimal) Float64() float64 {
return float64(d) / 100
}
func (d Decimal) MarshalJSON() ([]byte, error) {
return []byte(d.String()), nil
}
func ParseDecimal(s string) (Decimal, error) {
wholeStr, fracStr, ok := strings.Cut(s, ".")
if !ok {
wholeStr = s
fracStr = "0"
}
whole, err := strconv.ParseInt(wholeStr, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse %q (whole part): %v", s, err)
}
if len(fracStr) == 1 {
fracStr += "0"
}
frac, err := strconv.ParseInt(fracStr, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse %q (fractional part): %v", s, err)
}
if whole < 0 {
frac = -frac
}
return Decimal(whole*100 + frac), nil
}
type Document struct {
ProgramName string
ProgramVersion string
GeneratedAt time.Time
GeneratedBy string
Type string
OrgNo string
CompanyName string
AccountPlan string
Accounts []Account
Entries []Entry
Starts time.Time
Ends time.Time
Annotations []Annotation
}
type Account struct {
ID int
Type string
Description string
InBalance Decimal
OutBalance Decimal
}
type Entry struct {
ID string
Type string
Date time.Time
Description string
Filed time.Time
Transactions []Transaction
}
type Transaction struct {
AccountID int
Annotations []Annotation
Amount Decimal
}
type Annotation struct {
Tag int
Text string
Description string
}
func (a Annotation) Equals(other Annotation) bool {
return a.Tag == other.Tag && a.Text == other.Text
}
func (a Annotation) String() string {
if a.Description != "" {
return a.Description
}
return fmt.Sprintf("%d-%s", a.Tag, a.Text)
}
func (d *Document) CopyForAnnotation(ann Annotation) *Document {
cpy := *d
cpy.Entries = make([]Entry, 0, len(d.Entries))
for _, e := range d.Entries {
e2 := e
e2.Transactions = make([]Transaction, 0, len(e.Transactions))
for _, t := range e.Transactions {
for _, a := range t.Annotations {
if a.Equals(ann) {
e2.Transactions = append(e2.Transactions, t)
break
}
}
}
if len(e2.Transactions) > 0 {
cpy.Entries = append(cpy.Entries, e2)
}
}
return &cpy
}
func (d *Document) CopyWithoutAnnotations() *Document {
cpy := *d
cpy.Entries = make([]Entry, 0, len(d.Entries))
for _, e := range d.Entries {
e2 := e
e2.Transactions = make([]Transaction, 0, len(e.Transactions))
for _, t := range e.Transactions {
if len(t.Annotations) == 0 {
e2.Transactions = append(e2.Transactions, t)
}
}
if len(e2.Transactions) > 0 {
cpy.Entries = append(cpy.Entries, e2)
}
}
return &cpy
}
func (d *Document) AddEntriesFrom(other *Document) {
d.Entries = append(d.Entries, other.Entries...)
}