public ActionResult Index() { //Khai báo biến SearchHoaDonBanHang model = new SearchHoaDonBanHang(); CriterialHoaDonBanHang critHoaDonBanHang = new CriterialHoaDonBanHang(); SumHoaDonBanHang sumHoaDonBanHang = new SumHoaDonBanHang(); var hoadonBanHangs = db.HoaDonBanHang.OrderBy(p => p.thoigian).ToList(); if (hoadonBanHangs.Count >= 1) { foreach (var value in hoadonBanHangs) { value.thoigian = value.thoigian.Value.AddHours(7); } } //Default View ViewBag.nguoiban = new SelectList(db.User.OrderBy(p => p.Name), "UserId", "Name"); ViewBag.khachhang = new SelectList(db.KhachHang.OrderBy(p => p.ten), "id", "ten"); //Footer sumHoaDonBanHang.tongTongTien = hoadonBanHangs.Sum(p => p.tongTien); sumHoaDonBanHang.tongGiamGia = hoadonBanHangs.Sum(p => p.giamgia); sumHoaDonBanHang.tongThanhToan = hoadonBanHangs.Sum(p => p.thanhtoan); //Model model.hoadonBanHangs = hoadonBanHangs.ToList(); model.sumHoaDonBanHang = sumHoaDonBanHang; //Export _hoadonBanHangs = model.hoadonBanHangs.ToList(); _sumHoaDonBanHang = model.sumHoaDonBanHang; return(View(model)); }
public ActionResult Index([Bind(Include = "tuNgay,denNgay,nguoiban,khachhang")] CriterialHoaDonBanHang critHoaDonBanHang, FormCollection fc) { //Khai báo biến SearchHoaDonBanHang model = new SearchHoaDonBanHang(); SumHoaDonBanHang sumHoaDonBanHang = new SumHoaDonBanHang(); //Lấy giá trị từ form string khachhang = fc["khachhang"].ToString(); string nguoiban = fc["nguoiban"].ToString(); DateTime?tuNgay; if (critHoaDonBanHang.tuNgay != null) { tuNgay = DateTime.Parse(critHoaDonBanHang.tuNgay.ToString()); } else { tuNgay = db.HoaDonBanHang.OrderBy(p => p.thoigian).Select(p => p.thoigian).FirstOrDefault(); } DateTime?denNgay; if (critHoaDonBanHang.denNgay != null) { denNgay = DateTime.Parse(critHoaDonBanHang.denNgay.ToString()); } else { denNgay = DateTime.Now; } //Truy vấn var hoadonBanHangs = from c in db.HoaDonBanHang select c; { //Lọc theo Từ ngày if (!string.IsNullOrEmpty(tuNgay.ToString())) { tuNgay = tuNgay.Value.AddHours(-7); hoadonBanHangs = hoadonBanHangs.Where(p => p.thoigian >= tuNgay); } //Lọc theo Đến ngày if (!string.IsNullOrEmpty(denNgay.ToString())) { denNgay = denNgay.Value.AddHours(-7); hoadonBanHangs = hoadonBanHangs.Where(p => p.thoigian <= denNgay); } //Lọc theo khách hàng if (!string.IsNullOrEmpty(khachhang.ToString())) { int?temp = int.Parse(khachhang.ToString()); hoadonBanHangs = hoadonBanHangs.Where(p => p.idKhachHang == temp); } //Lọc theo người bán if (!string.IsNullOrEmpty(nguoiban.ToString())) { int?temp = int.Parse(nguoiban.ToString()); hoadonBanHangs = hoadonBanHangs.Where(p => p.idNhanVien == temp); } } if (hoadonBanHangs.ToList().Count >= 1) { foreach (var value in hoadonBanHangs) { value.thoigian = value.thoigian.Value.AddHours(7); } } //Default View ViewBag.nguoiban = new SelectList(db.User.OrderBy(p => p.Name), "UserId", "Name", nguoiban); ViewBag.khachhang = new SelectList(db.KhachHang.OrderBy(p => p.ten), "id", "ten", khachhang); //Foottable sumHoaDonBanHang.tongTongTien = hoadonBanHangs.Sum(p => p.tongTien); sumHoaDonBanHang.tongGiamGia = hoadonBanHangs.Sum(p => p.giamgia); sumHoaDonBanHang.tongThanhToan = hoadonBanHangs.Sum(p => p.thanhtoan); //Model model.hoadonBanHangs = hoadonBanHangs; model.critHoaDonBanHang = critHoaDonBanHang; model.sumHoaDonBanHang = sumHoaDonBanHang; //Export _hoadonBanHangs = model.hoadonBanHangs.ToList(); _sumHoaDonBanHang = model.sumHoaDonBanHang; return(View(model)); }