gnucash

package module
v0.0.0-...-2ce5888 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

gnucash

GoDoc Build Status

This repository contains a simple Go library for reading GnuCash XML files.

It also contains a dump-transactions command-line program that can be used to print transactions to stdout.

Installation

Install Go and run go install ./cmd/dump-transactions.

Usage

Usage: dump-transactions [flag]... <FILE>
Dumps transactions from a GnuCash XML file.

  -aggregate string
    	Comma-separated list of fields for aggregating values, e.g. "date_year,account"
  -collapse value
    	Account whose children should be collapsed into it, e.g. "Expenses"
  -exclude value
    	Account (plus children) to exclude, e.g. "Assets" or "Expenses:Auto"
  -fields string
    	Comma-separated list of fields to print (default "date,account,value,desc")
  -format value
    	Output format (csv, json, text, tsv) (default tsv)
  -list-fields
    	Print available fields for -fields
  -max-date string
    	Maximum transaction date (YYYY-MM-DD, YYYY-MM, or YYYY)
  -min-date string
    	Minimum transaction date (YYYY-MM-DD, YYYY-MM, or YYYY)
  -only value
    	Account (plus children) to keep while excluding all others, e.g. "Expenses"

Dumping all splits from transactions in CSV format:

dump-transactions -format csv testdata/finances.gnucash
2019-01-01,Assets:Current Assets:Checking Account,2000.00,Starting balance
2019-01-01,Equity:Opening Balances,-2000.00,Starting balance
2019-06-23,Expenses:Books,200.00,Textbooks
2019-06-23,Assets:Current Assets:Checking Account,-200.00,Textbooks
...
2020-02-20,Expenses:Auto:Fuel,20.00,More gas
2020-02-20,Assets:Current Assets:Cash in Wallet,-20.00,More gas
2020-05-12,Expenses:Books,3.00,Used novel
2020-05-12,Assets:Current Assets:Cash in Wallet,-3.00,Used novel

Aggregating expenses by year and account while collapsing several accounts’ children:

dump-transactions \
  -aggregate date_year,account \
  -only Expenses \
  -exclude Expenses:Taxes \
  -collapse Expenses:Auto \
  -collapse Expenses:Utilities \
  -min-date 2010 \
  -max-date 2020 \
  -format text \
  testdata/finances.gnucash
2019  Expenses:Auto       100.00
2019  Expenses:Books      200.00
2020  Expenses:Auto       360.00
2020  Expenses:Books       18.00
2020  Expenses:Utilities  190.00

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ScanXML

func ScanXML(r io.Reader, opts ...ScanOption) error

ScanXML reads a GnuCash XML document from r. Options can be passed to configure its behavior.

func ScanXMLFile

func ScanXMLFile(p string, opts ...ScanOption) error

ScanXMLFile is a wrapper around ScanXML that reads a file at p. If the file is gzip-compressed, it is automatically decompressed.

Types

type Account

type Account struct {
	// ID is a GUID identifying this account.
	ID string
	// Name is a short name describing this account, e.g. "Credit Card" or "Parking".
	Name string
	// Desc is a longer description of the account.
	Desc string
	// Type describes how this account is used.
	Type AccountType
	// Commodity is the ID of the commodity held in this account, e.g. "USD".
	Commodity string
	// SCU is the "smallest commodity unit".
	//
	// https://code.gnucash.org/docs/STABLE/Account_8h.html: "The SCU [signifies] the smallest
	// non-zero amount that can be stored in the account. It is represented as the integer
	// denominator of a fraction. Thus, for example, a SCU of 12 means that 1/12 of something is the
	// smallest amount that can be stored in the account. SCU's can be any value; they do not need
	// to be decimal."
	//
	// For USD, this is 100, indicating that there are 100 cents in a dollar.
	SCU int
	// Parent points to this account's parent account. The root account's parent is nil.
	Parent *Account
	// contains filtered or unexported fields
}

func (*Account) FullName

func (a *Account) FullName() string

FullName returns the account's full colon-separated name, e.g. "Expenses:Auto:Parking".

func (*Account) String

