ledger

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: May 18, 2021 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DateFormat = "2006/01/02"
)
View Source
const (

	// OpeningBalanceID is an ID used only for the ledger's opening balances transaction (in an equity: posting)
	OpeningBalanceID = "Opening-Balance"
)

Variables

This section is empty.

Functions

func NewValidateError

func NewValidateError(firstFailure int, cause error) error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func (Error) Error

func (e Error) Error() string

type Ledger

type Ledger struct {
	// contains filtered or unexported fields
}

Ledger tracks transactions from multiple institutions. Include error checking and validation for all ledger changes. Serializes into a "plain-text accounting" ledger file.

func New

func New(transactions []Transaction) (*Ledger, error)

New creates a ledger with the given transactions. Must not contain any duplicate IDs

func NewFromReader

func NewFromReader(reader io.Reader) (*Ledger, error)

NewFromReader creates a ledger from the given "plain-text accounting" ledger-encoded reader

func (*Ledger) AccountBalance added in v0.6.0

func (l *Ledger) AccountBalance(account string, start, end time.Time) decimal.Decimal

AccountBalance returns the cumulative sum of all postings for 'account' between start and end times

func (*Ledger) AddTransactions

func (l *Ledger) AddTransactions(txns []Transaction) error

AddTransactions attempts to add the provided transactions. Returns an error if the ledger fails validation (i.e. fail balance assertions). In the event of an error, attempts to add all valid transactions up to the error.

func (*Ledger) Balances added in v0.2.0

func (l *Ledger) Balances() (start, end *time.Time, balances map[string][]decimal.Decimal)

Balances returns a cumulative balance sheet for all accounts over the given time period. Current interval is monthly.

func (*Ledger) FirstTransactionTime added in v0.4.0

func (l *Ledger) FirstTransactionTime() time.Time

FirstTransactionTime returns the first transaction's Date field. Returns 0 if there are no transactions

func (*Ledger) LastTransactionTime

func (l *Ledger) LastTransactionTime() time.Time

LastTransactionTime returns the last transaction's Date field. Returns 0 if there are no transactions

func (*Ledger) LeftOverAccountBalances added in v0.6.0

func (l *Ledger) LeftOverAccountBalances(start, end time.Time, accounts ...string) map[string]decimal.Decimal

LeftOverAccountBalances retrieves balances for any accounts or account prefixes not found in 'accounts' between start and end times

func (*Ledger) OpeningBalances added in v0.4.0

func (l *Ledger) OpeningBalances() (opening Transaction, found bool)

OpeningBalances attempts to find the opening balances transaction and return it

func (*Ledger) Query added in v0.3.0

func (l *Ledger) Query(options QueryOptions, page, results int) QueryResult

Query searches the ledger and paginates the results

func (*Ledger) RenameAccount added in v0.6.0

func (l *Ledger) RenameAccount(oldName, newName, oldID, newID string) int

RenameAccount replaces 'oldName' prefixes with a 'newName' prefix Returns the number of renamed postings

func (*Ledger) Size added in v0.8.0

func (l *Ledger) Size() int

func (*Ledger) String

func (l *Ledger) String() string

func (*Ledger) Transaction added in v0.8.0

func (l *Ledger) Transaction(id string) (txn Transaction, found bool)

func (*Ledger) UpdateAccount added in v0.3.0

func (l *Ledger) UpdateAccount(oldAccount, newAccount string) error

UpdateAccount changes all transactions' accounts matching oldAccount to newAccount

func (*Ledger) UpdateOpeningBalance added in v0.4.0

func (l *Ledger) UpdateOpeningBalance(opening Transaction) error

UpdateOpeningBalance inserts or updates an account's opening balance for this ledger.

func (*Ledger) UpdateTransaction added in v0.3.0

func (l *Ledger) UpdateTransaction(id string, transaction Transaction) error

UpdateTransaction replaces a transaction where ID is 'id' with 'transaction' The new transaction must be valid

func (*Ledger) Validate

func (l *Ledger) Validate() error

Validate returns a descriptive error should anything be wrong with the current ledger's transactions

type Posting

type Posting struct {
	Account  string
	Amount   decimal.Decimal
	Balance  *decimal.Decimal `json:",omitempty"`
	Comment  string           `json:",omitempty"`
	Currency string
	Tags     map[string]string `json:",omitempty"`
}

