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 exchangeRate and convertedUnitPrice to quoteLinePrice table

Rob Carrington8b227f1da79b
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index 3332c790ea..c920472ebb 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -1031,52 +1031,6 @@ export async function insertCustomerLocation(
.single();
}
-export async function insertQuoteLinePrice(
- client: SupabaseClient<Database>,
- quoteLinePrice: {
- quoteId: string;
- quoteLineId: string;
- quantity?: number;
- markupPercent?: number;
- unitCost?: number;
- createdBy: string;
- }
-) {
- const quoteLine = await getQuoteLine(client, quoteLinePrice.quoteLineId);
- if (quoteLine.error) {
- return quoteLine;
- }
-
- let unitCost = 0;
-
- if (quoteLine.data?.methodType !== "Make") {
- const itemId = quoteLine.data?.itemId;
- if (!itemId) {
- throw new Error("itemId not found");
- }
- const [itemCost] = await Promise.all([
- client.from("itemCost").select("unitCost").eq("itemId", itemId).single(),
- ]);
-
- if (itemCost.error) {
- return itemCost;
- }
-
- unitCost = itemCost.data?.unitCost;
- }
-
- const totalCost = unitCost * (quoteLinePrice.quantity ?? 0);
-
- return client.from("quoteLinePrice").insert([
- {
- ...quoteLinePrice,
- unitCost,
- extendedPrice:
- totalCost + totalCost * ((quoteLinePrice?.markupPercent ?? 0) / 100),
- },
- ]);
-}
-
export async function insertSalesOrderLines(
client: SupabaseClient<Database>,
salesOrderLines:
@@ -1664,9 +1618,9 @@ export async function upsertQuoteLineAdditionalCharges(
export async function upsertQuoteLinePrices(
client: SupabaseClient<Database>,
+ quoteId: string,
lineId: string,
quoteLinePrices: {
- quoteId: string;
quoteLineId: string;
unitPrice: number;
leadTime: number;
@@ -1691,8 +1645,20 @@ export async function upsertQuoteLinePrices(
return deletePrices;
}
+ const quoteExchangeRate = await client
+ .from("quote")
+ .select("id, exchangeRate")
+ .eq("id", quoteId)
+ .single();
+
const pricesByQuantity = existingPrices.data.reduce<
- Record<number, { discountPercent: number; leadTime: number }>
+ Record<
+ number,
+ {
+ discountPercent: number;
+ leadTime: number;
+ }
+ >
>((acc, price) => {
acc[price.quantity] = price;
return acc;
@@ -1704,9 +1670,15 @@ export async function upsertQuoteLinePrices(
...p,
discountPercent: pricesByQuantity[p.quantity].discountPercent,
leadTime: pricesByQuantity[p.quantity].leadTime,
+ quoteId: quoteId,
+ exchangeRate: quoteExchangeRate.data?.exchangeRate ?? 1,
};
}
- return p;
+ return {
+ ...p,
+ quoteId: quoteId,
+ exchangeRate: quoteExchangeRate.data?.exchangeRate ?? 1,
+ };
});
return client
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
index b0931cd734..83c28f8765 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
@@ -227,6 +227,12 @@ const QuoteLinePricing = ({
value: number
) => {
if (!carbon) return;
+ const quoteExchangeRate = await carbon
+ .from("quote")
+ .select("id, exchangeRate")
+ .eq("id", quoteId)
+ .single();
+
const hasPrice = prices[quantity];
const oldPrices = { ...prices };
const newPrices = { ...oldPrices };
@@ -238,6 +244,7 @@ const QuoteLinePricing = ({
leadTime: 0,
unitPrice: 0,
discountPercent: 0,
+ exchangeRate: quoteExchangeRate.data?.exchangeRate ?? 1,
createdBy: userId,
} as unknown as QuotationPrice;
}
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.recalculate-price.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.recalculate-price.tsx
index 3b1fb39808..e70fc3fc46 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.recalculate-price.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.recalculate-price.tsx
@@ -43,7 +43,6 @@ export async function action({ request, params }: ActionFunctionArgs) {
}
const inserts = unitCostsByQuantity.data.map((unitCost, index) => ({
- quoteId,
quoteLineId: lineId,
quantity: quantities.data[index],
unitPrice: unitCost * (1 + markup / 100),
@@ -52,7 +51,12 @@ export async function action({ request, params }: ActionFunctionArgs) {
createdBy: userId,
}));
- const insertLinePrices = await upsertQuoteLinePrices(client, lineId, inserts);
+ const insertLinePrices = await upsertQuoteLinePrices(
+ client,
+ quoteId,
+ lineId,
+ inserts
+ );
if (insertLinePrices.error) {
return json(
{ data: null, error: insertLinePrices.error.message },
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 59a00b6f95..285ac71914 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -20604,6 +20604,12 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLinePrice.unitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.exchangeRate",
+ },
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.convertedUnitPrice",
+ },
{
$ref: "#/parameters/select",
},
@@ -20693,6 +20699,12 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLinePrice.unitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.exchangeRate",
+ },
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.convertedUnitPrice",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -20736,6 +20748,12 @@ export default {
{
$ref: "#/parameters/rowFilter.quoteLinePrice.unitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.exchangeRate",
+ },
+ {
+ $ref: "#/parameters/rowFilter.quoteLinePrice.convertedUnitPrice",
+ },
{
$ref: "#/parameters/body.quoteLinePrice",
},
@@ -45789,6 +45807,15 @@ export default {
format: "numeric",
type: "number",
},
+ exchangeRate: {
+ default: 1,
+ format: "numeric",
+ type: "number",
+ },
+ convertedUnitPrice: {
+ format: "numeric",
+ type: "number",
+ },
},
type: "object",
},
@@ -64382,6 +64409,20 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.quoteLinePrice.exchangeRate": {
+ name: "exchangeRate",
+ required: false,
+ format: "numeric",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.quoteLinePrice.convertedUnitPrice": {
+ name: "convertedUnitPrice",
+ required: false,
+ format: "numeric",
+ in: "query",
+ type: "string",
+ },
"body.makeMethod": {
name: "makeMethod",
description: "makeMethod",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index e70dc4f4ac..b507bb9212 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -14609,9 +14609,11 @@ export type Database = {
}
quoteLinePrice: {
Row: {
+ convertedUnitPrice: number | null
createdAt: string
createdBy: string
discountPercent: number
+ exchangeRate: number | null
leadTime: number
quantity: number
quoteId: string
@@ -14621,9 +14623,11 @@ export type Database = {
updatedBy: string | null
}
Insert: {
+ convertedUnitPrice?: number | null
createdAt?: string
createdBy: string
discountPercent?: number
+ exchangeRate?: number | null
leadTime?: number
quantity?: number
quoteId: string
@@ -14633,9 +14637,11 @@ export type Database = {
updatedBy?: string | null
}
Update: {
+ convertedUnitPrice?: number | null
createdAt?: string
createdBy?: string
discountPercent?: number
+ exchangeRate?: number | null
leadTime?: number
quantity?: number
quoteId?: string
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index e70dc4f4ac..b507bb9212 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -14609,9 +14609,11 @@ export type Database = {
}
quoteLinePrice: {
Row: {
+ convertedUnitPrice: number | null
createdAt: string
createdBy: string
discountPercent: number
+ exchangeRate: number | null
leadTime: number
quantity: number
quoteId: string
@@ -14621,9 +14623,11 @@ export type Database = {
updatedBy: string | null
}
Insert: {
+ convertedUnitPrice?: number | null
createdAt?: string
createdBy: string
discountPercent?: number
+ exchangeRate?: number | null
leadTime?: number
quantity?: number
quoteId: string
@@ -14633,9 +14637,11 @@ export type Database = {
updatedBy?: string | null
}
Update: {
+ convertedUnitPrice?: number | null
createdAt?: string
createdBy?: string
discountPercent?: number
+ exchangeRate?: number | null
leadTime?: number
quantity?: number
quoteId?: string
diff --git a/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql b/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql
index d4a46fbd84..ef9a672161 100644
--- a/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql
+++ b/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql
@@ -116,4 +116,26 @@ CREATE POLICY "Employees with accounting_view can view currencies" ON "currency"
);
DROP POLICY IF EXISTS "Employees with accounting_create can insert currencies" ON "currency";
-DROP POLICY IF EXISTS "Employees with accounting_delete can delete currencies" ON "currency";
\ No newline at end of file
+DROP POLICY IF EXISTS "Employees with accounting_delete can delete currencies" ON "currency";
+
+-- Add exchangeRate and convertedUnitPrice to quoteLinePrice
+ALTER TABLE "quoteLinePrice" ADD COLUMN "exchangeRate" NUMERIC(10,4) DEFAULT 1;
+ALTER TABLE "quoteLinePrice" ADD COLUMN "convertedUnitPrice" NUMERIC(10,5) GENERATED ALWAYS AS ("unitPrice" * "exchangeRate") STORED;
+
+-- Add a trigger to update the exchangeRate on quoteLinePrice when the exchangeRate is updated
+CREATE OR REPLACE FUNCTION update_quote_line_price_exchange_rate()
+RETURNS TRIGGER AS $$
+BEGIN
+ UPDATE "quoteLinePrice"
+ SET "exchangeRate" = NEW."exchangeRate"
+ WHERE "quoteId" = NEW."id";
+
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql SECURITY DEFINER;
+
+CREATE TRIGGER update_quote_line_price_exchange_rate_trigger
+AFTER UPDATE OF "exchangeRate" ON "quote"
+FOR EACH ROW
+WHEN (OLD."exchangeRate" IS DISTINCT FROM NEW."exchangeRate")
+EXECUTE FUNCTION update_quote_line_price_exchange_rate();