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: update SalesOrderPDF to support currency conversion

Rob Carringtone168d2476911
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index d8a24bc54d..714ba877ec 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -4617,6 +4617,9 @@ export default {
{
$ref: "#/parameters/rowFilter.salesOrderLines.convertedUnitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.salesOrderLines.convertedAddOnCost",
+ },
{
$ref: "#/parameters/rowFilter.salesOrderLines.modelId",
},
@@ -9384,6 +9387,9 @@ export default {
{
$ref: "#/parameters/rowFilter.salesOrderLine.convertedUnitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.salesOrderLine.convertedAddOnCost",
+ },
{
$ref: "#/parameters/select",
},
@@ -9545,6 +9551,9 @@ export default {
{
$ref: "#/parameters/rowFilter.salesOrderLine.convertedUnitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.salesOrderLine.convertedAddOnCost",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -9660,6 +9669,9 @@ export default {
{
$ref: "#/parameters/rowFilter.salesOrderLine.convertedUnitPrice",
},
+ {
+ $ref: "#/parameters/rowFilter.salesOrderLine.convertedAddOnCost",
+ },
{
$ref: "#/parameters/body.salesOrderLine",
},
@@ -38193,6 +38205,10 @@ export default {
format: "numeric",
type: "number",
},
+ convertedAddOnCost: {
+ format: "numeric",
+ type: "number",
+ },
modelId: {
format: "text",
type: "string",
@@ -40755,6 +40771,10 @@ export default {
format: "numeric",
type: "number",
},
+ convertedAddOnCost: {
+ format: "numeric",
+ type: "number",
+ },
},
type: "object",
},
@@ -54530,6 +54550,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.salesOrderLines.convertedAddOnCost": {
+ name: "convertedAddOnCost",
+ required: false,
+ format: "numeric",
+ in: "query",
+ type: "string",
+ },
"rowFilter.salesOrderLines.modelId": {
name: "modelId",
required: false,
@@ -57794,6 +57821,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.salesOrderLine.convertedAddOnCost": {
+ name: "convertedAddOnCost",
+ required: false,
+ format: "numeric",
+ in: "query",
+ type: "string",
+ },
"body.salesOrderTransaction": {
name: "salesOrderTransaction",
description: "salesOrderTransaction",
diff --git a/packages/database/src/types.ts b/packages/database/src/types.ts
index 837b3250e6..21a1a7a10a 100644
--- a/packages/database/src/types.ts
+++ b/packages/database/src/types.ts
@@ -16593,6 +16593,7 @@ export type Database = {
addOnCost: number
assetId: string | null
companyId: string
+ convertedAddOnCost: number | null
convertedUnitPrice: number | null
createdAt: string
createdBy: string
@@ -16629,6 +16630,7 @@ export type Database = {
addOnCost?: number
assetId?: string | null
companyId: string
+ convertedAddOnCost?: number | null
convertedUnitPrice?: number | null
createdAt?: string
createdBy: string
@@ -16665,6 +16667,7 @@ export type Database = {
addOnCost?: number
assetId?: string | null
companyId?: string
+ convertedAddOnCost?: number | null
convertedUnitPrice?: number | null
createdAt?: string
createdBy?: string
@@ -27093,6 +27096,7 @@ export type Database = {
assetId: string | null
autodeskUrn: string | null
companyId: string | null
+ convertedAddOnCost: number | null
convertedUnitPrice: number | null
createdAt: string | null
createdBy: string | null
diff --git a/packages/database/supabase/functions/lib/types.ts b/packages/database/supabase/functions/lib/types.ts
index 837b3250e6..21a1a7a10a 100644
--- a/packages/database/supabase/functions/lib/types.ts
+++ b/packages/database/supabase/functions/lib/types.ts
@@ -16593,6 +16593,7 @@ export type Database = {
addOnCost: number
assetId: string | null
companyId: string
+ convertedAddOnCost: number | null
convertedUnitPrice: number | null
createdAt: string
createdBy: string
@@ -16629,6 +16630,7 @@ export type Database = {
addOnCost?: number
assetId?: string | null
companyId: string
+ convertedAddOnCost?: number | null
convertedUnitPrice?: number | null
createdAt?: string
createdBy: string
@@ -16665,6 +16667,7 @@ export type Database = {
addOnCost?: number
assetId?: string | null
companyId?: string
+ convertedAddOnCost?: number | null
convertedUnitPrice?: number | null
createdAt?: string
createdBy?: string
@@ -27093,6 +27096,7 @@ export type Database = {
assetId: string | null
autodeskUrn: string | null
companyId: string | null
+ convertedAddOnCost: number | null
convertedUnitPrice: number | null
createdAt: string | null
createdBy: string | null
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 d19297fb7a..fdb6c3943b 100644
--- a/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql
+++ b/packages/database/supabase/migrations/20241010193506_quote-order-presentation-currency.sql
@@ -147,6 +147,7 @@ EXECUTE FUNCTION update_quote_line_price_exchange_rate();
-- Add exchangeRate and convertedUnitPrice to salesOrderLine
ALTER TABLE "salesOrderLine" ADD COLUMN "exchangeRate" NUMERIC(10,4) DEFAULT 1;
ALTER TABLE "salesOrderLine" ADD COLUMN "convertedUnitPrice" NUMERIC(10,5) GENERATED ALWAYS AS ("unitPrice" * "exchangeRate") STORED;
+ALTER TABLE "salesOrderLine" ADD COLUMN "convertedAddOnCost" NUMERIC(10,5) GENERATED ALWAYS AS ("addOnCost" * "exchangeRate") STORED;
DROP VIEW "salesOrderLines";
CREATE OR REPLACE VIEW "salesOrderLines" WITH(SECURITY_INVOKER=true) AS (
diff --git a/packages/documents/src/pdf/SalesOrderPDF.tsx b/packages/documents/src/pdf/SalesOrderPDF.tsx
index c1228be766..1083eb4914 100644
--- a/packages/documents/src/pdf/SalesOrderPDF.tsx
+++ b/packages/documents/src/pdf/SalesOrderPDF.tsx
@@ -64,7 +64,9 @@ const SalesOrderPDF = ({
paymentCountryName,
} = salesOrderLocations;
- const formatter = getCurrencyFormatter(company.baseCurrencyCode, locale);
+ const currencyCode = salesOrder.currencyCode ?? company.baseCurrencyCode;
+ const shouldConvertCurrency = currencyCode !== company.baseCurrencyCode;
+ const formatter = getCurrencyFormatter(currencyCode, locale);
return (
<Template
@@ -193,17 +195,25 @@ const SalesOrderPDF = ({
<Text style={tw("w-1/6 text-right")}>
{line.salesOrderLineType === "Comment"
? null
- : formatter.format(line.unitPrice ?? 0)}
+ : formatter.format(
+ shouldConvertCurrency
+ ? line.convertedUnitPrice ?? 0
+ : line.unitPrice ?? 0
+ )}
</Text>
<Text style={tw("w-1/6 text-right")}>
{line.salesOrderLineType === "Comment"
? null
- : formatter.format(line.addOnCost ?? 0)}
+ : formatter.format(
+ shouldConvertCurrency
+ ? line.convertedAddOnCost ?? 0
+ : line.addOnCost ?? 0
+ )}
</Text>
<Text style={tw("w-1/6 text-right")}>
{line.salesOrderLineType === "Comment"
? null
- : formatter.format(getLineTotal(line))}
+ : formatter.format(getLineTotal(line, shouldConvertCurrency))}
</Text>
</View>
))}
@@ -214,7 +224,9 @@ const SalesOrderPDF = ({
>
<Text>Total</Text>
<Text style={tw("font-bold text-black")}>
- {formatter.format(getTotal(salesOrderLines))}
+ {formatter.format(
+ getTotal(salesOrderLines, shouldConvertCurrency)
+ )}
</Text>
</View>
</View>
diff --git a/packages/documents/src/utils/sales-order.ts b/packages/documents/src/utils/sales-order.ts
index c64e2e9ebb..69149f0cb3 100644
--- a/packages/documents/src/utils/sales-order.ts
+++ b/packages/documents/src/utils/sales-order.ts
@@ -37,24 +37,33 @@ export function getLineDescriptionDetails(
}
export function getLineTotal(
- line: Database["public"]["Views"]["salesOrderLines"]["Row"]
+ line: Database["public"]["Views"]["salesOrderLines"]["Row"],
+ shouldConvertCurrency: boolean
) {
- if (line?.saleQuantity && line?.unitPrice) {
- return line.saleQuantity * (line.unitPrice + (line.addOnCost ?? 0));
+ if (shouldConvertCurrency) {
+ if (line?.saleQuantity && line?.convertedUnitPrice) {
+ return (
+ line.saleQuantity * line.convertedUnitPrice +
+ (line.convertedAddOnCost ?? 0)
+ );
+ }
+ return 0;
+ } else {
+ if (line?.saleQuantity && line?.unitPrice) {
+ return line.saleQuantity * line.unitPrice + (line.addOnCost ?? 0);
+ }
}
-
return 0;
}
export function getTotal(
- lines: Database["public"]["Views"]["salesOrderLines"]["Row"][]
+ lines: Database["public"]["Views"]["salesOrderLines"]["Row"][],
+ shouldConvertCurrency: boolean
) {
let total = 0;
lines.forEach((line) => {
- if (line?.saleQuantity && line?.unitPrice) {
- total += line.saleQuantity * (line.unitPrice + (line.addOnCost ?? 0));
- }
+ total += getLineTotal(line, shouldConvertCurrency);
});
return total;