Phương thức lấy ExchangeRate theo ngày, từ Currency đến Currency


08/04/2020- duocnt    1125 Views    


Nội DUNG

  1. Phương thức hỗ trợ ExchangeRateTypeRecId GetDefaultExchangeRateTypeId().
  2. Phương thức hỗ trợ TransTxt exchangeRateText().
  3. Phương thức lấy exchangerate container ExchangeRateValue().
  4. Cách gọi phương thức.
  5. Lưu ý ExchangeRate.

1 . Phương thức hỗ trợ ExchangeRateTypeRecId GetDefaultExchangeRateTypeId()

    public static ExchangeRateTypeRecId GetDefaultExchangeRateTypeId()
    {
        ExchangeRateType defaultExchangeRateType;
        ExchangeRateTypeRecId defaultExchangeRateTypeId;
        select count(RecId) from defaultExchangeRateType;
        if (defaultExchangeRateType.RecId == 1)
        {
            select firstonly RecId from defaultExchangeRateType;
            defaultExchangeRateTypeId = defaultExchangeRateType.RecId;
        }
        else
        {
            CurrencyIDerivationRule currencyDerivationRule = null;
            SysPluginMetadataCollection metadataCollection = new SysPluginMetadataCollection();
            metadataCollection.SetManagedValue("CurrencyDerivationRule", "DefaultCurrency");
            currencyDerivationRule = SysPluginFactory::Instance("Dynamics.AX.Application", classStr(CurrencyIDerivationRule), metadataCollection);
 
            if(currencyDerivationRule)
            {
                defaultExchangeRateTypeId=currencyDerivationRule.defaultExchangeRateType();
            }
        }
        return defaultExchangeRateTypeId;
    }


2 . Phương thức hỗ trợ TransTxt exchangeRateText().

    public static TransTxt exchangeRateText(
                                            ExchangeRateCurrencyPair selectedCurrencyPair, 
                                            AmountCur exchangeRateValue
                                            )
    {
        return strFmt(
                "@SYS78085",
                num2str(exchangeRateValue, -1, numOfDec(exchangeRateValue), -1, -1),
                selectedCurrencyPair.ToCurrencyCode,
               enum2str(selectedCurrencyPair.ExchangeRateDisplayFactor),
               selectedCurrencyPair.FromCurrencyCode);
    }

3 . Phương thức lấy ExchangeRate

    public static container ExchangeRateValue(
                                                DlvDate _ParmDate,
                                                boolean _decimal,
                                                str FromCurrency,
                                                str ToCurrency)
    {
        try
        {
            container                    _container;
            TransTxt                     _exchangeRateText;
            EXCHANGERATE                 _ExchangeRate;
            ExchangeRateType             _ExchangeRateType;
            ExchangeRateTypeRecId        _ExchangeRateTypeRecId;
            ExchangeRateCurrencyPair     _ExchangeRateCurrencyPair;
            AmountCur                    _ExchangeRateValue;
            int                          _SelectedMonth;
            int                          _SelectedYear;
            date                         _startDateRange;
            date                         _endDateRange;
            boolean                      _indicator    =  false;
 
 
            //Get month of selected date
            _SelectedMonth = mthOfYr(_ParmDate);
            _SelectedYear = year(_ParmDate);
            _ExchangeRateTypeRecId = ExtendedFunctions::GetDefaultExchangeRateTypeId();
            select * from _ExchangeRateType where _ExchangeRateType.RecId== _ExchangeRateTypeRecId;
            select * from _ExchangeRateCurrencyPair
            where _ExchangeRateCurrencyPair.ExchangeRateType==_ExchangeRateType.RecId
            && _ExchangeRateCurrencyPair.FromCurrencyCode==FromCurrency
            && _ExchangeRateCurrencyPair.ToCurrencyCode==ToCurrency;
            //HERE : Reference to AX
            if (DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()) < dateNull() + 30)
            {
                _startDateRange = dateNull();
            }
            else
            {
              //_startDateRange = DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone())-30
                _startDateRange = _ParmDate;
            }
 
            if (DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()) > dateMax() - 30)
            {
                _endDateRange = dateMax();
            }
            else
            {
                //_endDateRange =DateTimeUtil::getSystemDate(DateTimeUtil::getUserPreferredTimeZone()) +30;
                _endDateRange = _startDateRange + 30;
            }
            if (_ExchangeRateCurrencyPair.RecId>0)
            {
                while select validtimestate(_startDateRange, _endDateRange) _ExchangeRate
                order by _ExchangeRate.ValidFrom
                where _ExchangeRate.ExchangeRateCurrencyPair == _ExchangeRateCurrencyPair.RecId
                {
                    int MonthInDb = mthOfYr(_ExchangeRate.ValidFrom);
                    int YearInDb = year(_ExchangeRate.ValidFrom);
                    if(YearInDb == _SelectedYear && MonthInDb== _SelectedMonth)
                    {
                        _ExchangeRateValue =_ExchangeRate.ExchangeRate;
                        _indicator=true;
                        break;
                    }
                }
                //In case there is not exchagerate matched with selectedmonth => get latest ExchangeRate
                int i=1;
                if(_indicator==false)
                {
                    while select validtimestate(_startDateRange, _endDateRange) _ExchangeRate
                    order by _ExchangeRate.ValidFrom desc
                    where _ExchangeRate.ExchangeRateCurrencyPair == _ExchangeRateCurrencyPair.RecId
                    {
                        int MonthInDb = mthOfYr(_ExchangeRate.ValidFrom);
                        int YearInDb = year(_ExchangeRate.ValidFrom);
                        if(YearInDb == _SelectedYear)
                        {
                            if((MonthInDb-1) == _SelectedMonth)
                            {                                                                _ExchangeRateValue = _ExchangeRate.ExchangeRate;
                                break;
                            }
                            i++;
                        }
                    }
                }
            }
            //END If _ExchangeRateCurrencyPair.RecId HERE
 
            //Determin what type of return value (decimal or integer)
            if(_decimal==true)
            {
                _ExchangeRateValue =      _ExchangeRateValue/100;
            }
 
           _exchangeRateText = ExtendedFunctions::exchangeRateText(                                                                         _ExchangeRateCurrencyPair,                                                                         _ExchangeRateValue                                                                     );
 
            _container =[_ExchangeRateValue,_exchangeRateText];
            return _container;
            throw Exception::Error;
        }
        catch(Exception::Error)
        {
            infolog.add(Exception::Error, "It's broken");
            throw Exception::Error; // number 3
        }
    }

4 . Cách sử dụng
 - Lưu ý: theo cách gọi dưới đây thì phương thức này phải được viết trong class 
ExtendedFunctions

        container   _container;

        AmountCur   _exchangerate;

        TransTxt    _exchangeRateText;

        _container          =  ExtendedFunctions::ExchangeRateValue(today(),true,"USD","VND");

        _exchangerate       =  conPeek(_container,1);

        _exchangeRateText   =  conPeek(_container,2);

5 . Lưu ý về ExchageRate.

 - ExchangeRate phải được setup theo từng tháng như hình bên dưới.

Góp ý kiến

;
;