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.

chore(mes): trim step backflush comments

Raul28bf9fd25dfd
diff --git a/apps/mes/app/components/AssemblyView.tsx b/apps/mes/app/components/AssemblyView.tsx
index 29f25e2ab4..9c7795e45b 100644
--- a/apps/mes/app/components/AssemblyView.tsx
+++ b/apps/mes/app/components/AssemblyView.tsx
@@ -772,10 +772,9 @@ export function AssemblyView({
// with NO step link are unassigned ("General") and show on the FIRST step only — loose
// untracked parts are backflushed when the unit's first step is recorded, so that's
// where the operator sees and handles them; repeating them on every step just buries
- // the step-specific parts. A General part attached to another operation of the method
- // belongs to that operation's first step, where its backflush runs. Quantity and issuing
- // live on the jobMaterial (the part), so a part on several assigned steps never
- // multiplies the requirement.
+ // the step-specific parts. A General part on another operation shows on that
+ // operation's first step instead. Quantity and issuing live on the jobMaterial (the
+ // part), so a part on several assigned steps never multiplies the requirement.
const stepNumberById = new Map(steps.map((s, i) => [s.id, i + 1] as const));
const allMaterials: any[] = materials?.materials ?? [];
const isGeneralMaterial = (m: any) =>
diff --git a/apps/mes/app/services/operations.service.ts b/apps/mes/app/services/operations.service.ts
index 4f4ad81d6f..29cc403cab 100644
--- a/apps/mes/app/services/operations.service.ts
+++ b/apps/mes/app/services/operations.service.ts
@@ -1389,20 +1389,8 @@ export async function getJobMaterialsByOperationId(
* built one at a time; an untracked (not serial/batch) part is consumed as the
* step that "owns" it is recorded, so recording that step issues one unit's worth
* automatically instead of making the operator issue it by hand (tracked parts
- * still require a scan). Step ownership:
- * • A step-assigned part (jobMaterialStep) is owned by its assigned step(s) and
- * issues when any of those steps is recorded for a unit.
- * • A loose part (no step assignment) is owned by its operation's FIRST step —
- * that's where the operator sees it. A loose part attached to another
- * operation of the same make method waits for that operation's first step,
- * as the completion backflush attributes it (`planStepBackflush`).
- * Idempotent by construction: per material the target is
- * (distinct units that recorded an owning step) × per-unit quantity, and only the
- * shortfall vs quantityIssued is issued — re-records, undo/redo, manual issues,
- * and the completion-time backflush (backflush_job_materials, which tops up to
- * completed × per-unit) can all overlap without double-issuing. A part assigned to
- * several steps is still counted once per unit, so its requirement never
- * multiplies. Eligibility mirrors backflush_job_materials (Material/Part/Consumable,
+ * still require a scan). `planStepBackflush` decides which step owns a part and
+ * how much to issue. Eligibility mirrors backflush_job_materials (Material/Part/Consumable,
* not Make to Order, untracked). Issue failures (e.g. insufficient stock) are
* collected, not thrown — the part simply stays unissued and manually issuable.
*/
@@ -1452,8 +1440,6 @@ export async function backflushUntrackedMaterialsOnStepRecord(
return { error: materials.error };
}
- // Step ownership: materialId → the step ids it's assigned to (empty = loose),
- // plus each link's per-step quantity (NULL = the full BOM line quantity).
// Falls back to the bare links against a pre-migration schema.
let stepLinks = await client
.from("jobMaterialStep")
@@ -1473,8 +1459,6 @@ export async function backflushUntrackedMaterialsOnStepRecord(
}
if (stepLinks.error) return { error: stepLinks.error };
- // Units (index) that have recorded each of the operation's steps, so we can
- // count distinct started units per material from its owning step(s).
const records = await client
.from("jobOperationStepRecord")
.select("jobOperationStepId, index")
diff --git a/apps/mes/app/services/step-backflush.test.ts b/apps/mes/app/services/step-backflush.test.ts
index 4627a45fe6..4ddbd685a2 100644
--- a/apps/mes/app/services/step-backflush.test.ts
+++ b/apps/mes/app/services/step-backflush.test.ts
@@ -34,8 +34,6 @@ describe("planStepBackflush", () => {
});
it("does not issue a loose part that belongs to another operation", () => {
- // A two-operation method: the resin is on operation 2. Recording operation
- // 1's first step must not consume it; operation 2 owns it.
const issues = planStepBackflush({
operationId: "op-1",
stepIds: ["s1"],
diff --git a/apps/mes/app/services/step-backflush.ts b/apps/mes/app/services/step-backflush.ts
index 7adc9636fd..118f302b9a 100644
--- a/apps/mes/app/services/step-backflush.ts
+++ b/apps/mes/app/services/step-backflush.ts
@@ -2,7 +2,6 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
-/** An untracked job material the step-record backflush may issue. */
export type StepBackflushMaterial = {
id: string;
itemId: string;
@@ -25,10 +24,8 @@ export type StepBackflushIssue = {
};
/**
- * Whether an operation owns a loose (step-unassigned) part: the part is on this
- * operation, or on no operation at all. A part attached to another operation of
- * the same make method is that operation's, the same attribution the completion
- * backflush (`issueJobOperationMaterials`, by `jobOperationId`) uses.
+ * A loose part belongs to its own operation, or to any operation when it has
+ * none, as the completion backflush attributes it.
*/
export function ownsLooseMaterial(
material: Pick<StepBackflushMaterial, "jobOperationId">,
@@ -76,7 +73,6 @@ export function planStepBackflush(args: {
linkQuantities.set(link.jobMaterialId, quantities);
}
- // Units (index) that have recorded each of the operation's steps.
const unitsByStep = new Map<string, Set<number>>();
for (const r of args.records) {
const set = unitsByStep.get(r.jobOperationStepId) ?? new Set<number>();
@@ -88,8 +84,6 @@ export function planStepBackflush(args: {
for (const material of args.materials) {
const perUnit = material.quantity ?? 0;
if (perUnit <= 0) continue;
- // Owning steps: the material's assigned steps (scoped to this operation), or
- // the first step when it's loose and this operation owns it.
const owning = ownedSteps.get(material.id);
const isLoose = !owning || owning.size === 0;
if (isLoose && !ownsLooseMaterial(material, args.operationId)) continue;
@@ -100,21 +94,13 @@ export function planStepBackflush(args: {
const hasSplitQuantities = triggerStepIds.some(
(id) => (quantities?.get(id) ?? null) !== null
);
- // Distinct units that recorded at least one owning step — the line-level
- // requirement bound: a material can never owe more than units × per-unit.
const units = new Set<number>();
for (const id of triggerStepIds) {
for (const idx of unitsByStep.get(id) ?? []) units.add(idx);
}
let target: number;
if (hasSplitQuantities) {
- // The line is SPLIT across steps (5 screws at step 1, 5 at step 2): each
- // owning step consumes its own share as it is recorded, so the target is
- // the per-step sum. A link left without an explicit quantity falls back
- // to the full per-unit quantity for its step. The sum is capped at the
- // line-level bound — the BOM line is the source of truth, so shares that
- // over-allocate the line (2 + 10 on a 5-quantity line) must not issue
- // more stock than the line requires.
+ // A link with no quantity of its own takes the full per-unit quantity.
target = Math.min(
triggerStepIds.reduce(
(sum, id) =>
@@ -125,8 +111,6 @@ export function planStepBackflush(args: {
units.size * perUnit
);
} else {
- // Unsplit: distinct units counted once, so the requirement never
- // multiplies across a material's several owning steps.
target = units.size * perUnit;
}
const delta = target - (material.quantityIssued ?? 0);