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.

fix: get method subassembly bug

Brad Barbina72243f69656
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index 8e47810212..c9705828f6 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -2559,6 +2559,15 @@ export async function upsertMaterial(
const firstError = itemInserts.find((insert) => insert.error);
return firstError!;
}
+
+ const itemIds = itemInserts.map((insert) => insert.data!.id);
+
+ await client
+ .from("itemCost")
+ .update({
+ unitCost: material.unitCost,
+ })
+ .in("itemId", itemIds);
} else {
const itemInsert = await client
.from("item")
@@ -2577,6 +2586,13 @@ export async function upsertMaterial(
.select("id")
.single();
if (itemInsert.error) return itemInsert;
+
+ await client
+ .from("itemCost")
+ .update({
+ unitCost: material.unitCost,
+ })
+ .eq("itemId", itemInsert.data!.id);
}
const materialInsert = await client.from("material").upsert({
@@ -2614,6 +2630,7 @@ export async function upsertMaterial(
defaultMethodType: material.defaultMethodType,
itemTrackingType: material.itemTrackingType,
unitOfMeasureCode: material.unitOfMeasureCode,
+ unitCost: material.unitCost,
active: material.active,
};
diff --git a/apps/erp/app/modules/items/ui/Materials/MaterialForm.tsx b/apps/erp/app/modules/items/ui/Materials/MaterialForm.tsx
index 3d92edcda3..3869d1f76b 100644
--- a/apps/erp/app/modules/items/ui/Materials/MaterialForm.tsx
+++ b/apps/erp/app/modules/items/ui/Materials/MaterialForm.tsx
@@ -21,6 +21,7 @@ import {
DefaultMethodType,
Hidden,
InputControlled,
+ Number,
Select,
Submit,
UnitOfMeasure,
@@ -31,7 +32,7 @@ import MaterialGrade from "~/components/Form/MaterialGrade";
import MaterialType, { useMaterialTypes } from "~/components/Form/MaterialType";
import Shape, { useShape } from "~/components/Form/Shape";
import Substance, { useSubstance } from "~/components/Form/Substance";
-import { useNextItemId, usePermissions } from "~/hooks";
+import { useNextItemId, usePermissions, useUser } from "~/hooks";
import { useSettings } from "~/hooks/useSettings";
import { getMaterialDescription, getMaterialId } from "~/utils/items";
import { path } from "~/utils/path";
@@ -75,6 +76,9 @@ const MaterialForm = ({
const [substanceId, setSubstanceId] = useState<string | undefined>();
const [formId, setFormId] = useState<string | undefined>();
+ const { company } = useUser();
+ const baseCurrency = company?.baseCurrencyCode ?? "USD";
+
const fetcher = useFetcher<PostgrestResponse<{ id: string }>>();
const materialTypes = useMaterialTypes(substanceId, formId);
const substance = useSubstance();
@@ -283,6 +287,16 @@ const MaterialForm = ({
label="Inventory Unit of Measure"
/>
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: baseCurrency,
+ }}
+ minValue={0}
+ />
+
<Array name="sizes" label="Sizes" />
<CustomFormFields table="material" tags={initialValues.tags} />
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 8a15ef3646..3ee463c7c3 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -2388,6 +2388,7 @@ export type Database = {
Row: {
companyId: string
configurationParameterGroupId: string | null
+ constraints: Json | null
createdAt: string
createdBy: string
dataType: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -2403,6 +2404,7 @@ export type Database = {
Insert: {
companyId: string
configurationParameterGroupId?: string | null
+ constraints?: Json | null
createdAt?: string
createdBy: string
dataType: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -2418,6 +2420,7 @@ export type Database = {
Update: {
companyId?: string
configurationParameterGroupId?: string | null
+ constraints?: Json | null
createdAt?: string
createdBy?: string
dataType?: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -26232,7 +26235,7 @@ export type Database = {
internalNotes: Json | null
locationId: string | null
opportunityId: string | null
- orderDate: string
+ orderDate: string | null
revisionId: number
salesOrderId: string
salesPersonId: string | null
@@ -26263,7 +26266,7 @@ export type Database = {
internalNotes?: Json | null
locationId?: string | null
opportunityId?: string | null
- orderDate?: string
+ orderDate?: string | null
revisionId?: number
salesOrderId: string
salesPersonId?: string | null
@@ -26294,7 +26297,7 @@ export type Database = {
internalNotes?: Json | null
locationId?: string | null
opportunityId?: string | null
- orderDate?: string
+ orderDate?: string | null
revisionId?: number
salesOrderId?: string
salesPersonId?: string | null
@@ -46711,6 +46714,7 @@ export type Database = {
methodType: Database["public"]["Enums"]["methodType"]
parentMaterialId: string
order: number
+ kit: boolean
isRoot: boolean
}[]
}
diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.ts
index fc5d9ac285..5c2fcc9df8 100644
--- a/packages/database/supabase/functions/get-method/index.ts
+++ b/packages/database/supabase/functions/get-method/index.ts
@@ -823,7 +823,7 @@ serve(async (req: Request) => {
const jobMakeMethods = await trx
.selectFrom("jobMakeMethod")
- .select(["id"])
+ .select(["id", "parentMaterialId"])
.where(
"parentMaterialId",
"in",
@@ -831,11 +831,24 @@ serve(async (req: Request) => {
)
.execute();
+ // Create proper mapping from parentMaterialId to jobMakeMethodId
+ const materialIdToJobMakeMethodId: Record<string, string> = {};
+ jobMakeMethods.forEach((jmm) => {
+ if (jmm.parentMaterialId && jmm.id) {
+ materialIdToJobMakeMethodId[jmm.parentMaterialId] = jmm.id;
+ }
+ });
+
+ // Use proper correlation instead of index-based assumption
for (const [index, child] of madeChildren.entries()) {
- const makeMethodId = jobMakeMethods[index].id ?? null;
+ const materialId = madeMaterialIds[index]?.id;
+ const jobMakeMethodId = materialId
+ ? materialIdToJobMakeMethodId[materialId]
+ : null;
+
// prevent an infinite loop
- if (child.data.itemId !== itemId) {
- await traverseMethod(child, makeMethodId);
+ if (child.data.itemId !== itemId && jobMakeMethodId) {
+ await traverseMethod(child, jobMakeMethodId);
}
}
}
@@ -1114,7 +1127,7 @@ serve(async (req: Request) => {
const jobMakeMethods = await trx
.selectFrom("jobMakeMethod")
- .select(["id"])
+ .select(["id", "parentMaterialId"])
.where(
"parentMaterialId",
"in",
@@ -1122,11 +1135,24 @@ serve(async (req: Request) => {
)
.execute();
+ // Create proper mapping from parentMaterialId to jobMakeMethodId
+ const materialIdToJobMakeMethodId: Record<string, string> = {};
+ jobMakeMethods.forEach((jmm) => {
+ if (jmm.parentMaterialId && jmm.id) {
+ materialIdToJobMakeMethodId[jmm.parentMaterialId] = jmm.id;
+ }
+ });
+
+ // Use proper correlation instead of index-based assumption
for (const [index, child] of madeChildren.entries()) {
- const makeMethodId = jobMakeMethods[index].id ?? null;
+ const materialId = madeMaterialIds[index]?.id;
+ const jobMakeMethodId = materialId
+ ? materialIdToJobMakeMethodId[materialId]
+ : null;
+
// prevent an infinite loop
- if (child.data.itemId !== itemId) {
- await traverseMethod(child, makeMethodId);
+ if (child.data.itemId !== itemId && jobMakeMethodId) {
+ await traverseMethod(child, jobMakeMethodId);
}
}
}
@@ -1691,7 +1717,7 @@ serve(async (req: Request) => {
const quoteMakeMethods = await trx
.selectFrom("quoteMakeMethod")
- .select(["id"])
+ .select(["id", "parentMaterialId"])
.where(
"parentMaterialId",
"in",
@@ -1699,11 +1725,24 @@ serve(async (req: Request) => {
)
.execute();
+ // Create proper mapping from parentMaterialId to quoteMakeMethodId
+ const materialIdToQuoteMakeMethodId: Record<string, string> = {};
+ quoteMakeMethods.forEach((qmm) => {
+ if (qmm.parentMaterialId && qmm.id) {
+ materialIdToQuoteMakeMethodId[qmm.parentMaterialId] = qmm.id;
+ }
+ });
+
+ // Use proper correlation instead of index-based assumption
for (const [index, child] of madeChildren.entries()) {
- const makeMethodId = quoteMakeMethods[index].id ?? null;
+ const materialId = madeMaterialIds[index]?.id;
+ const quoteMakeMethodId = materialId
+ ? materialIdToQuoteMakeMethodId[materialId]
+ : null;
+
// prevent an infinite loop
- if (child.data.itemId !== itemId) {
- await traverseMethod(child, makeMethodId);
+ if (child.data.itemId !== itemId && quoteMakeMethodId) {
+ await traverseMethod(child, quoteMakeMethodId);
}
}
}
@@ -1977,7 +2016,7 @@ serve(async (req: Request) => {
const quoteMakeMethods = await trx
.selectFrom("quoteMakeMethod")
- .select(["id"])
+ .select(["id", "parentMaterialId"])
.where(
"parentMaterialId",
"in",
@@ -1985,11 +2024,24 @@ serve(async (req: Request) => {
)
.execute();
+ // Create proper mapping from parentMaterialId to quoteMakeMethodId
+ const materialIdToQuoteMakeMethodId: Record<string, string> = {};
+ quoteMakeMethods.forEach((qmm) => {
+ if (qmm.parentMaterialId && qmm.id) {
+ materialIdToQuoteMakeMethodId[qmm.parentMaterialId] = qmm.id;
+ }
+ });
+
+ // Use proper correlation instead of index-based assumption
for (const [index, child] of madeChildren.entries()) {
- const makeMethodId = quoteMakeMethods[index].id ?? null;
+ const materialId = madeMaterialIds[index]?.id;
+ const quoteMakeMethodId = materialId
+ ? materialIdToQuoteMakeMethodId[materialId]
+ : null;
+
// prevent an infinite loop
- if (child.data.itemId !== itemId) {
- await traverseMethod(child, makeMethodId);
+ if (child.data.itemId !== itemId && quoteMakeMethodId) {
+ await traverseMethod(child, quoteMakeMethodId);
}
}
}
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 8a15ef3646..3ee463c7c3 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -2388,6 +2388,7 @@ export type Database = {
Row: {
companyId: string
configurationParameterGroupId: string | null
+ constraints: Json | null
createdAt: string
createdBy: string
dataType: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -2403,6 +2404,7 @@ export type Database = {
Insert: {
companyId: string
configurationParameterGroupId?: string | null
+ constraints?: Json | null
createdAt?: string
createdBy: string
dataType: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -2418,6 +2420,7 @@ export type Database = {
Update: {
companyId?: string
configurationParameterGroupId?: string | null
+ constraints?: Json | null
createdAt?: string
createdBy?: string
dataType?: Database["public"]["Enums"]["configurationParameterDataType"]
@@ -26232,7 +26235,7 @@ export type Database = {
internalNotes: Json | null
locationId: string | null
opportunityId: string | null
- orderDate: string
+ orderDate: string | null
revisionId: number
salesOrderId: string
salesPersonId: string | null
@@ -26263,7 +26266,7 @@ export type Database = {
internalNotes?: Json | null
locationId?: string | null
opportunityId?: string | null
- orderDate?: string
+ orderDate?: string | null
revisionId?: number
salesOrderId: string
salesPersonId?: string | null
@@ -26294,7 +26297,7 @@ export type Database = {
internalNotes?: Json | null
locationId?: string | null
opportunityId?: string | null
- orderDate?: string
+ orderDate?: string | null
revisionId?: number
salesOrderId?: string
salesPersonId?: string | null
@@ -46711,6 +46714,7 @@ export type Database = {
methodType: Database["public"]["Enums"]["methodType"]
parentMaterialId: string
order: number
+ kit: boolean
isRoot: boolean
}[]
}
diff --git a/packages/database/supabase/migrations/20250821182357_get-method-kit-fix.sql b/packages/database/supabase/migrations/20250821182357_get-method-kit-fix.sql
new file mode 100644
index 0000000000..6f78240f63
--- /dev/null
+++ b/packages/database/supabase/migrations/20250821182357_get-method-kit-fix.sql
@@ -0,0 +1,80 @@
+DROP FUNCTION IF EXISTS get_job_methods_by_method_id;
+CREATE OR REPLACE FUNCTION get_job_methods_by_method_id(mid TEXT)
+RETURNS TABLE (
+ "jobId" TEXT,
+ "methodMaterialId" TEXT,
+ "jobMakeMethodId" TEXT,
+ "jobMaterialMakeMethodId" TEXT,
+ "itemId" TEXT,
+ "itemReadableId" TEXT,
+ "description" TEXT,
+ "unitOfMeasureCode" TEXT,
+ "itemType" TEXT,
+ "quantity" NUMERIC,
+ "unitCost" NUMERIC,
+ "methodType" "methodType",
+ "parentMaterialId" TEXT,
+ "order" DOUBLE PRECISION,
+ "kit" BOOLEAN,
+ "isRoot" BOOLEAN
+) AS $$
+WITH RECURSIVE material AS (
+ SELECT
+ "jobId",
+ "id",
+ "id" AS "jobMakeMethodId",
+ 'Make'::"methodType" AS "methodType",
+ "id" AS "jobMaterialMakeMethodId",
+ "itemId",
+ 'Part' AS "itemType",
+ 1::NUMERIC AS "quantity",
+ 0::NUMERIC AS "unitCost",
+ "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order",
+ FALSE AS "kit",
+ TRUE AS "isRoot"
+ FROM
+ "jobMakeMethod"
+ WHERE
+ "id" = mid
+ UNION
+ SELECT
+ child."jobId",
+ child."id",
+ child."jobMakeMethodId",
+ child."methodType",
+ child."jobMaterialMakeMethodId",
+ child."itemId",
+ child."itemType",
+ child."quantity",
+ child."unitCost",
+ parent."id" AS "parentMaterialId",
+ child."order",
+ child."kit",
+ FALSE AS "isRoot"
+ FROM
+ "jobMaterialWithMakeMethodId" child
+ INNER JOIN material parent ON parent."jobMaterialMakeMethodId" = child."jobMakeMethodId"
+ WHERE parent."methodType" = 'Make'
+)
+SELECT
+ material."jobId",
+ material.id as "methodMaterialId",
+ material."jobMakeMethodId",
+ material."jobMaterialMakeMethodId",
+ material."itemId",
+ item."readableId" AS "itemReadableId",
+ item."name" AS "description",
+ item."unitOfMeasureCode",
+ material."itemType",
+ material."quantity",
+ material."unitCost",
+ material."methodType",
+ material."parentMaterialId",
+ material."order",
+ material."kit",
+ material."isRoot"
+FROM material
+INNER JOIN item ON material."itemId" = item.id
+ORDER BY "order"
+$$ LANGUAGE sql STABLE;