Skip to content

feat(finance): ledger API + auto-posting (#97 increment 2)#97

Merged
ankitsejwal merged 1 commit into
mainfrom
feat/m3.5-ledger-api
Jul 16, 2026
Merged

feat(finance): ledger API + auto-posting (#97 increment 2)#97
ankitsejwal merged 1 commit into
mainfrom
feat/m3.5-ledger-api

Conversation

@ankitsejwal

Copy link
Copy Markdown
Member

M3.5 · Task #97 (increment 2 of 2) — completes the GL

Wires the pure posting domain from #96 into the running app. Builds on the merged foundation.

API (apps/api/src/routes/ledger.ts, ADMIN-only)

  • POST /ledger/init — idempotently seed the default chart of accounts (lib/coa.ts). Run once to switch accounting on.
  • GET /ledger/accounts — chart of accounts.
  • POST /ledger/journal-entries — manual/adjusting entries; the balanced-entry invariant is enforced at the boundary (unbalanced → 400).
  • GET /ledger/journal-entries — filter by ?period / ?sourceType.
  • GET /ledger/accounts/:id/ledger — account drill-down with a running balance in the account's normal direction.

Auto-posting (apps/api/src/lib/ledger-posting.ts)

  • Resolves accounts by stable code, inserts balanced entries idempotently (partial unique index catches re-posts).
  • §6.1 bill-issued and §6.2 payment-received posting, wired into bill create + generate, the monthly cron, and payment record.
  • Best-effort: failures are logged and swallowed so a ledger hiccup never fails the finance write; posting is a no-op until POST /ledger/init runs.

Schema

  • account_id on bill_line_items + bill_config template lines → charges post to the right income/fund head (default Maintenance Income). Migration 0020 (additive column + FK).

Tests

splitReceivableAdvance unit tests + /ledger route tests (auth + unbalanced-entry rejection). Full suite 311 green; API lint clean; web typechecks.

Deploy note

Migrations 0019 + 0020 are committed but not applied to any DB here — apply on deploy, then POST /ledger/init. Historical backfill / opening balances and the trial-balance surface land with #98 (statements).

Increment 2 of the general ledger — wires the domain from #96 into the app.

- /ledger admin API (routes/ledger.ts): POST /init seeds the default chart of
  accounts (idempotent, lib/coa.ts); GET /accounts; POST /journal-entries for
  manual/adjusting entries with the balanced-entry guard enforced at the
  boundary; GET /journal-entries; GET /accounts/:id/ledger drill-down with a
  running balance in the account's normal direction.
- Posting service (lib/ledger-posting.ts): resolves accounts by code, inserts
  balanced entries idempotently (unique index), and auto-posts §6.1 bill-issued
  and §6.2 payment-received. Best-effort + swallowed so a ledger issue never
  blocks the finance write; no-op until the CoA is initialized.
- Wired into bills (create + generate), the monthly cron, and payments.
- account_id on bill_line_items + bill_config template lines so charges post to
  the right income/fund head (default Maintenance Income). Migration 0020.
- Tests: splitReceivableAdvance unit tests + /ledger route tests (auth +
  unbalanced-entry rejection). Full suite 311 green.

Migrations 0019/0020 are committed for the deploy step; not applied to any DB
here. Backfill/opening-balances and the trial balance surface come with #98.
@ankitsejwal ankitsejwal merged commit ccb0ea6 into main Jul 16, 2026
1 check passed
@ankitsejwal ankitsejwal deleted the feat/m3.5-ledger-api branch July 16, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant