Thursday, June 20, 2013

Domain-Driven Design: Accounting Domain Model

In my previous post, I talked about two things that helped me improve my implementation of domain-driven design. In this post, I'll try to use the ubiquitous language and develop an accounting model.

Disclaimer: Note that I'm no accountant by any means.

This is based on "Accounting for Non-accountants" by Daniel R. Cruz (as listed here) — a text book from De la Salle University Press that I was fortunate enough to read. Thanks to Garen and her MBA studies.

There are eight steps in the accounting cycle. For purposes of this post, I'll limit only to journalizing and posting.

Record transactions in journal.
Post transactions to ledger accounts.

An accounting plug-in/sub-module would enable users to enter transactions and generate journal entries. These entries can be exported (posted) to the real or main accounting system which would tally the account balances of the enterprise using transactions from different systems/sources.

So, what's a journal entry? Here's what Wikipedia gives us:

A journal entry, in accounting, is a logging of transactions into accounting journal items. The journal entry can consist of several items, each of which is either a debit or a credit. The total of the debits must equal the total of the credits or the journal entry is said to be "unbalanced".

Journal Entries

Let's start modeling this. The JournalEntry is an entity. An aggregate can be formed around a journal entry and its items. The JournalEntry is the aggregate root. Note how I'm trying to use the ubiquitous language in naming our entities.

package accounting.domain.model;

public class JournalEntry {
  private Date date;
  private String description;
  private List<JournalEntryItem> items = new ArrayList<JournalEntryItem>();

