-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFTBXExchangeRateProviderCBODK.xml
More file actions
430 lines (364 loc) · 16.1 KB
/
FTBXExchangeRateProviderCBODK.xml
File metadata and controls
430 lines (364 loc) · 16.1 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?xml version="1.0" encoding="utf-8"?>
<AxClass xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Name>FTBXExchangeRateProviderCBODK</Name>
<SourceCode>
<Declaration><![CDATA[
using Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.Framework;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.HttpStatusCode;
using SysCollections = System.Collections.Generic;
using System.ComponentModel.Composition;
[
ExportMetadataAttribute(
enumStr(ExchangeRateProvider),
ExchangeRateProvider::FTBXCentralBankOfDenmark),
ExportAttribute('Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.Framework.IExchangeRateProvider')
]
internal final class FTBXExchangeRateProviderCBODK implements IExchangeRateProvider
{
private const ExchangeRateProviderId providerId = 'fdf2f821-a580-4fe2-8810-8adc8bbd6149';
private const str currencyDNK = 'DKK';
private const str statBankTable = 'DNVALD';
private const str statBankFormat = 'DSTML';
private const str statBankOrder = 'Ascending';
private const str statBankLanguage = 'en';
private const str statBankVariableCurrency = 'VALUTA';
private const str statBankVariableTime = 'TID';
private const str statBankVariableType = 'KURTYP';
private const str statBankDateTimeFormat = '%1M%2*';
private const str statBankDSTMLCurrency = 'currency';
private const str statBankDSTMLTime = 'time';
private const str configNameCentralBankDenmarkEndpoint = 'Central Bank of Denmark API endpoint';
private const str configNameCentralBankDenmarkHistEndpoint = 'Central Bank of Denmark API endpoint (5 days)';
private const str configNameStatistikbankEndpoint = 'Statistikbank API endpoint';
private const str configNameStatistikbankExchangeRateType = 'Statistikbank exchange rate type';
private const str configNameSource = 'Source';
private const str gesmesPathCube = '/gesmes:Envelope/Cube';
private const str gesmesElementCube = 'Cube';
private const str gesmesCurrency = 'currency';
private const str gesmesRate = 'rate';
private const str gesmesDate = 'time';
IExchangeRateProviderFrameworkFactory factory;
}
]]></Declaration>
<Methods>
<Method>
<Name>GetConfigurationDefaults</Name>
<Source><![CDATA[
public IExchangeRateProviderConfigDefaults GetConfigurationDefaults()
{
IExchangeRateProviderConfigDefaults configurationDefaults = factory.CreateExchangeRateProviderConfigDefaults();
//Only use the endpoint that returns data in GESMES/TS-XML format
/*
configurationDefaults.addNameValueConfigurationPair(
configNameCentralBankDenmarkEndpoint,
@'https://www.nationalbanken.dk/api/currencyratesxml');
*/
configurationDefaults.addNameValueConfigurationPair(
configNameCentralBankDenmarkHistEndpoint,
@'https://www.nationalbanken.dk/api/currencyratesxmlhistory');
configurationDefaults.addNameValueConfigurationPair(
configNameStatistikbankEndpoint,
@'https://api.statbank.dk/v1/data');
configurationDefaults.addNameValueConfigurationPair(
configNameStatistikbankExchangeRateType,
'KBH');
configurationDefaults.AddNameValueConfigurationPair(
configNameSource,
"1");
return configurationDefaults;
}
]]></Source>
</Method>
<Method>
<Name>get_Name</Name>
<Source><![CDATA[
public ExchangeRateProviderName get_Name()
{
return "@FinanceToolbox:ExchangeRateProviderCentralBankOfDenmark";
}
]]></Source>
</Method>
<Method>
<Name>get_Id</Name>
<Source><![CDATA[
public ExchangeRateProviderId get_Id()
{
return ProviderId;
}
]]></Source>
</Method>
<Method>
<Name>set_Factory</Name>
<Source><![CDATA[
public void set_Factory(IExchangeRateProviderFrameworkFactory _factory)
{
factory = _factory;
}
]]></Source>
</Method>
<Method>
<Name>GetSupportedOptions</Name>
<Source><![CDATA[
public IExchangeRateProviderSupportedOptions GetSupportedOptions()
{
ExchangeRateProviderSupportedOptions options = factory.CreateExchangeRateProviderSupportedOptions();
options.set_doesSupportSpecificCurrencyPairs(false);
options.set_doesSupportSpecificDates(true);
options.set_fixedBaseIsoCurrency(currencyDnk);
options.set_doesSupportPreventImportOnNationalHoliday(false);
options.set_doesSupportExchangeRateFromPreviousDay(false);
return options;
}
]]></Source>
</Method>
<Method>
<Name>ValidateConfigurationDetail</Name>
<Source><![CDATA[
/// <summary>
/// Validation of configuration values
/// No validation has been implemented yet, as only endpoint can be configured. It will fail at the import
/// </summary>
/// <param name = "_key">Configuration key</param>
/// <param name = "_value">Value</param>
/// <returns>True if the configured value can be validated</returns>
public boolean ValidateConfigurationDetail(ExchangeRateProviderPropertyKey _key, ExchangeRateProviderPropertyValue _value)
{
return true;
}
]]></Source>
</Method>
<Method>
<Name>EnumNameForLookup</Name>
<Source><![CDATA[
public str EnumNameForLookup(ExchangeRateProviderPropertyKey _key)
{
switch (_key)
{
case configNameSource:
return enumStr(FTBXExchangeRateProviderCBODKSource);
}
return '';
}
]]></Source>
</Method>
<Method>
<Name>addExchangeRatesCentralBankGESMES</Name>
<Source><![CDATA[
private void addExchangeRatesCentralBankGESMES(
ExchangeRateResponse _response,
str _apiEndpoint)
{
var httpClient = new HttpClient();
var httpRequestMessage = new HttpRequestMessage(new HttpMethod('get'), _apiEndpoint);
var responseMessage = httpClient.SendAsync(httpRequestMessage)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (responseMessage && responseMessage.Content)
{
var nodeListCube = XmlDocument::newXml(
responseMessage.Content.ReadAsStringAsync()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult()
).getElementsByTagName('Cube');
if (nodeListCube.length() > 0)
{
for(var nodeCube = nodeListCube.item(1); nodeCube; nodeCube = nodeCube.nextSibling())
{
var exchDate = nodeCube.attributes().getNamedItem(gesmesDate).Value();
var parsedDate = str2Date(exchDate, 321);
for (var nodeRate = nodeCube.firstChild(); nodeRate; nodeRate = nodeRate.nextSibling())
{
var exchCurrency = nodeRate.attributes().getNamedItem(gesmesCurrency).Value();
var exchangeRateStr = nodeRate.attributes().getNamedItem(gesmesRate).Value();
if (str2numOK(exchangeRateStr))
{
real parsedExchangeRate = str2Num(ExchangeRateStr);
if (parsedExchangeRate > 0)
{
var currencyPair = ExchangeRateResponseCurrencyPair::construct();
currencypair.set_fromCurrency(exchCurrency);
currencypair.set_toCurrency(currencyDNK);
currencypair.set_exchangeRateDisplayFactor(ExchangeRateDisplayFactor::Hundred);
var exchangeRate = ExchangeRateResponseExchangeRate::construct();
exchangeRate.set_validFrom(parsedDate);
exchangeRate.set_exchangeRate(parsedExchangeRate);
currencyPair.addExchangeRate(exchangeRate);
_response.addOrUpdateCurrencyPair(currencyPair);
}
}
}
}
}
}
}
]]></Source>
</Method>
<Method>
<Name>addExchangeRatesStatbank</Name>
<Source><![CDATA[
private void addExchangeRatesStatbank(
ExchangeRateResponse _response,
str _apiEndpoint,
str _exchangeRateType,
FromDate _fromDate,
ToDate _toDate)
{
var httpClient = new HttpClient();
var httpRequestMessage = new HttpRequestMessage(new HttpMethod('post'), _apiEndpoint);
var statbankRequest = new FTBXExchangeRateProviderCBODKStatbankRequestContract();
statbankRequest.parmLang(statBankLanguage);
statbankRequest.parmTable(statBankTable);
statbankRequest.parmFormat(statBankFormat);
statbankRequest.parmTimeOrder(statBankOrder);
var currencyVariable = new FTBXExchangeRateProviderCBODKStatbankVariableContract();
currencyVariable.parmCode(statBankVariableCurrency);
currencyVariable.parmValues().addEnd('*');
statbankRequest.parmVariables().addEnd(currencyVariable);
var exchangeRateTypeVariable = new FTBXExchangeRateProviderCBODKStatbankVariableContract();
exchangeRateTypeVariable.parmCode(statBankVariableType);
exchangeRateTypeVariable.parmValues().addEnd(_exchangeRateType);
statbankRequest.parmVariables().addEnd(exchangeRateTypeVariable);
var exchangeRateDatesVariable = new FTBXExchangeRateProviderCBODKStatbankVariableContract();
exchangeRateDatesVariable.parmCode(statBankVariableTime);
var fromDate = dateStartMth(_fromDate);
var toDate = dateEndMth(_toDate);
var curDate = fromDate;
do
{
//Filter format example: 2024M06*
//This filter will return all values for that month
exchangeRateDatesVariable.parmValues().addEnd(
strFmt(
statBankDateTimeFormat,
year(curDate),
strRFix(int2Str(mthOfYr(curDate)), 2, '0')));
curDate = nextMth(curDate);
}
while (curDate <= toDate);
statbankRequest.parmVariables().addEnd(exchangeRateDatesVariable);
httpRequestMessage.Content = new StringContent(
FormJsonSerializer::serializeClass(statbankRequest),
System.Text.Encoding::UTF8,
'application/json');
var responseMessage = httpClient.SendAsync(httpRequestMessage)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
if (responseMessage && responseMessage.Content)
{
var dstmlDocument = XmlDocument::newXml(
responseMessage.Content.ReadAsStringAsync()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult());
str dateVariableCode;
str currencyVariableCode;
for (var dataNode = dstmlDocument.getElementsByTagName('Variable').item(0); dataNode; dataNode = dataNode.nextSibling())
{
var attributes = dataNode.attributes();
switch(attributes.getNamedItem('Text').Value())
{
case statBankDSTMLCurrency :
currencyVariableCode = attributes.getNamedItem('Code').Value();
break;
case statBankDSTMLTime :
dateVariableCode = attributes.getNamedItem('Code').Value();
break;
}
}
if (!currencyVariableCode || !dateVariableCode)
{
throw Error("@FinanceToolbox:ExchangeRateProviderDSTMLFormatMissingVariable");
}
for (var rateNode = dstmlDocument.getElementsByTagName('No').item(0); rateNode; rateNode = rateNode.nextSibling())
{
var attributes = rateNode.attributes();
str dateVariableStr = attributes.getNamedItem(dateVariableCode).Value();
str currencyVariableStr = attributes.getNamedItem(currencyVariableCode).Value();
str exchangeRateStr = rateNode.text();
//Format: 2024M05D01
var parsedDate = mkDate(
str2int(substr(dateVariableStr, 9, 2)), //Day
str2int(substr(dateVariableStr, 6, 2)), //Month
str2int(substr(dateVariableStr, 1, 4))); //Year
if (parsedDate > _toDate)
{
break;
}
if (currencyVariableStr == currencyDNK)
{
continue;
}
if (str2numOK(exchangeRateStr))
{
var parsedExchangeRate = str2Num(ExchangeRateStr);
if (parsedExchangeRate > 0)
{
var currencyPair = ExchangeRateResponseCurrencyPair::construct();
currencypair.set_fromCurrency(currencyVariableStr);
currencypair.set_toCurrency(currencyDNK);
currencypair.set_exchangeRateDisplayFactor(ExchangeRateDisplayFactor::Hundred);
var exchangeRate = ExchangeRateResponseExchangeRate::construct();
exchangeRate.set_validFrom(parsedDate);
exchangeRate.set_exchangeRate(parsedExchangeRate);
currencyPair.addExchangeRate(exchangeRate);
_response.addOrUpdateCurrencyPair(currencyPair);
}
}
}
}
}
]]></Source>
</Method>
<Method>
<Name>GetExchangeRates</Name>
<Source><![CDATA[
public IExchangeRateResponse GetExchangeRates(IExchangeRateRequest _request, IExchangeRateProviderConfig _config)
{
var exchangeRateResponse = ExchangeRateResponse::construct();
FTBXExchangeRateProviderCBODKSource source = str2Int(_config.getPropertyValue(ProviderId, configNameSource));
try
{
if (source != FTBXExchangeRateProviderCBODKSource::CentralBank
&& !(source == FTBXExchangeRateProviderCBODKSource::CentralBankStatbank && _request.ImportDateType == Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.ExchangeRateImportDateType::CurrentDate))
{
this.addExchangeRatesStatbank(
exchangeRateResponse,
_config.getPropertyValue(ProviderId, configNameStatistikbankEndpoint),
_config.getPropertyValue(ProviderId, configNameStatistikbankExchangeRateType),
_request.FromDate,
_request.ToDate);
}
}
catch
{
exceptionTextFallThrough();
}
try
{
//Rates returned by the central bank always takes precedence over rates from Statbank, even though it's the same data owner
if (source != FTBXExchangeRateProviderCBODKSource::Statbank)
{
//Only the GESMES/TS-XML endpoint has been implemented. It returns the same data as the daily export, and since it's only 5 days it could
//be considered overkill to implement both formats
this.addExchangeRatesCentralBankGESMES(
exchangeRateResponse,
_config.getPropertyValue(ProviderId, configNameCentralBankDenmarkHistEndpoint));
}
}
catch
{
exceptionTextFallThrough();
}
return exchangeRateResponse;
}
]]></Source>
</Method>
</Methods>
</SourceCode>
</AxClass>