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.

7.4 KB
Raw Blame

paths: - "apps/erp/app/modules/purchasing/**" - "apps/erp/app/components/Form/ConversionFactor.tsx" - "apps/erp/app/modules/shared/shared.service.ts" - "apps/erp/app/modules/purchasing/purchasing.service.ts" - "packages/database/src/supplier-part-price.ts" - "packages/database/supabase/migrations/conversion.sql"

Purchasing Conversion Factors

Lets an item be purchased in one unit of measure and stocked in another. The conversionFactor is the number of inventory units per one purchase unit:

inventoryQuantity = purchaseQuantity * conversionFactor
inventoryUnitCost = purchaseUnitPrice  / conversionFactor

When purchase UoM == inventory UoM the factor is 1 (the UI disables the input).

Where the factor is stored (schema)

The physical column is named conversionFactor on every table below (the column is not abbreviated; the planning views use a separate ln alias internally, not the base column). Verify against the newest migration before relying on a default.

Table Column Type / default Migration
purchaseOrderLine conversionFactor bare NUMERIC DEFAULT 1 widened by 20260811123616_widen-purchasing-scale.sql (was NUMERIC(10,2))
purchaseInvoiceLine conversionFactor bare NUMERIC DEFAULT 1 widened by 20260811123616_widen-purchasing-scale.sql (was NUMERIC(10,2))
receiptLine conversionFactor bare NUMERIC DEFAULT 1 widened by 20260811123616_widen-purchasing-scale.sql (was NUMERIC(10,2))
supplierQuoteLine conversionFactor NUMERIC(10,5) DEFAULT 1, NOT NULL 20241202192419_supplier-quotes.sql
supplierPart (was buyMethod) conversionFactor NUMERIC(15,5) DEFAULT 1, NOT NULL 20230330024716_parts.sql

supplierPart is the source default (buyMethod was renamed in 20241202145357_rename-buyMethod-to-supplierPart.sql). It also has supplierUnitOfMeasureCode — it does not split into purchase/inventory UoM columns. The line tables (purchaseOrderLine, supplierQuoteLine, etc.) carry both inventoryUnitOfMeasureCode and purchaseUnitOfMeasureCode (FKs to unitOfMeasure). receiptLine stores only a denormalized unitOfMeasure text.

Quote/sales-order lines (quoteLine, salesOrderLine) have no conversionFactor — they only have a single unitOfMeasureCode.

Where it's applied

Auto-load on item select. Forms hydrate conversionFactor from the supplier part, with a fallback chain:

  • apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx: supplierPart?.data?.conversionFactor ?? itemReplenishment?.conversionFactor ?? 1
  • SupplierQuoteLineForm.tsx: supplierPart.data?.conversionFactor ?? 1

Validators (apps/erp/app/modules/purchasing/purchasing.models.ts, apps/erp/app/modules/items/items.models.ts): the field is conversionFactor: zfd.numeric(z.number().optional()) on the purchasing line validators, and zfd.numeric(z.number().min(0)) on supplierPartValidator. (>0 is not enforced in zod; supplierPart allows 0.)

Receipt / inventory posting — the real conversion happens here. When a PO is received, the create posting converts purchase quantities to inventory quantities and unit cost to inventory unit cost (createDocument, apps/erp/app/modules/shared/shared.service.ts):

orderQuantity:    d.purchaseQuantity * (d.conversionFactor ?? 1),
receivedQuantity: outstandingQuantity * (d.conversionFactor ?? 1),
unitPrice:        unitPrice / (d.conversionFactor ?? 1) + shippingAndTaxUnitCost,
unitOfMeasure:    d.inventoryUnitOfMeasureCode ?? "EA",

shippingAndTaxUnitCost is also spread over inventory units ((taxAmount + shippingCost) / (purchaseQuantity * conversionFactor)). The convert (quote→PO) and update-purchased-prices functions carry the factor forward likewise (update-purchased-prices uses the planning ln alias).

Reverse direction (planning → purchase qty). MRP/planning has an inventory requirement and computes the purchase quantity, rounding up: round(inventoryQuantity / conversionFactor, 0, RoundingMode.Up) (when factor

  1. — see PurchasingPlanningOrderDrawer.tsx and production/ui/Planning/planned-orders-from-actions.ts.

Display-only conversions multiply purchase qty by the factor to show derived inventory qty (e.g. PurchaseOrderSummary.tsx, SupplierQuoteSummary.tsx) and divide price by it for inventory unit price (SupplierQuoteLinePricing.tsx).

Gotchas

  • The generated price columns (unitPrice, extendedPrice, etc.) on purchaseOrderLine / supplierQuoteLinePrice are GENERATED ALWAYS and only divide by exchangeRate — they do NOT divide by conversionFactor (20250807094441_fix-purchasing-conversion-factor.sql). The conversion-factor division is applied in app/posting code, not in those DB columns.
  • Exchange-rate direction: supplierUnitPrice / exchangeRate (supplier→base currency), guarded against divide-by-zero.
  • The line tables used to clamp the factor to 2 dp, so 10.764 silently became 10.76 once it landed on a PO/invoice/receipt line. 20260811123616_widen-purchasing-scale.sql retyped them to bare NUMERIC (along with supplierPart.unitPrice, which was clamped to 2 dp and rounded distributor prices like 0.164 on entry), so the factor is now stored exactly as entered. See .claude/rules/numeric-precision.md.

Supplier part prices: per purchase unit, in the part's currency

supplierPart.supplierUnitPrice and supplierPartPrice.supplierUnitPrice (renamed from unitPrice in 20261009181437_supplier-part-currency.sql) hold the price as the supplier quoted it: per PURCHASE unit, in supplierPart.currencyCode (NULL = the company base currency; the migration backfilled base). Break quantities are purchase quantities. Nothing stores a converted copy — every reader converts at the CURRENT rate through @carbon/database/supplier-part-price:

  • Onto a purchase document (PO/invoice line forms, planning orders, kanban, replacement PO, return lines): convertUnitPrice(price, partCurrency, documentCurrency) — identity when the codes match, else through base at the part currency's get_exchange_rate and the document's stamped rate. ERP forms load getSupplierPartPricing and call resolveSupplierPrice.
  • Into quote costing (base per INVENTORY unit): buildSupplierPriceMap, used by getSupplierPriceBreaksForItems (ERP) and its methods.ts mirror; a part whose currency has no rate is left out, never costed at par.
  • Writers that learn a price (finalize-supplier-quote, convert supplierQuoteToPurchaseOrder, update-purchased-prices) record the document's supplierUnitPrice per purchase unit. A part they create takes the document's currency; an existing part keeps its own and the price is converted into it (getSupplierPartPriceConverter, shared.service.ts).

Before this, convert and finalize stored base per INVENTORY unit while every reader assumed per purchase unit; the migration repaired those rows.

UI component

apps/erp/app/components/Form/ConversionFactor.tsx — modal input showing both directions ("There are N {inventory} in one {purchase}" and the inverse 1 / conversionFactor). Disabled when inventoryCode === purchasingCode. Props: name, label?, inventoryCode, purchasingCode, value, onChange, isReadOnly, isRequired. Used by SupplierPartForm, PO and supplier-quote line forms.