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(scheduling): schedule subassemblies by their item lead time (#1108)
Siddharth Gaikwad23c6df7c7fd9
diff --git a/packages/database/supabase/functions/lib/scheduling/date-calculator.ts b/packages/database/supabase/functions/lib/scheduling/date-calculator.tsindex 1e31e06b13..f4d73c460d 100644--- a/packages/database/supabase/functions/lib/scheduling/date-calculator.ts+++ b/packages/database/supabase/functions/lib/scheduling/date-calculator.ts@@ -135,18 +135,38 @@ export class BackwardSchedulingStrategy implements SchedulingStrategy {const dependentConstraints = dependents.map((depId) => {const scheduledOp = scheduled.get(depId);- const baseOp = operationMap.get(depId);+ const depOp = operationMap.get(depId);if (!scheduledOp?.startDate) return null;- // Subtract lead time from dependent's start date- // Lead time represents how early the subassembly needs to be ready- // before the parent operation starts- const leadTimeDays = baseOp?.operationLeadTime ?? 0;- if (leadTimeDays > 0) {- const startDate = new Date(scheduledOp.startDate);- return subtractBusinessDays(startDate, leadTimeDays);+ let constraintDate = new Date(scheduledOp.startDate);++ // Operation-level lead time on the consuming (dependent) operation:+ // how early this input must be ready before that operation starts.+ const operationLeadTime = depOp?.operationLeadTime ?? 0;+ if (operationLeadTime > 0) {+ constraintDate = subtractBusinessDays(+ constraintDate,+ operationLeadTime+ );+ }++ // Assembly boundary: this op is a subassembly's last operation+ // feeding a parent make method's operation. Pull its due date back+ // by the subassembly item's manufacturing lead time so the whole+ // subassembly is scheduled to finish that many days early.+ const isAssemblyEdge =+ !!op.jobMakeMethodId &&+ !!depOp?.jobMakeMethodId &&+ op.jobMakeMethodId !== depOp.jobMakeMethodId;+ const assemblyLeadTime = op.assemblyLeadTime ?? 0;+ if (isAssemblyEdge && assemblyLeadTime > 0) {+ constraintDate = subtractBusinessDays(+ constraintDate,+ assemblyLeadTime+ );}- return new Date(scheduledOp.startDate);++ return constraintDate;}).filter((date): date is Date => date !== null);diff --git a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.tsindex cbc2793650..404e205a23 100644--- a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts+++ b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts@@ -117,6 +117,10 @@ export class SchedulingEngine {.orderBy("order").execute()) as BaseOperation[];+ // Enrich each operation with its make method item's manufacturing lead time+ // so the date calculator can pull subassemblies earlier at assembly edges.+ await this.assignAssemblyLeadTimes();+// Load existing dependencies (for reschedule mode)if (this.mode === "reschedule") {const deps = await this.db@@ -163,6 +167,63 @@ export class SchedulingEngine {}}+ /**+ * Populate `assemblyLeadTime` on each loaded operation from the manufacturing+ * lead time (itemReplenishment.leadTime) of the item its make method builds.+ * Used at assembly boundaries in date calculation so a subassembly finishes+ * its lead time (in business days) before the parent operation that consumes it.+ */+ private async assignAssemblyLeadTimes(): Promise<void> {+ const makeMethodIds = [+ ...new Set(+ this.operations+ .map((op) => op.jobMakeMethodId)+ .filter((id): id is string => Boolean(id))+ ),+ ];+ if (makeMethodIds.length === 0) return;++ const makeMethods = await this.db+ .selectFrom("jobMakeMethod")+ .select(["id", "itemId"])+ .where("id", "in", makeMethodIds)+ .execute();++ const itemIds = [+ ...new Set(+ makeMethods+ .map((m) => m.itemId)+ .filter((id): id is string => Boolean(id))+ ),+ ];+ if (itemIds.length === 0) return;++ const replenishments = await this.db+ .selectFrom("itemReplenishment")+ .select(["itemId", "leadTime"])+ .where("itemId", "in", itemIds)+ .execute();++ // NUMERIC columns can come back from pg as strings — coerce to a number.+ const toLeadTimeDays = (value: unknown): number => {+ const n = Number(value ?? 0);+ return Number.isFinite(n) && n > 0 ? n : 0;+ };+ const leadTimeByItemId = new Map(+ replenishments.map((r) => [r.itemId, toLeadTimeDays(r.leadTime)])+ );+ const leadTimeByMakeMethodId = new Map(+ makeMethods.map((m) => [m.id, leadTimeByItemId.get(m.itemId ?? "") ?? 0])+ );++ for (const op of this.operations) {+ if (op.jobMakeMethodId) {+ op.assemblyLeadTime =+ leadTimeByMakeMethodId.get(op.jobMakeMethodId) ?? 0;+ }+ }+ }+/*** Create operation dependencies based on assembly structure.* Loads ALL operations (including Done) to build the complete DAG.diff --git a/packages/database/supabase/functions/lib/scheduling/types.ts b/packages/database/supabase/functions/lib/scheduling/types.tsindex bcb92442e1..859cc44698 100644--- a/packages/database/supabase/functions/lib/scheduling/types.ts+++ b/packages/database/supabase/functions/lib/scheduling/types.ts@@ -36,6 +36,13 @@ export type BaseOperation = {operationQuantity?: number | null;operationType?: OperationType;operationLeadTime?: number;+ /**+ * Manufacturing lead time (in business days) of the make method's item that+ * this operation belongs to. Applied only at assembly boundaries so a+ * subassembly is scheduled to finish this many days before its parent+ * consumes it. Populated by the engine from itemReplenishment.leadTime.+ */+ assemblyLeadTime?: number;priority?: number;processId: string | null;setupTime?: number;