Esempio n. 1
0
        private void LoadCalendarData()
        {
            // TODO: reload only if main currency or currency exchange rate was changed
            _data.MainCurrency = _currencyService.GetMain(_userId).ToReferenceView();
            _data.Rates        = _currencyExchangeRateService.GetList(_userId, _data.MainCurrency.Id);

            // always reload
            _data.Storages   = _storageService.GetListForUser(_userId);
            _data.Categories = _categoryService.GetListForUser(_userId);
        }
Esempio n. 2
0
        private decimal CalculateSpendValue(LimitModel model)
        {
            // load actual transactions for the limit
            var transactions = _transactionService.GetActual(model.UserId, model.DateFrom, model.DateUntil,
                                                             RecordType.Expense, model.IncludeWithoutCategory, model.AllCategories ? null : model.ActualCategoryIds);

            // calculate the same currency amount
            var spentValue = transactions
                             .Where(x => x.TransactionCurrencyId == model.CurrencyId)
                             ?.Sum(x => x.TransactionAmount) ?? 0;

            // fetch other currency transactions
            var diffCurrTransactions = transactions.Where(x => x.TransactionCurrencyId != model.CurrencyId).ToList();

            if (diffCurrTransactions.Any())
            {
                // load currency exchange rates
                var currencyIds = diffCurrTransactions.Select(x => x.TransactionCurrencyId).Distinct()
                                  .Union(new[] { model.CurrencyId }).ToList();
                var currencyExchangeRates = _currencyExchangeRateService.GetList(currencyIds);

                diffCurrTransactions.ForEach(t =>
                {
                    // try to find exchange rate
                    var rate = currencyExchangeRates
                               .FirstOrDefault(x => x.CurrencyFromId == t.TransactionCurrencyId && x.CurrencyToId == model.CurrencyId)
                               ?.Rate;

                    // try to fing an opposite exchange rate
                    if (!rate.HasValue)
                    {
                        rate = currencyExchangeRates
                               .FirstOrDefault(x => x.CurrencyFromId == model.CurrencyId && x.CurrencyToId == t.TransactionCurrencyId)
                               ?.Rate;

                        if (rate.HasValue)
                        {
                            rate = 1M / rate;
                        }
                    }

                    // check any rate was found
                    if (!rate.HasValue)
                    {
                        rate = 1;
                    }

                    // update spent value
                    spentValue += t.TransactionAmount * rate.Value;
                });
            }

            return(-spentValue);
        }
Esempio n. 3
0
        internal static List <T> UpdateEventsExchangeRate <T>(ICurrencyExchangeRateService currencyExchangeRateService, List <T> events)
            where T : EventModel
        {
            var currencyExchangeRates = currencyExchangeRateService.GetList(events
                                                                            .Where(x => x.TakeExistingCurrencyExchangeRate && x.IsCurrencyExchangeRateRequired)
                                                                            .Select(x => new[] { x.CurrencyFromId, x.CurrencyToId })
                                                                            .SelectMany(x => x)
                                                                            .ToList());

            // update currency exchange rates for all events
            foreach (var evnt in events.Where(x => x.TakeExistingCurrencyExchangeRate && x.IsCurrencyExchangeRateRequired).ToList())
            {
                evnt.CurrencyExchangeRate = currencyExchangeRates.FirstOrDefault(x =>
                                                                                 x.CurrencyFromId == evnt.CurrencyFromId && x.CurrencyToId == evnt.CurrencyToId)?.Rate ?? 1;
            }

            return(events);
        }
Esempio n. 4
0
 public SimpleStorageSummary CalculateSingleCurrencySummary(CurrencyModel currency,
                                                            ICurrencyExchangeRateService currencyExchangeRateService)
 {
     return(CalculateSingleCurrencySummary(currency,
                                           currencyExchangeRateService.GetList(_userId, currency.Id)));
 }