12
12
13
13
import { LineItem , Order } from "@medusajs/medusa" ;
14
14
import { generateHr } from "./hr" ;
15
+ import { getDecimalDigits } from "../../../../utils/currency" ;
15
16
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 ( ) } ` ;
18
20
}
19
21
20
22
function generateTableRow (
@@ -60,9 +62,9 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
60
62
position ,
61
63
item . title ,
62
64
item . description ,
63
- formatCurrency ( item . total / item . quantity , order . currency_code ) ,
65
+ amountToDisplay ( item . total / item . quantity , order . currency_code ) ,
64
66
item . quantity ,
65
- formatCurrency ( item . total , order . currency_code )
67
+ amountToDisplay ( item . total , order . currency_code )
66
68
) ;
67
69
68
70
generateHr ( doc , position + 20 ) ;
@@ -76,7 +78,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
76
78
"" ,
77
79
"Shipping" ,
78
80
"" ,
79
- formatCurrency ( order . shipping_total , order . currency_code )
81
+ amountToDisplay ( order . shipping_total , order . currency_code )
80
82
) ;
81
83
82
84
const taxPosition = subtotalPosition + 30 ;
@@ -87,7 +89,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
87
89
"" ,
88
90
"Tax" ,
89
91
"" ,
90
- formatCurrency ( order . tax_total , order . currency_code )
92
+ amountToDisplay ( order . tax_total , order . currency_code )
91
93
) ;
92
94
93
95
const duePosition = taxPosition + 45 ;
@@ -99,7 +101,7 @@ export function generateInvoiceTable(doc, y, order: Order, items: LineItem[]) {
99
101
"" ,
100
102
"Total" ,
101
103
"" ,
102
- formatCurrency ( order . total , order . currency_code )
104
+ amountToDisplay ( order . total , order . currency_code )
103
105
) ;
104
106
doc . font ( "Helvetica" ) ;
105
107
}
0 commit comments