func NewPostingFromString

func NewPostingFromString(line string) (Posting, error)

func (Posting) FormatTable

func (p Posting) FormatTable(accountLen, amountLen int) string

func (Posting) ID

func (p Posting) ID() string

func (Posting) IsOpeningBalance added in v0.4.0

func (p Posting) IsOpeningBalance() bool

IsOpeningBalance returns true if this matches an "opening balance" posting, false otherwise

func (Posting) String

func (p Posting) String() string

type QueryOptions added in v0.8.0

type QueryOptions struct {
	Search   string    `form:"search"`
	Start    time.Time `form:"start"`
	End      time.Time `form:"end"`
	Accounts []string  `form:"accounts[]"`
}

QueryOptions contains all available options to query the ledger

type QueryResult added in v0.3.0

type QueryResult struct {
	Count        int
	Page         int
	Results      int
	Transactions []Transaction
}

QueryResult is a paginated search result containing relevant transactions

type Store added in v0.8.3

type Store struct {
	*Ledger
	// contains filtered or unexported fields
}

Store enables ledger syncing both in memory and on disk

func NewStore added in v0.8.3

func NewStore(file vcs.File, logger *zap.Logger) (*Store, error)

NewStore creates a Ledger Store from the given file

func (*Store) AddTransactions added in v0.8.3

func (s *Store) AddTransactions(txns []Transaction) error

AddTransactions wraps ledger.AddTransactions and syncs changes to disk

func (*Store) RenameAccount added in v0.8.3

func (s *Store) RenameAccount(oldName, newName, oldID, newID string) (int, error)

RenameAccount wraps ledger.RenameAccount and syncs changes to disk

func (*Store) Resync added in v0.8.3

func (s *Store) Resync(download downloader, processTxns txnMutator)

Resync runs Sync from the first date in the ledger until now

func (*Store) StartSync added in v0.8.3

func (s *Store) StartSync(start, end time.Time, download downloader, processTxns txnMutator)

StartSync asynchronously downloads and processes new transactions between the start and end dates If a partial failure occurs during the sync, writes to disk anyway

func (*Store) SubmitSyncPrompt added in v0.8.4

func (s *Store) SubmitSyncPrompt(response prompter.Response)

func (*Store) SyncRecent added in v0.8.3

func (s *Store) SyncRecent(download downloader, processTxns txnMutator)

SyncRecent runs Sync for any new transactions since the last sync. Currently assumes last the last txn's date should be the start date.

func (*Store) SyncStatus added in v0.8.3

func (s *Store) SyncStatus() (syncing bool, prompt *prompter.Request, lastSyncErr error)

SyncStatus returns whether sync is running and the most recent sync error

func (*Store) UpdateAccount added in v0.8.3

func (s *Store) UpdateAccount(oldAccount, newAccount string) error

UpdateAccount wraps ledger.UpdateAccount and syncs changes to disk

func (*Store) UpdateOpeningBalance added in v0.8.3

func (s *Store) UpdateOpeningBalance(opening Transaction) error

UpdateOpeningBalance wraps ledger.UpdateOpeningBalance and syncs changes to disk

func (*Store) UpdateTransaction added in v0.8.3

func (s *Store) UpdateTransaction(id string, txn Transaction) error

UpdateTransaction wraps ledger.UpdateTransaction and syncs changes to disk

func (*Store) UpdateTransactions added in v0.8.3

func (s *Store) UpdateTransactions(txns map[string]Transaction) error

UpdateTransactions wraps ledger.UpdateTransactions and syncs changes to disk

type Transaction

type Transaction struct {
	Comment  string `json:",omitempty"`
	Date     time.Time
	Payee    string
	Postings []Posting
	Tags     map[string]string `json:",omitempty"`
}

Transaction is a strict(er) representation of a ledger transaction. The extra restrictions are used to verify correctness more easily.

func (Transaction) Balanced

func (t Transaction) Balanced() bool

func (Transaction) ID

func (t Transaction) ID() string

func (Transaction) String

func (t Transaction) String() string

func (Transaction) Validate

func (t Transaction) Validate() error

type Transactions

type Transactions []*Transaction

func (Transactions) Sort

func (txns Transactions) Sort()

Jump to

Keyboard shortcuts

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