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: add tracking type to materials
Brad Barbin71cf89dc754e
diff --git a/apps/erp/app/modules/items/items.service.ts b/apps/erp/app/modules/items/items.service.tsindex 77646689e8..410ad96bb7 100644--- a/apps/erp/app/modules/items/items.service.ts+++ b/apps/erp/app/modules/items/items.service.ts@@ -844,7 +844,7 @@ export async function getMethodMaterialsByMakeMethod() {return client.from("methodMaterial")- .select("*, item(name)")+ .select("*, item(name, itemTrackingType)").eq("makeMethodId", makeMethodId).order("order", { ascending: true });}diff --git a/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx b/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsxindex bd9669d37a..df9d3e704e 100644--- a/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx+++ b/apps/erp/app/modules/items/ui/Item/BillOfMaterial.tsx@@ -13,6 +13,9 @@ import {HStack,IconButton,toast,+ Tooltip,+ TooltipContent,+ TooltipTrigger,useDisclosure,useThrottle,VStack,@@ -24,7 +27,7 @@ import { useCallback, useEffect, useState } from "react";import { flushSync } from "react-dom";import { LuSettings2, LuSquareFunction, LuX } from "react-icons/lu";import type { z } from "zod";-import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { MethodIcon, MethodItemTypeIcon, TrackingTypeIcon } from "~/components";import type { Configuration } from "~/components/Configurator/types";import {DefaultMethodType,@@ -54,9 +57,14 @@ import { path } from "~/utils/path";import type { methodOperationValidator } from "../../items.models";import { methodMaterialValidator } from "../../items.models";import type { ConfigurationParameter, ConfigurationRule } from "../../types";+import { Database } from "@carbon/database";type Material = z.infer<typeof methodMaterialValidator> & {description: string;+ item: {+ name: string;+ itemTrackingType: Database["public"]["Enums"]["itemTrackingType"];+ };};type Operation = z.infer<typeof methodOperationValidator>;@@ -136,6 +144,10 @@ const BillOfMaterial = ({materialsById.set("temporary", {...pendingMaterial,description: "",+ item: {+ name: "",+ itemTrackingType: "Inventory",+ },});} else {materialsById.set(pendingMaterial.id, {@@ -818,14 +830,38 @@ function makeItem(checked,details: (<HStack spacing={2}>- <Badge variant="secondary">- <MethodIcon type={material.methodType} />- </Badge>+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodIcon type={material.methodType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.methodType}</TooltipContent>+ </Tooltip>++ {["Batch", "Serial"].includes(material.item.itemTrackingType) && (+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <TrackingTypeIcon type={material.item.itemTrackingType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>+ {material.item.itemTrackingType} Tracking+ </TooltipContent>+ </Tooltip>+ )}<Badge variant="secondary">{material.quantity}</Badge>- <Badge variant="secondary">- <MethodItemTypeIcon type={material.itemType} />- </Badge>++ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodItemTypeIcon type={material.itemType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.itemType}</TooltipContent>+ </Tooltip></HStack>),data: {diff --git a/apps/erp/app/modules/production/ui/Jobs/JobBillOfMaterial.tsx b/apps/erp/app/modules/production/ui/Jobs/JobBillOfMaterial.tsxindex 1ddd33893e..b9f9f6761a 100644--- a/apps/erp/app/modules/production/ui/Jobs/JobBillOfMaterial.tsx+++ b/apps/erp/app/modules/production/ui/Jobs/JobBillOfMaterial.tsx@@ -12,6 +12,9 @@ import {cn,HStack,toast,+ Tooltip,+ TooltipContent,+ TooltipTrigger,useDebounce,VStack,} from "@carbon/react";@@ -21,7 +24,7 @@ import type { Dispatch, SetStateAction } from "react";import { useCallback, useEffect, useState } from "react";import { LuSettings2, LuX } from "react-icons/lu";import type { z } from "zod";-import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { MethodIcon, MethodItemTypeIcon, TrackingTypeIcon } from "~/components";import {DefaultMethodType,Hidden,@@ -45,7 +48,10 @@ import type { jobOperationValidator } from "../../production.models";import { jobMaterialValidator } from "../../production.models";import type { Job } from "../../types";-type Material = z.infer<typeof jobMaterialValidator>;+type Material = z.infer<typeof jobMaterialValidator> & {+ requiresBatchTracking: boolean;+ requiresSerialTracking: boolean;+};type Operation = z.infer<typeof jobOperationValidator>;@@ -105,14 +111,43 @@ function makeItem(checked: checked,details: (<HStack spacing={2}>- <Badge variant="secondary">- <MethodIcon type={material.methodType} />- </Badge>-+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodIcon type={material.methodType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.methodType}</TooltipContent>+ </Tooltip>+ {material.requiresBatchTracking ? (+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <TrackingTypeIcon type="Batch" />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>Batch Tracking</TooltipContent>+ </Tooltip>+ ) : material.requiresSerialTracking ? (+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <TrackingTypeIcon type="Serial" />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>Serial Tracking</TooltipContent>+ </Tooltip>+ ) : null}<Badge variant="secondary">{material.quantity}</Badge>- <Badge variant="secondary">- <MethodItemTypeIcon type={material.itemType} />- </Badge>++ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodItemTypeIcon type={material.itemType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.itemType}</TooltipContent>+ </Tooltip></HStack>),data: {@@ -200,6 +235,8 @@ const JobBillOfMaterial = ({materialsById.set("temporary", {...pendingMaterial,description: "",+ requiresBatchTracking: false,+ requiresSerialTracking: false,});} else {materialsById.set(pendingMaterial.id, {diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.tsindex d08c60911e..2d06dc4f4b 100644--- a/apps/erp/app/modules/sales/sales.service.ts+++ b/apps/erp/app/modules/sales/sales.service.ts@@ -934,7 +934,7 @@ export async function getQuoteMaterialsByMethodId() {return client.from("quoteMaterial")- .select("*")+ .select("*, item(name, itemTrackingType)").eq("quoteMakeMethodId", quoteMakeMethodId).order("order", { ascending: true });}diff --git a/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx b/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsxindex d759a21012..213ad80834 100644--- a/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx+++ b/apps/erp/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx@@ -12,6 +12,9 @@ import {cn,HStack,toast,+ Tooltip,+ TooltipContent,+ TooltipTrigger,useDebounce,VStack,} from "@carbon/react";@@ -21,7 +24,7 @@ import type { Dispatch, SetStateAction } from "react";import { useCallback, useEffect, useState } from "react";import { LuSettings2, LuX } from "react-icons/lu";import type { z } from "zod";-import { MethodIcon, MethodItemTypeIcon } from "~/components";+import { MethodIcon, MethodItemTypeIcon, TrackingTypeIcon } from "~/components";import {DefaultMethodType,Hidden,@@ -44,8 +47,14 @@ import { path } from "~/utils/path";import type { quoteOperationValidator } from "../../sales.models";import { quoteMaterialValidator } from "../../sales.models";import type { Quotation } from "../../types";+import { Database } from "@carbon/database";-type Material = z.infer<typeof quoteMaterialValidator>;+type Material = z.infer<typeof quoteMaterialValidator> & {+ item: {+ name: string;+ itemTrackingType: Database["public"]["Enums"]["itemTrackingType"];+ };+};type Operation = z.infer<typeof quoteOperationValidator>;@@ -105,14 +114,38 @@ function makeItem(checked: checked,details: (<HStack spacing={2}>- <Badge variant="secondary">- <MethodIcon type={material.methodType} />- </Badge>+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodIcon type={material.methodType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.methodType}</TooltipContent>+ </Tooltip>++ {["Batch", "Serial"].includes(material.item.itemTrackingType) && (+ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <TrackingTypeIcon type={material.item.itemTrackingType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>+ {material.item.itemTrackingType} Tracking+ </TooltipContent>+ </Tooltip>+ )}<Badge variant="secondary">{material.quantity}</Badge>- <Badge variant="secondary">- <MethodItemTypeIcon type={material.itemType} />- </Badge>++ <Tooltip>+ <TooltipTrigger>+ <Badge variant="secondary">+ <MethodItemTypeIcon type={material.itemType} />+ </Badge>+ </TooltipTrigger>+ <TooltipContent>{material.itemType}</TooltipContent>+ </Tooltip></HStack>),data: {@@ -206,6 +239,10 @@ const QuoteBillOfMaterial = ({materialsById.set("temporary", {...pendingMaterial,description: "",+ item: {+ name: "",+ itemTrackingType: "Inventory",+ },});} else {materialsById.set(pendingMaterial.id, {diff --git a/apps/erp/app/routes/x+/part+/$itemId.manufacturing.$makeMethodId.$materialId.tsx b/apps/erp/app/routes/x+/part+/$itemId.manufacturing.$makeMethodId.$materialId.tsxindex d8869e22bb..5f7dbae06a 100644--- a/apps/erp/app/routes/x+/part+/$itemId.manufacturing.$makeMethodId.$materialId.tsx+++ b/apps/erp/app/routes/x+/part+/$itemId.manufacturing.$makeMethodId.$materialId.tsx@@ -114,10 +114,12 @@ export async function loader({ request, params }: LoaderFunctionArgs) {methodOperations:methodOperations.data?.map((operation) => ({...operation,- workCenterId: operation.workCenterId ?? undefined,+ description: operation.description ?? "",+ procedureId: operation.procedureId ?? undefined,operationSupplierProcessId:operation.operationSupplierProcessId ?? undefined,tags: operation.tags ?? [],+ workCenterId: operation.workCenterId ?? undefined,workInstruction: operation.workInstruction as JSONContent | null,})) ?? [],model: getModelByItemId(client, material.data.itemId),diff --git a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.make.$methodId.$materialId.tsx b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.make.$methodId.$materialId.tsxindex 46721b5109..50017d26ce 100644--- a/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.make.$methodId.$materialId.tsx+++ b/apps/erp/app/routes/x+/quote+/$quoteId.$lineId.make.$methodId.$materialId.tsx@@ -97,6 +97,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {materials:materials?.data.map((m) => ({...m,+ description: m.description ?? "",itemType: m.itemType as "Part",unitOfMeasureCode: m.unitOfMeasureCode ?? "",quoteOperationId: m.quoteOperationId ?? undefined,@@ -105,6 +106,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {operations.data?.map((o) => ({...o,description: o.description ?? "",+ procedureId: o.procedureId ?? undefined,workCenterId: o.workCenterId ?? undefined,laborRate: o.laborRate ?? 0,machineRate: o.machineRate ?? 0,diff --git a/packages/database/src/types.ts b/packages/database/src/types.tsindex 2ead2b0624..13381bfff7 100644--- a/packages/database/src/types.ts+++ b/packages/database/src/types.ts@@ -8262,6 +8262,8 @@ export type Database = {quantity: number;quantityIssued: number | null;quantityToIssue: number | null;+ requiresBatchTracking: boolean;+ requiresSerialTracking: boolean;scrapQuantity: number;shelfId: string | null;unitCost: number;@@ -8289,6 +8291,8 @@ export type Database = {quantity?: number;quantityIssued?: number | null;quantityToIssue?: number | null;+ requiresBatchTracking?: boolean;+ requiresSerialTracking?: boolean;scrapQuantity?: number;shelfId?: string | null;unitCost?: number;@@ -8316,6 +8320,8 @@ export type Database = {quantity?: number;quantityIssued?: number | null;quantityToIssue?: number | null;+ requiresBatchTracking?: boolean;+ requiresSerialTracking?: boolean;scrapQuantity?: number;shelfId?: string | null;unitCost?: number;@@ -30343,14 +30349,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"];@@ -31649,14 +31655,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"];@@ -33307,14 +33313,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"];@@ -35909,6 +35915,7 @@ export type Database = {unitCost: number;quantity: number;methodType: Database["public"]["Enums"]["methodType"];+ itemTrackingType: string;parentMaterialId: string;order: number;operationId: string;@@ -35992,6 +35999,7 @@ export type Database = {description: string;unitOfMeasureCode: string;itemType: string;+ itemTrackingType: string;quantity: number;unitCost: number;methodType: Database["public"]["Enums"]["methodType"];diff --git a/packages/database/supabase/functions/get-method/index.ts b/packages/database/supabase/functions/get-method/index.tsindex a154537b90..54a63c68ca 100644--- a/packages/database/supabase/functions/get-method/index.ts+++ b/packages/database/supabase/functions/get-method/index.ts@@ -661,11 +661,17 @@ serve(async (req: Request) => {let itemReadableId = child.data.itemReadableId;let itemType = child.data.itemType;let unitCost = child.data.unitCost;+ let requiresSerialTracking =+ child.data.itemTrackingType === "Serial";+ let requiresBatchTracking =+ child.data.itemTrackingType === "Batch";if (itemId !== child.data.itemId) {const item = await client.from("item")- .select("readableId, type, name, itemCost(unitCost)")+ .select(+ "readableId, type, name, itemTrackingType, itemCost(unitCost)"+ ).eq("id", itemId).eq("companyId", companyId).single();@@ -677,6 +683,10 @@ serve(async (req: Request) => {if (description === child.data.description) {description = item.data.name;}+ requiresSerialTracking =+ item.data.itemTrackingType === "Serial";+ requiresBatchTracking =+ item.data.itemTrackingType === "Batch";} else {itemId = child.data.itemId;}@@ -694,6 +704,8 @@ serve(async (req: Request) => {order: child.data.order,description,quantity,+ requiresSerialTracking,+ requiresBatchTracking,unitOfMeasureCode,unitCost,companyId,@@ -1010,6 +1022,8 @@ serve(async (req: Request) => {order: child.data.order,description: child.data.description,quantity: child.data.quantity,+ requiresBatchTracking: child.data.itemTrackingType === "Batch",+ requiresSerialTracking: child.data.itemTrackingType === "Serial",unitOfMeasureCode: child.data.unitOfMeasureCode,unitCost: child.data.unitCost,companyId,@@ -2941,6 +2955,10 @@ serve(async (req: Request) => {child.data.quoteMakeMethodId],quantity: child.data.quantity,+ requiresBatchTracking:+ child.data.itemTrackingType === "Batch",+ requiresSerialTracking:+ child.data.itemTrackingType === "Serial",unitOfMeasureCode: child.data.unitOfMeasureCode,companyId,createdBy: userId,diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.tsindex 2ead2b0624..13381bfff7 100644--- a/packages/database/supabase/functions/lib/types.ts+++ b/packages/database/supabase/functions/lib/types.ts@@ -8262,6 +8262,8 @@ export type Database = {quantity: number;quantityIssued: number | null;quantityToIssue: number | null;+ requiresBatchTracking: boolean;+ requiresSerialTracking: boolean;scrapQuantity: number;shelfId: string | null;unitCost: number;@@ -8289,6 +8291,8 @@ export type Database = {quantity?: number;quantityIssued?: number | null;quantityToIssue?: number | null;+ requiresBatchTracking?: boolean;+ requiresSerialTracking?: boolean;scrapQuantity?: number;shelfId?: string | null;unitCost?: number;@@ -8316,6 +8320,8 @@ export type Database = {quantity?: number;quantityIssued?: number | null;quantityToIssue?: number | null;+ requiresBatchTracking?: boolean;+ requiresSerialTracking?: boolean;scrapQuantity?: number;shelfId?: string | null;unitCost?: number;@@ -30343,14 +30349,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"];@@ -31649,14 +31655,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"];@@ -33307,14 +33313,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"];@@ -35909,6 +35915,7 @@ export type Database = {unitCost: number;quantity: number;methodType: Database["public"]["Enums"]["methodType"];+ itemTrackingType: string;parentMaterialId: string;order: number;operationId: string;@@ -35992,6 +35999,7 @@ export type Database = {description: string;unitOfMeasureCode: string;itemType: string;+ itemTrackingType: string;quantity: number;unitCost: number;methodType: Database["public"]["Enums"]["methodType"];diff --git a/packages/database/supabase/migrations/20250301125444_tracked-materials.sql b/packages/database/supabase/migrations/20250301125444_tracked-materials.sqlnew file mode 100644index 0000000000..9f82ed5771--- /dev/null+++ b/packages/database/supabase/migrations/20250301125444_tracked-materials.sql@@ -0,0 +1,187 @@+ALTER TABLE "jobMaterial" ADD COLUMN "requiresSerialTracking" BOOLEAN NOT NULL DEFAULT FALSE;+ALTER TABLE "jobMaterial" ADD COLUMN "requiresBatchTracking" BOOLEAN NOT NULL DEFAULT FALSE;++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+) 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"+ 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"+ 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."readableId" AS "itemReadableId",+ item."name" AS "description",+ item."unitOfMeasureCode",+ material."itemType",+ item."itemTrackingType",+ material."quantity",+ material."unitCost",+ material."methodType",+ material."parentMaterialId",+ material."order",+ material."isRoot"+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+) AS $$+WITH RECURSIVE material AS (+ SELECT+ "id",+ "makeMethodId",+ "methodType",+ "materialMakeMethodId",+ "itemId",+ "itemType",+ "quantity",+ "makeMethodId" AS "parentMaterialId",+ NULL AS "operationId",+ COALESCE("order", 1) AS "order"+ 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"+ FROM+ "methodMaterial" child+ INNER JOIN material parent ON parent."materialMakeMethodId" = child."makeMethodId"+)+SELECT+ material.id as "methodMaterialId",+ material."makeMethodId",+ material."materialMakeMethodId",+ material."itemId",+ item."readableId" 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"+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."readableId" 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"+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;\ No newline at end of file