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
fix(erp): show BoM explorer quantities at two decimals (#1256)
Chase Foster8cf4a257ec15
diff --git a/apps/erp/app/components/BoMExplorer/BoMExplorer.tsx b/apps/erp/app/components/BoMExplorer/BoMExplorer.tsxindex 04e594d87a..46c31d831b 100644--- a/apps/erp/app/components/BoMExplorer/BoMExplorer.tsx+++ b/apps/erp/app/components/BoMExplorer/BoMExplorer.tsx@@ -30,7 +30,7 @@ import {LuTable} from "react-icons/lu";import { Link, useNavigate, useSearchParams } from "react-router";-import { useOptimisticLocation } from "~/hooks";+import { useOptimisticLocation, useQuantityFormatter } from "~/hooks";import { useIntegrations } from "~/hooks/useIntegrations";import { getLinkToItemDetails } from "~/modules/items/ui/Item/ItemForm";import { generateBomIds } from "~/utils/bom";@@ -534,16 +534,19 @@ function BoMNodeText({ node }: { node: BoMNode }) {function BoMNodeData({ node }: { node: BoMNode }) {const integrations = useIntegrations();const onShapeState = getOnshapeState(node, integrations.has("onshape"));+ // Display only — the exact quantity is unchanged everywhere else, and the+ // node's preview card still shows it at full precision.+ const formatQuantity = useQuantityFormatter();return (<HStack spacing={1}>- <Badge className="text-xs" variant="outline">+ <Badge className="text-xs tabular-nums" variant="outline"><MethodIcontype={node.data.methodType}isKit={node.data.kit ?? undefined}className="mr-2"/>- {node.data.quantity}+ {formatQuantity(node.data.quantity)}</Badge>{onShapeState && <OnshapeStatus status={onShapeState} />}</HStack>diff --git a/apps/erp/app/hooks/index.ts b/apps/erp/app/hooks/index.tsindex 0c37d70c34..a2b5461766 100644--- a/apps/erp/app/hooks/index.ts+++ b/apps/erp/app/hooks/index.ts@@ -20,6 +20,7 @@ import { useOnboarding } from "./useOnboarding";import { usePercentFormatter } from "./usePercentFormatter";import { usePermissions } from "./usePermissions";import { usePlanGate } from "./usePlanGate";+import { useQuantityFormatter } from "./useQuantityFormatter";import { useRealtime } from "./useRealtime";import { useScrollPosition } from "./useScrollPosition";import { useScrollToHash } from "./useScrollToHash";@@ -49,6 +50,7 @@ export {usePermissions,usePlanGate,usePrinting,+ useQuantityFormatter,useRealtime,useRouteData,useScrollPosition,diff --git a/apps/erp/app/hooks/useQuantityFormatter.test.ts b/apps/erp/app/hooks/useQuantityFormatter.test.tsnew file mode 100644index 0000000000..a6131e2a13--- /dev/null+++ b/apps/erp/app/hooks/useQuantityFormatter.test.ts@@ -0,0 +1,75 @@+import { describe, expect, it } from "vitest";+import { formatQuantityForDisplay } from "./useQuantityFormatter";++const formatter = new Intl.NumberFormat("en-US", {+ minimumFractionDigits: 0,+ maximumFractionDigits: 2+});++const format = (quantity: number) =>+ formatQuantityForDisplay(quantity, formatter);++describe("formatQuantityForDisplay", () => {+ it("rounds to two decimals", () => {+ expect(format(3.232227)).toBe("3.23");+ expect(format(1.983918)).toBe("1.98");+ expect(format(0.02008193)).toBe("0.02");+ expect(format(1.005)).toBe("1.01");+ });++ it("keeps whole numbers whole", () => {+ expect(format(1)).toBe("1");+ expect(format(3)).toBe("3");+ expect(format(1000)).toBe("1,000");+ expect(format(3.0)).toBe("3");+ });++ it("drops a trailing zero on a single-decimal quantity", () => {+ expect(format(1.5)).toBe("1.5");+ expect(format(2.1)).toBe("2.1");+ });++ it("never renders a non-zero quantity as zero", () => {+ expect(format(0.004)).toBe("<0.01");+ expect(format(0.000001)).toBe("<0.01");+ expect(format(0.00999)).toBe("<0.01");+ });++ it("renders a genuine zero as zero", () => {+ expect(format(0)).toBe("0");+ });++ it("keeps the boundary value exact", () => {+ expect(format(0.01)).toBe("0.01");+ });++ it("handles small negative quantities symmetrically", () => {+ expect(format(-0.004)).toBe(">-0.01");+ expect(format(-2.567)).toBe("-2.57");+ });++ it("returns an empty string for non-finite values", () => {+ expect(format(Number.NaN)).toBe("");+ expect(format(Number.POSITIVE_INFINITY)).toBe("");+ });++ it("respects the locale of the formatter it is given", () => {+ const de = new Intl.NumberFormat("de-DE", {+ minimumFractionDigits: 0,+ maximumFractionDigits: 2+ });+ expect(formatQuantityForDisplay(3.232227, de)).toBe("3,23");+ expect(formatQuantityForDisplay(0.004, de)).toBe("<0,01");+ });++ it("uses the locale's own negative sign below the threshold", () => {+ // sv-SE renders negatives with U+2212 MINUS SIGN, not an ASCII hyphen.+ const sv = new Intl.NumberFormat("sv-SE", {+ minimumFractionDigits: 0,+ maximumFractionDigits: 2+ });+ expect(formatQuantityForDisplay(-0.004, sv)).toBe(`>${sv.format(-0.01)}`);+ expect(formatQuantityForDisplay(-0.004, sv)).toBe(">−0,01");+ expect(formatQuantityForDisplay(0.004, sv)).toBe("<0,01");+ });+});diff --git a/apps/erp/app/hooks/useQuantityFormatter.tsx b/apps/erp/app/hooks/useQuantityFormatter.tsxnew file mode 100644index 0000000000..7f5ea5fd43--- /dev/null+++ b/apps/erp/app/hooks/useQuantityFormatter.tsx@@ -0,0 +1,44 @@+import { useLocale } from "@react-aria/i18n";+import { useMemo } from "react";++const MAXIMUM_FRACTION_DIGITS = 2;+const SMALLEST_DISPLAYABLE_QUANTITY = 0.01;++/**+ * Formats a quantity for display at two decimals. This is presentation only —+ * the underlying quantity is never rounded, so BOMs, costing, reports, exports+ * and the API keep the exact value.+ *+ * - Whole numbers stay whole: `3` renders as "3", not "3.00".+ * - A non-zero quantity too small to render at two decimals shows as "<0.01"+ * rather than a misleading "0".+ */+export function formatQuantityForDisplay(+ quantity: number,+ formatter: Intl.NumberFormat+) {+ if (!Number.isFinite(quantity)) return "";++ if (quantity !== 0 && Math.abs(quantity) < SMALLEST_DISPLAYABLE_QUANTITY) {+ // Format the signed threshold rather than prepending "-": locales differ on+ // the negative sign (sv/fi/nb use U+2212, ar/fa add a directional mark).+ return quantity > 0+ ? `<${formatter.format(SMALLEST_DISPLAYABLE_QUANTITY)}`+ : `>${formatter.format(-SMALLEST_DISPLAYABLE_QUANTITY)}`;+ }++ return formatter.format(quantity);+}++export function useQuantityFormatter() {+ const { locale } = useLocale();++ return useMemo(() => {+ const formatter = new Intl.NumberFormat(locale, {+ minimumFractionDigits: 0,+ maximumFractionDigits: MAXIMUM_FRACTION_DIGITS+ });++ return (quantity: number) => formatQuantityForDisplay(quantity, formatter);+ }, [locale]);+}