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: add conversion methods for job

Brad Barbinbcddc7abe310
diff --git a/apps/carbon/app/modules/production/production.models.ts b/apps/carbon/app/modules/production/production.models.ts
index c197e89363..8f270693e0 100644
--- a/apps/carbon/app/modules/production/production.models.ts
+++ b/apps/carbon/app/modules/production/production.models.ts
@@ -349,8 +349,8 @@ export const jobMaterialValidator = z
);
export const getJobMethodValidator = z.object({
- jobId: z.string().min(1, { message: "Job ID is required" }),
- itemId: z.string().min(1, { message: "Please select a source method" }),
+ sourceId: z.string().min(1, { message: "Source ID is required" }),
+ targetId: z.string().min(1, { message: "Please select a source method" }),
});
export const getJobMaterialMethodValidator = z.object({
diff --git a/apps/carbon/app/modules/production/production.service.ts b/apps/carbon/app/modules/production/production.service.ts
index 15ce226d04..fa227f3480 100644
--- a/apps/carbon/app/modules/production/production.service.ts
+++ b/apps/carbon/app/modules/production/production.service.ts
@@ -329,20 +329,18 @@ export async function upsertJobOperation(
export async function upsertJobMethod(
client: SupabaseClient<Database>,
+ type: "itemToJob" | "quoteLineToJob",
jobMethod: {
- itemId: string;
- jobId: string;
+ sourceId: string;
+ targetId: string;
companyId: string;
userId: string;
}
) {
return client.functions.invoke("get-method", {
body: {
- type: "itemToJob",
- sourceId: jobMethod.itemId,
- targetId: jobMethod.jobId,
- companyId: jobMethod.companyId,
- userId: jobMethod.userId,
+ type,
+ ...jobMethod,
},
});
}
diff --git a/apps/carbon/app/routes/x+/job+/methods+/get.tsx b/apps/carbon/app/routes/x+/job+/methods+/get.tsx
index ba7ba9aa8d..ca86a3446d 100644
--- a/apps/carbon/app/routes/x+/job+/methods+/get.tsx
+++ b/apps/carbon/app/routes/x+/job+/methods+/get.tsx
@@ -19,7 +19,7 @@ export async function action({ request }: ActionFunctionArgs) {
const type = formData.get("type") as string;
const serviceRole = getSupabaseServiceRole();
- if (type === "method") {
+ if (["item", "quoteLine"].includes(type)) {
const validation = await validator(getJobMethodValidator).validate(
formData
);
@@ -27,11 +27,15 @@ export async function action({ request }: ActionFunctionArgs) {
return validationError(validation.error);
}
- const jobMethod = await upsertJobMethod(serviceRole, {
- ...validation.data,
- companyId,
- userId,
- });
+ const jobMethod = await upsertJobMethod(
+ serviceRole,
+ type === "item" ? "itemToJob" : "quoteLineToJob",
+ {
+ ...validation.data,
+ companyId,
+ userId,
+ }
+ );
return json({
error: jobMethod.error ? "Failed to get job method" : null,
diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.ts
index 4b31815357..333480e92c 100644
--- a/packages/database/supabase/functions/get-method/index.ts
+++ b/packages/database/supabase/functions/get-method/index.ts
@@ -19,10 +19,13 @@ const db = getDatabaseClient<DB>(pool);
const payloadValidator = z.object({
type: z.enum([
"itemToItem",
+ "itemToJob",
+ "itemToJobMakeMethod",
"itemToQuoteMakeMethod",
"itemToQuoteLine",
- "quoteMakeMethodToItem",
"quoteLineToItem",
+ "quoteLineToJob",
+ "quoteMakeMethodToItem",
]),
sourceId: z.string(),
targetId: z.string(),
@@ -123,6 +126,400 @@ serve(async (req: Request) => {
break;
}
+ case "itemToJob": {
+ const jobId = targetId;
+ if (!jobId) {
+ throw new Error("Invalid targetId");
+ }
+ const itemId = sourceId;
+
+ const [makeMethod, jobMakeMethod, workCenters, supplierProcesses] =
+ await Promise.all([
+ client.from("makeMethod").select("*").eq("itemId", itemId).single(),
+ client
+ .from("jobMakeMethod")
+ .select("*")
+ .eq("jobId", jobId)
+ .is("parentMaterialId", null)
+ .single(),
+ client.from("workCenters").select("*").eq("companyId", companyId),
+ client
+ .from("supplierProcess")
+ .select("*")
+ .eq("companyId", companyId),
+ ]);
+
+ if (makeMethod.error) {
+ throw new Error("Failed to get make method");
+ }
+
+ if (jobMakeMethod.error) {
+ throw new Error("Failed to get job make method");
+ }
+
+ if (workCenters.error) {
+ throw new Error("Failed to get related work centers");
+ }
+
+ const [methodTrees] = await Promise.all([
+ getMethodTree(client, makeMethod.data.id),
+ ]);
+
+ if (methodTrees.error) {
+ throw new Error("Failed to get method tree");
+ }
+
+ const methodTree = methodTrees.data?.[0] as MethodTreeItem;
+ if (!methodTree) throw new Error("Method tree not found");
+
+ const getLaborAndOverheadRates = getRatesFromWorkCenters(
+ workCenters?.data
+ );
+ const getOutsideOperationRates = getRatesFromSupplierProcesses(
+ supplierProcesses?.data
+ );
+
+ await db.transaction().execute(async (trx) => {
+ // Delete existing jobMakeMethod, jobMakeMethodOperation, jobMakeMethodMaterial
+ await Promise.all([
+ trx
+ .deleteFrom("jobMakeMethod")
+ .where((eb) =>
+ eb.and([
+ eb("jobId", "=", jobId),
+ eb("parentMaterialId", "is not", null),
+ ])
+ )
+ .execute(),
+ trx.deleteFrom("jobMaterial").where("jobId", "=", jobId).execute(),
+ trx.deleteFrom("jobOperation").where("jobId", "=", jobId).execute(),
+ ]);
+
+ // traverse method tree and create:
+ // - jobMakeMethod
+ // - jobMakeMethodOperation
+ // - jobMakeMethodMaterial
+ async function traverseMethod(
+ node: MethodTreeItem,
+ parentJobMakeMethodId: string | null
+ ) {
+ const relatedOperations = await client
+ .from("methodOperation")
+ .select("*")
+ .eq("makeMethodId", node.data.materialMakeMethodId);
+
+ const jobOperations =
+ relatedOperations?.data?.map((op) => ({
+ jobId,
+ jobMakeMethodId: parentJobMakeMethodId!,
+ processId: op.processId,
+ workCenterId: op.workCenterId,
+ description: op.description,
+ setupTime: op.setupTime,
+ setupUnit: op.setupUnit,
+ laborTime: op.laborTime,
+ laborUnit: op.laborUnit,
+ machineTime: op.machineTime,
+ machineUnit: op.machineUnit,
+ ...getLaborAndOverheadRates(op.processId, op.workCenterId),
+ order: op.order,
+ operationOrder: op.operationOrder,
+ operationType: op.operationType,
+ operationSupplierProcessId: op.operationSupplierProcessId,
+ ...getOutsideOperationRates(
+ op.processId,
+ op.operationSupplierProcessId
+ ),
+ workInstruction: op.workInstruction,
+ companyId,
+ createdBy: userId,
+ customFields: {},
+ })) ?? [];
+
+ let methodOperationsToJobOperations: Record<string, string> = {};
+ if (jobOperations?.length > 0) {
+ const operationIds = await trx
+ .insertInto("jobOperation")
+ .values(jobOperations)
+ .returning(["id"])
+ .execute();
+
+ methodOperationsToJobOperations =
+ relatedOperations.data?.reduce<Record<string, string>>(
+ (acc, op, index) => {
+ if (operationIds[index].id) {
+ acc[op.id!] = operationIds[index].id!;
+ }
+ return acc;
+ },
+ {}
+ ) ?? {};
+ }
+
+ const mapMethodMaterialToJobMaterial = (child: MethodTreeItem) => ({
+ jobId,
+ jobMakeMethodId: parentJobMakeMethodId!,
+ jobOperationId:
+ methodOperationsToJobOperations[child.data.operationId],
+ itemId: child.data.itemId,
+ itemReadableId: child.data.itemReadableId,
+ itemType: child.data.itemType,
+ methodType: child.data.methodType,
+ order: child.data.order,
+ description: child.data.description,
+ quantity: child.data.quantity,
+ unitOfMeasureCode: child.data.unitOfMeasureCode,
+ unitCost: child.data.unitCost,
+ companyId,
+ createdBy: userId,
+ customFields: {},
+ });
+
+ const madeChildren = node.children.filter(
+ (child) => child.data.methodType === "Make"
+ );
+ const unmadeChildren = node.children.filter(
+ (child) => child.data.methodType !== "Make"
+ );
+
+ const madeMaterials = madeChildren.map(
+ mapMethodMaterialToJobMaterial
+ );
+ const pickedOrBoughtMaterials = unmadeChildren.map(
+ mapMethodMaterialToJobMaterial
+ );
+ if (madeMaterials.length > 0) {
+ const madeMaterialIds = await trx
+ .insertInto("jobMaterial")
+ .values(madeMaterials)
+ .returning(["id"])
+ .execute();
+
+ const jobMakeMethods = await trx
+ .selectFrom("jobMakeMethod")
+ .select(["id"])
+ .where(
+ "parentMaterialId",
+ "in",
+ madeMaterialIds.map((m) => m.id)
+ )
+ .execute();
+
+ for (const [index, child] of madeChildren.entries()) {
+ const makeMethodId = jobMakeMethods[index].id ?? null;
+ // prevent an infinite loop
+ if (child.data.itemId !== itemId) {
+ await traverseMethod(child, makeMethodId);
+ }
+ }
+ }
+
+ if (pickedOrBoughtMaterials.length > 0) {
+ await trx
+ .insertInto("jobMaterial")
+ .values(pickedOrBoughtMaterials)
+ .execute();
+ }
+ }
+
+ await traverseMethod(methodTree, jobMakeMethod.data.id);
+ });
+
+ break;
+ }
+ case "itemToJobMakeMethod": {
+ const jobMakeMethodId = targetId;
+
+ if (!jobMakeMethodId) {
+ throw new Error("Invalid targetId");
+ }
+ const itemId = sourceId;
+
+ const [makeMethod, jobMakeMethod, workCenters, supplierProcesses] =
+ await Promise.all([
+ client.from("makeMethod").select("*").eq("itemId", itemId).single(),
+ client
+ .from("jobMakeMethod")
+ .select("*")
+ .eq("id", jobMakeMethodId)
+ .single(),
+ client.from("workCenters").select("*").eq("companyId", companyId),
+ client
+ .from("supplierProcess")
+ .select("*")
+ .eq("companyId", companyId),
+ ]);
+
+ if (makeMethod.error) {
+ throw new Error("Failed to get make method");
+ }
+
+ if (jobMakeMethod.error || !jobMakeMethod.data) {
+ throw new Error("Failed to get job make method");
+ }
+
+ const [methodTrees] = await Promise.all([
+ getMethodTree(client, makeMethod.data.id),
+ ]);
+
+ if (methodTrees.error) {
+ throw new Error("Failed to get method tree");
+ }
+
+ const methodTree = methodTrees.data?.[0] as MethodTreeItem;
+ if (!methodTree) throw new Error("Method tree not found");
+
+ const getLaborAndOverheadRates = getRatesFromWorkCenters(
+ workCenters?.data
+ );
+ const getOutsideOperationRates = getRatesFromSupplierProcesses(
+ supplierProcesses?.data
+ );
+
+ await db.transaction().execute(async (trx) => {
+ // Delete existing jobMakeMethodOperation, jobMakeMethodMaterial
+ await Promise.all([
+ trx
+ .deleteFrom("jobMaterial")
+ .where("jobMakeMethodId", "=", jobMakeMethodId)
+ .execute(),
+ trx
+ .deleteFrom("jobOperation")
+ .where("jobMakeMethodId", "=", jobMakeMethodId)
+ .execute(),
+ ]);
+
+ // traverse method tree and create:
+ // - jobMakeMethod
+ // - jobMakeMethodOperation
+ // - jobMakeMethodMaterial
+ async function traverseMethod(
+ node: MethodTreeItem,
+ parentJobMakeMethodId: string | null
+ ) {
+ const relatedOperations = await client
+ .from("methodOperation")
+ .select("*")
+ .eq("makeMethodId", node.data.materialMakeMethodId);
+
+ const jobOperations =
+ relatedOperations?.data?.map((op) => ({
+ jobId: jobMakeMethod.data?.jobId!,
+ jobMakeMethodId: parentJobMakeMethodId!,
+ processId: op.processId,
+ workCenterId: op.workCenterId,
+ description: op.description,
+ setupTime: op.setupTime,
+ setupUnit: op.setupUnit,
+ laborTime: op.laborTime,
+ laborUnit: op.laborUnit,
+ machineTime: op.machineTime,
+ machineUnit: op.machineUnit,
+ ...getLaborAndOverheadRates(op.processId, op.workCenterId),
+ order: op.order,
+ operationOrder: op.operationOrder,
+ operationType: op.operationType,
+ operationSupplierProcessId: op.operationSupplierProcessId,
+ ...getOutsideOperationRates(
+ op.processId,
+ op.operationSupplierProcessId
+ ),
+ workInstruction: op.workInstruction,
+ companyId,
+ createdBy: userId,
+ customFields: {},
+ })) ?? [];
+
+ let methodOperationsToJobOperations: Record<string, string> = {};
+ if (jobOperations?.length > 0) {
+ const operationIds = await trx
+ .insertInto("jobOperation")
+ .values(jobOperations)
+ .returning(["id"])
+ .execute();
+
+ methodOperationsToJobOperations =
+ relatedOperations.data?.reduce<Record<string, string>>(
+ (acc, op, index) => {
+ if (operationIds[index].id) {
+ acc[op.id!] = operationIds[index].id!;
+ }
+ return acc;
+ },
+ {}
+ ) ?? {};
+ }
+
+ const mapMethodMaterialToJobMaterial = (child: MethodTreeItem) => ({
+ jobId: jobMakeMethod.data?.jobId!,
+ jobMakeMethodId: parentJobMakeMethodId!,
+ jobOperationId:
+ methodOperationsToJobOperations[child.data.operationId],
+ itemId: child.data.itemId,
+ itemReadableId: child.data.itemReadableId,
+ itemType: child.data.itemType,
+ methodType: child.data.methodType,
+ order: child.data.order,
+ description: child.data.description,
+ quantity: child.data.quantity,
+ unitOfMeasureCode: child.data.unitOfMeasureCode,
+ unitCost: child.data.unitCost,
+ companyId,
+ createdBy: userId,
+ customFields: {},
+ });
+
+ const madeChildren = node.children.filter(
+ (child) => child.data.methodType === "Make"
+ );
+ const unmadeChildren = node.children.filter(
+ (child) => child.data.methodType !== "Make"
+ );
+
+ const madeMaterials = madeChildren.map(
+ mapMethodMaterialToJobMaterial
+ );
+ const pickedOrBoughtMaterials = unmadeChildren.map(
+ mapMethodMaterialToJobMaterial
+ );
+ if (madeMaterials.length > 0) {
+ const madeMaterialIds = await trx
+ .insertInto("jobMaterial")
+ .values(madeMaterials)
+ .returning(["id"])
+ .execute();
+
+ const jobMakeMethods = await trx
+ .selectFrom("jobMakeMethod")
+ .select(["id"])
+ .where(
+ "parentMaterialId",
+ "in",
+ madeMaterialIds.map((m) => m.id)
+ )
+ .execute();
+
+ for (const [index, child] of madeChildren.entries()) {
+ const makeMethodId = jobMakeMethods[index].id ?? null;
+ // prevent an infinite loop
+ if (child.data.itemId !== itemId) {
+ await traverseMethod(child, makeMethodId);
+ }
+ }
+ }
+
+ if (pickedOrBoughtMaterials.length > 0) {
+ await trx
+ .insertInto("jobMaterial")
+ .values(pickedOrBoughtMaterials)
+ .execute();
+ }
+ }
+
+ await traverseMethod(methodTree, jobMakeMethod.data.id);
+ });
+ break;
+ }
case "itemToQuoteLine": {
const [quoteId, quoteLineId] = (targetId as string).split(":");
if (!quoteId || !quoteLineId) {
@@ -534,7 +931,7 @@ serve(async (req: Request) => {
case "quoteLineToItem": {
const [quoteId, quoteLineId] = (sourceId as string).split(":");
if (!quoteId || !quoteLineId) {
- throw new Error("Invalid targetId");
+ throw new Error("Invalid sourceId");
}
const itemId = targetId;
@@ -708,6 +1105,162 @@ serve(async (req: Request) => {
break;
}
+ case "quoteLineToJob": {
+ const jobId = targetId;
+ if (!jobId) {
+ throw new Error("Invalid targetId");
+ }
+
+ const [quoteId, quoteLineId] = (sourceId as string).split(":");
+ if (!quoteId || !quoteLineId) {
+ throw new Error("Invalid sourceId");
+ }
+
+ const [
+ jobMakeMethod,
+ quoteMakeMethods,
+ quoteMaterials,
+ quoteOperations,
+ ] = await Promise.all([
+ client
+ .from("jobMakeMethod")
+ .select("*")
+ .eq("jobId", jobId)
+ .is("parentMaterialId", null)
+ .single(),
+ client
+ .from("quoteMakeMethod")
+ .select("*")
+ .eq("quoteLineId", quoteLineId),
+ client
+ .from("quoteMaterial")
+ .select("*")
+ .eq("quoteLineId", quoteLineId),
+ client
+ .from("quoteOperation")
+ .select("*")
+ .eq("quoteLineId", quoteLineId),
+ ]);
+
+ if (jobMakeMethod.error || !jobMakeMethod.data) {
+ throw new Error("Failed to get job make method");
+ }
+
+ if (
+ quoteMakeMethods.error ||
+ quoteMaterials.error ||
+ quoteOperations.error
+ ) {
+ throw new Error("Failed to fetch quote data");
+ }
+
+ await db.transaction().execute(async (trx) => {
+ // delete the existing jobMakeMethods where jobId is jobId and parentMaterialId is not null
+ await trx
+ .deleteFrom("jobMakeMethod")
+ .where((eb) =>
+ eb.and([
+ eb("jobId", "=", jobId),
+ eb("parentMaterialId", "is not", null),
+ ])
+ )
+ .execute();
+
+ // Delete existing jobMaterials for this job
+ await trx
+ .deleteFrom("jobMaterial")
+ .where("jobId", "=", jobId)
+ .execute();
+
+ // Delete existing jobOperations for this job
+ await trx
+ .deleteFrom("jobOperation")
+ .where("jobId", "=", jobId)
+ .execute();
+
+ // Copy quoteMakeMethods to jobMakeMethods
+ const jobMakeMethodInserts = quoteMakeMethods.data.map((qmm) => {
+ const {
+ quoteId: _quoteId,
+ quoteLineId: _quoteLineId,
+ ...data
+ } = qmm;
+ if (qmm.parentMaterialId === null) {
+ return {
+ ...data,
+ id: jobMakeMethod.data.id,
+ jobId,
+ createdBy: userId,
+ };
+ }
+ return {
+ ...data,
+ jobId,
+ createdBy: userId,
+ };
+ });
+
+ const insertedJobMakeMethods = await trx
+ .insertInto("jobMakeMethod")
+ .values(jobMakeMethodInserts)
+ .returning(["id", "parentMaterialId"])
+ .execute();
+
+ // Create a map of quoteMakeMethodId to jobMakeMethodId
+ const makeMethodIdMap = new Map(
+ quoteMakeMethods.data.map((qmm, index) => [
+ qmm.id,
+ insertedJobMakeMethods[index].id,
+ ])
+ );
+
+ // Copy quoteMaterials to jobMaterials
+ const jobMaterialInserts = quoteMaterials.data.map((qm) => {
+ const {
+ quoteId: _quoteId,
+ quoteLineId: _quoteLineId,
+ ...data
+ } = qm;
+ return {
+ ...data,
+ jobId,
+ jobMakeMethodId: makeMethodIdMap.get(qm.quoteMakeMethodId)!,
+ createdBy: userId,
+ };
+ });
+
+ if (jobMakeMethodInserts.length > 0) {
+ await trx
+ .insertInto("jobMaterial")
+ .values(jobMaterialInserts)
+ .execute();
+ }
+
+ // Copy quoteOperations to jobOperations
+ const jobOperationInserts = quoteOperations.data.map((qo) => {
+ const {
+ quoteId: _quoteId,
+ quoteLineId: _quoteLineId,
+ quoteMakeMethodId,
+ ...data
+ } = qo;
+ return {
+ ...data,
+ jobId,
+ jobMakeMethodId: makeMethodIdMap.get(quoteMakeMethodId!)!,
+ createdBy: userId,
+ };
+ });
+
+ if (jobOperationInserts.length > 0) {
+ await trx
+ .insertInto("jobOperation")
+ .values(jobOperationInserts)
+ .execute();
+ }
+ });
+ break;
+ }
case "quoteMakeMethodToItem": {
const quoteMakeMethodId = sourceId;
const itemId = targetId;