Skip to content
AccountancyPath

Accounting for DevelopersModule 4: Production financial systemsAdvanced2 min read

Accounting Period Close and Locking in Software

Build period close and locking into your ledger: open, soft-closed and locked states, database triggers, late adjustments and automated year-end closing entries.

Lesson 13 of 16
Course outline (16 lessons)

Before this lesson:Journal and General Ledger Database DesignThe Accounting Cycle

Imagine your finance team sends March’s income statement to investors on 5 April—and on 9 April a background webhook retries a late March invoice, silently changing March revenue by $4,000. That destroys trust in the books. Period locking prevents past financial reports from ever drifting after close.

The three period states

Rather than a single is_closed boolean, model an accounting_periods table with three progressive states so finance has a quiet window to post adjusting entries before final sign-off:

State Automated traffic (billing, webhooks) Finance adjustments (periods:adjust) Typical window
open Allowed Allowed During the active month
soft_closed Blocked Allowed (accruals, depreciation, FX reval) Days 1–5 of the following month
locked Blocked Blocked (requires audited reopen) After close review and reporting
CREATE TYPE period_status AS ENUM ('open', 'soft_closed', 'locked');

CREATE TABLE accounting_periods (
  id          uuid PRIMARY KEY,
  entity_id   uuid NOT NULL REFERENCES entities(id),
  name        text NOT NULL,          -- e.g. '2026-03'
  start_date  date NOT NULL,
  end_date    date NOT NULL,
  status      period_status NOT NULL DEFAULT 'open',
  closed_by   text,
  closed_at   timestamptz,
  CHECK (start_date <= end_date),
  UNIQUE (entity_id, start_date)
);

Enforcing period locks in the database

Application middleware is easily bypassed by backfill scripts or worker queues. Put a BEFORE INSERT trigger directly on journal_entries so PostgreSQL guarantees no backdated entry ever lands in a locked period:

CREATE OR REPLACE FUNCTION enforce_open_period() RETURNS trigger AS $$
DECLARE
  p_status period_status;
BEGIN
  SELECT status INTO p_status
  FROM accounting_periods
  WHERE entity_id = NEW.entity_id
    AND NEW.effective_date BETWEEN start_date AND end_date;

  IF p_status IS NULL THEN
    RAISE EXCEPTION 'NO_ACCOUNTING_PERIOD for effective_date %', NEW.effective_date;
  ELSIF p_status = 'locked' THEN
    RAISE EXCEPTION 'CLOSED_PERIOD: % falls in a locked period', NEW.effective_date;
  ELSIF p_status = 'soft_closed' AND NEW.source_type NOT IN ('adjustment', 'closing', 'revaluation') THEN
    RAISE EXCEPTION 'SOFT_CLOSED_PERIOD: only finance adjustments allowed for %', NEW.effective_date;
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER journal_entries_period_guard
  BEFORE INSERT ON journal_entries
  FOR EACH ROW EXECUTE FUNCTION enforce_open_period();

If an executive controller genuinely must reopen a locked period for an audit adjustment, expose a dedicated admin action that logs (period_id, actor, reason, reopened_at) in an append-only period_audit_events table and invalidates any cached period balance snapshots from that period onward.

Handling late transactions in the next open period

When a webhook or vendor invoice arrives with a timestamp from a locked month, do not silently drop it and do not reopen the month for routine items. Map its effective_date to the first day of the earliest open period while preserving the original occurred_at date in metadata:

function resolveEffectiveDate(eventDate: string, earliestOpenDate: string): string {
  return eventDate < earliestOpenDate ? earliestOpenDate : eventDate;
}

Automated year-end closing entries

At fiscal year-end, permanent accounts (Assets, Liabilities, Equity) carry their balances forward into the new year, while temporary accounts (Revenue and Expenses) must reset to zero and transfer Net Income into Retained Earnings.

Formula · Year-end closing transfer

Net Income (to Retained Earnings) = Σ Revenue credits − Σ Expense debits

Revenue accounts
debited for their full credit balance so each resets to $0
Expense accounts
credited for their full debit balance so each resets to $0
Retained Earnings
credited for net profit (or debited for a net loss)
Worked example

