예제 #1
0
        public ActionResult Search(int?duAnId, int?nhanVienSalesId)
        {
            var duAns         = new List <DuAn>();
            var nhanVienSales = new List <NhanVien>();
            var phiHoaHongs   = new List <PhiHoaHong>();

            if (User.IsInRole(RoleName.SalesMan))
            {
                var salesManAccountId = User.Identity.GetUserId();
                var salesMan          = _context.NhanViens.SingleOrDefault(x => x.Account.Id == salesManAccountId);
                phiHoaHongs = _context.PhiHoaHongs.Include(x => x.PhanCongSales).Include(x => x.PhanCongSales.NhanVienSales)
                              .Include(X => X.PhanCongSales.NhaDat.DuAn).Include(X => X.PhanCongSales.NhaDat.LoaiNhaDat)
                              .Where(x => x.PhanCongSales.NhanVienSales.Id == salesMan.Id).ToList();
                foreach (var item in phiHoaHongs)
                {
                    duAns.Add(item.PhanCongSales.NhaDat.DuAn);
                }
                nhanVienSales.Add(salesMan);
            }
            else
            {
                var salesRole = _context.Roles.Single(x => x.Name == RoleName.SalesMan);
                nhanVienSales = _context.NhanViens.Where(x => x.Account.Roles.FirstOrDefault().RoleId == salesRole.Id).ToList();
                duAns         = _context.DuAns.ToList();
                phiHoaHongs   = _context.PhiHoaHongs.Include(x => x.PhanCongSales).Include(x => x.PhanCongSales.NhanVienSales)
                                .Include(X => X.PhanCongSales.NhaDat.DuAn).Include(x => x.PhanCongSales.NhaDat.LoaiNhaDat).ToList();
            }

            var viewModel = new QuanLyPhiHoaHongViewModel
            {
                PhiHoaHongs   = phiHoaHongs,
                DuAns         = duAns,
                NhanVienSales = nhanVienSales
            };

            if (duAnId.HasValue)
            {
                viewModel.DuAnId      = duAnId.Value;
                viewModel.PhiHoaHongs = viewModel.PhiHoaHongs.Where(x => x.PhanCongSales.NhaDat.DuAn.Id == duAnId.Value);
            }

            if (nhanVienSalesId.HasValue)
            {
                viewModel.NhanVienSalesId = nhanVienSalesId.Value;
                viewModel.PhiHoaHongs     = viewModel.PhiHoaHongs.Where(x => x.PhanCongSales.NhanVienSalesId == nhanVienSalesId.Value);
            }

            return(View("Index", viewModel));
        }
예제 #2
0
        // GET: Admin/QuanLyPhiHoaHong
        public ActionResult Index()
        {
            if (TempData["success"] != null)
            {
                ViewBag.Success = TempData["success"].ToString();
                TempData.Remove("success");
            }
            if (TempData["error"] != null)
            {
                ViewBag.Error = TempData["error"].ToString();
                TempData.Remove("error");
            }

            var duAns         = new List <DuAn>();
            var nhanVienSales = new List <NhanVien>();
            var phiHoaHongs   = new List <PhiHoaHong>();

            if (User.IsInRole(RoleName.SalesMan))
            {
                var salesManAccountId = User.Identity.GetUserId();
                var salesMan          = _context.NhanViens.SingleOrDefault(x => x.Account.Id == salesManAccountId);
                phiHoaHongs = _context.PhiHoaHongs.Include(x => x.PhanCongSales).Include(x => x.PhanCongSales.NhanVienSales)
                              .Include(X => X.PhanCongSales.NhaDat.DuAn).Include(x => x.PhanCongSales.NhaDat.LoaiNhaDat)
                              .Where(x => x.PhanCongSales.NhanVienSales.Id == salesMan.Id).ToList();
                foreach (var item in phiHoaHongs)
                {
                    duAns.Add(item.PhanCongSales.NhaDat.DuAn);
                }
                nhanVienSales.Add(salesMan);
            }
            else
            {
                var salesRole = _context.Roles.Single(x => x.Name == RoleName.SalesMan);
                nhanVienSales = _context.NhanViens.Where(x => x.Account.Roles.FirstOrDefault().RoleId == salesRole.Id).ToList();
                duAns         = _context.DuAns.ToList();
                phiHoaHongs   = _context.PhiHoaHongs.Include(x => x.PhanCongSales).Include(x => x.PhanCongSales.NhanVienSales)
                                .Include(X => X.PhanCongSales.NhaDat.DuAn).Include(x => x.PhanCongSales.NhaDat.LoaiNhaDat).ToList();
            }

            var viewModel = new QuanLyPhiHoaHongViewModel
            {
                PhiHoaHongs   = phiHoaHongs,
                DuAns         = duAns,
                NhanVienSales = nhanVienSales
            };

            return(View(viewModel));
        }