A Seneca.js plugin
![]() |
This open source module is sponsored and supported by Voxgig. |
|---|
Ledger business logic plugin for the Seneca platform.
$ npm install seneca@^4 @seneca/entity @seneca/ledgerRequires Seneca 4 and Node.js 24 or later. Seneca 3 users stay on
@seneca/ledger@0.7.
// Setup
const seneca = Seneca().use('@seneca/entity').use('@seneca/ledger')
await seneca.ready()
// Create an account
await seneca.post('biz:ledger,create:account', {
account: { oref: 'o0', path: 'Asset', name: 'Cash', normal: 'debit' },
})
// Get an account
await seneca.post('biz:ledger,get:account', { aref: 'o0/Asset/Cash' })
// List accounts
await seneca.post('biz:ledger,list:account', { org_id: 'o0' })
// Update an account
await seneca.post('biz:ledger,update:account', {
aref: 'o0/Asset/Cash',
account: { custom_field: 'value' },
})
// Create a book (accounting period)
await seneca.post('biz:ledger,create:book', {
book: { oref: 'o0', name: 'Q1', start: 20220101, end: 20220331 },
})
// Get a book
await seneca.post('biz:ledger,get:book', { bref: 'o0/Q1/20220101' })
// List books
await seneca.post('biz:ledger,list:book', { oref: 'o0' })
// Update a book
await seneca.post('biz:ledger,update:book', {
bref: 'o0/Q1/20220101',
book: { end: 20220331 },
})
// Create a journal entry (double-entry: debit + credit)
await seneca.post('biz:ledger,create:entry', {
bref: 'o0/Q1/20220101',
daref: 'o0/Asset/Cash',
caref: 'o0/Income/Sales',
val: 100,
desc: 'Jan Sales',
date: 20220131,
})
// List entries
await seneca.post('biz:ledger,list:entry', {
oref: 'o0',
bref: 'o0/Q1/20220101',
})
// Balance an account
await seneca.post('biz:ledger,balance:account', {
aref: 'o0/Asset/Cash',
bref: 'o0/Q1/20220101',
})
// Close an account (zero balance and optionally carry forward to target book)
await seneca.post('biz:ledger,close:account', {
aref: 'o0/Asset/Cash',
bref: 'o0/Q1/20220101',
target_bref: 'o0/Q2/20220401',
})
// Close a book (close all accounts and mark book as closed)
await seneca.post('biz:ledger,close:book', {
bref: 'o0/Q1/20220101',
target_bref: 'o0/Q2/20220401',
})
// Export account to CSV
await seneca.post('biz:ledger,export:account,format:csv', {
aref: 'o0/Asset/Cash',
bref: 'o0/Q1/20220101',
})
// Export book to CSV (all accounts summary)
await seneca.post('biz:ledger,export:book,format:csv', {
bref: 'o0/Q1/20220101',
})See test/ for more usage examples.
A Seneca.js plugin.
If you're using this module and need help, you can:
- Post a github issue
- Tweet to @senecajs
- Ask on the Gitter
None.
- balance:account,biz:ledger
- balance:book,biz:ledger
- biz:ledger,close:account
- biz:ledger,close:book
- biz:ledger,create:account
- biz:ledger,create:book
- biz:ledger,create:entry
- biz:ledger,export:account,format:csv
- biz:ledger,export:book,format:csv
- biz:ledger,get:account
- biz:ledger,get:book
- biz:ledger,list:account
- biz:ledger,list:book
- biz:ledger,list:balance
- biz:ledger,list:entry
- biz:ledger,update:account
- biz:ledger,update:book
- biz:ledger,void:entry
Calculate the current balance of a ledger account for a given accounting period (book). Sums all debit and credit entries in the book and returns the net balance according to the account's normal direction.
{
ok: '_true_ on success',
creditTotal: 'sum of credit entry values',
debitTotal: 'sum of debit entry values',
balance: "net balance according to the account's normal direction",
account_id: 'account identifier',
aref: 'account reference',
book_id: 'book identifier',
bref: 'book reference',
start: 'book start date',
end: 'book end date',
creditCount: 'number of credit entries',
debitCount: 'number of debit entries',
normal: 'account normal direction (`debit` or `credit`)',
when: 'calculation timestamp (unix milliseconds)',
date: 'calculation date (YYYYMMDD)',
time: 'calculation time (HHMMSS string, UTC)',
why: 'reason for failure when ok is _false_'
}
Not yet implemented.
Close a ledger account within a book. Creates a closing entry against the opening balance account (default oref/Equity/Opening Balance, created if missing) and, when target_bref or target_book_id is supplied, an opening entry in the target book so the balance carries forward to the next accounting period.
{
ok: '_true_ on success',
account_id: 'account identifier',
aref: 'account reference',
book_id: 'book identifier',
bref: 'book reference',
target_book_id: 'target book identifier, if supplied',
target_bref: 'target book reference, if supplied',
original_balance: 'account balance before closing',
closing_balance: 'account balance in the book after closing',
opening_balance: 'account balance in the target book, or _null_',
opening_balance_aref: 'reference of the opening balance account, or _null_',
closing_entries: 'array of closing entry results',
opening_entries: 'array of opening entry results',
closing_date: 'date of the closing entry',
why: 'reason for failure when ok is _false_',
error: 'the failed sub-operation result, when applicable'
}
Close an accounting period (book) by closing all its accounts in batches of batch_size (default 5) and marking the book as closed. When target_bref or target_book_id is supplied, closing balances are carried forward as opening entries in the target book.
{
ok: '_true_ on success',
book_id: 'book identifier',
bref: 'book reference',
target_book_id: 'target book identifier, if supplied',
target_bref: 'target book reference, if supplied',
closing_date: 'book end date used for closing',
account_closures: 'array of per-account closure results',
summary: 'totals: `total_accounts`, `successful_closures`, `failed_closures`, `total_balance_transferred`, `all_accounts_zeroed`',
op_balance_check: 'opening balance account check in the target book, or _null_',
closure_successful: '_true_ if all accounts closed with zero balance',
note: 'explanation when no accounts were closed or closure failed',
why: 'reason for failure when ok is _false_',
error: 'the failed opening balance account creation result, when applicable'
}
Create a new ledger account within an organisation. An account is identified by its oref (or org_id), path (e.g. Asset or Asset/Current), and name. The normal field must be either "debit" or "credit".
{
ok: '_true_ if account was created',
account: 'the saved account entity',
why: 'reason for failure when ok is _false_'
}
Create a new accounting period (book) for an organisation. A book is identified by its oref (or org_id), name, and start date. Omit end (or set it to 0) for an open-ended period.
{
ok: '_true_ if book was created',
book: 'the saved book entity',
why: 'reason for failure when ok is _false_'
}
Create a double-entry journal entry that debits one account and credits another within a given book. Both the debit account (daref or debit) and credit account (caref or credit) must exist, and val, desc, and date are required. The entry date must fall within the book's period, and the book must not be closed.
{
ok: '_true_ on success',
credit: 'the saved credit entry entity',
debit: 'the saved debit entry entity',
why: 'reason for failure when ok is _false_'
}
Export all entries for a single ledger account within a book as a CSV string. Each row represents one journal entry with columns for date, description, debit, credit, and running balance.
{
ok: '_true_ on success',
account_id: 'account identifier',
aref: 'account reference',
normal: 'account normal direction (`debit` or `credit`)',
book_id: 'book identifier',
bref: 'book reference',
content: 'CSV formatted string of entries',
entry_count: 'number of entries exported',
final_balance: 'account balance for the book',
closing_balance: 'value of the closing entry, or 0 if none',
why: 'reason for failure when ok is _false_',
error: 'the failed balance or entry listing result, when applicable'
}
Export every account with entries in a book (excluding the opening balance account) as CSV, processed in batches of batch_size (default 5), plus a summary CSV with columns for account name, normal balance, type, total (or closing) balance, and entry count. Accounts (in both exports and the summary rows) are ordered credit-normal before debit-normal, then by name, then by aref.
{
ok: '_true_ if all account exports succeeded',
book_id: 'book identifier',
bref: 'book reference',
book_name: 'book name',
total_accounts: 'number of accounts exported',
successful_exports: 'number of successful account exports',
failed_exports: 'number of failed account exports',
exports: 'array of per-account export results',
summary: 'summary CSV result (`ok`, `content`, or `why` and `error` on failure)',
note: 'explanation when no accounts were found',
why: 'reason for failure when the book is not found'
}
Retrieve a single ledger account by its aref (account reference, in the form oref/path/name), account_id, or id.
{
ok: '_true_ if account was found',
account: 'the account entity',
why: 'reason for failure when ok is _false_'
}
Retrieve a single accounting period (book) by its bref (book reference, in the form oref/name/start), book_id, or id.
{
ok: '_true_ if book was found',
book: 'the book entity',
why: 'reason for failure when ok is _false_'
}
List ledger accounts that belong to an organisation (org_id or oref). If neither is given, all accounts are listed.
{
ok: '_true_ on success',
q: 'the query used to list accounts',
list: 'array of account entities'
}
List accounting periods (books) that belong to an organisation (org_id or oref). If neither is given, all books are listed.
{
ok: '_true_ on success',
q: 'the query used to list books',
list: 'array of book entities'
}
Not yet implemented.
List journal entries for an organisation (oref, required), optionally filtered by book (bref or book_id), account (aref, account_id, or id), and an extra query q. Set credit: false or debit: false to skip that side.
{
ok: '_true_ on success',
creditTotal: 'sum of credit entry values',
debitTotal: 'sum of debit entry values',
balance: 'net balance when an account is given, otherwise 0',
credits: 'array of credit entry entities',
debits: 'array of debit entry entities',
cq: 'the query used to list credit entries',
dq: 'the query used to list debit entries',
why: 'reason for failure when ok is _false_'
}
Update fields on an existing ledger account. Supply the aref (or account_id / id) to identify the account and an account object containing the fields to change.
{
ok: '_true_ if account was updated',
account: 'the updated account entity',
why: 'reason for failure when ok is _false_'
}
Update fields on an existing accounting period (book). Supply the bref (or book_id / id) to identify the book and a book object containing the fields to change.
{
ok: '_true_ if book was updated',
book: 'the updated book entity',
why: 'reason for failure when ok is _false_'
}
Not yet implemented.
The Senecajs org encourages open participation. If you feel you can help in any way, be it with documentation, examples, extra testing, or new features please get in touch.
npm run testPart of the Senecajs org.

