Beispiel #1
0
        public async Task <RevenueByCategoryViewModel> ExecuteAsync(DateTime startDate, DateTime endDate)
        {
            var distanceDays = DateTimeExtension.CountDays(startDate, endDate);
            var result       = new RevenueByCategoryViewModel();
            var ordersResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate, endDate, null);

            var ordersInPastResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate.AddDays(-distanceDays), endDate.AddDays(-distanceDays), null);

            var orderDetails = ordersResult.Items.SelectMany(n => n.OrderDetails).Where(n => n.CategoryId.HasValue).ToList();
            var totalRevenue = orderDetails.Sum(n => n.Quantity * n.Price);
            var categories   = await _orderDetailsRepository.TableNoTracking
                               .Where(n => n.CategoryId.HasValue)
                               .Select(n => new CategoryViewModel()
            {
                CategoryId   = n.CategoryId ?? 0,
                CategoryName = n.CategoryName
            })
                               .Distinct()
                               .ToListAsync();

            result.StartDate        = startDate;
            result.EndDate          = endDate;
            result.Details          = GetRevenueByCategoryDetail(categories, ordersResult.Items, ordersInPastResult.Items);
            result.CategoryRevenues = orderDetails
                                      .GroupBy(n => n.CategoryId)
                                      .Select(n => new CategoryRevenueViewModel()
            {
                CategoryId   = n.Key ?? 0,
                CategoryName = n.FirstOrDefault()?.CategoryName,
                TotalRevenue = n.Sum(x => x.Quantity * x.Price),
                Percent      = totalRevenue > 0 ? Math.Round(n.Sum(x => x.Quantity * x.Price) / totalRevenue * 100, 2) : 0
            }).ToList();

            return(result);
        }
Beispiel #2
0
        public async Task <RevenueByStoreViewModel> ExecuteAsync(DateTime startDate, DateTime endDate)
        {
            var distanceDays = DateTimeExtension.CountDays(startDate, endDate);
            var result       = new RevenueByStoreViewModel();
            var ordersResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate, endDate, null);

            var ordersInPastResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate.AddDays(-distanceDays), endDate.AddDays(-distanceDays), null);

            var totalRevenue = ordersResult.Items.Sum(n => n.Total);
            var stores       = await _orderRepository.TableNoTracking
                               .Select(n => new StoreViewModel()
            {
                Id   = n.StoreId,
                Name = n.StoreName
            })
                               .Distinct()
                               .ToListAsync();

            stores = stores.GroupBy(n => n.Id).Select(n => n.First()).ToList();

            result.StartDate     = startDate;
            result.EndDate       = endDate;
            result.Details       = GetRevenueByStoreDetail(stores, ordersResult.Items, ordersInPastResult.Items);
            result.StoreRevenues = ordersResult.Items
                                   .GroupBy(n => n.StoreId)
                                   .Select(n => new StoreRevenueViewModel()
            {
                StoreId      = n.Key,
                StoreName    = n.FirstOrDefault()?.StoreName,
                RevenueByDay = GetStoreRevenueByDay(n, startDate, endDate),
                TotalRevenue = (int)n.Sum(x => x.Total),
                Percent      = totalRevenue > 0 ? Math.Round(n.Sum(x => x.Total) / totalRevenue * 100, 2) : 0
            }).ToList();

            return(result);
        }
        public async Task <BestSellingProductsViewModel> ExecuteAsync(DateTime startDate, DateTime endDate)
        {
            var distanceDays = DateTimeExtension.CountDays(startDate, endDate);
            var result       = new BestSellingProductsViewModel();
            var ordersResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate, endDate, null);

            var ordersInPastResult = await _getListOrderQuery.ExecuteAsync(0, 0, (int)OrderStatus.Completed, null, startDate.AddDays(-distanceDays), endDate.AddDays(-distanceDays), null);

            var products = await _orderDetailsRepository.TableNoTracking
                           .Select(n => new ProductChartViewModel()
            {
                ProductId   = n.ProductId,
                ProductName = n.ProductName
            })
                           .Distinct()
                           .ToListAsync();

            result.StartDate = startDate;
            result.EndDate   = endDate;
            result.Details   = GetRevenueByProductDetail(products, ordersResult.Items, ordersInPastResult.Items);
            result.CalculateChartData();

            return(result);
        }