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
feat: sync onshape status to bom
Brad Barbindf73cc09b4a8
diff --git a/apps/erp/app/components/Icons.tsx b/apps/erp/app/components/Icons.tsxindex bae5ec04ee..59168fe773 100644--- a/apps/erp/app/components/Icons.tsx+++ b/apps/erp/app/components/Icons.tsx@@ -1,5 +1,5 @@import type { Database } from "@carbon/database";-import { Badge, cn, HStack } from "@carbon/react";+import { Badge, cn, HStack, Status } from "@carbon/react";import { AiOutlinePartition } from "react-icons/ai";import { FaCodePullRequest } from "react-icons/fa6";import {@@ -140,6 +140,27 @@ function getReplenishmentBadgeColor(type: MethodType, mode: "light" | "dark") {: getColor("orange", mode);}+export const OnshapeStatus = ({+ status,+ className,+}: {+ status: string;+ className?: string;+}) => {+ switch (status) {+ case "In progress":+ return <AlmostDoneIcon className={className} />;+ case "Released":+ return <LuCircleCheck className={cn("text-blue-600", className)} />;+ case "Rejected":+ return <LuCircleX className={cn("text-red-600", className)} />;+ case "Pending":+ return <InProgressStatusIcon className={className} />;+ default:+ return <Status color="gray">{status}</Status>;+ }+};+export function OperationStatusIcon({status,className,diff --git a/apps/erp/app/integrations/onshape/lib/OnshapeSync.tsx b/apps/erp/app/integrations/onshape/lib/OnshapeSync.tsxindex 565cab7516..15ccd1dbcc 100644--- a/apps/erp/app/integrations/onshape/lib/OnshapeSync.tsx+++ b/apps/erp/app/integrations/onshape/lib/OnshapeSync.tsx@@ -24,6 +24,7 @@ import { useFetcher } from "@remix-run/react";import { useEffect, useMemo, useRef, useState } from "react";import { LuChevronRight } from "react-icons/lu";import { MethodIcon } from "~/components";+import { OnshapeStatus } from "~/components/Icons";import { Logo } from "~/integrations/onshape/config";import { methodType } from "~/modules/shared";import type { action as onShapeSyncAction } from "~/routes/api+/integrations.onshape.sync";@@ -45,8 +46,8 @@ interface TreeData {unitOfMeasure: string;replenishmentSystem: string;defaultMethodType: string;- mass: number;level: number;+ data: Record<string, any>;}export const OnshapeSync = ({@@ -423,6 +424,9 @@ export const OnshapeSync = ({<Status color="red">No part ID</Status>)}{!isSynced && partId && <PulsingDot className="mt-0.5" />}+ {row.data["State"] && (+ <OnshapeStatus status={row.data["State"]} />+ )}</div><HStack spacing={1}><Badge className="text-xs" variant="outline">diff --git a/apps/erp/app/integrations/onshape/lib/data.ts b/apps/erp/app/integrations/onshape/lib/data.tsindex 5f935a7656..2f41051ee8 100644--- a/apps/erp/app/integrations/onshape/lib/data.ts+++ b/apps/erp/app/integrations/onshape/lib/data.ts@@ -12,5 +12,6 @@ export const onShapeDataValidator = zquantity: z.number(),replenishmentSystem: z.enum(itemReplenishmentSystems),defaultMethodType: z.enum(methodType),+ data: z.record(z.string(), z.any()),}).array();diff --git a/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx b/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsxindex 57d1e84872..96372d7b89 100644--- a/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx+++ b/apps/erp/app/modules/items/ui/Item/BoMExplorer.tsx@@ -23,6 +23,7 @@ import {LuSearch,} from "react-icons/lu";import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { OnshapeStatus } from "~/components/Icons";import type { FlatTreeItem } from "~/components/TreeView";import { LevelLine, TreeView, useTree } from "~/components/TreeView";import { useIntegrations } from "~/hooks/useIntegrations";@@ -263,20 +264,36 @@ function NodeText({ node }: { node: FlatTreeItem<Method> }) {}function NodeData({ node }: { node: FlatTreeItem<Method> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<HStack spacing={1}><Badge className="text-xs" variant="outline">{node.data.quantity}</Badge>- <Badge variant="secondary">- <MethodItemTypeIcon type={node.data.itemType} />- </Badge>+ {onShapeState ? (+ <OnshapeStatus status={onShapeState} />+ ) : (+ <Badge variant="secondary">+ <MethodItemTypeIcon type={node.data.itemType} />+ </Badge>+ )}</HStack>);}function NodePreview({ node }: { node: FlatTreeItem<Method> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<VStack className="w-full text-sm"><VStack spacing={1}>@@ -340,6 +357,17 @@ function NodePreview({ node }: { node: FlatTreeItem<Method> }) {<span>{node.data.itemType}</span></HStack></VStack>+ {onShapeState && (+ <VStack spacing={1}>+ <span className="text-xs text-muted-foreground font-medium">+ Onshape Status+ </span>+ <HStack className="w-full">+ <OnshapeStatus status={onShapeState} />+ <span>{onShapeState}</span>+ </HStack>+ </VStack>+ )}</VStack>);}diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsxindex 5940e2a329..e632e951ef 100644--- a/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx+++ b/apps/erp/app/modules/production/ui/Jobs/JobBoMExplorer.tsx@@ -10,9 +10,6 @@ import {InputGroup,InputLeftElement,Spinner,- Tooltip,- TooltipContent,- TooltipTrigger,VStack,cn,} from "@carbon/react";@@ -25,9 +22,11 @@ import {LuSearch,} from "react-icons/lu";import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { OnshapeStatus } from "~/components/Icons";import type { FlatTree, FlatTreeItem } from "~/components/TreeView";import { LevelLine, TreeView, useTree } from "~/components/TreeView";import { useOptimisticLocation } from "~/hooks";+import { useIntegrations } from "~/hooks/useIntegrations";import { getLinkToItemDetails } from "~/modules/items/ui/Item/ItemForm";import { useBom } from "~/stores";import { path } from "~/utils/path";@@ -232,36 +231,40 @@ function NodeText({ node }: { node: FlatTreeItem<JobMethod> }) {<span className="font-medium text-sm truncate">{node.data.description || node.data.itemReadableId}</span>- {node.data.revision && node.data.revision !== "0" && (- <Badge variant="outline" className="text-xs">- {node.data.revision}- </Badge>- )}</div>);}function NodeData({ node }: { node: FlatTreeItem<JobMethod> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<HStack spacing={1}><Badge className="text-xs" variant="outline">{node.data.quantity}</Badge>- <Tooltip>- <TooltipTrigger>- <Badge variant="secondary">- <MethodItemTypeIcon type={node.data.itemType} />- </Badge>- </TooltipTrigger>- <TooltipContent side="left">- <span>{node.data.itemType}</span>- </TooltipContent>- </Tooltip>+ {onShapeState ? (+ <OnshapeStatus status={onShapeState} />+ ) : (+ <Badge variant="secondary">+ <MethodItemTypeIcon type={node.data.itemType} />+ </Badge>+ )}</HStack>);}function NodePreview({ node }: { node: FlatTreeItem<JobMethod> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<VStack className="w-full text-sm"><VStack spacing={1}>@@ -323,6 +326,17 @@ function NodePreview({ node }: { node: FlatTreeItem<JobMethod> }) {<span>{node.data.itemType}</span></HStack></VStack>+ {onShapeState && (+ <VStack spacing={1}>+ <span className="text-xs text-muted-foreground font-medium">+ Onshape Status+ </span>+ <HStack className="w-full">+ <OnshapeStatus status={onShapeState} />+ <span>{onShapeState}</span>+ </HStack>+ </VStack>+ )}</VStack>);}diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsxindex 34c28751df..e5a78ddbf7 100644--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteBoMExplorer.tsx@@ -10,9 +10,6 @@ import {InputGroup,InputLeftElement,Spinner,- Tooltip,- TooltipContent,- TooltipTrigger,VStack,cn,} from "@carbon/react";@@ -25,9 +22,11 @@ import {LuSearch,} from "react-icons/lu";import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { OnshapeStatus } from "~/components/Icons";import type { FlatTree, FlatTreeItem } from "~/components/TreeView";import { LevelLine, TreeView, useTree } from "~/components/TreeView";import { useOptimisticLocation } from "~/hooks";+import { useIntegrations } from "~/hooks/useIntegrations";import { getLinkToItemDetails } from "~/modules/items/ui/Item/ItemForm";import { useBom } from "~/stores";import { path } from "~/utils/path";@@ -250,26 +249,35 @@ function NodeText({ node }: { node: FlatTreeItem<QuoteMethod> }) {}function NodeData({ node }: { node: FlatTreeItem<QuoteMethod> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<HStack spacing={1}><Badge className="text-xs" variant="outline">{node.data.quantity}</Badge>- <Tooltip>- <TooltipTrigger>- <Badge variant="secondary">- <MethodItemTypeIcon type={node.data.itemType} />- </Badge>- </TooltipTrigger>- <TooltipContent side="left">- <span>{node.data.itemType}</span>- </TooltipContent>- </Tooltip>+ {onShapeState ? (+ <OnshapeStatus status={onShapeState} />+ ) : (+ <Badge variant="secondary">+ <MethodItemTypeIcon type={node.data.itemType} />+ </Badge>+ )}</HStack>);}function NodePreview({ node }: { node: FlatTreeItem<QuoteMethod> }) {+ const integrations = useIntegrations();+ const onShapeState = integrations.has("onshape")+ ? // @ts-expect-error+ node.data.externalId?.["onshapeData"]?.["State"]+ : null;+return (<VStack className="w-full text-sm"><VStack spacing={1}>@@ -331,6 +339,17 @@ function NodePreview({ node }: { node: FlatTreeItem<QuoteMethod> }) {<span>{node.data.itemType}</span></HStack></VStack>+ {onShapeState && (+ <VStack spacing={1}>+ <span className="text-xs text-muted-foreground font-medium">+ Onshape Status+ </span>+ <HStack className="w-full">+ <OnshapeStatus status={onShapeState} />+ <span>{onShapeState}</span>+ </HStack>+ </VStack>+ )}</VStack>);}diff --git a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.tsindex 6aee7290e0..24c9ce5e3a 100644--- a/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts+++ b/apps/erp/app/routes/api+/integrations.onshape.d.$did.v.$vid.e.$eid.bom.ts@@ -121,8 +121,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {).in("readableIdWithRevision", Array.from(uniquePartNumbers));- console.log({ items: items.data });-itemsMap = new Map(items.data?.map((item) => [item.readableIdWithRevision,@@ -133,8 +131,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {},]));-- console.log({ itemsMap });}const flattenedDataWithMetadata = flattenedData.map((row) => {@@ -161,7 +157,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {}return {- mass: row["Mass"],index: row["Item"],readableId: row["Part number"],revision: row["Revision"],@@ -175,11 +170,10 @@ export async function loader({ request, params }: LoaderFunctionArgs) {defaultMethodType,quantity: row["Quantity"],level: row["Item"].toString().split(".").length,+ data: row,};});- console.log({ flattenedDataWithMetadata });-// Return the transformed data instead of the raw responsereturn json({data: {diff --git a/packages/database/src/types.ts b/packages/database/src/types.tsindex f2c66f8e9c..614ca1d600 100644--- a/packages/database/src/types.ts+++ b/packages/database/src/types.ts@@ -40137,14 +40137,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -40680,14 +40680,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["paymentCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["paymentCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -43476,6 +43476,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_my_claim: {@@ -43588,6 +43589,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_quote_methods_by_method_id: {@@ -43614,6 +43616,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_recent_job_operations_by_employee: {diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.tsindex f2c66f8e9c..614ca1d600 100644--- a/packages/database/supabase/functions/lib/types.ts+++ b/packages/database/supabase/functions/lib/types.ts@@ -40137,14 +40137,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["invoiceCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["invoiceCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -40680,14 +40680,14 @@ export type Database = {Relationships: [{foreignKeyName: "address_countryCode_fkey"- columns: ["customerCountryCode"]+ columns: ["paymentCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]},{foreignKeyName: "address_countryCode_fkey"- columns: ["paymentCountryCode"]+ columns: ["customerCountryCode"]isOneToOne: falsereferencedRelation: "country"referencedColumns: ["alpha2"]@@ -43476,6 +43476,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_my_claim: {@@ -43588,6 +43589,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_quote_methods_by_method_id: {@@ -43614,6 +43616,7 @@ export type Database = {isRoot: booleankit: booleanrevision: string+ externalId: Json}[]}get_recent_job_operations_by_employee: {diff --git a/packages/database/supabase/functions/sync/index.ts b/packages/database/supabase/functions/sync/index.tsindex 32b33d7d97..177c5e7a66 100644--- a/packages/database/supabase/functions/sync/index.ts+++ b/packages/database/supabase/functions/sync/index.ts@@ -18,6 +18,7 @@ const onShapeDataValidator = z.object({quantity: z.number(),replenishmentSystem: z.enum(["Make", "Buy", "Buy and Make"]),defaultMethodType: z.enum(["Make", "Buy", "Pick"]),+ data: z.record(z.string(), z.any()),});const payloadValidator = z.discriminatedUnion("type", [@@ -180,7 +181,20 @@ serve(async (req: Request) => {const isMade = children.length > 0;let itemId = id;- if (!itemId) {+ if (itemId) {+ // Update existing item with Onshape data+ await trx+ .updateTable("item")+ .set({+ externalId: {+ onshapeData: data.data,+ },+ updatedBy: userId,+ updatedAt: new Date().toISOString(),+ })+ .where("id", "=", itemId)+ .execute();+ } else {// Check if we've already created this part in this transactionitemId = newlyCreatedItemsByPartId.get(partId);@@ -198,6 +212,9 @@ serve(async (req: Request) => {replenishmentSystem,defaultMethodType,companyId,+ externalId: {+ onshapeData: data.data,+ },createdBy: userId,}).returning(["id"])diff --git a/packages/database/supabase/migrations/20250523234547_revisions-bom-expolorer.sql b/packages/database/supabase/migrations/20250523234547_revisions-bom-expolorer.sqlindex 2e5fe52c09..faf29331ce 100644--- a/packages/database/supabase/migrations/20250523234547_revisions-bom-expolorer.sql+++ b/packages/database/supabase/migrations/20250523234547_revisions-bom-expolorer.sql@@ -302,7 +302,8 @@ RETURNS TABLE ("order" DOUBLE PRECISION,"isRoot" BOOLEAN,"kit" BOOLEAN,- "revision" TEXT+ "revision" TEXT,+ "externalId" JSONB) AS $$WITH RECURSIVE material AS (SELECT@@ -360,7 +361,8 @@ SELECTmaterial."order",material."isRoot",material."kit",- item."revision"+ item."revision",+ item."externalId"FROM materialINNER JOIN item ON material."itemId" = item.idWHERE material."jobId" = jiddiff --git a/packages/database/supabase/migrations/20250527123150_onshape-status-bom-explorer.sql b/packages/database/supabase/migrations/20250527123150_onshape-status-bom-explorer.sqlnew file mode 100644index 0000000000..7c54d51274--- /dev/null+++ b/packages/database/supabase/migrations/20250527123150_onshape-status-bom-explorer.sql@@ -0,0 +1,375 @@+DROP FUNCTION IF EXISTS get_quote_methods_by_method_id;+CREATE OR REPLACE FUNCTION get_quote_methods_by_method_id(mid TEXT)+RETURNS TABLE (+ "quoteId" TEXT,+ "quoteLineId" TEXT,+ "methodMaterialId" TEXT,+ "quoteMakeMethodId" TEXT,+ "quoteMaterialMakeMethodId" TEXT,+ "itemId" TEXT,+ "itemReadableId" TEXT,+ "description" TEXT,+ "unitOfMeasureCode" TEXT,+ "itemType" TEXT,+ "itemTrackingType" TEXT,+ "quantity" NUMERIC,+ "unitCost" NUMERIC,+ "methodType" "methodType",+ "parentMaterialId" TEXT,+ "order" DOUBLE PRECISION,+ "isRoot" BOOLEAN,+ "kit" BOOLEAN,+ "revision" TEXT,+ "externalId" JSONB+) AS $$+WITH RECURSIVE material AS (+ SELECT+ "quoteId",+ "quoteLineId",+ "id",+ "id" AS "quoteMakeMethodId",+ 'Make'::"methodType" AS "methodType",+ "id" AS "quoteMaterialMakeMethodId",+ "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"+ FROM+ "quoteMakeMethod"+ WHERE+ "id" = mid+ UNION+ SELECT+ child."quoteId",+ child."quoteLineId",+ child."id",+ child."quoteMakeMethodId",+ child."methodType",+ child."quoteMaterialMakeMethodId",+ child."itemId",+ child."itemType",+ child."quantity",+ child."unitCost",+ parent."id" AS "parentMaterialId",+ child."order",+ FALSE AS "isRoot",+ child."kit"+ FROM+ "quoteMaterialWithMakeMethodId" child+ INNER JOIN material parent ON parent."quoteMaterialMakeMethodId" = child."quoteMakeMethodId"+ WHERE parent."methodType" = 'Make'+)+SELECT+ material."quoteId",+ material."quoteLineId",+ material.id as "methodMaterialId",+ material."quoteMakeMethodId",+ material."quoteMaterialMakeMethodId",+ material."itemId",+ item."readableIdWithRevision" AS "itemReadableId",+ item."name" AS "description",+ item."unitOfMeasureCode",+ material."itemType",+ item."itemTrackingType",+ material."quantity",+ material."unitCost",+ material."methodType",+ material."parentMaterialId",+ material."order",+ material."isRoot",+ material."kit",+ item."revision",+ item."externalId"+FROM material+INNER JOIN item ON material."itemId" = item.id+ORDER BY "order"+$$ LANGUAGE sql STABLE;+++DROP FUNCTION get_method_tree;+CREATE OR REPLACE FUNCTION get_method_tree(uid TEXT)+RETURNS TABLE (+ "methodMaterialId" TEXT,+ "makeMethodId" TEXT,+ "materialMakeMethodId" TEXT,+ "itemId" TEXT,+ "itemReadableId" TEXT,+ "itemType" TEXT,+ "description" TEXT,+ "unitOfMeasureCode" TEXT,+ "unitCost" NUMERIC,+ "quantity" NUMERIC,+ "methodType" "methodType",+ "itemTrackingType" TEXT,+ "parentMaterialId" TEXT,+ "order" DOUBLE PRECISION,+ "operationId" TEXT,+ "isRoot" BOOLEAN,+ "kit" BOOLEAN,+ "revision" TEXT,+ "externalId" JSONB+) AS $$+WITH RECURSIVE material AS (+ SELECT+ "id",+ "makeMethodId",+ "methodType",+ "materialMakeMethodId",+ "itemId",+ "itemType",+ "quantity",+ "makeMethodId" AS "parentMaterialId",+ NULL AS "operationId",+ COALESCE("order", 1) AS "order",+ "kit"+ FROM+ "methodMaterial"+ WHERE+ "makeMethodId" = uid+ UNION+ SELECT+ child."id",+ child."makeMethodId",+ child."methodType",+ child."materialMakeMethodId",+ child."itemId",+ child."itemType",+ child."quantity",+ parent."id" AS "parentMaterialId",+ child."methodOperationId" AS "operationId",+ child."order",+ child."kit"+ FROM+ "methodMaterial" child+ INNER JOIN material parent ON parent."materialMakeMethodId" = child."makeMethodId"+)+SELECT+ material.id as "methodMaterialId",+ material."makeMethodId",+ material."materialMakeMethodId",+ material."itemId",+ item."readableIdWithRevision" AS "itemReadableId",+ material."itemType",+ item."name" AS "description",+ item."unitOfMeasureCode",+ cost."unitCost",+ material."quantity",+ material."methodType",+ item."itemTrackingType",+ material."parentMaterialId",+ material."order",+ material."operationId",+ false AS "isRoot",+ material."kit",+ item."revision",+ item."externalId"+FROM material+INNER JOIN item+ ON material."itemId" = item.id+INNER JOIN "itemCost" cost+ ON item.id = cost."itemId"+UNION+SELECT+ mm."id" AS "methodMaterialId",+ NULL AS "makeMethodId",+ mm.id AS "materialMakeMethodId",+ mm."itemId",+ item."readableIdWithRevision" AS "itemReadableId",+ item."type"::text,+ item."name" AS "description",+ item."unitOfMeasureCode",+ cost."unitCost",+ 1 AS "quantity",+ 'Make' AS "methodType",+ item."itemTrackingType",+ NULL AS "parentMaterialId",+ CAST(1 AS DOUBLE PRECISION) AS "order",+ NULL AS "operationId",+ true AS "isRoot",+ false AS "kit",+ item."revision",+ item."externalId"+FROM "makeMethod" mm+INNER JOIN item+ ON mm."itemId" = item.id+INNER JOIN "itemCost" cost+ ON item.id = cost."itemId"+WHERE mm.id = uid+ORDER BY "order"+$$ LANGUAGE sql STABLE;+++DROP FUNCTION IF EXISTS get_quote_methods;+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,+ "methodType" "methodType",+ "parentMaterialId" TEXT,+ "order" DOUBLE PRECISION,+ "isRoot" BOOLEAN,+ "kit" BOOLEAN,+ "revision" TEXT,+ "externalId" JSONB+) AS $$+WITH RECURSIVE material AS (+ SELECT+ "quoteId",+ "quoteLineId",+ "id",+ "id" AS "quoteMakeMethodId",+ 'Make'::"methodType" AS "methodType",+ "id" AS "quoteMaterialMakeMethodId",+ "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"+ 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",+ parent."id" AS "parentMaterialId",+ child."order",+ FALSE AS "isRoot",+ child."kit"+ 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",+ material."itemType",+ material."quantity",+ material."unitCost",+ material."methodType",+ material."parentMaterialId",+ material."order",+ material."isRoot",+ material."kit",+ item."revision",+ item."externalId"+FROM material+INNER JOIN item ON material."itemId" = item.id+WHERE material."quoteId" = qid+ORDER BY "order"+$$ LANGUAGE sql STABLE;++DROP FUNCTION IF EXISTS get_job_method;+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+) AS $$+WITH RECURSIVE material AS (+ SELECT+ "jobId",+ "id",+ "id" AS "jobMakeMethodId",+ 'Make'::"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"+ 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"+ FROM+ "jobMaterialWithMakeMethodId" child+ INNER JOIN material parent ON parent."jobMaterialMakeMethodId" = child."jobMakeMethodId"+ WHERE parent."methodType" = 'Make'+)+SELECT+ material."jobId",+ material.id as "methodMaterialId",+ material."jobMakeMethodId",+ material."jobMaterialMakeMethodId",+ material."itemId",+ item."readableIdWithRevision" AS "itemReadableId",+ item."name" AS "description",+ material."itemType",+ material."quantity",+ material."unitCost",+ material."methodType",+ material."parentMaterialId",+ material."order",+ material."isRoot",+ material."kit",+ item."revision"+FROM material+INNER JOIN item ON material."itemId" = item.id+WHERE material."jobId" = jid+ORDER BY "order"+$$ LANGUAGE sql STABLE;\ No newline at end of file