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.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(items): method import counts an existing PART row's part as updated
Raulf807977d8d84
diff --git a/.ai/plans/2026-10-21-method-import-append.md b/.ai/plans/2026-10-21-method-import-append.mdindex 58b4579ed6..9c373cdbd9 100644--- a/.ai/plans/2026-10-21-method-import-append.md+++ b/.ai/plans/2026-10-21-method-import-append.md@@ -79,6 +79,8 @@ import match the BOM and Bill of Process editors.| CodeRabbit: the change notice approval check ran a separate request while a part's transaction was open. | The import reads every change notice lock before any transaction. || CodeRabbit: the status check read the version before the row lock, so a Draft activated in that gap was still written. | The refusals read the locked row. || CodeRabbit: two imports that created the same new version at once both inserted it, and the second failed on the unique constraint. | `writeGroup` locks the part's item row (`FOR NO KEY UPDATE`) before it reads the versions. |+| CodeRabbit: a PART row for a part that exists counted it as inserted. | A PART row counts as inserted only when it created the part. For an existing part it is listed as skipped. |+| CodeRabbit: the toast dropped the skipped count when rows also needed fixing. | The toast gives both counts. || The BOM wizard preset EA as the Unit of Measure, so a blank unit never reached the import's fallback to the material's unit. | The wizard presets no BOM unit. || The results said "4 updated" for 4 parts and 6 BOM lines, and counted skipped rows in the same sentence. | Method imports also return the rows added by kind (`added`). The results dialog and the toast name the parts, then the rows. |diff --git a/.claude/rules/csv-import-system.md b/.claude/rules/csv-import-system.mdindex d758713431..ca215fabff 100644--- a/.claude/rules/csv-import-system.md+++ b/.claude/rules/csv-import-system.md@@ -282,7 +282,10 @@ continue.the BOM lines, operations, steps, tools and parameters written. `importCsvFile`returns it (null for every other table), and `ImportResultsModal`(`MethodImportCounts`) and the `ImportCSVModal` toast name the parts, then- the rows.+ the rows. A PART row counts as inserted only when it created the part+ (`xmax = 0` on the item upsert). For a part that exists, the upsert keeps+ the part's fields, so the PART row is listed as skipped and the part counts+ as updated only when its method rows were written.- **Rows match the editors' rows.** BOM lines copy `methodType` /`sourcingType` / `materialMakeMethodId` / `storageUnitIds` from the componentitem, as `upsertMethodMaterial` does, and take the component's unit when thediff --git a/apps/erp/app/components/ImportCSVModal/ImportCSVModal.tsx b/apps/erp/app/components/ImportCSVModal/ImportCSVModal.tsxindex 0de3905fb2..5c6e2d7792 100644--- a/apps/erp/app/components/ImportCSVModal/ImportCSVModal.tsx+++ b/apps/erp/app/components/ImportCSVModal/ImportCSVModal.tsx@@ -238,7 +238,9 @@ function methodImportMessage(counts: {.join(", ");const outcome =counts.errors > 0- ? ` — ${counts.errors} row(s) need fixing`+ ? ` — ${counts.errors} row(s) need fixing${+ counts.skipped > 0 ? `, ${counts.skipped} skipped` : ""+ }`: counts.skipped > 0? `, skipped ${counts.skipped} row(s)`: "";diff --git a/apps/erp/app/modules/shared/shared.service.ts b/apps/erp/app/modules/shared/shared.service.tsindex 925a154044..fcee500fb2 100644--- a/apps/erp/app/modules/shared/shared.service.ts+++ b/apps/erp/app/modules/shared/shared.service.ts@@ -3443,13 +3443,11 @@ export async function importMethods(// not something the user needs to fix, so a skip, not an error.for (const s of result.skipped) summary.skipped.push(s);addMethodRows(added, result.added);- if (Object.values(result.added).some((n) => n > 0)) {- // A new part counts as imported; an existing part as updated.- if (g.partRow) summary.inserted++;- else summary.updated++;- } else if (g.partRow) {- // A PART row with no BOM/BOP rows — only the part itself was created.- summary.inserted++;+ // A part its PART row created counts as inserted; a part that existed+ // counts as updated when the import wrote rows into its method.+ if (result.createdPart) summary.inserted++;+ else if (Object.values(result.added).some((n) => n > 0)) {+ summary.updated++;}} catch (err) {if (err instanceof MethodImportRefused) {@@ -4023,7 +4021,11 @@ async function writeGroup(releaseControl: ReleaseControl;changeNoticeLocks: Map<string, string | null>;}-): Promise<{ added: MethodRowsAdded; skipped: RowIssue[] }> {+): Promise<{+ added: MethodRowsAdded;+ skipped: RowIssue[];+ createdPart: boolean;+}> {const { companyId, userId } = ctx;const now = () => datetime.timestamp();const partLabel = `${g.readableId} rev ${g.revision}`;@@ -4036,6 +4038,7 @@ async function writeGroup(// Resolve (or create) the parent item.let parentItemId: string;let revisionStatus: string | null;+ let createdPart = false;if (g.partRow) {const r = g.partRow.record;const inserted = await trx@@ -4064,10 +4067,24 @@ async function writeGroup(updatedBy: userId} as never))- .returning(["id", "revisionStatus"])+ // xmax is 0 on a row this statement inserted, not on one it updated.+ .returning([+ "id",+ "revisionStatus",+ sql<boolean>`xmax = 0`.as("created")+ ]).execute();parentItemId = inserted[0]!.id;revisionStatus = inserted[0]!.revisionStatus ?? null;+ createdPart = inserted[0]!.created;+ if (!createdPart) {+ // The upsert keeps the existing part's fields, so the row writes+ // nothing; its BOM and BOP rows still apply.+ skipped.push({+ row: rowOf(g.partRow.index),+ reason: `Part ${partLabel} already exists; its PART row changes nothing`+ });+ }// The makeMethod Draft v1 is created by the AFTER INSERT trigger on item.// The part row links to the item by readableId (part.id = item.readableId).@@ -4471,7 +4488,7 @@ async function writeGroup(}if (issues.length > 0) throw new MethodImportRefused(issues);- return { added, skipped };+ return { added, skipped, createdPart };});}diff --git a/apps/erp/test/database/method-import-guards.test.ts b/apps/erp/test/database/method-import-guards.test.tsindex 156abadde2..424c26d070 100644--- a/apps/erp/test/database/method-import-guards.test.ts+++ b/apps/erp/test/database/method-import-guards.test.ts@@ -319,6 +319,42 @@ describe("method import guards", () => {});+ databaseTest(+ "counts a part as created only when its PART row created it",+ async () => {+ f = await methodImportFixture();+ const part = {+ rowType: "PART",+ readableId: "NEW-1",+ name: "New assembly"+ };+ const line = (componentId: string) => ({+ rowType: "BOM",+ parentId: "NEW-1",+ componentId,+ quantity: "2"+ });++ const created = await f.run("partWithMethod", [part, line("COMP-1")]);+ expect(created.errors).toEqual([]);+ expect([created.inserted, created.updated]).toEqual([1, 0]);+ expect(created.skipped).toEqual([]);++ const again = await f.run("partWithMethod", [part, line("COMP-1")]);+ expect(again.errors).toEqual([]);+ expect([again.inserted, again.updated]).toEqual([0, 0]);+ expect(again.skipped.map((s) => s.reason)).toEqual([+ "Part NEW-1 rev 0 already exists; its PART row changes nothing",+ "Material COMP-1 rev 0 is already on the BOM of NEW-1 rev 0; skipped"+ ]);++ const more = await f.run("partWithMethod", [part, line("COMP-2")]);+ expect(more.errors).toEqual([]);+ expect([more.inserted, more.updated]).toEqual([0, 1]);+ expect(more.added?.materials).toBe(1);+ }+ );+databaseTest("two imports that create the same version at the same time create it once",async () => {