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: update unit cost on item creation

Brad Barbin79e456bb242b
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.ts
index c449158656..e8c721d3b4 100644
--- a/apps/erp/app/modules/items/items.service.ts
+++ b/apps/erp/app/modules/items/items.service.ts
@@ -2027,25 +2027,15 @@ export async function upsertConsumable(
.from("itemCost")
.update(
sanitize({
- itemPostingGroupId: consumable.postingGroupId
+ itemPostingGroupId: consumable.postingGroupId,
+ unitCost: consumable.unitCost
})
)
.eq("itemId", itemId)
]);
if (consumableInsert.error) return consumableInsert;
- if (itemCostUpdate.error) {
- console.error(itemCostUpdate.error);
- }
-
- const costUpdate = await client
- .from("itemCost")
- .update({ unitCost: consumable.unitCost })
- .eq("itemId", itemId)
- .select("*")
- .single();
-
- if (costUpdate.error) return costUpdate;
+ if (itemCostUpdate.error) return itemCostUpdate;
const newConsumable = await client
.from("consumables")
@@ -2139,7 +2129,9 @@ export async function upsertPart(
.from("itemCost")
.update(
sanitize({
- itemPostingGroupId: part.postingGroupId
+ itemPostingGroupId: part.postingGroupId,
+ unitCost:
+ part.replenishmentSystem !== "Make" ? part.unitCost : undefined
})
)
.eq("itemId", itemId)
@@ -2150,15 +2142,6 @@ export async function upsertPart(
console.error(itemCostUpdate.error);
}
- if (part.replenishmentSystem !== "Make") {
- const costUpdate = await client
- .from("itemCost")
- .update({ unitCost: part.unitCost })
- .eq("itemId", itemId);
-
- if (costUpdate.error) return costUpdate;
- }
-
if (part.replenishmentSystem !== "Buy") {
const itemReplenishmentInsert = await client
.from("itemReplenishment")
@@ -2681,7 +2664,8 @@ export async function upsertMaterial(
.from("itemCost")
.update(
sanitize({
- itemPostingGroupId: material.postingGroupId
+ itemPostingGroupId: material.postingGroupId,
+ unitCost: material.unitCost
})
)
.eq("itemId", insert.data?.id ?? "")
@@ -2713,7 +2697,8 @@ export async function upsertMaterial(
.from("itemCost")
.update(
sanitize({
- itemPostingGroupId: material.postingGroupId
+ itemPostingGroupId: material.postingGroupId,
+ unitCost: material.unitCost
})
)
.eq("itemId", itemId);
@@ -2757,7 +2742,6 @@ export async function upsertMaterial(
defaultMethodType: material.defaultMethodType,
itemTrackingType: material.itemTrackingType,
unitOfMeasureCode: material.unitOfMeasureCode,
- unitCost: material.unitCost,
active: true
};
@@ -3200,25 +3184,15 @@ export async function upsertTool(
.from("itemCost")
.update(
sanitize({
- itemPostingGroupId: tool.postingGroupId
+ itemPostingGroupId: tool.postingGroupId,
+ unitCost: tool.unitCost
})
)
.eq("itemId", itemId)
]);
if (toolInsert.error) return toolInsert;
- if (itemCostUpdate.error) {
- console.error(itemCostUpdate.error);
- }
-
- const costUpdate = await client
- .from("itemCost")
- .update({ unitCost: tool.unitCost })
- .eq("itemId", itemId)
- .select("*")
- .single();
-
- if (costUpdate.error) return costUpdate;
+ if (itemCostUpdate.error) return itemCostUpdate;
const newTool = await client
.from("tools")