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 rpc method for creating a sales rfq with multiple lines

Brad Barbin05d5227d58d6
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index d88ac9ae50..18de816a23 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -5926,6 +5926,7 @@ export type Database = {
itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId: string | null
name: string
+ notes: Json | null
readableId: string
replenishmentSystem: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath: string | null
@@ -5947,6 +5948,7 @@ export type Database = {
itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId?: string | null
name: string
+ notes?: Json | null
readableId: string
replenishmentSystem?: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath?: string | null
@@ -5968,6 +5970,7 @@ export type Database = {
itemTrackingType?: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId?: string | null
name?: string
+ notes?: Json | null
readableId?: string
replenishmentSystem?: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath?: string | null
@@ -16056,6 +16059,7 @@ export type Database = {
id: string
locationId: string | null
receiptRequestedDate: string | null
+ shippingCost: number | null
shippingMethodId: string | null
shippingTermId: string | null
updatedAt: string | null
@@ -16066,6 +16070,7 @@ export type Database = {
id: string
locationId?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
updatedAt?: string | null
@@ -16076,6 +16081,7 @@ export type Database = {
id?: string
locationId?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
updatedAt?: string | null
@@ -17406,6 +17412,7 @@ export type Database = {
notes: string | null
receiptPromisedDate: string | null
receiptRequestedDate: string | null
+ shippingCost: number | null
shippingMethodId: string | null
shippingTermId: string | null
supplierId: string | null
@@ -17427,6 +17434,7 @@ export type Database = {
notes?: string | null
receiptPromisedDate?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
supplierId?: string | null
@@ -17448,6 +17456,7 @@ export type Database = {
notes?: string | null
receiptPromisedDate?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
supplierId?: string | null
@@ -23145,6 +23154,7 @@ export type Database = {
| Database["public"]["Enums"]["itemTrackingType"]
| null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -25075,6 +25085,7 @@ export type Database = {
materialSubstance: string | null
materialSubstanceId: string | null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -25450,6 +25461,7 @@ export type Database = {
modelPath: string | null
modelSize: number | null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -27273,7 +27285,9 @@ export type Database = {
salesOrderId: string | null
salesPersonId: string | null
salesRfqId: string | null
+ shippingCost: number | null
status: Database["public"]["Enums"]["quoteStatus"] | null
+ tags: string[] | null
updatedAt: string | null
updatedBy: string | null
}
@@ -28081,6 +28095,7 @@ export type Database = {
revisionId: number | null
salesOrderId: string | null
salesPersonId: string | null
+ shippingCost: number | null
shippingMethodName: string | null
shippingTermName: string | null
status: Database["public"]["Enums"]["salesOrderStatus"] | null
@@ -29509,6 +29524,7 @@ export type Database = {
| Database["public"]["Enums"]["itemTrackingType"]
| null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -29914,6 +29930,19 @@ export type Database = {
rfq_line_id: string
}[]
}
+ create_rfq_from_models_v1: {
+ Args: {
+ company_id: string
+ email: string
+ sequence_number: string
+ model_data: Json[]
+ }
+ Returns: {
+ rfq_id: string
+ rfq_readable_id: string
+ rfq_line_ids: string[]
+ }[]
+ }
get_active_job_count: {
Args: {
employee_id: string
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index d88ac9ae50..18de816a23 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -5926,6 +5926,7 @@ export type Database = {
itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId: string | null
name: string
+ notes: Json | null
readableId: string
replenishmentSystem: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath: string | null
@@ -5947,6 +5948,7 @@ export type Database = {
itemTrackingType: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId?: string | null
name: string
+ notes?: Json | null
readableId: string
replenishmentSystem?: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath?: string | null
@@ -5968,6 +5970,7 @@ export type Database = {
itemTrackingType?: Database["public"]["Enums"]["itemTrackingType"]
modelUploadId?: string | null
name?: string
+ notes?: Json | null
readableId?: string
replenishmentSystem?: Database["public"]["Enums"]["itemReplenishmentSystem"]
thumbnailPath?: string | null
@@ -16056,6 +16059,7 @@ export type Database = {
id: string
locationId: string | null
receiptRequestedDate: string | null
+ shippingCost: number | null
shippingMethodId: string | null
shippingTermId: string | null
updatedAt: string | null
@@ -16066,6 +16070,7 @@ export type Database = {
id: string
locationId?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
updatedAt?: string | null
@@ -16076,6 +16081,7 @@ export type Database = {
id?: string
locationId?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
updatedAt?: string | null
@@ -17406,6 +17412,7 @@ export type Database = {
notes: string | null
receiptPromisedDate: string | null
receiptRequestedDate: string | null
+ shippingCost: number | null
shippingMethodId: string | null
shippingTermId: string | null
supplierId: string | null
@@ -17427,6 +17434,7 @@ export type Database = {
notes?: string | null
receiptPromisedDate?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
supplierId?: string | null
@@ -17448,6 +17456,7 @@ export type Database = {
notes?: string | null
receiptPromisedDate?: string | null
receiptRequestedDate?: string | null
+ shippingCost?: number | null
shippingMethodId?: string | null
shippingTermId?: string | null
supplierId?: string | null
@@ -23145,6 +23154,7 @@ export type Database = {
| Database["public"]["Enums"]["itemTrackingType"]
| null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -25075,6 +25085,7 @@ export type Database = {
materialSubstance: string | null
materialSubstanceId: string | null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -25450,6 +25461,7 @@ export type Database = {
modelPath: string | null
modelSize: number | null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -27273,7 +27285,9 @@ export type Database = {
salesOrderId: string | null
salesPersonId: string | null
salesRfqId: string | null
+ shippingCost: number | null
status: Database["public"]["Enums"]["quoteStatus"] | null
+ tags: string[] | null
updatedAt: string | null
updatedBy: string | null
}
@@ -28081,6 +28095,7 @@ export type Database = {
revisionId: number | null
salesOrderId: string | null
salesPersonId: string | null
+ shippingCost: number | null
shippingMethodName: string | null
shippingTermName: string | null
status: Database["public"]["Enums"]["salesOrderStatus"] | null
@@ -29509,6 +29524,7 @@ export type Database = {
| Database["public"]["Enums"]["itemTrackingType"]
| null
name: string | null
+ notes: Json | null
replenishmentSystem:
| Database["public"]["Enums"]["itemReplenishmentSystem"]
| null
@@ -29914,6 +29930,19 @@ export type Database = {
rfq_line_id: string
}[]
}
+ create_rfq_from_models_v1: {
+ Args: {
+ company_id: string
+ email: string
+ sequence_number: string
+ model_data: Json[]
+ }
+ Returns: {
+ rfq_id: string
+ rfq_readable_id: string
+ rfq_line_ids: string[]
+ }[]
+ }
get_active_job_count: {
Args: {
employee_id: string
diff --git a/packages/database/supabase/migrations/20241127133125_sales-rfq-from-models-rpc.sql b/packages/database/supabase/migrations/20241127133125_sales-rfq-from-models-rpc.sql
new file mode 100644
index 0000000000..2659cb4e1a
--- /dev/null
+++ b/packages/database/supabase/migrations/20241127133125_sales-rfq-from-models-rpc.sql
@@ -0,0 +1,97 @@
+
+CREATE OR REPLACE FUNCTION create_rfq_from_models_v1(
+ company_id text,
+ email text,
+ sequence_number text,
+ model_data json[]
+)
+RETURNS TABLE (
+ rfq_id text,
+ rfq_readable_id text,
+ rfq_line_ids text[]
+)
+LANGUAGE plpgsql
+SECURITY DEFINER
+SET search_path = public
+AS $$
+DECLARE
+ v_rfq_id text;
+ v_line_ids text[] := '{}';
+ v_customer_id text;
+ v_line_id text;
+ model_item json;
+BEGIN
+ IF session_user = 'authenticator' THEN
+ IF NOT (has_role('employee', company_id) OR has_valid_api_key_for_company(company_id)) THEN
+ RAISE EXCEPTION 'Insufficient permissions';
+ END IF;
+ END IF;
+
+ -- Find customer ID from email
+ SELECT cc."customerId" INTO v_customer_id
+ FROM "contact" c
+ JOIN "customerContact" cc ON cc."contactId" = c.id
+ WHERE c."email" = create_rfq_from_models_v1.email
+ AND c."companyId" = company_id
+ LIMIT 1;
+
+ -- Create RFQ
+ INSERT INTO "salesRfq" (
+ "rfqId",
+ "customerId",
+ "companyId",
+ "createdBy"
+ )
+ VALUES (
+ sequence_number,
+ v_customer_id,
+ company_id,
+ 'system'
+ )
+ RETURNING id INTO v_rfq_id;
+
+ -- Create RFQ Lines with models
+ FOREACH model_item IN ARRAY model_data
+ LOOP
+ INSERT INTO "salesRfqLine" (
+ "salesRfqId",
+ "customerPartId",
+ "modelUploadId",
+ "unitOfMeasureCode",
+ "internalNotes",
+ quantity,
+ "companyId",
+ "createdBy"
+ )
+ VALUES (
+ v_rfq_id,
+ (model_item->>'customer_part_id')::text,
+ (model_item->>'model_id')::text,
+ (model_item->>'unit_of_measure')::text,
+ COALESCE((model_item->>'notes')::json, '{}'::json),
+ ARRAY[1],
+ company_id,
+ 'system'
+ )
+ RETURNING id INTO v_line_id;
+
+ v_line_ids := array_append(v_line_ids, v_line_id);
+ END LOOP;
+
+ -- Create Opportunity
+ INSERT INTO "opportunity" (
+ "salesRfqId",
+ "companyId"
+ )
+ VALUES (
+ v_rfq_id,
+ company_id
+ );
+
+ RETURN QUERY SELECT v_rfq_id, sequence_number, v_line_ids;
+
+EXCEPTION
+ WHEN OTHERS THEN
+ RAISE;
+END;
+$$;