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: service jobs show their real item type and never read "Shipped" (#1923)

Sid0888d1ca7129
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
index 5b1d46c0a9..8bafe0e456 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderLineJobs.tsx
@@ -158,7 +158,10 @@ export function SalesOrderLineJobs({
index === jobs.length - 1 && "border-b-0"
)}
>
- <SalesOrderJobItem job={job as SalesOrderJob} />
+ <SalesOrderJobItem
+ job={job as SalesOrderJob}
+ isService={line.salesOrderLineType === "Service"}
+ />
</div>
))}
</div>
@@ -285,7 +288,14 @@ export function SalesOrderLineJobs({
);
}
-export function SalesOrderJobItem({ job }: { job: SalesOrderJob }) {
+export function SalesOrderJobItem({
+ job,
+ isService = false
+}: {
+ job: SalesOrderJob;
+ // A Service job's output is never shipped, so it has no shipped count.
+ isService?: boolean;
+}) {
const disclosure = useDisclosure();
const permissions = usePermissions();
const releaseModal = useDisclosure();
@@ -336,12 +346,14 @@ export function SalesOrderJobItem({ job }: { job: SalesOrderJob }) {
{job.quantityComplete ?? 0}/{job.quantity ?? 0}
</p>
</div>
- <div>
- <label className="text-xs text-muted-foreground">Shipped</label>
- <p className="text-sm">
- {job.quantityShipped ?? 0}/{job.quantity ?? 0}
- </p>
- </div>
+ {!isService && (
+ <div>
+ <label className="text-xs text-muted-foreground">Shipped</label>
+ <p className="text-sm">
+ {job.quantityShipped ?? 0}/{job.quantity ?? 0}
+ </p>
+ </div>
+ )}
</HStack>
<HStack>
<Button
diff --git a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
index ec8a5eb27c..a1c87ee760 100644
--- a/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
+++ b/apps/erp/app/modules/sales/ui/SalesOrder/SalesOrderSummary.tsx
@@ -458,6 +458,7 @@ function LineItems({
: line.itemReadableId;
const description = distinctItemText(title, line.description);
+ const isService = line.salesOrderLineType === "Service";
const { jobLabel, jobVariant, jobs } = getSalesOrderJobStatus(
// @ts-expect-error TS2345 - TODO: fix type
salesOrder?.jobs as SalesOrderJob[] | undefined,
@@ -755,7 +756,7 @@ function LineItems({
)}
>
{/* @ts-expect-error TS2739 */}
- <SalesOrderJobItem job={job} />
+ <SalesOrderJobItem job={job} isService={isService} />
</div>
))}
</div>
diff --git a/packages/database/supabase/migrations/20261014190935_method-tree-root-item-type.sql b/packages/database/supabase/migrations/20261014190935_method-tree-root-item-type.sql
new file mode 100644
index 0000000000..9d4cd43331
--- /dev/null
+++ b/packages/database/supabase/migrations/20261014190935_method-tree-root-item-type.sql
@@ -0,0 +1,297 @@
+-- The top node of a job or quote method tree is the job's (or quote line's)
+-- own item, but these functions labelled it 'Part' whatever its type. The
+-- method explorer shows that type and builds its "open item" link from it, so
+-- a Service or Tool job read "Part" and the link went to the part page, which
+-- found no part and fell back to the parts list. The root now takes the type
+-- of the item the functions already join; every other row is unchanged.
+-- Return types are unchanged, so CREATE OR REPLACE is enough.
+
+CREATE OR REPLACE FUNCTION get_job_method(jid TEXT)
+RETURNS TABLE (
+ "jobId" TEXT,
+ "methodMaterialId" TEXT,
+ "jobMakeMethodId" TEXT,
+ "jobMaterialMakeMethodId" TEXT,
+ "itemId" TEXT,
+ "itemReadableId" TEXT,
+ "description" TEXT,
+ "itemType" TEXT,
+ "quantity" NUMERIC,
+ "unitCost" NUMERIC,
+ "methodType" "methodType",
+ "parentMaterialId" TEXT,
+ "order" DOUBLE PRECISION,
+ "isRoot" BOOLEAN,
+ "kit" BOOLEAN,
+ "revision" TEXT,
+ "version" NUMERIC,
+ "storageUnitId" TEXT,
+ "replenishmentSystem" "itemReplenishmentSystem"
+) AS $$
+WITH RECURSIVE material AS (
+ SELECT
+ "jobId",
+ "id",
+ "id" AS "jobMakeMethodId",
+ 'Make to Order'::"methodType" AS "methodType",
+ "id" AS "jobMaterialMakeMethodId",
+ "itemId",
+ 'Part' AS "itemType",
+ 1::NUMERIC AS "quantity",
+ 0::NUMERIC AS "unitCost",
+ "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order",
+ TRUE AS "isRoot",
+ FALSE AS "kit",
+ "version",
+ NULL::TEXT AS "storageUnitId"
+ FROM
+ "jobMakeMethod"
+ WHERE
+ "jobId" = jid
+ AND "parentMaterialId" IS NULL
+ UNION
+ SELECT
+ child."jobId",
+ child."id",
+ child."jobMakeMethodId",
+ child."methodType",
+ child."jobMaterialMakeMethodId",
+ child."itemId",
+ child."itemType",
+ child."quantity",
+ child."unitCost",
+ parent."id" AS "parentMaterialId",
+ child."order",
+ FALSE AS "isRoot",
+ child."kit",
+ child."version",
+ child."storageUnitId"
+ FROM
+ "jobMaterialWithMakeMethodId" child
+ INNER JOIN material parent ON parent."jobMaterialMakeMethodId" = child."jobMakeMethodId"
+ WHERE parent."methodType" = 'Make to Order'
+)
+SELECT
+ material."jobId",
+ material.id as "methodMaterialId",
+ material."jobMakeMethodId",
+ material."jobMaterialMakeMethodId",
+ material."itemId",
+ item."readableIdWithRevision" AS "itemReadableId",
+ item."name" AS "description",
+ CASE WHEN material."isRoot" THEN item."type"::TEXT ELSE material."itemType" END AS "itemType",
+ material."quantity",
+ material."unitCost",
+ material."methodType",
+ material."parentMaterialId",
+ material."order",
+ material."isRoot",
+ material."kit",
+ item."revision",
+ material."version",
+ material."storageUnitId",
+ item."replenishmentSystem"
+FROM material
+INNER JOIN item ON material."itemId" = item.id
+WHERE material."jobId" = jid
+ORDER BY "order"
+$$ LANGUAGE sql STABLE;
+
+CREATE OR REPLACE FUNCTION get_quote_methods(qid TEXT)
+RETURNS TABLE (
+ "quoteId" TEXT,
+ "quoteLineId" TEXT,
+ "methodMaterialId" TEXT,
+ "quoteMakeMethodId" TEXT,
+ "quoteMaterialMakeMethodId" TEXT,
+ "itemId" TEXT,
+ "itemReadableId" TEXT,
+ "description" TEXT,
+ "itemType" TEXT,
+ "quantity" NUMERIC,
+ "unitCost" NUMERIC,
+ "unitCostSource" TEXT,
+ "methodType" "methodType",
+ "parentMaterialId" TEXT,
+ "order" DOUBLE PRECISION,
+ "isRoot" BOOLEAN,
+ "kit" BOOLEAN,
+ "revision" TEXT,
+ "externalId" JSONB,
+ "version" NUMERIC,
+ "storageUnitId" TEXT,
+ "replenishmentSystem" "itemReplenishmentSystem"
+) AS $$
+WITH RECURSIVE material AS (
+ SELECT
+ "quoteId",
+ "quoteLineId",
+ "id",
+ "id" AS "quoteMakeMethodId",
+ 'Make to Order'::"methodType" AS "methodType",
+ "id" AS "quoteMaterialMakeMethodId",
+ "itemId",
+ 'Part' AS "itemType",
+ 1::NUMERIC AS "quantity",
+ 0::NUMERIC AS "unitCost",
+ 'system'::TEXT AS "unitCostSource",
+ "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order",
+ TRUE AS "isRoot",
+ FALSE AS "kit",
+ "version",
+ NULL::TEXT AS "storageUnitId"
+ FROM
+ "quoteMakeMethod"
+ WHERE
+ "quoteId" = qid
+ AND "parentMaterialId" IS NULL
+ UNION
+ SELECT
+ child."quoteId",
+ child."quoteLineId",
+ child."id",
+ child."quoteMakeMethodId",
+ child."methodType",
+ child."quoteMaterialMakeMethodId",
+ child."itemId",
+ child."itemType",
+ child."quantity",
+ child."unitCost",
+ child."unitCostSource",
+ parent."id" AS "parentMaterialId",
+ child."order",
+ FALSE AS "isRoot",
+ child."kit",
+ child."version",
+ child."storageUnitId"
+ FROM
+ "quoteMaterialWithMakeMethodId" child
+ INNER JOIN material parent ON parent."quoteMaterialMakeMethodId" = child."quoteMakeMethodId"
+)
+SELECT
+ material."quoteId",
+ material."quoteLineId",
+ material.id as "methodMaterialId",
+ material."quoteMakeMethodId",
+ material."quoteMaterialMakeMethodId",
+ material."itemId",
+ item."readableIdWithRevision" AS "itemReadableId",
+ item."name" AS "description",
+ CASE WHEN material."isRoot" THEN item."type"::TEXT ELSE material."itemType" END AS "itemType",
+ material."quantity",
+ material."unitCost",
+ material."unitCostSource",
+ material."methodType",
+ material."parentMaterialId",
+ material."order",
+ material."isRoot",
+ material."kit",
+ item."revision",
+ (
+ SELECT COALESCE(
+ jsonb_object_agg(
+ eim."integration",
+ CASE
+ WHEN eim."metadata" IS NOT NULL THEN eim."metadata"
+ ELSE to_jsonb(eim."externalId")
+ END
+ ) FILTER (WHERE eim."externalId" IS NOT NULL),
+ '{}'::jsonb
+ )
+ FROM "externalIntegrationMapping" eim
+ WHERE eim."entityType" = 'item' AND eim."entityId" = item.id
+ ) AS "externalId",
+ material."version",
+ material."storageUnitId",
+ item."replenishmentSystem"
+FROM material
+INNER JOIN item ON material."itemId" = item.id
+WHERE material."quoteId" = qid
+ORDER BY "order"
+$$ LANGUAGE sql STABLE;
+
+CREATE OR REPLACE FUNCTION get_job_methods_by_method_id(mid TEXT)
+RETURNS TABLE (
+ "jobId" TEXT,
+ "methodMaterialId" TEXT,
+ "jobMakeMethodId" TEXT,
+ "jobMaterialMakeMethodId" TEXT,
+ "itemId" TEXT,
+ "itemReadableId" TEXT,
+ "description" TEXT,
+ "unitOfMeasureCode" TEXT,
+ "itemType" TEXT,
+ "quantity" NUMERIC,
+ "unitCost" NUMERIC,
+ "methodType" "methodType",
+ "parentMaterialId" TEXT,
+ "order" DOUBLE PRECISION,
+ "kit" BOOLEAN,
+ "isRoot" BOOLEAN,
+ "storageUnitId" TEXT
+) AS $$
+WITH RECURSIVE material AS (
+ SELECT
+ "jobId",
+ "id",
+ "id" AS "jobMakeMethodId",
+ 'Make to Order'::"methodType" AS "methodType",
+ "id" AS "jobMaterialMakeMethodId",
+ "itemId",
+ 'Part' AS "itemType",
+ 1::NUMERIC AS "quantity",
+ 0::NUMERIC AS "unitCost",
+ "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order",
+ FALSE AS "kit",
+ TRUE AS "isRoot",
+ NULL::TEXT AS "storageUnitId"
+ FROM
+ "jobMakeMethod"
+ WHERE
+ "id" = mid
+ UNION
+ SELECT
+ child."jobId",
+ child."id",
+ child."jobMakeMethodId",
+ child."methodType",
+ child."jobMaterialMakeMethodId",
+ child."itemId",
+ child."itemType",
+ child."quantity",
+ child."unitCost",
+ parent."id" AS "parentMaterialId",
+ child."order",
+ child."kit",
+ FALSE AS "isRoot",
+ child."storageUnitId"
+ FROM
+ "jobMaterialWithMakeMethodId" child
+ INNER JOIN material parent ON parent."jobMaterialMakeMethodId" = child."jobMakeMethodId"
+ WHERE parent."methodType" = 'Make to Order'
+)
+SELECT
+ material."jobId",
+ material.id as "methodMaterialId",
+ material."jobMakeMethodId",
+ material."jobMaterialMakeMethodId",
+ material."itemId",
+ item."readableId" AS "itemReadableId",
+ item."name" AS "description",
+ item."unitOfMeasureCode",
+ CASE WHEN material."isRoot" THEN item."type"::TEXT ELSE material."itemType" END AS "itemType",
+ material."quantity",
+ material."unitCost",
+ material."methodType",
+ material."parentMaterialId",
+ material."order",
+ material."kit",
+ material."isRoot",
+ material."storageUnitId"
+FROM material
+INNER JOIN item ON material."itemId" = item.id
+ORDER BY "order"
+$$ LANGUAGE sql STABLE;
diff --git a/packages/utils/src/status.test.ts b/packages/utils/src/status.test.ts
new file mode 100644
index 0000000000..af108c148e
--- /dev/null
+++ b/packages/utils/src/status.test.ts
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// 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.
+
+import { describe, expect, it } from "vitest";
+import { getSalesOrderJobStatus } from "./status";
+
+type Line = Parameters<typeof getSalesOrderJobStatus>[1];
+type Job = NonNullable<Parameters<typeof getSalesOrderJobStatus>[0]>[number];
+
+const line = (overrides: Partial<Line>): Line => ({
+ id: "line",
+ salesOrderLineType: "Part",
+ methodType: "Make to Order",
+ saleQuantity: 2,
+ quantitySent: 0,
+ sentComplete: false,
+ invoicedComplete: false,
+ ...overrides
+});
+
+const job = (overrides: Partial<Job>): Job => ({
+ id: "job",
+ jobId: "J000001",
+ salesOrderLineId: "line",
+ productionQuantity: 2,
+ quantityComplete: 2,
+ status: "Completed",
+ dueDate: null,
+ ...overrides
+});
+
+const label = (l: Line, jobs: Job[]) =>
+ getSalesOrderJobStatus(jobs, l).jobLabel;
+
+describe("getSalesOrderJobStatus", () => {
+ it("reads Shipped once a part line's completed jobs have shipped", () => {
+ expect(
+ label(line({ quantitySent: 2, sentComplete: true }), [job({})])
+ ).toBe("Shipped");
+ expect(label(line({ quantitySent: 1 }), [job({})])).toBe("Completed");
+ expect(
+ label(line({ quantitySent: 1 }), [
+ job({ status: "In Progress", quantityComplete: 1 })
+ ])
+ ).toBe("Partially Shipped");
+ });
+
+ // Completing a service job fills quantitySent / sentComplete, but nothing
+ // was shipped, so the line must never claim it was.
+ it("never reads Shipped for a Service line", () => {
+ const service = { salesOrderLineType: "Service" as const };
+ expect(
+ label(line({ ...service, quantitySent: 2, sentComplete: true }), [
+ job({})
+ ])
+ ).toBe("Completed");
+ expect(
+ label(line({ ...service, quantitySent: 1 }), [
+ job({ status: "In Progress", quantityComplete: 1 })
+ ])
+ ).toBe("In Progress");
+ });
+});
diff --git a/packages/utils/src/status.ts b/packages/utils/src/status.ts
index 5fb10ca5aa..d3fc9db4ba 100644
--- a/packages/utils/src/status.ts
+++ b/packages/utils/src/status.ts
@@ -196,8 +196,13 @@ export const getSalesOrderJobStatus = (
const hasAnyQuantityReleased = totalReleased > 0;
const isCompleted =
hasEnoughJobsToCoverQuantity && hasEnoughCompletedToCoverQuantity;
+ // A Service line never ships: completing its jobs fulfills it, and the
+ // database fills quantitySent / sentComplete from those jobs. Reading that as
+ // "Shipped" told people a shipment had gone out when none can.
+ const isService = line.salesOrderLineType === "Service";
const quantitySent = line.quantitySent ?? 0;
- const isPartiallyShipped = quantitySent > 0 && quantitySent < saleQuantity;
+ const isPartiallyShipped =
+ !isService && quantitySent > 0 && quantitySent < saleQuantity;
let jobVariant: "green" | "red" | "orange";
let jobLabel:
@@ -208,7 +213,7 @@ export const getSalesOrderJobStatus = (
| "Shipped"
| "Partially Shipped";
- if (isCompleted && line.sentComplete) {
+ if (isCompleted && line.sentComplete && !isService) {
jobLabel = "Shipped";
jobVariant = "green";
} else if (isCompleted) {