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.

feat: include cost estimates in json bom exports

Brad Barbin6eb0f9bbabaa
diff --git a/apps/erp/app/routes/api+/items.methods.$id.bom.tsx b/apps/erp/app/routes/api+/items.methods.$id.bom.tsx
index c10cc2d224..f11d4d6564 100644
--- a/apps/erp/app/routes/api+/items.methods.$id.bom.tsx
+++ b/apps/erp/app/routes/api+/items.methods.$id.bom.tsx
@@ -5,7 +5,12 @@ import type { FlatTreeItem } from "~/components/TreeView";
import { flattenTree } from "~/components/TreeView";
import type { Method } from "~/modules/items";
import { getMethodTree } from "~/modules/items";
-import { calculateTotalQuantity, generateBomIds } from "~/utils/bom";
+import type { BomOperation } from "~/utils/bom";
+import {
+ calculateMadePartCosts,
+ calculateTotalQuantity,
+ generateBomIds
+} from "~/utils/bom";
import { makeDurations } from "~/utils/duration";
export async function loader({ request, params }: LoaderFunctionArgs) {
@@ -33,43 +38,71 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
...new Set(methods.map((method) => method.data.makeMethodId))
];
+ const methodOperations = await client
+ .from("methodOperation")
+ .select(
+ "*, ...process(processName:name), ...workCenter(workCenterName:name, laborRate, machineRate, overheadRate)"
+ )
+ .in("makeMethodId", makeMethodIds)
+ .eq("companyId", companyId);
+
let operationsByMakeMethodId: Record<
string,
Array<
Database["public"]["Tables"]["methodOperation"]["Row"] & {
processName: string;
workCenterName: string | null;
+ laborRate: number | null;
+ machineRate: number | null;
+ overheadRate: number | null;
}
>
> = {};
- if (withOperations) {
- const methodOperations = await client
- .from("methodOperation")
- .select(
- "*, ...process(processName:name), ...workCenter(workCenterName:name)"
- )
- .in("makeMethodId", makeMethodIds)
- .eq("companyId", companyId);
- if (methodOperations.data) {
- operationsByMakeMethodId = methodOperations.data.reduce(
- (acc, operation) => {
- acc[operation.makeMethodId] = [
- ...(acc[operation.makeMethodId] || []),
- operation
- ];
- return acc;
- },
- {} as typeof operationsByMakeMethodId
- );
- }
+ if (methodOperations.data) {
+ operationsByMakeMethodId = methodOperations.data.reduce(
+ (acc, operation) => {
+ acc[operation.makeMethodId] = [
+ ...(acc[operation.makeMethodId] || []),
+ operation
+ ];
+ return acc;
+ },
+ {} as typeof operationsByMakeMethodId
+ );
}
+ // Build BomOperation map for cost calculation
+ const bomOperationsByKey: Record<string, BomOperation[]> = {};
+ for (const [key, ops] of Object.entries(operationsByMakeMethodId)) {
+ bomOperationsByKey[key] = ops.map((op) => ({
+ operationType: op.operationType,
+ setupTime: op.setupTime,
+ setupUnit: op.setupUnit,
+ laborTime: op.laborTime,
+ laborUnit: op.laborUnit,
+ machineTime: op.machineTime,
+ machineUnit: op.machineUnit,
+ operationUnitCost: op.operationUnitCost,
+ operationMinimumCost: op.operationMinimumCost,
+ laborRate: op.laborRate ?? 0,
+ machineRate: op.machineRate ?? 0,
+ overheadRate: op.overheadRate ?? 0
+ }));
+ }
+
+ const computedCosts = calculateMadePartCosts(
+ methods,
+ bomOperationsByKey,
+ (node) => node.data.materialMakeMethodId
+ );
+
const bomIds = generateBomIds(methods);
const result = methods.map((node, index) => {
const total = calculateTotalQuantity(node, methods);
- const totalCost = total * (node.data.unitCost || 0);
+ const unitCost = computedCosts.get(node.id) ?? node.data.unitCost ?? 0;
+ const totalCost = total * unitCost;
const bomItem = {
id: bomIds[index],
@@ -77,7 +110,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
description: node.data.description,
quantity: node.data.quantity,
total,
- unitCost: node.data.unitCost,
+ unitCost,
totalCost,
uom: node.data.unitOfMeasureCode,
methodType: node.data.methodType,
diff --git a/apps/erp/app/routes/api+/production.methods.$id.bom.tsx b/apps/erp/app/routes/api+/production.methods.$id.bom.tsx
index 77ab72f156..4d46c8463e 100644
--- a/apps/erp/app/routes/api+/production.methods.$id.bom.tsx
+++ b/apps/erp/app/routes/api+/production.methods.$id.bom.tsx
@@ -5,7 +5,12 @@ import type { FlatTreeItem } from "~/components/TreeView";
import { flattenTree } from "~/components/TreeView";
import { getJobMethodTree } from "~/modules/production";
import type { JobMethod } from "~/modules/production/types";
-import { calculateTotalQuantity, generateBomIds } from "~/utils/bom";
+import type { BomOperation } from "~/utils/bom";
+import {
+ calculateMadePartCosts,
+ calculateTotalQuantity,
+ generateBomIds
+} from "~/utils/bom";
import { makeDurations } from "~/utils/duration";
export async function loader({ request, params }: LoaderFunctionArgs) {
@@ -33,6 +38,14 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
...new Set(methods.map((method) => method.data.jobMakeMethodId))
];
+ const methodOperations = await client
+ .from("jobOperation")
+ .select(
+ "*, ...process(processName:name), ...workCenter(workCenterName:name)"
+ )
+ .in("jobMakeMethodId", makeMethodIds)
+ .eq("companyId", companyId);
+
let operationsByMakeMethodId: Record<
string,
Array<
@@ -43,33 +56,50 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
>
> = {};
- if (withOperations) {
- const methodOperations = await client
- .from("jobOperation")
- .select(
- "*, ...process(processName:name), ...workCenter(workCenterName:name)"
- )
- .in("jobMakeMethodId", makeMethodIds)
- .eq("companyId", companyId);
- if (methodOperations.data) {
- operationsByMakeMethodId = methodOperations.data.reduce(
- (acc, operation) => {
- acc[operation.jobMakeMethodId ?? ""] = [
- ...(acc[operation.jobMakeMethodId ?? ""] || []),
- operation
- ];
- return acc;
- },
- {} as typeof operationsByMakeMethodId
- );
- }
+ if (methodOperations.data) {
+ operationsByMakeMethodId = methodOperations.data.reduce(
+ (acc, operation) => {
+ acc[operation.jobMakeMethodId ?? ""] = [
+ ...(acc[operation.jobMakeMethodId ?? ""] || []),
+ operation
+ ];
+ return acc;
+ },
+ {} as typeof operationsByMakeMethodId
+ );
}
+ // Build BomOperation map for cost calculation
+ const bomOperationsByKey: Record<string, BomOperation[]> = {};
+ for (const [key, ops] of Object.entries(operationsByMakeMethodId)) {
+ bomOperationsByKey[key] = ops.map((op) => ({
+ operationType: op.operationType,
+ setupTime: op.setupTime,
+ setupUnit: op.setupUnit,
+ laborTime: op.laborTime,
+ laborUnit: op.laborUnit,
+ machineTime: op.machineTime,
+ machineUnit: op.machineUnit,
+ operationUnitCost: op.operationUnitCost,
+ operationMinimumCost: op.operationMinimumCost,
+ laborRate: op.laborRate ?? 0,
+ machineRate: op.machineRate ?? 0,
+ overheadRate: op.overheadRate ?? 0
+ }));
+ }
+
+ const computedCosts = calculateMadePartCosts(
+ methods,
+ bomOperationsByKey,
+ (node) => node.data.jobMaterialMakeMethodId
+ );
+
const bomIds = generateBomIds(methods);
const result = methods.map((node, index) => {
const total = calculateTotalQuantity(node, methods);
- const totalCost = total * (node.data.unitCost || 0);
+ const unitCost = computedCosts.get(node.id) ?? node.data.unitCost ?? 0;
+ const totalCost = total * unitCost;
const bomItem = {
id: bomIds[index],
@@ -77,7 +107,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
description: node.data.description,
quantity: node.data.quantity,
total,
- unitCost: node.data.unitCost,
+ unitCost,
totalCost,
methodType: node.data.methodType,
itemType: node.data.itemType,
diff --git a/apps/erp/app/routes/api+/sales.quote.line.$id.bom.tsx b/apps/erp/app/routes/api+/sales.quote.line.$id.bom.tsx
index 87f4bc2223..6f3d710e40 100644
--- a/apps/erp/app/routes/api+/sales.quote.line.$id.bom.tsx
+++ b/apps/erp/app/routes/api+/sales.quote.line.$id.bom.tsx
@@ -3,7 +3,12 @@ import type { Database } from "@carbon/database";
import type { LoaderFunctionArgs } from "react-router";
import { flattenTree } from "~/components/TreeView";
import { getQuoteMethodTrees } from "~/modules/sales";
-import { calculateTotalQuantity, generateBomIds } from "~/utils/bom";
+import type { BomOperation } from "~/utils/bom";
+import {
+ calculateMadePartCosts,
+ calculateTotalQuantity,
+ generateBomIds
+} from "~/utils/bom";
import { makeDurations } from "~/utils/duration";
export async function loader({ request, params }: LoaderFunctionArgs) {
@@ -40,6 +45,14 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
...new Set(flattenedMethods.map((method) => method.data.quoteMakeMethodId))
];
+ const quoteOperations = await client
+ .from("quoteOperation")
+ .select(
+ "*, ...process(processName:name), ...workCenter(workCenterName:name)"
+ )
+ .in("quoteMakeMethodId", makeMethodIds)
+ .eq("companyId", companyId);
+
let operationsByMakeMethodId: Record<
string,
Array<
@@ -50,33 +63,50 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
>
> = {};
- if (withOperations) {
- const quoteOperations = await client
- .from("quoteOperation")
- .select(
- "*, ...process(processName:name), ...workCenter(workCenterName:name)"
- )
- .in("quoteMakeMethodId", makeMethodIds)
- .eq("companyId", companyId);
- if (quoteOperations.data) {
- operationsByMakeMethodId = quoteOperations.data.reduce(
- (acc, operation) => {
- acc[operation.quoteMakeMethodId ?? ""] = [
- ...(acc[operation.quoteMakeMethodId ?? ""] || []),
- operation
- ];
- return acc;
- },
- {} as typeof operationsByMakeMethodId
- );
- }
+ if (quoteOperations.data) {
+ operationsByMakeMethodId = quoteOperations.data.reduce(
+ (acc, operation) => {
+ acc[operation.quoteMakeMethodId ?? ""] = [
+ ...(acc[operation.quoteMakeMethodId ?? ""] || []),
+ operation
+ ];
+ return acc;
+ },
+ {} as typeof operationsByMakeMethodId
+ );
}
+ // Build BomOperation map for cost calculation
+ const bomOperationsByKey: Record<string, BomOperation[]> = {};
+ for (const [key, ops] of Object.entries(operationsByMakeMethodId)) {
+ bomOperationsByKey[key] = ops.map((op) => ({
+ operationType: op.operationType,
+ setupTime: op.setupTime,
+ setupUnit: op.setupUnit,
+ laborTime: op.laborTime,
+ laborUnit: op.laborUnit,
+ machineTime: op.machineTime,
+ machineUnit: op.machineUnit,
+ operationUnitCost: op.operationUnitCost,
+ operationMinimumCost: op.operationMinimumCost,
+ laborRate: op.laborRate ?? 0,
+ machineRate: op.machineRate ?? 0,
+ overheadRate: op.overheadRate ?? 0
+ }));
+ }
+
+ const computedCosts = calculateMadePartCosts(
+ flattenedMethods,
+ bomOperationsByKey,
+ (node) => node.data.quoteMaterialMakeMethodId
+ );
+
const bomIds = generateBomIds(flattenedMethods);
const result = flattenedMethods.map((node, index) => {
const total = calculateTotalQuantity(node, flattenedMethods);
- const totalCost = total * (node.data.unitCost || 0);
+ const unitCost = computedCosts.get(node.id) ?? node.data.unitCost ?? 0;
+ const totalCost = total * unitCost;
const bomItem = {
id: bomIds[index],
@@ -84,7 +114,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
description: node.data.description,
quantity: node.data.quantity,
total,
- unitCost: node.data.unitCost,
+ unitCost,
totalCost,
methodType: node.data.methodType,
itemType: node.data.itemType,
diff --git a/apps/erp/app/routes/api+/sales.quote.line.$id.bom[.]csv.tsx b/apps/erp/app/routes/api+/sales.quote.line.$id.bom[.]csv.tsx
index 52c0d2f927..2bc5ef1822 100644
--- a/apps/erp/app/routes/api+/sales.quote.line.$id.bom[.]csv.tsx
+++ b/apps/erp/app/routes/api+/sales.quote.line.$id.bom[.]csv.tsx
@@ -70,6 +70,9 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
});
}
+ if (!quote.data?.quoteId) {
+ throw new Error("Failed to fetch quote");
+ }
const methodTrees = await getQuoteMethodTrees(client, quote.data?.quoteId);
if (methodTrees.error) {