public FinanceOperationModel TransferOperationToOtherCurrency(FinanceOperationModel operationParam, string currencyForSetParam)
        {
            if (string.Equals(operationParam.CurrencyName, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase))
            {
                return(operationParam);
            }

            var rates = _ratesGetter.GetCurrenciesRatesList();

            operationParam.Summ        = _convertHelper.ConvertToOtherCurrency(operationParam, rates, currencyForSetParam);
            operationParam.SummDecimal = Convert.ToDecimal(operationParam.Summ);

            return(operationParam);
        }
示例#2
0
        public bool IsConvertAvailable(FinanceOperationModel operationParam, IEnumerable <CurrenciesRatesModel> ratesParam, string currencyForSetParam)
        {
            bool isAvailable = ratesParam.Any
                                   (x => string.Equals(x.CurrencyFrom, operationParam.CurrencyName, StringComparison.CurrentCultureIgnoreCase) &&
                                   string.Equals(x.CurrencyTo, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase)

                                   || string.Equals(x.CurrencyTo, operationParam.CurrencyName, StringComparison.CurrentCultureIgnoreCase) &&
                                   string.Equals(x.CurrencyFrom, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase));

            if (!isAvailable)
            {
                throw new Exception($"OperationId {operationParam.OperationId} conversion from {operationParam.CurrencyName} to {currencyForSetParam} is not available");
            }
            else
            {
                return(isAvailable);
            }
        }
        /// <summary>
        /// Get max operation from the list
        /// </summary>
        /// <param name="operationListParam">list of operations to check</param>
        /// <param name="GetCurrentListCurrency">if - tre, get currency to change from </param>
        /// <returns>operation sum, id of operation from list and corrected sum</returns>
        protected virtual FinanceOperationModel GetMaxOperations(IEnumerable <IGrouping <string, Operation> > operationListParam, bool GetCurrentListCurrency = false)
        {
            FinanceOperationModel maxOperation = new FinanceOperationModel();

            foreach (var operation in operationListParam)
            {
                var financeOperationsList = operation.Select(x => new FinanceOperationModel()
                {
                    CurrencyName = x.Currency.Name,
                    OperationId  = x.Id.ToString(),
                    Summ         = Convert.ToDouble(x.Summ)
                }).ToList();
                var correctedSum = _rateScripter.SetOneCurrencyForAllOperations(financeOperationsList, GetCurrentListCurrency ? financeOperationsList.FirstOrDefault().CurrencyName : "USD").Sum(x => x.Summ);
                if (maxOperation.Summ < correctedSum)
                {
                    maxOperation.Summ         = correctedSum;
                    maxOperation.CurrencyName = financeOperationsList.FirstOrDefault().CurrencyName;
                    maxOperation.OperationId  = operation.FirstOrDefault().Id.ToString();
                }
            }
            return(maxOperation);
        }
示例#4
0
 public double ConvertToOtherCurrency(FinanceOperationModel operationParam, IEnumerable <CurrenciesRatesModel> ratesParam, string currencyForSetParam)
 {
     if (IsConvertAvailable(operationParam, ratesParam, currencyForSetParam))
     {
         if (ratesParam.Any(x => string.Equals(x.CurrencyFrom, operationParam.CurrencyName, StringComparison.CurrentCultureIgnoreCase) &&
                            string.Equals(x.CurrencyTo, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase)))
         {
             operationParam.Summ = operationParam.Summ /
                                   ratesParam.FirstOrDefault(rate => string.Equals(rate.CurrencyFrom, operationParam.CurrencyName, StringComparison.CurrentCultureIgnoreCase) &&
                                                             string.Equals(rate.CurrencyTo, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase)).BuyRate;
             return(operationParam.Summ);
         }
         else
         {
             operationParam.Summ = operationParam.Summ *
                                   ratesParam.FirstOrDefault(rate => string.Equals(rate.CurrencyFrom, operationParam.CurrencyName, StringComparison.CurrentCultureIgnoreCase) &&
                                                             string.Equals(rate.CurrencyTo, currencyForSetParam, StringComparison.CurrentCultureIgnoreCase)).SaleRate;
             return(operationParam.Summ);
         }
     }
     return(0);
 }