This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCurrencyUtils.ts
86 lines (74 loc) · 2.68 KB
/
CurrencyUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { get } from "./functions/get";
import { Currency } from "./types/Currency";
import { CurrencyRates } from "./types/CurrencyRates";
export class CurrencyUtils {
public static stringifySum (
sum : number
) : string {
return (Math.round(sum*100)/100).toFixed(2);
}
public static getSum (
price : number,
amount : number,
discountPercent ?: number | undefined
): number {
return this.getSumWithDiscount(amount * price, discountPercent);
}
/**
* Returns the total sum including VAT
* @param price The price without VAT
* @param amount The amount of items
* @param vatPercent The VAT percentage
* @param discountPercent The discount percentage
*/
public static getSumWithVat (
price : number,
amount : number,
vatPercent : number,
discountPercent ?: number | undefined
): number {
const sum = this.getSumWithDiscount(amount * price, discountPercent);
return Math.round( (sum*100) + (sum*100) * vatPercent ) / 100;
}
public static getSumWithDiscount (
price : number,
discountPercent : number | undefined
): number {
return discountPercent !== undefined && discountPercent > 0 && discountPercent <= 1 ? price - price * discountPercent : price;
}
public static getVatlessSum (
sum: number,
vatPercent: number,
discountPercent ?: number | undefined
) : number {
const realSum = this.getSumWithDiscount(sum, discountPercent);
return (realSum * 100 / (1+vatPercent)) / 100;
}
public static roundByAccuracy (
value: number,
accuracy: number
) {
const m = Math.pow(10, accuracy);
return Math.round(value * m) / m;
}
public static convertCurrencyAmount (
rates : CurrencyRates,
amount : number,
from : Currency,
to : Currency,
accuracy : number = 10
) : number {
const toRate = get(rates, to);
if (toRate === undefined) throw new TypeError(`CurrencyService: To: No exchange rate found: ${to}`);
const fromRate = get(rates, from);
if (fromRate === undefined) throw new TypeError(`CurrencyService: From: No exchange rate found: ${from}`);
return CurrencyUtils.roundByAccuracy( (amount / fromRate) * toRate, accuracy);
}
public static getCents (value: number) : number {
return Math.round(value*100);
}
public static fromCents (value: number) : number {
return Math.round(value) / 100;
}
}