Monday, November 17, 2025

Bank of Canada exchange rate provider config

 using Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.Framework; 

using Microsoft.Dynamics.Currency.Instrumentation; 

using System.Collections; 

using System.ComponentModel.Composition; 

  

/// <summary> 

/// The <c>ExchangeRateProviderBOC_CA</c> class represents exchange rate provider for Bank of Canada. 

/// </summary> 

[ExportMetadataAttribute(enumStr(ExchangeRateProvider), ExchangeRateProvider::ValetAPI), ExportAttribute('Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.Framework.IExchangeRateProvider')] 

class TRI_ExchangerateProviderValetAPI implements IExchangeRateProvider 

{ 

    #File 

    #define.ProviderId('CB024E9B-312B-44CE-BE89-3ED8597B007F') 

    #define.CAD('CAD') 

  

    // xml node names 

    public static str dateNoteStr = "d"; 

    public static str baseCurrencyNodeStr = "cb:baseCurrency"; 

    public static str targetCurrencyNodeStr = "cb:targetCurrency"; 

    public static str valueNodeStr = "v"; 

    public static str Observations = "o"; 

  

  

  

    //configuration values 

    #define.ServiceCurrentUrl('ServiceCurrentUrl') 

    #define.urlCurrentFmt("https://www.bankofcanada.ca/valet/observations/FXUSDCAD/XML?start_date=%1&end_date=%2") 

  

    IExchangeRateProviderFrameworkFactory factory; 

  

    public IExchangeRateProviderConfigDefaults getConfigurationDefaults() 

