Skip to content

Commit

Permalink
Adding multicurrency support
Browse files Browse the repository at this point in the history
  • Loading branch information
radoslaw-sz committed Apr 14, 2024
1 parent 60eebe0 commit fafdd92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rsc-labs/medusa-documents",
"version": "0.3.1",
"version": "0.4.0",
"description": "Generate documents from Medusa",
"main": "dist/index.js",
"author": "RSC Labs (https://rsoftcon.com)",
Expand Down
16 changes: 9 additions & 7 deletions src/services/templates/invoices/basic/parts/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

import { LineItem, Order } from "@medusajs/medusa";
import { generateHr } from "./hr";
import { getDecimalDigits } from "../../../../utils/currency";

function formatCurrency(cents, currencyCode) {
return `${(cents / 100).toFixed(2)} ${currencyCode}`;
function amountToDisplay(amount: number, currencyCode: string) : string {
const decimalDigits = getDecimalDigits(currencyCode);
return `${(amount / Math.pow(10, decimalDigits)).toFixed(decimalDigits)} ${currencyCode.toUpperCase()}`;
}

function generateTableRow(
Expand Down Expand Up @@ -60,9 +62,9 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
position,
item.title,
item.description,
formatCurrency(item.total / item.quantity, order.currency_code),
amountToDisplay(item.total / item.quantity, order.currency_code),
item.quantity,
formatCurrency(item.total, order.currency_code)
amountToDisplay(item.total, order.currency_code)
);

generateHr(doc, position + 20);
Expand All @@ -76,7 +78,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
"",
"Shipping",
"",
formatCurrency(order.shipping_total, order.currency_code)
amountToDisplay(order.shipping_total, order.currency_code)
);

const taxPosition = subtotalPosition + 30;
Expand All @@ -87,7 +89,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
"",
"Tax",
"",
formatCurrency(order.tax_total, order.currency_code)
amountToDisplay(order.tax_total, order.currency_code)
);

const duePosition = taxPosition + 45;
Expand All @@ -99,7 +101,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
"",
"Total",
"",
formatCurrency(order.total, order.currency_code)
amountToDisplay(order.total, order.currency_code)
);
doc.font("Helvetica");
}
15 changes: 15 additions & 0 deletions src/services/utils/currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defaultCurrencies } from "@medusajs/utils";

const DEFAULT_DECIMAL_DIGITS = 2;

export function getDecimalDigits(currencyCode: string): number {
try {
const decimalDigits = defaultCurrencies[currencyCode.toUpperCase()] !== undefined ? defaultCurrencies[currencyCode.toUpperCase()].decimal_digits : undefined;
if (decimalDigits !== undefined) {
return decimalDigits;
}
} catch {
return DEFAULT_DECIMAL_DIGITS;
}
return DEFAULT_DECIMAL_DIGITS;
}

0 comments on commit fafdd92

Please sign in to comment.