Esempio n. 1
0
        public void GetCountOfProducts()
        {
            RequestModel model = new RequestModel
            {
                Id        = 1,
                StartDate = new DateTime(2014, 1, 1),
                EndDate   = new DateTime(2017, 12, 31)
            };
            SalesByProductModel modelProduct = set.SalesByProduct.Report(model);

            Assert.AreEqual(modelProduct.ProductSales.Count, 3);
        }
Esempio n. 2
0
        public void GetPercentProducts()
        {
            RequestModel model = new RequestModel
            {
                Id        = 1,
                StartDate = new DateTime(2014, 1, 1),
                EndDate   = new DateTime(2017, 12, 31)
            };
            SalesByProductModel modelProduct = set.SalesByProduct.Report(model);
            double total = 0;

            foreach (var produc in modelProduct.ProductSales)
            {
                total += produc.ProductPercent;
            }
            Assert.AreEqual(modelProduct.PercentTotal, total);
        }
        public SalesByProductModel Report(RequestModel Request)
        {
            List <Item> Items = UnitOfWork.Items.Get()
                                .Where(x => x.Invoice.Date >= Request.StartDate &&
                                       x.Invoice.Date <= Request.EndDate &&
                                       x.Product.Category.Id == Request.Id
                                       ).ToList();

            List <Invoice> Invoices      = UnitOfWork.Invoices.Get().Where(x => x.Date >= Request.StartDate && x.Date <= Request.EndDate).ToList();
            string         categoryName  = UnitOfWork.Categories.Get(Request.Id).Name;
            double         categoryTotal = Items.Sum(x => Math.Round(x.Quantity * x.Price, 2));

            double invoiceTotal = Invoices.Sum(x => x.SubTotal);
            double percentTotal = Math.Round(100 * categoryTotal / invoiceTotal, 2);


            SalesByProductModel result = Factory.SalesByProductCreate(Request.StartDate, Request.EndDate, categoryName, categoryTotal, percentTotal);

            result.ProductSales = Items.Select(x => Factory.Create(Invoices, x.Product.Name, x.SubTotal, categoryTotal, invoiceTotal))
                                  .ToList();
            return(result);
        }
Esempio n. 4
0
        public SalesByProductModel Report(DateTime start, DateTime end, int CategoryId)
        {
            SalesByProductModel result = new SalesByProductModel();
            var      Invoices          = _unitOfWork.Invoices.Get().Where(x => (x.Date >= start && x.Date <= end)).ToList();
            var      Items             = Invoices.SelectMany(x => x.Items).ToList();
            Category a = _unitOfWork.Categories.Get(CategoryId);

            result.StartDate     = start;
            result.EndDate       = end;
            result.Sales         = new List <CategorySalesByProductModel>();
            result.CategoryName  = a.Name;
            result.CategoryTotal = Items.Where(x => x.Product.Category.Id == CategoryId).Sum(x => x.SubTotal);
            double CategoryTotal = Items.Where(x => x.Product.Category.Id == CategoryId).Sum(x => x.SubTotal);
            double grandTotal    = Items.Sum(x => x.SubTotal);

            result.CategoryPercent = Math.Round(100 * CategoryTotal / grandTotal, 2);

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

            foreach (var item in query)
            {
                CategorySalesByProductModel product = new CategorySalesByProductModel()
                {
                    Name         = item.Name,
                    Total        = item.Total,
                    Percent      = Math.Round(100 * item.Total / CategoryTotal, 2),
                    TotalPercent = Math.Round(100 * item.Total / grandTotal, 2)
                };
                result.Sales.Add(product);
            }
            return(result);
        }