Example #1
0
        public JsonResult GetStatisticProduct(int id)
        {
            var product = handler.Get(id);
            var productStatisticViewModel = new ProductStatisticViewModel();

            productStatisticViewModel.Product = product.Name;
            var price = product.Price;

            productStatisticViewModel.Price = price;
            var operations = GetOperations(id);
            var quantity   = GetTotalQuantity(operations);

            productStatisticViewModel.TotalQuantity = quantity;
            productStatisticViewModel.TotalCost     = quantity * price;

            return(Json(productStatisticViewModel, JsonRequestBehavior.AllowGet));
        }
        public string GetStatisticProduct(string productId)
        {
            var id = (int)JsonConvert.DeserializeObject(productId);

            var product = handler.Get(id);
            var productStatisticViewModel = new ProductStatisticViewModel();

            productStatisticViewModel.Product = product.Name;
            var price = product.Price;

            productStatisticViewModel.Price = price;
            var quantity = kernel.Get <IHandler <OperationDto> >().GetAll().Where(i => i.ProductId == id).Select(i => i.Quantity).Sum();

            productStatisticViewModel.TotalQuantity = quantity;
            productStatisticViewModel.TotalCost     = quantity * price;

            return(JsonConvert.SerializeObject(productStatisticViewModel));
        }
        public async Task <ProductStatisticViewModel> GetProductStatistics()
        {
            var statistics = new ProductStatisticViewModel();

            //count product
            var query = from product in _context.HangHoa
                        group product by product.LoaiHangHoaId
                        into grProduct
                        select new
            {
                Id    = grProduct.Key,
                Total = grProduct.Count()
            };

            var countProducts = await query.ToListAsync();

            if (countProducts.ElementAtOrDefault(0) != null)
            {
                statistics.TotalBooks = countProducts[0].Total;
            }

            if (countProducts.ElementAtOrDefault(1) != null)
            {
                statistics.TotalStationerys = countProducts[1].Total;
            }

            //best selling book right now
            var bestSellingBook = await GetBestSellingGoods(1, TimeEnum.Week, ProductType.Book);

            if (bestSellingBook.Count != 0)
            {
                statistics.BestSellingBook = new BestSellingProduct
                {
                    Id    = bestSellingBook.FirstOrDefault().Id,
                    Name  = bestSellingBook.FirstOrDefault().Name,
                    Solds = bestSellingBook.FirstOrDefault().TotalSold
                };
            }
            else
            {
                statistics.BestSellingBook = new BestSellingProduct
                {
                    Id    = -1,
                    Name  = "",
                    Solds = 0
                };
            }

            //number of return products: in this week
            var      now = DateTime.Now;
            DateTime start, end;

            start = now.Date.AddDays(-(int)now.DayOfWeek); // prev sunday 00:00
            end   = start.AddDays(7);                      // next sunday 00:00

            var numberOfProductsReturn = await(from ret in _context.PhieuTraHang
                                               where ret.NgayLap >= start && ret.NgayLap < end
                                               join detail in _context.ChiTietPhieuTraHang
                                               on ret.Id equals detail.PhieuTraHangId
                                               select detail).SumAsync(d => d.SoLuong);

            statistics.TotalReturnThisWeek = numberOfProductsReturn;

            return(statistics);
        }