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(mes): allow unconsuming multiple batches in issue modal (#1228)

Siddharth Gaikwad8012f0e816b9
diff --git a/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx b/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx
index dc0ff1226a..845fc18233 100644
--- a/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx
+++ b/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx
@@ -28,7 +28,7 @@ import {
useThrottle,
VStack
} from "@carbon/react";
-import { getItemReadableId } from "@carbon/utils";
+import { getItemById, getItemReadableId } from "@carbon/utils";
import { Trans, useLingui } from "@lingui/react/macro";
import { motion } from "framer-motion";
import { nanoid } from "nanoid";
@@ -201,6 +201,7 @@ const BillOfMaterial = ({
const addItemButtonRef = useRef<HTMLButtonElement>(null);
const [items] = useItems();
+ const itemName = getItemById(items, makeMethod.itemId)?.name;
const fetcher = useFetcher<{}>();
const [searchParams] = useSearchParams();
@@ -469,6 +470,11 @@ const BillOfMaterial = ({
<CardHeader>
<CardTitle className="flex flex-row items-center gap-2">
<Trans>Bill of Material</Trans>
+ {itemName && (
+ <span className="text-xs text-muted-foreground font-normal">
+ {itemName}
+ </span>
+ )}
{isReadOnly && (
<Tooltip>
<TooltipTrigger className="text-muted-foreground">
diff --git a/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx b/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
index c432016f9e..c94618153f 100644
--- a/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
+++ b/apps/erp/app/modules/items/ui/Item/BillOfProcess.tsx
@@ -37,6 +37,7 @@ import {
VStack
} from "@carbon/react";
import { Editor } from "@carbon/react/Editor";
+import { getItemById } from "@carbon/utils";
import { getLocalTimeZone, today } from "@internationalized/date";
import { Trans, useLingui } from "@lingui/react/macro";
import type { DragControls } from "framer-motion";
@@ -260,6 +261,7 @@ const BillOfProcess = ({
const { id: userId } = useUser();
const [allItems] = useItems();
+ const itemName = getItemById(allItems, makeMethod.itemId)?.name;
const materialItemIds = useMemo(
() => new Set((materials ?? []).map((m) => m.itemId)),
@@ -818,6 +820,11 @@ const BillOfProcess = ({
<CardHeader>
<CardTitle className="flex flex-row items-center gap-2">
<Trans>Bill of Process</Trans>
+ {itemName && (
+ <span className="text-xs text-muted-foreground font-normal">
+ {itemName}
+ </span>
+ )}
{isReadOnly && (
<Tooltip>
<TooltipTrigger className="text-muted-foreground">
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
index 88bd5fe7cf..9d5f58bf3f 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
@@ -1,5 +1,5 @@
{
- "generated": "2026-08-11T07:07:18.210Z",
+ "generated": "2026-08-11T11:05:29.792Z",
"totalTools": 1431,
"modules": 15,
"tools": [
diff --git a/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx b/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx
index 04b765ffae..b51bcc1ea5 100644
--- a/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx
+++ b/apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx
@@ -360,7 +360,9 @@ export function IssueMaterialModal({
Array<{ index: number; id: string; quantity: number }>
>([{ index: 0, id: "", quantity: initialQuantity }]);
const [batchErrors, setBatchErrors] = useState<Record<number, string>>({});
- const [unconsumedBatch, setUnconsumedBatch] = useState("");
+ const [unconsumedBatches, setUnconsumedBatches] = useState<
+ Array<{ index: number; id: string }>
+ >([{ index: 0, id: "" }]);
// Tab state
const [activeTab, setActiveTab] = useState("scan");
@@ -834,6 +836,35 @@ export function IssueMaterialModal({
});
}, []);
+ // Update functions for unconsume batch rows
+ const updateUnconsumedBatch = useCallback(
+ (batch: { index: number; id: string }) => {
+ setUnconsumedBatches((prev) => {
+ const newBatches = [...prev];
+ newBatches[batch.index] = batch;
+ return newBatches;
+ });
+ },
+ []
+ );
+
+ const addUnconsumedBatch = useCallback(() => {
+ setUnconsumedBatches((prev) => {
+ // Pre-fill the new row with the next consumed batch not already selected
+ const used = new Set(prev.map((b) => b.id).filter(Boolean));
+ const next = trackedInputs.find((input) => !used.has(input.id));
+ return [...prev, { index: prev.length, id: next?.id ?? "" }];
+ });
+ }, [trackedInputs]);
+
+ const removeUnconsumedBatch = useCallback((indexToRemove: number) => {
+ setUnconsumedBatches((prev) =>
+ prev
+ .filter((_, i) => i !== indexToRemove)
+ .map((item, i) => ({ ...item, index: i }))
+ );
+ }, []);
+
// Submit handlers
const handleSubmitSerial = useCallback(() => {
if (!parentId) {
@@ -1025,8 +1056,9 @@ export function IssueMaterialModal({
}, [selectedTrackedInputs, material?.id, parentId, unconsumeFetcher]);
const handleUnconsumeBatch = useCallback(() => {
- if (!unconsumedBatch) {
- toast.error("Please select a batch to unconsume");
+ const selectedBatches = unconsumedBatches.filter((batch) => batch.id);
+ if (selectedBatches.length === 0) {
+ toast.error("Please select at least one batch to unconsume");
return;
}
@@ -1038,14 +1070,11 @@ export function IssueMaterialModal({
const payload = {
materialId: material.id,
parentTrackedEntityId: parentId,
- children: [
- {
- trackedEntityId: unconsumedBatch,
- quantity:
- trackedInputs.find((input) => input.id === unconsumedBatch)
- ?.quantity ?? 0
- }
- ]
+ children: selectedBatches.map((batch) => ({
+ trackedEntityId: batch.id,
+ quantity:
+ trackedInputs.find((input) => input.id === batch.id)?.quantity ?? 0
+ }))
};
unconsumeFetcher.submit(JSON.stringify(payload), {
@@ -1054,7 +1083,7 @@ export function IssueMaterialModal({
encType: "application/json"
});
}, [
- unconsumedBatch,
+ unconsumedBatches,
material?.id,
parentId,
trackedInputs,
@@ -1834,30 +1863,65 @@ export function IssueMaterialModal({
{hasTrackedInputs && (
<TabsContent value="unconsume">
<div className="flex flex-col gap-4">
- <div className="flex gap-2">
- <div className="flex-1">
- <ComboboxBase
- value={unconsumedBatch}
- onChange={setUnconsumedBatch}
- options={unconsumeOptions}
- placeholder="Select batch to unconsume"
- />
- </div>
- {unconsumedBatch && (
- <div className="w-24">
- <Input
- isReadOnly
- value={
- trackedInputs
- .find(
- (input) =>
- input.id === unconsumedBatch
- )
- ?.quantity.toString() ?? "0"
+ {unconsumedBatches.map((batch, index) => (
+ <div key={index} className="flex gap-2">
+ <div className="flex-1">
+ <ComboboxBase
+ value={batch.id}
+ onChange={(value) =>
+ updateUnconsumedBatch({
+ index,
+ id: value
+ })
}
+ options={unconsumeOptions.filter(
+ (option) =>
+ !unconsumedBatches.some(
+ (b, i) =>
+ b.id === option.value && i !== index
+ )
+ )}
+ placeholder="Select batch to unconsume"
/>
</div>
- )}
+ {batch.id && (
+ <div className="w-24">
+ <Input
+ isReadOnly
+ value={
+ trackedInputs
+ .find(
+ (input) => input.id === batch.id
+ )
+ ?.quantity.toString() ?? "0"
+ }
+ />
+ </div>
+ )}
+ {index > 0 && (
+ <IconButton
+ aria-label="Remove Batch"
+ icon={<LuX />}
+ variant="ghost"
+ onClick={() => removeUnconsumedBatch(index)}
+ className="flex-shrink-0"
+ />
+ )}
+ </div>
+ ))}
+ <div>
+ <Button
+ type="button"
+ variant="secondary"
+ leftIcon={<LuCirclePlus />}
+ onClick={addUnconsumedBatch}
+ isDisabled={
+ unconsumedBatches.length >=
+ trackedInputs.length
+ }
+ >
+ Add Batch
+ </Button>
</div>
<div className="h-8" />
</div>
@@ -1924,7 +1988,7 @@ export function IssueMaterialModal({
unconsumeFetcher.state !== "idle" ||
(trackingType === "Serial"
? selectedTrackedInputs.length === 0
- : !unconsumedBatch)
+ : !unconsumedBatches.some((batch) => batch.id))
}
>
Unconsume