Documentation ¶
Overview ¶
Package accounting implements a double-entry accounting system.
It can use ledger, txtdb or PostgreSQL back-ends
Index ¶
- Constants
- func Register(name string, driver Driver)
- type Account
- type Backend
- type Balance
- type ConnExtra
- type Connection
- type Currency
- type Driver
- type ID
- type Ledger
- func (l *Ledger) Account(id ID) *Account
- func (l *Ledger) Clone() *Ledger
- func (l *Ledger) Close() error
- func (l *Ledger) Convert(v Value, when time.Time, currency *Currency) (Value, error)
- func (l *Ledger) EditAccount(a Account) (*Account, error)
- func (l *Ledger) EditTransaction(t Transaction) (*Transaction, error)
- func (l *Ledger) Fill() error
- func (l *Ledger) Flush() error
- func (l *Ledger) GetBalance(account *Account, when time.Time) Balance
- func (l *Ledger) GetCurrency(s string) (*Currency, bool)
- func (l *Ledger) NewAccount(a Account) (*Account, error)
- func (l *Ledger) NewTransaction(t Transaction) (*Transaction, error)
- func (l *Ledger) Refresh()
- func (l *Ledger) TransactionsInAccount(account ID) []*Transaction
- func (l *Ledger) TransactionsInInterval(start, end time.Time) []*Transaction
- type Price
- type Split
- type Tag
- type Transaction
- type Value
Constants ¶
const U = 100_000_000
U is the number by which every amount must be multiplied before storing it.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Account ¶
type Account struct { ID ID // used to identify this account. Parent *Account // Optional Children []*Account // Automatically filled. Level int // Number of ancestors does this Account have. Automatically filled. Name string // Common (short) name (ie, "Cash") Code string // Optional. For example, account number Splits []*Split // List of movements in this account StartBalance Balance // Balance at the start of current period (zero if no start date was specified) }
Account specifies one origin or destination of funds.
TransferAccount is a special account used when a transaction has two or more splits with different times. Ledger.Fill() automatically generates splits with this account.
func SortAccounts ¶
SortAccounts returns a properly sorted copy of a slice of accounts. Input parameter "accounts" may be modified by this function.
type Backend ¶
type Backend struct { Ledger *Ledger // contains filtered or unexported fields }
Backend contains the Ledger and some methods to be called only by the backends.
func (*Backend) NewTransaction ¶
func (b *Backend) NewTransaction(t *Transaction) error
NewTransaction adds a new transaction to the ledger, updating the ledger's Accounts and Transactions fields. It also runs some sanity checks.
type Balance ¶
type Balance []Value
Balance is a list of currencies and amounts.
func (*Balance) AddBalance ¶
AddBalance adds one balance to another
func (Balance) String ¶
String returns "0" for empty balances, or a list of its values separated by commas.
func (*Balance) SubBalance ¶
SubBalance substracts one balance from another
type ConnExtra ¶
type ConnExtra interface { // Account returns the account with the specified id, if present Account(id int) *Account // GetBalance gets an account balance at a given time. // If passed the zero value, it gets the current balance. GetBalance(account int, t time.Time) int // TransactionsInAccount gets the list of all the transactions // involving that account. TransactionsInAccount(account int) []Transaction // TransactionsInInterval returns all the transactions between two times. TransactionsInInterval(start, end time.Time) []Transaction // NewAccount adds a new Account in a ledger. // ID field is ignored, and regenerated. NewAccount(a Account) (*Account, error) // EditAccount edits an Account in a ledger. // ID field must remain unchanged. EditAccount(a Account) (*Account, error) // NewTransaction adds a new Transaction in a ledger NewTransaction(t Transaction) (*Transaction, error) // EditTransaction edits a Transaction in a ledger EditTransaction(t Transaction) (*Transaction, error) // Flush writes all the pending changes to the backend. // If not implemented, we suppose it is not necessary // and return nil. Flush() error }
ConnExtra contains some extra methods that Conn could support. If it supports any ot these methods, the package will use them. If they are not available, it will fall back to another approach, or fail if it is not possible.
type Connection ¶
type Connection interface { // Close flushes, if necessary, and closes the connection. Close() error // Refresh loads again (if needed) all the accounting data. Refresh() }
Connection is a connection to an accounting backend. It should use the Backend.Ledger which was sent to Driver.Open()
type Currency ¶
type Currency struct { ID ID // used to identify this currency Name string // "EUR", "USD", etc PrintBefore bool // "$1.00" vs "1.00$" WithoutSpace bool // "1.00EUR" vs "1.00 EUR" Thousand string // What to use (if any) every 3 digits Decimal string // decimal separator ("." if empty) Precision int // Number of decimal places to show ISIN string // International Securities Identification Number }
Currency represents a currency or commodity, and stores its name and how to display it with an amount.
For more ideas on Currency, see github.com/leekchan/accounting
type Driver ¶
type Driver interface {
Open(url string, backend *Backend) (Connection, error)
}
Driver is the interface that must be implemented by the accounting backend.
type ID ¶
type ID interface {
String() string
}
ID is used to identify one currency, account, transaction, split or price.
type Ledger ¶
type Ledger struct { Accounts []*Account Transactions []*Transaction // sorted by Time. Currencies []*Currency // can be empty. Prices []*Price // can be empty; sorted by Time. Comments map[interface{}][]string // Comments in Accounts, Transactions, Currencies or Prices. Assertions map[*Split]Value // Value that should be in an account after one split. SplitPrices map[*Split]Value // Price for the value in a split, in another currency. DefaultCurrency *Currency // Default currency. // contains filtered or unexported fields }
Ledger stores all the accounts and transactions in one accounting.
func Open ¶
Open opens a ledger specified by a URL-like string, where the scheme is the backend name and the rest of the URL is backend-specific (usually consisting on a file name or a database name).
func (*Ledger) EditAccount ¶
EditAccount edits an Account in a ledger
func (*Ledger) EditTransaction ¶
func (l *Ledger) EditTransaction(t Transaction) (*Transaction, error)
EditTransaction edits a Transaction in a ledger
func (*Ledger) GetBalance ¶
GetBalance gets an account balance at a given time. If passed the zero value, it gets the current balance.
func (*Ledger) GetCurrency ¶
GetCurrency returns a Currency, given its name, and whether it is a new one or not
func (*Ledger) NewAccount ¶
NewAccount adds a new Account in a ledger
func (*Ledger) NewTransaction ¶
func (l *Ledger) NewTransaction(t Transaction) (*Transaction, error)
NewTransaction adds a new Transaction in a ledger
func (*Ledger) Refresh ¶
func (l *Ledger) Refresh()
Refresh loads again (if needed) all the accounting data.
func (*Ledger) TransactionsInAccount ¶
func (l *Ledger) TransactionsInAccount(account ID) []*Transaction
TransactionsInAccount gets the list of all the transactions involving that account.
func (*Ledger) TransactionsInInterval ¶
func (l *Ledger) TransactionsInInterval(start, end time.Time) []*Transaction
TransactionsInInterval returns all the transactions between two times.
type Price ¶
type Price struct { ID ID // used to identify this price. Time time.Time Currency *Currency Value Value }
Price declares a market price, which is an exchange rate between two currencies on a certain date.
type Split ¶
type Split struct { ID ID // used to identify this split. Account *Account // Origin or destination of funds. Transaction *Transaction // Transaction this split belongs to. Time *time.Time // In most cases, this is equal to Transaction.Time Value Value // Amount to be transferred. Balance Balance // Balance of this account, after this movement. }
Split is a deposit or withdrawal from an account.
type Transaction ¶
type Transaction struct { ID ID // used to identify this transaction. Time time.Time // Date and time Description string // Short description Splits []*Split // List of movements }
Transaction stores an entry in the journal, consisting in a timestamp, a description and two or more money movements from different accounts.
type Value ¶
type Value struct { Amount int64 // Amount (actual value times U) Currency *Currency // Currency or commodity }
Value specifies an amount and its currency
func (Value) FullString ¶
FullString returns a string with the correct representation of that value, including its currency. The amount is represented with all the relevant digits.
func (Value) GetString ¶
getString returns a string with the correct representation of that value, with or without its currency (units flag), using just the defaults digits for the currency, or all the non-zero ones (full flag).