# AccountingLedger

## Description

AccountingLedger is the immutable, posted-only general-ledger fact table for the financial-accounting module. Each row represents one posted debit or credit effect for one account, company, accounting period, and posting date. JournalEntry owns the posting lifecycle, JournalLine stores prepared lines for DRAFT journal workflows, and AccountingLedger is the flat fact surface used for balances, reporting, and traceability after posting succeeds.

AccountingLedger rows carry `journalEntryId` as the required parent reference and `journalLineId` as optional provenance to the prepared line that produced the posted fact. The posted fact also stores company, accounting period, posting date, account, and amount fields directly so ledger queries do not need to join back to JournalEntry for common filters.

This design represents a single implicit primary ledger per company: the company owns the base currency, fiscal calendar, and chart-of-accounts scope, so no separate Ledger master exists. Current ledger amounts are recorded in the company's base currency. If parallel accounting (multi-GAAP / multi-book) is needed later, it is added as an `AccountingBook` entity with a `accountingBookId` column on JournalEntry and AccountingLedger, backfilling existing rows with the company's primary book.

## Domain Model Definitions

### Model type

AppendOnly

### Command Definitions

- [postJournalEntry](../command/PostJournalEntry.md) - Creates AccountingLedger rows from balanced JournalLine records on a DRAFT JournalEntry
- [reverseJournalEntry](../command/ReverseJournalEntry.md) - Creates reversal AccountingLedger rows for the auto-posted reversal JournalEntry

### Query Definitions

- None yet

### Models

- AccountingLedger
- JournalEntry
- JournalLine

### Invariants

- AccountingLedger rows are created only by posting commands
- AccountingLedger is posted-only and carries no lifecycle status
- AccountingLedger rows are append-only and are not edited for corrections
- `journalEntryId` is required and references the JournalEntry that produced the posted fact
- `journalLineId` is optional provenance to the prepared JournalLine
- `companyId`, `accountingPeriodId`, and `postingDate` are copied from JournalEntry for flat ledger queries
- Each ledger row specifies either a debit amount or a credit amount
- Corrections are represented by reversal or adjustment journal entries, not by editing existing AccountingLedger rows

### Relationships

- **References JournalEntry**: Each posted fact belongs to the JournalEntry that produced it
- **Optionally References JournalLine**: A posted fact can trace back to the prepared JournalLine used by DRAFT journal posting
- **References Company**: Company scope is stored directly for ledger filtering
- **References AccountingPeriod**: Accounting period is stored directly for ledger filtering
- **References Account**: Each posted fact references a GL Account via accountId