func (a *Account) String() string

type AccountFilter

type AccountFilter struct {
	// Only contains names of accounts to consider for inclusion (along with their children);
	// all unmatched accounts will be automatically excluded. If Only is empty, all accounts
	// will be considered for inclusion.
	Only []string
	// Exclude contains names of accounts to exclude (along with their children).
	Exclude []string
	// Collapse contains names of accounts whose children should be collapsed into the named account.
	Collapse []string
}

AccountFilter describes how accounts should be filtered.

type AccountType

type AccountType int

AccountType describes how an account is used.

This is based on the GNCAccountType enum described at https://code.gnucash.org/docs/STABLE/group__Account.html.

const (
	AccountAsset AccountType = iota
	AccountBank
	AccountCash
	AccountCredit
	AccountCurrency
	AccountEquity
	AccountExpense
	AccountIncome
	AccountLiability
	AccountMutual
	AccountPayable
	AccountReceivable
	AccountRoot
	AccountStock
	AccountTrading
)

type Amount

type Amount struct {
	// Num is the number of SCUs present in this amount.
	// For USD (with an SCU of 100), Num is 1000 for a $10.00 amount.
	Num int
	// SCU is the "smallest currency unit". For USD, it is 100.
	SCU int
}

Amount describes a monetary amount in a specific currency.

func (Amount) Add

func (a Amount) Add(b Amount) (Amount, error)

Add returns a new amount equivalent to a plus b.

func (Amount) MarshalJSON

func (a Amount) MarshalJSON() ([]byte, error)

func (Amount) String

func (a Amount) String() string

type Date

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

Date contains a possibly-partial date, with unset fields set to 0.

func (Date) String

func (d Date) String() string

func (Date) ToYear

func (d Date) ToYear() Date

func (Date) ToYearMonth

func (d Date) ToYearMonth() Date

type ScanOption

type ScanOption func(*scanConfig)

ScanOption can be passed to ScanXML to configure its behavior.

func ScanAccountFilter

func ScanAccountFilter(f AccountFilter) ScanOption

ScanAccountFilter filters accounts and transaction splits as described by f. If a transaction has no remaining splits after filtering, it will be excluded.

func ScanAccountFunc

func ScanAccountFunc(f func(*Account) error) ScanOption

ScanAccountFunc adds a function that will be called for each account. If the function returns a non-nil error, scanning will be aborted.

func ScanMaxTransactionDate

func ScanMaxTransactionDate(d Date) ScanOption

ScanMaxTransactionDate excludes transactions whose date comes after d.

func ScanMinTransactionDate

func ScanMinTransactionDate(d Date) ScanOption

ScanMinTransactionDate excludes transactions whose date precedes d.

func ScanTransactionFunc

func ScanTransactionFunc(f func(*Transaction) error) ScanOption

ScanTransactionFunc adds a function that will be called for each transaction. If the function returns a non-nil error, scanning will be aborted. Note that transactions will be passed in the event that they're read from the file, which probably won't match the order in which they were posted.

type Split

type Split struct {
	// ID is a GUID identifying this split.
	ID string
	// Value is the split's value in the transaction's currency.
	Value Amount
	// Quantity is the split's value in Account's currency.
	Quantity Amount
	// Account is the account to/from which Quantity was moved.
	Account *Account
}

Split describes a source or recipient of value in a transaction.

func (Split) String

func (s Split) String() string

type Transaction

type Transaction struct {
	// ID is a GUID identifying this transaction.
	ID string
	// Date is the date on which the transaction was posted.
	Date Date
	// Desc describes the transaction.
	Desc string
	// Currency is the ID of the commodity in which this transaction took place, e.g. "USD".
	Currency string
	// Splits describes where the value came from and went to.
	Splits []Split
}

Transaction describes value being moved between two or more accounts.

func (Transaction) String

func (t Transaction) String() string

Directories

Path Synopsis
cmd
dump-transactions
Package main implements a command-line program for dumping GnuCash transactions.
Package main implements a command-line program for dumping GnuCash transactions.

Jump to

Keyboard shortcuts

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