Skip to content

Commit fafdd92

Browse files
committed
Adding multicurrency support
1 parent 60eebe0 commit fafdd92

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rsc-labs/medusa-documents",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"description": "Generate documents from Medusa",
55
"main": "dist/index.js",
66
"author": "RSC Labs (https://rsoftcon.com)",

src/services/templates/invoices/basic/parts/table.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
import { LineItem, Order } from "@medusajs/medusa";
1414
import { generateHr } from "./hr";
15+
import { getDecimalDigits } from "../../../../utils/currency";
1516

16-
function formatCurrency(cents, currencyCode) {
17-
return `${(cents / 100).toFixed(2)} ${currencyCode}`;
17+
function amountToDisplay(amount: number, currencyCode: string) : string {
18+
const decimalDigits = getDecimalDigits(currencyCode);
19+
return `${(amount / Math.pow(10, decimalDigits)).toFixed(decimalDigits)} ${currencyCode.toUpperCase()}`;
1820
}
1921

2022
function generateTableRow(
@@ -60,9 +62,9 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
6062
position,
6163
item.title,
6264
item.description,
63-
formatCurrency(item.total / item.quantity, order.currency_code),
65+
amountToDisplay(item.total / item.quantity, order.currency_code),
6466
item.quantity,
65-
formatCurrency(item.total, order.currency_code)
67+
amountToDisplay(item.total, order.currency_code)
6668
);
6769

6870
generateHr(doc, position + 20);
@@ -76,7 +78,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
7678
"",
7779
"Shipping",
7880
"",
79-
formatCurrency(order.shipping_total, order.currency_code)
81+
amountToDisplay(order.shipping_total, order.currency_code)
8082
);
8183

8284
const taxPosition = subtotalPosition + 30;
@@ -87,7 +89,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
8789
"",
8890
"Tax",
8991
"",
90-
formatCurrency(order.tax_total, order.currency_code)
92+
amountToDisplay(order.tax_total, order.currency_code)
9193
);
9294

9395
const duePosition = taxPosition + 45;
@@ -99,7 +101,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
99101
"",
100102
"Total",
101103
"",
102-
formatCurrency(order.total, order.currency_code)
104+
amountToDisplay(order.total, order.currency_code)
103105
);
104106
doc.font("Helvetica");
105107
}

src/services/utils/currency.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defaultCurrencies } from "@medusajs/utils";
2+
3+
const DEFAULT_DECIMAL_DIGITS = 2;
4+
5+
export function getDecimalDigits(currencyCode: string): number {
6+
try {
7+
const decimalDigits = defaultCurrencies[currencyCode.toUpperCase()] !== undefined ? defaultCurrencies[currencyCode.toUpperCase()].decimal_digits : undefined;
8+
if (decimalDigits !== undefined) {
9+
return decimalDigits;
10+
}
11+
} catch {
12+
return DEFAULT_DECIMAL_DIGITS;
13+
}
14+
return DEFAULT_DECIMAL_DIGITS;
15+
}

0 commit comments

Comments
 (0)