Skip to content

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.

fix(accounting): push AR invoice amounts in the currency the payload declares (#1547)

BettrCallRaul0af80d271b5a
diff --git a/packages/ee/src/accounting/core/models.ts b/packages/ee/src/accounting/core/models.ts
index 2b820d1500..4474080b92 100644
--- a/packages/ee/src/accounting/core/models.ts
+++ b/packages/ee/src/accounting/core/models.ts
@@ -960,7 +960,12 @@ export const SalesInvoiceLineSchema = z.object({
itemCode: withNullable(z.string()), // readableIdWithRevision
description: withNullable(z.string()),
quantity: z.number(),
+ // BASE currency, matching the stored column.
unitPrice: z.number(),
+ // The document-currency mirror (unitPrice * exchangeRate). Optional because
+ // not every provider selects it; push this to any payload that declares a
+ // currency code, since unitPrice above is base.
+ convertedUnitPrice: withNullable(z.number()).optional(),
taxPercent: z.number(),
lineAmount: z.number()
});
diff --git a/packages/ee/src/accounting/providers/rillet/entities/invoice.ts b/packages/ee/src/accounting/providers/rillet/entities/invoice.ts
index cf9631f8f9..8f9778cf14 100644
--- a/packages/ee/src/accounting/providers/rillet/entities/invoice.ts
+++ b/packages/ee/src/accounting/providers/rillet/entities/invoice.ts
@@ -81,7 +81,10 @@ type InvoiceLineRow = {
itemId: string | null;
description: string | null;
quantity: number;
+ /** BASE currency, as stored. */
unitPrice: number;
+ /** Document-currency mirror (unitPrice * exchangeRate). */
+ convertedUnitPrice: number | null;
taxPercent: number;
itemReadableIdWithRevision: string | null;
};
@@ -133,7 +136,13 @@ export function mapSalesInvoiceToRilletInvoice(args: {
product_id: productId,
description: line.description ?? line.itemCode ?? "Invoice line",
quantity: line.quantity,
- total_amount: toRilletMoney(line.quantity * line.unitPrice, currency),
+ // `currency` is the DOCUMENT currency, so the amount must be too.
+ // salesInvoiceLine.unitPrice is stored in the company BASE currency;
+ // convertedUnitPrice is the document-currency mirror.
+ total_amount: toRilletMoney(
+ line.quantity * (line.convertedUnitPrice ?? line.unitPrice),
+ currency
+ ),
// CUSTOMER_CUSTOM satisfies rev-rec validation and carries the line id
// for audit. NO `carbon` ref here: Rillet counts header + item
// references together for RESTRICTED types, and organizations that
@@ -244,6 +253,7 @@ export class RilletSalesInvoiceSyncer extends RilletTransactionSyncer<
"salesInvoiceLine.description",
"salesInvoiceLine.quantity",
"salesInvoiceLine.unitPrice",
+ "salesInvoiceLine.convertedUnitPrice",
"salesInvoiceLine.taxPercent",
"item.readableIdWithRevision as itemReadableIdWithRevision"
])
@@ -286,6 +296,11 @@ export class RilletSalesInvoiceSyncer extends RilletTransactionSyncer<
lines: lines.map((line) => {
const quantity = Number(line.quantity) || 0;
const unitPrice = Number(line.unitPrice) || 0;
+ const convertedUnitPrice =
+ line.convertedUnitPrice === null ||
+ line.convertedUnitPrice === undefined
+ ? null
+ : Number(line.convertedUnitPrice);
return {
id: line.id,
invoiceLineType: line.invoiceLineType,
@@ -294,6 +309,7 @@ export class RilletSalesInvoiceSyncer extends RilletTransactionSyncer<
description: line.description,
quantity,
unitPrice,
+ convertedUnitPrice,
taxPercent: Number(line.taxPercent) || 0,
lineAmount: quantity * unitPrice
};
diff --git a/packages/ee/src/accounting/providers/xero/entities/__tests__/invoice.test.ts b/packages/ee/src/accounting/providers/xero/entities/__tests__/invoice.test.ts
index d24c8e1175..63f4bf76f7 100644
--- a/packages/ee/src/accounting/providers/xero/entities/__tests__/invoice.test.ts
+++ b/packages/ee/src/accounting/providers/xero/entities/__tests__/invoice.test.ts
@@ -154,3 +154,81 @@ describe("SalesInvoiceSyncer.mapToRemote (item revenue account)", () => {
});
});
});
+
+/**
+ * Foreign-currency AR. `salesInvoiceLine.unitPrice` is stored in the company
+ * BASE currency (`convertedUnitPrice` is the document mirror), so a payload
+ * declaring `CurrencyCode: "EUR"` must carry EUR amounts, not the base ones.
+ *
+ * Xero's `CurrencyRate` runs the SAME direction as Carbon's
+ * `currency.exchangeRate`: foreign per base. Xero's multicurrency guide is
+ * explicit — "The units of CurrencyRate are always [Foreign Currency] PER
+ * [Base Currency] ... A CurrencyRate of 1.10 for a EUR invoice against a
+ * GBP-base-currency organisation says that 1 GBP = 1.1 EUR." So the rate is
+ * passed through unchanged; inverting it triggers Xero's "inverse rate"
+ * warning and books base amounts wrong.
+ */
+const fxInvoice = (): Accounting.SalesInvoice =>
+ ({
+ ...invoice(),
+ currencyCode: "EUR",
+ // 0.80 EUR per 1 USD of base -- Xero's units exactly
+ exchangeRate: 0.8,
+ subtotal: 100,
+ totalAmount: 100,
+ balance: 100,
+ lines: [
+ {
+ id: "sil-1",
+ invoiceLineType: "Part",
+ itemId: "item-1",
+ itemCode: "WIDGET-1",
+ description: "Widget",
+ quantity: 2,
+ // base currency: 2 x 50 = 100 base, i.e. 80 EUR
+ unitPrice: 50,
+ convertedUnitPrice: 40,
+ taxPercent: 0,
+ lineAmount: 100
+ }
+ ]
+ }) as unknown as Accounting.SalesInvoice;
+
+describe("SalesInvoiceSyncer.mapToRemote (foreign currency)", () => {
+ const db = () =>
+ makeInvoiceDb({
+ accountDefault: { salesAccount: "acct_sales" },
+ accountMappings: [
+ {
+ id: "m-1",
+ accountId: "acct_sales",
+ externalId: "sales-remote",
+ metadata: { externalCode: "4000" },
+ lastSyncedAt: null,
+ accountNumber: "4000",
+ accountName: "Sales Revenue"
+ }
+ ]
+ });
+
+ it("pushes line amounts in the currency the payload declares", async () => {
+ const payload = await makeInvoiceSyncer(db()).mapToRemote(fxInvoice());
+ expect(payload.CurrencyCode).toBe("EUR");
+ // 40 EUR/ea, not the 50 base
+ expect(payload.LineItems[0]?.UnitAmount).toBe(40);
+ expect(payload.LineItems[0]?.LineAmount).toBe(80);
+ });
+
+ it("passes CurrencyRate through unchanged (foreign per base, both sides)", async () => {
+ const payload = await makeInvoiceSyncer(db()).mapToRemote(fxInvoice());
+ // Xero wants EUR per USD, which is what Carbon already stores. Sending the
+ // reciprocal (1.25) is the documented "inverse rate" mistake.
+ expect(payload.CurrencyRate).toBeCloseTo(0.8, 6);
+ });
+
+ it("omits CurrencyRate on a base-currency invoice rather than sending 1", async () => {
+ const payload = await makeInvoiceSyncer(db()).mapToRemote(invoice());
+ // Xero: "Setting a CurrencyRate of 1 is redundant and considered incorrect."
+ expect(payload.CurrencyRate).toBeUndefined();
+ });
+});
diff --git a/packages/ee/src/accounting/providers/xero/entities/invoice.ts b/packages/ee/src/accounting/providers/xero/entities/invoice.ts
index f66d77e0b4..365e4f8524 100644
--- a/packages/ee/src/accounting/providers/xero/entities/invoice.ts
+++ b/packages/ee/src/accounting/providers/xero/entities/invoice.ts
@@ -54,7 +54,10 @@ type InvoiceLineRow = {
itemId: string | null;
description: string | null;
quantity: number;
+ /** BASE currency, as stored. */
unitPrice: number;
+ /** Document-currency mirror (unitPrice * exchangeRate). */
+ convertedUnitPrice: number | null;
taxPercent: number;
// For item code lookup
itemReadableIdWithRevision: string | null;
@@ -264,6 +267,7 @@ export class SalesInvoiceSyncer extends BaseEntitySyncer<
"salesInvoiceLine.description",
"salesInvoiceLine.quantity",
"salesInvoiceLine.unitPrice",
+ "salesInvoiceLine.convertedUnitPrice",
"salesInvoiceLine.taxPercent",
"item.readableIdWithRevision as itemReadableIdWithRevision"
])
@@ -309,6 +313,11 @@ export class SalesInvoiceSyncer extends BaseEntitySyncer<
const quantity = Number(line.quantity) || 0;
const unitPrice = Number(line.unitPrice) || 0;
const taxPercent = Number(line.taxPercent) || 0;
+ const convertedUnitPrice =
+ line.convertedUnitPrice === null ||
+ line.convertedUnitPrice === undefined
+ ? null
+ : Number(line.convertedUnitPrice);
return {
id: line.id,
invoiceLineType: line.invoiceLineType,
@@ -317,6 +326,7 @@ export class SalesInvoiceSyncer extends BaseEntitySyncer<
description: line.description,
quantity,
unitPrice,
+ convertedUnitPrice,
taxPercent,
lineAmount: quantity * unitPrice
};
@@ -388,15 +398,18 @@ export class SalesInvoiceSyncer extends BaseEntitySyncer<
// Build line items, resolving item dependencies
const lineItems: Xero.InvoiceLineItem[] = [];
for (const line of local.lines) {
- const taxAmount =
- (line.quantity * line.unitPrice * line.taxPercent) / 100;
+ // The payload declares CurrencyCode below, so every amount on it must be
+ // in THAT currency. salesInvoiceLine.unitPrice is stored in the company
+ // BASE currency; convertedUnitPrice is the document-currency mirror.
+ const unitAmount = line.convertedUnitPrice ?? line.unitPrice;
+ const taxAmount = (line.quantity * unitAmount * line.taxPercent) / 100;
const lineItem: Xero.InvoiceLineItem = {
Description: line.description ?? undefined,
Quantity: line.quantity,
- UnitAmount: line.unitPrice,
+ UnitAmount: unitAmount,
TaxAmount: taxAmount,
- LineAmount: line.quantity * line.unitPrice,
+ LineAmount: line.quantity * unitAmount,
// The item's mapped revenue account (item-referenced AR).
AccountCode: salesAccountCode,
// TaxType is required by Xero: OUTPUT for sales tax, NONE for zero tax
@@ -448,6 +461,12 @@ export class SalesInvoiceSyncer extends BaseEntitySyncer<
AmountDue: local.balance,
AmountPaid: local.totalAmount - local.balance,
CurrencyCode: local.currencyCode,
+ // Xero's CurrencyRate is [invoice currency] PER [base currency] -- the
+ // same direction Carbon stores exchangeRate in, so it passes through
+ // unchanged. Inverting it earns Xero's "inverse rate" warning and books
+ // the base amounts wrong. Omit it on base-currency invoices: Xero calls
+ // an explicit rate of 1 redundant and warns on a 1 that reaches an FX
+ // document.
CurrencyRate: local.exchangeRate !== 1 ? local.exchangeRate : undefined
};
}