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: add database triggers to insert a quote make method when a line or material is created

Brad Barbina3a105a08f97
diff --git a/apps/carbon/app/modules/sales/types.ts b/apps/carbon/app/modules/sales/types.ts
index 602c21f15e..dd150ac42d 100644
--- a/apps/carbon/app/modules/sales/types.ts
+++ b/apps/carbon/app/modules/sales/types.ts
@@ -6,7 +6,7 @@ import type {
getCustomers,
getCustomerStatuses,
getCustomerTypes,
- getQuoteExternalDocuments,
+ getQuoteDocuments,
getQuoteLines,
getQuoteMakeMethod,
getQuoteMaterials,
@@ -52,7 +52,7 @@ export type QuotationMakeMethod = NonNullable<
>;
export type QuotationAttachment = NonNullable<
- Awaited<ReturnType<typeof getQuoteExternalDocuments>>["data"]
+ Awaited<ReturnType<typeof getQuoteDocuments>>["data"]
>[number];
export type Quotation = NonNullable<
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
index 1b19caeb10..427671cd4d 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.details.tsx
@@ -154,7 +154,7 @@ export default function QuoteLine() {
return (
<>
- <QuoteLineForm initialValues={initialValues} />
+ <QuoteLineForm key={initialValues.id} initialValues={initialValues} />
{permissions.is("employee") && (
<>
<div className="grid grid-cols-1 xl:grid-cols-2 w-full flex-grow gap-2 ">
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
index fd33593134..e5e0138556 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
@@ -325,7 +325,7 @@ function QuoteLineItem({ line, onDelete }: QuoteLineItemProps) {
className={cn(
"w-full p-2 items-center justify-between hover:bg-accent/30 cursor-pointer",
!disclosure.isOpen && "border-b border-border",
- isSelected && "bg-accent/60 hover:bg-accent/50"
+ isSelected && "bg-accent/60 hover:bg-accent/50 shadow-inner"
)}
onClick={() => onLineClick(line)}
>
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 3bbfebdc4e..bd9178b0f7 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -12826,6 +12826,7 @@ export type Database = {
id: string
itemId: string
itemReadableId: string | null
+ itemType: string
methodType: Database["public"]["Enums"]["methodType"]
modelUploadId: string | null
notes: Json | null
@@ -12847,6 +12848,7 @@ export type Database = {
id?: string
itemId: string
itemReadableId?: string | null
+ itemType?: string
methodType?: Database["public"]["Enums"]["methodType"]
modelUploadId?: string | null
notes?: Json | null
@@ -12868,6 +12870,7 @@ export type Database = {
id?: string
itemId?: string
itemReadableId?: string | null
+ itemType?: string
methodType?: Database["public"]["Enums"]["methodType"]
modelUploadId?: string | null
notes?: Json | null
@@ -13216,14 +13219,12 @@ export type Database = {
createdAt: string
createdBy: string
customFields: Json | null
- description: string | null
id: string
itemId: string
parentMaterialId: string | null
quantityPerParent: number
quoteId: string
quoteLineId: string
- unitOfMeasureCode: string | null
updatedAt: string | null
updatedBy: string | null
}
@@ -13232,14 +13233,12 @@ export type Database = {
createdAt?: string
createdBy: string
customFields?: Json | null
- description?: string | null
id?: string
itemId: string
parentMaterialId?: string | null
quantityPerParent?: number
quoteId: string
quoteLineId: string
- unitOfMeasureCode?: string | null
updatedAt?: string | null
updatedBy?: string | null
}
@@ -13248,14 +13247,12 @@ export type Database = {
createdAt?: string
createdBy?: string
customFields?: Json | null
- description?: string | null
id?: string
itemId?: string
parentMaterialId?: string | null
quantityPerParent?: number
quoteId?: string
quoteLineId?: string
- unitOfMeasureCode?: string | null
updatedAt?: string | null
updatedBy?: string | null
}
@@ -13379,13 +13376,6 @@ export type Database = {
referencedRelation: "quoteLines"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quoteMakeMethod_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode", "companyId"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code", "companyId"]
- },
{
foreignKeyName: "quoteMakeMethod_updatedBy_fkey"
columns: ["updatedBy"]
@@ -13432,6 +13422,8 @@ export type Database = {
description: string
id: string
itemId: string
+ itemType: string
+ methodType: Database["public"]["Enums"]["methodType"]
quantity: number
quoteId: string
quoteLineId: string
@@ -13450,6 +13442,8 @@ export type Database = {
description: string
id?: string
itemId: string
+ itemType?: string
+ methodType?: Database["public"]["Enums"]["methodType"]
quantity?: number
quoteId: string
quoteLineId: string
@@ -13468,6 +13462,8 @@ export type Database = {
description?: string
id?: string
itemId?: string
+ itemType?: string
+ methodType?: Database["public"]["Enums"]["methodType"]
quantity?: number
quoteId?: string
quoteLineId?: string
@@ -22882,6 +22878,7 @@ export type Database = {
id: string | null
itemId: string | null
itemReadableId: string | null
+ itemType: string | null
methodType: Database["public"]["Enums"]["methodType"] | null
modelId: string | null
modelName: string | null
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 3bbfebdc4e..bd9178b0f7 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -12826,6 +12826,7 @@ export type Database = {
id: string
itemId: string
itemReadableId: string | null
+ itemType: string
methodType: Database["public"]["Enums"]["methodType"]
modelUploadId: string | null
notes: Json | null
@@ -12847,6 +12848,7 @@ export type Database = {
id?: string
itemId: string
itemReadableId?: string | null
+ itemType?: string
methodType?: Database["public"]["Enums"]["methodType"]
modelUploadId?: string | null
notes?: Json | null
@@ -12868,6 +12870,7 @@ export type Database = {
id?: string
itemId?: string
itemReadableId?: string | null
+ itemType?: string
methodType?: Database["public"]["Enums"]["methodType"]
modelUploadId?: string | null
notes?: Json | null
@@ -13216,14 +13219,12 @@ export type Database = {
createdAt: string
createdBy: string
customFields: Json | null
- description: string | null
id: string
itemId: string
parentMaterialId: string | null
quantityPerParent: number
quoteId: string
quoteLineId: string
- unitOfMeasureCode: string | null
updatedAt: string | null
updatedBy: string | null
}
@@ -13232,14 +13233,12 @@ export type Database = {
createdAt?: string
createdBy: string
customFields?: Json | null
- description?: string | null
id?: string
itemId: string
parentMaterialId?: string | null
quantityPerParent?: number
quoteId: string
quoteLineId: string
- unitOfMeasureCode?: string | null
updatedAt?: string | null
updatedBy?: string | null
}
@@ -13248,14 +13247,12 @@ export type Database = {
createdAt?: string
createdBy?: string
customFields?: Json | null
- description?: string | null
id?: string
itemId?: string
parentMaterialId?: string | null
quantityPerParent?: number
quoteId?: string
quoteLineId?: string
- unitOfMeasureCode?: string | null
updatedAt?: string | null
updatedBy?: string | null
}
@@ -13379,13 +13376,6 @@ export type Database = {
referencedRelation: "quoteLines"
referencedColumns: ["id"]
},
- {
- foreignKeyName: "quoteMakeMethod_unitOfMeasureCode_fkey"
- columns: ["unitOfMeasureCode", "companyId"]
- isOneToOne: false
- referencedRelation: "unitOfMeasure"
- referencedColumns: ["code", "companyId"]
- },
{
foreignKeyName: "quoteMakeMethod_updatedBy_fkey"
columns: ["updatedBy"]
@@ -13432,6 +13422,8 @@ export type Database = {
description: string
id: string
itemId: string
+ itemType: string
+ methodType: Database["public"]["Enums"]["methodType"]
quantity: number
quoteId: string
quoteLineId: string
@@ -13450,6 +13442,8 @@ export type Database = {
description: string
id?: string
itemId: string
+ itemType?: string
+ methodType?: Database["public"]["Enums"]["methodType"]
quantity?: number
quoteId: string
quoteLineId: string
@@ -13468,6 +13462,8 @@ export type Database = {
description?: string
id?: string
itemId?: string
+ itemType?: string
+ methodType?: Database["public"]["Enums"]["methodType"]
quantity?: number
quoteId?: string
quoteLineId?: string
@@ -22882,6 +22878,7 @@ export type Database = {
id: string | null
itemId: string | null
itemReadableId: string | null
+ itemType: string | null
methodType: Database["public"]["Enums"]["methodType"] | null
modelId: string | null
modelName: string | null
diff --git a/packages/database/supabase/migrations/20240715024405_quotes.sql b/packages/database/supabase/migrations/20240715024405_quotes.sql
index 47c33dd36b..38ce2af9f1 100644
--- a/packages/database/supabase/migrations/20240715024405_quotes.sql
+++ b/packages/database/supabase/migrations/20240715024405_quotes.sql
@@ -68,6 +68,7 @@ CREATE TABLE "quoteLine" (
"estimatorId" TEXT,
"itemId" TEXT NOT NULL,
"itemReadableId" TEXT,
+ "itemType" TEXT NOT NULL DEFAULT 'Part',
"description" TEXT NOT NULL,
"customerPartId" TEXT,
"customerPartRevision" TEXT,
@@ -97,6 +98,8 @@ CREATE TABLE "quoteMaterial" (
"quoteId" TEXT NOT NULL,
"quoteLineId" TEXT NOT NULL,
"itemId" TEXT NOT NULL,
+ "itemType" TEXT NOT NULL DEFAULT 'Part',
+ "methodType" "methodType" NOT NULL DEFAULT 'Make',
"description" TEXT NOT NULL,
"quantity" NUMERIC(10, 2) NOT NULL DEFAULT 0,
"unitOfMeasureCode" TEXT,
@@ -122,16 +125,13 @@ CREATE INDEX "quoteMaterial_quoteId_idx" ON "quoteMaterial" ("quoteId");
CREATE INDEX "quoteMaterial_quoteLineId_idx" ON "quoteMaterial" ("quoteLineId");
-
CREATE TABLE "quoteMakeMethod" (
"id" TEXT NOT NULL DEFAULT xid(),
"quoteId" TEXT NOT NULL,
"quoteLineId" TEXT NOT NULL,
"parentMaterialId" TEXT,
"itemId" TEXT NOT NULL,
- "description" TEXT,
- "unitOfMeasureCode" TEXT,
- "quantityPerParent" NUMERIC(10, 2) NOT NULL DEFAULT 0,
+ "quantityPerParent" NUMERIC(10, 2) NOT NULL DEFAULT 1,
"companyId" TEXT NOT NULL,
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"createdBy" TEXT NOT NULL,
@@ -144,12 +144,54 @@ CREATE TABLE "quoteMakeMethod" (
CONSTRAINT "quoteMakeMethod_quoteLineId_fkey" FOREIGN KEY ("quoteLineId") REFERENCES "quoteLine" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "quoteMakeMethod_parentMaterialId_fkey" FOREIGN KEY ("parentMaterialId") REFERENCES "quoteMaterial" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "quoteMakeMethod_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "item" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
- CONSTRAINT "quoteMakeMethod_unitOfMeasureCode_fkey" FOREIGN KEY ("unitOfMeasureCode", "companyId") REFERENCES "unitOfMeasure" ("code", "companyId") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quoteMakeMethod_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "company" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quoteMakeMethod_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "user" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "quoteMakeMethod_updatedBy_fkey" FOREIGN KEY ("updatedBy") REFERENCES "user" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
+CREATE OR REPLACE FUNCTION insert_quote_line_make_method()
+RETURNS TRIGGER AS $$
+BEGIN
+ INSERT INTO "quoteMakeMethod" ("quoteId", "quoteLineId", "itemId", "companyId", "createdAt", "createdBy")
+ VALUES (new."quoteId", new."id", new."itemId", new."companyId", NOW(), new."createdBy");
+ RETURN new;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER insert_quote_line_make_method_trigger
+AFTER INSERT ON "quoteLine"
+FOR EACH ROW
+WHEN (new."methodType" = 'Make')
+EXECUTE FUNCTION insert_quote_line_make_method();
+
+CREATE OR REPLACE FUNCTION update_quote_line_make_method_item_id()
+ RETURNS TRIGGER AS $$
+ BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM "quoteMakeMethod"
+ WHERE "quoteLineId" = new."id" AND "parentMaterialId" IS NULL
+ ) THEN
+ INSERT INTO "quoteMakeMethod" ("quoteId", "quoteLineId", "itemId", "companyId", "createdAt", "createdBy")
+ VALUES (new."quoteId", new."id", new."itemId", new."companyId", NOW(), new."createdBy");
+ ELSE
+ UPDATE "quoteMakeMethod"
+ SET "itemId" = new."itemId"
+ WHERE "quoteLineId" = new."id" AND "parentMaterialId" IS NULL;
+ END IF;
+ RETURN new;
+ END;
+ $$ LANGUAGE plpgsql;
+
+CREATE TRIGGER update_quote_line_make_method_item_id_trigger
+ AFTER UPDATE OF "itemId", "methodType" ON "quoteLine"
+ FOR EACH ROW
+ WHEN (
+ (old."methodType" = 'Make' AND old."itemId" IS DISTINCT FROM new."itemId") OR
+ (new."methodType" = 'Make' AND old."methodType" <> 'Make')
+ )
+ EXECUTE FUNCTION update_quote_line_make_method_item_id();
+
+
CREATE INDEX "quoteMakeMethod_quoteId_idx" ON "quoteMakeMethod" ("quoteId");
CREATE INDEX "quoteMakeMethod_quoteLineId_idx" ON "quoteMakeMethod" ("quoteLineId");
CREATE INDEX "quoteMakeMethod_parentMaterialId_idx" ON "quoteMakeMethod" ("parentMaterialId");
@@ -157,6 +199,51 @@ CREATE INDEX "quoteMakeMethod_parentMaterialId_idx" ON "quoteMakeMethod" ("paren
ALTER TABLE "quoteMaterial" ADD COLUMN "quoteMakeMethodId" TEXT,
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()
+ RETURNS TRIGGER AS $$
+ BEGIN
+ INSERT INTO "quoteMakeMethod" ("quoteId", "quoteLineId", "parentMaterialId", "itemId", "companyId", "createdAt", "createdBy")
+ VALUES (new."quoteId", new."quoteLineId", new."id", new."itemId", new."companyId", NOW(), new."createdBy");
+ RETURN new;
+ END;
+ $$ LANGUAGE plpgsql;
+
+CREATE TRIGGER insert_quote_material_make_method_trigger
+ AFTER INSERT ON "quoteMaterial"
+ FOR EACH ROW
+ WHEN (new."methodType" = 'Make')
+ EXECUTE FUNCTION insert_quote_material_make_method();
+
+CREATE OR REPLACE FUNCTION update_quote_material_make_method_item_id()
+ RETURNS TRIGGER AS $$
+ BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM "quoteMakeMethod"
+ WHERE "quoteLineId" = new."quoteLineId" AND "parentMaterialId" = new."id"
+ ) THEN
+ INSERT INTO "quoteMakeMethod" ("quoteId", "quoteLineId", "parentMaterialId", "itemId", "companyId", "createdAt", "createdBy")
+ VALUES (new."quoteId", new."quoteLineId", new."id", new."itemId", new."companyId", NOW(), new."createdBy");
+ ELSE
+ UPDATE "quoteMakeMethod"
+ SET "itemId" = new."itemId"
+ WHERE "quoteLineId" = new."quoteLineId" AND "parentMaterialId" = new."id";
+ END IF;
+ RETURN new;
+ END;
+ $$ LANGUAGE plpgsql;
+
+CREATE TRIGGER update_quote_material_make_method_item_id_trigger
+ AFTER UPDATE OF "itemId", "methodType" ON "quoteMaterial"
+ FOR EACH ROW
+ WHEN (
+ (old."methodType" = 'Make' AND old."itemId" IS DISTINCT FROM new."itemId") OR
+ (new."methodType" = 'Make' AND old."methodType" <> 'Make')
+ )
+ EXECUTE FUNCTION update_quote_material_make_method_item_id();
+
+
+
+
CREATE TABLE "quoteOperation" (
"id" TEXT NOT NULL DEFAULT xid(),
"quoteId" TEXT NOT NULL,
@@ -196,8 +283,10 @@ CREATE INDEX "quoteOperation_quoteMakeMethodId_idx" ON "quoteOperation" ("quoteM
ALTER TABLE "quoteMaterial" ADD COLUMN "quoteOperationId" TEXT,
ADD CONSTRAINT "quoteMaterial_quoteOperationId_fkey" FOREIGN KEY ("quoteOperationId") REFERENCES "quoteOperation" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
+COMMIT;
CREATE INDEX "quoteMaterial_quoteOperationId_idx" ON "quoteMaterial" ("quoteOperationId");
+
CREATE TABLE "quoteFavorite" (
"quoteId" TEXT NOT NULL,
"userId" TEXT NOT NULL,