    { 

        ExchangeRateProviderConfigDefaults defaults = ExchangeRateProviderConfigDefaults::construct(); 

     

        defaults.addNameValueConfigurationPair(#ServiceCurrentUrl, #urlCurrentFmt); 

     

        return defaults; 

    } 

  

    public IExchangeRateResponse getExchangeRates(IExchangeRateRequest _request, IExchangeRateProviderConfig _config) 

    { 

        ExchangeRateResponse    exchangeRateResponse    = ExchangeRateResponse::construct(); 

        TransDate               exchangeDate            = _request.get_fromDate(); 

        str                     serviceUrl; 

     

        serviceUrl = ExchangeRateProviderConfig::construct().getPropertyValue(#ProviderId, #ServiceCurrentUrl); 

     

        if (_request.get_importDateType() == Microsoft.Dynamics.ApplicationSuite.FinancialManagement.Currency.ExchangeRateImportDateType::CurrentDate) 

        { 

           // this.getExchangeRatesFromService(serviceUrlexchangeRateResponse, _request.get_exchangeRateTypeId()); 

        } 

        else 

        { 

            serviceUrl = strFmt(serviceUrl,date2str(exchangeDate3212DateSeparator::Hyphen, 2DateSeparator::Hyphen, 4DateFlags::None),date2str(_request.get_toDate(), 3212DateSeparator::Hyphen, 2DateSeparator::Hyphen, 4DateFlags::None)); 

            this.getExchangeRatesFromService(serviceUrlexchangeRateResponse, _request.get_exchangeRateTypeId()); 

        } 

     

        return exchangeRateResponse; 

    } 

  

    private void getExchangeRatesFromService( 

        str                     _serviceUrl, 

        ExchangeRateResponse    _exchangeRateResponse, 

        ExchangeRateTypeRecId   _exchangeRateTypeRecId) 

    { 

        XmlDocument                         xmlRates; 

        XmlNode                             xmlNode; 

        XMLNode                             rateDateNodebaseCurNodetargetCurNodevalueNodeObservationNode; 

        XMLNodeList                         DataList,rateDateListtargetCurListbaseCurListvalueListObservationList; 

        XmlElement                          NodedataNodeObservation,NodeNote; 

        TransDate                           rateDate; 

        ExchangeRateResponseCurrencyPair    currencyPair; 

        ExchRate                            exchRateValue; 

        ExchangeRateResponseExchangeRate    exchangeRate; 

        CurrencyCode                        fromCurrencyCodetoCurrencyCode; 

        int                                 i, j; 

     

        xmlRates = new XmlDocument(); 

     

        new FileIOPermission(_serviceUrl, #io_read).assert(); 

        if (!xmlRates.load(_serviceUrl)) 

        { 

            return; 

        } 

     

        xmlNode = xmlRates.documentElement(); 

  

        rateDateList    = xmlRates.getElementsByTagName(dateNoteStr); 

        Nodedata    = xmlRates.getNamedElement("data"); 

        DataList = Nodedata.childNodes(); 

  

        for(i=0i < DataList.length(); i++) 

        { 

            NodeObservation =DataList.item(i); 

  

            ObservationList = NodeObservation.childNodes(); 

            if(NodeObservation.name() == 'observations') 

            { 

                ObservationList = NodeObservation.childNodes(); 

  

                for (j=0; j < ObservationList.length(); j++) 

                { 

                    NodeNote = ObservationList.item(j); 

                    info(strFmt("Date: %1"NodeNote.getAttribute("d"))); 

                    info(strFmt("Value: %1"NodeNote.getNamedElement("v").Text())); 

                    rateDate         = str2date(substr(NodeNote.getAttribute("d"),1,10),321); 

                        fromCurrencyCode = 'CAD' ;//baseCurNode.text(); 

                        toCurrencyCode   = 'USD';//targetCurNode.text(); 

                        exchRateValue    = str2num(NodeNote.getNamedElement("v").Text()); 

                        if (exchRateValue) 

                        { 

                                exchRateValue =  (1 / exchRateValue) * 100; 

  

                                currencyPair = ExchangeRateResponseCurrencyPair::construct(); 

  

                                currencyPair.set_fromCurrency(fromCurrencyCode); 

                                currencyPair.set_toCurrency(toCurrencyCode); 

                                currencyPair.set_exchangeRateDisplayFactor(ExchangeRateDisplayFactor::Hundred); 

  

                                exchangeRate = ExchangeRateResponseExchangeRate::construct(); 

                                exchangeRate.set_validFrom(rateDate); 

                                exchangeRate.set_exchangeRate(exchRateValue); 

                                currencyPair.addExchangeRate(exchangeRate); 

  

                                _exchangeRateResponse.addOrUpdateCurrencyPair(currencyPair); 

                            } 

  

                } 

            } 

  

  

        } 

  

       // baseCurList     = xmlRates.getElementsByTagName(baseCurrencyNodeStr); 

       // targetCurList   = xmlRates.getElementsByTagName(targetCurrencyNodeStr); 

        //valueList       = xmlRates.getElementsByTagName(valueNodeStr); 

        //ObservationList     = xmlRates.getElementsByTagName('observations'); 

        //for (i = 0; i < ObservationList.length(); i++) 

        //{ 

        ////    rateDateNode    = rateDateList.item(i); 

        ////   // baseCurNode     = baseCurList.item(i); 

        ////   // targetCurNode   = targetCurList.item(i); 

        ////    valueNode       = valueList.item(i); 

        //NodeObservation     = ObservationList.item(i); 

        //    info(strFmt("Date: %1", NodeObservation.getAttribute("d"))); 

        //    info(strFmt("Value: %1", NodeObservation.getNamedElement("v").text())); 

        //} 

  

        ////    rateDate         = str2date(substr(rateDateNode.text(),1,10),321); 

        ////    fromCurrencyCode = 'CAD' ;baseCurNode.text(); 

        ////    toCurrencyCode   = 'USD';//targetCurNode.text(); 

        ////    exchRateValue    = str2num(valueNode.text()); 

  

        //    //Info(DecimalNode.text()); 

  

        //    if (exchRateValue) 

        //    { 

        //        exchRateValue =  (1 / exchRateValue) * 100; 

  

        //        currencyPair = ExchangeRateResponseCurrencyPair::construct(); 

  

        //        currencyPair.set_fromCurrency(fromCurrencyCode); 

        //        currencyPair.set_toCurrency(toCurrencyCode); 

        //        currencyPair.set_exchangeRateDisplayFactor(ExchangeRateDisplayFactor::Hundred); 

  

        //        exchangeRate = ExchangeRateResponseExchangeRate::construct(); 

        //        exchangeRate.set_validFrom(rateDate); 

        //        exchangeRate.set_exchangeRate(exchRateValue); 

        //        currencyPair.addExchangeRate(exchangeRate); 

  

        //        _exchangeRateResponse.addOrUpdateCurrencyPair(currencyPair); 

        //    //} 

        //} 

    } 

  

    public ExchangeRateProviderName get_Name() 

    { 

        return "Valet API"; 

    } 

  

    public ExchangeRateProviderId get_Id() 

    { 

        return #ProviderId; 

    } 

  

    public IExchangeRateProviderSupportedOptions getSupportedOptions() 

    { 

        ExchangeRateProviderSupportedOptions options = ExchangeRateProviderSupportedOptions::construct(); 

     

        //Set the parmDoesSupportSpecificCurrencyPairs option to true only if the exchange rate service requires that a source and destination currency be passed to get an exchange rate. Many exchange rate services return rates with respect to a fixed currency or a given set of currency pairs. For these services, the value of this option should be set to false. 

        options.set_DoesSupportSpecificCurrencyPairs(false); 

        //Set the parmDoesSupportSpecificDates option to true if the exchange rate service enables rates to be requested for specific dates. Services that provide the exchange rate only for the current date will set this value to false. 

        options.set_DoesSupportSpecificDates(false); 

        options.set_fixedBaseIsoCurrency(#CAD); 

     

        return options; 

    } 

  

    /// <summary> 

    /// Returns the Bank of Canada exchange rate provider ID of the provider. 

    /// </summary> 

    /// <returns> 

    /// The Central Bank of Canada exchange rate provider ID of the provider. 

    /// </returns> 

    /// <remarks> 

    /// The exchange rate provider ID should be a GUID string. 

    /// </remarks> 

    public static ExchangeRateProviderId providerId() 

    { 

        return #ProviderId; 

    } 

  

    public boolean validateConfigurationDetail(ExchangeRateProviderPropertyKey _key, ExchangeRateProviderPropertyValue _value) 

    { 

        return true; 

    } 

  

    public str enumNameForLookup(ExchangeRateProviderPropertyKey _key) 

    { 

        return ''; 

    } 

  

    public void set_factory(IExchangeRateProviderFrameworkFactory _factory) 

    { 

        factory = _factory; 

    } 

  

} 

 

No comments:

Post a Comment