import { PostingDirection, TaxLineComponent, TaxResolver } from "./resolver.mjs"; //#region src/document.d.ts type PriceBasis = 'inclusive' | 'exclusive'; declare const DOCUMENT_TAX_ALGORITHM: "document-tax/1"; interface DocumentTaxLineInput { /** Stable id the breakdown line is keyed by — the document's own line id. */ readonly lineId: string; readonly quantity: number; /** Display only — the line is rated on `amountMinor`. */ readonly unitPriceMinor: number; /** The line's pre-discount amount, as the caller's pricing computed it (measured goods included). */ readonly amountMinor: number; /** A discount on this line alone, minor units, in `[0, amountMinor]`. */ readonly lineDiscountMinor: number; readonly taxClass: string; /** Whether the document-level discount may reach this line. A delivery fee states `false`. */ readonly discountable: boolean; /** Overrides the document's basis for this line only. */ readonly priceBasis?: PriceBasis; /** Pack extras (`sdRate`, …). `priceMode` is always set from the basis and cannot be overridden here. */ readonly extra?: Readonly>; /** * Where `taxClass` came from, in the pack's vocabulary (`class`, `deployment_default`, …) — echoed so a * line priced on a defaulted class stays flagged for review on every document that reads it. Required: * an omitted basis would read as "classified". */ readonly classBasis: string; } /** The fiscal position the classes were mapped under — decided once, before rating, and stored. */ interface DocumentFiscalPosition { readonly position: string; readonly reference?: string; readonly reason: string; } interface DocumentTaxInput { /** Do the line amounts include the tax? Required: the basis decides every figure on the document. */ readonly priceBasis: PriceBasis; readonly lines: readonly DocumentTaxLineInput[]; /** A discount on the whole document, minor units, allocated before tax. */ readonly documentDiscountMinor: number; readonly direction: PostingDirection; /** The time of supply: the rates in force then are the ones every later document carries. */ readonly asOf: Date; /** Required: the position the line classes were already mapped under (a domestic sale states its default). */ readonly fiscalPosition: DocumentFiscalPosition; } interface DocumentTaxLine { readonly lineId: string; readonly quantity: number; readonly unitPriceMinor: number; readonly taxClass: string; readonly priceBasis: PriceBasis; readonly amountMinor: number; readonly lineDiscountMinor: number; /** This line's share of the document discount. */ readonly documentDiscountMinor: number; /** Value excluding tax. */ readonly netMinor: number; /** Every component's tax (VAT and SD alike). Print each by `taxes[].rateCode`, never this as "VAT". */ readonly taxMinor: number; /** What the buyer pays for this line: `netMinor + taxMinor`. */ readonly grossMinor: number; readonly taxes: readonly TaxLineComponent[]; /** Present when the pack ruled the class not taxable — the legal reason for the zero. */ readonly notTaxableReason?: string; readonly classBasis: string; } interface DocumentTaxRate { readonly rateCode: string; readonly name?: string; readonly rate: number; /** Σ of the components' assessable bases; absent when any component did not state one. */ readonly baseMinor?: number; readonly taxMinor: number; /** For a class that carries no tax: the pack's legal reason. */ readonly notTaxableReason?: string; } interface DocumentTaxBreakdown { readonly algorithm: typeof DOCUMENT_TAX_ALGORITHM; readonly priceBasis: PriceBasis; readonly direction: PostingDirection; readonly asOf: Date; readonly fiscalPosition: DocumentFiscalPosition; readonly lines: readonly DocumentTaxLine[]; readonly totals: { readonly amountMinor: number; /** Line discounts plus the document discount. */ readonly discountMinor: number; readonly netMinor: number; readonly taxMinor: number; /** What the document charges. */ readonly grossMinor: number; }; readonly byRate: readonly DocumentTaxRate[]; } /** A broken arithmetic invariant or an input the algorithm cannot rate. Never clamped. */ declare class TaxDocumentError extends Error { readonly code = "TAX_DOCUMENT_INVALID"; constructor(message: string); } /** Rate every line of a document and return its breakdown. Throws {@link TaxDocumentError}. */ declare function computeDocumentTax(input: DocumentTaxInput, resolver: Pick): DocumentTaxBreakdown; /** * Re-check a STORED breakdown before anything reads money from it: the algorithm version is known, every * line reconciles, and the totals and the per-rate roll-up are the sums of the lines. Throws * {@link TaxDocumentError}; a breakdown that fails was edited or truncated after it was computed. */ declare function verifyDocumentTax(breakdown: DocumentTaxBreakdown): DocumentTaxBreakdown; /** What one line has already given back: cumulative gross refunded and units returned. */ interface TaxRefundLedgerLine { readonly lineId: string; readonly grossMinor: number; readonly quantity: number; } /** The document's refund history, one entry per touched line. The caller persists it with the document. */ type TaxRefundLedger = readonly TaxRefundLedgerLine[]; /** What a refund returns, measured against the stored breakdown and its ledger. */ type TaxRefundRequest = /** Money back with no goods named: allocated over what each line has left, pro rata by that remainder. */ { readonly kind: 'amount'; readonly amountMinor: number; } | /** * Goods back: units of named lines, priced at the line's own gross per unit — or at `grossMinor`, the * money actually refunded for those units (a restocking fee kept, a partial credit), never above their worth. */ { readonly kind: 'lines'; readonly lines: readonly { readonly lineId: string; readonly quantity: number; readonly grossMinor?: number; }[]; }; interface TaxRefundLine { readonly lineId: string; readonly grossMinor: number; readonly netMinor: number; /** Every component's tax (VAT and SD alike); print each by `taxes[].rateCode`, never this as "VAT". */ readonly taxMinor: number; readonly taxes: readonly { readonly rateCode: string; readonly rate: number; readonly taxMinor: number; }[]; } interface TaxRefund { readonly grossMinor: number; readonly netMinor: number; readonly taxMinor: number; readonly lines: readonly TaxRefundLine[]; /** The ledger after this refund — persist it with the document and pass it to the next refund. */ readonly ledger: TaxRefundLedger; } /** * The tax a refund reverses, read off the STORED breakdown — never re-rated. Each line's tax is taken * cumulatively (tax on gross refunded to date minus tax on gross refunded before), so a line refunded in * full returns exactly its stored tax, and no sequence of refunds — money, goods, or both — drifts or * returns more than the line holds. */ declare function refundTaxOf(breakdown: DocumentTaxBreakdown, request: TaxRefundRequest, ledger: TaxRefundLedger): TaxRefund; /** * The share of `whole` (a line's tax) that a partial refund moving its cumulative base from `before` * to `after` (of `of`) returns: the cumulative share now minus the cumulative share before. Successive * partials therefore always sum to exactly `whole` once `after` reaches `of`, whatever the split — the * ONE allocation the 6.7 (`refundTaxOf`) and an invoice credit note both use. */ declare function partialTaxShare(whole: number, before: number, after: number, of: number): number; //#endregion export { DOCUMENT_TAX_ALGORITHM, DocumentFiscalPosition, DocumentTaxBreakdown, DocumentTaxInput, DocumentTaxLine, DocumentTaxLineInput, DocumentTaxRate, PriceBasis, TaxDocumentError, TaxRefund, TaxRefundLedger, TaxRefundLedgerLine, TaxRefundLine, TaxRefundRequest, computeDocumentTax, partialTaxShare, refundTaxOf, verifyDocumentTax };