-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCurrencyUtils.js
More file actions
259 lines (217 loc) · 9.03 KB
/
CurrencyUtils.js
File metadata and controls
259 lines (217 loc) · 9.03 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var CurrencyUtils = Class.create();
CurrencyUtils.METHOD_CONVERT = 'CurrencyUtils.convert';
CurrencyUtils.METHOD_PARAMETER_SOURCECURRENCY = 'strSourceCurrency';
CurrencyUtils.METHOD_PARAMETER_TARGETCURRENCY = 'strTargetCurrency';
CurrencyUtils.METHOD_PARAMETER_ORIGINALAMOUNT = 'strOriginalAmount';
CurrencyUtils.METHOD_PARAMETER_LOCALE = 'strLocale';
CurrencyUtils.AJAX_PARAMETER_SOURCECURRENCY = 'sysparm_source_currency';
CurrencyUtils.AJAX_PARAMETER_TARGETCURRENCY = 'sysparm_target_currency';
CurrencyUtils.AJAX_PARAMETER_ORIGINALAMOUNT = 'sysparm_original_amount';
CurrencyUtils.AJAX_PARAMETER_LOCALE = 'sysparm_locale';
/**
* Helper class for dealing with currencies.
* All methods can be called both server-side and client-side.
*
* @class CurrencyUtils
* @author Maik Skoddow
* @see https://www.servicenow.com/community/developer-articles/universal-pattern-for-script-includes/ta-p/2323602
*/
CurrencyUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
/*
* Converts a number for a given currency to the value of a target currency with the help of a ServiceNow API.
* The conversion rates are determined automatically on a daily basis by ServiceNow.
*
* @param {String} strSourceCurrency Original currency of the given amount to be converted as
* a 3-letter code defined by the <a href="https://en.wikipedia.org/wiki/ISO_4217">ISO 4217</a>.
* @param {String} strTargetCurrency Target currency the given amount should be converted to as
* a 3-letter code defined by the <a href="https://en.wikipedia.org/wiki/ISO_4217">ISO 4217</a>.
* @return {JSON} Value object containing all information. In casde of errors the property `error_message`
* contains a respective message.
*/
convert: function(strSourceCurrency, strTargetCurrency, strOriginalAmount, strLocale) {
var IS_CLIENT_CALL = JSUtil.notNil(this.getName());
var SESSION_LOCALE = String(GlideLocale.get().getCurrent());
var PARAMETER_SOURCECURRENCY =
IS_CLIENT_CALL ?
CurrencyUtils.AJAX_PARAMETER_SOURCECURRENCY :
CurrencyUtils.METHOD_PARAMETER_SOURCECURRENCY;
var PARAMETER_TARGETCURRENCY =
IS_CLIENT_CALL ?
CurrencyUtils.AJAX_PARAMETER_TARGETCURRENCY :
CurrencyUtils.METHOD_PARAMETER_TARGETCURRENCY;
var PARAMETER_ORGINALAMOUNT =
IS_CLIENT_CALL ?
CurrencyUtils.AJAX_PARAMETER_ORIGINALAMOUNT :
CurrencyUtils.METHOD_PARAMETER_ORIGINALAMOUNT;
var PARAMETER_LOCALE = IS_CLIENT_CALL ?
CurrencyUtils.AJAX_PARAMETER_LOCALE :
CurrencyUtils.METHOD_PARAMETER_LOCALE;
var _getReturnObject = function() {
var _objReturn = {
error_message: String(arguments[0] || ''),
source_currency: String(arguments[1] || ''),
target_currency: String(arguments[2] || ''),
original_amount: String(arguments[3] || ''),
converted_amount: String(arguments[4] || ''),
exchange_rate: String(arguments[5] || ''),
used_locale: String(arguments[6] || '')
};
var _strReturn = JSON.stringify(_objReturn);
LogHelper.debug(CurrencyUtils.METHOD_CONVERT, 'Return Values: {0}', _strReturn);
return IS_CLIENT_CALL ? _strReturn : _objReturn;
};
try {
var _strSourceCurrency =
String(strSourceCurrency || this.getParameter(CurrencyUtils.AJAX_PARAMETER_SOURCECURRENCY)).trim();
var _strTargetCurrency =
String(strTargetCurrency || this.getParameter(CurrencyUtils.AJAX_PARAMETER_TARGETCURRENCY)).trim();
var _strOriginalAmount =
String(strOriginalAmount || this.getParameter(CurrencyUtils.AJAX_PARAMETER_ORIGINALAMOUNT)).trim();
var _strLocale =
String(
strLocale || this.getParameter(CurrencyUtils.AJAX_PARAMETER_LOCALE) || SESSION_LOCALE
).trim();
LogHelper.debug(
CurrencyUtils.METHOD_CONVERT,
'Entering method with:\n<{0}> = >{1}<\n<{2}> = >{3}<\n<{4}> = >{5}<\n<{6}> = >{7}<',
PARAMETER_SOURCECURRENCY, _strSourceCurrency,
PARAMETER_TARGETCURRENCY, _strTargetCurrency,
PARAMETER_ORGINALAMOUNT, _strOriginalAmount,
PARAMETER_LOCALE, _strLocale
);
// source currencey code is not valid
if (!this._isValidCurrencyCode(_strSourceCurrency)) {
return _getReturnObject(
LogHelper.error(
CurrencyUtils.METHOD_CONVERT,
'>{0}< is not a valid currency code at <{1}>!',
_strSourceCurrency, PARAMETER_SOURCECURRENCY
)
);
}
// target currencey code is not valid
if (!this._isValidCurrencyCode(_strTargetCurrency)) {
return _getReturnObject(
LogHelper.error(
CurrencyUtils.METHOD_CONVERT,
'>{0}< is not a valid currency code at <{1}>!',
_strTargetCurrency, PARAMETER_TARGETCURRENCY
)
);
}
// locale has not the required format
if (!new RegExp('^[a-z]{2}_[A-Z]{2}$').test(_strLocale)) {
return _getReturnObject(
LogHelper.error(
CurrencyUtils.METHOD_CONVERT,
'Value >{0}< at <{1}> does not represent a valid format for a locale identifier!',
_strLocale, PARAMETER_LOCALE)
);
}
// normalize amount to english format
var _strNormalizedOriginalAmount = this._getNormalizedNumber(_strOriginalAmount, _strLocale);
// amount is not a valid number for the given locale
if (_strNormalizedOriginalAmount === null) {
return _getReturnObject(
LogHelper.error(
CurrencyUtils.METHOD_CONVERT,
'Value >{0}< at <{1}> does not represent a valid number for the locale >{2}<!',
_strOriginalAmount, PARAMETER_ORGINALAMOUNT, _strLocale)
);
}
// preconfigure the currency converter
var gcvConverter = new sn_currency.GlideCurrencyConverter(_strSourceCurrency, _strTargetCurrency);
// set the value to be converted
gcvConverter.setAmount(_strNormalizedOriginalAmount);
// perform currency conversion
var _gcevConversionResult = gcvConverter.convert();
// something went wrong
if (_gcevConversionResult === null) {
return _getReturnObject(
LogHelper.warn(
CurrencyUtils.METHOD_CONVERT,
'Value >{0}< for orignal currency >{1}< could not be converted to target currency >{2}<',
_strOriginalAmount, _strSourceCurrency, _strTargetCurrency
)
);
}
return _getReturnObject(
'',
_strSourceCurrency,
_strTargetCurrency,
_strOriginalAmount,
this._getDenormalizedNumber(_gcevConversionResult.getAmount(), _strLocale),
this._getDenormalizedNumber(_gcevConversionResult.getRate(), _strLocale, 6),
_strLocale
);
}
catch (e) {
return _getReturnObject(
LogHelper.fatal(CurrencyUtils.METHOD_CONVERT, 'Unexpected error!', e)
);
}
},
/**
* Checks whether the given parameter value is a valid 3-letter currency code defined by ISO 4217.
*
* @param {String} strCurrencyCode Currency code as a 3-letter value defined by ISO 4217.
* @return {boolean} `true` if passed cvurrency code is valid otherwise `false`.
*/
_isValidCurrencyCode: function(strCurrencyCode) {
try {
var _gcp = new sn_currency.GlideCurrencyParser();
_gcp.setDefaultCurrencyCode(strCurrencyCode);
_gcp.parse("1");
return true;
}
catch (e) {
return false;
}
},
/**
* Based on the locale the passed number at `strNumber` is converted to the English notation for numbers
* with a dot as decimal separator and commas as grouping separator.
*
* @param {String} strNumber Number to be converted based on the given locale.
* @param {String} strLocale Considered locale when interpreting the number.
* @return {String} Converted number in case a conversion was necessary. If not, the same number as
* passed will be returned.
*/
_getNormalizedNumber: function(strNumber, strLocale) {
var _gcpParser = new sn_currency.GlideCurrencyParser();
var _arrLocale = strLocale.split('_');
try {
_gcpParser.setLocale(_arrLocale[0], _arrLocale[1]);
_gcpParser.setDefaultCurrencyCode("USD");
var _gcvParsedValue = _gcpParser.parse(strNumber);
return _gcvParsedValue.getAmount();
}
catch (e) {
return null;
}
},
/**
* Based on the locale the passed number at `strNumber` is converted to the English notation for numbers
* with a dot as decimal separator and commas as grouping separator.
*
* @param {String} strNumber Number to be converted based on the locale.
* @param {String} strLocale Considered locale when converting the number.
* @param {Number} [numFractionDigits] Optional number of digits after the decimal sign.
* @return {String} Converted number in case a conversion was necessary. If not, the same number as
* passed will be returned.
*/
_getDenormalizedNumber: function(strNumber, strLocale, numFractionDigits) {
try {
var _gcfCurrencyFormatter = new sn_currency.GlideCurrencyFormatter("%v");
var _arrLocale = strLocale.split('_');
var _parsedInt = parseInt(numFractionDigits, 10);
var _numFractionDigits = isNaN(_parsedInt) ? 2 : Math.abs(_parsedInt);
_gcfCurrencyFormatter.setLocale(_arrLocale[0], _arrLocale[1]);
return _gcfCurrencyFormatter.setMaxFractionDigits(_numFractionDigits).format(strNumber, 'USD');
}
catch (e) {
return null;
}
},
type: 'CurrencyUtils'
});