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.
MES Assembly — exact per-unit (and per-step) issued attribution
Goal: Make the per-unit "issued" count in the assembly view accurate for batch
(and untracked) parents the same way it already is for serial parents — including
surfacing over-issues (scan 2 where 1 is required → shows 2/1). Do it by capturing the
unit and step at issue time and attributing consumed inputs to them, instead of
reconstructing a per-unit share from the job-wide total.
Background / root cause (confirmed)
Reproduced on job J000003 (SERIAL-BUY: 3-unit job, 1/unit required, quantityIssued = 4
— a real over-issue) which the UI rendered as 1/1 on unit 3.
- Serial parent works because each unit is its own
trackedEntity. The consume records that unit's entity as the activity output (issue/index.ts:2093-2102,trackedActivityOutput.trackedEntityId = parentTrackedEntityId). The loader readsgetTrackedInputs(unitEntity)and gets exactly what went into that unit (operations.service.ts:702-712), soMaterialRowshows it raw (per-unit, no clamp). - Batch parent can't, because every unit shares one lot entity
(
assembly.$operationId.tsx:179-182,effectiveEntityId = navEntities[0]). Every consume's output points at that same lot — nothing records which unit (or step) a serial went into. The loader falls to theelsebranch (operations.service.ts:723-732) returning the job-widequantityIssued, andMaterialRowreconstructs a per-unit share viaMath.min(required, totalIssued − unitIndex*required)(AssemblyView.tsx,getIssuedForUnit). TheMath.min(required, …)clamp hides the over-issue →1/1. - The unit/step is never captured. The Consume activity stores only
attributes: { Job, "Job Make Method", "Job Material", Employee }(issue/index.ts:2082), and the issue payload (issueTrackedEntityValidator,models.ts:161) carries no step or unit.
Approach
Stamp the unit index and step id onto the Consume activity at issue time (the assembly view knows both), then attribute consumed inputs by unit in the loader. Serial parents are unchanged (already exact). Batch/untracked become exact for newly-issued parts; consumes issued before this change (no stamp) fall back to today's heuristic.
Decision (default; confirm): stamp both Unit and Job Operation Step. Unit is what
fixes the count; step is the extra signal (which step within the unit) and is cheap to carry.
Changes
1. Issue payload carries unit + step
apps/mes/app/services/models.ts — extend issueTrackedEntityValidator (line 161):
jobOperationStepId: z.string().optional(),
unitIndex: z.number().int().nonnegative().optional(),
apps/mes/app/components/JobOperation/components/IssueMaterialModal.tsx:
- Add optional props
jobOperationStepId?: stringandunitIndex?: number. - Include them in the serial (
~:818-836) and batch (~:895-908)issueTrackedEntitypayloads. The plain-inventorypath.to.issueform (non-tracked) is out of scope — those parts backflush and don't gate.
apps/mes/app/components/AssemblyView.tsx — pass them where <IssueMaterialModal> is
rendered (~:1946): jobOperationStepId={step?.id} and unitIndex={currentUnitIndex}.
The operation view (JobOperation.tsx) omits them (undefined) → no behavior change there.
2. Route passes through
apps/mes/app/routes/x+/issue-tracked-entity.tsx — destructure the two new fields from
validation.data and forward them in the serviceRole.functions.invoke("issue", { body }).
3. Edge function stamps the Consume activity
packages/database/supabase/functions/issue/index.ts, trackedEntitiesToOperation case:
- Extend the payload validator (
~:849-868) with optionaljobOperationStepId,unitIndex. - Add to the Consume activity
attributes(~:2082):
Additive JSONB keys — safe alongside existing...(jobOperationStepId ? { "Job Operation Step": jobOperationStepId } : {}), ...(unitIndex !== undefined ? { Unit: unitIndex } : {}),attributes->>lookups. (Ships on merge tomain; the[functions.issue]config.toml entry already exists.)
4. Loader attributes per unit for batch parents
apps/mes/app/services/operations.service.ts — getJobMaterialsByOperationId:
- Add
unitIndex: numberandrequiresBatchTracking: booleanto args; the route already computes both (assembly.$operationId.tsx:170-182) — pass them in (~:184-190). - For a batch parent (
requiresBatchTracking && !requiresSerialTracking), the existinggetTrackedInputs(effectiveEntityId=lot)already returns every consumed serial descendant of the lot. For each tracked material compute:issuedThisUnit= Σinput.quantitywhereactivityAttributes["Job Material"] === material.idandactivityAttributes["Unit"] === unitIndex(stamped, exact).issuedLegacy= Σ for the same material whereactivityAttributes["Unit"]is absent (pre-change consumes) → fall back to the job-wide heuristic share for those only.quantityIssued = issuedThisUnit + heuristicShareOf(issuedLegacy). Return this as a per-unit value (same contract as the serial branch).
- Serial branch unchanged. Untracked parent (no lot entity) keeps current behavior.
5. Component treats batch tracked issued as per-unit
apps/mes/app/components/AssemblyView.tsx:
- In
visibleMaterialsWithState, setissuedIsPerUnitfor tracked materials under a batch parent too:(requiresSerialTracking || requiresBatchTracking) && isTrackedMat. getIssuedForUnit's per-unit branch already returnstotalIssuedwithout theMath.minclamp, so an over-issue renders as2/1automatically. The clamp stays only on the legacy heuristic branch (untracked / unstamped). No clamp change needed.
Backward compatibility
- In-progress batch builds started before this ships have unstamped consumes → those units use the heuristic (current behavior). Every new scan is exact. No migration/backfill required.
- Serial parents and the operation view are behaviorally unchanged.
Verification
pnpm exec turbo run typecheck --filter=mesand--filter=@carbon/databasegreen.- DB check: after issuing 2 serials on unit 3 of a batch parent (1/unit required), the
Consume activities carry
attributes->>Unit = '2'; loader returnsquantityIssued = 2for that material on unit 3;MaterialRowshows2/1. Paging to a completed unit still shows its own count (attributed by itsUnit), not the job-wide total. - Serial parent unchanged: still exact.
/testbrowser pass on a batch-parent assembly: scan 1 →1/1; scan a 2nd →2/1; the soft-gate (tracked parts must be scanned before Mark done) still behaves.
Decisions (confirmed)
- Stamp both
Unit+Job Operation Step. - Store
Unitas a 1-based unit number (currentUnitIndex + 1); loader compares againstunitIndex + 1. Payload/prop namedunitNumberto signal 1-based. - Legacy unstamped batch consumes fall back to the job-wide heuristic (current behavior).