miser

package module
v0.0.0-...-93355e3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 13 Imported by: 0

README

miser - tiny ledger software

codecov goreportcard

Documentation

Index

Constants

View Source
const (
	Asset     = "Asset"
	Liability = "Liability"
	Equity    = "Equity"
	Income    = "Income"
	Expense   = "Expense"
)
View Source
const (
	Credit = iota
	Debit
)
View Source
const (
	ACCOUNTS_FILE     = "miser.ar"
	TRANSACTIONS_FILE = "miser.tr"
	BALANCE_FILE      = "miser.br"
	TAGS_FILE         = "miser.tg"
	TAGS_MAPPING_FILE = "miser.tm"
)
View Source
const (
	Initial    = "Initial"
	Unexpected = "Unexpected"

	// todo add analysis of transactions during the load and mark some transactions as:
	OverAverage = "OverAverage"
	Periodic    = "Periodic"
)

Special system tag names:

View Source
const (
	Uncleared = iota // recorded but not yet reconciled; needs review
	Pending          // tentatively reconciled (if needed, eg during a big reconciliation)
	Cleared          // complete, reconciled as far as possible, and considered correct
)
View Source
const Million = 1_000_000
View Source
const RANDOM_BYTES_LENGTH = 10

Variables

This section is empty.

Functions

func CleanUp

func CleanUp[E Entities](fpath string) int

Remove all entites from journal marked for deletion.

func InitCypher

func InitCypher(key string)

Init cypher service for a given key.

func Load

func Load[E Entities, R Registry[E]](registry R, fpath string) (n int, err error)

func Save

func Save[E Entities, R Registry[E]](registry R, fpath string) (n int, err error)

Types

type Account

type Account struct {
	ID                    ID
	Name, Type, Desc, Cur EncryptedString
	OpenedAt, ClosedAt    time.Time
	Deleted               bool
}

type AccountRegistry

type AccountRegistry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func CreateAccountRegistry

func CreateAccountRegistry() *AccountRegistry

func (*AccountRegistry) Add

func (ar *AccountRegistry) Add(a Account) int

func (*AccountRegistry) AddQueued

func (ar *AccountRegistry) AddQueued(a Account)

func (*AccountRegistry) Get

func (ar *AccountRegistry) Get(accID ID) *Account

func (*AccountRegistry) List

func (ar *AccountRegistry) List() map[ID]Account

func (*AccountRegistry) Load

func (ar *AccountRegistry) Load() (int, error)

func (*AccountRegistry) Save

func (ar *AccountRegistry) Save() (int, error)

func (*AccountRegistry) SyncQueued

func (ar *AccountRegistry) SyncQueued() []Account

type Balance

type Balance struct {
	Account, Transaction ID // in fact the id of balance item is transaction id
	Value                int64
}

Balance is value object, it is immutable, do not try to change it, do create another one instead, the last version will be used (see Add method of BalanceRegistry).

func (Balance) Amount

func (b Balance) Amount() float64

func (Balance) ID

func (b Balance) ID() string

type BalanceRegistry

type BalanceRegistry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func CreateBalanceRegistry

func CreateBalanceRegistry() *BalanceRegistry

func (*BalanceRegistry) Add

func (br *BalanceRegistry) Add(b Balance) int

func (*BalanceRegistry) AddQueued

func (br *BalanceRegistry) AddQueued(b Balance)

func (*BalanceRegistry) List

func (br *BalanceRegistry) List() []Balance

func (*BalanceRegistry) Load

func (br *BalanceRegistry) Load() (int, error)

func (*BalanceRegistry) Save

func (br *BalanceRegistry) Save() (int, error)

func (*BalanceRegistry) SyncQueued

func (br *BalanceRegistry) SyncQueued() (changes []Balance)

func (*BalanceRegistry) TransactionBalance

func (br *BalanceRegistry) TransactionBalance(accID, trID ID) *Balance

Find a balance of given account transaction. Keep in mind: every transaction creates two different balances for source and destination accounts.

type Currency

type Currency struct{ Code, Name, Sign string }

type CurrencyRegistry

type CurrencyRegistry map[string]Currency

func CreateCurrencyRegistry

func CreateCurrencyRegistry() *CurrencyRegistry

func (CurrencyRegistry) Get

func (cr CurrencyRegistry) Get(code string) *Currency

type Cypher

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

Cypher provides ecrypt and decrypt methods.

type EncryptedString

type EncryptedString string

func (EncryptedString) MarshalJSON

func (s EncryptedString) MarshalJSON() ([]byte, error)

func (*EncryptedString) UnmarshalJSON

func (s *EncryptedString) UnmarshalJSON(b []byte) error

type Entities

type Entities interface {
	Account | Transaction | Balance | Tag | TagMap
}

type ID

type ID string

func CreateID

func CreateID() ID

type Ledger

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

func (*Ledger) AccountAmount

func (l *Ledger) AccountAmount(accID ID) float64

Account amount.

func (*Ledger) AccountBalance

func (l *Ledger) AccountBalance(accID ID) *Balance

Account balance: balance at time of last transaction.

