forked from tn47/goledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
248 lines (179 loc) · 7.17 KB
/
interface.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package api
import "time"
// Datastorer maintains the state of all accounts. Accounts are maintained as
// singleton objects.
type Datastorer interface {
// GetAccount() to get the singleton object, if a singleton is not already
// created for `name`, GetAccount() will create a new singleton object for
// that name, and return the same.
GetAccount(name string) Accounter
// GetCommodity() returns the commodity object by name or creates an new
// one. Note that this commodity object serves as the blue print for
// commodity instansiation.
GetCommodity(name string) Commoditiser
// Accountnames return list of all account names.
Accountnames() []string
// Commoditynames return list of all commodity names.
Commoditynames() []string
// Balance amount, for commodity specified by `obj`, after all transactions
// are tallied between the accounts. Note that each accounts can exchange
// with any number of commodities.
// `obj` can either be an instance implementing Commoditiser or it can be
// string calling out the commodity name.
Balance(obj interface{}) Commoditiser
// Balances return the list of all commodities' balance amounts after all
// transactions are tallied between the accounts.
Balances() []Commoditiser
// Firstpass to parse journal file.
Firstpass(obj interface{}) error
// Secondpass to apply journal entries.
Secondpass() error
// Firstpassok means firstpass completed on the datastore.
Firstpassok()
// Secondpassok means secondpass completed on the datastore.
Secondpassok()
// Clone this instance and all its nested reference.
Clone(Reporter) Datastorer
// AggregateTotal containing ledger for each posting.
AggregateTotal(Transactor, Poster) error
// IsCommodityDeclared return true if commodity is pre-declared
IsCommodityDeclared(name string) bool
// IsAccountDeclared return true if account is pre-declared
IsAccountDeclared(name string) bool
// IsPayeeDeclared return true if payee is pre-declared
IsPayeeDeclared(name string) bool
Formatter
}
// Transactor encapsulates a single transaction between one or more creditors
// and one or more debtors.
type Transactor interface {
// Date of transaction.
Date() time.Time
// Payee is the person or organization that receives money, a person or
// organization that is paid money.
Payee() string
// GetPostings return list of all postings under this transaction.
GetPostings() []Poster
// Crc64 return a hash value that can uniquely identify the transaction.
Crc64() uint64
// Journalfile in which this transaction is defined.
Journalfile() string
// Printlines return the original lines from which transaction was parsed.
Printlines() []string
}
// Poster encapsulates a single posting within a transaction.
type Poster interface {
// Commodity posted as credit or debit.
Commodity() Commoditiser
// Lotprice return the lot-price of this posting's commodity.
Lotprice() Commoditiser
// Costprice return the cost-price of this posting's commodity.
Costprice() Commoditiser
// Balanceprice return the balance-price of this posting's account.
Balanceprice() Commoditiser
// Payee for this posting, if unspecified in the posting, shall return the
// transaction's payee.
Payee() string
// IsCredit takes the amount from the account, giver.
IsCredit() bool
// IsDebit accumulates the amount to the account, taker.
IsDebit() bool
// Account to which the commodity should be posted.
Account() Accounter
}
// Commoditiser encapsulates a commodity.
type Commoditiser interface {
// Name of the commodity, like: $, INR, Gold, Oil etc...
Name() string
// Notes return list of notes declared on this commodity.
Notes() []string
// Amount as in quantity, not necessarily as value.
Amount() float64
// Currency is true if commodity is of type bool.
Currency() bool
// ApplyAmount commodities
ApplyAmount(other Commoditiser) error
// BalanceEqual is equality between two commodity, which implies equality
// in Name(), Amount() and Currency().
BalanceEqual(Commoditiser) (bool, error)
// IsCredit commodity is positive.
IsCredit() bool
// IsDebit commodity is negative.
IsDebit() bool
// MakeSimilar create a new instance of commodity simlar to this commodity
MakeSimilar(amount float64) Commoditiser
// Directive return the commodity details as directive declaration.
Directive() string
Clone(db Datastorer) Commoditiser
String() string
}
// Accounter encapsulates an account.
type Accounter interface {
// Name of the account.
Name() string
// Notes return list of notes declared on this account.
Notes() []string
// Balance amount, for commodity specified by `obj`, after all postings
// from all transactions are applied on to this account. Note that each
// accounts can exchange with any number of commodities.
// `obj` can either be an instance implementing Commoditiser or it can be
// string calling out the commodity name.
Balance(obj interface{}) Commoditiser
// Balances return the list of all commodities' balance amounts after all
// postings form all transactions are applied on to this account.
Balances() []Commoditiser
// HasPosting return true if this account has ever participated in a
// transaction posting.
HasPosting() bool
// IsIncome return true of account is declared as income account
IsIncome() bool
// IsExpense return true if account is declared as expense account
IsExpense() bool
// Directive return the account details as directive declaration.
Directive() string
Formatter
}
// Reporter encapsulates callbacks that can be used by report generating
// plugins.
type Reporter interface {
Firstpass(Datastorer, Transactor, Poster) error
Transaction(Datastorer, Transactor) error
Posting(Datastorer, Transactor, Poster) error
BubblePosting(Datastorer, Transactor, Poster, Accounter) error
Render(args []string, db Datastorer)
Startjournal(journalfile string, included bool)
Clone() Reporter
}
// Formatter implements are uniform tabularized {row,column} formatting across
// all types under dblentry package.
type Formatter interface {
// FmtBalances used for `balance` reporting.
FmtBalances(Datastorer, Transactor, Poster, Accounter) [][]string
// FmtDCBalances used for `balance` reporting, in debit-credit format.
FmtDCBalances(Datastorer, Transactor, Poster, Accounter) [][]string
// FmtRegister used for `register` reporting.
FmtRegister(Datastorer, Transactor, Poster, Accounter) [][]string
// FmtEquity used for `equity` reporting.
FmtEquity(Datastorer, Transactor, Poster, Accounter) [][]string
// FmtPassbook used for `passbook` reporting.
FmtPassbook(Datastorer, Transactor, Poster, Accounter) [][]string
}
// Store maintains an index of key,vlue pairs, key being time.Time and value
// as interface.
type Store interface {
// Insert a new {key,value} pair
Insert(k time.Time, v interface{}) error
// Range over {key,value} pairs, entries can be used for returning the
// results.
Range(low, high *time.Time, incl string, entries []TimeEntry) []TimeEntry
// Make an immutable copy of store.
Clone()
}
// TimeEntry captures a single key,value pair, key being time.Time and value
// as interface.
type TimeEntry interface {
// Key return the key part.
Key() time.Time
// Value return the value part.
Value() interface{}
}