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.

feat: recursive query for quote methods

Brad Barbin5f8bd27e5dcb
diff --git a/apps/carbon/app/modules/items/items.service.ts b/apps/carbon/app/modules/items/items.service.ts
index 5fdf8e6de2..06b0080c86 100644
--- a/apps/carbon/app/modules/items/items.service.ts
+++ b/apps/carbon/app/modules/items/items.service.ts
@@ -549,7 +549,7 @@ export async function getMethodTree(
) {
const items = await getMethodTreeArray(client, makeMethodId);
if (items.error) return items;
- console.log({ items: items.data });
+
const tree = getMethodTreeArrayToTree(items.data);
return {
diff --git a/apps/carbon/app/modules/sales/sales.models.ts b/apps/carbon/app/modules/sales/sales.models.ts
index 19e811fb1f..590a06245d 100644
--- a/apps/carbon/app/modules/sales/sales.models.ts
+++ b/apps/carbon/app/modules/sales/sales.models.ts
@@ -153,7 +153,9 @@ export const quotationMaterialValidator = z.object({
export const quoteMaterialValidator = z
.object({
id: zfd.text(z.string().optional()),
- makeMethodId: z.string().min(20, { message: "Make method is required" }),
+ quoteMakeMethodId: z
+ .string()
+ .min(20, { message: "Make method is required" }),
order: zfd.numeric(z.number().min(0)),
itemType: z.enum(methodItemType, {
errorMap: (issue, ctx) => ({
@@ -165,9 +167,9 @@ export const quoteMaterialValidator = z
message: "Method type is required",
}),
}),
- itemId: z.string().optional(),
+ itemId: z.string().min(1, { message: "Item is required" }),
itemReadableId: z.string().optional(),
- description: zfd.text(z.string().optional()),
+ description: z.string().min(1, { message: "Description is required" }),
methodOperationId: zfd.text(z.string().optional()),
// description: z.string().min(1, { message: "Description is required" }),
quantity: zfd.numeric(z.number().min(0)),
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 48dcb0c293..4da24df6d5 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -15,10 +15,10 @@ import type {
customerTypeValidator,
customerValidator,
quotationAssemblyValidator,
- quotationMaterialValidator,
quotationOperationValidator,
quotationPricingValidator,
quoteLineValidator,
+ quoteMaterialValidator,
quoteValidator,
salesOrderLineValidator,
salesOrderPaymentValidator,
@@ -482,6 +482,17 @@ export async function getQuoteAssemblies(
return client.from("quoteMakeMethod").select("*").eq("quoteId", quoteId);
}
+export async function getQuoteCustomerDetails(
+ client: SupabaseClient<Database>,
+ quoteId: string
+) {
+ return client
+ .from("quoteCustomerDetails")
+ .select("*")
+ .eq("quoteId", quoteId)
+ .single();
+}
+
export async function getQuoteDocuments(
client: SupabaseClient<Database>,
companyId: string,
@@ -497,22 +508,85 @@ export async function getQuoteLine(
return client.from("quoteLines").select("*").eq("id", quoteLineId).single();
}
-export async function getQuoteLines(
+type QuoteMethod = NonNullable<
+ Awaited<ReturnType<typeof getQuoteMethodTreeArray>>["data"]
+>[number];
+type QuoteMethodTreeItem = {
+ id: string;
+ data: QuoteMethod;
+ children: QuoteMethodTreeItem[];
+};
+
+export async function getQuoteMethodTrees(
client: SupabaseClient<Database>,
quoteId: string
) {
- return client.from("quoteLines").select("*").eq("quoteId", quoteId);
+ const items = await getQuoteMethodTreeArray(client, quoteId);
+ if (items.error) return items;
+
+ const tree = getQuoteMethodTreeArrayToTree(items.data);
+
+ return {
+ data: tree,
+ error: null,
+ };
}
-export async function getQuoteCustomerDetails(
+export async function getQuoteMethodTreeArray(
client: SupabaseClient<Database>,
quoteId: string
) {
- return client
- .from("quoteCustomerDetails")
- .select("*")
- .eq("quoteId", quoteId)
- .single();
+ return client.rpc("get_quote_methods", {
+ qid: quoteId,
+ });
+}
+
+function getQuoteMethodTreeArrayToTree(
+ items: QuoteMethod[]
+): QuoteMethodTreeItem[] {
+ // function traverseAndRenameIds(node: QuoteMethodTreeItem) {
+ // const clone = structuredClone(node);
+ // clone.id = `node-${Math.random().toString(16).slice(2)}`;
+ // clone.children = clone.children.map((n) => traverseAndRenameIds(n));
+ // return clone;
+ // }
+
+ const rootItems: QuoteMethodTreeItem[] = [];
+ const lookup: { [id: string]: QuoteMethodTreeItem } = {};
+
+ for (const item of items) {
+ const itemId = item.methodMaterialId;
+ const parentId = item.parentMaterialId;
+
+ if (!Object.prototype.hasOwnProperty.call(lookup, itemId)) {
+ // @ts-ignore
+ lookup[itemId] = { id: itemId, children: [] };
+ }
+
+ lookup[itemId]["data"] = item;
+
+ const treeItem = lookup[itemId];
+
+ if (parentId === null || parentId === undefined) {
+ rootItems.push(treeItem);
+ } else {
+ if (!Object.prototype.hasOwnProperty.call(lookup, parentId)) {
+ // @ts-ignore
+ lookup[parentId] = { id: parentId, children: [] };
+ }
+
+ lookup[parentId]["children"].push(treeItem);
+ }
+ }
+ return rootItems;
+ // return rootItems.map((item) => traverseAndRenameIds(item));
+}
+
+export async function getQuoteLines(
+ client: SupabaseClient<Database>,
+ quoteId: string
+) {
+ return client.from("quoteLines").select("*").eq("quoteId", quoteId);
}
export async function getSalesOrderExternalDocuments(
@@ -1254,8 +1328,8 @@ export async function updateQuoteLinePrice(
export async function upsertQuoteMaterial(
client: SupabaseClient<Database>,
- quotationMaterial:
- | (Omit<z.infer<typeof quotationMaterialValidator>, "id"> & {
+ quoteMaterial:
+ | (Omit<z.infer<typeof quoteMaterialValidator>, "id"> & {
quoteId: string;
quoteLineId: string;
quoteOperationId: string;
@@ -1263,7 +1337,7 @@ export async function upsertQuoteMaterial(
createdBy: string;
customFields?: Json;
})
- | (Omit<z.infer<typeof quotationMaterialValidator>, "id"> & {
+ | (Omit<z.infer<typeof quoteMaterialValidator>, "id"> & {
id: string;
quoteId: string;
quoteLineId: string;
@@ -1272,17 +1346,17 @@ export async function upsertQuoteMaterial(
customFields?: Json;
})
) {
- if ("id" in quotationMaterial) {
+ if ("id" in quoteMaterial) {
return client
.from("quoteMaterial")
- .update(sanitize(quotationMaterial))
- .eq("id", quotationMaterial.id)
+ .update(sanitize(quoteMaterial))
+ .eq("id", quoteMaterial.id)
.select("id")
.single();
}
return client
.from("quoteMaterial")
- .insert([quotationMaterial])
+ .insert([quoteMaterial])
.select("id")
.single();
}
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx
index f483091b51..e3b577ddb0 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfMaterial.tsx
@@ -56,8 +56,8 @@ type ItemWithData = SortableItem & {
data: Material;
};
-type BillOfMaterialProps = {
- makeMethodId: string;
+type QuoteBillOfMaterialProps = {
+ quoteMakeMethodId: string;
materials: Material[];
operations: Operation[];
};
@@ -96,7 +96,7 @@ function makeItem(material: Material): ItemWithData {
};
}
-const initialMethodMaterial: Omit<Material, "makeMethodId" | "order"> & {
+const initialMethodMaterial: Omit<Material, "quoteMakeMethodId" | "order"> & {
description: string;
} = {
itemId: "",
@@ -108,11 +108,11 @@ const initialMethodMaterial: Omit<Material, "makeMethodId" | "order"> & {
unitOfMeasureCode: "EA",
};
-const BillOfMaterial = ({
- makeMethodId,
+const QuoteBillOfMaterial = ({
+ quoteMakeMethodId,
materials,
operations,
-}: BillOfMaterialProps) => {
+}: QuoteBillOfMaterialProps) => {
const fetcher = useFetcher<{}>();
const [items, setItems] = useState<ItemWithData[]>(
@@ -147,7 +147,7 @@ const BillOfMaterial = ({
data: {
...initialMethodMaterial,
order: newOrder,
- makeMethodId,
+ quoteMakeMethodId,
},
},
];
@@ -163,7 +163,7 @@ const BillOfMaterial = ({
fetcher.submit(new FormData(), {
method: "post",
- action: path.to.deleteMethodMaterial(makeMethodId, item.id),
+ action: path.to.deleteMethodMaterial(quoteMakeMethodId, item.id),
});
};
@@ -191,7 +191,7 @@ const BillOfMaterial = ({
formData.append("updates", JSON.stringify(updates));
fetcher.submit(formData, {
method: "post",
- action: path.to.methodMaterialsOrder(makeMethodId),
+ action: path.to.methodMaterialsOrder(quoteMakeMethodId),
});
}, 1000);
@@ -368,7 +368,7 @@ const BillOfMaterial = ({
);
};
-export default BillOfMaterial;
+export default QuoteBillOfMaterial;
function isTemporaryId(id: string) {
return id.length < 20;
@@ -475,8 +475,8 @@ function MaterialForm({
<ValidatedForm
action={
isTemporaryId(item.id)
- ? path.to.newMethodMaterial(item.data.makeMethodId!)
- : path.to.methodMaterial(item.data.makeMethodId, item.id!)
+ ? path.to.newMethodMaterial(item.data.quoteMakeMethodId!)
+ : path.to.methodMaterial(item.data.quoteMakeMethodId, item.id!)
}
method="post"
defaultValues={item.data}
@@ -496,7 +496,7 @@ function MaterialForm({
);
}}
>
- <Hidden name="makeMethodId" />
+ <Hidden name="quoteMakeMethodId" />
<Hidden name="itemReadableId" value={itemData.itemReadableId} />
<Hidden name="order" />
<VStack className="pt-4">
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
index d44f773f31..ee896a080f 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteBillOfProcess.tsx
@@ -43,7 +43,7 @@ type ItemWithData = Item & {
data: Operation;
};
-type BillOfProcessProps = {
+type QuoteBillOfProcessProps = {
quoteMakeMethodId: string;
operations: Operation[];
};
@@ -83,10 +83,10 @@ const initialMethodOperation: Omit<Operation, "quoteMakeMethodId" | "order"> = {
overheadRate: 0,
};
-const BillOfProcess = ({
+const QuoteBillOfProcess = ({
quoteMakeMethodId,
operations,
-}: BillOfProcessProps) => {
+}: QuoteBillOfProcessProps) => {
const { supabase } = useSupabase();
const sortOrderFetcher = useFetcher<{}>();
@@ -352,7 +352,7 @@ const BillOfProcess = ({
);
};
-export default BillOfProcess;
+export default QuoteBillOfProcess;
function isTemporaryId(id: string) {
return id.length < 20;
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/index.ts b/apps/carbon/app/modules/sales/ui/Quotes/index.ts
index ab1458dbbb..c81a7605c4 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/index.ts
+++ b/apps/carbon/app/modules/sales/ui/Quotes/index.ts
@@ -1,4 +1,6 @@
import DeleteQuoteLine from "./DeleteQuoteLine";
+import QuoteBillOfMaterial from "./QuoteBillOfMaterial";
+import QuoteBillOfProcess from "./QuoteBillOfProcess";
import QuoteBoMExplorer from "./QuoteBoMExplorer";
import QuoteBreadcrumbs from "./QuoteBreadcrumbs";
import QuoteDocuments from "./QuoteDocuments";
@@ -10,6 +12,8 @@ import QuotesTable from "./QuotesTable";
export {
DeleteQuoteLine,
+ QuoteBillOfMaterial,
+ QuoteBillOfProcess,
QuoteBoMExplorer,
QuoteBreadcrumbs,
QuoteDocuments,
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.make.$makeMethodId.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.make.$makeMethodId.tsx
deleted file mode 100644
index 20e8a2a147..0000000000
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.make.$makeMethodId.tsx
+++ /dev/null
@@ -1,3 +0,0 @@
-export default function QuoteMakeMethodRoute() {
- return null;
-}
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.method.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.method.tsx
new file mode 100644
index 0000000000..faffa3c8ec
--- /dev/null
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.method.tsx
@@ -0,0 +1,27 @@
+import { VStack } from "@carbon/react";
+import { useParams } from "@remix-run/react";
+
+import { QuoteBillOfMaterial, QuoteBillOfProcess } from "~/modules/sales";
+
+export default function QuoteMakeMethodRoute() {
+ const { quoteId, lineId } = useParams();
+ if (!quoteId) throw new Error("Could not find quoteId");
+
+ const quoteMakeMethodId = "";
+
+ return (
+ <VStack spacing={2} className="p-2">
+ <QuoteBillOfProcess
+ key={`bop:${quoteId}:${lineId}`}
+ quoteMakeMethodId={quoteMakeMethodId}
+ operations={[]}
+ />
+ <QuoteBillOfMaterial
+ key={`bom:${quoteId}:${lineId}`}
+ quoteMakeMethodId={quoteMakeMethodId}
+ materials={[]}
+ operations={[]}
+ />
+ </VStack>
+ );
+}
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
index 71d7bdc02a..5ea1f28428 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
@@ -29,6 +29,7 @@ import {
getQuote,
getQuoteDocuments,
getQuoteLines,
+ getQuoteMethodTrees,
QuoteBoMExplorer,
QuoteBreadcrumbs,
QuoteHeader,
@@ -54,10 +55,11 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const { quoteId } = params;
if (!quoteId) throw new Error("Could not find quoteId");
- const [quote, lines, files] = await Promise.all([
+ const [quote, lines, files, methods] = await Promise.all([
getQuote(client, quoteId),
getQuoteLines(client, quoteId),
getQuoteDocuments(client, companyId, quoteId),
+ getQuoteMethodTrees(client, quoteId),
]);
if (quote.error) {
@@ -70,6 +72,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
return json({
quote: quote.data,
lines: lines.data ?? [],
+ methods: methods.data ?? [],
files: files.data ?? [],
});
}
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 2360304e21..f84169f2d4 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -13341,6 +13341,13 @@ export type Database = {
referencedRelation: "quoteMaterial"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteMakeMethod_parentMaterialId_fkey"
+ columns: ["parentMaterialId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "quoteMakeMethod_quoteId_fkey"
columns: ["quoteId"]
@@ -13428,7 +13435,7 @@ export type Database = {
quantity: number
quoteId: string
quoteLineId: string
- quoteMakeMethodId: string | null
+ quoteMakeMethodId: string
quoteOperationId: string | null
unitCost: number
unitOfMeasureCode: string | null
@@ -13449,7 +13456,7 @@ export type Database = {
quantity?: number
quoteId: string
quoteLineId: string
- quoteMakeMethodId?: string | null
+ quoteMakeMethodId: string
quoteOperationId?: string | null
unitCost?: number
unitOfMeasureCode?: string | null
@@ -13470,7 +13477,7 @@ export type Database = {
quantity?: number
quoteId?: string
quoteLineId?: string
- quoteMakeMethodId?: string | null
+ quoteMakeMethodId?: string
quoteOperationId?: string | null
unitCost?: number
unitOfMeasureCode?: string | null
@@ -13597,6 +13604,13 @@ export type Database = {
referencedRelation: "quoteMakeMethod"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
{
foreignKeyName: "quoteMaterial_quoteOperationId_fkey"
columns: ["quoteOperationId"]
@@ -13831,6 +13845,13 @@ export type Database = {
referencedRelation: "quoteMakeMethod"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteOperation_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
{
foreignKeyName: "quoteOperation_updatedBy_fkey"
columns: ["updatedBy"]
@@ -23087,6 +23108,207 @@ export type Database = {
},
]
}
+ quoteMaterialWithMakeMethodId: {
+ Row: {
+ companyId: string | null
+ createdAt: string | null
+ createdBy: string | null
+ customFields: Json | null
+ description: string | null
+ id: string | null
+ itemId: string | null
+ itemType: string | null
+ methodType: Database["public"]["Enums"]["methodType"] | null
+ order: number | null
+ quantity: number | null
+ quoteId: string | null
+ quoteLineId: string | null
+ quoteMakeMethodId: string | null
+ quoteMaterialMakeMethodId: string | null
+ quoteOperationId: string | null
+ unitCost: number | null
+ unitOfMeasureCode: string | null
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "companies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "customFieldTables"
+ referencedColumns: ["companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "integrations"
+ referencedColumns: ["companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_itemId_fkey"
+ columns: ["itemId"]
+ isOneToOne: false
+ referencedRelation: "item"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_itemId_fkey"
+ columns: ["itemId"]
+ isOneToOne: false
+ referencedRelation: "itemQuantities"
+ referencedColumns: ["itemId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quote"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quoteCustomerDetails"
+ referencedColumns: ["quoteId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quotes"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteLineId_fkey"
+ columns: ["quoteLineId"]
+ isOneToOne: false
+ referencedRelation: "quoteLine"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteLineId_fkey"
+ columns: ["quoteLineId"]
+ isOneToOne: false
+ referencedRelation: "quoteLines"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMakeMethod"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteOperationId_fkey"
+ columns: ["quoteOperationId"]
+ isOneToOne: false
+ referencedRelation: "quoteOperation"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_unitOfMeasureCode_fkey"
+ columns: ["unitOfMeasureCode", "companyId"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code", "companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
quotes: {
Row: {
assignee: string | null
@@ -25420,17 +25642,17 @@ export type Database = {
Returns: {
quoteId: string
quoteLineId: string
- parentMaterialId: string
- parentItemId: string
- quantityPerParent: number
- quoteMaterialId: string
+ methodMaterialId: string
+ quoteMakeMethodId: string
+ quoteMaterialMakeMethodId: string
itemId: string
+ itemReadableId: string
itemType: string
- methodType: string
- order: number
quantity: number
- unitOfMeasureCode: string
- unitCost: number
+ methodType: Database["public"]["Enums"]["methodType"]
+ parentMaterialId: string
+ order: number
+ isRoot: boolean
}[]
}
groups_for_user: {
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 2360304e21..f84169f2d4 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -13341,6 +13341,13 @@ export type Database = {
referencedRelation: "quoteMaterial"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteMakeMethod_parentMaterialId_fkey"
+ columns: ["parentMaterialId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["id"]
+ },
{
foreignKeyName: "quoteMakeMethod_quoteId_fkey"
columns: ["quoteId"]
@@ -13428,7 +13435,7 @@ export type Database = {
quantity: number
quoteId: string
quoteLineId: string
- quoteMakeMethodId: string | null
+ quoteMakeMethodId: string
quoteOperationId: string | null
unitCost: number
unitOfMeasureCode: string | null
@@ -13449,7 +13456,7 @@ export type Database = {
quantity?: number
quoteId: string
quoteLineId: string
- quoteMakeMethodId?: string | null
+ quoteMakeMethodId: string
quoteOperationId?: string | null
unitCost?: number
unitOfMeasureCode?: string | null
@@ -13470,7 +13477,7 @@ export type Database = {
quantity?: number
quoteId?: string
quoteLineId?: string
- quoteMakeMethodId?: string | null
+ quoteMakeMethodId?: string
quoteOperationId?: string | null
unitCost?: number
unitOfMeasureCode?: string | null
@@ -13597,6 +13604,13 @@ export type Database = {
referencedRelation: "quoteMakeMethod"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
{
foreignKeyName: "quoteMaterial_quoteOperationId_fkey"
columns: ["quoteOperationId"]
@@ -13831,6 +13845,13 @@ export type Database = {
referencedRelation: "quoteMakeMethod"
referencedColumns: ["id"]
},
+ {
+ foreignKeyName: "quoteOperation_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
{
foreignKeyName: "quoteOperation_updatedBy_fkey"
columns: ["updatedBy"]
@@ -23087,6 +23108,207 @@ export type Database = {
},
]
}
+ quoteMaterialWithMakeMethodId: {
+ Row: {
+ companyId: string | null
+ createdAt: string | null
+ createdBy: string | null
+ customFields: Json | null
+ description: string | null
+ id: string | null
+ itemId: string | null
+ itemType: string | null
+ methodType: Database["public"]["Enums"]["methodType"] | null
+ order: number | null
+ quantity: number | null
+ quoteId: string | null
+ quoteLineId: string | null
+ quoteMakeMethodId: string | null
+ quoteMaterialMakeMethodId: string | null
+ quoteOperationId: string | null
+ unitCost: number | null
+ unitOfMeasureCode: string | null
+ updatedAt: string | null
+ updatedBy: string | null
+ }
+ Relationships: [
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "company"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "companies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "customFieldTables"
+ referencedColumns: ["companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_companyId_fkey"
+ columns: ["companyId"]
+ isOneToOne: false
+ referencedRelation: "integrations"
+ referencedColumns: ["companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_createdBy_fkey"
+ columns: ["createdBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_itemId_fkey"
+ columns: ["itemId"]
+ isOneToOne: false
+ referencedRelation: "item"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_itemId_fkey"
+ columns: ["itemId"]
+ isOneToOne: false
+ referencedRelation: "itemQuantities"
+ referencedColumns: ["itemId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quote"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quoteCustomerDetails"
+ referencedColumns: ["quoteId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteId_fkey"
+ columns: ["quoteId"]
+ isOneToOne: false
+ referencedRelation: "quotes"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteLineId_fkey"
+ columns: ["quoteLineId"]
+ isOneToOne: false
+ referencedRelation: "quoteLine"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteLineId_fkey"
+ columns: ["quoteLineId"]
+ isOneToOne: false
+ referencedRelation: "quoteLines"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMakeMethod"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteMakeMethodId_fkey"
+ columns: ["quoteMakeMethodId"]
+ isOneToOne: false
+ referencedRelation: "quoteMaterialWithMakeMethodId"
+ referencedColumns: ["quoteMaterialMakeMethodId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_quoteOperationId_fkey"
+ columns: ["quoteOperationId"]
+ isOneToOne: false
+ referencedRelation: "quoteOperation"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_unitOfMeasureCode_fkey"
+ columns: ["unitOfMeasureCode", "companyId"]
+ isOneToOne: false
+ referencedRelation: "unitOfMeasure"
+ referencedColumns: ["code", "companyId"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "user"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeeSummary"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employees"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "employeesAcrossCompanies"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "quoteMaterial_updatedBy_fkey"
+ columns: ["updatedBy"]
+ isOneToOne: false
+ referencedRelation: "userDefaults"
+ referencedColumns: ["userId"]
+ },
+ ]
+ }
quotes: {
Row: {
assignee: string | null
@@ -25420,17 +25642,17 @@ export type Database = {
Returns: {
quoteId: string
quoteLineId: string
- parentMaterialId: string
- parentItemId: string
- quantityPerParent: number
- quoteMaterialId: string
+ methodMaterialId: string
+ quoteMakeMethodId: string
+ quoteMaterialMakeMethodId: string
itemId: string
+ itemReadableId: string
itemType: string
- methodType: string
- order: number
quantity: number
- unitOfMeasureCode: string
- unitCost: number
+ methodType: Database["public"]["Enums"]["methodType"]
+ parentMaterialId: string
+ order: number
+ isRoot: boolean
}[]
}
groups_for_user: {
diff --git a/packages/database/supabase/migrations/20240715024405_quotes.sql b/packages/database/supabase/migrations/20240715024405_quotes.sql
index 2655eceed7..2cbf922838 100644
--- a/packages/database/supabase/migrations/20240715024405_quotes.sql
+++ b/packages/database/supabase/migrations/20240715024405_quotes.sql
@@ -197,7 +197,7 @@ CREATE INDEX "quoteMakeMethod_quoteId_idx" ON "quoteMakeMethod" ("quoteId");
CREATE INDEX "quoteMakeMethod_quoteLineId_idx" ON "quoteMakeMethod" ("quoteLineId");
CREATE INDEX "quoteMakeMethod_parentMaterialId_idx" ON "quoteMakeMethod" ("parentMaterialId");
-ALTER TABLE "quoteMaterial" ADD COLUMN "quoteMakeMethodId" TEXT,
+ALTER TABLE "quoteMaterial" ADD COLUMN "quoteMakeMethodId" TEXT NOT NULL,
ADD CONSTRAINT "quoteMaterial_quoteMakeMethodId_fkey" FOREIGN KEY ("quoteMakeMethodId") REFERENCES "quoteMakeMethod" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
CREATE OR REPLACE FUNCTION insert_quote_material_make_method()
@@ -338,41 +338,96 @@ CREATE OR REPLACE VIEW "quotes" WITH(SECURITY_INVOKER=true) AS
LEFT JOIN "location" l
ON l.id = q."locationId";
+CREATE OR REPLACE VIEW "quoteMaterialWithMakeMethodId" WITH(SECURITY_INVOKER=true) AS
+ SELECT
+ qm.*,
+ qmm."id" AS "quoteMaterialMakeMethodId"
+ FROM "quoteMaterial" qm
+ LEFT JOIN "quoteMakeMethod" qmm
+ ON qmm."parentMaterialId" = qm."id";
+
CREATE OR REPLACE FUNCTION get_quote_methods(qid TEXT)
RETURNS TABLE (
- "quoteId" TEXT,
- "quoteLineId" TEXT,
- "parentMaterialId" TEXT,
- "parentItemId" TEXT,
- "quantityPerParent" NUMERIC(10, 4),
- "quoteMaterialId" TEXT,
- "itemId" TEXT,
- "itemType" TEXT,
- "methodType" TEXT,
- "order" DOUBLE PRECISION,
- "quantity" NUMERIC(10, 4),
- "unitOfMeasureCode" TEXT,
- "unitCost" NUMERIC(10,4)
+ "quoteId" TEXT,
+ "quoteLineId" TEXT,
+ "methodMaterialId" TEXT,
+ "quoteMakeMethodId" TEXT,
+ "quoteMaterialMakeMethodId" TEXT,
+ "itemId" TEXT,
+ "itemReadableId" TEXT,
+ "itemType" TEXT,
+ "quantity" NUMERIC,
+ "methodType" "methodType",
+ "parentMaterialId" TEXT,
+ "order" DOUBLE PRECISION,
+ "isRoot" BOOLEAN
) AS $$
-BEGIN
- RETURN QUERY
- SELECT
- make."quoteId",
- make."quoteLineId",
- make."parentMaterialId",
- make."itemId" AS "parentItemId",
- make."quantityPerParent",
- mat."id" AS "quoteMaterialId",
- mat."itemId",
- mat."itemType",
- mat."methodType",
- mat."order",
- mat."quantity",
- mat."unitOfMeasureCode",
- mat."unitCost"
- FROM "quoteMakeMethod" make
- LEFT OUTER JOIN "quoteMaterial" mat
- ON make."id" = mat."quoteMakeMethodId"
- WHERE make."quoteId" = qid;
-END;
-$$ LANGUAGE plpgsql;
+WITH RECURSIVE material AS (
+ SELECT
+ "quoteId",
+ "quoteLineId",
+ "id",
+ "quoteMakeMethodId",
+ "methodType",
+ "quoteMaterialMakeMethodId",
+ "itemId",
+ "itemType",
+ "quantity",
+ "quoteMakeMethodId" AS "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order"
+ FROM
+ "quoteMaterialWithMakeMethodId"
+ WHERE
+ "quoteId" = qid
+ UNION
+ SELECT
+ child."quoteId",
+ child."quoteLineId",
+ child."id",
+ child."quoteMakeMethodId",
+ child."methodType",
+ child."quoteMaterialMakeMethodId",
+ child."itemId",
+ child."itemType",
+ child."quantity",
+ parent."id" AS "parentMaterialId",
+ child."order"
+ 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."readableId" AS "itemReadableId",
+ material."itemType",
+ material."quantity",
+ material."methodType",
+ material."parentMaterialId",
+ material."order",
+ false AS "isRoot"
+FROM material INNER JOIN item ON material."itemId" = item.id
+UNION
+SELECT
+ mm."quoteId",
+ mm."quoteLineId",
+ mm."id" AS "methodMaterialId",
+ NULL AS "makeMethodId",
+ mm.id AS "quoteMakeMethodId",
+ mm."itemId",
+ i."readableId" AS "itemReadableId",
+ i."type"::text,
+ 1 AS "quantity",
+ 'Make' AS "methodType",
+ NULL AS "parentMaterialId",
+ CAST(1 AS DOUBLE PRECISION) AS "order",
+ true AS "isRoot"
+
+FROM "quoteMakeMethod" mm INNER JOIN item i ON mm."itemId" = i.id
+WHERE mm."quoteId" = qid
+ORDER BY "order"
+$$ LANGUAGE sql STABLE;