# journalEntriesForDocument

## Overview

Given a business document's human-readable number, returns **all journal entries that document produced**, as flat rows grouped by `journalEntryId` — the same row shape as [journalLinesGroupedByEntryId](./journalLinesGroupedByEntryId.md), so the same table/modal UI can render the result.

A `JournalEntry` only ever references its **direct accounting driver** through (`sourceDocumentType`, `sourceDocumentId`). Business documents reach the general ledger by different paths, so this resolver resolves the number to that driver set per `documentType`:

- **ACCOUNT_PAYABLE_DOCUMENT** — the AP posting journal (`sourceDocumentType = ACCOUNT_PAYABLE_DOCUMENT`, `sourceDocumentId = invoice id`), **plus** any acquisition cost adjustment journal (erp-kit 0.50): when the invoiced price differs from the provisional order price, posting trues up the receipt cost, booking a separate costing journal. That entry references an `AcquisitionCostAdjustment` (`sourceDocumentType = ACQUISITION_COST_ADJUSTMENT`), and the adjustment references the invoice (`sourceType = ACCOUNT_PAYABLE_DOCUMENT`, `sourceId = invoice id`) — so this is invoice → adjustment ids → journals (two hops).
- **ACCOUNT_RECEIVABLE_DOCUMENT** — the AR posting journal for a customer invoice (`sourceDocumentType = SALES_INVOICE`, `sourceDocumentId = AR document id`). AR has no inventory integration, so there is no cost-adjustment hop.
- **INBOUND_SHIPMENT / OUTBOUND_SHIPMENT / TRANSFER_ORDER** — one hop: the movement posts `InventoryLedger` rows (`sourceType` / `sourceId`), and the costing journal references the ledger (`sourceDocumentType = INVENTORY_LEDGER`).
- **PURCHASE_ORDER** — a purchase order has **no journal of its own** (ordering is not an accounting event). Its related journals are the union of (a) the AP journals of the invoices billed from its lines, (b) the acquisition cost adjustment journals those invoices booked, and (c) the costing journals of the receipts raised against its lines.

`STOCK_ADJUSTMENT` and the payment documents (`INCOMING_PAYMENT` / `OUTGOING_PAYMENT`) are intentionally unsupported because they have no human-readable document number to look up. Every hop is a plain two-step `id IN (...)` lookup (TailorDB's SQL layer has no DISTINCT / correlated subqueries). Results are company-scoped (the journal side always filters by `companyId`) and ordered newest entry first. Because a single document's journals are few, the whole set is returned in one shot (`hasNextPage` is always false; `entryCount` equals `total`).

## Modules Commands Used

- (none) — a read-only lookup over `AccountPayableDocument` / `AccountReceivableDocument` / `PurchaseOrder` / `InboundShipment` / `OutboundShipment` / `TransferOrder`, `InventoryLedger`, `AcquisitionCostAdjustment`, and `JournalEntry` / `JournalLine` / `Account`; issues no module command.

## Exception Handling

| Error Code | Description |
| --- | --- |
| N/A (validation) | Thrown for an invalid `documentType` (not one of ACCOUNT_PAYABLE_DOCUMENT, ACCOUNT_RECEIVABLE_DOCUMENT, PURCHASE_ORDER, INBOUND_SHIPMENT, OUTBOUND_SHIPMENT, TRANSFER_ORDER) |
| N/A | Read-only; returns an empty result when the document number is not found or produced no journal entries |