  public JournalEntry() {

  public List<JournalEntryItem> getItems() {
    return this.items;

  public void setItems(List<JournalEntryItem> items) {
    // TO DO: Ensure items are "balanced"
    this.items = items;
package accounting.domain.model;

public class JournalEntryItem {
  public JournalEntryItem(…) {…}

At this point, we can see that keeping the items "balanced" has not yet been implemented. We can add a validate method to the JournalEntry entity, and have it called before it gets persisted. But what about keeping the entity valid all the time by doing something like this?

public class JournalEntry {
  private Date date;
  private String description;
  private List<JournalEntryItem> items;

  public JournalEntry(…, JournalEntryItem items[]) {
    if (!JournalEntry.isBalanced(items)) {
      throw new IllegalArgumentException(
          "The total of debits must equal the total of credits");
    this.items = Arrays.asList(items);

  public static boolean isBalanced(JournalEntryItem items[]) {
    … total = ….ZERO;
    for (JournalEntryItem item : items) {
      if (item.type() == Type.DEBIT) {
        total = total.subtract(item.amount());
      } else { // item.type() == Type.CREDIT
        total = total.add(item.amount());
    return total.equals(….ZERO);

  // getters and setters removed

  public List<JournalEntryItem> items() {
    return Collections.unmodifiableList(this.items);

  public String description() { return this.description; }
  public Date date() { return; }

Here's how the expanded journal entry item can be implemented.

public class JournalEntryItem {
  public static enum Type {

  private String accountId;
  private Type type;
  private … amount;

  public JournalEntryItem(String accountId, Type type, … amount) {
    // TO DO: Throw IllegalArgumentException if any argument is null

  public String accountId() { return this.accountId; }
  public Type type() { return this.type; }
  public boolean isCredit() { return this.type == Type.CREDIT; }
  public boolean isDebit() { return this.type == Type.DEBIT; }

The amount in the entry item was simplified for brevity. In a real implementation, it would be a value object representing money (an amount of a certain currency).

Based on the domain language, journal entries are created via business transactions, like buying merchandise inventory using cash, business owner depositing money to corporate bank account as capital, create invoice, receiving payment for invoice, paying bills, etc.

Now, let's give it a test run. Let's say the business purchased merchandise inventory worth $4,000, paying $3,000 in cash, and the balance on credit. This shall be represented as:

new JournalEntry(…, new JournalEntryItem[] {
    new JournalEntryItem("Inventory", ….DEBIT, 4000),
    new JournalEntryItem(    "Cash",     ….CREDIT, 3000),
    new JournalEntryItem(    "A/P",      ….CREDIT, 1000) // accounts payable

The Accounting Equation

To understand why certain business transactions result into journal entries with debit/credit against certain accounts, we need to understand the accounting equation.

Assets = Equity

Assets can be in any form of property owned or controlled by the business, whather tangible or intangible. Equity refers to the owners or claimants to the assets or properties of the business. Therefore, if the business has $100,000 worth of assets, there must necessarily be $100,000 worth of claimants.

- "Accounting for Non-accountants" by Daniel R. Cruz

The basic accounting equation is:

Assets = Liabilities + Capital

In actual businesses, there are normally two types of claimants to the assets of the business. The claim(s) of the owner(s) who invested in the business is called owners' equity or capital. The claim(s) of the creditor(s) who extended financial assistance to the business, whether in cash or in kind, is called creditors' equity or liability(ies). Total owners' equity (capital) plus total creditors' equity (liabilities) equals total equity.

If the business has $100,000 worth of assets and total capital investment of the owner is $60,000, then total liabilities (or claimants) must necessarily be $40,000. The left and right sides of the equation must always balance for every business transaction as well as for the summary of all transactions.

- "Accounting for Non-accountants" by Daniel R. Cruz

There are five accounting elements. Accounts are classified under one of the five elements. Depending on the account's classification, an increase or decrease may mean a debit or credit.


Let's go back to the previous example, where the business buys merchandise inventory worth $4,000, paying $3,000 in cash, and the rest on credit.

  • The "inventory" account is an asset account. Buying merchandise inventory increases it. Thus, there's a debit entry item against it.
  • The "cash" account is an asset account. Paying in cash decreases it. Thus, there's a credit entry item against it.
  • The "A/P" (accounts payable) account is a liability account. Paying on credit increases the accounts payable. Thus, there's a credit entry item against it.

Let's have another business transaction example. Let's say the business sold merchandise inventory (Cost of Goods Sold or CGS) worth $500 for $900, receiving $600 in cash, and the balance on credit (Accounts Receivable or A/R). How would this look using our domain model?

// for the sale of goods transaction
new JournalEntry(…, new JournalEntryItem[] {
    new JournalEntryItem("CGS",  ….DEBIT, 500), // cost of goods sold
    new JournalEntryItem(  "Inventory", ….CREDIT, 500)
// for the sale on account and cash sales transaction
new JournalEntry(…, new JournalEntryItem[] {
    new JournalEntryItem("Cash", ….DEBIT, 600),
    new JournalEntryItem("A/R",  ….DEBIT, 300), // accounts receivable
    new JournalEntryItem(  "Sales",     ….CREDIT, 900)
  • When we move an item from inventory to sell as a product, we move the product from an asset (inventory) to an expense (cost of goods sold). Thus, we increase from "cost of goods sold", and decrease "inventory".
  • The "cash" and "accounts receivable" accounts are asset accounts. Receiving cash and credit increases it. Thus, there are debit entry items against it.
  • The "sales" account is an income/revenue account. Making a sale increases it. Thus, there is a credit entry item against it.

More examples can be found here (at Wikipedia).

Special Journals

Special Journals are designed to facilitate the process of journalizing and posting transactions. They are used for the most frequent transactions in a business. For example, in merchandising businesses, companies acquire merchandise from vendors, and then in turn sell the merchandise to individuals or other businesses. Sales and purchases are the most common transactions for the merchandising businesses. A business such as a retail store will record the following transactions many times a day for sales on account and cash sales.

Automated systems would usually create interfaces that have a pre-determined set of journal entry items for the most frequent transactions.

Let's take the sales journal for example. An entry is made when creating invoices (or to record transactions that involve sales on credit). The journal entry items are as follows:

  • Debit on "A/R" account (increasing an asset-type account)
  • Credit on "Sales" account (increasing an income-type account)

The interface for the sales journal would look more like creating an invoice, rather than creating journal entry items. The description of the journal entry shall contain a reference to the number (or unique ID) of the created invoice.

Ledger Accounts

The astute reader would have seen that each entry item references an (ledger) account. From my previous post on DDD, we're referencing other aggregates by ID (and not by type).

The list of ledger accounts is called the "chart of accounts". Typically, an organization (or enterprise, or business) would setup a chart of accounts, and each account is classified under a certain type of account. The type shall determine how increases and decreases affect the balance of the account. The account is uniquely identified using an alpha-numeric string that follows some kind of convention.

So, how can we model the account?

public class LedgerAccount {
  private String id;
  private String name;
  private String description;
  private String type;
  private List<LegderAccount> childAccounts;
  // private … balance; // can be calculated

It's a pretty straight-forward entity. Generally, accounts are arranged in a hierarchy.

In automated systems, the journal entry items already reference the account. So, to get the balance of an account, it's just a matter of doing a simple query that joins the ledger account and its related journal entry items. The posting step is now optional.


This post has gotten quite lengthy. And we've only touched the journal entries. Hopefully, it's enough to get some accounting domain model started. If you've got questions/suggestions, please hit the comments.