Redwood Labs (fictional) — FY 2026 closing entry and late vendor bill

On 31 December 2026 (while December is soft_closed), Redwood Labs queries cumulative FY 2026 P&L postings:

  • Revenues: Subscription Revenue $180,000 Cr + Services Revenue $40,000 Cr = $220,000
  • Expenses: Payroll Expense $125,000 Dr + Hosting Expense $35,000 Dr + Marketing Expense $22,000 Dr = $182,000
  • Net Income: $220,000 − $182,000 = $38,000

The close job generates one automated source_type = 'closing' entry dated 2026-12-31, then locks December 2026:

31 December 2026: Automated year-end closing entry
AccountDebit (Dr)Credit (Cr)
Subscription Revenue$180,000
Services Revenue$40,000
Payroll Expense$125,000
Hosting Expense$35,000
Marketing Expense$22,000
Retained Earnings$38,000
Total✓ Balanced$220,000$220,000

All five P&L accounts now sit at $0 for 1 January 2027, and Retained Earnings increased by $38,000.

Late adjustment (7 January 2027): A forgotten $1,200 December hosting invoice arrives after December is locked. Instead of unlocking FY 2026, finance books it into the next open period (2027-01-01) with memo "Late Dec 2026 hosting bill":

1 January 2027: Late vendor bill booked in next open period
AccountDebit (Dr)Credit (Cr)
Hosting Expense$1,200
Accounts Payable$1,200
Total✓ Balanced$1,200$1,200

Real-world application

Finance teams run a structured monthly close checklist—bank reconciliation, deferred revenue release, FX revaluation, accruals—during the soft_closed window before flipping the period to locked. Once locked, your ledger can safely write immutable account_period_snapshots, making historical trial balances and audit exports instantaneous and guaranteed reproducible.

Recap

  • Model accounting_periods with open, soft_closed and locked states to protect published numbers while supporting close adjustments.
  • Guard journal_entries with a BEFORE INSERT database trigger checking effective_date against period status.
  • Route late operational items to the earliest open period rather than casually reopening locked months.
  • Automate fiscal year-end closing entries idempotently to zero Revenue and Expenses into Retained Earnings.

Active recall

Quick check · Test yourself

Work out each answer in your head first, then reveal the explanation and mark whether you got it right.

  1. 1What is the difference between a soft_closed period and a locked period?

    Show worked answer

    A soft_closed period blocks automated operational traffic (billing, payouts, webhooks) while allowing authorised finance roles to post month-end accruals and adjustments. A locked period blocks all postings for everyone so published reports never change.

  2. 2March 2026 is locked. On 8 April, finance discovers an unrecorded $1,200 March hosting bill. How should the ledger record it?

    Show worked answer

    Do not reopen March for a routine immaterial bill. Post Dr Hosting Expense $1,200 / Cr Accounts Payable $1,200 with effective_date = 2026-04-01 (the next open period) and note 'March 2026 late vendor bill' in the entry description.

  3. 3At fiscal year-end, Revenue accounts total $220,000 Cr and Expense accounts total $182,000 Dr. What balanced closing entry zeroes them into Retained Earnings?

    Show worked answer

    Debit Revenue accounts $220,000, credit Expense accounts $182,000, and credit Retained Earnings $38,000 ($220,000 − $182,000 net income). Total debits ($220,000) equal total credits ($220,000).

Key terms in this lesson

Lesson 13 of 16 · Accounting for Developers

Finished this lesson?

Mark it complete to track your progress through the course. Progress is saved in this browser only.

Previous lesson: Implementing Inventory Costing (FIFO and Average) in Code

Subject GuideAccounting Systems & Personal Finance

Mei-Ling Chen(陈美玲)

Turns accounting invariants into clean code—and everyday personal money clarity.

  • Double-Entry Ledger Engineering
  • SQL Schemas & Financial APIs
  • Multi-Currency & Billing Code

More lessons with Mei-Ling

Published:

Educational content only—not accounting, tax, legal or investment advice. See our editorial policy or report an error via our corrections policy.

Search