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.

refactor: remove jobOrderStatus module and integrate its logic into production models and service

naveenkash60bf1821c0a9
diff --git a/apps/erp/app/modules/production/index.ts b/apps/erp/app/modules/production/index.ts
index 2fd079ae3a..ef8f9462f1 100644
--- a/apps/erp/app/modules/production/index.ts
+++ b/apps/erp/app/modules/production/index.ts
@@ -1,4 +1,3 @@
-export * from "./jobOrderStatus";
export * from "./production.models";
export * from "./production.service";
// @ts-ignore
diff --git a/apps/erp/app/modules/production/jobOrderStatus.ts b/apps/erp/app/modules/production/jobOrderStatus.ts
deleted file mode 100644
index b0c6866a39..0000000000
--- a/apps/erp/app/modules/production/jobOrderStatus.ts
+++ /dev/null
@@ -1,223 +0,0 @@
-import type { Database } from "@carbon/database";
-import type {
- JobMaterialPurchaseOrderLine,
- JobMaterialSupplyJobLine
-} from "./types";
-
-export type PurchaseOrderStatus =
- Database["public"]["Enums"]["purchaseOrderStatus"];
-
-export type JobStatus = Database["public"]["Enums"]["jobStatus"];
-
-export type ItemOrderStatus = {
- needsOrder: boolean;
- shortfall: number;
- status: PurchaseOrderStatus | null;
- // Highest-priority active job producing this (manufactured) item, if any. The
- // supply-side counterpart to `status`; lets a planned/in-flight job surface a
- // positive indicator instead of a bare "needs order" dot.
- supplyJobStatus: JobStatus | null;
- // This job's full requirement is met from on-hand stock (after priority
- // allocation) — i.e. the parts are already in inventory, not merely on order.
- // Lets a fulfilled high-priority job show "in stock" while a lower-priority job
- // sharing the same partially-received PO still shows "receiving".
- coveredByOnHand: boolean;
- ordered: number;
- received: number;
-};
-
-export type JobOrderStatusCategory =
- | "needsOrder"
- | "planned"
- | "plannedJob"
- | "awaitingApproval"
- | "onOrder"
- | "received"
- | "inStock";
-
-// Must match the demand window in get_job_quantity_on_hand and the scheduling
-// sequence.
-export const ACTIVE_JOB_STATUSES: Database["public"]["Enums"]["jobStatus"][] = [
- "Planned",
- "Ready",
- "In Progress",
- "Paused"
-];
-
-// Explicit list, not the complement of ACTIVE_JOB_STATUSES — active-but-late
-// states like Overdue still surface indicators.
-const JOB_STATUSES_WITHOUT_ORDER_STATUS = [
- "Draft",
- "Completed",
- "Cancelled",
- "Closed"
-];
-
-export function isJobOrderStatusHidden(
- jobStatus: string | null | undefined
-): boolean {
- return !!jobStatus && JOB_STATUSES_WITHOUT_ORDER_STATUS.includes(jobStatus);
-}
-
-// Highest priority first. Draft, cancelled, closed and rejected are
-// intentionally excluded — a dead PO isn't a real order.
-export const PO_STATUS_PRIORITY: PurchaseOrderStatus[] = [
- "To Receive",
- "To Receive and Invoice",
- "Needs Approval",
- "To Review",
- "Planned",
- "To Invoice",
- "Completed"
-];
-
-type OrderStatusMaterial = {
- itemTrackingType: string | null;
- methodType: string | null;
-};
-
-// Highest priority first — picks the representative supply-job status when an
-// item is produced by more than one active job. In-flight states outrank merely
-// planned ones, mirroring PO_STATUS_PRIORITY.
-export const JOB_SUPPLY_STATUS_PRIORITY: JobStatus[] = [
- "In Progress",
- "Paused",
- "Ready",
- "Planned"
-];
-
-export function getJobMaterialOrderStatus(
- material: OrderStatusMaterial,
- poLines: JobMaterialPurchaseOrderLine[],
- supplyJobLines: JobMaterialSupplyJobLine[],
- shortfall: number,
- coveredByOnHand: boolean
-): ItemOrderStatus {
- const needsOrder =
- material.itemTrackingType !== "Non-Inventory" &&
- material.methodType !== "Make to Order" &&
- shortfall > 0;
-
- const status =
- PO_STATUS_PRIORITY.find((candidate) =>
- poLines.some((line) => line.status === candidate)
- ) ?? null;
-
- const supplyJobStatus =
- JOB_SUPPLY_STATUS_PRIORITY.find((candidate) =>
- supplyJobLines.some((line) => line.status === candidate)
- ) ?? null;
-
- let ordered = 0;
- let received = 0;
- if (status) {
- for (const line of poLines) {
- if (line.status !== status) continue;
- ordered += line.purchaseQuantity ?? 0;
- received += line.quantityReceived ?? 0;
- }
- }
-
- return {
- needsOrder,
- shortfall,
- status,
- supplyJobStatus,
- coveredByOnHand,
- ordered,
- received
- };
-}
-
-// The single source of truth for order-status precedence. Both the status filter
-// and JobOrderStatusBadge derive from this (the badge maps each category to its
-// icon/label), so they can never disagree. Order of the checks below IS the
-// precedence — keep the highest-priority indicator first.
-export function getJobOrderStatusCategory(
- status: ItemOrderStatus | undefined
-): JobOrderStatusCategory | null {
- // A still-unmet, priority-adjusted shortfall outranks the supply indicators —
- // see JobOrderStatusBadge for why.
- if (status?.needsOrder) return "needsOrder";
-
- if (status?.coveredByOnHand) return "inStock";
-
- switch (status?.status) {
- case "Planned":
- return "planned";
- case "Needs Approval":
- case "To Review":
- return "awaitingApproval";
- case "To Receive":
- case "To Receive and Invoice": {
- const fraction =
- status.ordered > 0 ? status.received / status.ordered : 0;
- if (fraction < 1) {
- return status.received > 0 ? "received" : "onOrder";
- }
- break;
- }
- }
-
- if (status?.supplyJobStatus) return "plannedJob";
- return null;
-}
-
-type OrderStatusBuildMaterial = OrderStatusMaterial & {
- id: string | null;
- jobMaterialItemId: string | null;
-};
-
-// One status per material id (= the tree node's methodMaterialId) — the single
-// source the table, tree, and filter all read from.
-export type ItemShortfall = {
- shortfall: number;
- // True when this job's whole requirement is met from on-hand stock (no
- // reliance on incoming PO/job supply) after priority allocation.
- coveredByOnHand: boolean;
-};
-
-export function getJobOrderStatusByMaterial(
- materials: OrderStatusBuildMaterial[],
- purchaseOrderLines: JobMaterialPurchaseOrderLine[],
- supplyJobLines: JobMaterialSupplyJobLine[],
- shortfallByItemId: Record<string, ItemShortfall>
-): Record<string, ItemOrderStatus> {
- const linesByItemId = new Map<string, JobMaterialPurchaseOrderLine[]>();
- for (const line of purchaseOrderLines) {
- if (!line.itemId) continue;
- const lines = linesByItemId.get(line.itemId) ?? [];
- lines.push(line);
- linesByItemId.set(line.itemId, lines);
- }
-
- const jobLinesByItemId = new Map<string, JobMaterialSupplyJobLine[]>();
- for (const line of supplyJobLines) {
- if (!line.itemId) continue;
- const lines = jobLinesByItemId.get(line.itemId) ?? [];
- lines.push(line);
- jobLinesByItemId.set(line.itemId, lines);
- }
-
- const byMaterialId: Record<string, ItemOrderStatus> = {};
- for (const material of materials) {
- if (!material.id) continue;
- const poLines = material.jobMaterialItemId
- ? (linesByItemId.get(material.jobMaterialItemId) ?? [])
- : [];
- const jobLines = material.jobMaterialItemId
- ? (jobLinesByItemId.get(material.jobMaterialItemId) ?? [])
- : [];
- const itemShortfall = material.jobMaterialItemId
- ? shortfallByItemId[material.jobMaterialItemId]
- : undefined;
- byMaterialId[material.id] = getJobMaterialOrderStatus(
- material,
- poLines,
- jobLines,
- itemShortfall?.shortfall ?? 0,
- itemShortfall?.coveredByOnHand ?? false
- );
- }
- return byMaterialId;
-}
diff --git a/apps/erp/app/modules/production/production.models.ts b/apps/erp/app/modules/production/production.models.ts
index 8961470758..92cd02072e 100644
--- a/apps/erp/app/modules/production/production.models.ts
+++ b/apps/erp/app/modules/production/production.models.ts
@@ -1,3 +1,4 @@
+import type { Database } from "@carbon/database";
import { z } from "zod";
import { zfd } from "zod-form-data";
import {
@@ -8,6 +9,12 @@ import {
procedureStepType,
standardFactorType
} from "../shared";
+import type {
+ ItemOrderStatus,
+ JobOrderStatusCategory,
+ JobStatus,
+ PurchaseOrderStatus
+} from "./types";
export const KPIs = [
{
@@ -1012,3 +1019,83 @@ export const demandProjectionValidator = z.object({
])
)
});
+
+// Must match the demand window in get_job_quantity_on_hand and the scheduling
+// sequence.
+export const ACTIVE_JOB_STATUSES: Database["public"]["Enums"]["jobStatus"][] = [
+ "Planned",
+ "Ready",
+ "In Progress",
+ "Paused"
+];
+
+// Explicit list, not the complement of ACTIVE_JOB_STATUSES — active-but-late
+// states like Overdue still surface indicators.
+const JOB_STATUSES_WITHOUT_ORDER_STATUS = [
+ "Draft",
+ "Completed",
+ "Cancelled",
+ "Closed"
+];
+
+export function isJobOrderStatusHidden(
+ jobStatus: string | null | undefined
+): boolean {
+ return !!jobStatus && JOB_STATUSES_WITHOUT_ORDER_STATUS.includes(jobStatus);
+}
+
+// Highest priority first. Draft, cancelled, closed and rejected are
+// intentionally excluded — a dead PO isn't a real order.
+export const PO_STATUS_PRIORITY: PurchaseOrderStatus[] = [
+ "To Receive",
+ "To Receive and Invoice",
+ "Needs Approval",
+ "To Review",
+ "Planned",
+ "To Invoice",
+ "Completed"
+];
+
+// Highest priority first — picks the representative supply-job status when an
+// item is produced by more than one active job. In-flight states outrank merely
+// planned ones, mirroring PO_STATUS_PRIORITY.
+export const JOB_SUPPLY_STATUS_PRIORITY: JobStatus[] = [
+ "In Progress",
+ "Paused",
+ "Ready",
+ "Planned"
+];
+
+// The single source of truth for order-status precedence. Both the status filter
+// and JobOrderStatusBadge derive from this (the badge maps each category to its
+// icon/label), so they can never disagree. Order of the checks below IS the
+// precedence — keep the highest-priority indicator first.
+export function getJobOrderStatusCategory(
+ status: ItemOrderStatus | undefined
+): JobOrderStatusCategory | null {
+ // A still-unmet, priority-adjusted shortfall outranks the supply indicators —
+ // see JobOrderStatusBadge for why.
+ if (status?.needsOrder) return "needsOrder";
+
+ if (status?.coveredByOnHand) return "inStock";
+
+ switch (status?.status) {
+ case "Planned":
+ return "planned";
+ case "Needs Approval":
+ case "To Review":
+ return "awaitingApproval";
+ case "To Receive":
+ case "To Receive and Invoice": {
+ const fraction =
+ status.ordered > 0 ? status.received / status.ordered : 0;
+ if (fraction < 1) {
+ return status.received > 0 ? "received" : "onOrder";
+ }
+ break;
+ }
+ }
+
+ if (status?.supplyJobStatus) return "plannedJob";
+ return null;
+}
diff --git a/apps/erp/app/modules/production/production.service.ts b/apps/erp/app/modules/production/production.service.ts
index 70aeae7d7e..6003ba080e 100644
--- a/apps/erp/app/modules/production/production.service.ts
+++ b/apps/erp/app/modules/production/production.service.ts
@@ -16,13 +16,6 @@ import type {
operationStepValidator,
operationToolValidator
} from "../shared";
-import {
- ACTIVE_JOB_STATUSES,
- getJobOrderStatusByMaterial,
- type ItemOrderStatus,
- type ItemShortfall,
- isJobOrderStatusHidden
-} from "./jobOrderStatus";
import type {
deadlineTypes,
failureModeValidator,
@@ -45,7 +38,15 @@ import type {
productionQuantityValidator,
scrapReasonValidator
} from "./production.models";
+import {
+ ACTIVE_JOB_STATUSES,
+ isJobOrderStatusHidden,
+ JOB_SUPPLY_STATUS_PRIORITY,
+ PO_STATUS_PRIORITY
+} from "./production.models";
import type {
+ ItemOrderStatus,
+ ItemShortfall,
Job,
JobMaterialPurchaseOrderLine,
JobMaterialSupplyJobLine
@@ -1099,6 +1100,108 @@ export async function getJobMaterialShortfallByItem(
return shortfallByItem;
}
+type OrderStatusMaterial = {
+ itemTrackingType: string | null;
+ methodType: string | null;
+};
+
+type OrderStatusBuildMaterial = OrderStatusMaterial & {
+ id: string | null;
+ jobMaterialItemId: string | null;
+};
+
+// Builds one material's ItemOrderStatus from its PO lines, supply jobs, and
+// priority-adjusted shortfall. Pure — all DB reads happen in the callers.
+function getJobMaterialOrderStatus(
+ material: OrderStatusMaterial,
+ poLines: JobMaterialPurchaseOrderLine[],
+ supplyJobLines: JobMaterialSupplyJobLine[],
+ shortfall: number,
+ coveredByOnHand: boolean
+): ItemOrderStatus {
+ const needsOrder =
+ material.itemTrackingType !== "Non-Inventory" &&
+ material.methodType !== "Make to Order" &&
+ shortfall > 0;
+
+ const status =
+ PO_STATUS_PRIORITY.find((candidate) =>
+ poLines.some((line) => line.status === candidate)
+ ) ?? null;
+
+ const supplyJobStatus =
+ JOB_SUPPLY_STATUS_PRIORITY.find((candidate) =>
+ supplyJobLines.some((line) => line.status === candidate)
+ ) ?? null;
+
+ let ordered = 0;
+ let received = 0;
+ if (status) {
+ for (const line of poLines) {
+ if (line.status !== status) continue;
+ ordered += line.purchaseQuantity ?? 0;
+ received += line.quantityReceived ?? 0;
+ }
+ }
+
+ return {
+ needsOrder,
+ shortfall,
+ status,
+ supplyJobStatus,
+ coveredByOnHand,
+ ordered,
+ received
+ };
+}
+
+// One ItemOrderStatus per material id (= the tree node's methodMaterialId) — the
+// single source the table, tree, and filter all read from.
+function getJobOrderStatusByMaterial(
+ materials: OrderStatusBuildMaterial[],
+ purchaseOrderLines: JobMaterialPurchaseOrderLine[],
+ supplyJobLines: JobMaterialSupplyJobLine[],
+ shortfallByItemId: Record<string, ItemShortfall>
+): Record<string, ItemOrderStatus> {
+ const linesByItemId = new Map<string, JobMaterialPurchaseOrderLine[]>();
+ for (const line of purchaseOrderLines) {
+ if (!line.itemId) continue;
+ const lines = linesByItemId.get(line.itemId) ?? [];
+ lines.push(line);
+ linesByItemId.set(line.itemId, lines);
+ }
+
+ const jobLinesByItemId = new Map<string, JobMaterialSupplyJobLine[]>();
+ for (const line of supplyJobLines) {
+ if (!line.itemId) continue;
+ const lines = jobLinesByItemId.get(line.itemId) ?? [];
+ lines.push(line);
+ jobLinesByItemId.set(line.itemId, lines);
+ }
+
+ const byMaterialId: Record<string, ItemOrderStatus> = {};
+ for (const material of materials) {
+ if (!material.id) continue;
+ const poLines = material.jobMaterialItemId
+ ? (linesByItemId.get(material.jobMaterialItemId) ?? [])
+ : [];
+ const jobLines = material.jobMaterialItemId
+ ? (jobLinesByItemId.get(material.jobMaterialItemId) ?? [])
+ : [];
+ const itemShortfall = material.jobMaterialItemId
+ ? shortfallByItemId[material.jobMaterialItemId]
+ : undefined;
+ byMaterialId[material.id] = getJobMaterialOrderStatus(
+ material,
+ poLines,
+ jobLines,
+ itemShortfall?.shortfall ?? 0,
+ itemShortfall?.coveredByOnHand ?? false
+ );
+ }
+ return byMaterialId;
+}
+
// One status per material id for a job — the single source the table and tree
// both consume. Empty for jobs that show no indicators.
export async function getJobOrderStatusMap(
diff --git a/apps/erp/app/modules/production/types.ts b/apps/erp/app/modules/production/types.ts
index 030ebe6a13..f522c932db 100644
--- a/apps/erp/app/modules/production/types.ts
+++ b/apps/erp/app/modules/production/types.ts
@@ -117,6 +117,35 @@ export type JobMaterialSupplyJobLine = {
status: Database["public"]["Enums"]["jobStatus"] | null;
};
+export type PurchaseOrderStatus =
+ Database["public"]["Enums"]["purchaseOrderStatus"];
+
+export type JobStatus = Database["public"]["Enums"]["jobStatus"];
+
+export type ItemOrderStatus = {
+ needsOrder: boolean;
+ shortfall: number;
+ status: PurchaseOrderStatus | null;
+ supplyJobStatus: JobStatus | null;
+ coveredByOnHand: boolean;
+ ordered: number;
+ received: number;
+};
+
+export type JobOrderStatusCategory =
+ | "needsOrder"
+ | "planned"
+ | "plannedJob"
+ | "awaitingApproval"
+ | "onOrder"
+ | "received"
+ | "inStock";
+
+export type ItemShortfall = {
+ shortfall: number;
+ coveredByOnHand: boolean;
+};
+
export type ProductionEvent = NonNullable<
Awaited<ReturnType<typeof getProductionEvents>>["data"]
>[number];
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
index f395ed4fd6..3e6e69eb97 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx
@@ -56,8 +56,8 @@ import { useIntegrations } from "~/hooks/useIntegrations";
import { getLinkToItemDetails } from "~/modules/items/ui/Item/ItemForm";
import { generateBomIds } from "~/utils/bom";
import { path } from "~/utils/path";
-import type { ItemOrderStatus } from "../../jobOrderStatus";
import type { JobMethod } from "../../production.service";
+import type { ItemOrderStatus } from "../../types";
import { JobOrderStatusBadge } from "./JobOrderStatus";
// Keyed by material id (= the tree node's methodMaterialId); built by the loader.
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobMaterialsTable.tsx b/apps/erp/app/modules/production/ui/Jobs/JobMaterialsTable.tsx
index 24500c548b..c007292148 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobMaterialsTable.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobMaterialsTable.tsx
@@ -39,10 +39,9 @@ import { type Item, useItems } from "~/stores";
import { path } from "~/utils/path";
import {
ACTIVE_JOB_STATUSES,
- getJobOrderStatusCategory,
- type ItemOrderStatus
-} from "../../jobOrderStatus";
-import type { Job, JobMaterial } from "../../types";
+ getJobOrderStatusCategory
+} from "../../production.models";
+import type { ItemOrderStatus, Job, JobMaterial } from "../../types";
import { JobOrderStatusBadge } from "./JobOrderStatus";
type JobMaterialsTableProps = {
diff --git a/apps/erp/app/modules/production/ui/Jobs/JobOrderStatus.tsx b/apps/erp/app/modules/production/ui/Jobs/JobOrderStatus.tsx
index fd429c2643..2898dce8f4 100644
--- a/apps/erp/app/modules/production/ui/Jobs/JobOrderStatus.tsx
+++ b/apps/erp/app/modules/production/ui/Jobs/JobOrderStatus.tsx
@@ -5,10 +5,8 @@ import { LuCircleAlert, LuCircleCheck, LuClock, LuStamp } from "react-icons/lu";
import { AlmostDoneIcon } from "~/assets/icons/AlmostDoneIcon";
import { InProgressStatusIcon } from "~/assets/icons/InProgressStatusIcon";
import { TodoStatusIcon } from "~/assets/icons/TodoStatusIcon";
-import {
- getJobOrderStatusCategory,
- type ItemOrderStatus
-} from "../../jobOrderStatus";
+import { getJobOrderStatusCategory } from "../../production.models";
+import type { ItemOrderStatus } from "../../types";
// Presentation only — precedence lives in getJobOrderStatusCategory (shared with
// the status filter); this maps the resolved category to an icon + label.
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
index 8b853065c5..83c933d8ea 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
@@ -1,5 +1,5 @@
{
- "generated": "2026-06-24T06:20:49.542Z",
+ "generated": "2026-06-24T14:25:29.083Z",
"totalTools": 1214,
"modules": 15,
"tools": [