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(production): refuse to scrap a unit of another operation
Raul14f4568f70bc
diff --git a/apps/erp/app/modules/inventory/inventory.service.ts b/apps/erp/app/modules/inventory/inventory.service.tsindex a0a1549299..571daffa12 100644--- a/apps/erp/app/modules/inventory/inventory.service.ts+++ b/apps/erp/app/modules/inventory/inventory.service.ts@@ -23446,6 +23446,7 @@ export async function issueMaterial(if (!jobOperation.data || !jobOperation.data.jobMakeMethodId) {throw new NotFoundError("Job operation not found");}+ const completedMakeMethodId = jobOperation.data.jobMakeMethodId;// UNIT-AXIS order, identical to the apps' getTrackedEntitiesByMakeMethodId:// position here is the `index` step records are stored under, so the@@ -23515,10 +23516,13 @@ export async function issueMaterial(// the timing (under 'production' every unit; otherwise only a unit// left unnumbered by a timing change). A Consumed entity is not cut to// one unit below, so it is numbered only when it already holds one.+ // The unit id comes from the request, so only a unit of this+ // operation's make method draws a number.await sequence.assignSerialNumberIfMissing(trx, {trackedEntityId,companyId,- onlyIfSingleUnit: trackedEntity.status === "Consumed"+ onlyIfSingleUnit: trackedEntity.status === "Consumed",+ jobMakeMethodId: completedMakeMethodId});if (trackedEntity.status !== "Consumed") {@@ -23764,6 +23768,20 @@ export async function issueMaterial(if (isSerial && quantity !== 1) {throw new Error("Serial units are scrapped one at a time");}+ // The unit id comes from the request, and a scrap cuts an unsplit seed+ // to one unit: another job's seed would lose the units it holds.+ if (isSerial) {+ if (trackedEntitiesRes.error) {+ throw new Error("Failed to fetch the operation's units");+ }+ if (+ !trackedEntitiesRes.data?.some((unit) => unit.id === scrapEntityId)+ ) {+ throw new InvalidInputError(+ "The unit does not belong to this operation"+ );+ }+ }const todayScrapOp = datetime.today(await getCompanyTimeZone(db, companyId))diff --git a/apps/erp/test/database/serial-number-timing.test.ts b/apps/erp/test/database/serial-number-timing.test.tsindex 4d99e73edb..22cb61ad6f 100644--- a/apps/erp/test/database/serial-number-timing.test.ts+++ b/apps/erp/test/database/serial-number-timing.test.ts@@ -666,6 +666,47 @@ describe.skipIf(!client)("serial number timing", () => {}});+ it("refuses to scrap another job's unit, and leaves that unit whole", async () => {+ const f = await alwaysPostFixture();+ try {+ await stockPart(f, 20);+ await serialSetup(f, "production");+ const job = await untouchedSerialJob(f, "8", 3);+ const otherJob = await untouchedSerialJob(f, "9", 3);+ const [otherSeed] = await units(f, otherJob.makeMethodId);+ const scrapReason = await f.db+ .insertInto("scrapReason")+ .values({ name: "Damaged", companyId: f.companyId, createdBy: USER })+ .returning("id")+ .executeTakeFirstOrThrow();++ const result = await invokePosting(+ handlesOf(f.ctx),+ "issue",+ {+ type: "jobOperationScrap",+ jobOperationId: job.operationId,+ quantity: 1,+ scrapReasonId: scrapReason.id,+ trackedEntityId: otherSeed!.id+ },+ issue+ );++ expect(result.error?.status).toBe(400);+ expect(await units(f, otherJob.makeMethodId)).toEqual([+ expect.objectContaining({+ readableId: null,+ quantity: 3,+ status: "Reserved"+ })+ ]);+ expect(await drawn(f)).toBe(0);+ } finally {+ await f.cleanup();+ }+ });+it("numbers and receives untouched units when the last operation is marked Done", async () => {const f = await alwaysPostFixture();try {