Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
Exchange-Rate Convention Normalization (GL & Payments)
Status: draft Author: Claude (audit follow-up), for [email protected] Date: 2026-07-02
Superseded corrective design: Accounting Posting Corrections resolves the amount contract and covers the current invoice/payment/provider code. The user explicitly authorized assuming accounting has no users, so this document's historical-transition proposal does not apply to that implementation. Retained below as historical context, not implementation instructions.
TLDR
The platform's documented currency convention is currency.exchangeRate =
foreign units per base unit, so document→base conversion is divide
(this is how the purchaseInvoiceLine generated columns, the invoice views,
and .ai/rules/purchasing-conversion-factors.md all work). The GL posting and
payment chain (post-sales-invoice, post-purchase-invoice, post-payment /
build-payment-journal, tie-out RPCs, seeded payment amounts) instead assumes
base = document × rate. The chain is internally self-consistent — control
accounts net to zero and tie-outs pass — but for every non-base-currency
document the GL base amounts are wrong by the rate factor squared relative to
true base, seeded payment totalAmounts label base sums with the foreign
currency code (wrong remittance instruction), PO-matched FX invoices book a
phantom PPV, and realized FX gain/loss signs are inverted. This spec proposes
normalizing the whole chain to the documented divide-to-base convention in one
coordinated change.
Problem Statement
Facts established by the 2026-07-02 audit (.ai/runs/2026-07-02-invoice-payment-audit.md):
purchaseInvoiceLine.unitPrice/shippingCost/taxAmountare GENERATED assupplierX / exchangeRate(migration20250807094441) — i.e. base currency. ThepurchaseInvoices/salesInvoicesviews therefore expose base-denominatedtotalAmount/balance.post-purchase-invoice/index.tsmultiplies those already-base line amounts byexchangeRatebefore journaling (comment claims they are in invoice currency — false).post-sales-invoicedoes the same on the sales side (wheresalesInvoiceLine.unitPriceis also base).post-receiptbooks GR/IR at base (no multiplier), so the invoice-side multiplier produces a phantom PPV oflineBase × (R − 1)on every PO-matched foreign-currency invoice even at identical prices.post-payment/build-payment-journalmirror the multiply convention (applied × targetExchangeRatecontrol relief,totalAmount × exchangeRatecash), which is why the loop closes and nothing crashes.- Payment seeding (
payments new.tsx) sums view balances (base) intopayment.totalAmountbut labels it with the invoice's foreigncurrencyCode→ the remittance instruction is wrong by the rate factor. - FX gain/loss sign: with foreign-per-base rates, a falling rate means the foreign currency strengthened (an AP loss), but the builder books a gain.
- Tie-out RPCs (
20260630104012) treat the base-denominated view totals as document-currency and multiply by rate. $paymentId.tsxconverts base→"payment currency" by divide — the same inverted convention.- costLedger / fixedAsset acquisition costs from FX purchase invoices are stored at the ×R magnitude, inconsistent with receipt-side costing.
Interim state (already merged on this branch): header supplierShippingCost
is divided to base at posting and folded in before the chain's ×R multiplier,
so AP credit == view balance × R for every component and payments settle
exactly. That keeps the loop closed but leaves the loop itself on the wrong
convention.
Proposed Solution
Adopt divide-to-base everywhere; journal lines are true base amounts; rates appear only in FX gain/loss computation and display conversions.
Concretely (one coordinated change):
post-sales-invoice/post-purchase-invoice: remove the× invoiceExchangeRatemultiplier — line amounts (and the header shipping, already divided) are base already. Delete the false-premise comments.post-receipt: convert header shipping with divide (it currently multiplies atindex.ts:562-564); line amounts are already base.build-payment-journal: defineappliedAmount/discountAmount/writeOffAmountas base currency (they already are in practice — users enter them against base-denominated view balances). Control/discount/ write-off lines post the raw amounts (no× targetExchangeRate). Cash line =totalAmountwherepayment.totalAmountbecomes a base amount, with the document-currency amount derived for display (× exchangeRate), or vice versa — decide in Open Questions. FX gain/loss = document-currency principal × (1/payRate − 1/invRate), with the sign convention re-derived and the(isAR ? 1 : -1)normalization + the storedinvoiceSettlement.fxGainLossAmountgenerated column updated together.- Payment seeding (
new.tsx): seed the document-currency amount (balance × exchangeRate) when labeling with the foreign currency code, or seed base and label with the base currency code (Open Question). - Tie-out RPCs (
get_ar_tie_out/get_ap_tie_out/ aging): drop the ×rate on view totals. $paymentId.tsxavailableCredit conversion: flip to match.- Update golden-master tests in
post-payment.test.ts(they currently pin the multiply convention) and the costLedger/fixedAsset magnitudes noted in the audit.
Design Decisions
| Decision | Choice | Rationale |
|---|---|---|
| Rate semantics | foreign-per-base, divide to base | Matches generated columns, views, update-exchange-rates job, and .ai/rules docs |
| Journal denominates | true base currency | GL must be base; display converts |
| Settlement amounts | base currency | Users enter them against base view balances today; no data migration needed |
| Migration of history | none (forward-only) | Historic journals were self-consistent; restating them is a separate decision |
Data Model Changes
invoiceSettlement.fxGainLossAmountgenerated column: re-derive formula and sign normalization (migration).- No table shape changes expected.
API / Service Changes
Edge functions: post-sales-invoice, post-purchase-invoice, post-receipt
(header shipping), post-payment, build-payment-journal, post-memo
(verify), tie-out RPC migrations. App: payment seeding route, PaymentApplyTable
formatting, $paymentId.tsx credit conversion.
Acceptance Criteria
- For a foreign-currency invoice, journal base amounts equal document amount ÷ rate for every component.
- PO-matched FX invoice at identical prices books zero PPV.
- Payment seeded from an FX invoice shows the correct document-currency remittance amount.
- Realized FX gain/loss sign matches economic direction on both AR and AP.
- Tie-out RPCs reconcile subledger to control accounts for FX documents.
- post-payment golden-master tests updated and passing.
- Rate = 1 behavior byte-identical to today (regression guard).
Risks
| Risk | Severity | Mitigation |
|---|---|---|
| Historic FX journals inconsistent with new ones | Med | Forward-only cutover; document the cutover date; optional restatement tooling later |
| Partial deployment (one function updated, another not) breaks the closed loop | High | Ship as one PR; the invariant "AP/AR credit == what payment debits" must hold at every commit |
| Hidden consumers assuming ×R magnitudes (reports, Xero sync) | Med | Grep for exchangeRate usages across packages/ee and jobs before implementation |
Open Questions
HARD STOP: Do not proceed with implementation until these are answered.
- Should
payment.totalAmountstore base or document currency? (Base avoids migration of the seeding flow; document matches the remittance mental model.) - Restate or leave historic FX journals? (Proposal: leave.)
- Does Xero sync (BillSyncer/SalesInvoiceSyncer) push amounts derived from the affected fields, and in which currency does Xero expect them?
Changelog
- 2026-07-02: Created from the invoice/payment audit findings (P2 + Pay1).