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: negative adjustment on serial items with legacy stock (#821)

Siddharth Gaikwadfa7713539392
diff --git a/apps/erp/app/modules/inventory/inventory.service.ts b/apps/erp/app/modules/inventory/inventory.service.ts
index 2eaf190443..d1dd3c8f3c 100644
--- a/apps/erp/app/modules/inventory/inventory.service.ts
+++ b/apps/erp/app/modules/inventory/inventory.service.ts
@@ -1352,6 +1352,71 @@ export async function insertManualInventoryAdjustment(
}
}
+ // Resolve the correct stock target for a negative adjustment when:
+ // - readableId is provided: always resolve via serial number (currentQuantity
+ // may point to the wrong row due to loose null == undefined matching), OR
+ // - No currentQuantity found at all: fall back to untracked (legacy) stock.
+ //
+ // 1. If readableId is provided, adjust the entity with that serial number that
+ // has positive stock. If not found, return an error — never silently fall back.
+ // 2. If no readableId, fall back to untracked (legacy) stock.
+ if (
+ data.entryType === "Negative Adjmt." &&
+ (readableId || !currentQuantity)
+ ) {
+ if (readableId) {
+ // storageUnitQuantities is scoped to this item + location.
+ // Filter to positive-qty rows only — multiple entities can share a readableId
+ // if the same serial was used across repeated positive adjustments.
+ const resolvedQtyRow = storageUnitQuantities?.data?.find(
+ (q) =>
+ q.readableId === readableId &&
+ q.trackedEntityId != null &&
+ (q.quantity ?? 0) > 0
+ );
+ if (!resolvedQtyRow) {
+ return { error: "Serial number not found" };
+ }
+ const resolvedId = resolvedQtyRow.trackedEntityId as string;
+ const resolvedQty = resolvedQtyRow.quantity ?? 0;
+ if (data.quantity > resolvedQty) {
+ return { error: "Insufficient quantity for negative adjustment" };
+ }
+ const entityUpdate = await client
+ .from("trackedEntity")
+ .update({ quantity: resolvedQty - data.quantity, readableId })
+ .eq("id", resolvedId);
+ if (entityUpdate.error) return entityUpdate;
+ return client
+ .from("itemLedger")
+ .insert([
+ {
+ ...data,
+ trackedEntityId: resolvedId,
+ quantity: -Math.abs(data.quantity)
+ }
+ ])
+ .select("*")
+ .single();
+ }
+ // No serial number — fall back to legacy (untracked) stock
+ const legacyQty =
+ storageUnitQuantities?.data?.find(
+ (q) =>
+ q.trackedEntityId == null && q.storageUnitId == data.storageUnitId
+ )?.quantity ?? 0;
+ if (data.quantity > legacyQty) {
+ return { error: "Insufficient quantity for negative adjustment" };
+ }
+ return client
+ .from("itemLedger")
+ .insert([
+ { ...data, trackedEntityId: null, quantity: -Math.abs(data.quantity) }
+ ])
+ .select("*")
+ .single();
+ }
+
// Check if it's a negative adjustment and if the quantity is sufficient
if (data.entryType === "Negative Adjmt.") {
if (data.quantity > currentQuantityOnHand) {
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 7a3ee51ae7..8ffbdbef0a 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-05-28T18:11:04.093Z",
+ "generated": "2026-05-29T14:16:09.146Z",
"totalTools": 1083,
"modules": 15,
"tools": [
diff --git a/apps/erp/app/routes/x+/inventory+/quantities+/$itemId.adjustment.tsx b/apps/erp/app/routes/x+/inventory+/quantities+/$itemId.adjustment.tsx
index 211b4acac4..233ea14946 100644
--- a/apps/erp/app/routes/x+/inventory+/quantities+/$itemId.adjustment.tsx
+++ b/apps/erp/app/routes/x+/inventory+/quantities+/$itemId.adjustment.tsx
@@ -105,7 +105,9 @@ export async function action({ request, params }: ActionFunctionArgs) {
const flashMessage =
itemLedger.error === "Insufficient quantity for negative adjustment"
? "Insufficient quantity for negative adjustment"
- : "Failed to create manual inventory adjustment";
+ : itemLedger.error === "Serial number not found"
+ ? "Serial number not found"
+ : "Failed to create manual inventory adjustment";
throw redirect(
path.to.inventoryItem(itemId),
diff --git a/packages/locale/locales/de/mes.po b/packages/locale/locales/de/mes.po
index ad4d3030ca..ca60537aa7 100644
--- a/packages/locale/locales/de/mes.po
+++ b/packages/locale/locales/de/mes.po
@@ -283,9 +283,9 @@ msgstr "Datei hierher ziehen oder klicken, um eine Datei auszuwählen"
msgid "Dragging {0} cancelled."
msgstr "Ziehen von {0} abgebrochen."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Fällig {0}"
diff --git a/packages/locale/locales/en/mes.po b/packages/locale/locales/en/mes.po
index 4757f0a078..831da7d0b2 100644
--- a/packages/locale/locales/en/mes.po
+++ b/packages/locale/locales/en/mes.po
@@ -283,9 +283,9 @@ msgstr "Drag and drop a file here, or click to select a file"
msgid "Dragging {0} cancelled."
msgstr "Dragging {0} cancelled."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Due {0}"
diff --git a/packages/locale/locales/es/mes.po b/packages/locale/locales/es/mes.po
index 75a472000e..96ad6e6299 100644
--- a/packages/locale/locales/es/mes.po
+++ b/packages/locale/locales/es/mes.po
@@ -283,9 +283,9 @@ msgstr "Arrastre y suelte un archivo aquí, o haga clic para seleccionar un arch
msgid "Dragging {0} cancelled."
msgstr "Se canceló el arrastre de {0}."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Vence {0}"
diff --git a/packages/locale/locales/fr/mes.po b/packages/locale/locales/fr/mes.po
index ec2c9181d0..cdd5b7521c 100644
--- a/packages/locale/locales/fr/mes.po
+++ b/packages/locale/locales/fr/mes.po
@@ -283,9 +283,9 @@ msgstr "Glissez-déposez un fichier ici, ou cliquez pour sélectionner un fichie
msgid "Dragging {0} cancelled."
msgstr "Déplacement de {0} annulé."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Échéance {0}"
diff --git a/packages/locale/locales/hi/mes.po b/packages/locale/locales/hi/mes.po
index beecdc33c9..23208e72d9 100644
--- a/packages/locale/locales/hi/mes.po
+++ b/packages/locale/locales/hi/mes.po
@@ -283,9 +283,9 @@ msgstr "यहाँ एक फ़ाइल को ड्रैग और ड्
msgid "Dragging {0} cancelled."
msgstr "\"{0}\" को ड्रैग करना रद्द किया गया।"
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Due {0} के लिए"
diff --git a/packages/locale/locales/it/mes.po b/packages/locale/locales/it/mes.po
index b2cc36b142..f55972a4ad 100644
--- a/packages/locale/locales/it/mes.po
+++ b/packages/locale/locales/it/mes.po
@@ -283,9 +283,9 @@ msgstr "Trascina e rilascia un file qui, o clicca per selezionare un file"
msgid "Dragging {0} cancelled."
msgstr "Trascinamento di {0} annullato."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Scadenza {0}"
diff --git a/packages/locale/locales/ja/mes.po b/packages/locale/locales/ja/mes.po
index bce9d823fc..ff36698dbd 100644
--- a/packages/locale/locales/ja/mes.po
+++ b/packages/locale/locales/ja/mes.po
@@ -283,9 +283,9 @@ msgstr "ファイルをここにドラッグ&ドロップするか、クリッ
msgid "Dragging {0} cancelled."
msgstr "{0} のドラッグがキャンセルされました。"
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "期限 {0}"
diff --git a/packages/locale/locales/pl/mes.po b/packages/locale/locales/pl/mes.po
index 766514875a..f2208b9cc5 100644
--- a/packages/locale/locales/pl/mes.po
+++ b/packages/locale/locales/pl/mes.po
@@ -283,9 +283,9 @@ msgstr "Przeciągnij i upuść plik tutaj lub kliknij, aby wybrać plik"
msgid "Dragging {0} cancelled."
msgstr "Przeciąganie {0} anulowane."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Termin {0}"
diff --git a/packages/locale/locales/pt/mes.po b/packages/locale/locales/pt/mes.po
index 99ed9b0aa5..7982da9d06 100644
--- a/packages/locale/locales/pt/mes.po
+++ b/packages/locale/locales/pt/mes.po
@@ -283,9 +283,9 @@ msgstr "Arraste e solte um arquivo aqui ou clique para selecionar um arquivo"
msgid "Dragging {0} cancelled."
msgstr "Arraste de {0} cancelado."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Vencimento {0}"
diff --git a/packages/locale/locales/ru/mes.po b/packages/locale/locales/ru/mes.po
index 3b741cba23..56f218fa73 100644
--- a/packages/locale/locales/ru/mes.po
+++ b/packages/locale/locales/ru/mes.po
@@ -283,9 +283,9 @@ msgstr "Перетащите файл сюда или нажмите для вы
msgid "Dragging {0} cancelled."
msgstr "Перетаскивание {0} отменено."
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "Срок {0}"
diff --git a/packages/locale/locales/zh/mes.po b/packages/locale/locales/zh/mes.po
index ac490cd3bc..f06c6f5d03 100644
--- a/packages/locale/locales/zh/mes.po
+++ b/packages/locale/locales/zh/mes.po
@@ -283,9 +283,9 @@ msgstr "拖放文件到此处,或点击选择文件"
msgid "Dragging {0} cancelled."
msgstr "拖拽 {0} 已取消。"
+#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString( operation.operationDueDate ) )
#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(operation.jobDueDate) )
-#. placeholder {0}: formatRelativeTime( convertDateStringToIsoString(item.dueDate) )
msgid "Due {0}"
msgstr "截止 {0}"