Пример #1
0
        public ViewResult YearlyReport(DateTime?date)
        {
            if (date == null)
            {
                date = DateTime.Now;
            }

            IAvapiConnection connection = AvapiConnection.Instance;

            connection.Connect("BXGO930UI9P053HT");

            Int_DIGITAL_CURRENCY_MONTHLY time_series_monthly = connection.GetQueryObject_DIGITAL_CURRENCY_MONTHLY();

            List <Coin>            shares = _repo.Coins.Where(e => e.Date.Year == date.Value.Year).ToList();
            List <PieChartData>    data   = new List <PieChartData>();
            List <CryptoViewModel> svm    = new List <CryptoViewModel>();
            List <CryptoCurrency>  stocks = _stockRepo.CryptoCurrencies.ToList();

            Dictionary <int, IAvapiResponse_DIGITAL_CURRENCY_MONTHLY> stockData = new Dictionary <int, IAvapiResponse_DIGITAL_CURRENCY_MONTHLY>();

            foreach (var stock in stocks)
            {
                IAvapiResponse_DIGITAL_CURRENCY_MONTHLY time_series_monthlyResponse = time_series_monthly.QueryPrimitive(
                    stock.Ticker,
                    "USD"
                    );

                stockData.Add(stock.ID, time_series_monthlyResponse);

                var sData = time_series_monthlyResponse.Data.TimeSeries.First();

                List <Coin> listOfShares = shares.Where(e => e.CryptoCurrencyID == stock.ID).ToList();
                decimal     totalShares  = listOfShares.Select(e => e.NumOfCoins).Sum();

                svm.Add(new CryptoViewModel()
                {
                    Currency        = stock,
                    TotalNumOfCoins = totalShares,
                    CurrentValue    = Convert.ToDouble(sData.CloseUSD),
                    TotalValue      = Convert.ToDouble(sData.CloseUSD) * (double)totalShares
                });
            }

            List <LineChartData> lineData = new List <LineChartData>();

            int    month        = 1;
            double currentValue = 0;

            while (month <= date.Value.Month)
            {
                double total = 0;
                foreach (var stock in stocks)
                {
                    IAvapiResponse_DIGITAL_CURRENCY_MONTHLY time_series_monthlyResponse = stockData.GetValueOrDefault(stock.ID);

                    var sData = time_series_monthlyResponse.Data.TimeSeries.Where(x => Convert.ToDateTime(x.DateTime).Month == month &&
                                                                                  Convert.ToDateTime(x.DateTime).Year == date.Value.Year).FirstOrDefault();
                    if (sData != null)
                    {
                        List <Coin> listOfShares = shares.Where(e => e.CryptoCurrencyID == stock.ID && (e.Date.Year < date.Value.Year || (e.Date.Year == date.Value.Year && e.Date.Month <= month))).ToList();
                        decimal     totalShares  = listOfShares.Select(e => e.NumOfCoins).Sum();
                        total += Convert.ToDouble(sData.Close) * (double)totalShares;
                    }
                }

                currentValue = total;

                lineData.Add(new LineChartData
                {
                    XData = DateTimeFormatInfo.CurrentInfo.GetMonthName(month).Substring(0, 3),
                    YData = currentValue.ToString()
                });

                month++;
            }


            List <int> sectors = _stockRepo.CryptoCurrencies.Select(x => x.ID).Distinct().ToList();

            foreach (var sector in sectors)
            {
                List <Coin> sectorShares = shares.Where(e => e.Date.Year == date.Value.Year ? (e.Date.Month <= date.Value.Month)
                    : e.Date.Year <= date.Value.Year).ToList();
                decimal totalShares = sectorShares.Where(e => e.CryptoCurrency.ID == sector).Select(e => e.NumOfCoins).Sum();
                data.Add(new PieChartData
                {
                    Category = _stockRepo.CryptoCurrencies.Where(x => x.ID == sector).FirstOrDefault().Name,
                    Data     = Convert.ToString(totalShares)
                });
            }

            double amountInvested = 0;

            foreach (var s in shares)
            {
                amountInvested += (double)s.PurchasePrice * (double)s.NumOfCoins;
            }

            List <IncomeEntry> incomes = _incomeRepo.IncomeEntries.Where(i => i.Date.Month == date.Value.Month && i.Date.Year == date.Value.Year).ToList();
            int    daysInMonth         = DateTime.DaysInMonth(date.Value.Year, date.Value.Month);
            double sum = 0;

            for (int j = 1; j <= daysInMonth; j++)
            {
                sum += incomes.Where(i => i.Date.Day == j).Select(e => e.Amount).Sum();
            }

            return(View(new CryptoListViewModel
            {
                Stocks = svm,
                Date = date.Value,
                PieChartData = data,
                LineChartData = lineData,
                AmountInvested = Convert.ToDecimal(shares.Select(e => (double)e.NumOfCoins * e.PurchasePrice).Sum())
            }));
        }