Exemplo n.º 1
0
        public async Task <IActionResult> Index()
        {
            var user = await userManager.GetUserAsync(User);

            var previousMonth         = DateTime.Now.AddDays(-DateTime.Now.Day);
            var previousPreviousMonth = previousMonth.AddDays(-previousMonth.Day);

            return(View(new UserHome {
                Last30DaysSumCosts = new CostsForPeriod {
                    Sum = await costsRepository.GetSumCostsAsync(user, DateTime.Now.AddDays(-30)),
                    CostsByCategory = await costsRepository.GroupCostsByCategoryAsync(user, DateTime.Now.AddDays(-30), take: 3),
                    PreviousPeriodSum = await costsRepository.GetSumCostsAsync(user, DateTime.Now.AddDays(-60), DateTime.Now.AddDays(-30)),
                },
                CurrentMonthSumCosts = new CostsForPeriod {
                    Sum = await costsRepository.GetSumCostsAsync(user, previousMonth),
                    CostsByCategory = await costsRepository.GroupCostsByCategoryAsync(user, previousMonth, take: 3),
                    PreviousPeriodSum = await costsRepository.GetSumCostsAsync(user, previousPreviousMonth, previousMonth),
                },
                YearSumCosts = new CostsForPeriod {
                    Sum = await costsRepository.GetSumCostsAsync(user, DateTime.Now.AddYears(-1)),
                    CostsByCategory = await costsRepository.GroupCostsByCategoryAsync(user, DateTime.Now.AddYears(-1), take: 3),
                    PreviousPeriodSum = await costsRepository.GetSumCostsAsync(user, DateTime.Now.AddYears(-2), DateTime.Now.AddYears(-1)),
                }
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Index(GetStatistic getStatistic = null)
        {
            if (getStatistic.Start == default || getStatistic.End == default)
            {
                getStatistic = new GetStatistic {
                    Start = DateTime.Now.AddYears(-1), End = DateTime.Now
                };
            }
            else if (!ModelState.IsValid)
            {
                return(View(getStatistic));
            }
            var user = await userManager.GetUserAsync(User);

            getStatistic.SumCosts = await costsRepository.GetSumCostsAsync(user, getStatistic.Start, getStatistic.End);

            var groupCostsByProduct = await costsRepository.GroupCostsByProductAsync(user, getStatistic.Start, getStatistic.End, take : 20);

            List <string>         productLabels    = new();
            List <ChartJSDataset> productsDatasets = new();

            foreach (var cbp in groupCostsByProduct)
            {
                productLabels.Add(cbp.GroupName);
                ChartJSDataset dataset = new(cbp.GroupName);
                dataset.Add(Convert.ToDouble(cbp.Sum), NextChartColor);
                productsDatasets.Add(dataset);
            }

            var groupCostsByCategory = await costsRepository.GroupCostsByCategoryAsync(user, getStatistic.Start, getStatistic.End);

            List <string>  categoryLabels = new();
            ChartJSDataset costsDataset   = new("Расходы по категориям");

            foreach (var cbc in groupCostsByCategory)
            {
                categoryLabels.Add(cbc.GroupName);
                costsDataset.Add(Convert.ToDouble(cbc.Sum), NextChartColor);
            }

            List <string>  monthLabels      = new();
            ChartJSDataset costsSumDataset  = new("Расходы за месяц", NextChartColor);
            List <string>  top5ProductNames = new();

            for (int i = 0; i < 5 && i < productLabels.Count; i++)
            {
                top5ProductNames.Add(productLabels[i]);
            }
            int countTopProducts    = productLabels.Count >= 5 ? 5 : productLabels.Count;
            var top5ProductsDataset = new ChartJSDataset[countTopProducts];

            for (int i = 0; i < top5ProductsDataset.Length; i++)
            {
                top5ProductsDataset[i] = new ChartJSDataset(top5ProductNames[i], NextChartColor);
            }
            DateTime tempDate = getStatistic.Start;

            while (tempDate < getStatistic.End)
            {
                monthLabels.Add(tempDate.ToString("MMMM") + $" {tempDate.Year}");
                decimal sumCostsPerMonth = await costsRepository.GetSumCostsPerMonthAsync(user, tempDate);

                costsSumDataset.AddValue(Convert.ToDouble(sumCostsPerMonth));

                for (int i = 0; i < top5ProductsDataset.Length; i++)
                {
                    decimal productSumPerMonth = await costsRepository.GetSumCostsPerMonthAsync(user, top5ProductNames[i], tempDate);

                    top5ProductsDataset[i].AddValue(Convert.ToDouble(productSumPerMonth));
                }

                tempDate = tempDate.AddMonths(1);
            }

            ViewData["productsBarChart"]     = GenerateBarChart(productsDatasets);
            ViewData["categoriesPieChart"]   = GeneratePieChart(costsDataset, categoryLabels);
            ViewData["costsLineChart"]       = GenerateLineChart(costsSumDataset, monthLabels);
            ViewData["topProductsLineChart"] = GenerateLineChart(top5ProductsDataset, monthLabels);
            getStatistic.IsCompleteStatistic = true;

            return(View(getStatistic));
        }