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 unit cost to new item forms

Brad Barbin9f3a210c7189
diff --git a/apps/carbon/app/components/Form/Item.tsx b/apps/carbon/app/components/Form/Item.tsx
index 3250125fe2..c1ed9aec75 100644
--- a/apps/carbon/app/components/Form/Item.tsx
+++ b/apps/carbon/app/components/Form/Item.tsx
@@ -214,6 +214,7 @@ const Item = ({
unitOfMeasureCode: "EA",
defaultMethodType: "Make",
active: props?.includeInactive === undefined,
+ unitCost: 0,
}}
/>
)}
@@ -234,6 +235,7 @@ const Item = ({
replenishmentSystem: "Buy",
defaultMethodType: "Buy",
active: props?.includeInactive === undefined,
+ unitCost: 0,
}}
/>
)}
@@ -254,6 +256,7 @@ const Item = ({
replenishmentSystem: "Make",
defaultMethodType: "Buy",
active: props?.includeInactive === undefined,
+ unitCost: 0,
}}
/>
)}
@@ -276,6 +279,7 @@ const Item = ({
replenishmentSystem: "Buy",
defaultMethodType: "Buy",
active: props?.includeInactive === undefined,
+ unitCost: 0,
}}
/>
)}
@@ -297,6 +301,7 @@ const Item = ({
replenishmentSystem: "Buy",
defaultMethodType: "Buy",
active: props?.includeInactive === undefined,
+ unitCost: 0,
}}
/>
)}
diff --git a/apps/carbon/app/modules/items/items.models.ts b/apps/carbon/app/modules/items/items.models.ts
index e26d53204c..a8b5cfd44b 100644
--- a/apps/carbon/app/modules/items/items.models.ts
+++ b/apps/carbon/app/modules/items/items.models.ts
@@ -67,7 +67,7 @@ export const itemValidator = z.object({
unitOfMeasureCode: z
.string()
.min(1, { message: "Unit of Measure is required" }),
-
+ unitCost: zfd.numeric(z.number().nonnegative().optional()),
active: zfd.checkbox(),
});
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index 92f5743430..2d4f74b6ad 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -1114,7 +1114,7 @@ export async function upsertConsumable(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const consumableInsert = await client
.from("consumable")
.insert({
id: consumable.id,
@@ -1125,6 +1125,19 @@ export async function upsertConsumable(
})
.select("*")
.single();
+
+ if (consumableInsert.error) return consumableInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: consumable.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return consumableInsert;
}
const itemUpdate = {
@@ -1197,18 +1210,31 @@ export async function upsertFixture(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const fixtureInsert = await client
.from("fixture")
.insert({
id: fixture.id,
itemId: itemId,
companyId: fixture.companyId,
- customerId: fixture.customerId ? fixture.customerId : undefined,
+ customerId: fixture.customerId || null,
createdBy: fixture.createdBy,
customFields: fixture.customFields,
})
.select("*")
.single();
+
+ if (fixtureInsert.error) return fixtureInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: fixture.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return fixtureInsert;
}
const itemUpdate = {
@@ -1281,7 +1307,7 @@ export async function upsertPart(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const partInsert = await client
.from("part")
.insert({
id: part.id,
@@ -1292,6 +1318,19 @@ export async function upsertPart(
})
.select("*")
.single();
+
+ if (partInsert.error) return partInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: part.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return partInsert;
}
const itemUpdate = {
@@ -1637,7 +1676,7 @@ export async function upsertMaterial(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const materialInsert = await client
.from("material")
.insert({
id: material.id,
@@ -1653,6 +1692,19 @@ export async function upsertMaterial(
})
.select("*")
.single();
+
+ if (materialInsert.error) return materialInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: material.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return materialInsert;
}
const itemUpdate = {
@@ -1795,7 +1847,7 @@ export async function upsertService(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const serviceInsert = await client
.from("service")
.insert({
id: service.id,
@@ -1803,9 +1855,23 @@ export async function upsertService(
serviceType: service.serviceType,
companyId: service.companyId,
createdBy: service.createdBy,
+ customFields: service.customFields,
})
.select("*")
.single();
+
+ if (serviceInsert.error) return serviceInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: service.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return serviceInsert;
}
const itemUpdate = {
id: service.id,
@@ -1906,7 +1972,7 @@ export async function upsertTool(
if (itemInsert.error) return itemInsert;
const itemId = itemInsert.data?.id;
- return client
+ const toolInsert = await client
.from("tool")
.insert({
id: tool.id,
@@ -1917,6 +1983,19 @@ export async function upsertTool(
})
.select("*")
.single();
+
+ if (toolInsert.error) return toolInsert;
+
+ const costUpdate = await client
+ .from("itemCost")
+ .update({ unitCost: tool.unitCost })
+ .eq("itemId", itemId)
+ .select("*")
+ .single();
+
+ if (costUpdate.error) return costUpdate;
+
+ return toolInsert;
}
const itemUpdate = {
diff --git a/apps/carbon/app/modules/items/ui/Consumables/ConsumableForm.tsx b/apps/carbon/app/modules/items/ui/Consumables/ConsumableForm.tsx
index 8d6914c623..9cf7f381bf 100644
--- a/apps/carbon/app/modules/items/ui/Consumables/ConsumableForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Consumables/ConsumableForm.tsx
@@ -22,6 +22,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -146,7 +147,17 @@ const ConsumableForm = ({
name="unitOfMeasureCode"
label="Unit of Measure"
/>
-
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
<CustomFormFields table="consumable" />
diff --git a/apps/carbon/app/modules/items/ui/Fixtures/FixtureForm.tsx b/apps/carbon/app/modules/items/ui/Fixtures/FixtureForm.tsx
index 2b2dce61a4..5e8f903c1f 100644
--- a/apps/carbon/app/modules/items/ui/Fixtures/FixtureForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Fixtures/FixtureForm.tsx
@@ -23,6 +23,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -169,6 +170,17 @@ const FixtureForm = ({
/>
<Customer name="customerId" label="Customer" />
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
<CustomFormFields table="fixture" />
diff --git a/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx b/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
index 37b14db487..a582f41858 100644
--- a/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Materials/MaterialForm.tsx
@@ -24,6 +24,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -166,8 +167,18 @@ const MaterialForm = ({
label="Unit of Measure"
/>
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
-
<CustomFormFields table="material" />
</div>
</ModalCardBody>
diff --git a/apps/carbon/app/modules/items/ui/Parts/PartForm.tsx b/apps/carbon/app/modules/items/ui/Parts/PartForm.tsx
index 01ac0a0a41..4894c4fbf0 100644
--- a/apps/carbon/app/modules/items/ui/Parts/PartForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Parts/PartForm.tsx
@@ -22,6 +22,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -167,8 +168,18 @@ const PartForm = ({ initialValues, type = "card", onClose }: PartFormProps) => {
label="Unit of Measure"
/>
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
-
<CustomFormFields table="part" />
</div>
</ModalCardBody>
diff --git a/apps/carbon/app/modules/items/ui/Services/ServiceForm.tsx b/apps/carbon/app/modules/items/ui/Services/ServiceForm.tsx
index e54b9f7ecd..5ae4d7b28e 100644
--- a/apps/carbon/app/modules/items/ui/Services/ServiceForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Services/ServiceForm.tsx
@@ -15,6 +15,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -89,7 +90,17 @@ const ServiceForm = ({ initialValues }: ServiceFormProps) => {
{isEditing && (
<TextArea name="description" label="Long Description" />
)}
-
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
<CustomFormFields table="service" />
</div>
diff --git a/apps/carbon/app/modules/items/ui/Tools/ToolForm.tsx b/apps/carbon/app/modules/items/ui/Tools/ToolForm.tsx
index a70207ff5b..7edd1a424f 100644
--- a/apps/carbon/app/modules/items/ui/Tools/ToolForm.tsx
+++ b/apps/carbon/app/modules/items/ui/Tools/ToolForm.tsx
@@ -22,6 +22,7 @@ import {
Hidden,
Input,
InputControlled,
+ Number,
Select,
Submit,
TextArea,
@@ -143,7 +144,17 @@ const ToolForm = ({ initialValues, type = "card", onClose }: ToolFormProps) => {
name="unitOfMeasureCode"
label="Unit of Measure"
/>
-
+ {!isEditing && (
+ <Number
+ name="unitCost"
+ label="Unit Cost"
+ formatOptions={{
+ style: "currency",
+ currency: "USD",
+ }}
+ minValue={0}
+ />
+ )}
<Boolean name="active" label="Active" />
<CustomFormFields table="tool" />
diff --git a/apps/carbon/app/routes/x+/consumable+/new.tsx b/apps/carbon/app/routes/x+/consumable+/new.tsx
index 243da901c9..6f20c34811 100644
--- a/apps/carbon/app/routes/x+/consumable+/new.tsx
+++ b/apps/carbon/app/routes/x+/consumable+/new.tsx
@@ -76,6 +76,7 @@ export default function ConsumablesNewRoute() {
replenishmentSystem: "Buy" as const,
defaultMethodType: "Buy" as const,
unitOfMeasureCode: "EA",
+ unitCost: 0,
active: true,
};
diff --git a/apps/carbon/app/routes/x+/fixture+/new.tsx b/apps/carbon/app/routes/x+/fixture+/new.tsx
index 5c654b5470..64d8a45cbb 100644
--- a/apps/carbon/app/routes/x+/fixture+/new.tsx
+++ b/apps/carbon/app/routes/x+/fixture+/new.tsx
@@ -71,6 +71,7 @@ export default function FixturesNewRoute() {
defaultMethodType: "Make" as const,
itemTrackingType: "Inventory" as "Inventory",
unitOfMeasureCode: "EA",
+ unitCost: 0,
active: true,
};
diff --git a/apps/carbon/app/routes/x+/material+/new.tsx b/apps/carbon/app/routes/x+/material+/new.tsx
index 243be66dbc..49efbe28f1 100644
--- a/apps/carbon/app/routes/x+/material+/new.tsx
+++ b/apps/carbon/app/routes/x+/material+/new.tsx
@@ -78,6 +78,7 @@ export default function MaterialsNewRoute() {
defaultMethodType: "Buy" as const,
itemTrackingType: "Inventory" as "Inventory",
unitOfMeasureCode: "EA",
+ unitCost: 0,
active: true,
};
diff --git a/apps/carbon/app/routes/x+/part+/new.tsx b/apps/carbon/app/routes/x+/part+/new.tsx
index 93bea20d8e..afe6c7b22f 100644
--- a/apps/carbon/app/routes/x+/part+/new.tsx
+++ b/apps/carbon/app/routes/x+/part+/new.tsx
@@ -66,6 +66,7 @@ export default function PartsNewRoute() {
replenishmentSystem: "Buy" as "Buy",
defaultMethodType: "Buy" as "Buy",
unitOfMeasureCode: "EA",
+ unitCost: 0,
active: true,
};
diff --git a/apps/carbon/app/routes/x+/service+/new.tsx b/apps/carbon/app/routes/x+/service+/new.tsx
index 261f3a335a..06965cebe9 100644
--- a/apps/carbon/app/routes/x+/service+/new.tsx
+++ b/apps/carbon/app/routes/x+/service+/new.tsx
@@ -63,6 +63,7 @@ export default function ServiceNewRoute() {
name: "",
description: "",
serviceType: "External" as "External",
+ unitCost: 0,
active: true,
};
diff --git a/apps/carbon/app/routes/x+/tool+/new.tsx b/apps/carbon/app/routes/x+/tool+/new.tsx
index 7a9eac542d..7d873ee629 100644
--- a/apps/carbon/app/routes/x+/tool+/new.tsx
+++ b/apps/carbon/app/routes/x+/tool+/new.tsx
@@ -66,6 +66,7 @@ export default function ToolsNewRoute() {
defaultMethodType: "Buy" as const,
itemTrackingType: "Inventory" as "Inventory",
unitOfMeasureCode: "EA",
+ unitCost: 0,
active: true,
};
diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.ts
index d1e1a61b29..4d1d3f6152 100644
--- a/packages/database/supabase/functions/get-method/index.ts
+++ b/packages/database/supabase/functions/get-method/index.ts
@@ -1562,12 +1562,7 @@ serve(async (req: Request) => {
const jobMakeMethodInserts: Database["public"]["Tables"]["jobMakeMethod"]["Insert"][] =
[];
- console.log({ node });
for await (const child of node.children) {
- console.log({
- child,
- });
-
jobMaterialInserts.push({
id: child.id,
jobId,
@@ -1606,9 +1601,6 @@ serve(async (req: Request) => {
.insertInto("jobMaterial")
.values(jobMaterialInserts)
.execute();
- console.log(
- `finished inserting ${jobMaterialInserts.length} materials`
- );
}
if (jobMakeMethodInserts.length > 0) {