public ActionResult Create([Bind(Include = "MaDHN,MaSP,SoLuong,TrangThai,isXoa")] CHITIETDONHANGNHAP cHITIETDONHANGNHAP)
        {
            if (ModelState.IsValid)
            {
                db.CHITIETDONHANGNHAPs.Add(cHITIETDONHANGNHAP);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cHITIETDONHANGNHAP));
        }
        // GET: CHITIETDONHANGNHAPs/Delete/5
        public ActionResult Delete(int?id, int?MaSP)
        {
            CHITIETDONHANGNHAP cHITIETDONHANGNHAP = db.CHITIETDONHANGNHAPs.Where(x => x.MaDHN == id && x.MaSP == MaSP).First();

            if (cHITIETDONHANGNHAP == null)
            {
                return(HttpNotFound());
            }
            cHITIETDONHANGNHAP.isXoa           = true;
            db.Entry(cHITIETDONHANGNHAP).State = EntityState.Modified;
            db.SaveChanges();
            return(RedirectToAction("Details", "DonHangNhaps", new { id = cHITIETDONHANGNHAP.MaDHN }));
        }
        // GET: CHITIETDONHANGNHAPs/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CHITIETDONHANGNHAP cHITIETDONHANGNHAP = db.CHITIETDONHANGNHAPs.Find(id);

            if (cHITIETDONHANGNHAP == null)
            {
                return(HttpNotFound());
            }
            return(View(cHITIETDONHANGNHAP));
        }
        // GET: CHITIETDONHANGNHAPs/Edit/5
        public ActionResult Edit(int?id, int?MaSP)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CHITIETDONHANGNHAP cHITIETDONHANGNHAP = db.CHITIETDONHANGNHAPs.Where(x => x.MaDHN == id && x.MaSP == MaSP).First();

            if (cHITIETDONHANGNHAP == null)
            {
                return(HttpNotFound());
            }
            ViewBag.TenSanPham = db.SANPHAMs.Where(x => x.isXoa == false && cHITIETDONHANGNHAP.MaSP == x.MaSP).First().TenSP;
            return(View(cHITIETDONHANGNHAP));
        }
        public ActionResult Edit([Bind(Include = "MaDHN,MaSP,SoLuong,TrangThai,isXoa")] CHITIETDONHANGNHAP cHITIETDONHANGNHAP)
        {
            string str = "";

            if (ModelState.IsValid)
            {
                CHITIETDONHANGNHAP ct = db.CHITIETDONHANGNHAPs.AsNoTracking().First(x => x.MaSP == cHITIETDONHANGNHAP.MaSP && x.MaDHN == cHITIETDONHANGNHAP.MaDHN);
                string             sp = db.SANPHAMs.AsNoTracking().FirstOrDefault(x => x.MaSP == cHITIETDONHANGNHAP.MaSP).TenSP;
                str += "<b>Sản Phẩm: </b>" + sp + "</br>";
                str += "<b>Số Lượng: </b>" + ct.SoLuong + " => " + cHITIETDONHANGNHAP.SoLuong + "</br>";
                str += "<b>Trạng Thái: </b>" + ct.TrangThai + " => " + cHITIETDONHANGNHAP.TrangThai + "</br>";
                cHITIETDONHANGNHAP.isXoa           = false;
                db.Entry(cHITIETDONHANGNHAP).State = EntityState.Modified;
                db.SaveChanges();
                ghiLog.AddNewGhiLog("Sửa", Session["username"] + "", str, "Chi Tiết Đơn Hàng Nhập");

                return(RedirectToAction("Details", "DonHangNhaps", new { id = cHITIETDONHANGNHAP.MaDHN }));
            }
            return(View(cHITIETDONHANGNHAP));
        }
        public ActionResult Create([Bind(Include = "MaDHN,MaNhaCC,NgayNhap,MaNV,isXoa,MaSP,SoLuong,ThoiGian,PhuongTien")] DONHANGNHAP dONHANGNHAP)
        {
            if (ModelState.IsValid)
            {
                dONHANGNHAP.isXoa = false;
                db.DONHANGNHAPs.Add(dONHANGNHAP);
                db.SaveChanges();
                int id = dONHANGNHAP.MaDHN;

                string ncc = db.NHACUNGCAPs.AsNoTracking().FirstOrDefault(x => x.MaNCC == dONHANGNHAP.MaNhaCC).TenNhaCC;
                string nv  = db.NHANVIENs.AsNoTracking().FirstOrDefault(x => x.MaNV == dONHANGNHAP.MaNV).Hoten;
                string str = "<b>Mã DHN:</b> " + id + "</br>";
                str += "<b>Nhà Cung Cấp:</b> " + ncc + "</br>";
                str += "<b>Ngày Nhập:</b> " + Convert.ToDateTime(dONHANGNHAP.NgayNhap).ToString("dd/MM/yyyy") + "</br>";
                str += "<b>Nhân Viên:</b> " + nv + "</br>";
                str += "------------------</br>";
                foreach (string maSP in dONHANGNHAP.MaSP)
                {
                    CHITIETDONHANGNHAP ct = new CHITIETDONHANGNHAP();
                    int    masp           = int.Parse(maSP);
                    string sp             = db.SANPHAMs.AsNoTracking().FirstOrDefault(x => x.MaSP == masp).TenSP;
                    ct.MaDHN     = id;
                    ct.MaSP      = masp;
                    ct.SoLuong   = dONHANGNHAP.SoLuong;
                    ct.TrangThai = null;
                    ct.isXoa     = false;
                    db.CHITIETDONHANGNHAPs.Add(ct);
                    db.SaveChanges();
                    str += "<b>Sản Phẩm:</b> " + sp + "</br>";
                    str += "<b>Số Lượng:</b> " + dONHANGNHAP.SoLuong + "</br>";
                    str += "------------------</br>";
                }
                ghiLog.AddNewGhiLog("Thêm mới", Session["username"] + "", str, "Đơn Hàng Nhập");
                return(RedirectToAction("Index"));
            }

            ViewBag.NhanVien = db.NHANVIENs.Where(x => x.isXoa == false).ToList();
            ViewBag.ListNCC  = db.NHACUNGCAPs.Where(x => x.isXoa == false).ToList();
            ViewBag.ListSP   = db.SANPHAMs.Where(x => x.isXoa == false).ToList();
            return(View(dONHANGNHAP));
        }
        public ActionResult Edit([Bind(Include = "MaDHN,MaNhaCC,NgayNhap,MaNV,isXoa,MaSP,SoLuong,ThoiGian,PhuongTien")] DONHANGNHAP dONHANGNHAP)
        {
            if (ModelState.IsValid)
            {
                string      str   = "<b>Mã DHX: </b>" + dONHANGNHAP.MaDHN + "</br>";
                DONHANGNHAP dhnCu = db.DONHANGNHAPs.AsNoTracking().FirstOrDefault(x => x.MaDHN == dONHANGNHAP.MaDHN);
                if (dhnCu.NgayNhap != dONHANGNHAP.NgayNhap)
                {
                    str += "<b>Ngày Nhập: </b>" + dhnCu.NgayNhap + " => " + dONHANGNHAP.NgayNhap + "</br>";
                }

                if (dhnCu.MaNhaCC != dONHANGNHAP.MaNhaCC)
                {
                    string nccCu  = db.NHACUNGCAPs.AsNoTracking().FirstOrDefault(x => x.MaNCC == dhnCu.MaNhaCC).TenNhaCC;
                    string nccMoi = db.NHACUNGCAPs.AsNoTracking().FirstOrDefault(x => x.MaNCC == dONHANGNHAP.MaNhaCC).TenNhaCC;
                    str += "<b>Nhà Cung Cấp: </b>" + nccCu + " => " + nccMoi + "</br>";
                }

                if (dhnCu.MaNV != dONHANGNHAP.MaNV)
                {
                    string nvCu  = db.NHANVIENs.AsNoTracking().FirstOrDefault(x => x.MaNV == dhnCu.MaNV).Hoten;
                    string nvMoi = db.NHANVIENs.AsNoTracking().FirstOrDefault(x => x.MaNV == dONHANGNHAP.MaNV).Hoten;
                    str += "<b>Nhân Viên: </b>" + nvCu + " => " + nvMoi + "</br>";
                }

                if (dhnCu.ThoiGian != dONHANGNHAP.ThoiGian)
                {
                    str += "<b>Thời Gian: </b>" + dhnCu.ThoiGian + " => " + dONHANGNHAP.ThoiGian + "</br>";
                }

                if (dhnCu.PhuongTien != dONHANGNHAP.PhuongTien)
                {
                    str += "<b>Phương Tiện: </b>" + dhnCu.PhuongTien + " => " + dONHANGNHAP.PhuongTien + "</br>";
                }
                dONHANGNHAP.isXoa           = false;
                db.Entry(dONHANGNHAP).State = EntityState.Modified;
                db.SaveChanges();
                str += "------------------</br>";

                List <CHITIETDONHANGNHAP> ct = db.CHITIETDONHANGNHAPs.Where(x => x.MaDHN == dONHANGNHAP.MaDHN).ToList();

                List <string> maSpDelete = ct.Select(x => x.MaSP + "").ToArray().Except(dONHANGNHAP.MaSP).ToList();
                foreach (string item in maSpDelete)
                {
                    CHITIETDONHANGNHAP itemDelete = db.CHITIETDONHANGNHAPs.Where(x => x.MaDHN == dONHANGNHAP.MaDHN && x.MaSP + "" == item).First();
                    if (itemDelete != null && !(bool)itemDelete.isXoa)
                    {
                        itemDelete.isXoa           = true;
                        db.Entry(itemDelete).State = EntityState.Modified;
                        db.SaveChanges();
                        string tensp = db.SANPHAMs.AsNoTracking().FirstOrDefault(x => x.MaSP + "" == item).TenSP;
                        str += "Sản Phẩm <b>" + tensp + "</b> Đã bị xóa.</br>";
                    }
                }

                string addNewstr = "--------Thêm Mới---------</br>";
                string restoreSP = "--------Sửa----------</br>";

                foreach (string msp in dONHANGNHAP.MaSP)
                {
                    int id = int.Parse(msp);
                    CHITIETDONHANGNHAP addNew = db.CHITIETDONHANGNHAPs.FirstOrDefault(x => x.MaDHN == dONHANGNHAP.MaDHN && x.MaSP == id);
                    string             sp     = db.SANPHAMs.AsNoTracking().FirstOrDefault(x => x.MaSP == id).TenSP;

                    if (addNew == null)
                    {
                        addNew           = new CHITIETDONHANGNHAP();
                        addNew.isXoa     = false;
                        addNew.MaDHN     = dONHANGNHAP.MaDHN;
                        addNew.MaSP      = id;
                        addNew.TrangThai = null;
                        addNew.SoLuong   = dONHANGNHAP.SoLuong;
                        db.CHITIETDONHANGNHAPs.Add(addNew);
                        db.SaveChanges();

                        addNewstr += "<b>Sản Phẩm:</b> " + sp + "</br>";
                        addNewstr += "<b>Số Lượng:</b> " + dONHANGNHAP.SoLuong + "</br>";
                        addNewstr += "------------------</br>";
                    }
                    else
                    {
                        if ((bool)addNew.isXoa)
                        {
                            addNew.isXoa = false;
                        }
                        addNew.SoLuong         = dONHANGNHAP.SoLuong;
                        db.Entry(addNew).State = EntityState.Modified;
                        db.SaveChanges();
                        restoreSP += "<b>Sản Phẩm:</b> " + sp + "</br>";
                        restoreSP += "<b>Số Lượng:</b> " + addNew.SoLuong + " => " + dONHANGNHAP.SoLuong + "</br>";
                    }
                }
                ghiLog.AddNewGhiLog("Sửa", Session["username"] + "", str + addNewstr + restoreSP, "Đơn Hàng Nhập");
                return(RedirectToAction("Index"));
            }
            ViewBag.NhanVien = db.NHANVIENs.Where(x => x.isXoa == false).ToList();
            ViewBag.ListNCC  = db.NHACUNGCAPs.Where(x => x.isXoa == false).ToList();
            ViewBag.ListSP   = db.SANPHAMs.Where(x => x.isXoa == false).ToList();
            return(View(dONHANGNHAP));
        }