protected virtual void InsertRate(CurrencyRateInfo currencyRateInfo)
        {
            Entity rateEntity = CurrencyRateSchema.CreateEntity(UserConnection);

            rateEntity.SetDefColumnValues();
            SetColumnsValueToInvertRate(currencyRateInfo, rateEntity);
            rateEntity.Save(false);
        }
        protected virtual void SetColumnsValueToInvertRate(CurrencyRateInfo currencyRateInfo, Entity rateEntity)
        {
            var currentDate = UserConnection.CurrentUser.GetCurrentDateTime();

            rateEntity.SetColumnValue("Id", Guid.NewGuid());
            rateEntity.SetColumnValue("StartDate", currentDate);
            rateEntity.SetColumnValue("CurrencyId", currencyRateInfo.CurrencyId);
            rateEntity.SetColumnValue("Rate", currencyRateInfo.Rate);
            rateEntity.SetColumnValue("RateMantissa", currencyRateInfo.RateMantissa);
        }
Пример #3
0
 protected decimal GetConvertedAmount(decimal amount, CurrencyRateInfo currentCurrencyRateInfo,
                                      CurrencyRateInfo targetCurrencyRateInfo)
 {
     currentCurrencyRateInfo.CheckArgumentNull(nameof(currentCurrencyRateInfo));
     targetCurrencyRateInfo.CheckArgumentNull(nameof(targetCurrencyRateInfo));
     ValidateRateAndDivicion(currentCurrencyRateInfo);
     ValidateRateAndDivicion(targetCurrencyRateInfo);
     return((amount * currentCurrencyRateInfo.Division * targetCurrencyRateInfo.Rate) /
            (targetCurrencyRateInfo.Division * currentCurrencyRateInfo.Rate));
 }
Пример #4
0
        private CurrencyRateInfo GetCurrencyRateInfo(Guid currencyId, IEnumerable <CurrencyRateInfo> currenciesRate)
        {
            CurrencyRateInfo currencyRateInfo = currenciesRate.FirstOrDefault(a => a.CurrencyId == currencyId);

            if (currencyRateInfo == null)
            {
                throw new CurrencyNotFoundException(ResourceStorage, currencyId);
            }
            return(currencyRateInfo);
        }
        private List <CurrencyRateInfo> GetCurrencyRateInfosFromDb(IEnumerable <Guid> currenciesId, DateTime date)
        {
            Select query = GetCurrencyRatesSelect(currenciesId, date);
            var    rates = new List <CurrencyRateInfo>();

            query.ExecuteReader((reader) => {
                CurrencyRateInfo rate = GetCurrencyRateInfo(reader);
                rates.Add(rate);
            });
            return(rates);
        }
Пример #6
0
        /// <summary>
        /// Returns converted amount to currency.
        /// </summary>
        /// <param name="currentCurrencyRateInfo">Current currency rate information.</param>
        /// <param name="targetCurrencyRateInfo">Target currency rate information.</param>
        /// <param name="amount">Amount.</param>
        /// <returns>Converted amount.</returns>
        public decimal GetConvertedAmountToCurrency(CurrencyRateInfo currentCurrencyRateInfo,
                                                    CurrencyRateInfo targetCurrencyRateInfo, decimal amount)
        {
            if (amount == 0)
            {
                return(amount);
            }
            currentCurrencyRateInfo.CheckArgumentNull(nameof(currentCurrencyRateInfo));
            targetCurrencyRateInfo.CheckArgumentNull(nameof(targetCurrencyRateInfo));
            decimal convertedValue = GetConvertedAmount(amount, currentCurrencyRateInfo, targetCurrencyRateInfo);

            return(convertedValue);
        }
