コード例 #1
0
        public SalesByCategoryModel Report(RequestModel Request)
        {
            SalesByCategoryModel result = new SalesByCategoryModel()
            {
                StartDate = Request.StartDate,
                EndDate   = Request.EndDate,
            };
            //Get list of invoices in Time Period
            var Invoices = UnitOfWork.Invoices.Get().Where(x => x.Date >= Request.StartDate && x.Date <= Request.EndDate).ToList();
            //Returns list of Items for Invoices
            var Items = Invoices.SelectMany(x => x.Items).ToList();

            result.GrandTotal = Invoices.Sum(x => x.SubTotal);

            var query = Items.GroupBy(x => x.Product.Category)
                        .Select(x =>
                                new
            {
                CategoryName  = x.Key.Name,
                CategoryTotal = x.Sum(y => y.SubTotal)
            }).ToList();


            foreach (var item in query)
            {
                CategorySalesModel calculatedCategory = new CategorySalesModel
                {
                    CategoryName    = item.CategoryName,
                    CategoryTotal   = item.CategoryTotal,
                    CategoryPercent = 100 * item.CategoryTotal / result.GrandTotal
                };
                result.Sales.Add(calculatedCategory);
            }
            return(result);
        }
コード例 #2
0
ファイル: SalesByCategory.cs プロジェクト: drveni92/echo
        public SalesByCategoryModel Report(DateTime start, DateTime end)
        {
            SalesByCategoryModel result = new SalesByCategoryModel(start, end);

            var Invoices = _unitOfWork.Invoices.Get().Where(x => (x.Date >= start && x.Date <= end)).ToList();
            var Items    = Invoices.SelectMany(x => x.Items).ToList();

            result.GrandTotal = Invoices.Sum(x => x.SubTotal);

            var query = Items.GroupBy(x => x.Product.Category)
                        .Select(x => new { CategoryId = x.Key, CategoryTotal = x.Sum(y => y.SubTotal) })
                        .ToList();

            foreach (var item in query)
            {
                CategorySalesModel category = new CategorySalesModel()
                {
                    Id      = item.CategoryId.Id,
                    Name    = item.CategoryId.Name,
                    Total   = item.CategoryTotal,
                    Percent = Math.Round(item.CategoryTotal / result.GrandTotal * 100, 2),
                };

                result.Sales.Add(category);
            }

            return(result);
        }
コード例 #3
0
        public void GetCountOfCategory()
        {
            RequestModel model = new RequestModel()
            {
                StartDate = new DateTime(2015, 1, 1),
                EndDate   = new DateTime(2018, 1, 1)
            };

            SalesByCategoryModel categoryModel = set.SalesByCategory.Report(model);

            Assert.IsNotNull(categoryModel.Sales.Count);
        }
コード例 #4
0
        public void GetGrandTotal()
        {
            RequestModel model = new RequestModel()
            {
                StartDate = new DateTime(2015, 1, 1),
                EndDate   = new DateTime(2018, 1, 1)
            };

            SalesByCategoryModel categoryModel = set.SalesByCategory.Report(model);
            double total = 2099;

            Assert.AreEqual(categoryModel.GrandTotal, total);
        }