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); }
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); }
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); }
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); }