Models (Core)

Account

class hordak.models.Account(*args, **kwargs)

Represents an account

An account may have a parent, and may have zero or more children. Only root accounts can have a type, all child accounts are assumed to have the same type as their parent.

An account’s balance is calculated as the sum of all of the transaction Leg’s referencing the account.

uuid

UUID for account. Use to prevent leaking of IDs (if desired).

Type:

UUID

name

Name of the account. Required.

Type:

str

parent

Parent account, nonen if root account

Type:

Account|None

balance

Account balance, only populated when account is queried using Account.objects.with_balances()

Type:

Balance

code

Account code. Must combine with account codes of parent accounts to get fully qualified account code.

Type:

str

type

Type of account as defined by AccountType. Can only be set on root accounts. Child accounts are assumed to have the same time as their parent.

Type:

str

is_bank_account

Is this a bank account. This implies we can import bank statements into it and that it only supports a single currency.

Type:

bool

TYPES

alias of AccountType

save(*args, **kwargs)

If this is a new node, sets tree fields up before it is inserted into the database, making room in the tree structure as necessary, defaulting to making the new node the last child of its parent.

It the node’s left and right edge indicators already been set, we take this as indication that the node has already been set up for insertion, so its tree fields are left untouched.

If this is an existing node and its parent has been changed, performs reparenting in the tree structure, defaulting to making the node the last child of its new parent.

In either case, if the node’s class has its order_insertion_by tree option set, the node will be inserted or moved to the appropriate position to maintain ordering by the specified field.

classmethod validate_accounting_equation()

Check that all accounts sum to 0

property sign

Returns 1 if a credit should increase the value of the account, or -1 if a credit should decrease the value of the account.

This is based on the account type as is standard accounting practice. The signs can be derrived from the following expanded form of the accounting equation:

Assets = Liabilities + Equity + (Income - Expenses)

Which can be rearranged as:

0 = Liabilities + Equity + Income - Expenses - Assets

Further details here: https://en.wikipedia.org/wiki/Debits_and_credits

get_balance(as_of=None, leg_query=None, **kwargs)

Get the balance for this account, including child accounts

Note

Note that we recommend using AccountQuerySet.with_balances() where possible as it will almost certainly be more performant when fetching balances for multiple accounts.

Parameters:
  • as_of (Date) – Only include transactions on or before this date

  • kwargs (dict) – Will be used to filter the transaction legs

Returns:

Balance

get_simple_balance(as_of=None, leg_query=None, **kwargs)

Get the balance for this account, ignoring all child accounts

Parameters:
  • as_of (Date) – Only include transactions on or before this date

  • raw (bool) – If true the returned balance should not have its sign adjusted for display purposes.

  • leg_query (models.Q) – Django Q-expression, will be used to filter the transaction legs. allows for more complex filtering than that provided by **kwargs.

  • kwargs (dict) – Will be used to filter the transaction legs

Returns:

Balance

transfer_to(to_account, amount, **transaction_kwargs)

Create a transaction which credits self and debits to_account.

See https://en.wikipedia.org/wiki/Double-entry_bookkeeping.

This is a shortcut utility method which simplifies the process of transferring where self is Cr and to_account is Dr.

For example:

  • Transferring income -> income will result in the former increasing and the latter decreasing

  • Transferring income -> asset (i.e. bank) will result in the balance of both increasing

  • Transferring asset -> asset will result in the former decreasing and the latter increasing

Note

      LHS                          RHS
{asset | expense} <-> {income | liability | equity}

Transfers LHS (A) -> RHS (B) will decrease A and increase B
Transfers LHS (A) -> LHS (B) will decrease A and increase B
Transfers RHS (A) -> LHS (B) will increase A and increase B
Transfers RHS (A) -> RHS (B) will increase A and decrease B
Parameters:
  • to_account (Account) – The destination account.

  • amount (Money) – The amount to be transferred.

  • transaction_kwargs – Passed through to transaction creation. Useful for setting the transaction description or date fields.

exception DoesNotExist
exception MultipleObjectsReturned
class hordak.models.AccountQuerySet(model=None, query=None, using=None, hints=None)

Utilities available to querysets of Accounts

net_balance()

Get the total balance of all accounts in this queryset

with_balances(to_field_name='balance', as_of: date | None = None, as_of_leg_id: int | None = None)

Annotate the account queryset with account balances

This is a much more performant way to calculate account balances, especially when calculating balances for a lot of accounts.

You can get the balance at a particular point in time by specifying as_of and (optionally) as_of_leg_id.

Note that you will get better performance by setting the as_of to None (the default). This is because the underlying custom database function can avoid a join.

Example

>>> # Will execute in a single database query
>>> for account in Account.objects.with_balances():
>>>     print(account.balance)

Transaction

class hordak.models.Transaction(*args, **kwargs)

Represents a transaction

A transaction is a movement of funds between two accounts. Each transaction will have two or more legs, each leg specifies an account and an amount.

See also

Account.transfer_to() is a useful shortcut to avoid having to create transactions manually.

Examples

You can manually create a transaction as follows:

from django.db import transaction as db_transaction
from hordak.models import Transaction, Leg

with db_transaction.atomic():
    transaction = Transaction.objects.create()
    Leg.objects.create(transaction=transaction, account=my_account1, amount=Money(100, 'EUR'))
    Leg.objects.create(transaction=transaction, account=my_account2, amount=Money(-100, 'EUR'))
uuid

UUID for transaction. Use to prevent leaking of IDs (if desired).

Type:

models.UUIDField

timestamp

The datetime when the object was created.

Type:

datetime

date

The date when the transaction actually occurred, as this may be different to timestamp.

Type:

date

description

Optional user-provided description

Type:

str

exception DoesNotExist
exception MultipleObjectsReturned

Leg

class hordak.models.Leg(*args, amount: Money | None = None, **kwargs)

The leg of a transaction

Represents a single amount either into or out of a transaction. All legs for a transaction must sum to zero, all legs must be of the same currency.

uuid

UUID for transaction leg. Use to prevent leaking of IDs (if desired).

Type:

UUID

transaction

Transaction to which the Leg belongs.

Type:

Transaction

account

Account the leg is transferring to/from.

Type:

Account

amount

The amount being transferred

Type:

Money

description

Optional user-provided description

Type:

str

type

hordak.models.DEBIT or hordak.models.CREDIT.

Type:

str

account_balance_after

The account balance before this transaction. Only populated when account is queried using Leg.objects.with_account_balance_after()

Type:

Balance

account_balance_before

The account balance after this transaction. Only populated when account is queried using Leg.objects.with_account_balance_before()

Type:

Balance

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

exception DoesNotExist
exception MultipleObjectsReturned
class hordak.models.LegQuerySet(model=None, query=None, using=None, hints=None)

Utilities available to querysets of Legs

sum_to_debit_and_credit() Tuple[Balance, Balance]

Sum the Legs of the QuerySet to get balance objects for both credits and debits

Example

>>> total_debits, total_credits = Leg.objects.sum_to_debit_and_credit()
sum_to_balance(account_type=None)

Sum the Legs of the QuerySet to get a single Balance object

Specifying account_type for the account will ensure the resulting balance is signed (ie +/-) correctly. Otherwise this method will perform an additional database query to determine the account type as best it can (and will issue a warning if it fails).

Example

>>> balance = Leg.objects.sum_to_balance()
with_account_balance_after()

Get the balance of the account associated with each leg following the transaction

Annotate the queryset with the account_balance_after property. This is the account balance following after the leg happened. Useful for rendering account statements.

Example

>>> legs = my_account.legs.with_account_balance_after()
>>> for leg in legs:
>>>     print(f"{leg.transaction.date} {leg.type_short} {leg.amount} {leg.balance_after}")
2000-01-01 CR €100.00 €100.00
2000-01-01 CR €10.00 €110.00
with_account_balance_before()

Get the balance of the account associated with each leg prior to the transaction

Annotate the queryset with the account_balance_before property. This is the account balance before after the leg happened.

Example

>>> legs = my_account.legs.with_account_balance_before()
>>> for leg in legs:
>>>     print(f"{leg.transaction.date} {leg.type_short} {leg.amount} {leg.balance_before}")
2000-01-01 CR €100.00 €0.00
2000-01-01 CR €10.00 €100.00
debits()

Filter for legs that are debits

credits()

Filter for legs that are credits

LegView (Database View)

class hordak.models.LegView(*args, **kwargs)

An accounting view onto the Legs table

Warning

Hordak’s database views are still experimental and may change or be removed in a future version.

This provides a number of features on top of the raw Legs table:

  1. Shows the leg type (debit/credit)

  2. All amounts are reported as positive

  3. Amounts are available in the credit and debit columns (and are NULL if not applicable)

  4. A running total account_balance field is provided

  5. Shows some transaction fields (date, description)

Note that this is a database view and is therefore read-only.

You can also improve query performance (in Postgresql) by deferring the account_balance field, assuming the value not required. For example:

HordakLegView.objects.defer('account_balance')
id

The leg ID

Type:

int

uuid

The leg UUID

Type:

UUID

transaction

The transaction which contains this leg

Type:

Transaction

account

The account this leg is associated with

Type:

Account

date

The date when the parent transaction actually occurred

Type:

date

amount

The balance of this leg (use amount.currency to get the currency for the other Decimal fields on this view.

Type:

Balance

type

Either LegType.debit or LegType.credit.

Type:

LegType

credit

Amount of this credit, or NULL if not a credit

Type:

Decimal

debit

Amount of this debit, or NULL if not a debit

Type:

Decimal

account_name

The name of account for the leg

Type:

str

account_full_code

The full account code of account for the leg

Type:

str

account_type

The type of account for the leg

Type:

AccountType

account_balance

Account balance following this transaction. For multiple-currency accounts this will be the balance of the same currency as the leg amount.

Type:

Decimal

leg_description

Description of the leg

Type:

str

transaction_description

Description of the transaction

Type:

str

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

exception DoesNotExist
exception MultipleObjectsReturned

TransactionView (Database View)

class hordak.models.TransactionView(*args, **kwargs)

An accounting view onto the Transaction table

Warning

Hordak’s database views are still experimental and may change or be removed in a future version.

This provides a number of features on top of the raw Transaction table:

  1. Shows the transaction amount (JSON list, one value per currency)

  2. Shows credited/debited account IDs & names

Note that this is a database view and is therefore read-only.

You can also improve query performance (in Postgresql) by deferring unneeded fields. For example:

HordakLegView.objects.defer(
    'credit_account_ids',
    'debit_account_ids',
    'credit_account_names',
    'debit_account_names',
)
save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

exception DoesNotExist
exception MultipleObjectsReturned