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.
How exchange rates get updated today (the integration path to eliminate)
Researched 2026-09-02 on branch currency-exchange-rate-refactor (base = main @ ed0bdebd95).
All paths absolute under /Users/barbinbrad/conductor/workspaces/carbon/hat-yai/.
Important branch caveat: two open PRs touch exactly this area and are NOT in this
tree — #1542 fix(jobs): rebase exchange rates once per company group, not once per company (commit b4d30d3ca3, branch origin/fix/exchange-rate-job-group-scoping) and
#1530 fix(accounting): take the refresh rate from the document, not the request
(commit f8e3a386dd, branch origin/fix/exchange-rate-refresh-document-currency).
Several sibling commits on those branches (aec17de6ba, 02a7ce60f0, 4931842606,
deee8198af, 1f3d0bb0e5, 0328146b0c — which add a resolveCurrencyAndRate
resolver) are likewise unmerged. Everything below describes the tree as it stands,
with the pending changes flagged inline.
1. The integration
- Definition:
packages/ee/src/exchange-rates/config.tsx:6-19—defineIntegration({ name: "Exchange Rates", id: "exchange-rates-v1", active: true, category: "Accounting", settings: [], schema: z.object({}) }). Zero settings, empty schema — there is nothing to configure per company; installing it is a pure on/off toggle. (defineIntegrationitself:packages/ee/src/fns.ts:46+.) - Registry: imported and included in the
integrationsarray atpackages/ee/src/index.ts:2and:35. - Plan gating:
packages/ee/src/plan.ts:29-33—INTEGRATION_WHITELISTcontains"exchange-rates-v1"(alongside"email"), so it bypasses theINTEGRATIONSBusiness/Partner plan gate and is available on every plan. I.e. it is already "free"; the only friction is that a user must find and install it. - API key is platform-level, not per-company:
packages/env/src/index.ts:172—EXCHANGE_RATES_API_KEYis a single global env var (listed under optional integration vars in.claude/rules/environment-configuration.md). Companies never supply a key. - Install path: Settings → Integrations →
apps/erp/app/routes/x+/settings+/integrations.$id.tsxaction →upsertCompanyIntegrationinapps/erp/app/modules/settings/settings.server.ts:293-314: upserts onecompanyIntegrationrow{ id: "exchange-rates-v1", companyId, active: true, metadata: {} }withonConflict: "id,companyId". Table:packages/database/supabase/migrations/20240119095150_integrations.sql:26-34(PK("id","companyId")— company-scoped, which matters in §2). - No lifecycle hooks:
packages/ee/src/hooks.server.tsregisters hooks for email/jira/linear/onshape/quickbooks/etc. — nothing for exchange-rates. Enabling it creates noeventSystemSubscription, no vault secret, nothing but thecompanyIntegrationrow. The nightly job simply looks for that row.
2. The updater job
packages/jobs/src/inngest/functions/scheduled/update-exchange-rates.ts
(updateExchangeRatesFunction, registered via
packages/jobs/src/inngest/functions/scheduled/index.ts → packages/jobs/src/inngest/index.ts).
- Schedule: Inngest cron
"0 0 * * *"(daily, midnight UTC),retries: 2(update-exchange-rates.ts:73-74). - Gate: selects
companyIntegrationrows withid = "exchange-rates-v1"andactive = true(:79-83). Zero rows → the job exits without fetching anything (:92-95). This is the "rates require an integration" behavior to eliminate. - Source API:
getExchangeRatesClient(EXCHANGE_RATES_API_KEY)(:102-104) frompackages/ee/src/exchange-rates/exchange-rates.server.ts:94-104— default URLhttps://api.exchangeratesapi.io/v1/latest(apilayer's exchangeratesapi.io). Free tier is EUR-base only (comment at exchange-rates.server.ts:47-50), so the job fetches EUR rates once and rebases to each company's base currency by division (convertExchangeRates, exchange-rates.server.ts:68-91), caching per base currency (:129-132, :154-166). - What it writes: for each company with the integration, it loads the company's
baseCurrencyCode(:139-153), then full-row upserts everycurrencyrow for the company'scompanyGroupIdwithexchangeRate: round(rate)(internal SCALE=5 rounding, comment :200-203) andupdatedAt(:168, :197-221). Rows whose feed rate is missing or rounds to 0 are filtered out and left untouched (:206) — silently stale. NoupdatedByis set. It does NOT writeexchangeRateHistory(see §6 — nothing does). - The group-scoping bug (pending PR #1542):
currencyrows are group-scoped (§5) but the loop iterates per-company integration, so a multi-company group is rewritten once per member, and members with different base currencies overwrite each other — last writer wins for the whole group. The PR version (read viagit show b4d30d3ca3:...update-exchange-rates.ts) buckets companies bycompanyGroupId, refuses groups with mixed base currencies, rebases and writes once per group, logs the currencies the feed omitted, and setsupdatedBy: "system". - With no integration: rates stay at whatever they were — the seeded
1(§5) or the last manual edit. There is no staleness indicator anywhere except the per-documentexchangeRateUpdatedAttimestamp. - There is no edge function for rate updates; this Inngest job is the only writer
of feed rates. (
grep exchange packages/database/supabase/functionsonly hits document-levelexchangeRateusage increate/post-purchase-invoice.)
3. Manual updates
Yes — fully supported, and it is the same column the job overwrites.
- Routes:
/x/accounting/exchange-rateslist —apps/erp/app/routes/x+/accounting+/exchange-rates.tsx:17-36(loader:getCurrencies(client, companyGroupId, …), permissionview: "accounting"). Edit drawer/x/accounting/exchange-rates/:currencyId—apps/erp/app/routes/x+/accounting+/exchange-rates.$currencyId.tsx:43-81(action:update: "accounting"→upsertCurrency). - Service:
upsertCurrency—apps/erp/app/modules/accounting/accounting.ee.service.ts:3696-3725(plain update of thecurrencyrow:exchangeRate,historicalExchangeRate,decimalPlaces,customFields,updatedBy). - Form:
apps/erp/app/modules/accounting/ui/ExchangeRates/ExchangeRateForm.tsx— editableexchangeRateNumberField (:162-171; base currency pinned to 1 via min/max, :166-167),historicalExchangeRatefor IAS 21 equity translation (:172-182), plus a history chart/table/CSV fed byexchangeRateHistory(:187-298) — which is empty in practice (§6). - Table:
ExchangeRatesTable.tsxshows name/code/exchangeRate with an "Edit Currency" context item only — no "New" button; the currency list is fixed at seed time (§5). (path.to.newExchangeRateexists atapps/erp/app/utils/path.ts:1484andExchangeRateForm.tsx:142references it, but there is noexchange-rates.new.tsxroute — the create path is dead code.) - Sidebar: always visible under Accounting —
apps/erp/app/modules/accounting/ui/useAccountingSubmodules.tsx:127. Also an onboarding "get started" checklist item (packages/onboarding/src/content/setup.ts:82-87). - Clobbering: yes. The nightly job upserts every group currency row it has a feed
rate for (§2), so a manual edit to a feed-covered currency survives at most until
the next midnight UTC while any company in the group has the integration active.
There is no per-row "manual override / don't auto-update" flag on
currency. ConverselyhistoricalExchangeRateis never touched by the job — manual-only. - Rate history shown in the form comes from
getExchangeRateHistory(accounting.ee.service.ts:4735-4750, last 6 months bycompanyGroupId+currencyCode) — but see §6: no writer exists.
4. On-demand refresh on documents
Six sibling routes, one per FX-bearing document (all present in tree):
apps/erp/app/routes/x+/quote+/$quoteId.exchange-rate.tsxapps/erp/app/routes/x+/sales-order+/$orderId.exchange-rate.tsxapps/erp/app/routes/x+/purchase-order+/$orderId.exchange-rate.tsxapps/erp/app/routes/x+/purchase-invoice+/$invoiceId.exchange-rate.tsxapps/erp/app/routes/x+/sales-invoice+/$invoiceId.exchange-rate.tsxapps/erp/app/routes/x+/supplier-quote+/$id.exchange-rate.tsx
Behavior (read from the PO route, representative): POSTed currencyCode from the
form → getCurrencyByCode(client, companyGroupId, currencyCode)
(accounting.ee.service.ts:3735+ — reads the live currency table, no API
call) → writes only exchangeRate onto the document via
update<Doc>ExchangeRate. The refresh is therefore only as fresh as the shared
table — with no integration it re-stamps the seeded/manual value.
- UI: refresh button in each document's Properties panel, e.g.
apps/erp/app/modules/sales/ui/Quotes/QuoteProperties.tsx:430-450(posts the document's owncurrencyCodetopath.to.quoteExchangeRate), with theexchangeRateUpdatedAttimestamp shown via tooltip (:420-428). Same pattern inSalesOrderProperties,PurchaseOrderProperties,SupplierQuoteProperties,PurchaseInvoiceProperties,SalesInvoiceProperties. - Pending PR #1530: because the routes trust the posted
currencyCode, a crafted POST can stamp another currency's rate onto a document that keeps its own code. The PR reads the code from the loaded document and stops parsing the form, and routes the lookup through the (also-unmerged)resolveCurrencyAndRateso a refresh can't install a missing/unusable rate. - Where documents get their initial rate: at creation the service resolves it from
the currency table with a fallback to 1 — e.g. quote insert at
apps/erp/app/modules/sales/sales.service.ts:3516-3529(exchangeRate = currency.data.exchangeRate ?? 1,exchangeRateUpdatedAt = now). When the currency is changed via Properties bulk-update, e.g.apps/erp/app/routes/x+/quote+/update.tsx:69-86: writes code + rate together when the lookup succeeds, but falls through to the generic setter and writes the code with no rate when the lookup fails (deliberate// don't breakcomment :86-87) — fixed by unmergedaec17de6ba. - Line cascade:
packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql:122-144—quoteLinePrice.exchangeRatemirrors the header via an AFTER UPDATE trigger, andconvertedUnitPrice/convertedNetExtendedPriceetc. are GENERATED columns multiplying by it. Sales invoices cascade similarly (unmerged080573aca2touches this area too).
5. Defaults — new company, no integration
currencytable: created inpackages/database/supabase/migrations/20230330024715_accounts.sql:6with"exchangeRate" NUMERIC(20,8) NOT NULL DEFAULT 1andCHECK ("exchangeRate" > 0)(:20). Group-scoped since20260228023426_company-groups.sql:325-341(companyIddropped,companyGroupIdNOT NULL,UNIQUE ("code","companyGroupId")). Read through thecurrenciesview (currency ⋈ currencyCode for the display name; latest definition20260228023426_company-groups.sql:924-928).- Seeding: the
seed-companyedge function inserts, for a new company group only, all 118 ISO currencies frompackages/database/supabase/functions/lib/seed.data.ts:483-…— every rowexchangeRate: 1(seed-company/index.ts:333-338). Subsidiaries joining an existing group reuse the group's rows. Dev/demo datasets likewise assume rate 1 (packages/database/src/datasets/bootstrap.ts:300-305inserts the same list; document tiers hardcodeexchangeRate: 1, e.g.datasets/tiers/04-sales.ts:20,05-purchasing.ts:192,246,321). - So the out-of-box state is: every currency exists, every rate is 1. A brand-new company that quotes in EUR against a USD base silently prices at parity until someone installs the integration or hand-edits the rate.
- Missing-rate fallback is
?? 1everywhere (~25+ sites):apps/erp/app/modules/sales/sales.service.ts:3525, 3657, 4045, 4425, 4549, 4638, 5333, 5447, 5826;apps/erp/app/modules/purchasing/purchasing.service.ts:1514, 1615, 2083, 2188; UI e.g.QuoteSummary.tsx:258,760,788,843,QuoteLinePricing.tsx:514,1320,1345,SalesOrderSummary.tsx:130,QuoteToOrderDrawer.tsx:424; sync corepackages/ee/src/accounting/core/document-costing.ts:90(Number(invoice?.exchangeRate) || 1); and in SQL,translateTrialBalanceCOALESCEs all three rate kinds to 1 (20260315000002_exchange-rate-history.sql:139-141). The unmerged branch commits (aec17de6ba,4931842606) exist precisely because this fallback family kept writing rate-1 documents.
6. Other places rates enter or leave the system
- Xero sync (per-document, both directions) — the only provider carrying explicit
rates. Outbound:
packages/ee/src/accounting/providers/xero/entities/invoice.ts:451pushesCurrencyRate: local.exchangeRate !== 1 ? local.exchangeRate : undefined(serialization contractxeroCurrencyRate,packages/ee/src/accounting/providers/xero/serialize.ts:25). Inbound: remote documents mapexchangeRate: remote.CurrencyRate ?? 1—entities/invoice.ts:489,entities/payment.ts:224,entities/purchase-order.ts:416. These rates land on document rows (invoice / payment / POexchangeRatecolumns), never on thecurrencytable. Unmergeddeee8198af("pass Xero's CurrencyRate through, don't invert it") fixes a direction bug here. QBO and Rillet entities have no CurrencyRate handling (grepcurrencyRateinpackages/ee/srchits only Xero files + core). - Sync journal conversion reads the document's stored rate:
packages/ee/src/accounting/core/document-costing.ts:61-90, 210-233(base = transaction × rate; divide with penny-reconciliation). - Paperless Parts import:
packages/jobs/src/inngest/functions/integrations/paperless-parts.ts:283-331— imported quotes takecurrency.data.exchangeRate ?? undefinedfrom the shared table (falls back toexchangeRate = 1when the currency row isn't found). Rates flow table→document, nothing external enters the table. exchangeRateHistoryhas NO writer in the repo. Table created in20260315000002_exchange-rate-history.sql:1-56with the comment "daily spot rates auto-populated by sync job" — but neither the current updater job nor PR #1542's version inserts into it; the only in-repo references are the readergetExchangeRateHistory(accounting.ee.service.ts:4735-4750), thetranslateTrialBalanceRPC (closing/average rates, same migration:116-141, revised in20260713225803), and the auto-generated REST swagger (packages/database/src/swagger-docs-schema.ts:36294+, i.e. writable via the public API only). Consequence: the form's history chart is empty and consolidation "Average"/"Closing" rates COALESCE down to the spot fallback → 1. Any redesign should make the new shared store the history writer.- No other rate sources found (no ECB/fixer/openexchangerates references anywhere;
exchangeratesapi.iois the only feed).
Summary for the redesign (shared on-by-default store + per-company override)
- The integration is a pure toggle with zero config — no per-company key, no
settings, already plan-whitelisted for everyone (
plan.ts:29-33). Eliminating it costs nothing config-wise; the only thing thecompanyIntegrationrow does is gate the cron job's company list. - The natural "shared store" already half-exists but is mis-keyed. Rates live on
group-scoped
currencyrows, rebased into each group's own base currency — so today's storage is per-group derived data, not a shared truth. A single platform-level rate table (e.g. EUR- or USD-based daily rates, one row per code+date) with per-group/company override rows would let the rebase happen at read time and makeexchangeRateHistory(currently writer-less) the natural platform history. - Manual edits and the feed write the same column with no precedence flag — the
nightly job clobbers manual edits; nothing marks a rate as "user-pinned". The
override capability the redesign wants needs an explicit marker (or a separate
override table), not just the current single
exchangeRatecolumn. - Fallback-to-1 is pervasive and silent (
?? 1in ~25 call sites plus SQL COALESCEs) — with rates on by default, most of these become unreachable, but the unmerged branch (resolveCurrencyAndRate, refuse-don't-default) is the intended direction and should be reconciled with/landed before or with this work. - Coordinate with the two open PRs (#1542 group-scoped job rewrite, #1530 document-sourced refresh + resolver commits on the same branches) — they rewrite exactly the files this refactor replaces; landing order matters to avoid conflicts or re-fixing.