accounting

package module
v0.0.0-...-be74457 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

README

Back-ends

txtdb

TODO: github.com/fsnotify/fsnotify

PostgreSQL

Ledger

TODO: github.com/fsnotify/fsnotify

File format

accounting uses a ledger file format mostly compatible with the original "ledger" and with "hledger". See https://www.ledger-cli.org/3.0/doc/ledger3.html and https://hledger.org/journal.html

To-Do list

  • ledger: notifier
  • ledger: implement "-b" (begin date) and "-e" (end date) (print, balance, stats)
  • ledger: implement "--value", "--cost", "--market", "--exchange"
  • tacc: improve. Get ideas from hledger-ui

Documentation

Overview

Package accounting implements a double-entry accounting system.

It can use ledger, txtdb or PostgreSQL back-ends

Index

Constants

View Source
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

func Register

func Register(name string, driver Driver)

Register makes an accounting backend available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

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.

var TransferAccount Account = Account{
	Name: "Assets:Transfer account",
}

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

func SortAccounts(accounts []*Account) []*Account

SortAccounts returns a properly sorted copy of a slice of accounts. Input parameter "accounts" may be modified by this function.

func (Account) FullName

func (a Account) FullName() string

FullName returns the fully qualified name of the account: the name of all its ancestors, separated by ":", and ending with this account's name.

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) Add

func (b *Balance) Add(v Value)

Add adds a value to a balance.

func (*Balance) AddBalance

func (b *Balance) AddBalance(b2 Balance)

AddBalance adds one balance to another

func (Balance) Dup

func (b Balance) Dup() Balance

Dup duplicates a Balance.

func (Balance) String

func (b Balance) String() string

String returns "0" for empty balances, or a list of its values separated by commas.

func (*Balance) Sub

func (b *Balance) Sub(v Value)

Sub substracts a value to a balance.

func (*Balance) SubBalance

func (b *Balance) SubBalance(b2 Balance)

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

func Open(dataSource string) (*Ledger, error)

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) Account

func (l *Ledger) Account(id ID) *Account

Account returns details for one account, given its ID.

func (*Ledger) Clone

func (l *Ledger) Clone() *Ledger

Clone returns a deep copy of l.

func (*Ledger) Close

func (l *Ledger) Close() error

Close closes the ledger and prevents new queries from starting.

func (*Ledger) Convert

func (l *Ledger) Convert(v Value, when time.Time, currency *Currency) (Value, error)

Convert returns a value to another currency.

func (*Ledger) EditAccount

func (l *Ledger) EditAccount(a Account) (*Account, error)

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) Fill

func (l *Ledger) Fill() error

Fill re-calculates all the automatic fields in all the accounting data.

func (*Ledger) Flush

func (l *Ledger) Flush() error

Flush writes all the pending changes to the backend.

func (*Ledger) GetBalance

func (l *Ledger) GetBalance(account *Account, when time.Time) Balance

GetBalance gets an account balance at a given time. If passed the zero value, it gets the current balance.

func (*Ledger) GetCurrency

func (l *Ledger) GetCurrency(s string) (*Currency, bool)

GetCurrency returns a Currency, given its name, and whether it is a new one or not

func (*Ledger) NewAccount

func (l *Ledger) NewAccount(a Account) (*Account, error)

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 Tag

type Tag struct {
	Name  string
	Value string
}

A Tag is a label which can be added to a transaction or movement.

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

func (value Value) FullString() string

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

func (value Value) GetString(full bool, units bool) string

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).

func (*Value) Mul

func (value *Value) Mul(v2 Value)

Mul multiplies a value times the amount of another.

func (Value) String

func (value Value) String() string

String returns a string with the correct representation of that value, including its currency. The amount is represented with just the default digits in the currency definition.

Directories

Path Synopsis
backend
postgres
Package postgres is a Postgres driver for the github.com/cespedes/accounting package.
Package postgres is a Postgres driver for the github.com/cespedes/accounting package.
cmd
acc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL