Models

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:SmallUUID
name

Name of the account. Required.

Type:str
parent

Parent account, nonen if root account

Type:Account|None
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 Account.TYPES. Can only be set on root accounts. Child accounts are assumed to have the same time as their parent.

Type:str
TYPES

Available account types. Uses Choices from django-model-utils. Types can be accessed in the form Account.TYPES.asset, Account.TYPES.expense, etc.

Type:Choices
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
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 neccessary, 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

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

balance(as_of=None, raw=False, leg_query=None, **kwargs)

Get the balance for this account, including 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.
  • kwargs (dict) – Will be used to filter the transaction legs
Returns:

Balance

See also

simple_balance()

simple_balance(as_of=None, raw=False, 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 transfers amount to to_account

This is a shortcut utility method which simplifies the process of transferring between accounts.

This method attempts to perform the transaction in an intuitive manner. For example:

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

Note

Transfers in any direction between {asset | expense} <-> {income | liability | equity} will always result in both balances increasing. This may change in future if it is found to be unhelpful.

Transfers to trading accounts will always behave as normal.

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 field.
exception DoesNotExist
exception MultipleObjectsReturned

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:SmallUUID
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, **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:SmallUUID
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
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.

account_balance_after()

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

account_balance_before()

Get the balance of the account associated with this leg before the transaction

exception DoesNotExist
exception MultipleObjectsReturned

StatementImport

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

Records an import of a bank statement

uuid

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

Type:SmallUUID
timestamp

The datetime when the object was created.

Type:datetime
bank_account

The account the import is for (should normally point to an asset account which represents your bank account)

Type:Account
exception DoesNotExist
exception MultipleObjectsReturned

StatementLine

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

Records an single imported bank statement line

A StatementLine is purely a utility to aid in the creation of transactions (in the process known as reconciliation). StatementLines have no impact on account balances.

However, the StatementLine.create_transaction() method can be used to create a transaction based on the information in the StatementLine.

uuid

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

Type:SmallUUID
timestamp

The datetime when the object was created.

Type:datetime
date

The date given by the statement line

Type:date
statement_import

The import to which the line belongs

Type:StatementImport
amount

The amount for the statement line, positive or nagative.

Type:Decimal
description

Any description/memo information provided

Type:str
transaction

Optionally, the transaction created for this statement line. This normally occurs during reconciliation. See also StatementLine.create_transaction().

Type:Transaction
is_reconciled

Has this statement line been reconciled?

Determined as True if transaction has been set.

Returns:True if reconciled, False if not.
Return type:bool
create_transaction(to_account)

Create a transaction for this statement amount and account, into to_account

This will also set this StatementLine’s transaction attribute to the newly created transaction.

Parameters:to_account (Account) – The account the transaction is into / out of.
Returns:The newly created (and committed) transaction.
Return type:Transaction
exception DoesNotExist
exception MultipleObjectsReturned