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(purchasing): convert supplier part prices to the supplier's currency, not away from it (#1543)
BettrCallRaul48b967eab30c
diff --git a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsxindex e3c16ec3f3..7b575c9bee 100644--- a/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx+++ b/apps/erp/app/modules/invoicing/ui/PurchaseInvoice/PurchaseInvoiceLineForm.tsx@@ -149,7 +149,11 @@ const PurchaseInvoiceLineForm = ({taxAmount: initialValues.supplierTaxAmount ?? 0,taxPercent: initialValues.taxPercent ?? 0,priceBreaks: [],- fallbackUnitPrice: initialValues.supplierUnitPrice ?? 0+ // fallbackUnitPrice is BASE currency, matching what resolveSupplierPrice+ // expects. initialValues.supplierUnitPrice is the SUPPLIER's, so divide.+ fallbackUnitPrice:+ (initialValues.supplierUnitPrice ?? 0) /+ (routeData?.purchaseInvoice?.exchangeRate || 1)});// Re-derive the tax amount when the line's base changes — never on mount, so@@ -355,10 +359,12 @@ const PurchaseInvoiceLineForm = ({const itemReplenishment = item?.data?.itemReplenishment;const exchangeRate = routeData?.purchaseInvoice?.exchangeRate ?? 1;const initialQty = supplierPart?.data?.minimumOrderQuantity ?? 1;+ // BASE currency: supplierPart.unitPrice and itemCost.unitCost are both+ // stored in base. resolveSupplierPrice converts to the supplier's.const baseFallback =supplierPart?.data?.unitPrice !== null &&supplierPart?.data?.unitPrice !== undefined- ? supplierPart.data.unitPrice / exchangeRate+ ? supplierPart.data.unitPrice: (itemCost?.unitCost ?? 0);const breaks = supplierPart?.data?.iddiff --git a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsxindex ecb1165ee4..18a057d3f4 100644--- a/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx+++ b/apps/erp/app/modules/purchasing/ui/PurchaseOrder/PurchaseOrderLineForm.tsx@@ -150,7 +150,11 @@ const PurchaseOrderLineForm = ({itemId: initialValues.itemId ?? "",conversionFactor: initialValues.conversionFactor ?? 1,description: initialValues.description ?? "",- fallbackUnitPrice: initialValues.supplierUnitPrice ?? 0,+ // fallbackUnitPrice is BASE currency, matching what resolveSupplierPrice+ // expects. initialValues.supplierUnitPrice is the SUPPLIER's, so divide.+ fallbackUnitPrice:+ (initialValues.supplierUnitPrice ?? 0) /+ (routeData?.purchaseOrder?.exchangeRate || 1),inventoryUom: initialValues.inventoryUnitOfMeasureCode ?? "",minimumOrderQuantity: undefined,purchaseQuantity: initialValues.purchaseQuantity ?? 1,@@ -378,10 +382,12 @@ const PurchaseOrderLineForm = ({const exchangeRate = routeData?.purchaseOrder?.exchangeRate ?? 1;const initialQty = supplierPart?.data?.minimumOrderQuantity ?? 1;const leadTime = item?.data?.itemReplenishment?.leadTime ?? 0;+ // BASE currency: supplierPart.unitPrice and itemCost.unitCost are both+ // stored in base. resolveSupplierPrice converts to the supplier's.const baseFallback =supplierPart?.data?.unitPrice !== null &&supplierPart?.data?.unitPrice !== undefined- ? supplierPart.data.unitPrice / exchangeRate+ ? supplierPart.data.unitPrice: (itemCost?.unitCost ?? 0);const breaks = supplierPart?.data?.iddiff --git a/apps/erp/app/modules/shared/shared.service.ts b/apps/erp/app/modules/shared/shared.service.tsindex 08ee7acef6..56f84ab50f 100644--- a/apps/erp/app/modules/shared/shared.service.ts+++ b/apps/erp/app/modules/shared/shared.service.ts@@ -1428,8 +1428,16 @@ export function resolveBuyUnitCost(}/**- * Resolve the best supplier unit price for a quantity, applying exchange- * rate conversion.+ * Resolve the supplier unit price for a quantity.+ *+ * `supplierPart.unitPrice` and `supplierPartPrice.unitPrice` are stored in the+ * company BASE currency -- neither table has a currency column, and all three+ * writers put base there. The field this feeds (`supplierUnitPrice`) is in the+ * SUPPLIER's currency. The document's `exchangeRate` is foreign-per-base, so+ * base to supplier is MULTIPLY.+ *+ * @param fallbackUnitPrice base currency, used when no break matches+ * @returns the price in the supplier's currency*/export function resolveSupplierPrice(priceBreaks: PriceBreak[],@@ -1437,12 +1445,8 @@ export function resolveSupplierPrice(fallbackUnitPrice: number,exchangeRate: number): number {- if (!priceBreaks.length) return fallbackUnitPrice;- return (- lookupPriceFromBreaks(- priceBreaks,- quantity,- fallbackUnitPrice * exchangeRate- ) / exchangeRate- );+ const basePrice = priceBreaks.length+ ? lookupPriceFromBreaks(priceBreaks, quantity, fallbackUnitPrice)+ : fallbackUnitPrice;+ return basePrice * exchangeRate;}