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: render converted currencies on quotes

Rob Carrington37d48db991f6
diff --git a/apps/carbon/app/hooks/useCurrencyFormatter.tsx b/apps/carbon/app/hooks/useCurrencyFormatter.tsx
index 429d56b68e..4a00584fe5 100644
--- a/apps/carbon/app/hooks/useCurrencyFormatter.tsx
+++ b/apps/carbon/app/hooks/useCurrencyFormatter.tsx
@@ -2,17 +2,18 @@ import { useLocale } from "@react-aria/i18n";
import { useMemo } from "react";
import { useUser } from "./useUser";
-export function useCurrencyFormatter() {
+export function useCurrencyFormatter(currencyCode?: string) {
const { company } = useUser();
const baseCurrency = company?.baseCurrencyCode ?? "USD";
const { locale } = useLocale();
+ const currency = currencyCode ?? baseCurrency;
const formatter = useMemo(
() =>
new Intl.NumberFormat(locale, {
style: "currency",
- currency: baseCurrency,
+ currency: currency,
}),
- [locale, baseCurrency]
+ [locale, currency]
);
return formatter;
}
diff --git a/apps/carbon/app/modules/accounting/accounting.service.ts b/apps/carbon/app/modules/accounting/accounting.service.ts
index 4f0682af6d..b57b0e1453 100644
--- a/apps/carbon/app/modules/accounting/accounting.service.ts
+++ b/apps/carbon/app/modules/accounting/accounting.service.ts
@@ -409,6 +409,19 @@ export async function getCurrency(
return client.from("currencies").select("*").eq("id", currencyId).single();
}
+export async function getCurrencyByCode(
+ client: SupabaseClient<Database>,
+ companyId: string,
+ currencyCode: string
+) {
+ return client
+ .from("currencies")
+ .select("*")
+ .eq("code", currencyCode)
+ .eq("companyId", companyId)
+ .single();
+}
+
export async function getCurrencies(
client: SupabaseClient<Database>,
companyId: string,
diff --git a/apps/carbon/app/modules/accounting/ui/Currencies/CurrencyForm.tsx b/apps/carbon/app/modules/accounting/ui/Currencies/CurrencyForm.tsx
index c56dba4fb0..605b2a8427 100644
--- a/apps/carbon/app/modules/accounting/ui/Currencies/CurrencyForm.tsx
+++ b/apps/carbon/app/modules/accounting/ui/Currencies/CurrencyForm.tsx
@@ -41,7 +41,7 @@ const CurrencyForm = ({ initialValues }: CurrencyFormProps) => {
const isBaseCurrency = company?.baseCurrencyCode === initialValues.code;
const exchangeRateHelperText = isBaseCurrency
? "This is the base currency. Exchange rate is always 1."
- : `One ${initialValues.code} is equal to how many ${company?.baseCurrencyCode}?`;
+ : `One ${company?.baseCurrencyCode} is equal to how many ${initialValues.code}?`;
const isEditing = initialValues.id !== undefined;
const isDisabled = isEditing
diff --git a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
index 355f6f9d68..e6e837935f 100644
--- a/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
+++ b/apps/carbon/app/modules/sales/ui/Quotes/QuoteLinePricing.tsx
@@ -78,6 +78,7 @@ const QuoteLinePricing = ({
const routeData = useRouteData<{
quote: Quotation;
+ presentationExchangeRate: number;
}>(path.to.quote(quoteId));
const isEditable =
permissions.can("update", "sales") &&
@@ -106,6 +107,9 @@ const QuoteLinePricing = ({
const baseCurrency = company?.baseCurrencyCode ?? "USD";
const formatter = useCurrencyFormatter();
+ const presentationCurrencyFormatter = useCurrencyFormatter(
+ routeData?.quote?.presentationCurrencyCode ?? baseCurrency
+ );
const additionalCharges = useMemo(() => {
if (fetcher.formAction === path.to.quoteLineCost(quoteId, lineId)) {
@@ -751,6 +755,49 @@ const QuoteLinePricing = ({
);
})}
</Tr>
+ {routeData?.quote?.presentationCurrencyCode !== baseCurrency && (
+ <>
+ <Tr className="[&>td]:bg-muted/60">
+ <Td className="border-r border-border group-hover:bg-muted/50">
+ <HStack className="w-full justify-between ">
+ <span>Exchange Rate</span>
+ </HStack>
+ </Td>
+ {quantities.map((quantity, index) => (
+ <Td key={index} className="group-hover:bg-muted/50">
+ <VStack spacing={0}>
+ <span>{routeData?.presentationExchangeRate ?? 1}</span>
+ </VStack>
+ </Td>
+ ))}
+ </Tr>
+ <Tr className="font-bold [&>td]:bg-muted/60">
+ <Td className="border-r border-border group-hover:bg-muted/50">
+ <HStack className="w-full justify-between ">
+ <span>Converted Total Price</span>
+ </HStack>
+ </Td>
+ {quantities.map((quantity, index) => {
+ const price =
+ netPricesByQuantity[index] * quantity +
+ additionalChargesByQuantity[index];
+ const convertedPrice =
+ price * (routeData?.presentationExchangeRate ?? 1);
+ return (
+ <Td key={index} className="group-hover:bg-muted/50">
+ <VStack spacing={0}>
+ <span>
+ {presentationCurrencyFormatter.format(
+ convertedPrice
+ )}
+ </span>
+ </VStack>
+ </Td>
+ );
+ })}
+ </Tr>
+ </>
+ )}
</Tbody>
</Table>
</CardContent>
diff --git a/apps/carbon/app/routes/file+/quote+/$id[.]pdf.tsx b/apps/carbon/app/routes/file+/quote+/$id[.]pdf.tsx
index 3e462f5fbd..7f83f29ddd 100644
--- a/apps/carbon/app/routes/file+/quote+/$id[.]pdf.tsx
+++ b/apps/carbon/app/routes/file+/quote+/$id[.]pdf.tsx
@@ -3,7 +3,7 @@ import { QuotePDF } from "@carbon/documents";
import type { JSONContent } from "@carbon/react";
import { renderToStream } from "@react-pdf/renderer";
import { type LoaderFunctionArgs } from "@vercel/remix";
-import { getPaymentTermsList } from "~/modules/accounting";
+import { getCurrencyByCode, getPaymentTermsList } from "~/modules/accounting";
import { getShippingMethodsList } from "~/modules/inventory";
import {
getQuote,
@@ -109,10 +109,23 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
return acc;
}, {}) ?? {};
+ let presentationExchangeRate = 1;
+ if (quote.data?.presentationCurrencyCode) {
+ const presentationCurrency = await getCurrencyByCode(
+ client,
+ companyId,
+ quote.data.presentationCurrencyCode
+ );
+ if (presentationCurrency.data?.exchangeRate) {
+ presentationExchangeRate = presentationCurrency.data.exchangeRate;
+ }
+ }
+
const stream = await renderToStream(
<QuotePDF
company={company.data}
locale={locale}
+ presentationExchangeRate={presentationExchangeRate}
quote={quote.data}
quoteLines={quoteLines.data ?? []}
quoteLinePrices={quoteLinePrices.data ?? []}
diff --git a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
index 9a24e14eb8..33d6461ff2 100644
--- a/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
+++ b/apps/carbon/app/routes/x+/quote+/$quoteId.tsx
@@ -12,6 +12,7 @@ import {
import { Outlet, useLoaderData, useParams } from "@remix-run/react";
import type { LoaderFunctionArgs } from "@vercel/remix";
import { defer, redirect } from "@vercel/remix";
+import { getCurrencyByCode } from "~/modules/accounting";
import {
getOpportunityByQuote,
getOpportunityDocuments,
@@ -84,6 +85,18 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
);
}
+ let presentationExchangeRate = 1;
+ if (quote.data?.presentationCurrencyCode) {
+ const presentationCurrency = await getCurrencyByCode(
+ client,
+ companyId,
+ quote.data.presentationCurrencyCode
+ );
+ if (presentationCurrency.data?.exchangeRate) {
+ presentationExchangeRate = presentationCurrency.data.exchangeRate;
+ }
+ }
+
return defer({
quote: quote.data,
lines: lines.data ?? [],
@@ -93,6 +106,7 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
shipment: shipment.data,
payment: payment.data,
opportunity: opportunity.data,
+ presentationExchangeRate,
});
}
diff --git a/packages/documents/src/pdf/QuotePDF.tsx b/packages/documents/src/pdf/QuotePDF.tsx
index b0c789ed11..caeb16fdcf 100644
--- a/packages/documents/src/pdf/QuotePDF.tsx
+++ b/packages/documents/src/pdf/QuotePDF.tsx
@@ -11,6 +11,7 @@ import { getCurrencyFormatter } from "../utils/shared";
import { Header, Note, Summary, Template } from "./components";
interface QuotePDFProps extends PDF {
+ presentationExchangeRate: number;
quote: Database["public"]["Views"]["quotes"]["Row"];
quoteLines: Database["public"]["Views"]["quoteLines"]["Row"][];
quoteCustomerDetails: Database["public"]["Views"]["quoteCustomerDetails"]["Row"];
@@ -42,6 +43,7 @@ const QuotePDF = ({
company,
locale,
meta,
+ presentationExchangeRate,
quote,
quoteLines,
quoteLinePrices,
@@ -64,7 +66,10 @@ const QuotePDF = ({
customerCountryName,
} = quoteCustomerDetails;
- const formatter = getCurrencyFormatter(company.baseCurrencyCode, locale);
+ const currencyCode =
+ quote.presentationCurrencyCode ?? company.baseCurrencyCode;
+ const shouldConvertCurrency = currencyCode !== company.baseCurrencyCode;
+ const formatter = getCurrencyFormatter(currencyCode, locale);
const pricesByLine = quoteLinePrices.reduce<
Record<string, Database["public"]["Tables"]["quoteLinePrice"]["Row"][]>
@@ -72,7 +77,11 @@ const QuotePDF = ({
if (!acc[price.quoteLineId]) {
acc[price.quoteLineId] = [];
}
- acc[price.quoteLineId].push(price);
+ const convertedPrice = { ...price };
+ if (shouldConvertCurrency) {
+ convertedPrice.unitPrice = price.unitPrice * presentationExchangeRate;
+ }
+ acc[price.quoteLineId].push(convertedPrice);
return acc;
}, {});
@@ -160,10 +169,10 @@ const QuotePDF = ({
"flex flex-row justify-between items-center py-1.5 px-[6px] border-t border-b border-gray-300 text-gray-500 font-bold uppercase"
)}
>
- <View style={tw("w-2/5")}>
+ <View style={tw("w-1/3")}>
<Text>Description</Text>
</View>
- <View style={tw("w-3/5 flex flex-row")}>
+ <View style={tw("w-2/3 flex flex-row")}>
<Text style={tw("w-1/5 text-right")}>Qty</Text>
<Text style={tw("w-1/5 text-right")}>Unit Price</Text>
<Text style={tw("w-1/5 text-right")}>Add-Ons</Text>
@@ -178,7 +187,10 @@ const QuotePDF = ({
(quantity) => {
const charges = Object.values(additionalCharges).reduce(
(acc, charge) => {
- const amount = charge.amounts?.[quantity] ?? 0;
+ let amount = charge.amounts?.[quantity] ?? 0;
+ if (shouldConvertCurrency) {
+ amount *= presentationExchangeRate;
+ }
return acc + amount;
},
0
@@ -194,7 +206,7 @@ const QuotePDF = ({
)}
key={line.id}
>
- <View style={tw("w-2/5")}>
+ <View style={tw("w-1/3")}>
<View>
<Text style={tw("font-bold mb-1")}>
{getLineDescription(line)}
@@ -236,7 +248,7 @@ const QuotePDF = ({
</View>
)}
</View>
- <View style={tw("flex flex-col w-3/5 gap-2")}>
+ <View style={tw("flex flex-col w-2/3 gap-2")}>
{line.status !== "No Quote" ? (
line.quantity.map((quantity, index) => {
const prices = pricesByLine[line.id] ?? [];