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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: LicenseRef-Carbon-Commercial
// Carbon Enterprise file, licensed only under the Carbon Commercial License
// (packages/ee/LICENSE). Not AGPL. Running, modifying, or copying it beyond those terms requires a commercial license.
import {
allocateSalesHeaderShipping,
assertExchangeRate,
calculateSalesPostingAmounts,
distributeRoundingResidual,
EPSILON,
round,
SCALE,
toDocumentAmount
} from "@carbon/utils";
import { JournalEntrySyncError } from "./posting";
import type { Accounting } from "./types";
export type SalesDocumentComponent = {
id: string;
sourceLineId: string | null;
kind:
| "Merchandise"
| "TaxableAddOn"
| "NonTaxableAddOn"
| "LineShipping"
| "HeaderShipping";
itemId: string | null;
itemCode: string | null;
/**
* The Carbon line type this component came from, `null` for HeaderShipping.
*
* Load-bearing, not informational: `post-sales-invoice` posts NO
* `"Sales Account"` line for a `Fixed Asset` line (`sales-posting-amounts.ts`,
* `if (!isAsset)`) — its proceeds go to the disposal gain/loss accounts. A
* mapper that cannot see the line type binds the disposal to the replayed
* sales-revenue account and reports it as revenue, which is a silent
* misstatement rather than a failure. `isRevenueComponent` below is the one
* place that decides.
*/
invoiceLineType: string | null;
description: string;
quantity: number;
unitAmount: number;
netAmount: number;
taxPercent: number;
taxAmount: number;
};
/** A line whose proceeds are NOT sales revenue in Carbon's own journal. */
const NON_REVENUE_LINE_TYPES: ReadonlySet<string> = new Set(["Fixed Asset"]);
/**
* Does this component's money belong on the invoice's sales-revenue account?
*
* Shipping has its own account, and a Fixed Asset disposal has no revenue
* posting at all. Every provider mapper asks this rather than re-deriving it
* from `kind`, so the three adapters cannot drift apart on it.
*/
export function isRevenueComponent(component: SalesDocumentComponent): boolean {
if (component.kind === "LineShipping" || component.kind === "HeaderShipping")
return false;
return !(
component.invoiceLineType &&
NON_REVENUE_LINE_TYPES.has(component.invoiceLineType)
);
}
/** Does this document have any component that posts to sales revenue? */
export function hasRevenueComponent(
components: readonly SalesDocumentComponent[]
): boolean {
return components.some(isRevenueComponent);
}
/**
* Refuse a document carrying a fixed-asset disposal.
*
* Carbon books a `Fixed Asset` line against the asset's disposal gain/loss
* accounts and posts no sales revenue for it. `SalesInvoice` replays only the
* sales and shipping revenue accounts, so there is nothing on the document that
* could code a disposal correctly — and binding it to the sales-revenue account
* would report a $5,000 machine disposal as $5,000 of sales revenue, in the
* customer's ledger of record, with nothing failing.
*
* So this refuses instead, with a reason naming the lines. Rillet used to refuse
* these incidentally, because its preflight demanded an `itemId` and an asset
* line carries `assetId` instead; dropping that demand (correctly, so manual
* charge and service lines could pass) removed the accidental guard. This is the
* deliberate replacement. Replaying the disposal accounts is the real fix and
* needs its own posting-role classification.
*/
export function assertNoAssetDisposalComponents(
document: SalesDocumentComponents
): void {
const disposals = document.components.filter(
(component) =>
component.invoiceLineType &&
NON_REVENUE_LINE_TYPES.has(component.invoiceLineType)
);
if (disposals.length === 0) return;
throw new JournalEntrySyncError({
errorCode: "UNMAPPED_ACCOUNTS",
warning: true,
message:
"Cannot sync invoice: it has a fixed-asset disposal line, which Carbon posts to the asset's disposal accounts rather than to sales revenue. Asset disposals are not yet represented on the external document — invoice the disposal separately, or post it as a journal entry.",
metadata: {
invoiceId: document.invoiceId,
componentIds: disposals.map((component) => component.id),
lineIds: [
...new Set(
disposals.flatMap((component) =>
component.sourceLineId ? [component.sourceLineId] : []
)
)
]
}
});
}
export type SalesDocumentComponents = {
invoiceId: string;
currencyCode: string;
decimalPlaces: number;
components: SalesDocumentComponent[];
subtotal: number;
totalTax: number;
totalAmount: number;
balance: number;
};
function finite(value: number, name: string): number {
if (!Number.isFinite(value)) throw new Error(`${name} must be finite`);
return value;
}
/** The line discount, a fraction in [0, 1]; absent is no discount. */
function lineDiscount(line: Accounting.SalesInvoiceLine): number {
return finite(line.discountPercent ?? 0, "Line discount");
}
function precision(value: number): void {
if (!Number.isInteger(value) || value < 0 || value > SCALE) {
throw new Error("Missing or unsupported currency decimal precision");
}
}
export function buildSalesDocumentComponents(
invoice: Accounting.SalesInvoice
): SalesDocumentComponents {
const source = invoice;
if (!source.currencyCode?.trim() || !source.baseCurrencyCode?.trim()) {
throw new Error("Missing invoice currency metadata");
}
precision(source.currencyDecimalPlaces);
precision(source.baseCurrencyDecimalPlaces);
const decimals = source.currencyDecimalPlaces;
const rate = source.exchangeRate;
assertExchangeRate(rate);
if (source.currencyCode === source.baseCurrencyCode && rate !== 1) {
throw new Error("Base-currency invoices require an identity exchange rate");
}
const header = finite(source.headerShippingCost, "Header shipping");
const lines = source.lines.filter(
(line) => line.invoiceLineType !== "Comment"
);
if (new Set(lines.map((line) => line.id)).size !== lines.length) {
throw new Error("Invoice source line IDs must be unique");
}
const headerAllocations = allocateSalesHeaderShipping(lines, header);
const baseAmounts = lines.map((line) =>
calculateSalesPostingAmounts({
...line,
allocatedHeaderShipping: headerAllocations.get(line.id) ?? 0
})
);
const sourceEnvelope = (lines.length * 4 + 1) / (2 * 10 ** SCALE) + EPSILON;
const reconcileSource = (actual: number, expected: number, label: string) => {
finite(actual, label);
finite(expected, label);
if (Math.abs(actual - expected) > sourceEnvelope) {
throw new Error(
`Invoice ${label} does not reconcile with its source components`
);
}
};
reconcileSource(
baseAmounts.reduce(
(sum, value) => sum + value.salesRevenueBase + value.shippingRevenueBase,
0
),
source.subtotal + header,
"subtotal"
);
reconcileSource(
baseAmounts.reduce((sum, value) => sum + value.salesTaxBase, 0),
source.totalTax,
"tax total"
);
reconcileSource(
source.subtotal + header + source.totalTax,
source.totalAmount,
"gross total"
);
type RawComponent = {
component: SalesDocumentComponent;
baseNet: number;
baseTax: number;
};
const raw: RawComponent[] = [];
const push = (args: {
kind: SalesDocumentComponent["kind"];
line?: Accounting.SalesInvoiceLine;
baseNet: number;
taxPercent: number;
description: string;
quantity?: number;
}) => {
const { line, kind, baseNet, taxPercent } = args;
const baseTax = finite(baseNet * taxPercent, "Component tax");
if (baseNet === 0 && baseTax === 0) return;
const quantity = args.quantity ?? 1;
let unitAmount = toDocumentAmount(baseNet, rate, decimals);
if (kind === "Merchandise" && line) {
// Both prices are LIST prices; the provider is sent the NET unit price,
// so the line discount is applied to each before they are compared. A
// discounted price is a rate, so it is rounded to storage scale; an
// undiscounted mirror is sent exactly as stored.
const discount = lineDiscount(line);
const net = 1 - discount;
const netUnit = line.unitPrice * net;
const convertedUnit =
line.convertedUnitPrice == null ? null : line.convertedUnitPrice * net;
const expectedUnit = finite(netUnit * rate, "Document unit price");
if (
convertedUnit != null &&
Math.abs(
finite(convertedUnit, "Document price mirror") - expectedUnit
) >
1 / (2 * 10 ** SCALE) + EPSILON
) {
throw new Error(
`Invoice line ${line.id} price mirror contradicts its exchange rate`
);
}
unitAmount =
convertedUnit == null
? toDocumentAmount(netUnit, rate, SCALE)
: discount
? round(convertedUnit)
: convertedUnit;
}
raw.push({
baseNet,
baseTax,
component: {
id: `${line?.id ?? source.id}:${kind}`,
sourceLineId: line?.id ?? null,
kind,
itemId: line?.itemId ?? null,
itemCode: line?.itemCode ?? null,
invoiceLineType: line?.invoiceLineType ?? null,
description: args.description,
quantity,
unitAmount,
netAmount: toDocumentAmount(baseNet, rate, decimals),
taxPercent,
taxAmount: toDocumentAmount(baseTax, rate, decimals)
}
});
};
for (const line of lines) {
push({
kind: "Merchandise",
line,
// The same expression `calculateSalesPostingAmounts` and the
// `salesInvoices` view use, so the reconciliation above holds.
baseNet: line.quantity * line.unitPrice * (1 - lineDiscount(line)),
taxPercent: line.taxPercent,
quantity: line.quantity,
description: line.description ?? line.itemCode ?? "Invoice line"
});
push({
kind: "TaxableAddOn",
line,
baseNet: line.addOnCost ?? 0,
taxPercent: line.taxPercent,
description: `Add-on — ${line.description ?? line.itemCode ?? "Invoice line"}`
});
push({
kind: "NonTaxableAddOn",
line,
baseNet: line.nonTaxableAddOnCost ?? 0,
taxPercent: 0,
description: `Non-taxable add-on — ${line.description ?? line.itemCode ?? "Invoice line"}`
});
push({
kind: "LineShipping",
line,
baseNet: line.shippingCost ?? 0,
taxPercent: line.taxPercent,
description: `Shipping — ${line.description ?? line.itemCode ?? "Invoice line"}`
});
}
push({
kind: "HeaderShipping",
baseNet: header,
taxPercent: 0,
description: "Shipping"
});
const totalAmount = toDocumentAmount(source.totalAmount, rate, decimals);
const totalTax = toDocumentAmount(source.totalTax, rate, decimals);
// The rounded authoritative gross and native tax define the document net.
// Any one-unit net difference is allocated to an existing component below.
const subtotal = round(totalAmount - totalTax, decimals);
// Rounding each component independently leaves a residual of up to half a
// minor unit per component. Spread it one unit at a time (largest remainder)
// rather than concentrating it: a component that absorbs the whole residual
// stops matching its own taxPercent, and providers that re-derive
// `tax = net × percent` reject the document outright.
const reconcileDocument = (
key: "netAmount" | "taxAmount",
baseKey: "baseNet" | "baseTax",
target: number
) => {
// A component with no basis on this axis must stay at zero — a zero-rated
// line carrying tax is itself a provider refusal. Order by component id so
// an exact tie resolves the same way whatever order the lines arrived in;
// the distributor's own tie-break is positional.
const participants = raw
.filter((row) => row[baseKey] !== 0)
.sort((a, b) => a.component.id.localeCompare(b.component.id));
if (participants.length === 0) {
if (round(target, decimals) !== 0) {
throw new Error(
`Invoice ${key} has no source component for its rounding residual`
);
}
return;
}
const allocated = distributeRoundingResidual(
participants.map((row) =>
finite(row[baseKey] * rate, `Component document ${key}`)
),
target,
decimals
);
participants.forEach((row, index) => {
row.component[key] = allocated[index]!;
});
};
reconcileDocument("netAmount", "baseNet", subtotal);
reconcileDocument("taxAmount", "baseTax", totalTax);
for (const { component } of raw) {
// Providers recompute the extended net from quantity × unit price, so the
// unit price has to reproduce the reconciled net. The stored
// `convertedUnitPrice` mirror and the converted extension are rounded on two
// independent float paths and can straddle a half-unit tie, so derive rather
// than refuse. A unit price is a rate: it keeps storage scale, not
// settlement decimals.
if (
round(component.quantity * component.unitAmount, decimals) !==
component.netAmount
) {
component.unitAmount = round(
finite(
component.netAmount / component.quantity,
"Reconciled document unit price"
),
SCALE
);
// Deriving cannot always converge: past roughly 10^SCALE units, one step
// of the unit price moves the extension by more than a minor unit, so no
// representable price reproduces the net. That is a real contradiction
// between the stored quantity and the document total — refuse it.
if (
round(component.quantity * component.unitAmount, decimals) !==
component.netAmount
) {
throw new Error(
`Invoice component ${component.id} unit price does not reconcile with its document net`
);
}
}
}
return {
invoiceId: source.id,
currencyCode: source.currencyCode,
decimalPlaces: decimals,
components: raw
.map((row) => row.component)
.filter((line) => line.netAmount !== 0 || line.taxAmount !== 0),
subtotal,
totalTax,
totalAmount,
balance: toDocumentAmount(source.balance, rate, decimals)
};
}