Models — Django Hordak 1.0 documentation

# Models

Contents

- Models
  - Design Overview
  - Account
  - Transaction
  - Leg
  - StatementImport
  - StatementLine

## Design Overview

The core models consist of:

- `Account` - Such as ‘Accounts Receivable’, a bank account, etc. Accounts can be arranged as a tree structure, where the balance of the parent account is the summation of the balances of all its children.
- `Transaction` - Represents a movement between accounts. Each transaction must have two or more legs.
- `Leg` - Represents a flow of money into (debit) or out of (credit) a transaction. Debits are represented by negative amounts, and credits by positive amounts. The sum of all a transaction’s legs must equal zero. This is enforced with a database constraint.

Additionally, there are models which related to the import of external bank statement data:

- `StatementImport` - Represents a simple import of zero or more statement lines relating to a specific `Account`.
- `StatementLine` - Represents a statement line. `StatementLine.create_transaction()` may be called to create a transaction for the statement line.

## 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`

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

  `name`

    str – Name of the account. Required.

  `parent`

    Account|None – Parent account, nonen if root account

  `code`

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

  `type`

    str – 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.

  `TYPES`

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

  `is_bank_account`

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

  classmethod `validate_accounting_equation`()

    Check that all accounts sum to 0

  `full_code`

    Get the full code for this account

    Do this by concatenating this account’s code with that of all the parent accounts.

  `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, **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, **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. **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 mannor. 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

    Transferrs 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.

    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.

## 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`

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

  `timestamp`

    datetime – The datetime when the object was created.

  `date`

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

  `description`

    str – Optional user-provided description

## 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`

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

  `transaction`

    Transaction – Transaction to which the Leg belongs.

  `account`

    Account – Account the leg is transferring to/from.

  `amount`

    Money – The amount being transferred

  `description`

    str – Optional user-provided description

  `type`

    str – `hordak.models.DEBIT` or `hordak.models.CREDIT`.

  `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

## StatementImport

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

  Records an import of a bank statement

  `uuid`

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

  `timestamp`

    datetime – The datetime when the object was created.

  `bank_account`

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

## 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`

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

  `timestamp`

    datetime – The datetime when the object was created.

  `date`

    date – The date given by the statement line

  `statement_import`

    StatementImport – The import to which the line belongs

  `amount`

    Decimal – The amount for the statement line, positive or nagative.

  `description`

    str – Any description/memo information provided

  `transaction`

    Transaction – Optionally, the transaction created for this statement line. This normally occurs during reconciliation. See also `StatementLine.create_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