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.

fix: dependencies for same level operations

Brad Barbinc2a47794cad0
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 8594c18073..40025f7175 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -8650,6 +8650,7 @@ export type Database = {
createdAt: string
createdBy: string
customFields: Json | null
+ dependencies: string[] | null
description: string | null
id: string
jobId: string
@@ -8690,6 +8691,7 @@ export type Database = {
createdAt?: string
createdBy: string
customFields?: Json | null
+ dependencies?: string[] | null
description?: string | null
id?: string
jobId: string
@@ -8730,6 +8732,7 @@ export type Database = {
createdAt?: string
createdBy?: string
customFields?: Json | null
+ dependencies?: string[] | null
description?: string | null
id?: string
jobId?: string
@@ -33472,14 +33475,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -34807,14 +34810,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -36902,14 +36905,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
diff --git a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
index dedc544e48..274de8ec9b 100644
--- a/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
+++ b/packages/database/supabase/functions/lib/scheduling/scheduling-engine.ts
@@ -58,15 +58,72 @@ class SchedulingEngine {
}
async addDependencies(): Promise<void> {
- const operationDependencies: Record<string, string[]> = {};
+ const operationDependencies: Record<string, Set<string>> = {};
+ const makeMethodIds = [
+ ...new Set(this.makeMethodDependencies.map((m) => m.id)),
+ ];
+ const jobMaterial = await this.db
+ .selectFrom("jobMaterialWithMakeMethodId")
+ .selectAll()
+ .where("id", "in", makeMethodIds)
+ .execute();
+
+ const jobMakeMethodToOperationId: Record<string, string | null> = {};
+ jobMaterial.forEach((m) => {
+ if (m.jobMaterialMakeMethodId) {
+ jobMakeMethodToOperationId[m.jobMaterialMakeMethodId] =
+ m.jobOperationId;
+ }
+ });
// Initialize dependencies for all operations
for (const operation of this.operationsToSchedule) {
if (!operation.id) continue;
- operationDependencies[operation.id] = [];
+ operationDependencies[operation.id] = new Set<string>();
+ }
+
+ for await (const makeMethod of this.makeMethodDependencies) {
+ const operations = this.operationsByJobMakeMethodId[makeMethod.id] ?? [];
+ const lastOperation = operations[operations.length - 1];
+
+ if (!lastOperation) continue;
+
+ if (makeMethod.parentId) {
+ const parentOperation = jobMakeMethodToOperationId[makeMethod.parentId];
+ if (parentOperation) {
+ operationDependencies[lastOperation.id!].add(parentOperation);
+ }
+ }
+
+ operations.forEach((op, index) => {
+ op.order = this.getParallelizedOrder(index, op, operations);
+ });
+ operations.sort((a, b) => (a?.order ?? 0) - (b?.order ?? 0));
+
+ let currentOrder = operations[0]?.order ?? 0;
+ let accumulatedDependencies = new Set<string>();
+ operations.forEach((op) => {
+ if (op.order === currentOrder && op.id) {
+ accumulatedDependencies.add(op.id);
+ } else {
+ currentOrder = op.order ?? 0;
+ operationDependencies[op.id!] = accumulatedDependencies;
+ accumulatedDependencies = new Set<string>();
+ }
+ });
}
- console.log({ makeMethodDependencies: this.makeMethodDependencies });
+ if (Object.keys(operationDependencies).length > 0) {
+ for await (const [operationId, dependencies] of Object.entries(
+ operationDependencies
+ )) {
+ await this.db
+ .updateTable("jobOperation")
+ .set({ dependencies: Array.from(dependencies) })
+ .where("id", "=", operationId)
+ .execute();
+ }
+ }
}
async assign() {
@@ -76,6 +133,18 @@ class SchedulingEngine {
);
}
+ getParallelizedOrder(index: number, op: BaseOperation, ops: BaseOperation[]) {
+ if (op?.operationOrder !== "With Previous") return index + 1;
+ // traverse backwards through the list of ops to find the first op that is not "With Previous" and return its index + 1
+ for (let i = index - 1; i >= 0; i--) {
+ if (ops[i].operationOrder !== "With Previous") {
+ return i + 1;
+ }
+ }
+
+ return 1;
+ }
+
async prioritize(
strategy: SchedulingStrategy = SchedulingStrategy.LeastTime
): Promise<void> {
diff --git a/packages/database/supabase/functions/lib/scheduling/types.ts b/packages/database/supabase/functions/lib/scheduling/types.ts
index fae09a44f3..ac034a0cf3 100644
--- a/packages/database/supabase/functions/lib/scheduling/types.ts
+++ b/packages/database/supabase/functions/lib/scheduling/types.ts
@@ -12,6 +12,7 @@ export type BaseOperation = {
laborUnit?: Database["public"]["Enums"]["factor"];
machineTime?: number;
machineUnit?: Database["public"]["Enums"]["factor"];
+ operationOrder?: Database["public"]["Enums"]["methodOperationOrder"];
operationQuantity?: number | null;
operationType?: Database["public"]["Enums"]["operationType"];
priority?: number;
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 8594c18073..40025f7175 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -8650,6 +8650,7 @@ export type Database = {
createdAt: string
createdBy: string
customFields: Json | null
+ dependencies: string[] | null
description: string | null
id: string
jobId: string
@@ -8690,6 +8691,7 @@ export type Database = {
createdAt?: string
createdBy: string
customFields?: Json | null
+ dependencies?: string[] | null
description?: string | null
id?: string
jobId: string
@@ -8730,6 +8732,7 @@ export type Database = {
createdAt?: string
createdBy?: string
customFields?: Json | null
+ dependencies?: string[] | null
description?: string | null
id?: string
jobId?: string
@@ -33472,14 +33475,14 @@ export type Database = {
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["id"]
+ columns: ["supplierLocationId"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
},
{
foreignKeyName: "partner_id_fkey"
- columns: ["supplierLocationId"]
+ columns: ["id"]
isOneToOne: false
referencedRelation: "supplierLocation"
referencedColumns: ["id"]
@@ -34807,14 +34810,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["supplierCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["supplierCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
@@ -36902,14 +36905,14 @@ export type Database = {
Relationships: [
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["paymentCountryCode"]
+ columns: ["customerCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]
},
{
foreignKeyName: "address_countryCode_fkey"
- columns: ["customerCountryCode"]
+ columns: ["paymentCountryCode"]
isOneToOne: false
referencedRelation: "country"
referencedColumns: ["alpha2"]