import { CurrencyCode } from "@classytic/primitives/currency"; import { DateRange } from "@classytic/primitives/period"; //#region src/obligation.d.ts /** * The period a computed obligation covers — primitives' inclusive * `DateRange` plus the human label authorities use. */ interface TaxPeriod extends DateRange { /** Human label — '2026-07', 'Q2 FY2026-27', 'AY 2026-27'. */ label: string; } /** * A computed tax obligation — the universal output of filing-time * computation. Jurisdiction-specific detail rides in `breakdown` (typed * via the generic, never flattened into this shape). */ interface TaxObligation { /** ISO 3166-1 alpha-2-ish jurisdiction code: 'BD', 'CA', 'US', 'EU-DE'. */ jurisdiction: string; /** Tax category: 'vat', 'gst-hst', 'income', 'tds', 'sales', 'payroll', … */ category: string; /** Period the obligation covers. */ period: TaxPeriod; /** * Total owing in INTEGER MINOR UNITS (paisa/cents — see primitives' * `minorUnitFactor(currency)` for the per-currency factor). Negative = * refund / carry-forward position. Minor-unit discipline is the * contract — packs that compute in major units must convert at their * boundary. Pairs with `currency` as primitives' `Money` shape. */ totalOwing: number; /** ISO 4217 currency (primitives' branded `CurrencyCode` accepted). */ currency: CurrencyCode | string; /** Jurisdiction-specific breakdown — typed by the pack, opaque to core. */ breakdown: TBreakdown; /** Authority reference once filed (return number, filing id). */ externalRef?: string | undefined; } /** * A filing-time computation unit. One engine per (jurisdiction, category, * form family) — `bd monthly VAT 9.1`, `ca GST34`, `ca T2` are separate * engines, not modes of one. * * Contract: * - `validate` narrows untrusted input (typically `schema.parse`); it * THROWS on bad input — compute never sees garbage. * - `compute` is pure w.r.t. its input (async allowed for packs that * look up rates from injected sources, but no hidden I/O). * - `render` produces the authority's filing format (XML/JSON/CSV) — * optional because not every obligation is filed as a document. */ interface TaxEngine { /** Validate untrusted input into the engine's typed input. Throws on failure. */ validate(input: unknown): TInput; /** Compute the obligation from validated input. */ compute(input: TInput): TaxObligation | Promise>; /** Render the obligation as a filing-ready document. */ render?(obligation: TaxObligation): string | Uint8Array; } /** Options threaded through obligation posting. */ interface PostObligationOptions { /** * Resolve a logical account ROLE ('collected', 'recoverable', * 'transition', 'payroll-cpp', …) to the host's real account id. Roles * are pack vocabulary; ids are host vocabulary — this function is the * entire coupling between them. */ resolveAccount: (role: string) => unknown; /** Posting date (defaults to `obligation.period.end`). */ postingDate?: Date | undefined; /** Partner ref to stamp on each item (the tax authority as a partner). */ partnerId?: unknown; /** * Transaction session, passed through to every repository create so the * whole obligation posts atomically when the host runs a transaction. * * OPTIONAL, but its absence is not free: without it a multi-entry obligation can post partially, so * `postObligation` reports exactly what landed (see {@link PartialObligationPostingError}) instead of * failing opaquely. */ session?: unknown; /** * The REGISTRATION this obligation is filed under — REQUIRED, and never defaulted. * * It is the part of the identity the obligation itself does not carry: two registrations file the * same jurisdiction, category and period, and their postings must not share an idempotency key. A * default here would silently merge two filers' obligations into one. */ registrationId: string; } /** * The deterministic identity of one obligation posting. * * DERIVED, never supplied and never random: a caller-invented key defeats deduplication while looking * correctly wired, which is the failure this exists to prevent. Two computations of the same * registration's same period produce the same key, so a retry is recognisable as a retry. * * The period bounds are ISO instants rather than the human `label` — a label is presentation ('Q2 * FY2026-27') and two packs can spell the same quarter differently. */ declare function obligationKeyOf(obligation: TaxObligation, registrationId: string): string; /** Per-entry key: the obligation key plus the entry's position in the adapter's output. */ declare function obligationEntryKey(obligationKey: string, index: number): string; /** * Thrown when a multi-entry obligation posted PARTIALLY — i.e. no transaction was supplied and a create * failed after at least one had already landed. * * It carries what converged and what did not, because the recovery is a re-drive: the same * `obligationKey` and per-entry keys are deterministic, so a sink that honours them will skip the * entries already written and complete the rest. Failing with a bare error would leave the caller unable * to tell a total failure from a half-posted obligation — and half a VAT obligation on the books is the * outcome worth naming. */ declare class PartialObligationPostingError extends Error { readonly obligationKey: string; readonly postedCount: number; readonly totalEntries: number; readonly failedIndex: number; readonly cause: unknown; readonly code = "OBLIGATION_PARTIALLY_POSTED"; constructor(obligationKey: string, postedCount: number, totalEntries: number, failedIndex: number, cause: unknown); } /** * Converts a TaxObligation into journal-entry payloads. One adapter per * pack (or per category when postings differ structurally). Returns an * ARRAY because real obligations post multiple entries (cash-basis VAT * with a transition account, payroll with per-agency splits). */ interface LedgerPostingAdapter { toJournalEntries(obligation: TaxObligation, options: PostObligationOptions): Array>; } /** Minimal repository surface `wireTaxToLedger` needs — structurally satisfied by @classytic/ledger's journalEntries repo. */ interface JournalEntrySink { create(data: Record, options?: Record): Promise; } /** * Wire an engine + adapter + journal sink into a single * `postObligation(input, options)` function — the entire ERP integration * surface. * * Entries post SEQUENTIALLY, in adapter order — obligations are small * (a handful of entries) and ordering + fail-fast beat parallel speed: * on failure, everything before the failing entry is visible/rolled back * by the host's session, and nothing after it was attempted. Pass * `options.session` to make the batch atomic. */ declare function wireTaxToLedger(engine: TaxEngine, adapter: LedgerPostingAdapter, journalEntries: JournalEntrySink): (input: unknown, options: PostObligationOptions) => Promise<{ obligation: TaxObligation; postedEntries: unknown[]; obligationKey: string; }>; //#endregion export { JournalEntrySink, LedgerPostingAdapter, PartialObligationPostingError, PostObligationOptions, TaxEngine, TaxObligation, TaxPeriod, obligationEntryKey, obligationKeyOf, wireTaxToLedger };