Пример #7
0
        /// <summary>
        /// Returns converted amount to currency.
        /// </summary>
        /// <param name="currentCurrencyRateInfo">Current currency rate information.</param>
        /// <param name="targetCurrencyId">Target currency identifier.</param>
        /// <param name="amount">Amount.</param>
        /// <param name="dateTime">Date of rate.</param>
        /// <returns>Converted amount.</returns>
        public decimal GetConvertedAmountToCurrency(CurrencyRateInfo currentCurrencyRateInfo,
                                                    Guid targetCurrencyId, decimal amount, DateTime dateTime)
        {
            if (amount == 0)
            {
                return(amount);
            }
            targetCurrencyId.CheckArgumentEmpty(nameof(targetCurrencyId));
            IEnumerable <CurrencyRateInfo> currenciesRate = CurrenciesRateStorage
                                                            .GetCurrenciesRate(new[] { targetCurrencyId }, dateTime);
            CurrencyRateInfo targetCurrencyRateInfo = GetCurrencyRateInfo(targetCurrencyId, currenciesRate);
            decimal          convertedValue         = GetConvertedAmountToCurrency(currentCurrencyRateInfo, targetCurrencyRateInfo,
                                                                                   amount);

            return(convertedValue);
        }
Пример #8
0
        /// <summary>
        /// Calculates the amount in primary currency.
        /// </summary>
        /// <param name="entity">The entity to calculate</param>
        /// <returns></returns>
        public virtual void CalculatePrimaryAmount(Entity entity)
        {
            var amount        = entity.GetTypedColumnValue <decimal>("Amount");
            var primaryAmount = entity.GetTypedColumnValue <decimal>("PrimaryAmount");
            var currencyId    = entity.GetTypedColumnValue <Guid>("CurrencyId");
            var currencyRate  = entity.GetTypedColumnValue <decimal>("CurrencyRate");

            if (currencyId == Guid.Empty)
            {
                return;
            }
            CurrencyRateInfo currencyInfo = null;

            if (currencyRate == 0)
            {
                currencyInfo = GetCurrencyInfo(currencyId);
                if (currencyInfo == null)
                {
                    return;
                }
                currencyRate = currencyInfo.Rate;
                entity.SetColumnValue("CurrencyRate", currencyRate);
            }
            if (primaryAmount == 0 && amount != 0)
            {
                if (currencyInfo == null)
                {
                    currencyInfo = GetCurrencyInfo(currencyId);
                }
                if (currencyInfo == null)
                {
                    return;
                }
                var currencyDivision = currencyInfo.Division;
                if (currencyDivision != 0)
                {
                    primaryAmount = amount / currencyRate * currencyDivision;
                    entity.SetColumnValue("PrimaryAmount", primaryAmount);
                }
            }
        }
Пример #9
0
        public virtual void OnCurrencySavedHandler()
        {
            Store.Cache[CacheLevel.Application].ExpireGroup("Currency");
            var currRateSchema      = UserConnection.EntitySchemaManager.GetInstanceByName("CurrencyRate");
            var currencyRateStorage = Terrasoft.Core.Factories.ClassFactory.Get <Terrasoft.Configuration.CurrencyRateStorage>(
                new Terrasoft.Core.Factories.ConstructorArgument("userConnection", UserConnection),
                new Terrasoft.Core.Factories.ConstructorArgument("schema", currRateSchema));
            List <CurrencyRateInfo> rates = currencyRateStorage.GetActualCurrencyRates(Entity.PrimaryColumnValue);
            CurrencyRateInfo        currentCurrenyRate = rates.FirstOrDefault();

            if (currentCurrenyRate != null)
            {
                OldCurrencyRate = CurrencyRateHelper.SetMantissaToRate(currentCurrenyRate.Rate, currentCurrenyRate.RateMantissa);
            }
            if (CurrencyRate > 0.00m && OldCurrencyRate != CurrencyRate)
            {
                currencyRateStorage.SaveRates(new CurrencyRateInfo()
                {
                    CurrencyId = Entity.PrimaryColumnValue,
                    Rate       = CurrencyRate
                });
            }
        }
Пример #10
0
 private void ValidateRateAndDivicion(CurrencyRateInfo currencyRateInfo)
 {
     ValidateValueLessOrEqualsZero(currencyRateInfo.Rate, "Rate", currencyRateInfo.CurrencyId);
     ValidateValueLessOrEqualsZero(currencyRateInfo.Division, "Division", currencyRateInfo.CurrencyId);
 }