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: round unit price in quoteLinePrice based on quoteLine unitPricePrecision

Rob Carrington1dcd80273b1f
diff --git a/apps/carbon/app/modules/sales/sales.service.ts b/apps/carbon/app/modules/sales/sales.service.ts
index eed5eee0b0..ec24672789 100644
--- a/apps/carbon/app/modules/sales/sales.service.ts
+++ b/apps/carbon/app/modules/sales/sales.service.ts
@@ -1745,6 +1745,12 @@ export async function upsertQuoteLinePrices(
.eq("id", quoteId)
.single();
+ const quoteLineUnitPricePrecision = await client
+ .from("quoteLine")
+ .select("unitPricePrecision")
+ .eq("id", lineId)
+ .single();
+
const pricesByQuantity = existingPrices.data.reduce<
Record<
number,
@@ -1762,6 +1768,12 @@ export async function upsertQuoteLinePrices(
if (p.quantity in pricesByQuantity) {
return {
...p,
+ unitPrice: Number(
+ // Round the unit price to the precision of the quote line
+ p.unitPrice.toFixed(
+ quoteLineUnitPricePrecision.data?.unitPricePrecision ?? 2
+ )
+ ),
discountPercent: pricesByQuantity[p.quantity].discountPercent,
leadTime: pricesByQuantity[p.quantity].leadTime,
quoteId: quoteId,
@@ -1770,6 +1782,12 @@ export async function upsertQuoteLinePrices(
}
return {
...p,
+ unitPrice: Number(
+ // Round the unit price to the precision of the quote line
+ p.unitPrice.toFixed(
+ quoteLineUnitPricePrecision.data?.unitPricePrecision ?? 2
+ )
+ ),
quoteId: quoteId,
exchangeRate: quoteExchangeRate.data?.exchangeRate ?? 1,
};
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
index 00310cea79..d4ed98b3f7 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
@@ -76,6 +76,8 @@ const QuoteLinePricing = ({
setPrices(pricesByQuantity);
}, [pricesByQuantity]);
+ const unitPricePrecision = line.unitPricePrecision ?? 2;
+
const routeData = useRouteData<{
quote: Quotation;
}>(path.to.quote(quoteId));
@@ -242,6 +244,13 @@ const QuoteLinePricing = ({
.eq("id", quoteId)
.single();
+ const quoteLineData = await carbon
+ .from("quoteLine")
+ .select("unitPricePrecision")
+ .eq("id", lineId)
+ .single();
+ const unitPricePrecision = quoteLineData.data?.unitPricePrecision ?? 2;
+
const hasPrice = prices[quantity];
const oldPrices = { ...prices };
const newPrices = { ...oldPrices };
@@ -258,14 +267,19 @@ const QuoteLinePricing = ({
createdBy: userId,
} as unknown as QuotationPrice;
}
- newPrices[quantity] = { ...newPrices[quantity], [key]: value };
+ let roundedValue = value;
+ if (key === "unitPrice") {
+ // Round the value to the precision of the quote line
+ roundedValue = Number(value.toFixed(unitPricePrecision));
+ }
+ newPrices[quantity] = { ...newPrices[quantity], [key]: roundedValue };
setPrices(newPrices);
if (hasPrice) {
const { error } = await carbon
.from("quoteLinePrice")
- .update({ [key]: value })
+ .update({ [key]: roundedValue })
.eq("quoteLineId", lineId)
.eq("quantity", quantity);
if (error) {
@@ -522,6 +536,7 @@ const QuoteLinePricing = ({
formatOptions={{
style: "currency",
currency: baseCurrency,
+ maximumFractionDigits: unitPricePrecision,
}}
minValue={0}
onChange={(value) => {
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.update-precision.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.update-precision.tsx
index cb1a2b66d9..b8ab4e5205 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.update-precision.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.$lineId.update-precision.tsx
@@ -1,7 +1,10 @@
import { assertIsPost } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { json, type ActionFunctionArgs } from "@vercel/remix";
-import { updateQuoteLinePrecision } from "~/modules/sales";
+import {
+ updateQuoteLinePrecision,
+ upsertQuoteLinePrices,
+} from "~/modules/sales";
export async function action({ request, params }: ActionFunctionArgs) {
assertIsPost(request);
@@ -30,5 +33,35 @@ export async function action({ request, params }: ActionFunctionArgs) {
);
}
+ // Update all the prices for the quote line to reflect the new precision
+ const prices = await client
+ .from("quoteLinePrice")
+ .select("*")
+ .eq("quoteLineId", lineId);
+
+ if (prices.data) {
+ const roundedPrices = prices.data?.map((price) => ({
+ quoteLineId: price.quoteLineId,
+ unitPrice: Number(price.unitPrice.toFixed(precision)),
+ leadTime: price.leadTime,
+ discountPercent: price.discountPercent,
+ quantity: price.quantity,
+ createdBy: price.createdBy,
+ }));
+
+ const updatePrices = await upsertQuoteLinePrices(
+ client,
+ quoteId,
+ lineId,
+ roundedPrices
+ );
+ if (updatePrices.error) {
+ return json(
+ { data: null, error: updatePrices.error.message },
+ { status: 400 }
+ );
+ }
+ }
+
return json({ data: null, error: null });
}
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 112de58cc3..69aba8b9ce 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -15380,7 +15380,6 @@ export type Database = {
quoteLineId: string
shippingCost: number
unitPrice: number
- unitPricePrecision: number
updatedAt: string | null
updatedBy: string | null
}
@@ -15401,7 +15400,6 @@ export type Database = {
quoteLineId: string
shippingCost?: number
unitPrice?: number
- unitPricePrecision?: number
updatedAt?: string | null
updatedBy?: string | null
}
@@ -15422,7 +15420,6 @@ export type Database = {
quoteLineId?: string
shippingCost?: number
unitPrice?: number
- unitPricePrecision?: number
updatedAt?: string | null
updatedBy?: string | null
}
@@ -27911,6 +27908,7 @@ export type Database = {
thumbnailPath: string | null
unitCost: number | null
unitOfMeasureCode: string | null
+ unitPricePrecision: number | null
updatedAt: string | null
updatedBy: string | null
}
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 112de58cc3..69aba8b9ce 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -15380,7 +15380,6 @@ export type Database = {
quoteLineId: string
shippingCost: number
unitPrice: number
- unitPricePrecision: number
updatedAt: string | null
updatedBy: string | null
}
@@ -15401,7 +15400,6 @@ export type Database = {
quoteLineId: string
shippingCost?: number
unitPrice?: number
- unitPricePrecision?: number
updatedAt?: string | null
updatedBy?: string | null
}
@@ -15422,7 +15420,6 @@ export type Database = {
quoteLineId?: string
shippingCost?: number
unitPrice?: number
- unitPricePrecision?: number
updatedAt?: string | null
updatedBy?: string | null
}
@@ -27911,6 +27908,7 @@ export type Database = {
thumbnailPath: string | null
unitCost: number | null
unitOfMeasureCode: string | null
+ unitPricePrecision: number | null
updatedAt: string | null
updatedBy: string | null
}
diff --git a/packages/database/supabase/migrations/20241206142751_quote-unit-price-rounding.sql b/packages/database/supabase/migrations/20241206142751_quote-unit-price-rounding.sql
index 47362ac870..4c0a921240 100644
--- a/packages/database/supabase/migrations/20241206142751_quote-unit-price-rounding.sql
+++ b/packages/database/supabase/migrations/20241206142751_quote-unit-price-rounding.sql
@@ -1,21 +1,24 @@
-- Add the unitPricePrecision to quoteLine and quoteLinePrice
ALTER TABLE "quoteLine" ADD COLUMN "unitPricePrecision" INTEGER NOT NULL DEFAULT 2 CHECK ("unitPricePrecision" IN (2, 3, 4));
-ALTER TABLE "quoteLinePrice" ADD COLUMN "unitPricePrecision" INTEGER NOT NULL DEFAULT 2 CHECK ("unitPricePrecision" IN (2, 3, 4));
--- Add a trigger to update the unitPricePrecision on quoteLinePrice when the unitPricePrecision on quoteLine is updated
-CREATE OR REPLACE FUNCTION update_quote_line_price_unit_price_precision()
-RETURNS TRIGGER AS $$
-BEGIN
- UPDATE "quoteLinePrice"
- SET "unitPricePrecision" = NEW."unitPricePrecision"
- WHERE "quoteLineId" = NEW."id";
-
- RETURN NEW;
-END;
-$$ LANGUAGE plpgsql SECURITY DEFINER;
-
-CREATE TRIGGER update_quote_line_price_unit_price_precision_trigger
-AFTER UPDATE OF "unitPricePrecision" ON "quoteLine"
-FOR EACH ROW
-WHEN (OLD."unitPricePrecision" IS DISTINCT FROM NEW."unitPricePrecision")
-EXECUTE FUNCTION update_quote_line_price_unit_price_precision();
\ No newline at end of file
+DROP VIEW IF EXISTS "quoteLines";
+CREATE OR REPLACE VIEW "quoteLines" WITH(SECURITY_INVOKER=true) AS (
+ SELECT
+ ql.*,
+ CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ WHEN i."thumbnailPath" IS NULL AND imu."thumbnailPath" IS NOT NULL THEN imu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END as "thumbnailPath",
+ COALESCE(mu.id, imu.id) as "modelId",
+ COALESCE(mu."autodeskUrn", imu."autodeskUrn") as "autodeskUrn",
+ COALESCE(mu."modelPath", imu."modelPath") as "modelPath",
+ COALESCE(mu."name", imu."name") as "modelName",
+ COALESCE(mu."size", imu."size") as "modelSize",
+ ic."unitCost" as "unitCost"
+ FROM "quoteLine" ql
+ LEFT JOIN "modelUpload" mu ON ql."modelUploadId" = mu."id"
+ INNER JOIN "item" i ON i.id = ql."itemId"
+ LEFT JOIN "itemCost" ic ON ic."itemId" = i.id
+ LEFT JOIN "modelUpload" imu ON imu.id = i."modelUploadId"
+);
\ No newline at end of file