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

            IAvapiConnection connection = AvapiConnection.Instance;

            connection.Connect("BXGO930UI9P053HT");

            Int_TIME_SERIES_MONTHLY time_series_monthly = connection.GetQueryObject_TIME_SERIES_MONTHLY();

            List <Share>          shares = _repo.Shares.Where(e => e.Date.Year == date.Value.Year).ToList();
            List <PieChartData>   data   = new List <PieChartData>();
            List <StockViewModel> svm    = new List <StockViewModel>();
            List <Stock>          stocks = _stockRepo.Stocks.ToList();

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

            foreach (var stock in stocks)
            {
                try
                {
                    IAvapiResponse_TIME_SERIES_MONTHLY time_series_monthlyResponse = time_series_monthly.QueryPrimitive(
                        stock.Ticker
                        );

                    stockData.Add(stock.ID, time_series_monthlyResponse);

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

                    List <Share> listOfShares = shares.Where(e => e.StockID == stock.ID).ToList();
                    int          totalShares  = listOfShares.Select(e => e.NumOfShares).Sum();

                    svm.Add(new StockViewModel()
                    {
                        Stock            = stock,
                        TotalNumOfShares = totalShares,
                        CurrentValue     = Convert.ToDouble(sData.close),
                        TotalValue       = Convert.ToDouble(sData.close) * (double)totalShares
                    });
                }
                catch (Exception e)
                {
                }
            }

            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_TIME_SERIES_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 <Share> listOfShares = shares.Where(e => e.StockID == stock.ID && (e.Date.Year < date.Value.Year || (e.Date.Year == date.Value.Year && e.Date.Month <= month))).ToList();
                        int          totalShares  = listOfShares.Select(e => e.NumOfShares).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.Stocks.Select(x => x.SectorID).Distinct().ToList();

            foreach (var sector in sectors)
            {
                List <Share> sectorShares = shares.Where(e => e.Date.Year == date.Value.Year ? (e.Date.Month <= date.Value.Month)
                    : e.Date.Year <= date.Value.Year).ToList();
                int totalShares = sectorShares.Where(e => e.Stock.SectorID == sector).Select(e => e.NumOfShares).Sum();
                data.Add(new PieChartData
                {
                    Category = _sectorRepo.Sectors.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.NumOfShares;
            }

            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 StockListViewModel
            {
                Stocks = svm,
                Date = date.Value,
                PieChartData = data,
                LineChartData = lineData,
                AmountInvested = Convert.ToDecimal(shares.Select(e => e.NumOfShares * e.PurchasePrice).Sum())
            }));
        }