func (*Ledger) AmountTransaction

func (l *Ledger) AmountTransaction(t *Transaction) string

func (*Ledger) CreateAccount

func (l *Ledger) CreateAccount(n, t, d, c string, openedAt time.Time, initBalance float64) (*Account, error)

func (*Ledger) CreateBalance

func (l *Ledger) CreateBalance(accID, trID ID, value int64) *Balance

func (*Ledger) CreateInitialTransaction

func (l *Ledger) CreateInitialTransaction(accID ID, openedAt time.Time, v int64) *Transaction

func (*Ledger) CreateTransaction

func (l *Ledger) CreateTransaction(src, dst ID, t time.Time, v float64, txt string) (*Transaction, error)

func (*Ledger) Save

func (l *Ledger) Save()

Save all queued data, sync it to disk.

func (*Ledger) UpdateBalance

func (l *Ledger) UpdateBalance(accID, trID ID, accType string, operType int, trTime time.Time, value int64) error

Credit - source, Debit - destination

type Registry

type Registry[E Entities] interface {
	*AccountRegistry | *TransactionRegistry | *BalanceRegistry | *TagRegistry | *TagMapRegistry

	Add(e E) int
	SyncQueued() []E
}

type Tag

type Tag struct {
	ID      ID
	Name    EncryptedString
	Deleted bool
}

Tag ties name of a tag and its id, nothing more. For tagging object use TagMap.

type TagMap

type TagMap struct {
	Tag, Item ID
}

TagMap ties together a tag and some object: account, transaction etc.

type TagMapRegistry

type TagMapRegistry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func CreateTagsMapRegistry

func CreateTagsMapRegistry() *TagMapRegistry

func (*TagMapRegistry) Add

func (tm *TagMapRegistry) Add(t TagMap) int

func (*TagMapRegistry) AddQueued

func (tm *TagMapRegistry) AddQueued(t TagMap)

func (*TagMapRegistry) Create

func (tm *TagMapRegistry) Create(tagID, itemID ID)

func (*TagMapRegistry) Items

func (tm *TagMapRegistry) Items(tagID ID) (items []ID)

func (*TagMapRegistry) Load

func (tm *TagMapRegistry) Load() (int, error)

func (*TagMapRegistry) Save

func (tm *TagMapRegistry) Save() (int, error)

func (*TagMapRegistry) SyncQueued

func (tm *TagMapRegistry) SyncQueued() []TagMap

func (*TagMapRegistry) Tags

func (tm *TagMapRegistry) Tags(itemID ID) (tags []ID)

type TagRegistry

type TagRegistry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func CreateTagRegistry

func CreateTagRegistry() *TagRegistry

func (*TagRegistry) Add

func (tg *TagRegistry) Add(t Tag) int

func (*TagRegistry) AddQueued

func (tg *TagRegistry) AddQueued(t Tag)

func (*TagRegistry) Create

func (tg *TagRegistry) Create(n string) *Tag

func (*TagRegistry) GetById

func (tg *TagRegistry) GetById(tagID ID) *Tag

func (*TagRegistry) GetByName

func (tg *TagRegistry) GetByName(n string) *Tag

func (*TagRegistry) List

func (tg *TagRegistry) List() map[ID]Tag

List all tags.

func (*TagRegistry) Load

func (tg *TagRegistry) Load() (int, error)

func (*TagRegistry) Save

func (tg *TagRegistry) Save() (int, error)

func (*TagRegistry) SyncQueued

func (tg *TagRegistry) SyncQueued() []Tag

type Transaction

type Transaction struct {
	ID, Source, Dest ID
	Time             time.Time
	Text             EncryptedString
	Value            int64 // in millionths
	State            int   // one of: Uncleared, Pending, Cleared
	Deleted          bool
}

func (*Transaction) IsInitial

func (t *Transaction) IsInitial() bool

type TransactionRegistry

type TransactionRegistry struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func CreateTransactionRegistry

func CreateTransactionRegistry() *TransactionRegistry

func (*TransactionRegistry) Add

func (tr *TransactionRegistry) Add(t Transaction) int

func (*TransactionRegistry) AddQueued

func (tr *TransactionRegistry) AddQueued(t Transaction)

func (*TransactionRegistry) AllAfter

func (tr *TransactionRegistry) AllAfter(accID ID, trTime time.Time) (trs []Transaction)

Find all transactions of account after given time.

func (*TransactionRegistry) FirstBefore

func (tr *TransactionRegistry) FirstBefore(accID ID, trTime time.Time) *Transaction

Find a transaction of account before given time.

func (*TransactionRegistry) Last

func (tr *TransactionRegistry) Last(accID ID) *Transaction

Find last transaction.

func (*TransactionRegistry) List

func (tr *TransactionRegistry) List() (transactions []Transaction)

func (*TransactionRegistry) Load

func (tr *TransactionRegistry) Load() (int, error)

func (*TransactionRegistry) Save

func (tr *TransactionRegistry) Save() (int, error)

func (*TransactionRegistry) SyncQueued

func (tr *TransactionRegistry) SyncQueued() []Transaction

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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