public ActionResult ChiTietTonKho(int id,int? hienthi, int? page)
        {
            List<TonKhoViewModel> dstonkho = new List<TonKhoViewModel>();
            var sanphams = db.SanPhams.ToList().OrderByDescending(s => s.IdSanPham);
            foreach (var sanpham in sanphams)
            {
                if (sanpham.ListTonKho.Count > 0)
                {
                    foreach (var item in sanpham.ListTonKho)
                    {
                        TonKhoViewModel tonkho = new TonKhoViewModel();
                        tonkho.ThoiGian = String.Format("{0:dd/mm/yyyy hh:mm:ss}",item.ThoiGian);
                        tonkho.SanPham = sanpham;
                        tonkho.TonDau = item.SoLuong-item.Nhap+item.Xuat;
                        tonkho.TonCuoi = item.SoLuong;
                        tonkho.Nhap = item.Nhap;
                        tonkho.Xuat = item.Xuat;
                        dstonkho.Add(tonkho);
                    }
                }
                else
                {
                    TonKhoViewModel tonkho = new TonKhoViewModel();
                    tonkho.SanPham = sanpham;
                    tonkho.TonDau = 0;
                    tonkho.TonCuoi = 0;
                    tonkho.Nhap = 0;
                    tonkho.Xuat = 0;
                    dstonkho.Add(tonkho);
                }

            }
            dstonkho = dstonkho.Where(t => t.SanPham.IdSanPham == id).ToList();
            var pageNumber = page ?? 1;
            ViewBag.id = id;
            ViewBag.tonkhos = dstonkho.ToPagedList(pageNumber, hienthi ?? 10);
            ViewBag.HienThi = hienthi;
            return View();
        }
 public ActionResult TonKho(string from,string to, int? hienthi, int? page)
 {
     DateTime FromDate = new DateTime();
     DateTime ToDate = new DateTime();
     if (!String.IsNullOrEmpty(from) && String.IsNullOrEmpty(to))
     {
         FromDate = DateTime.ParseExact(from, "dd/MM/yyyy", null);
         ToDate = System.DateTime.Now;
         ViewBag.FromDate = from;
     }
     if (String.IsNullOrEmpty(from) && !String.IsNullOrEmpty(to))
     {
         ToDate = DateTime.ParseExact(to, "dd/MM/yyyy", null);
         FromDate = new DateTime(2013, 01, 01);
         ViewBag.ToDate = to;
     }
     if (!String.IsNullOrEmpty(from) && !String.IsNullOrEmpty(to))
     {
         ToDate = DateTime.ParseExact(to, "dd/MM/yyyy", null);
         FromDate = DateTime.ParseExact(from, "dd/MM/yyyy", null);
         ViewBag.FromDate = from;
         ViewBag.ToDate = to;
     }
     if (String.IsNullOrEmpty(from) && String.IsNullOrEmpty(to))
     {
         ToDate = System.DateTime.Now;
         FromDate = new DateTime(2013,01,01);
         ViewBag.FromDate = from;
         ViewBag.ToDate = to;
     }
     List<TonKhoViewModel> tonkhos = new List<TonKhoViewModel>();
     var sanphams = db.SanPhams.ToList().OrderByDescending(s => s.IdSanPham);
     foreach (var sanpham in sanphams)
     {
         TonKhoViewModel tonkho = new TonKhoViewModel();
         tonkho.SanPham = sanpham;
         if (sanpham.ListTonKho.Count > 0)
         {
             List<TonKho> temp = sanpham.ListTonKho;
             sanpham.ListTonKho = sanpham.ListTonKho.Where(t => t.ThoiGian >= FromDate && t.ThoiGian < ToDate).ToList();
             if (sanpham.ListTonKho.Count <= 0)
             {
                 TonKho tk = temp.Where(t => t.ThoiGian < FromDate).ToList().OrderByDescending(t => t.Id).First();
                 tonkho.TonDau = tk.SoLuong;
                 tonkho.TonCuoi = tk.SoLuong;
                 tonkho.Nhap = 0;
                 tonkho.Xuat = 0;
             }
             else
             {
                 tonkho.TonDau = sanpham.ListTonKho.First().SoLuong - sanpham.ListTonKho.First().Nhap + sanpham.ListTonKho.First().Xuat;
                 tonkho.TonCuoi = sanpham.ListTonKho.OrderByDescending(t => t.Id).First().SoLuong;
                 tonkho.Nhap = (sanpham.ListTonKho.Select(t => t.Nhap)).Sum();
                 tonkho.Xuat = (sanpham.ListTonKho.Select(t => t.Xuat)).Sum();
             }
         }
         else
         {
             tonkho.TonDau = 0;
             tonkho.TonCuoi = 0;
             tonkho.Nhap = 0;
             tonkho.Xuat = 0;
         }
         tonkhos.Add(tonkho);
     }
     var pageNumber = page ?? 1;
     ViewBag.tonkhos = tonkhos.ToPagedList(pageNumber, hienthi ?? 10);
     ViewBag.HienThi = hienthi ?? 10;
     return View();
 }