aster.UI / Docs
Aster products
Showcase ↗

Component explorer / Application controls

Finance: invoices, accounts and a journal

Post an invoice and watch its journal entry appear and every account balance up the chart change, from one shared store.

Live preview

Open full size ↗

A local simulation with sample data: nothing is sent anywhere. Changes last for this visit.

Code for this example

These excerpts come from the code running above, formatted for reading. They focus on the component behaviour; imports, fixtures and page setup are available in Full application source below.

Edit an invoice and its lines as one record

One RecordEdit per invoice holds the header model and the owned lines; it is kept while another invoice is shown, and saves as one deep update.

From apps/showcase/src/examples/finance/finance-invoices.js

// One edit per invoice, kept while another is shown, so a half-finished edit is still there on return.
const stopSelect = effect(() => {
    const key = selection.key.value;
    if (key === undefined)
        return void (current.value = undefined);
    let record = records.get(key);
    if (!record) {
        record = new RecordEdit(ctx, "Invoices", key, {
            owned: {
                lines: {
                    positionField: "position", defaults: () => ({
                        description: "", quantity: "1.00", rate: "0.00"
                    })
                }
            }
        });
        records.set(key, record);
    }
    note.value = "";
    current.value = record;
});

Post an invoice: create the entry, then read the changed accounts

The entry's lines and its posted status are saved in one request. The service recomputes balances; the listener reads each reported account again so every view follows the store.

From apps/showcase/src/examples/finance/finance-service.js

// Balances are the service's to compute, so a write the client never made to an account still
// changes it. The service says which; each is read again into the store, and every view follows.
const reading = new Set();
const stop = mock.onChange(change => {
    if (change.set !== "Accounts" || change.kind !== "update")
        return;
    const read = ctx.reload("Accounts", keyText([Number(change.key)]), "external").finally(() => reading.delete(read));
    reading.add(read);
});
/** Resolves once the accounts a write changed have been read again. */
const settled = async () => { while (reading.size)
    await Promise.allSettled([...reading]); };

Show the service's roll-up in a tree grid

The grid is configured with a parent field and a currency format. It never sums a child: balance is a server-computed field.

From apps/showcase/src/examples/finance/finance-accounts.js

const view = {
    columns: [{
            field: "code", width: 150
        }, {
            field: "name"
        }, {
            field: "type", width: 130
        }, {
            field: "balance", width: 170
        }],
    sort: [{
            field: "code"
        }],
    filters: {},
    frozen: 0,
};
const presentation = {
    columns: {
        balance: {
            format: {
                kind: "number", style: "currency", currency: "GBP"
            }
        }
    },
    rowClass: [{
            when: {
                field: "postable", op: "eq", value: false
            }, class: "account-group"
        }],
};
const tree = {
    parentField: "parentId", autoOpen: 2
};
const toolbar = {
    advanced: false, views: false, columns: false
};

Check debits against credits across the lines

An edit model's rule sees one record, so the entry's totals are computed from the list's rows and block posting.

From apps/showcase/src/examples/finance/finance-journal.js

/** Debits, credits and their difference over the lines as they are now, unsaved edits included. For display: the rule decides nothing here. */
export function entryFigures(record) {
    const rows = record.owned.lines.rows.value.filter(row => !row.removing);
    let debit = 0n;
    let credit = 0n;
    for (const row of rows) {
        debit += amountOf(row.edit.values.value, "debit");
        credit += amountOf(row.edit.values.value, "credit");
    }
    return {
        count: rows.length, debit, credit, difference: debit - credit, balanced: debit === credit && debit > 0n
    };
}
Full application source Application code & shared presentation

Download finance.js

import "@getaster/elements";
import { effect, signal, untracked } from "@preact/signals-core";
import { html } from "lit-html";
import { mountAccounts } from "./finance-accounts.js";
import { mountInvoices } from "./finance-invoices.js";
import { mountJournal } from "./finance-journal.js";
import { connect } from "./finance-service.js";

const $ = selector => document.querySelector(selector);
const tabs = $("#screens");

try {
  const finance = await connect();
  const toaster = $("aster-toaster");
  const toast = (message, action) => toaster.show(message, action);
  const selected = signal("invoices");
  const roots = {};
  const stops = [];
  let journal;
  const mount = {
    invoices: root => stops.push(mountInvoices(root, finance, { toast, showEntry })),
    accounts: root => stops.push(mountAccounts(root, finance)),
    journal: root => stops.push((journal = mountJournal(root, finance, { toast })).dispose),
  };
  // A screen is built the first time its tab shows, and kept: its drafts stay while another screen is open.
  const screen = name => {
    if (!roots[name]) {
      roots[name] = Object.assign(document.createElement("div"), { className: "finance-screen" });
      untracked(() => mount[name](roots[name]));
    }
    return roots[name];
  };
  function showEntry(key) {
    selected.value = "journal";
    screen("journal");
    journal.show(key);
  }

  tabs.label = "Finance screens";
  tabs.items = [{ value: "invoices", label: "Invoices" }, { value: "accounts", label: "Chart of accounts" }, { value: "journal", label: "Journal" }];
  tabs.value = selected;
  tabs.renderPanel = item => html`${screen(item.value)}`;
  tabs.addEventListener("change", event => { if (event.target === tabs) selected.value = event.detail; });
  $("#loading").remove();

  // The same store the screens read, for the tests and for poking at from the console.
  globalThis.finance = Object.defineProperty(finance, "journal", { get: () => journal });
  const stop = effect(() => { document.documentElement.dataset.screen = selected.value; });
  addEventListener("pagehide", event => {
    if (event.persisted) return;
    stop();
    stops.forEach(dispose => dispose());
    finance.dispose();
  });
} catch (error) {
  const failure = $("#loading");
  failure.setAttribute("role", "alert");
  failure.textContent = `The finance workspace could not load: ${error instanceof Error ? error.message : String(error)}. Reload to try again.`;
}

Try the workflow

  1. Open INV-1042 and post it to the ledger.
  2. Open the Journal tab: the entry is there, balanced, with three lines.
  3. Open Chart of accounts: the receivable and every parent above it changed.
  4. Choose Undo in the notice, or edit JE-0008 until its debits equal its credits and post it.

What the example owns

One mock service and data context serve all three screens. Parent balances are a server-computed field; the example reads again the accounts the service reports as changed. The balanced check for a journal entry is the example's own.